netmap.downstream.gene_clustering.add_clusters_to_adata

netmap.downstream.gene_clustering.add_clusters_to_adata(adata, dist_linkage, cutoff_distance, cluster_var='correlation_cluster', genes=None)[source]

Cut the dendrogram and annotate adata.var with flat cluster assignments.

Applies scipy.cluster.hierarchy.fcluster at cutoff_distance to the provided linkage matrix and merges the resulting integer cluster labels into adata.var under cluster_var. If cutoff_distance is None, a -1 sentinel column is added instead and a warning is printed.

Parameters:
  • adata (anndata.AnnData) – AnnData object whose .var will receive the cluster label column. Must be the same object (or compatible) as the one used to compute dist_linkage.

  • dist_linkage – Linkage matrix as returned by get_hierarchical_clustering() (or any scipy.cluster.hierarchy linkage function).

  • cutoff_distance (float or None) – Height at which to cut the dendrogram. Pass None to skip clustering and fill with -1.

  • cluster_var (str) – Name of the column added to adata.var. Defaults to 'correlation_cluster'.

  • genes (list or None) – Gene subset used when building dist_linkage; restricts which rows of adata.var are matched. Defaults to None (all genes).

Returns:

The input adata with cluster_var merged into .var (outer join, so genes absent from the linkage receive NaN).

Return type:

anndata.AnnData