clone2vec 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.
- clone2vec/__init__.py +23 -0
- clone2vec/associations.py +1117 -0
- clone2vec/catboost.py +622 -0
- clone2vec/datasets.py +94 -0
- clone2vec/eigenvalues.py +187 -0
- clone2vec/embeddings.py +1204 -0
- clone2vec/integration.py +511 -0
- clone2vec/plotting.py +3540 -0
- clone2vec/preprocessing.py +660 -0
- clone2vec/seurat.py +720 -0
- clone2vec/tools.py +633 -0
- clone2vec/utils.py +618 -0
- clone2vec-0.1.0.dist-info/METADATA +86 -0
- clone2vec-0.1.0.dist-info/RECORD +16 -0
- clone2vec-0.1.0.dist-info/WHEEL +5 -0
- clone2vec-0.1.0.dist-info/top_level.txt +1 -0
clone2vec/datasets.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from scanpy import read, AnnData
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
zenodo_record_lt = "15334396"
|
|
7
|
+
|
|
8
|
+
def Weinreb_in_vitro(
|
|
9
|
+
file_path: str | Path = "data/Weinreb_in_vitro.h5ad",
|
|
10
|
+
) -> AnnData:
|
|
11
|
+
"""
|
|
12
|
+
Dataset from [PMID: 31974159] with in vitro hematopoiesis.
|
|
13
|
+
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
file_path : str | Path, optional
|
|
17
|
+
Path where .h5ad-container will be stored, by default "data/Weinreb_in_vitro.h5ad".
|
|
18
|
+
|
|
19
|
+
Returns
|
|
20
|
+
-------
|
|
21
|
+
AnnData
|
|
22
|
+
Annotated data matrix with the dataset.
|
|
23
|
+
"""
|
|
24
|
+
url = f"https://zenodo.org/records/{zenodo_record_lt}/files/Weinreb_in_vitro.h5ad"
|
|
25
|
+
|
|
26
|
+
adata = read(file_path, backup_url=url, sparse=True, cache=True)
|
|
27
|
+
return adata
|
|
28
|
+
|
|
29
|
+
def Erickson_murine_development(
|
|
30
|
+
perturbed: bool = False,
|
|
31
|
+
region: str = "trunk",
|
|
32
|
+
subset: str = "all",
|
|
33
|
+
file_path: str | Path | None = None,
|
|
34
|
+
) -> AnnData:
|
|
35
|
+
"""
|
|
36
|
+
Dataset from [PMID: 40502176] with clonal atlas of murine development.
|
|
37
|
+
|
|
38
|
+
Parameters
|
|
39
|
+
----------
|
|
40
|
+
perturbed : bool, optional
|
|
41
|
+
If the dataset should contain also experiments with mosaic knockouts,
|
|
42
|
+
by default False.
|
|
43
|
+
region : str, optional
|
|
44
|
+
Region of interest, one of `trunk` or `head`, by default "trunk".
|
|
45
|
+
subset : str, optional
|
|
46
|
+
Subset of interest (`all`, `neurons`, `mesenchyme` or `other`), by default "all".
|
|
47
|
+
file_path : str | Path | None, optional
|
|
48
|
+
Path where .h5ad-container will be stored, by default None.
|
|
49
|
+
|
|
50
|
+
Returns
|
|
51
|
+
-------
|
|
52
|
+
AnnData
|
|
53
|
+
Annotated data matrix with the dataset
|
|
54
|
+
"""
|
|
55
|
+
datasets = {
|
|
56
|
+
False: {
|
|
57
|
+
"trunk": {
|
|
58
|
+
"all": "Erickson_Trunk_Control_All.h5ad",
|
|
59
|
+
"mesenchyme": "Erickson_Trunk_Control_Mesenchyme.h5ad",
|
|
60
|
+
"neurons": "Erickson_Trunk_Control_Neurons.h5ad",
|
|
61
|
+
"other": "Erickson_Trunk_Control_Other.h5ad",
|
|
62
|
+
},
|
|
63
|
+
"head": {
|
|
64
|
+
"all": "Erickson_Head_Control_All.h5ad",
|
|
65
|
+
"mesenchyme": "Erickson_Head_Control_Mesenchyme.h5ad",
|
|
66
|
+
"neurons": "Erickson_Head_Control_Neurons.h5ad",
|
|
67
|
+
"other": "Erickson_Head_Control_Other.h5ad",
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
True: {
|
|
71
|
+
"trunk": {
|
|
72
|
+
"all": "Erickson_Trunk_Perturb_All.h5ad",
|
|
73
|
+
"mesenchyme": "Erickson_Trunk_Perturb_Mesenchyme.h5ad",
|
|
74
|
+
"neurons": "Erickson_Trunk_Perturb_Neurons.h5ad",
|
|
75
|
+
"other": "Erickson_Trunk_Perturb_Other.h5ad",
|
|
76
|
+
},
|
|
77
|
+
"head": {
|
|
78
|
+
"all": "Erickson_Head_Perturb_All.h5ad",
|
|
79
|
+
"mesenchyme": "Erickson_Head_Perturb_Mesenchyme.h5ad",
|
|
80
|
+
"neurons": "Erickson_Head_Perturb_Neurons.h5ad",
|
|
81
|
+
"other": "Erickson_Head_Perturb_Other.h5ad",
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
dataset = datasets[perturbed][region][subset]
|
|
87
|
+
|
|
88
|
+
if file_path is None:
|
|
89
|
+
file_path = f"data/{dataset}"
|
|
90
|
+
|
|
91
|
+
url = f"https://zenodo.org/records/{zenodo_record_lt}/files/{dataset}"
|
|
92
|
+
|
|
93
|
+
adata = read(file_path, backup_url=url, sparse=True, cache=True)
|
|
94
|
+
return adata
|
clone2vec/eigenvalues.py
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import scanpy as sc
|
|
4
|
+
import numpy as np
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from scipy import linalg, interpolate
|
|
8
|
+
from contextlib import nullcontext
|
|
9
|
+
from tqdm import tqdm
|
|
10
|
+
|
|
11
|
+
logg = sc.logging
|
|
12
|
+
|
|
13
|
+
tw_cdf_tabular = {
|
|
14
|
+
-9.0: 0.000000e+00,
|
|
15
|
+
-8.0: 0.000000e+00,
|
|
16
|
+
-7.0: 0.000000e+00,
|
|
17
|
+
-6.0: 1.28e-09,
|
|
18
|
+
-5.0: 1.59e-06,
|
|
19
|
+
-4.0: 0.00134,
|
|
20
|
+
-3.5: 0.00603,
|
|
21
|
+
-3.0: 0.01616,
|
|
22
|
+
-2.5: 0.05590,
|
|
23
|
+
-2.0: 0.14932,
|
|
24
|
+
-1.5: 0.30771,
|
|
25
|
+
-1.0: 0.51085,
|
|
26
|
+
-0.5: 0.70612,
|
|
27
|
+
0.0: 0.85172,
|
|
28
|
+
0.5: 0.93722,
|
|
29
|
+
1.0: 0.97720,
|
|
30
|
+
1.5: 0.99284,
|
|
31
|
+
2.0: 0.99802,
|
|
32
|
+
2.5: 0.99951,
|
|
33
|
+
3.0: 0.99989,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
def _tracy_widom_cdf(x: float) -> float:
|
|
37
|
+
"""
|
|
38
|
+
Computes the CDF of the Tracy-Widom distribution (with beta = 1).
|
|
39
|
+
|
|
40
|
+
Parameters
|
|
41
|
+
----------
|
|
42
|
+
x : float
|
|
43
|
+
Value at which to evaluate the CDF.
|
|
44
|
+
|
|
45
|
+
Returns
|
|
46
|
+
-------
|
|
47
|
+
float
|
|
48
|
+
The value of the CDF at x.
|
|
49
|
+
"""
|
|
50
|
+
if x > 3:
|
|
51
|
+
tail_prob = np.exp(-2.0/3.0 * x**1.5) / (4.0 * np.sqrt(np.pi) * x**1.5)
|
|
52
|
+
return 1.0 - tail_prob
|
|
53
|
+
elif x < -10:
|
|
54
|
+
return 0
|
|
55
|
+
else:
|
|
56
|
+
tw_x = np.array(list(tw_cdf_tabular.keys()))
|
|
57
|
+
tw_cdf = np.array(list(tw_cdf_tabular.values()))
|
|
58
|
+
interp = interpolate.interp1d(
|
|
59
|
+
tw_x,
|
|
60
|
+
tw_cdf,
|
|
61
|
+
kind="cubic",
|
|
62
|
+
bounds_error=False,
|
|
63
|
+
fill_value=(0.0, 1.0),
|
|
64
|
+
)
|
|
65
|
+
val = interp(x)
|
|
66
|
+
return float(np.clip(val, 0.0, 1.0))
|
|
67
|
+
|
|
68
|
+
def _calculate_statistics(X: np.ndarray) -> float:
|
|
69
|
+
"""
|
|
70
|
+
Calculates the test statistic for the Tracy-Widom distribution.
|
|
71
|
+
|
|
72
|
+
Parameters
|
|
73
|
+
----------
|
|
74
|
+
X : np.ndarray
|
|
75
|
+
Matrix of shape (n, p) containing the data.
|
|
76
|
+
|
|
77
|
+
Returns
|
|
78
|
+
-------
|
|
79
|
+
float
|
|
80
|
+
The value of the test statistic.
|
|
81
|
+
"""
|
|
82
|
+
n, p = X.shape
|
|
83
|
+
X_centered = (X - np.mean(X, axis=0)) / np.std(X, axis=0)
|
|
84
|
+
n_dof = n - 1
|
|
85
|
+
|
|
86
|
+
s = linalg.svdvals(X_centered)
|
|
87
|
+
eigenvalues = s ** 2
|
|
88
|
+
lambda_1 = eigenvalues[0]
|
|
89
|
+
sigma_sq_est = np.mean(eigenvalues[1:]) / n_dof
|
|
90
|
+
|
|
91
|
+
mu_np = (np.sqrt(n_dof) + np.sqrt(p)) ** 2
|
|
92
|
+
sigma_np = (np.sqrt(n_dof) + np.sqrt(p)) * \
|
|
93
|
+
((1 / np.sqrt(n_dof)) + (1 / np.sqrt(p))) ** (1/3)
|
|
94
|
+
|
|
95
|
+
stat = ((lambda_1 / sigma_sq_est) - mu_np) / sigma_np
|
|
96
|
+
return stat
|
|
97
|
+
|
|
98
|
+
def eigenvalue_test(
|
|
99
|
+
adata: sc.AnnData | np.ndarray,
|
|
100
|
+
key: str | None = None,
|
|
101
|
+
key_added: str = "eigenvalues_test",
|
|
102
|
+
flavor: Literal["asymptotic", "synthetic"] = "synthetic",
|
|
103
|
+
n_simulations: int = 10000,
|
|
104
|
+
progress_bar: bool = True,
|
|
105
|
+
null_distribution: np.ndarray | None = None,
|
|
106
|
+
):
|
|
107
|
+
"""
|
|
108
|
+
Performs Johnstone’s Spiked Covariance Test to identify if the embedding is random.
|
|
109
|
+
|
|
110
|
+
Parameters
|
|
111
|
+
----------
|
|
112
|
+
adata : sc.AnnData | np.ndarray
|
|
113
|
+
AnnData object or numpy array containing the data.
|
|
114
|
+
key : str | None
|
|
115
|
+
Key to use for AnnData input.
|
|
116
|
+
key_added : str
|
|
117
|
+
Key to use for adding the results to the AnnData object.
|
|
118
|
+
flavor : Literal["asymptotic", "synthetic"]
|
|
119
|
+
Flavor of the test to use.
|
|
120
|
+
n_simulations : int
|
|
121
|
+
Number of simulations to use for the synthetic approach.
|
|
122
|
+
progress_bar : bool
|
|
123
|
+
Whether to show a progress bar.
|
|
124
|
+
null_distribution : np.ndarray | None
|
|
125
|
+
Null distribution to use for the synthetic approach.
|
|
126
|
+
|
|
127
|
+
Returns
|
|
128
|
+
-------
|
|
129
|
+
dict
|
|
130
|
+
Dictionary containing the test statistic and p-value.
|
|
131
|
+
"""
|
|
132
|
+
start = logg.info(f"computing eigenvalues test via {flavor} approach")
|
|
133
|
+
|
|
134
|
+
if isinstance(adata, sc.AnnData):
|
|
135
|
+
if key is None:
|
|
136
|
+
raise ValueError("key must be specified for AnnData input")
|
|
137
|
+
X = adata.obsm[key]
|
|
138
|
+
elif isinstance(adata, np.ndarray):
|
|
139
|
+
X = adata
|
|
140
|
+
key_added = None
|
|
141
|
+
else:
|
|
142
|
+
raise ValueError("adata must be AnnData or numpy.ndarray")
|
|
143
|
+
|
|
144
|
+
if sc.settings.verbosity.value >= 2:
|
|
145
|
+
prefix = " "
|
|
146
|
+
progress_bar = True
|
|
147
|
+
else:
|
|
148
|
+
prefix = ""
|
|
149
|
+
|
|
150
|
+
stat = _calculate_statistics(X)
|
|
151
|
+
if flavor == "asymptotic":
|
|
152
|
+
p_value = 1.0 - _tracy_widom_cdf(stat)
|
|
153
|
+
elif flavor == "synthetic":
|
|
154
|
+
if null_distribution is None:
|
|
155
|
+
null_distribution = []
|
|
156
|
+
n, p = X.shape
|
|
157
|
+
cm = tqdm(
|
|
158
|
+
range(n_simulations),
|
|
159
|
+
desc=prefix + f"generating null distribution ({n_simulations} simulations)",
|
|
160
|
+
file=sys.stdout,
|
|
161
|
+
) if progress_bar else nullcontext(range(n_simulations))
|
|
162
|
+
with cm:
|
|
163
|
+
for i in cm:
|
|
164
|
+
X_sim = np.random.normal(size=(n, p))
|
|
165
|
+
stat_sim = _calculate_statistics(X_sim)
|
|
166
|
+
null_distribution.append(stat_sim)
|
|
167
|
+
null_distribution = np.array(null_distribution)
|
|
168
|
+
p_value = np.mean(null_distribution >= stat)
|
|
169
|
+
if p_value == 0:
|
|
170
|
+
p_value = f"<{1 / n_simulations}"
|
|
171
|
+
else:
|
|
172
|
+
raise ValueError("flavor must be 'asymptotic' or 'synthetic'")
|
|
173
|
+
|
|
174
|
+
if key_added:
|
|
175
|
+
lines = [
|
|
176
|
+
"added",
|
|
177
|
+
f" .uns['{key_added}'] eigenvalues test statistics and approximate p-value",
|
|
178
|
+
]
|
|
179
|
+
logg.info(" finished ({time_passed})", deep="\n".join([l for l in lines if l is not None]), time=start)
|
|
180
|
+
adata.uns[key_added] = {
|
|
181
|
+
"stat": stat,
|
|
182
|
+
"p_value": p_value,
|
|
183
|
+
"null_distribution": null_distribution,
|
|
184
|
+
}
|
|
185
|
+
else:
|
|
186
|
+
logg.info(" finished ({time_passed})", time=start)
|
|
187
|
+
return {"stat": stat, "p_value": p_value, "null_distribution": null_distribution}
|