Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
grst committed Mar 28, 2020
1 parent 071758e commit de99aa6
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 99 deletions.
24 changes: 9 additions & 15 deletions scirpy/_plotting/_vdj_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Callable, Union, List, Tuple
import pandas as pd
import numpy as np
from .._util import _normalize_counts


def vdj_usage(
Expand All @@ -17,7 +18,6 @@ def vdj_usage(
"TRB_1_j_gene",
],
fraction: Union[None, str, list, np.ndarray, pd.Series] = None,
size_column: str = "cell_weights",
ax: Union[plt.axes, None] = None,
bar_clip: int = 5,
top_n: Union[None, int] = 10,
Expand All @@ -40,9 +40,6 @@ def vdj_usage(
or an iterable specifying a size factor for each cell. By default, each cell count as 1,
but due to normalization to different sample sizes for example, it is possible that one cell
in a small sample is weighted more than a cell in a large sample.
size_column
The name of the column that will be used for storing cell weights. This value is used internally
and should be matched with the column name used by the tool function. Best left untouched.
ax
Custom axis if needed.
bar_clip
Expand All @@ -61,10 +58,7 @@ def vdj_usage(
Axes object.
"""

# Execute the tool
df = tl.vdj_usage(
adata, target_cols=target_cols, fraction=fraction, size_column=size_column
)
df = adata.obs.assign(cell_weights=_normalize_counts(adata.obs, fraction))

if top_n is None:
top_n = df.shape[0]
Expand All @@ -76,12 +70,12 @@ def vdj_usage(
for i in range(len(target_cols)):
td = (
df.groupby(target_cols[i])
.agg({size_column: "sum"})
.sort_values(by=size_column, ascending=False)
.agg({"cell_weights": "sum"})
.sort_values(by="cell_weights", ascending=False)
.reset_index()
)
genes = td[target_cols[i]].tolist()
td = td[size_column]
td = td["cell_weights"]
sector = target_cols[i][2:7]
# sector = sector.replace('_', '')
unct = td[bar_clip + 1 :,].sum()
Expand Down Expand Up @@ -119,18 +113,18 @@ def vdj_usage(
pass

# Count occurance of individual VDJ combinations
td = df.loc[:, target_cols + [size_column]]
td = df.loc[:, target_cols + ["cell_weights"]]
td["genecombination"] = td.apply(
lambda x, y: "|".join([x[e] for e in y]), y=target_cols, axis=1
)
td = (
td.groupby("genecombination")
.agg({size_column: "sum"})
.sort_values(by=size_column, ascending=False)
.agg({"cell_weights": "sum"})
.sort_values(by="cell_weights", ascending=False)
.reset_index()
)
td["genecombination"] = td.apply(
lambda x: [x[size_column]] + x["genecombination"].split("|"), axis=1
lambda x: [x["cell_weights"]] + x["genecombination"].split("|"), axis=1
)

# Draw ribbons
Expand Down
1 change: 0 additions & 1 deletion scirpy/_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from ._diversity import alpha_diversity
from ._group_abundance import group_abundance
from ._spectratype import spectratype
from ._vdj_usage import vdj_usage
from ._clonotypes import (
define_clonotypes,
clonotype_network,
Expand Down
55 changes: 0 additions & 55 deletions scirpy/_tools/_vdj_usage.py

This file was deleted.

28 changes: 0 additions & 28 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,31 +213,3 @@ def test_spectratype(adata_tra):
orient="index",
)
npt.assert_equal(res.values, expected_frac.values)


def test_vdj_usage(adata_vdj):

df = st.tl.vdj_usage(adata_vdj, fraction="sample")

# Check return formats
assert isinstance(df, pd.DataFrame)

# Check normalization
expected_weights = [
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667,
0.06666666666666667
]
npt.assert_equal(df["cell_weights"].tolist(), expected_weights)

0 comments on commit de99aa6

Please sign in to comment.