scmcp-shared 0.1.0__py3-none-any.whl → 0.2.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.
- scmcp_shared/__init__.py +1 -1
- scmcp_shared/schema/__init__.py +1 -1
- scmcp_shared/schema/base.py +11 -0
- scmcp_shared/schema/io.py +5 -8
- scmcp_shared/schema/pl.py +3 -3
- scmcp_shared/schema/pp.py +14 -70
- scmcp_shared/schema/tl.py +69 -18
- scmcp_shared/schema/util.py +18 -11
- scmcp_shared/server/__init__.py +51 -1
- scmcp_shared/server/io.py +19 -25
- scmcp_shared/server/pl.py +321 -0
- scmcp_shared/server/pp.py +363 -0
- scmcp_shared/server/tl.py +407 -0
- scmcp_shared/server/util.py +250 -0
- scmcp_shared/util.py +70 -47
- {scmcp_shared-0.1.0.dist-info → scmcp_shared-0.2.0.dist-info}/METADATA +2 -2
- scmcp_shared-0.2.0.dist-info/RECORD +20 -0
- scmcp_shared-0.1.0.dist-info/RECORD +0 -15
- {scmcp_shared-0.1.0.dist-info → scmcp_shared-0.2.0.dist-info}/WHEEL +0 -0
- {scmcp_shared-0.1.0.dist-info → scmcp_shared-0.2.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,407 @@
|
|
1
|
+
from fastmcp import FastMCP, Context
|
2
|
+
import os
|
3
|
+
import scanpy as sc
|
4
|
+
from ..schema.tl import *
|
5
|
+
from scmcp_shared.util import filter_args, add_op_log, forward_request, get_ads, generate_msg
|
6
|
+
from scmcp_shared.logging_config import setup_logger
|
7
|
+
logger = setup_logger()
|
8
|
+
|
9
|
+
tl_mcp = FastMCP("ScanpyMCP-TL-Server")
|
10
|
+
|
11
|
+
|
12
|
+
@tl_mcp.tool()
|
13
|
+
async def tsne(
|
14
|
+
request: TSNEModel = TSNEModel()
|
15
|
+
):
|
16
|
+
"""t-distributed stochastic neighborhood embedding (t-SNE) for visualization"""
|
17
|
+
|
18
|
+
try:
|
19
|
+
result = await forward_request("tl_tsne", request)
|
20
|
+
if result is not None:
|
21
|
+
return result
|
22
|
+
func_kwargs = filter_args(request, sc.tl.tsne)
|
23
|
+
ads = get_ads()
|
24
|
+
adata = ads.get_adata(request=request)
|
25
|
+
sc.tl.tsne(adata, **func_kwargs)
|
26
|
+
add_op_log(adata, sc.tl.tsne, func_kwargs)
|
27
|
+
return generate_msg(request, adata, ads)
|
28
|
+
except KeyError as e:
|
29
|
+
raise e
|
30
|
+
except Exception as e:
|
31
|
+
if hasattr(e, '__context__') and e.__context__:
|
32
|
+
raise Exception(f"{str(e.__context__)}")
|
33
|
+
else:
|
34
|
+
raise e
|
35
|
+
|
36
|
+
|
37
|
+
@tl_mcp.tool()
|
38
|
+
async def umap(
|
39
|
+
request: UMAPModel = UMAPModel()
|
40
|
+
):
|
41
|
+
"""Uniform Manifold Approximation and Projection (UMAP) for visualization"""
|
42
|
+
|
43
|
+
try:
|
44
|
+
result = await forward_request("tl_umap", request)
|
45
|
+
if result is not None:
|
46
|
+
return result
|
47
|
+
func_kwargs = filter_args(request, sc.tl.umap)
|
48
|
+
ads = get_ads()
|
49
|
+
adata = ads.get_adata(request=request)
|
50
|
+
sc.tl.umap(adata, **func_kwargs)
|
51
|
+
add_op_log(adata, sc.tl.umap, func_kwargs)
|
52
|
+
return [generate_msg(request, adata, ads)]
|
53
|
+
except KeyError as e:
|
54
|
+
raise e
|
55
|
+
except Exception as e:
|
56
|
+
if hasattr(e, '__context__') and e.__context__:
|
57
|
+
raise Exception(f"{str(e.__context__)}")
|
58
|
+
else:
|
59
|
+
raise e
|
60
|
+
|
61
|
+
@tl_mcp.tool()
|
62
|
+
async def draw_graph(
|
63
|
+
request: DrawGraphModel = DrawGraphModel()
|
64
|
+
):
|
65
|
+
"""Force-directed graph drawing"""
|
66
|
+
|
67
|
+
try:
|
68
|
+
result = await forward_request("tl_draw_graph", request)
|
69
|
+
if result is not None:
|
70
|
+
return result
|
71
|
+
func_kwargs = filter_args(request, sc.tl.draw_graph)
|
72
|
+
ads = get_ads()
|
73
|
+
adata = ads.get_adata(request=request)
|
74
|
+
sc.tl.draw_graph(adata, **func_kwargs)
|
75
|
+
add_op_log(adata, sc.tl.draw_graph, func_kwargs)
|
76
|
+
return [generate_msg(request, adata, ads)]
|
77
|
+
except KeyError as e:
|
78
|
+
raise e
|
79
|
+
except Exception as e:
|
80
|
+
if hasattr(e, '__context__') and e.__context__:
|
81
|
+
raise Exception(f"{str(e.__context__)}")
|
82
|
+
else:
|
83
|
+
raise e
|
84
|
+
|
85
|
+
@tl_mcp.tool()
|
86
|
+
async def diffmap(
|
87
|
+
request: DiffMapModel = DiffMapModel()
|
88
|
+
):
|
89
|
+
"""Diffusion Maps for dimensionality reduction"""
|
90
|
+
|
91
|
+
try:
|
92
|
+
result = await forward_request("tl_diffmap", request)
|
93
|
+
if result is not None:
|
94
|
+
return result
|
95
|
+
func_kwargs = filter_args(request, sc.tl.diffmap)
|
96
|
+
ads = get_ads()
|
97
|
+
adata = ads.get_adata(request=request)
|
98
|
+
sc.tl.diffmap(adata, **func_kwargs)
|
99
|
+
adata.obsm["X_diffmap"] = adata.obsm["X_diffmap"][:,1:]
|
100
|
+
add_op_log(adata, sc.tl.diffmap, func_kwargs)
|
101
|
+
return [generate_msg(request, adata, ads)]
|
102
|
+
except Exception as e:
|
103
|
+
if hasattr(e, '__context__') and e.__context__:
|
104
|
+
raise Exception(f"{str(e.__context__)}")
|
105
|
+
else:
|
106
|
+
raise e
|
107
|
+
|
108
|
+
@tl_mcp.tool()
|
109
|
+
async def embedding_density(
|
110
|
+
request: EmbeddingDensityModel = EmbeddingDensityModel()
|
111
|
+
):
|
112
|
+
"""Calculate the density of cells in an embedding"""
|
113
|
+
|
114
|
+
try:
|
115
|
+
result = await forward_request("tl_embedding_density", request)
|
116
|
+
if result is not None:
|
117
|
+
return result
|
118
|
+
func_kwargs = filter_args(request, sc.tl.embedding_density)
|
119
|
+
ads = get_ads()
|
120
|
+
adata = ads.get_adata(request=request)
|
121
|
+
sc.tl.embedding_density(adata, **func_kwargs)
|
122
|
+
add_op_log(adata, sc.tl.embedding_density, func_kwargs)
|
123
|
+
return [generate_msg(request, adata, ads)]
|
124
|
+
except Exception as e:
|
125
|
+
if hasattr(e, '__context__') and e.__context__:
|
126
|
+
raise Exception(f"{str(e.__context__)}")
|
127
|
+
else:
|
128
|
+
raise e
|
129
|
+
|
130
|
+
|
131
|
+
@tl_mcp.tool()
|
132
|
+
async def leiden(
|
133
|
+
request: LeidenModel = LeidenModel()
|
134
|
+
):
|
135
|
+
"""Leiden clustering algorithm for community detection"""
|
136
|
+
|
137
|
+
try:
|
138
|
+
result = await forward_request("tl_leiden", request)
|
139
|
+
if result is not None:
|
140
|
+
return result
|
141
|
+
func_kwargs = filter_args(request, sc.tl.leiden)
|
142
|
+
ads = get_ads()
|
143
|
+
adata = ads.get_adata(request=request)
|
144
|
+
sc.tl.leiden(adata, **func_kwargs)
|
145
|
+
add_op_log(adata, sc.tl.leiden, func_kwargs)
|
146
|
+
return [generate_msg(request, adata, ads)]
|
147
|
+
except Exception as e:
|
148
|
+
if hasattr(e, '__context__') and e.__context__:
|
149
|
+
raise Exception(f"{str(e.__context__)}")
|
150
|
+
else:
|
151
|
+
raise e
|
152
|
+
|
153
|
+
|
154
|
+
@tl_mcp.tool()
|
155
|
+
async def louvain(
|
156
|
+
request: LouvainModel = LouvainModel()
|
157
|
+
):
|
158
|
+
"""Louvain clustering algorithm for community detection"""
|
159
|
+
|
160
|
+
try:
|
161
|
+
result = await forward_request("tl_louvain", request)
|
162
|
+
if result is not None:
|
163
|
+
return result
|
164
|
+
func_kwargs = filter_args(request, sc.tl.louvain)
|
165
|
+
ads = get_ads()
|
166
|
+
adata = ads.get_adata(request=request)
|
167
|
+
sc.tl.louvain(adata, **func_kwargs)
|
168
|
+
add_op_log(adata, sc.tl.louvain, func_kwargs)
|
169
|
+
return [generate_msg(request, adata, ads)]
|
170
|
+
except Exception as e:
|
171
|
+
if hasattr(e, '__context__') and e.__context__:
|
172
|
+
raise Exception(f"{str(e.__context__)}")
|
173
|
+
else:
|
174
|
+
raise e
|
175
|
+
|
176
|
+
|
177
|
+
@tl_mcp.tool()
|
178
|
+
async def dendrogram(
|
179
|
+
request: DendrogramModel,
|
180
|
+
):
|
181
|
+
"""Hierarchical clustering dendrogram"""
|
182
|
+
|
183
|
+
try:
|
184
|
+
result = await forward_request("tl_dendrogram", request)
|
185
|
+
if result is not None:
|
186
|
+
return result
|
187
|
+
func_kwargs = filter_args(request, sc.tl.dendrogram)
|
188
|
+
ads = get_ads()
|
189
|
+
adata = ads.get_adata(request=request)
|
190
|
+
sc.tl.dendrogram(adata, **func_kwargs)
|
191
|
+
add_op_log(adata, sc.tl.dendrogram, func_kwargs)
|
192
|
+
return [generate_msg(request, adata, ads)]
|
193
|
+
except Exception as e:
|
194
|
+
if hasattr(e, '__context__') and e.__context__:
|
195
|
+
raise Exception(f"{str(e.__context__)}")
|
196
|
+
else:
|
197
|
+
raise e
|
198
|
+
|
199
|
+
|
200
|
+
@tl_mcp.tool()
|
201
|
+
async def dpt(
|
202
|
+
request: DPTModel = DPTModel()
|
203
|
+
):
|
204
|
+
"""Diffusion Pseudotime (DPT) analysis"""
|
205
|
+
|
206
|
+
try:
|
207
|
+
result = await forward_request("tl_dpt", request)
|
208
|
+
if result is not None:
|
209
|
+
return result
|
210
|
+
func_kwargs = filter_args(request, sc.tl.dpt)
|
211
|
+
ads = get_ads()
|
212
|
+
adata = ads.get_adata(request=request)
|
213
|
+
sc.tl.dpt(adata, **func_kwargs)
|
214
|
+
add_op_log(adata, sc.tl.dpt, func_kwargs)
|
215
|
+
return [generate_msg(request, adata, ads)]
|
216
|
+
except Exception as e:
|
217
|
+
if hasattr(e, '__context__') and e.__context__:
|
218
|
+
raise Exception(f"{str(e.__context__)}")
|
219
|
+
else:
|
220
|
+
raise e
|
221
|
+
|
222
|
+
|
223
|
+
@tl_mcp.tool()
|
224
|
+
async def paga(
|
225
|
+
request: PAGAModel = PAGAModel()
|
226
|
+
):
|
227
|
+
"""Partition-based graph abstraction"""
|
228
|
+
|
229
|
+
try:
|
230
|
+
result = await forward_request("tl_paga", request)
|
231
|
+
if result is not None:
|
232
|
+
return result
|
233
|
+
func_kwargs = filter_args(request, sc.tl.paga)
|
234
|
+
ads = get_ads()
|
235
|
+
adata = ads.get_adata(request=request)
|
236
|
+
sc.tl.paga(adata, **func_kwargs)
|
237
|
+
add_op_log(adata, sc.tl.paga, func_kwargs)
|
238
|
+
return [generate_msg(request, adata, ads)]
|
239
|
+
except Exception as e:
|
240
|
+
if hasattr(e, '__context__') and e.__context__:
|
241
|
+
raise Exception(f"{str(e.__context__)}")
|
242
|
+
else:
|
243
|
+
raise e
|
244
|
+
|
245
|
+
|
246
|
+
@tl_mcp.tool()
|
247
|
+
async def ingest(
|
248
|
+
request: IngestModel = IngestModel()
|
249
|
+
):
|
250
|
+
"""Map labels and embeddings from reference data to new data"""
|
251
|
+
|
252
|
+
try:
|
253
|
+
result = await forward_request("tl_ingest", request)
|
254
|
+
if result is not None:
|
255
|
+
return result
|
256
|
+
func_kwargs = filter_args(request, sc.tl.ingest)
|
257
|
+
ads = get_ads()
|
258
|
+
adata = ads.get_adata(request=request)
|
259
|
+
sc.tl.ingest(adata, **func_kwargs)
|
260
|
+
add_op_log(adata, sc.tl.ingest, func_kwargs)
|
261
|
+
return [generate_msg(request, adata, ads)]
|
262
|
+
except Exception as e:
|
263
|
+
if hasattr(e, '__context__') and e.__context__:
|
264
|
+
raise Exception(f"{str(e.__context__)}")
|
265
|
+
else:
|
266
|
+
raise e
|
267
|
+
|
268
|
+
@tl_mcp.tool()
|
269
|
+
async def rank_genes_groups(
|
270
|
+
request: RankGenesGroupsModel,
|
271
|
+
|
272
|
+
):
|
273
|
+
"""Rank genes for characterizing groups, for differentially expressison analysis"""
|
274
|
+
|
275
|
+
try:
|
276
|
+
result = await forward_request("tl_rank_genes_groups", request)
|
277
|
+
if result is not None:
|
278
|
+
return result
|
279
|
+
func_kwargs = filter_args(request, sc.tl.rank_genes_groups)
|
280
|
+
ads = get_ads()
|
281
|
+
adata = ads.get_adata(request=request)
|
282
|
+
sc.tl.rank_genes_groups(adata, **func_kwargs)
|
283
|
+
add_op_log(adata, sc.tl.rank_genes_groups, func_kwargs)
|
284
|
+
return [generate_msg(request, adata, ads)]
|
285
|
+
except Exception as e:
|
286
|
+
if hasattr(e, '__context__') and e.__context__:
|
287
|
+
raise Exception(f"{str(e.__context__)}")
|
288
|
+
else:
|
289
|
+
raise e
|
290
|
+
|
291
|
+
|
292
|
+
@tl_mcp.tool()
|
293
|
+
async def filter_rank_genes_groups(
|
294
|
+
request: FilterRankGenesGroupsModel = FilterRankGenesGroupsModel()
|
295
|
+
):
|
296
|
+
"""Filter out genes based on fold change and fraction of genes"""
|
297
|
+
|
298
|
+
try:
|
299
|
+
result = await forward_request("tl_filter_rank_genes_groups", request)
|
300
|
+
if result is not None:
|
301
|
+
return result
|
302
|
+
func_kwargs = filter_args(request, sc.tl.filter_rank_genes_groups)
|
303
|
+
ads = get_ads()
|
304
|
+
adata = ads.get_adata(request=request)
|
305
|
+
sc.tl.filter_rank_genes_groups(adata, **func_kwargs)
|
306
|
+
add_op_log(adata, sc.tl.filter_rank_genes_groups, func_kwargs)
|
307
|
+
return [generate_msg(request, adata, ads)]
|
308
|
+
except Exception as e:
|
309
|
+
if hasattr(e, '__context__') and e.__context__:
|
310
|
+
raise Exception(f"{str(e.__context__)}")
|
311
|
+
else:
|
312
|
+
raise e
|
313
|
+
|
314
|
+
|
315
|
+
@tl_mcp.tool()
|
316
|
+
async def marker_gene_overlap(
|
317
|
+
request: MarkerGeneOverlapModel = MarkerGeneOverlapModel()
|
318
|
+
):
|
319
|
+
"""Calculate overlap between data-derived marker genes and reference markers"""
|
320
|
+
|
321
|
+
try:
|
322
|
+
result = await forward_request("tl_marker_gene_overlap", request)
|
323
|
+
if result is not None:
|
324
|
+
return result
|
325
|
+
func_kwargs = filter_args(request, sc.tl.marker_gene_overlap)
|
326
|
+
ads = get_ads()
|
327
|
+
adata = ads.get_adata(request=request)
|
328
|
+
sc.tl.marker_gene_overlap(adata, **func_kwargs)
|
329
|
+
add_op_log(adata, sc.tl.marker_gene_overlap, func_kwargs)
|
330
|
+
return [generate_msg(request, adata, ads)]
|
331
|
+
except Exception as e:
|
332
|
+
if hasattr(e, '__context__') and e.__context__:
|
333
|
+
raise Exception(f"{str(e.__context__)}")
|
334
|
+
else:
|
335
|
+
raise e
|
336
|
+
|
337
|
+
@tl_mcp.tool()
|
338
|
+
async def score_genes(
|
339
|
+
request: ScoreGenesModel,
|
340
|
+
|
341
|
+
):
|
342
|
+
"""Score a set of genes based on their average expression"""
|
343
|
+
try:
|
344
|
+
result = await forward_request("tl_score_genes", request)
|
345
|
+
if result is not None:
|
346
|
+
return result
|
347
|
+
func_kwargs = filter_args(request, sc.tl.score_genes)
|
348
|
+
ads = get_ads()
|
349
|
+
adata = ads.get_adata(request=request)
|
350
|
+
sc.tl.score_genes(adata, **func_kwargs)
|
351
|
+
add_op_log(adata, sc.tl.score_genes, func_kwargs)
|
352
|
+
return [generate_msg(request, adata, ads)]
|
353
|
+
except Exception as e:
|
354
|
+
if hasattr(e, '__context__') and e.__context__:
|
355
|
+
raise Exception(f"{str(e.__context__)}")
|
356
|
+
else:
|
357
|
+
raise e
|
358
|
+
|
359
|
+
@tl_mcp.tool()
|
360
|
+
async def score_genes_cell_cycle(
|
361
|
+
request: ScoreGenesCellCycleModel,
|
362
|
+
|
363
|
+
):
|
364
|
+
"""Score cell cycle genes and assign cell cycle phases"""
|
365
|
+
|
366
|
+
try:
|
367
|
+
result = await forward_request("tl_score_genes_cell_cycle", request)
|
368
|
+
if result is not None:
|
369
|
+
return result
|
370
|
+
func_kwargs = filter_args(request, sc.tl.score_genes_cell_cycle)
|
371
|
+
ads = get_ads()
|
372
|
+
adata = ads.get_adata(request=request)
|
373
|
+
sc.tl.score_genes_cell_cycle(adata, **func_kwargs)
|
374
|
+
add_op_log(adata, sc.tl.score_genes_cell_cycle, func_kwargs)
|
375
|
+
return [generate_msg(request, adata, ads)]
|
376
|
+
except Exception as e:
|
377
|
+
if hasattr(e, '__context__') and e.__context__:
|
378
|
+
raise Exception(f"{str(e.__context__)}")
|
379
|
+
else:
|
380
|
+
raise e
|
381
|
+
|
382
|
+
|
383
|
+
@tl_mcp.tool()
|
384
|
+
async def pca(
|
385
|
+
request: PCAModel = PCAModel()
|
386
|
+
):
|
387
|
+
"""Principal component analysis"""
|
388
|
+
|
389
|
+
try:
|
390
|
+
result = await forward_request("tl_pca", request)
|
391
|
+
if result is not None:
|
392
|
+
return result
|
393
|
+
func_kwargs = filter_args(request, sc.pp.pca)
|
394
|
+
ads = get_ads()
|
395
|
+
adata = ads.get_adata(request=request)
|
396
|
+
sc.pp.pca(adata, **func_kwargs)
|
397
|
+
add_op_log(adata, sc.pp.pca, func_kwargs)
|
398
|
+
return [
|
399
|
+
generate_msg(request, adata, ads)
|
400
|
+
]
|
401
|
+
except KeyError as e:
|
402
|
+
raise e
|
403
|
+
except Exception as e:
|
404
|
+
if hasattr(e, '__context__') and e.__context__:
|
405
|
+
raise Exception(f"{str(e.__context__)}")
|
406
|
+
else:
|
407
|
+
raise e
|
@@ -0,0 +1,250 @@
|
|
1
|
+
import os
|
2
|
+
import inspect
|
3
|
+
from pathlib import Path
|
4
|
+
import scanpy as sc
|
5
|
+
from fastmcp import FastMCP , Context
|
6
|
+
from ..schema.util import *
|
7
|
+
from ..util import filter_args, forward_request, get_ads, generate_msg,add_op_log
|
8
|
+
|
9
|
+
|
10
|
+
ul_mcp = FastMCP("SCMCP-Util-Server")
|
11
|
+
|
12
|
+
|
13
|
+
@ul_mcp.tool()
|
14
|
+
async def query_op_log(request: QueryOpLogModel = QueryOpLogModel()):
|
15
|
+
"""Query the adata operation log"""
|
16
|
+
adata = get_ads().get_adata(request=request)
|
17
|
+
op_dic = adata.uns["operation"]["op"]
|
18
|
+
opids = adata.uns["operation"]["opid"][-n:]
|
19
|
+
op_list = []
|
20
|
+
for opid in opids:
|
21
|
+
op_list.append(op_dic[opid])
|
22
|
+
return op_list
|
23
|
+
|
24
|
+
|
25
|
+
@ul_mcp.tool()
|
26
|
+
async def mark_var(
|
27
|
+
request: MarkVarModel = MarkVarModel()
|
28
|
+
):
|
29
|
+
"""
|
30
|
+
Determine if each gene meets specific conditions and store results in adata.var as boolean values.
|
31
|
+
For example: mitochondrion genes startswith MT-.
|
32
|
+
The tool should be called first when calculate quality control metrics for mitochondrion, ribosomal, harhemoglobin genes, or other qc_vars.
|
33
|
+
"""
|
34
|
+
try:
|
35
|
+
result = await forward_request("ul_mark_var", request)
|
36
|
+
if result is not None:
|
37
|
+
return result
|
38
|
+
adata = get_ads().get_adata(request=request)
|
39
|
+
var_name = request.var_name
|
40
|
+
gene_class = request.gene_class
|
41
|
+
pattern_type = request.pattern_type
|
42
|
+
patterns = request.patterns
|
43
|
+
if gene_class is not None:
|
44
|
+
if gene_class == "mitochondrion":
|
45
|
+
adata.var["mt"] = adata.var_names.str.startswith(('MT-', 'Mt','mt-'))
|
46
|
+
var_name = "mt"
|
47
|
+
elif gene_class == "ribosomal":
|
48
|
+
adata.var["ribo"] = adata.var_names.str.startswith(("RPS", "RPL", "Rps", "Rpl"))
|
49
|
+
var_name = "ribo"
|
50
|
+
elif gene_class == "hemoglobin":
|
51
|
+
adata.var["hb"] = adata.var_names.str.contains("^HB[^(P)]", case=False)
|
52
|
+
var_name = "hb"
|
53
|
+
elif pattern_type is not None and patterns is not None:
|
54
|
+
if pattern_type == "startswith":
|
55
|
+
adata.var[var_name] = adata.var_names.str.startswith(patterns)
|
56
|
+
elif pattern_type == "endswith":
|
57
|
+
adata.var[var_name] = adata.var_names.str.endswith(patterns)
|
58
|
+
elif pattern_type == "contains":
|
59
|
+
adata.var[var_name] = adata.var_names.str.contains(patterns)
|
60
|
+
else:
|
61
|
+
raise ValueError(f"Did not support pattern_type: {pattern_type}")
|
62
|
+
else:
|
63
|
+
raise ValueError(f"Please provide validated parameter")
|
64
|
+
|
65
|
+
res = {var_name: adata.var[var_name].value_counts().to_dict(), "msg": f"add '{var_name}' column in adata.var"}
|
66
|
+
func_kwargs = {"var_name": var_name, "gene_class": gene_class, "pattern_type": pattern_type, "patterns": patterns}
|
67
|
+
add_op_log(adata, "mark_var", func_kwargs)
|
68
|
+
return res
|
69
|
+
except KeyError as e:
|
70
|
+
raise e
|
71
|
+
except Exception as e:
|
72
|
+
if hasattr(e, '__context__') and e.__context__:
|
73
|
+
raise Exception(f"{str(e.__context__)}")
|
74
|
+
else:
|
75
|
+
raise e
|
76
|
+
|
77
|
+
|
78
|
+
@ul_mcp.tool()
|
79
|
+
async def list_var(
|
80
|
+
request: ListVarModel = ListVarModel()
|
81
|
+
):
|
82
|
+
"""List key columns in adata.var. It should be called for checking when other tools need var key column names as input."""
|
83
|
+
try:
|
84
|
+
result = await forward_request("ul_list_var", request)
|
85
|
+
if result is not None:
|
86
|
+
return result
|
87
|
+
adata = get_ads().get_adata(request=request)
|
88
|
+
columns = list(adata.var.columns)
|
89
|
+
add_op_log(adata, list_var, {})
|
90
|
+
return columns
|
91
|
+
except KeyError as e:
|
92
|
+
raise e
|
93
|
+
except Exception as e:
|
94
|
+
if hasattr(e, '__context__') and e.__context__:
|
95
|
+
raise Exception(f"{str(e.__context__)}")
|
96
|
+
else:
|
97
|
+
raise e
|
98
|
+
|
99
|
+
@ul_mcp.tool()
|
100
|
+
async def list_obs(
|
101
|
+
request: ListObsModel = ListObsModel()
|
102
|
+
):
|
103
|
+
"""List key columns in adata.obs. It should be called before other tools need obs key column names input."""
|
104
|
+
try:
|
105
|
+
result = await forward_request("ul_list_obs", request)
|
106
|
+
if result is not None:
|
107
|
+
return result
|
108
|
+
adata = get_ads().get_adata(request=request)
|
109
|
+
columns = list(adata.obs.columns)
|
110
|
+
add_op_log(adata, list_obs, {})
|
111
|
+
return columns
|
112
|
+
except KeyError as e:
|
113
|
+
raise e
|
114
|
+
except Exception as e:
|
115
|
+
if hasattr(e, '__context__') and e.__context__:
|
116
|
+
raise Exception(f"{str(e.__context__)}")
|
117
|
+
else:
|
118
|
+
raise e
|
119
|
+
|
120
|
+
@ul_mcp.tool()
|
121
|
+
async def check_var(
|
122
|
+
request: VarNamesModel = VarNamesModel()
|
123
|
+
):
|
124
|
+
"""Check if genes/variables exist in adata.var_names. This tool should be called before gene expression visualizations or color by genes."""
|
125
|
+
try:
|
126
|
+
result = await forward_request("ul_check_var", request)
|
127
|
+
if result is not None:
|
128
|
+
return result
|
129
|
+
adata = get_ads().get_adata(request=request)
|
130
|
+
var_names = request.var_names
|
131
|
+
result = {v: v in adata.var_names for v in var_names}
|
132
|
+
add_op_log(adata, check_var, {"var_names": var_names})
|
133
|
+
return result
|
134
|
+
except KeyError as e:
|
135
|
+
raise e
|
136
|
+
except Exception as e:
|
137
|
+
if hasattr(e, '__context__') and e.__context__:
|
138
|
+
raise Exception(f"{str(e.__context__)}")
|
139
|
+
else:
|
140
|
+
raise e
|
141
|
+
|
142
|
+
@ul_mcp.tool()
|
143
|
+
async def merge_adata(
|
144
|
+
request: ConcatAdataModel = ConcatAdataModel()
|
145
|
+
):
|
146
|
+
"""Merge multiple adata objects."""
|
147
|
+
|
148
|
+
try:
|
149
|
+
result = await forward_request("ul_merge_adata", request)
|
150
|
+
if result is not None:
|
151
|
+
return result
|
152
|
+
ads = get_ads()
|
153
|
+
adata = ads.get_adata(request=request)
|
154
|
+
kwargs = {k: v for k, v in request.model_dump().items() if v is not None}
|
155
|
+
merged_adata = adata.concat(list(ads.adata_dic[dtype].values()), **kwargs)
|
156
|
+
ads.adata_dic[dtype] = {}
|
157
|
+
ads.active_id = "merged_adata"
|
158
|
+
add_op_log(merged_adata, ad.concat, kwargs)
|
159
|
+
ads.adata_dic[ads.active_id] = merged_adata
|
160
|
+
return {"status": "success", "message": "Successfully merged all AnnData objects"}
|
161
|
+
except KeyError as e:
|
162
|
+
raise e
|
163
|
+
except Exception as e:
|
164
|
+
if hasattr(e, '__context__') and e.__context__:
|
165
|
+
raise Exception(f"{str(e.__context__)}")
|
166
|
+
else:
|
167
|
+
raise e
|
168
|
+
|
169
|
+
|
170
|
+
@ul_mcp.tool()
|
171
|
+
async def set_dpt_iroot(
|
172
|
+
request: DPTIROOTModel,
|
173
|
+
|
174
|
+
):
|
175
|
+
"""Set the iroot cell"""
|
176
|
+
try:
|
177
|
+
result = await forward_request("ul_set_dpt_iroot", request)
|
178
|
+
if result is not None:
|
179
|
+
return result
|
180
|
+
adata = get_ads().get_adata(request=request)
|
181
|
+
diffmap_key = request.diffmap_key
|
182
|
+
dimension = request.dimension
|
183
|
+
direction = request.direction
|
184
|
+
if diffmap_key not in adata.obsm:
|
185
|
+
raise ValueError(f"Diffusion map key '{diffmap_key}' not found in adata.obsm")
|
186
|
+
if direction == "min":
|
187
|
+
adata.uns["iroot"] = adata.obsm[diffmap_key][:, dimension].argmin()
|
188
|
+
else:
|
189
|
+
adata.uns["iroot"] = adata.obsm[diffmap_key][:, dimension].argmax()
|
190
|
+
|
191
|
+
func_kwargs = {"diffmap_key": diffmap_key, "dimension": dimension, "direction": direction}
|
192
|
+
add_op_log(adata, "set_dpt_iroot", func_kwargs)
|
193
|
+
|
194
|
+
return {"status": "success", "message": f"Successfully set root cell for DPT using {direction} of dimension {dimension}"}
|
195
|
+
except KeyError as e:
|
196
|
+
raise e
|
197
|
+
except Exception as e:
|
198
|
+
if hasattr(e, '__context__') and e.__context__:
|
199
|
+
raise Exception(f"{str(e.__context__)}")
|
200
|
+
else:
|
201
|
+
raise e
|
202
|
+
|
203
|
+
@ul_mcp.tool()
|
204
|
+
async def add_layer(
|
205
|
+
request: AddLayerModel,
|
206
|
+
):
|
207
|
+
"""Add a layer to the AnnData object.
|
208
|
+
"""
|
209
|
+
try:
|
210
|
+
result = await forward_request("ul_add_layer", request)
|
211
|
+
if result is not None:
|
212
|
+
return result
|
213
|
+
adata = get_ads().get_adata(request=request)
|
214
|
+
layer_name = request.layer_name
|
215
|
+
|
216
|
+
# Check if layer already exists
|
217
|
+
if layer_name in adata.layers:
|
218
|
+
raise ValueError(f"Layer '{layer_name}' already exists in adata.layers")
|
219
|
+
# Add the data as a new layer
|
220
|
+
adata.layers[layer_name] = adata.X.copy()
|
221
|
+
|
222
|
+
func_kwargs = {"layer_name": layer_name}
|
223
|
+
add_op_log(adata, "add_layer", func_kwargs)
|
224
|
+
|
225
|
+
return {
|
226
|
+
"status": "success",
|
227
|
+
"message": f"Successfully added layer '{layer_name}' to adata.layers"
|
228
|
+
}
|
229
|
+
except KeyError as e:
|
230
|
+
raise e
|
231
|
+
except Exception as e:
|
232
|
+
if hasattr(e, '__context__') and e.__context__:
|
233
|
+
raise Exception(f"{str(e.__context__)}")
|
234
|
+
else:
|
235
|
+
raise e
|
236
|
+
|
237
|
+
@ul_mcp.tool()
|
238
|
+
async def check_samples():
|
239
|
+
"""check the stored samples
|
240
|
+
"""
|
241
|
+
try:
|
242
|
+
ads = get_ads()
|
243
|
+
return {"sampleid": [list(ads.adata_dic[dk].keys()) for dk in ads.adata_dic.keys()]}
|
244
|
+
except KeyError as e:
|
245
|
+
raise e
|
246
|
+
except Exception as e:
|
247
|
+
if hasattr(e, '__context__') and e.__context__:
|
248
|
+
raise Exception(f"{str(e.__context__)}")
|
249
|
+
else:
|
250
|
+
raise e
|