-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
160 lines (127 loc) · 4.1 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from pathlib import Path
import anndata as ad
import numpy as np
import pandas as pd
import scanpy as sc
import scipy as sp
def get_anndata(path):
X = sp.io.mmread(path / "counts.mtx").tocsr()
observations = pd.read_table(path / "observations.tsv", index_col=0)
features = pd.read_table(path / "features.tsv", index_col=0)
coordinates = (
pd.read_table(path / "coordinates.tsv", index_col=0)
.loc[observations.index, :]
.to_numpy()
)
adata = ad.AnnData(
X=X, obs=observations, var=features, obsm={"spatial": coordinates}
)
return adata
def preprocess_anndata(adata, seed=42, genes=1000, n_pcs=30, min_cells=10):
sc.pp.filter_genes(adata, min_cells=min_cells)
sc.pp.highly_variable_genes(adata, flavor="seurat_v3", n_top_genes=genes)
sc.pp.normalize_total(adata, target_sum=1e4)
sc.pp.log1p(adata)
# sc.pp.scale(adata, zero_center=False)
def process_imagingbased(
path: Path,
seed: int,
msPCA: bool = True,
neighbors="delaunay",
spatial_weight: float = 0.8,
n_pcs: int = 30,
):
import squidpy as sq
from multispaeti import MultispatiPCA
from spatialleiden import distance2connectivity, search_resolution
np.random.seed(seed=seed)
adata = ad.read_h5ad(path)
n_clusters = adata.obs["ground_truth"].nunique()
sc.pp.log1p(adata)
# sc.pp.scale(adata, zero_center=False)
if neighbors == "delaunay":
sq.gr.spatial_neighbors(adata, coord_type="generic", delaunay=True)
directed = False
elif isinstance(neighbors, int):
sq.gr.spatial_neighbors(adata, coord_type="generic", n_neighs=neighbors)
directed = True
else:
raise Exception
adata.obsp["spatial_connectivities"] = distance2connectivity(
adata.obsp["spatial_distances"]
)
if msPCA:
rep = "X_mspca"
adata.obsm[rep] = MultispatiPCA(
n_pcs, connectivity=adata.obsp["spatial_connectivities"]
).fit_transform(adata.X)
else:
sc.pp.pca(adata, n_comps=n_pcs, random_state=seed)
rep = "X_pca"
sc.pp.neighbors(adata, use_rep=rep, random_state=seed)
_ = search_resolution(
adata,
n_clusters,
latent_kwargs={"random_state": seed},
spatial_kwargs={
"layer_ratio": spatial_weight,
"directed": (False, directed),
"seed": seed,
},
)
df = adata.obs[["spatialleiden"]].copy()
df.columns = ["label"]
return df
def process_stereoseq(
path: Path,
seed: int,
SVG: bool,
spatial_weight: float = 0.8,
n_pcs: int = 30,
n_genes: int = 3_000,
n_neighs: int = 4,
n_rings: int = 1,
mspca: bool = True,
):
import squidpy as sq
from multispaeti import MultispatiPCA
from spatialleiden import search_resolution
np.random.seed(seed=seed)
adata = ad.read_h5ad(path)
preprocess_anndata(adata, genes=n_genes, n_pcs=n_pcs, seed=seed)
n_clusters = adata.obs["ground_truth"].nunique()
sq.gr.spatial_neighbors(
adata, coord_type="grid", n_neighs=n_neighs, n_rings=n_rings
)
if SVG:
sq.gr.spatial_autocorr(adata, mode="moran", genes=adata.var_names)
genes = adata.uns["moranI"].nlargest(n_genes, columns="I", keep="all").index
else:
genes = adata.var_names[adata.var["highly_variable"]]
if mspca:
rep = "X_mspca"
adata.obsm[rep] = MultispatiPCA(
n_pcs, connectivity=adata.obsp["spatial_connectivities"]
).fit_transform(adata[:, genes].X.toarray())
else:
sc.pp.pca(
adata,
n_comps=n_pcs,
mask_var=adata.var_names.isin(genes),
random_state=seed,
)
rep = "X_pca"
sc.pp.neighbors(adata, use_rep=rep, random_state=seed)
_ = search_resolution(
adata,
n_clusters,
latent_kwargs={"random_state": seed},
spatial_kwargs={
"layer_ratio": spatial_weight,
"directed": (False, False),
"seed": seed,
},
)
df = adata.obs[["spatialleiden"]].copy()
df.columns = ["label"]
return df