grassp.tl.knn_annotation

Contents

grassp.tl.knn_annotation#

knn_annotation(data, obs_ann_col, key_added='consensus_graph_annotation', exclude_category=None, inplace=True)[source]#

Propagate categorical annotations along the k-NN graph.

For each observation the function inspects its neighbourhood in adata.obsp['distances'] (generated by scanpy.pp.neighbors()) and assigns the majority category found in obs_ann_col. Ties are broken arbitrarily using pandas.DataFrame.mode().

Parameters:
data AnnData

anndata.AnnData with a populated neighbour graph (distances or connectivities).

obs_ann_col str

Observation column containing the source annotations to be propagated.

key_added str (default: 'consensus_graph_annotation')

Name of the new column that will hold the consensus annotation (default "consensus_graph_annotation").

exclude_category Union[str, List[str], None] (default: None)

One or multiple category labels that should be ignored when computing the neighbourhood majority (useful for unknown / NA categories).

inplace bool (default: True)

If True (default) modify data in place. Otherwise return a copy with the additional column.

Return type:

AnnData | None

Returns:

Modified object when inplace is False with a new column in .obs[key_added].