scmcp-shared 0.2.1__tar.gz → 0.2.5__tar.gz

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.
Files changed (23) hide show
  1. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/PKG-INFO +1 -1
  2. scmcp_shared-0.2.5/src/scmcp_shared/__init__.py +3 -0
  3. scmcp_shared-0.2.5/src/scmcp_shared/schema/__init__.py +13 -0
  4. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/schema/io.py +4 -4
  5. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/schema/pl.py +2 -3
  6. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/schema/pp.py +15 -15
  7. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/schema/tl.py +18 -19
  8. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/schema/util.py +11 -11
  9. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/server/__init__.py +6 -6
  10. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/server/io.py +17 -12
  11. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/server/pl.py +107 -65
  12. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/server/pp.py +75 -64
  13. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/server/tl.py +99 -84
  14. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/server/util.py +39 -29
  15. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/util.py +23 -28
  16. scmcp_shared-0.2.1/src/scmcp_shared/__init__.py +0 -3
  17. scmcp_shared-0.2.1/src/scmcp_shared/schema/__init__.py +0 -1
  18. scmcp_shared-0.2.1/src/scmcp_shared/schema/base.py +0 -11
  19. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/.github/workflows/publish.yml +0 -0
  20. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/LICENSE +0 -0
  21. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/README.md +0 -0
  22. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/pyproject.toml +0 -0
  23. {scmcp_shared-0.2.1 → scmcp_shared-0.2.5}/src/scmcp_shared/logging_config.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scmcp_shared
3
- Version: 0.2.1
3
+ Version: 0.2.5
4
4
  Summary: A shared function libray for scmcphub
5
5
  Author-email: shuang <hsh-me@outlook.com>
6
6
  License: BSD 3-Clause License
@@ -0,0 +1,3 @@
1
+
2
+ __version__ = "0.2.5"
3
+
@@ -0,0 +1,13 @@
1
+ from __future__ import annotations
2
+
3
+ from pydantic import Field, BaseModel,ConfigDict
4
+
5
+
6
+ class AdataModel(BaseModel):
7
+ """Input schema for the adata tool."""
8
+ sampleid: str | None = Field(default=None, description="adata sampleid")
9
+ adtype: str = Field(default="exp", description="The input adata.X data type for preprocess/analysis/plotting")
10
+
11
+ model_config = ConfigDict(
12
+ extra="ignore"
13
+ )
@@ -1,13 +1,13 @@
1
1
  from pydantic import (
2
2
  Field,
3
3
  field_validator,
4
- model_validator,
4
+ model_validator, BaseModel
5
5
  )
6
6
  from typing import Optional, Literal
7
- from .base import AdataModel
8
7
 
9
8
 
10
- class ReadModel(AdataModel):
9
+
10
+ class ReadModel(BaseModel):
11
11
  """Input schema for the read tool."""
12
12
  filename: str = Field(
13
13
  ...,
@@ -86,7 +86,7 @@ class ReadModel(AdataModel):
86
86
  return v
87
87
 
88
88
 
89
- class WriteModel(AdataModel):
89
+ class WriteModel(BaseModel):
90
90
  """Input schema for the write tool."""
91
91
  filename: str = Field(
92
92
  description="Path to save the file. If no extension is provided, the default format will be used."
@@ -7,7 +7,6 @@ from pydantic import (
7
7
  model_validator,
8
8
  BaseModel
9
9
  )
10
- from .base import AdataModel
11
10
 
12
11
  # 创建 Mixin 类处理特定功能
13
12
  class LegendMixin:
@@ -73,7 +72,7 @@ class FigureSizeMixin:
73
72
 
74
73
 
75
74
  # 基础可视化模型,包含所有可视化工具共享的字段
76
- class BaseVisualizationModel(AdataModel, LegendMixin, ColorMappingMixin, FigureSizeMixin):
75
+ class BaseVisualizationModel(BaseModel, LegendMixin, ColorMappingMixin, FigureSizeMixin):
77
76
  """基础可视化模型,包含所有可视化工具共享的字段"""
78
77
  pass
79
78
 
@@ -632,7 +631,7 @@ class RankGenesGroupsModel(BaseVisualizationModel):
632
631
 
633
632
 
634
633
  # 重构 ClusterMapModel
635
- class ClusterMapModel(AdataModel):
634
+ class ClusterMapModel(BaseModel):
636
635
  """Input schema for the clustermap plotting tool."""
637
636
 
638
637
  obs_keys: Optional[str] = Field(
@@ -3,15 +3,15 @@ from pydantic import (
3
3
  ValidationInfo,
4
4
  computed_field,
5
5
  field_validator,
6
- model_validator,
6
+ model_validator,BaseModel
7
7
  )
8
- from .base import AdataModel
8
+
9
9
  from typing import Optional, Union, List, Dict, Any
10
10
  from typing import Literal
11
11
  import numpy as np
12
12
 
13
13
 
14
- class FilterCells(AdataModel):
14
+ class FilterCells(BaseModel):
15
15
  """Input schema for the filter_cells preprocessing tool."""
16
16
 
17
17
  min_counts: Optional[int] = Field(
@@ -42,7 +42,7 @@ class FilterCells(AdataModel):
42
42
  return v
43
43
 
44
44
 
45
- class FilterGenes(AdataModel):
45
+ class FilterGenes(BaseModel):
46
46
  """Input schema for the filter_genes preprocessing tool."""
47
47
 
48
48
  min_counts: Optional[int] = Field(
@@ -73,7 +73,7 @@ class FilterGenes(AdataModel):
73
73
  return v
74
74
 
75
75
 
76
- class SubsetCellModel(AdataModel):
76
+ class SubsetCellModel(BaseModel):
77
77
  """Input schema for subsetting AnnData objects based on various criteria."""
78
78
  obs_key: Optional[str] = Field(
79
79
  default=None,
@@ -109,7 +109,7 @@ class SubsetCellModel(AdataModel):
109
109
  )
110
110
 
111
111
 
112
- class SubsetGeneModel(AdataModel):
112
+ class SubsetGeneModel(BaseModel):
113
113
  """Input schema for subsetting AnnData objects based on various criteria."""
114
114
  min_counts: Optional[int] = Field(
115
115
  default=None,
@@ -145,7 +145,7 @@ class SubsetGeneModel(AdataModel):
145
145
  )
146
146
 
147
147
 
148
- class CalculateQCMetrics(AdataModel):
148
+ class CalculateQCMetrics(BaseModel):
149
149
  """Input schema for the calculate_qc_metrics preprocessing tool."""
150
150
 
151
151
  expr_type: str = Field(
@@ -196,7 +196,7 @@ class CalculateQCMetrics(AdataModel):
196
196
 
197
197
 
198
198
 
199
- class Log1PModel(AdataModel):
199
+ class Log1PModel(BaseModel):
200
200
  """Input schema for the log1p preprocessing tool."""
201
201
 
202
202
  base: Optional[Union[int, float]] = Field(
@@ -233,7 +233,7 @@ class Log1PModel(AdataModel):
233
233
 
234
234
 
235
235
 
236
- class HighlyVariableGenesModel(AdataModel):
236
+ class HighlyVariableGenesModel(BaseModel):
237
237
  """Input schema for the highly_variable_genes preprocessing tool."""
238
238
 
239
239
  layer: Optional[str] = Field(
@@ -307,7 +307,7 @@ class HighlyVariableGenesModel(AdataModel):
307
307
  return v
308
308
 
309
309
 
310
- class RegressOutModel(AdataModel):
310
+ class RegressOutModel(BaseModel):
311
311
  """Input schema for the regress_out preprocessing tool."""
312
312
 
313
313
  keys: Union[str, List[str]] = Field(
@@ -340,7 +340,7 @@ class RegressOutModel(AdataModel):
340
340
  raise ValueError("keys must be a string or list of strings")
341
341
 
342
342
 
343
- class ScaleModel(AdataModel):
343
+ class ScaleModel(BaseModel):
344
344
  """Input schema for the scale preprocessing tool."""
345
345
 
346
346
  zero_center: bool = Field(
@@ -376,7 +376,7 @@ class ScaleModel(AdataModel):
376
376
  return v
377
377
 
378
378
 
379
- class CombatModel(AdataModel):
379
+ class CombatModel(BaseModel):
380
380
  """Input schema for the combat batch effect correction tool."""
381
381
 
382
382
  key: str = Field(
@@ -405,7 +405,7 @@ class CombatModel(AdataModel):
405
405
  return v
406
406
 
407
407
 
408
- class ScrubletModel(AdataModel):
408
+ class ScrubletModel(BaseModel):
409
409
  """Input schema for the scrublet doublet prediction tool."""
410
410
 
411
411
  adata_sim: Optional[str] = Field(
@@ -511,7 +511,7 @@ class ScrubletModel(AdataModel):
511
511
  return v.lower()
512
512
 
513
513
 
514
- class NeighborsModel(AdataModel):
514
+ class NeighborsModel(BaseModel):
515
515
  """Input schema for the neighbors graph construction tool."""
516
516
 
517
517
  n_neighbors: int = Field(
@@ -589,7 +589,7 @@ class NeighborsModel(AdataModel):
589
589
  return v
590
590
 
591
591
 
592
- class NormalizeTotalModel(AdataModel):
592
+ class NormalizeTotalModel(BaseModel):
593
593
  """Input schema for the normalize_total preprocessing tool."""
594
594
 
595
595
  target_sum: Optional[float] = Field(
@@ -1,9 +1,8 @@
1
- from pydantic import Field, field_validator, ValidationInfo
1
+ from pydantic import Field, field_validator, ValidationInfo, BaseModel
2
2
  from typing import Optional, Union, List, Dict, Any, Tuple, Literal, Mapping
3
3
 
4
- from .base import AdataModel
5
4
 
6
- class TSNEModel(AdataModel):
5
+ class TSNEModel(BaseModel):
7
6
  """Input schema for the t-SNE dimensionality reduction tool."""
8
7
  n_pcs: Optional[int] = Field(
9
8
  default=None,
@@ -60,7 +59,7 @@ class TSNEModel(AdataModel):
60
59
  return v.lower()
61
60
 
62
61
 
63
- class UMAPModel(AdataModel):
62
+ class UMAPModel(BaseModel):
64
63
  """Input schema for the UMAP dimensionality reduction tool."""
65
64
 
66
65
  min_dist: Optional[float] = Field(
@@ -146,7 +145,7 @@ class UMAPModel(AdataModel):
146
145
  return v.lower()
147
146
 
148
147
 
149
- class DrawGraphModel(AdataModel):
148
+ class DrawGraphModel(BaseModel):
150
149
  """Input schema for the force-directed graph drawing tool."""
151
150
 
152
151
  layout: str = Field(
@@ -200,7 +199,7 @@ class DrawGraphModel(AdataModel):
200
199
  return v
201
200
 
202
201
 
203
- class DiffMapModel(AdataModel):
202
+ class DiffMapModel(BaseModel):
204
203
  """Input schema for the Diffusion Maps dimensionality reduction tool."""
205
204
 
206
205
  n_comps: int = Field(
@@ -230,7 +229,7 @@ class DiffMapModel(AdataModel):
230
229
  return v
231
230
 
232
231
 
233
- class EmbeddingDensityModel(AdataModel):
232
+ class EmbeddingDensityModel(BaseModel):
234
233
  """Input schema for the embedding density calculation tool."""
235
234
 
236
235
  basis: str = Field(
@@ -258,7 +257,7 @@ class EmbeddingDensityModel(AdataModel):
258
257
  return v
259
258
 
260
259
 
261
- class LeidenModel(AdataModel):
260
+ class LeidenModel(BaseModel):
262
261
  """Input schema for the Leiden clustering algorithm."""
263
262
 
264
263
  resolution: Optional[float] = Field(
@@ -330,7 +329,7 @@ class LeidenModel(AdataModel):
330
329
  return v
331
330
 
332
331
 
333
- class LouvainModel(AdataModel):
332
+ class LouvainModel(BaseModel):
334
333
  """Input schema for the Louvain clustering algorithm."""
335
334
 
336
335
  resolution: Optional[float] = Field(
@@ -402,7 +401,7 @@ class LouvainModel(AdataModel):
402
401
  return v
403
402
 
404
403
 
405
- class DendrogramModel(AdataModel):
404
+ class DendrogramModel(BaseModel):
406
405
  """Input schema for the hierarchical clustering dendrogram tool."""
407
406
 
408
407
  groupby: str = Field(
@@ -467,7 +466,7 @@ class DendrogramModel(AdataModel):
467
466
  return v
468
467
 
469
468
 
470
- class DPTModel(AdataModel):
469
+ class DPTModel(BaseModel):
471
470
  """Input schema for the Diffusion Pseudotime (DPT) tool."""
472
471
 
473
472
  n_dcs: int = Field(
@@ -516,7 +515,7 @@ class DPTModel(AdataModel):
516
515
  raise ValueError("min_group_size must be between 0 and 1")
517
516
  return v
518
517
 
519
- class PAGAModel(AdataModel):
518
+ class PAGAModel(BaseModel):
520
519
  """Input schema for the Partition-based Graph Abstraction (PAGA) tool."""
521
520
 
522
521
  groups: Optional[str] = Field(
@@ -544,7 +543,7 @@ class PAGAModel(AdataModel):
544
543
  return v
545
544
 
546
545
 
547
- class IngestModel(AdataModel):
546
+ class IngestModel(BaseModel):
548
547
  """Input schema for the ingest tool that maps labels and embeddings from reference data to new data."""
549
548
 
550
549
  obs: Optional[Union[str, List[str]]] = Field(
@@ -593,7 +592,7 @@ class IngestModel(AdataModel):
593
592
  return v.lower()
594
593
 
595
594
 
596
- class RankGenesGroupsModel(AdataModel):
595
+ class RankGenesGroupsModel(BaseModel):
597
596
  """Input schema for the rank_genes_groups tool."""
598
597
 
599
598
  groupby: str = Field(
@@ -675,7 +674,7 @@ class RankGenesGroupsModel(AdataModel):
675
674
  return v
676
675
 
677
676
 
678
- class FilterRankGenesGroupsModel(AdataModel):
677
+ class FilterRankGenesGroupsModel(BaseModel):
679
678
  """Input schema for filtering ranked genes groups."""
680
679
 
681
680
  key: Optional[str] = Field(
@@ -738,7 +737,7 @@ class FilterRankGenesGroupsModel(AdataModel):
738
737
  return v
739
738
 
740
739
 
741
- class MarkerGeneOverlapModel(AdataModel):
740
+ class MarkerGeneOverlapModel(BaseModel):
742
741
  """Input schema for the marker gene overlap tool."""
743
742
 
744
743
  key: str = Field(
@@ -809,7 +808,7 @@ class MarkerGeneOverlapModel(AdataModel):
809
808
  return v
810
809
 
811
810
 
812
- class ScoreGenesModel(AdataModel):
811
+ class ScoreGenesModel(BaseModel):
813
812
  """Input schema for the score_genes tool that calculates gene scores based on average expression."""
814
813
 
815
814
  ctrl_size: int = Field(
@@ -852,7 +851,7 @@ class ScoreGenesModel(AdataModel):
852
851
  return v
853
852
 
854
853
 
855
- class ScoreGenesCellCycleModel(AdataModel):
854
+ class ScoreGenesCellCycleModel(BaseModel):
856
855
  """Input schema for the score_genes_cell_cycle tool that scores cell cycle genes."""
857
856
 
858
857
  s_genes: List[str] = Field(
@@ -902,7 +901,7 @@ class ScoreGenesCellCycleModel(AdataModel):
902
901
 
903
902
 
904
903
 
905
- class PCAModel(AdataModel):
904
+ class PCAModel(BaseModel):
906
905
  """Input schema for the PCA preprocessing tool."""
907
906
 
908
907
  n_comps: Optional[int] = Field(
@@ -4,13 +4,13 @@ from pydantic import (
4
4
  ValidationInfo,
5
5
  computed_field,
6
6
  field_validator,
7
- model_validator,
7
+ model_validator,BaseModel
8
8
  )
9
9
  from typing import Optional, Union, List, Dict, Any, Callable, Collection, Literal
10
- from .base import AdataModel
11
10
 
12
11
 
13
- class MarkVarModel(AdataModel):
12
+
13
+ class MarkVarModel(BaseModel):
14
14
  """Determine or mark if each gene meets specific conditions and store results in adata.var as boolean values"""
15
15
 
16
16
  var_name: str = Field(
@@ -32,15 +32,15 @@ class MarkVarModel(AdataModel):
32
32
  )
33
33
 
34
34
 
35
- class ListVarModel(AdataModel):
35
+ class ListVarModel(BaseModel):
36
36
  """ListVarModel"""
37
37
  pass
38
38
 
39
- class ListObsModel(AdataModel):
39
+ class ListObsModel(BaseModel):
40
40
  """ListObsModel"""
41
41
  pass
42
42
 
43
- class VarNamesModel(AdataModel):
43
+ class VarNamesModel(BaseModel):
44
44
  """ListObsModel"""
45
45
  var_names: List[str] = Field(
46
46
  default=None,
@@ -48,7 +48,7 @@ class VarNamesModel(AdataModel):
48
48
  )
49
49
 
50
50
 
51
- class ConcatAdataModel(AdataModel):
51
+ class ConcatBaseModel(BaseModel):
52
52
  """Model for concatenating AnnData objects"""
53
53
 
54
54
  axis: Literal['obs', 0, 'var', 1] = Field(
@@ -89,7 +89,7 @@ class ConcatAdataModel(AdataModel):
89
89
  )
90
90
 
91
91
 
92
- class DPTIROOTModel(AdataModel):
92
+ class DPTIROOTModel(BaseModel):
93
93
  """Input schema for setting the root cell for diffusion pseudotime."""
94
94
  diffmap_key: str = Field(
95
95
  default="X_diffmap",
@@ -103,7 +103,7 @@ class DPTIROOTModel(AdataModel):
103
103
  )
104
104
 
105
105
 
106
- class CelltypeMapCellTypeModel(AdataModel):
106
+ class CelltypeMapCellTypeModel(BaseModel):
107
107
  """Input schema for mapping cluster IDs to cell type names."""
108
108
  cluster_key: str = Field(
109
109
  description="Key in adata.obs containing cluster IDs."
@@ -122,14 +122,14 @@ class CelltypeMapCellTypeModel(AdataModel):
122
122
 
123
123
 
124
124
 
125
- class AddLayerModel(AdataModel):
125
+ class AddLayerModel(BaseModel):
126
126
  """Input schema for adding a layer to AnnData object."""
127
127
  layer_name: str = Field(
128
128
  description="Name of the layer to add to adata.layers."
129
129
  )
130
130
 
131
131
 
132
- class QueryOpLogModel(AdataModel):
132
+ class QueryOpLogModel(BaseModel):
133
133
  """QueryOpLogModel"""
134
134
  n: int = Field(
135
135
  default=10,
@@ -23,9 +23,9 @@ class AdataState:
23
23
  self.cr_kernel = {}
24
24
  self.cr_estimator = {}
25
25
 
26
- def get_adata(self, sampleid=None, adtype="exp", request=None):
27
- if request is not None:
28
- kwargs = request.model_dump()
26
+ def get_adata(self, sampleid=None, adtype="exp", adinfo=None):
27
+ if adinfo is not None:
28
+ kwargs = adinfo.model_dump()
29
29
  sampleid = kwargs.get("sampleid", None)
30
30
  adtype = kwargs.get("adtype", "exp")
31
31
  try:
@@ -38,9 +38,9 @@ class AdataState:
38
38
  except Exception as e:
39
39
  raise Exception(f"fuck {e} {type(e)}")
40
40
 
41
- def set_adata(self, adata, sampleid=None, sdtype="exp", request=None):
42
- if request is not None:
43
- kwargs = request.model_dump()
41
+ def set_adata(self, adata, sampleid=None, sdtype="exp", adinfo=None):
42
+ if adinfo is not None:
43
+ kwargs = adinfo.model_dump()
44
44
  sampleid = kwargs.get("sampleid", None)
45
45
  sdtype = kwargs.get("adtype", "exp")
46
46
  sampleid = sampleid or self.active_id
@@ -4,6 +4,7 @@ from pathlib import Path
4
4
  import scanpy as sc
5
5
  from fastmcp import FastMCP , Context
6
6
  from fastmcp.exceptions import ToolError
7
+ from ..schema import AdataModel
7
8
  from ..schema.io import *
8
9
  from ..util import filter_args, forward_request, get_ads, generate_msg
9
10
 
@@ -12,12 +13,15 @@ io_mcp = FastMCP("SCMCP-IO-Server")
12
13
 
13
14
 
14
15
  @io_mcp.tool()
15
- async def read(request: ReadModel):
16
+ async def read(
17
+ request: ReadModel,
18
+ adinfo: AdataModel = AdataModel()
19
+ ):
16
20
  """
17
21
  Read data from 10X directory or various file formats (h5ad, 10x, text files, etc.).
18
22
  """
19
23
  try:
20
- result = await forward_request("io_read", request)
24
+ result = await forward_request("io_read", request, adinfo)
21
25
  if result is not None:
22
26
  return result
23
27
  kwargs = request.model_dump()
@@ -35,19 +39,17 @@ async def read(request: ReadModel):
35
39
  else:
36
40
  raise FileNotFoundError(f"{kwargs['filename']} does not exist")
37
41
 
38
- sampleid = kwargs.get("sampleid", None)
39
- adtype = kwargs.get("adtype", "exp")
40
42
  ads = get_ads()
41
- if sampleid is not None:
42
- ads.active_id = sampleid
43
+ if adinfo.sampleid is not None:
44
+ ads.active_id = adinfo.sampleid
43
45
  else:
44
- ads.active_id = f"adata{len(ads.adata_dic[adtype])}"
46
+ ads.active_id = f"adata{len(ads.adata_dic[adinfo.adtype])}"
45
47
 
46
48
  adata.layers["counts"] = adata.X
47
49
  adata.var_names_make_unique()
48
50
  adata.obs_names_make_unique()
49
- ads.set_adata(adata, request=request)
50
- return generate_msg(request, adata, ads)
51
+ ads.set_adata(adata, adinfo=adinfo)
52
+ return generate_msg(adinfo, adata, ads)
51
53
  except ToolError as e:
52
54
  raise ToolError(e)
53
55
  except Exception as e:
@@ -58,15 +60,18 @@ async def read(request: ReadModel):
58
60
 
59
61
 
60
62
  @io_mcp.tool()
61
- async def write(request: WriteModel):
63
+ async def write(
64
+ request: WriteModel,
65
+ adinfo: AdataModel = AdataModel()
66
+ ):
62
67
  """save adata into a file.
63
68
  """
64
69
  try:
65
- result = await forward_request("io_write", request)
70
+ result = await forward_request("io_write", request, adinfo)
66
71
  if result is not None:
67
72
  return result
68
73
  ads = get_ads()
69
- adata = ads.get_adata(request=request)
74
+ adata = ads.get_adata(adinfo=adinfo)
70
75
  kwargs = request.model_dump()
71
76
  sc.write(kwargs["filename"], adata)
72
77
  return {"filename": kwargs["filename"], "msg": "success to save file"}