Skip to content

Commit

Permalink
Merge branch 'master' into group_abundance
Browse files Browse the repository at this point in the history
Former-commit-id: 451eb4b9338e477f18944867fba3b231f8714d79
  • Loading branch information
grst committed Feb 14, 2020
2 parents 90929e0 + 4e1c70f commit c0f0785
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.vscode
notebooks
tutorial/TraCeR
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"restructuredtext.confPath": "${workspaceFolder}/docs",
"editor.rulers": [88]
}
23 changes: 14 additions & 9 deletions sctcrpy/_io/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,20 @@ def _process_chains(chains, chain_type):
):
cell_name = summary_file.split(os.sep)[-3]
tcr_obj = TcrCell(cell_name)
with open(summary_file, "rb") as f:
tracer_obj = pickle.load(f)
chains = tracer_obj.recombinants["TCR"]
if "A" in chains:
for tmp_chain in _process_chains(chains["A"], "TRA"):
tcr_obj.add_chain(tmp_chain)
if "B" in chains:
for tmp_chain in _process_chains(chains["B"], "TRB"):
tcr_obj.add_chain(tmp_chain)
try:
with open(summary_file, "rb") as f:
tracer_obj = pickle.load(f)
chains = tracer_obj.recombinants["TCR"]
if "A" in chains and chains["A"] is not None:
for tmp_chain in _process_chains(chains["A"], "TRA"):
tcr_obj.add_chain(tmp_chain)
if "B" in chains and chains["B"] is not None:
for tmp_chain in _process_chains(chains["B"], "TRB"):
tcr_obj.add_chain(tmp_chain)
except Exception as e:
raise Exception(
"Error loading TCR data from cell {}".format(summary_file)
) from e

tcr_objs[cell_name] = tcr_obj

Expand Down
18 changes: 14 additions & 4 deletions sctcrpy/_tools/_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@


def define_clonotypes(
adata: AnnData, *, inplace: bool = True, key_added: str = "clonotype"
adata: AnnData,
*,
flavor: Literal["all_chains", "primary_only"] = "all_chains",
inplace: bool = True,
key_added: str = "clonotype",
) -> Union[None, np.ndarray]:
"""Define clonotypes based on CDR3 region.
Expand All @@ -20,6 +24,10 @@ def define_clonotypes(
----------
adata
Annotated data matrix
flavor
Biological model to define clonotypes.
`all_chains`: All four chains of a cell in a clonotype need to be the same.
`primary_only`: Only primary alpha and beta chain need to be the same.
inplace
If True, adds a column to adata.obs
key_added
Expand All @@ -32,12 +40,14 @@ def define_clonotypes(
or adds a `clonotype` column to `adata`.
"""
groupby_cols = {
"all_chains": ["TRA_1_cdr3", "TRB_1_cdr3", "TRA_2_cdr3", "TRA_2_cdr3"],
"primary_only": ["TRA_1_cdr3", "TRB_1_cdr3"],
}
clonotype_col = np.array(
[
"clonotype_{}".format(x)
for x in adata.obs.groupby(
["TRA_1_cdr3", "TRB_1_cdr3", "TRA_2_cdr3", "TRA_2_cdr3"]
).ngroup()
for x in adata.obs.groupby(groupby_cols[flavor]).ngroup()
]
)
clonotype_col[
Expand Down
10 changes: 10 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ def test_define_clonotypes():
["clonotype_1", np.nan, "clonotype_1", "clonotype_0", "clonotype_2"],
)

res_primary_only = st.tl.define_clonotypes(
adata, flavor="primary_only", inplace=False
)
npt.assert_equal(
# order is by alphabet: BBBB < nan
# we don't care about the order of numbers, so this is ok.
res_primary_only,
["clonotype_0", np.nan, "clonotype_0", "clonotype_0", "clonotype_1"],
)

# test inplace
st.tl.define_clonotypes(adata, key_added="clonotype_")
npt.assert_equal(res, adata.obs["clonotype_"].values)
Expand Down

0 comments on commit c0f0785

Please sign in to comment.