pl_pca_variance_ratio
Plot PCA variance ratios to visualize explained variance in single-cell RNA sequencing data, aiding dimensionality reduction and analysis interpretation. Supports customizable plotting parameters.
Instructions
Plot the PCA variance ratio to visualize explained variance.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| color_map | No | Color map to use for continuous variables. | |
| figsize | No | Figure size. Format is (width, height). | |
| legend_fontoutline | No | Line width of the legend font outline in pt. | |
| legend_fontsize | No | Numeric size in pt or string describing the size. | |
| legend_fontweight | No | Legend font weight. A numeric value in range 0-1000 or a string. | bold |
| legend_loc | No | Location of legend, either 'on data', 'right margin' or a valid keyword for the loc parameter. | right margin |
| log | No | Plot on logarithmic scale. | |
| n_pcs | No | Number of PCs to show. | |
| palette | No | Colors to use for plotting categorical annotation groups. | |
| vcenter | No | The value representing the center of the color scale. | |
| vmax | No | The value representing the upper limit of the color scale. | |
| vmin | No | The value representing the lower limit of the color scale. |
Implementation Reference
- src/scmcp/tool/pl.py:179-216 (handler)The main handler function that executes the tool logic for all pl tools, including pl_pca_variance_ratio, by looking up the scanpy function in pl_func and calling it with prepared arguments from the input schema.def run_pl_func(ads, func, arguments): """ Execute a Scanpy plotting function with the given arguments. Parameters ---------- adata : AnnData Annotated data matrix. func : str Name of the plotting function to execute. arguments : dict Arguments to pass to the plotting function. Returns ------- The result of the plotting function. """ adata = ads.adata_dic[ads.active] if func not in pl_func: raise ValueError(f"Unsupported function: {func}") run_func = pl_func[func] parameters = inspect.signature(run_func).parameters kwargs = {k: arguments.get(k) for k in parameters if k in arguments} if "title" not in parameters: kwargs.pop("title", False) kwargs.pop("return_fig", True) kwargs["show"] = False kwargs["save"] = ".png" try: fig = run_func(adata, **kwargs) fig_path = set_fig_path(func, **kwargs) add_op_log(adata, run_func, kwargs) return fig_path except Exception as e: raise e return fig_path
- src/scmcp/schema/pl.py:816-836 (schema)Pydantic model defining the input schema for the pl_pca_variance_ratio tool, with parameters n_pcs and log.class PCAVarianceRatioModel(BaseVisualizationModel): """Input schema for the pca_variance_ratio plotting tool.""" n_pcs: int = Field( default=30, description="Number of PCs to show.", gt=0 ) log: bool = Field( default=False, description="Plot on logarithmic scale." ) @field_validator('n_pcs') def validate_n_pcs(cls, v: int) -> int: """Validate n_pcs is positive""" if v <= 0: raise ValueError("n_pcs must be a positive integer") return v
- src/scmcp/tool/pl.py:113-117 (registration)Definition and registration of the MCP Tool instance for pl_pca_variance_ratio, including name, description, and schema reference.pl_pca_variance_ratio = types.Tool( name="pl_pca_variance_ratio", description="Plot the PCA variance ratio to visualize explained variance.", inputSchema=PCAVarianceRatioModel.model_json_schema(), )
- src/scmcp/tool/pl.py:121-138 (handler)Mapping dictionary pl_func that associates the tool name 'pl_pca_variance_ratio' with the core scanpy handler function sc.pl.pca_variance_ratio, used by run_pl_func.pl_func = { "pl_pca": sc.pl.pca, "pl_embedding": sc.pl.embedding, # Add the new embedding function "diffmap": sc.pl.diffmap, "pl_violin": sc.pl.violin, "pl_stacked_violin": sc.pl.stacked_violin, "pl_heatmap": sc.pl.heatmap, "pl_dotplot": sc.pl.dotplot, "pl_matrixplot": sc.pl.matrixplot, "pl_tracksplot": sc.pl.tracksplot, "pl_scatter": sc.pl.scatter, "embedding_density": sc.pl.embedding_density, "rank_genes_groups": sc.pl.rank_genes_groups, "pl_rank_genes_groups_dotplot": sc.pl.rank_genes_groups_dotplot, # Add function mapping "pl_clustermap": sc.pl.clustermap, "pl_highly_variable_genes": sc.pl.highly_variable_genes, "pl_pca_variance_ratio": sc.pl.pca_variance_ratio, }
- src/scmcp/tool/pl.py:141-159 (registration)Tools dictionary pl_tools that registers the pl_pca_variance_ratio Tool instance for use in the MCP server.pl_tools = { "pl_pca": pl_pca_tool, "pl_embedding": pl_embedding, # Add the new embedding tool # "diffmap": diffmap, "pl_violin": pl_violin, "pl_stacked_violin": pl_stacked_violin, "pl_heatmap": pl_heatmap, "pl_dotplot": pl_dotplot, "pl_matrixplot": pl_matrixplot, "pl_tracksplot": pl_tracksplot, "pl_scatter": pl_scatter, # "embedding_density": embedding_density, # "spatial": spatial, # "rank_genes_groups": rank_genes_groups, "pl_rank_genes_groups_dotplot": pl_rank_genes_groups_dotplot, # Add tool mapping # "pl_clustermap": pl_clustermap, "pl_highly_variable_genes": pl_highly_variable_genes, "pl_pca_variance_ratio": pl_pca_variance_ratio, }