nichecompass.benchmarking.compute_cas

nichecompass.benchmarking.compute_cas(adata, cell_type_key='cell_type', batch_key=None, spatial_knng_key='spatial_knng', latent_knng_key='nichecompass_latent_knng', spatial_key='spatial', latent_key='nichecompass_latent', n_neighbors=15, n_perms=1000, n_jobs=1, seed=0)

Compute the Cell Type Affinity Similarity (CAS). The CAS measures how accurately the latent nearest neighbor graph preserves cell-type-pair edges from the spatial (ground truth) nearest neighbor graph. A value of ‘1’ indicates perfect cell-type-pair similarity and a value of ‘0’ indicates no cell-type-pair similarity at all. The CAS is a variation of the Cell Type Affinity Distance which was first introduced by Lohoff, T. et al. Integration of spatial and single-cell transcriptomic data elucidates mouse organogenesis. Nat. Biotechnol. 40, 74–85 (2022).

If a ´batch_key´ is provided, separate spatial nearest neighbor graphs per batch will be computed and are then combined as disconnected components by padding with 0s.

If existent, uses precomputed nearest neighbor graphs stored in ´adata.obsp[spatial_knng_key + ‘_connectivities’]´ and ´adata.obsp[latent_knng_key + ‘_connectivities’]´. Alternatively, computes them on the fly using ´spatial_key´, ´latent_key´ and ´n_neighbors´, , and stores them in ´adata.obsp[spatial_knng_key + ‘_connectivities’]´ and ´adata.obsp[latent_knng_key + ‘_connectivities’]´ respectively.

Note that the used neighborhood enrichment implementation from squidpy slightly deviates from the original method and we construct nearest neighbor graphs using the original spatial coordinates and the latent representation from a model respectively to compute the similarity. The cell type affinity matrices, also called cell-cell contact (ccc) maps are stored in the AnnData object.

Parameters:
  • adata (AnnData) – AnnData object with cell type annotations stored in ´adata.obs[cell_type_key]´, precomputed nearest neighbor graphs stored in ´adata.obsp[spatial_knng_key + ‘_connectivities’]´ and ´adata.obsp[latent_knng_key + ‘_connectivities’]´ or spatial coordinates stored in ´adata.obsm[spatial_key]´ and the latent representation from a model stored in ´adata.obsm[latent_key]´.

  • cell_type_key (str (default: 'cell_type')) – Key under which the cell type annotations are stored in ´adata.obs´.

  • batch_key (Optional[str] (default: None)) – Key under which the batches are stored in ´adata.obs´. If ´None´, the adata is assumed to only have one unique batch.

  • spatial_knng_key (str (default: 'spatial_knng')) – Key under which the spatial nearest neighbor graph is / will be stored in ´adata.obsp´ with the suffix ‘_connectivities’.

  • latent_knng_key (str (default: 'nichecompass_latent_knng')) – Key under which the latent nearest neighbor graph is / will be stored in ´adata.obsp´ with the suffix ‘_connectivities’.

  • spatial_key (Optional[str] (default: 'spatial')) – Key under which the spatial coordinates are stored in ´adata.obsm´.

  • latent_key (Optional[str] (default: 'nichecompass_latent')) – Key under which the latent representation from a model is stored in ´adata.obsm´.

  • n_neighbors (Optional[int] (default: 15)) – Number of neighbors used for the construction of the nearest neighbor graphs from the spatial coordinates and the latent representation from a model in case they are constructed.

  • n_perms (int (default: 1000)) – Number of permutations used for the neighborhood enrichment score calculation.

  • n_jobs (int (default: 1)) – Number of jobs to use for parallelization of neighbor search.

  • seed (int (default: 0)) – Random seed for reproducibility.

Return type:

float

Returns:

cas: Matrix similarity between the latent cell type affinity matrix and the spatial (ground truth) cell type affinity matrix (or batch-aggregated spatial cell type affinity matrices) as measured by one minus the size-normalied Frobenius norm of the element-wise matrix differences.