dendrogram
Creates hierarchical clustering dendrograms for single-cell RNA sequencing data to visualize relationships between cell groups based on gene expression patterns.
Instructions
Hierarchical clustering dendrogram
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| groupby | Yes | The categorical observation annotation to use for grouping. | |
| n_pcs | No | Use this many PCs. If n_pcs==0 use .X if use_rep is None. | |
| use_rep | No | Use the indicated representation. 'X' or any key for .obsm is valid. | |
| var_names | No | List of var_names to use for computing the hierarchical clustering. If provided, use_rep and n_pcs are ignored. | |
| use_raw | No | Only when var_names is not None. Use raw attribute of adata if present. | |
| cor_method | No | Correlation method to use: 'pearson', 'kendall', or 'spearman'. | pearson |
| linkage_method | No | Linkage method to use for hierarchical clustering. | complete |
| optimal_ordering | No | Reorders the linkage matrix so that the distance between successive leaves is minimal. | |
| key_added | No | By default, the dendrogram information is added to .uns[f'dendrogram_{groupby}']. |
Implementation Reference
- src/scmcp/tool/tl.py:164-177 (handler)Handler function that executes the dendrogram tool. Retrieves sc.tl.dendrogram from tl_func mapping and calls it with parsed arguments on the active AnnData object.def run_tl_func(ads, func, arguments): adata = ads.adata_dic[ads.active] if func not in tl_func: raise ValueError(f"Unsupported function: {func}") run_func = tl_func[func] parameters = inspect.signature(run_func).parameters kwargs = {k: arguments.get(k) for k in parameters if k in arguments} try: res = run_func(adata, **kwargs) add_op_log(adata, run_func, kwargs) except Exception as e: logger.error(f"Error running function {func}: {e}") raise return
- src/scmcp/tool/tl.py:125-142 (helper)Mapping dictionary that associates the 'dendrogram' tool name with scanpy's sc.tl.dendrogram function.tl_func = { "tsne": sc.tl.tsne, "umap": sc.tl.umap, "draw_graph": sc.tl.draw_graph, "diffmap": sc.tl.diffmap, "embedding_density": sc.tl.embedding_density, "leiden": sc.tl.leiden, "louvain": sc.tl.louvain, "dendrogram": sc.tl.dendrogram, "dpt": sc.tl.dpt, "paga": sc.tl.paga, "ingest": sc.tl.ingest, "rank_genes_groups": sc.tl.rank_genes_groups, "filter_rank_genes_groups": sc.tl.filter_rank_genes_groups, "marker_gene_overlap": sc.tl.marker_gene_overlap, "score_genes": sc.tl.score_genes, "score_genes_cell_cycle": sc.tl.score_genes_cell_cycle, }
- src/scmcp/schema/tl.py:416-479 (schema)Pydantic model defining the input schema and validation for the dendrogram tool.class DendrogramModel(JSONParsingModel): """Input schema for the hierarchical clustering dendrogram tool.""" groupby: str = Field( ..., # Required field description="The categorical observation annotation to use for grouping." ) n_pcs: Optional[int] = Field( default=None, description="Use this many PCs. If n_pcs==0 use .X if use_rep is None.", ge=0 ) use_rep: Optional[str] = Field( default=None, description="Use the indicated representation. 'X' or any key for .obsm is valid." ) var_names: Optional[List[str]] = Field( default=None, description="List of var_names to use for computing the hierarchical clustering. If provided, use_rep and n_pcs are ignored." ) use_raw: Optional[bool] = Field( default=None, description="Only when var_names is not None. Use raw attribute of adata if present." ) cor_method: str = Field( default='pearson', description="Correlation method to use: 'pearson', 'kendall', or 'spearman'." ) linkage_method: str = Field( default='complete', description="Linkage method to use for hierarchical clustering." ) optimal_ordering: bool = Field( default=False, description="Reorders the linkage matrix so that the distance between successive leaves is minimal." ) key_added: Optional[str] = Field( default=None, description="By default, the dendrogram information is added to .uns[f'dendrogram_{groupby}']." ) @field_validator('cor_method') def validate_cor_method(cls, v: str) -> str: """Validate correlation method is supported""" valid_methods = ['pearson', 'kendall', 'spearman'] if v.lower() not in valid_methods: raise ValueError(f"cor_method must be one of {valid_methods}") return v.lower() @field_validator('linkage_method') def validate_linkage_method(cls, v: str) -> str: """Validate linkage method is supported""" valid_methods = ['single', 'complete', 'average', 'weighted', 'centroid', 'median', 'ward'] if v.lower() not in valid_methods: raise ValueError(f"linkage_method must be one of {valid_methods}") return v.lower() @field_validator('n_pcs') def validate_n_pcs(cls, v: Optional[int]) -> Optional[int]: """Validate n_pcs is non-negative""" if v is not None and v < 0: raise ValueError("n_pcs must be a non-negative integer") return v
- src/scmcp/tool/tl.py:61-66 (registration)Definition and registration of the MCP Tool object for 'dendrogram', referencing the input schema.# Add dendrogram tool dendrogram_tool = types.Tool( name="dendrogram", description="Hierarchical clustering dendrogram", inputSchema=DendrogramModel.model_json_schema(), )
- src/scmcp/tool/tl.py:145-162 (registration)Registration of the dendrogram_tool in the tl_tools dictionary, which is exposed via the MCP server.tl_tools = { "tsne": tsne_tool, "umap": umap_tool, "draw_graph": draw_graph_tool, "diffmap": diffmap_tool, "embedding_density": embedding_density_tool, "leiden": leiden_tool, "louvain": louvain_tool, "dendrogram": dendrogram_tool, "dpt": dpt_tool, "paga": paga_tool, "ingest": ingest_tool, "rank_genes_groups": rank_genes_groups_tool, "filter_rank_genes_groups": filter_rank_genes_groups_tool, "marker_gene_overlap": marker_gene_overlap_tool, "score_genes": score_genes_tool, "score_genes_cell_cycle": score_genes_cell_cycle_tool, }