nova-clone 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nova_clone/__init__.py +1 -0
- nova_clone/algorithm/__init__.py +2 -0
- nova_clone/algorithm/_build_intbc_graph.py +135 -0
- nova_clone/algorithm/_call_clones_from_coresets.py +235 -0
- nova_clone/algorithm/_clone_call_ops.py +258 -0
- nova_clone/algorithm/_clone_calling_algorithm.py +1571 -0
- nova_clone/algorithm/_compute_cellbc_vs_coreset_overlap_matrix.py +143 -0
- nova_clone/algorithm/_compute_coreset_co_assignment_matrix.py +170 -0
- nova_clone/algorithm/_find_maximal_coresets.py +428 -0
- nova_clone/algorithm/_keep_reliable_edges.py +202 -0
- nova_clone/algorithm/_make_coresets_disjoint.py +114 -0
- nova_clone/algorithm/_prune_coresets.py +164 -0
- nova_clone/algorithm/_prune_custom_intbcs.py +150 -0
- nova_clone/algorithm/_prune_likely_homoplastic_intbcs.py +162 -0
- nova_clone/algorithm/_prune_likely_sequencing_errors.py +201 -0
- nova_clone/algorithm/_prune_low_cellbc_count_intbcs.py +154 -0
- nova_clone/algorithm/_prune_low_umi_count_intbcs.py +152 -0
- nova_clone/algorithm/_types.py +150 -0
- nova_clone/algorithm/_utils.py +53 -0
- nova_clone/algorithm/cassiopeia/__init__.py +1 -0
- nova_clone/algorithm/cassiopeia/_call_lineage_groups.py +270 -0
- nova_clone/caching/__init__.py +24 -0
- nova_clone/caching/_cached.py +175 -0
- nova_clone/caching/_cached_computation.py +369 -0
- nova_clone/caching/_cached_parallel_computation.py +428 -0
- nova_clone/caching/_common.py +200 -0
- nova_clone/data/__init__.py +1 -0
- nova_clone/qc/__init__.py +179 -0
- nova_clone/qc/_clone_intbc_sets.py +234 -0
- nova_clone/qc/_compare_clone_calls.py +218 -0
- nova_clone/qc/_data_retention.py +175 -0
- nova_clone/qc/_homoplasy.py +81 -0
- nova_clone/qc/_inter_clonal_kinship.py +60 -0
- nova_clone/qc/_intra_clonal_kinship.py +172 -0
- nova_clone/qc/_multiplet_rate.py +158 -0
- nova_clone/qc/_number_of_clones.py +153 -0
- nova_clone/qc/_pre_qc.py +485 -0
- nova_clone/simulation/HDBSCAN_method_adjusted.py +68 -0
- nova_clone/simulation/__init__.py +0 -0
- nova_clone/simulation/_caching.py +40 -0
- nova_clone/simulation/benchmarking.py +1221 -0
- nova_clone/simulation/clone_to_sequences.py +408 -0
- nova_clone/simulation/creating_tree.py +416 -0
- nova_clone/simulation/evaluation.py +344 -0
- nova_clone/simulation/jaccard_hierarchical_by_intBC.py +78 -0
- nova_clone/simulation/plot_benchmark_lines.py +226 -0
- nova_clone/simulation/slice_benchmark.py +640 -0
- nova_clone/simulation/slice_benchmark_clone_calling.py +68 -0
- nova_clone-0.1.0.dist-info/METADATA +298 -0
- nova_clone-0.1.0.dist-info/RECORD +53 -0
- nova_clone-0.1.0.dist-info/WHEEL +5 -0
- nova_clone-0.1.0.dist-info/licenses/LICENSE +28 -0
- nova_clone-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Module for removing homoplastic intBCs from the coresets, thus making then disjoint.
|
|
3
|
+
|
|
4
|
+
The main function exposed by this module is `make_coresets_disjoint`.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
import sys
|
|
9
|
+
import time
|
|
10
|
+
|
|
11
|
+
from multiset import Multiset
|
|
12
|
+
|
|
13
|
+
from ._types import *
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _init_logger():
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
logger.setLevel(logging.INFO)
|
|
19
|
+
fmt_str = "[%(asctime)s] - %(name)s - %(levelname)s - %(message)s"
|
|
20
|
+
formatter = logging.Formatter(fmt_str)
|
|
21
|
+
|
|
22
|
+
consoleHandler = logging.StreamHandler(sys.stdout)
|
|
23
|
+
consoleHandler.setFormatter(formatter)
|
|
24
|
+
logger.addHandler(consoleHandler)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
_init_logger()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class MakeCoresetsDisjointReturnType(NamedTuple):
|
|
31
|
+
"""
|
|
32
|
+
Object returned by the `make_coresets_disjoint` function.
|
|
33
|
+
|
|
34
|
+
Attributes:
|
|
35
|
+
coresets: List of coresets after removing homopalstic intBCs.
|
|
36
|
+
total_time: Total time taken.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
coresets: List[Coreset[IntBCType]]
|
|
40
|
+
total_time: float
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def make_coresets_disjoint(
|
|
44
|
+
coresets: List[Coreset[IntBCType]],
|
|
45
|
+
) -> MakeCoresetsDisjointReturnType:
|
|
46
|
+
"""
|
|
47
|
+
Elements that appear in more than one coreset are removed from all the
|
|
48
|
+
coresets.
|
|
49
|
+
"""
|
|
50
|
+
st = time.time()
|
|
51
|
+
logger = logging.getLogger(__name__)
|
|
52
|
+
logger.info("Going to make_coresets_disjoint ...")
|
|
53
|
+
intbc_counts = defaultdict(int)
|
|
54
|
+
for coreset in coresets:
|
|
55
|
+
for intbc in set(coreset): # Use set(coreset) to handle multiset coresets, otherwise single-intBC coresets will get killed by `make_coresets_disjoint`.
|
|
56
|
+
intbc_counts[intbc] += 1
|
|
57
|
+
disjoint_coresets = []
|
|
58
|
+
homoplastic_intbcs = [intbc for intbc, cnt in intbc_counts.items() if cnt > 1]
|
|
59
|
+
for coreset in coresets:
|
|
60
|
+
disjoint_coreset = Multiset()
|
|
61
|
+
for intbc in coreset:
|
|
62
|
+
cnt = intbc_counts[intbc]
|
|
63
|
+
assert cnt >= 1
|
|
64
|
+
if cnt == 1:
|
|
65
|
+
disjoint_coreset.add(intbc)
|
|
66
|
+
disjoint_coresets.append(disjoint_coreset)
|
|
67
|
+
total_time = time.time() - st
|
|
68
|
+
res = MakeCoresetsDisjointReturnType(
|
|
69
|
+
coresets=disjoint_coresets,
|
|
70
|
+
total_time=total_time,
|
|
71
|
+
)
|
|
72
|
+
logger.info(
|
|
73
|
+
f"Found {len(homoplastic_intbcs)} homoplastic intBCs while making the coresets disjoint. Total time: {total_time}"
|
|
74
|
+
)
|
|
75
|
+
return res
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_make_coresets_disjoint():
|
|
79
|
+
coresets = [
|
|
80
|
+
{"1", "2", "3"},
|
|
81
|
+
{"3", "4", "5", "6"},
|
|
82
|
+
{"6"},
|
|
83
|
+
{"7"},
|
|
84
|
+
]
|
|
85
|
+
coresets_disjoint = make_coresets_disjoint(
|
|
86
|
+
coresets=coresets,
|
|
87
|
+
).coresets
|
|
88
|
+
assert coresets_disjoint == [
|
|
89
|
+
{"1", "2"},
|
|
90
|
+
{"4", "5"},
|
|
91
|
+
set(),
|
|
92
|
+
{"7"},
|
|
93
|
+
]
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_make_coresets_disjoint_with_multisets():
|
|
97
|
+
coresets = [
|
|
98
|
+
{"1", "2", "3"},
|
|
99
|
+
{"3", "4", "5", "6"},
|
|
100
|
+
{"6"},
|
|
101
|
+
{"7"},
|
|
102
|
+
Multiset(["8"]) + Multiset(["8"]),
|
|
103
|
+
]
|
|
104
|
+
coresets_disjoint = make_coresets_disjoint(
|
|
105
|
+
coresets=coresets,
|
|
106
|
+
).coresets
|
|
107
|
+
print(f"coresets_disjoint = {coresets_disjoint}")
|
|
108
|
+
assert coresets_disjoint == [
|
|
109
|
+
{"1", "2"},
|
|
110
|
+
{"4", "5"},
|
|
111
|
+
set(),
|
|
112
|
+
{"7"},
|
|
113
|
+
Multiset(["8"]) + Multiset(["8"]),
|
|
114
|
+
]
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Module for pruning aliased coresets.
|
|
3
|
+
|
|
4
|
+
The main function exposed by this module is `prune_coresets`.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
import sys
|
|
9
|
+
import time
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
from ._types import *
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _init_logger():
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
logger.setLevel(logging.INFO)
|
|
19
|
+
fmt_str = "[%(asctime)s] - %(name)s - %(levelname)s - %(message)s"
|
|
20
|
+
formatter = logging.Formatter(fmt_str)
|
|
21
|
+
|
|
22
|
+
consoleHandler = logging.StreamHandler(sys.stdout)
|
|
23
|
+
consoleHandler.setFormatter(formatter)
|
|
24
|
+
logger.addHandler(consoleHandler)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
_init_logger()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class PruneCoresetsReturnType(NamedTuple):
|
|
31
|
+
"""
|
|
32
|
+
Object returned by the `prune_coresets` function.
|
|
33
|
+
|
|
34
|
+
Attributes:
|
|
35
|
+
coresets: List of coresets after pruning aliased coresets.
|
|
36
|
+
total_time: Total time taken.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
coresets: pd.DataFrame
|
|
40
|
+
total_time: float
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def prune_coresets(
|
|
44
|
+
coresets: List[Coreset[IntBCType]],
|
|
45
|
+
coreset_co_assignment_matrix: np.array,
|
|
46
|
+
co_occurrence_cutoff: float,
|
|
47
|
+
min_cells_assigned: int,
|
|
48
|
+
) -> List[Coreset[IntBCType]]:
|
|
49
|
+
"""
|
|
50
|
+
Prune coresets.
|
|
51
|
+
|
|
52
|
+
A coreset is pruned if:
|
|
53
|
+
- It co-occurs (in the sense of p(y|x)) with another strictly larger coreset
|
|
54
|
+
(in the sense of number of cells assigned), or of same size but smaller
|
|
55
|
+
index. The coreset is pruned if p(larger coreset | coreset) >
|
|
56
|
+
co_occurrence_cutoff and said size condition (with index tiebreaking) is
|
|
57
|
+
met.
|
|
58
|
+
- It is too small, in the sense that the number of (putative) cells assigned
|
|
59
|
+
to it is < min_cells_assigned.
|
|
60
|
+
|
|
61
|
+
Returns the new list of coresets.
|
|
62
|
+
"""
|
|
63
|
+
st = time.time()
|
|
64
|
+
logger = logging.getLogger(__name__)
|
|
65
|
+
logger.info("Going to prune_coresets ...")
|
|
66
|
+
num_coresets = coreset_co_assignment_matrix.shape[0]
|
|
67
|
+
res = []
|
|
68
|
+
pruned_because_too_small = 0
|
|
69
|
+
pruned_because_aliased = 0
|
|
70
|
+
for coreset_idx_1 in range(num_coresets):
|
|
71
|
+
# Decide whether we keep coreset_idx_1
|
|
72
|
+
keep = True
|
|
73
|
+
c_1 = coreset_co_assignment_matrix[coreset_idx_1, coreset_idx_1]
|
|
74
|
+
if c_1 < min_cells_assigned:
|
|
75
|
+
keep = False # We don't keep.
|
|
76
|
+
pruned_because_too_small += 1
|
|
77
|
+
else:
|
|
78
|
+
for coreset_idx_2 in range(num_coresets):
|
|
79
|
+
if coreset_idx_1 == coreset_idx_2:
|
|
80
|
+
continue
|
|
81
|
+
c_2 = coreset_co_assignment_matrix[coreset_idx_2, coreset_idx_2]
|
|
82
|
+
c_12 = coreset_co_assignment_matrix[coreset_idx_1, coreset_idx_2]
|
|
83
|
+
co_occurrence = c_12 / c_1
|
|
84
|
+
if co_occurrence > co_occurrence_cutoff and (
|
|
85
|
+
(c_1 < c_2) or ((c_1 == c_2) and (coreset_idx_1 > coreset_idx_2))
|
|
86
|
+
):
|
|
87
|
+
keep = False
|
|
88
|
+
pruned_because_aliased += 1
|
|
89
|
+
if keep:
|
|
90
|
+
res.append(coresets[coreset_idx_1])
|
|
91
|
+
total_time = time.time() - st
|
|
92
|
+
logger.info(
|
|
93
|
+
f"Pruned {num_coresets - len(res)} coresets ({pruned_because_too_small} too small, {pruned_because_aliased} aliased). There remain {len(res)} coresets. Total time: {total_time}"
|
|
94
|
+
)
|
|
95
|
+
res = PruneCoresetsReturnType(
|
|
96
|
+
coresets=res,
|
|
97
|
+
total_time=total_time,
|
|
98
|
+
)
|
|
99
|
+
return res
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def test_prune_coresets():
|
|
103
|
+
coresets = [
|
|
104
|
+
{"1", "2", "3"},
|
|
105
|
+
{"4", "5", "6"},
|
|
106
|
+
{"7", "8"},
|
|
107
|
+
{"9"},
|
|
108
|
+
]
|
|
109
|
+
coreset_co_assignment_matrix = np.array(
|
|
110
|
+
[
|
|
111
|
+
[10, 2, 4, 1],
|
|
112
|
+
[2, 10, 1, 2],
|
|
113
|
+
[4, 1, 5, 0],
|
|
114
|
+
[1, 2, 1, 3],
|
|
115
|
+
]
|
|
116
|
+
)
|
|
117
|
+
pruned_coresets = prune_coresets(
|
|
118
|
+
coresets=coresets,
|
|
119
|
+
coreset_co_assignment_matrix=coreset_co_assignment_matrix,
|
|
120
|
+
co_occurrence_cutoff=0.9,
|
|
121
|
+
min_cells_assigned=0,
|
|
122
|
+
).coresets
|
|
123
|
+
assert pruned_coresets == coresets
|
|
124
|
+
pruned_coresets = prune_coresets(
|
|
125
|
+
coresets=coresets,
|
|
126
|
+
coreset_co_assignment_matrix=coreset_co_assignment_matrix,
|
|
127
|
+
co_occurrence_cutoff=0.7,
|
|
128
|
+
min_cells_assigned=0,
|
|
129
|
+
).coresets
|
|
130
|
+
assert pruned_coresets == [
|
|
131
|
+
{"1", "2", "3"},
|
|
132
|
+
{"4", "5", "6"},
|
|
133
|
+
{"9"},
|
|
134
|
+
]
|
|
135
|
+
pruned_coresets = prune_coresets(
|
|
136
|
+
coresets=coresets,
|
|
137
|
+
coreset_co_assignment_matrix=coreset_co_assignment_matrix,
|
|
138
|
+
co_occurrence_cutoff=0.6,
|
|
139
|
+
min_cells_assigned=0,
|
|
140
|
+
).coresets
|
|
141
|
+
assert pruned_coresets == [
|
|
142
|
+
{"1", "2", "3"},
|
|
143
|
+
{"4", "5", "6"},
|
|
144
|
+
]
|
|
145
|
+
pruned_coresets = prune_coresets(
|
|
146
|
+
coresets=coresets,
|
|
147
|
+
coreset_co_assignment_matrix=coreset_co_assignment_matrix,
|
|
148
|
+
co_occurrence_cutoff=0.7,
|
|
149
|
+
min_cells_assigned=4,
|
|
150
|
+
).coresets
|
|
151
|
+
assert pruned_coresets == [
|
|
152
|
+
{"1", "2", "3"},
|
|
153
|
+
{"4", "5", "6"},
|
|
154
|
+
]
|
|
155
|
+
pruned_coresets = prune_coresets(
|
|
156
|
+
coresets=coresets,
|
|
157
|
+
coreset_co_assignment_matrix=coreset_co_assignment_matrix,
|
|
158
|
+
co_occurrence_cutoff=1.0,
|
|
159
|
+
min_cells_assigned=6,
|
|
160
|
+
).coresets
|
|
161
|
+
assert pruned_coresets == [
|
|
162
|
+
{"1", "2", "3"},
|
|
163
|
+
{"4", "5", "6"},
|
|
164
|
+
]
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module implements filtering of intBCs based on a custom function.
|
|
3
|
+
|
|
4
|
+
The main function exposed by this module is `prune_custom_intbcs`.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
import sys
|
|
9
|
+
import time
|
|
10
|
+
from typing import Callable
|
|
11
|
+
|
|
12
|
+
from ._types import *
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _init_logger():
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
logger.setLevel(logging.INFO)
|
|
18
|
+
fmt_str = "[%(asctime)s] - %(name)s - %(levelname)s - %(message)s"
|
|
19
|
+
formatter = logging.Formatter(fmt_str)
|
|
20
|
+
|
|
21
|
+
consoleHandler = logging.StreamHandler(sys.stdout)
|
|
22
|
+
consoleHandler.setFormatter(formatter)
|
|
23
|
+
logger.addHandler(consoleHandler)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
_init_logger()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class PruneCustomIntBCsReturnType(NamedTuple):
|
|
30
|
+
"""
|
|
31
|
+
Object returned by the prune_custom_intbcs function.
|
|
32
|
+
|
|
33
|
+
Attributes:
|
|
34
|
+
intbc_table_per_sample: The filtered `intbc_table_per_sample`,
|
|
35
|
+
containing only the high-quality intBCs meeting the
|
|
36
|
+
`min_intbc_umi_count`
|
|
37
|
+
fraction_discarded_intbcs: What fraction of intBCs (i.e. rows in the
|
|
38
|
+
intBC tables) were discarded by the
|
|
39
|
+
`custom_intbc_filtering_function`.
|
|
40
|
+
total_time: Total time taken.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
intbc_table_per_sample: Dict[SampleNameType, IntBCTableType]
|
|
44
|
+
fraction_discarded_intbcs: float
|
|
45
|
+
total_time: float
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def prune_custom_intbcs(
|
|
49
|
+
intbc_table_per_sample: Dict[SampleNameType, IntBCTableType],
|
|
50
|
+
custom_intbc_filtering_function: Callable[[str], bool],
|
|
51
|
+
) -> PruneCustomIntBCsReturnType:
|
|
52
|
+
"""
|
|
53
|
+
Prunes away all intBCs for which `custom_intbc_filtering_function` is True.
|
|
54
|
+
|
|
55
|
+
The purpose of this function is to get rid of extraneous intBC sequences,
|
|
56
|
+
e.g. sequences that are too short or contain ambiguous bases such as 'N'.
|
|
57
|
+
"""
|
|
58
|
+
st = time.time()
|
|
59
|
+
logger = logging.getLogger(__name__)
|
|
60
|
+
logger.info("Going to prune_custom_intbcs ...")
|
|
61
|
+
validate_intbc_table_per_sample(intbc_table_per_sample)
|
|
62
|
+
new_intbc_table_per_sample = {}
|
|
63
|
+
for sample_name, intbc_table in intbc_table_per_sample.items():
|
|
64
|
+
validate_intbc_table(intbc_table)
|
|
65
|
+
mask = ~intbc_table["intBC"].apply(custom_intbc_filtering_function)
|
|
66
|
+
new_intbc_table = intbc_table.loc[mask].reset_index(drop=True)
|
|
67
|
+
validate_intbc_table(new_intbc_table)
|
|
68
|
+
new_intbc_table_per_sample[sample_name] = new_intbc_table
|
|
69
|
+
denominator = sum(
|
|
70
|
+
intbc_table.shape[0] for intbc_table in intbc_table_per_sample.values()
|
|
71
|
+
)
|
|
72
|
+
numerator = denominator - sum(
|
|
73
|
+
intbc_table.shape[0] for intbc_table in new_intbc_table_per_sample.values()
|
|
74
|
+
)
|
|
75
|
+
fraction_discarded_intbcs = numerator / (denominator + 1e-9)
|
|
76
|
+
validate_intbc_table_per_sample(new_intbc_table_per_sample)
|
|
77
|
+
total_time = time.time() - st
|
|
78
|
+
res = PruneCustomIntBCsReturnType(
|
|
79
|
+
intbc_table_per_sample=new_intbc_table_per_sample,
|
|
80
|
+
fraction_discarded_intbcs=fraction_discarded_intbcs,
|
|
81
|
+
total_time=total_time,
|
|
82
|
+
)
|
|
83
|
+
logger.info(
|
|
84
|
+
f"Pruned {100.0 * fraction_discarded_intbcs:.1f}% = {numerator} / {denominator} rows from the intBC tables. [Lower is better.]"
|
|
85
|
+
)
|
|
86
|
+
logger.info(f"Done with prune_custom_intbcs! Total time: {total_time}")
|
|
87
|
+
return res
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_prune_custom_intbcs():
|
|
91
|
+
intbc_table_1 = pd.DataFrame(
|
|
92
|
+
data=[
|
|
93
|
+
["cellBC_1", "AAA", 40],
|
|
94
|
+
["cellBC_1", "BBB", 40],
|
|
95
|
+
["cellBC_2", "AAA", 40],
|
|
96
|
+
["cellBC_2", "BBB", 40],
|
|
97
|
+
["cellBC_2", "CCC", 40],
|
|
98
|
+
["cellBC_3", "DDD", 40],
|
|
99
|
+
["cellBC_3", "EEE", 40],
|
|
100
|
+
["cellBC_3", "AA", 40],
|
|
101
|
+
["cellBC_3", "NNN", 40],
|
|
102
|
+
],
|
|
103
|
+
columns=["cellBC", "intBC", "UMI_count"],
|
|
104
|
+
)
|
|
105
|
+
intbc_table_2 = pd.DataFrame(
|
|
106
|
+
data=[
|
|
107
|
+
["cellBC_1", "NNN", 40],
|
|
108
|
+
["cellBC_1", "A", 40],
|
|
109
|
+
["cellBC_1", "FFF", 40],
|
|
110
|
+
["cellBC_2", "GGG", 40],
|
|
111
|
+
],
|
|
112
|
+
columns=["cellBC", "intBC", "UMI_count"],
|
|
113
|
+
)
|
|
114
|
+
res = prune_custom_intbcs(
|
|
115
|
+
intbc_table_per_sample={
|
|
116
|
+
"sample1": intbc_table_1,
|
|
117
|
+
"sample2": intbc_table_2,
|
|
118
|
+
},
|
|
119
|
+
custom_intbc_filtering_function=lambda intbc: len(intbc) <= 2 or "N" in intbc,
|
|
120
|
+
)
|
|
121
|
+
filtered_intbc_table_per_sample = res.intbc_table_per_sample
|
|
122
|
+
expected_intbc_table_per_sample = {
|
|
123
|
+
"sample1": pd.DataFrame(
|
|
124
|
+
data=[
|
|
125
|
+
["cellBC_1", "AAA", 40],
|
|
126
|
+
["cellBC_1", "BBB", 40],
|
|
127
|
+
["cellBC_2", "AAA", 40],
|
|
128
|
+
["cellBC_2", "BBB", 40],
|
|
129
|
+
["cellBC_2", "CCC", 40],
|
|
130
|
+
["cellBC_3", "DDD", 40],
|
|
131
|
+
["cellBC_3", "EEE", 40],
|
|
132
|
+
],
|
|
133
|
+
columns=["cellBC", "intBC", "UMI_count"],
|
|
134
|
+
),
|
|
135
|
+
"sample2": pd.DataFrame(
|
|
136
|
+
data=[
|
|
137
|
+
["cellBC_1", "FFF", 40],
|
|
138
|
+
["cellBC_2", "GGG", 40],
|
|
139
|
+
],
|
|
140
|
+
columns=["cellBC", "intBC", "UMI_count"],
|
|
141
|
+
),
|
|
142
|
+
}
|
|
143
|
+
assert (
|
|
144
|
+
filtered_intbc_table_per_sample.keys() == expected_intbc_table_per_sample.keys()
|
|
145
|
+
)
|
|
146
|
+
for sample in filtered_intbc_table_per_sample.keys():
|
|
147
|
+
pd.testing.assert_frame_equal(
|
|
148
|
+
filtered_intbc_table_per_sample[sample],
|
|
149
|
+
expected_intbc_table_per_sample[sample],
|
|
150
|
+
)
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module implements filtering of intBCs which are likely homoplastic (i.e.
|
|
3
|
+
appearing in more than one clone).
|
|
4
|
+
|
|
5
|
+
The main function exposed by this module is `prune_likely_homoplastic_intbcs`.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import logging
|
|
9
|
+
import sys
|
|
10
|
+
import time
|
|
11
|
+
|
|
12
|
+
from ._types import *
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _init_logger():
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
logger.setLevel(logging.INFO)
|
|
18
|
+
fmt_str = "[%(asctime)s] - %(name)s - %(levelname)s - %(message)s"
|
|
19
|
+
formatter = logging.Formatter(fmt_str)
|
|
20
|
+
|
|
21
|
+
consoleHandler = logging.StreamHandler(sys.stdout)
|
|
22
|
+
consoleHandler.setFormatter(formatter)
|
|
23
|
+
logger.addHandler(consoleHandler)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
_init_logger()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class PruneLikelyHomoplasticIntBCsReturnType(NamedTuple):
|
|
30
|
+
"""
|
|
31
|
+
Object returned by the prune_likely_homoplastic_intbcs function.
|
|
32
|
+
|
|
33
|
+
Attributes:
|
|
34
|
+
intbc_graph: the new graph after pruning away the homoplastic intBCs
|
|
35
|
+
found.
|
|
36
|
+
homoplastic_intbcs_filtered: The list of intBCs discovered to be likely
|
|
37
|
+
homoplastic.
|
|
38
|
+
percent_homoplastic_intbcs_found: Percent of intBCs filtered this way.
|
|
39
|
+
total_time: Total time taken.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
intbc_graph: GraphType
|
|
43
|
+
homoplastic_intbcs_filtered: List[IntBCType]
|
|
44
|
+
percent_homoplastic_intbcs_found: float
|
|
45
|
+
total_time: float
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def prune_likely_homoplastic_intbcs(
|
|
49
|
+
intbc_graph: GraphType,
|
|
50
|
+
p_max_cutoff: float,
|
|
51
|
+
) -> PruneLikelyHomoplasticIntBCsReturnType:
|
|
52
|
+
"""
|
|
53
|
+
Prune homoplastic intBCs.
|
|
54
|
+
|
|
55
|
+
IntBCs with small p_max are likely homoplastic. Here p_max is:
|
|
56
|
+
p_max(x) = max_y p(y | x) = max_y c(x, y) / c(x).
|
|
57
|
+
The comparisson is with STRICT inqequality, therefore using
|
|
58
|
+
`p_max_cutoff = 0.0` bypasses this filter.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
intbc_graph: The graph to prune homoplastic intBCs from.
|
|
62
|
+
p_max_cutoff: IntBCs with p_max lower than this will be pruned.
|
|
63
|
+
"""
|
|
64
|
+
st = time.time()
|
|
65
|
+
logger = logging.getLogger(__name__)
|
|
66
|
+
logger.info("Going to prune_likely_homoplastic_intbcs ...")
|
|
67
|
+
intbcs_to_prune = []
|
|
68
|
+
edge_counts, intbc_counts = intbc_graph
|
|
69
|
+
for intbc, counts in intbc_counts.items():
|
|
70
|
+
# Decide if prune this intbc.
|
|
71
|
+
p_max = max(
|
|
72
|
+
[co_counts / counts for neighbor, co_counts in edge_counts[intbc].items()]
|
|
73
|
+
+ [0.0] # In case it is an isolated intBC.
|
|
74
|
+
)
|
|
75
|
+
if p_max < p_max_cutoff:
|
|
76
|
+
intbcs_to_prune.append(intbc)
|
|
77
|
+
pruned_graph = prune_nodes_from_graph(
|
|
78
|
+
graph=intbc_graph,
|
|
79
|
+
nodes_to_prune=intbcs_to_prune,
|
|
80
|
+
)
|
|
81
|
+
percent_homoplastic_intbcs_found = len(intbcs_to_prune) / (
|
|
82
|
+
len(intbc_graph[1]) + 1e-8
|
|
83
|
+
)
|
|
84
|
+
total_time = time.time() - st
|
|
85
|
+
res = PruneLikelyHomoplasticIntBCsReturnType(
|
|
86
|
+
intbc_graph=pruned_graph,
|
|
87
|
+
homoplastic_intbcs_filtered=intbcs_to_prune,
|
|
88
|
+
percent_homoplastic_intbcs_found=percent_homoplastic_intbcs_found,
|
|
89
|
+
total_time=total_time,
|
|
90
|
+
)
|
|
91
|
+
edge_counts, node_counts = pruned_graph
|
|
92
|
+
num_nodes = len(node_counts)
|
|
93
|
+
num_edges = sum([len(x) for x in edge_counts.values()]) // 2
|
|
94
|
+
logger.info(
|
|
95
|
+
f"Pruned {len(intbcs_to_prune)} possibly homoplastic intBCs. Pruned IntBC graph has {num_nodes} nodes and {num_edges} edges. Density level: {100.0 * num_edges / (num_nodes * (num_nodes - 1.0) / 2.0 + 1e-9):.2f}%"
|
|
96
|
+
)
|
|
97
|
+
logger.info(f"Done with prune_likely_homoplastic_intbcs! Total time: {total_time}")
|
|
98
|
+
return res
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def test_prune_likely_homoplastic_intbcs():
|
|
102
|
+
graph = (
|
|
103
|
+
{
|
|
104
|
+
"1": {"2": 10, "3": 10, "4": 10},
|
|
105
|
+
"2": {"1": 10},
|
|
106
|
+
"3": {"1": 10},
|
|
107
|
+
"4": {"1": 10},
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
"1": 40,
|
|
111
|
+
"2": 10,
|
|
112
|
+
"3": 10,
|
|
113
|
+
"4": 10,
|
|
114
|
+
},
|
|
115
|
+
)
|
|
116
|
+
pruned_graph = prune_likely_homoplastic_intbcs(
|
|
117
|
+
intbc_graph=graph,
|
|
118
|
+
p_max_cutoff=0.0,
|
|
119
|
+
).intbc_graph
|
|
120
|
+
assert pruned_graph == graph
|
|
121
|
+
pruned_graph = prune_likely_homoplastic_intbcs(
|
|
122
|
+
intbc_graph=graph,
|
|
123
|
+
p_max_cutoff=0.24,
|
|
124
|
+
).intbc_graph
|
|
125
|
+
assert pruned_graph == graph
|
|
126
|
+
pruned_graph = prune_likely_homoplastic_intbcs(
|
|
127
|
+
intbc_graph=graph,
|
|
128
|
+
p_max_cutoff=0.26,
|
|
129
|
+
).intbc_graph
|
|
130
|
+
assert pruned_graph == (
|
|
131
|
+
{
|
|
132
|
+
"2": {},
|
|
133
|
+
"3": {},
|
|
134
|
+
"4": {},
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"2": 10,
|
|
138
|
+
"3": 10,
|
|
139
|
+
"4": 10,
|
|
140
|
+
},
|
|
141
|
+
)
|
|
142
|
+
pruned_graph = prune_likely_homoplastic_intbcs(
|
|
143
|
+
intbc_graph=graph,
|
|
144
|
+
p_max_cutoff=0.99,
|
|
145
|
+
).intbc_graph
|
|
146
|
+
assert pruned_graph == (
|
|
147
|
+
{
|
|
148
|
+
"2": {},
|
|
149
|
+
"3": {},
|
|
150
|
+
"4": {},
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"2": 10,
|
|
154
|
+
"3": 10,
|
|
155
|
+
"4": 10,
|
|
156
|
+
},
|
|
157
|
+
)
|
|
158
|
+
pruned_graph = prune_likely_homoplastic_intbcs(
|
|
159
|
+
intbc_graph=graph,
|
|
160
|
+
p_max_cutoff=1.1,
|
|
161
|
+
).intbc_graph
|
|
162
|
+
assert pruned_graph == ({}, {})
|