Skip to main content
Glama

neighbors

Compute nearest neighbors distance matrices and construct neighborhood graphs for single-cell RNA sequencing data analysis. Supports customizable parameters for manifold approximation, distance metrics, and kNN search methods.

Instructions

Compute nearest neighbors distance matrix and neighborhood graph

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
key_addedNoKey prefix for storing neighbor results.
knnNoWhether to use hard threshold for neighbor restriction.
methodNoMethod for computing connectivities ('umap' or 'gauss').umap
metricNoDistance metric to use.euclidean
metric_kwdsNoOptions for the distance metric.
n_neighborsNoSize of local neighborhood used for manifold approximation.
n_pcsNoNumber of PCs to use. If None, automatically determined.
random_stateNoRandom seed for reproducibility.
transformerNoApproximate kNN search implementation ('pynndescent' or 'rapids').
use_repNoKey for .obsm to use as representation.

Implementation Reference

  • Generic handler function that executes preprocessing tools including 'neighbors' by calling the corresponding scanpy.pp function (sc.pp.neighbors) with validated arguments and logging the operation.
    def run_pp_func(ads, func, arguments): adata = ads.adata_dic[ads.active] if func not in pp_func: raise ValueError(f"不支持的函数: {func}") run_func = pp_func[func] parameters = inspect.signature(run_func).parameters arguments["inplace"] = True 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 KeyError as e: raise KeyError(f"Can not foud {e} column in adata.obs or adata.var") except Exception as e: raise e return res
  • Pydantic model defining the input parameters and validation for the 'neighbors' tool, used in the tool's inputSchema.
    class NeighborsModel(JSONParsingModel): """Input schema for the neighbors graph construction tool.""" n_neighbors: int = Field( default=15, description="Size of local neighborhood used for manifold approximation.", gt=1, le=100 ) n_pcs: Optional[int] = Field( default=None, description="Number of PCs to use. If None, automatically determined.", ge=0 ) use_rep: Optional[str] = Field( default=None, description="Key for .obsm to use as representation." ) knn: bool = Field( default=True, description="Whether to use hard threshold for neighbor restriction." ) method: Literal['umap', 'gauss'] = Field( default='umap', description="Method for computing connectivities ('umap' or 'gauss')." ) transformer: Optional[str] = Field( default=None, description="Approximate kNN search implementation ('pynndescent' or 'rapids')." ) metric: str = Field( default='euclidean', description="Distance metric to use." ) metric_kwds: Dict[str, Any] = Field( default_factory=dict, description="Options for the distance metric." ) random_state: int = Field( default=0, description="Random seed for reproducibility." ) key_added: Optional[str] = Field( default=None, description="Key prefix for storing neighbor results." ) @field_validator('n_neighbors', 'n_pcs') def validate_positive_integers(cls, v: Optional[int]) -> Optional[int]: """Validate positive integers where applicable""" if v is not None and v <= 0: raise ValueError("must be a positive integer") return v @field_validator('method') def validate_method(cls, v: str) -> str: """Validate method is supported""" if v not in ['umap', 'gauss']: raise ValueError("method must be either 'umap' or 'gauss'") return v @field_validator('transformer') def validate_transformer(cls, v: Optional[str]) -> Optional[str]: """Validate transformer option is supported""" if v is not None and v not in ['pynndescent', 'rapids']: raise ValueError("transformer must be either 'pynndescent' or 'rapids'") return v
  • MCP Tool registration for 'neighbors', specifying name, description, and linking to the input schema.
    neighbors = types.Tool( name="neighbors", description="Compute nearest neighbors distance matrix and neighborhood graph", inputSchema=NeighborsModel.model_json_schema(), )
  • Addition of the 'neighbors' tool to the pp_tools dictionary, making it available for server registration.
    "neighbors": neighbors,
  • Mapping of 'neighbors' tool name to the underlying scanpy.pp.neighbors function in the pp_func dictionary used by the handler.
    "neighbors": sc.pp.neighbors,

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/huang-sh/scmcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server