Spatial

spatial_distribution

class spatialtis.spatial_distribution(data, method='nns', min_cells=5, pval=0.01, r=0.1, resample=500, quad=None, grid_size=None, **kwargs)[source]

Cell distribution pattern

There are three type of distribution pattern (0 if no cells)

  • Random (1)

  • Regular (2)

  • Cluster (3)

Three methods are provided

Random

Regular

Cluster

Index of dispersion: ID

ID = 1

ID < 1

ID > 1

Morisita’s index of dispersion: I

I = 1

I < 1

I > 1

Clark and Evans aggregation index: R

R = 1

R > 1

R < 1

Parameters
  • data (anndata._core.anndata.AnnData) – AnnData object to perform analysis

  • method (Optional[str]) – “vmr”, “quad”, and “nns” (Default: “nns”)

  • min_cells (int) – The minimum number of the specific type of cells in a ROI to perform analysis

  • pval (float) – The p-value threshold to determine significance

  • r (Union[int, float]) – Only use when method=”vmr”, determine diameter of sample window, should be in [0, 1], default is 0.1 this take 1/10 of the shortest side of the ROI as the diameter.

  • resample (int) – Only use when method=”vmr”, the number of random permutations to perform

  • quad (Optional[Tuple[int, int]]) – Only use when method=”quad”, how to perform rectangle tessellation. Default is (10, 10), this will use a 10*10 grid to perform tessellation.

  • grid_size (Optional[Union[int, float]]) – Only use when method=”quad”, the side of grid when perform rectangle tessellation.

  • **kwargs – Pass to spatialtis.abc.AnalysisBase

“quad” is quadratic statistic, it cuts a ROI into few rectangles, quad=(10,10) means the ROI will have 10*10 grid.

spatial_heterogeneity

class spatialtis.spatial_heterogeneity(data, method='leibovici', base=None, d=None, cut=None, compare=None, **kwargs)[source]

Evaluate tissue heterogeneity based on entropy

Entropy describes the amount of information.

  • Shannon entropy (No spatial info included): To compare the difference within a group (eg. different samples from same tumor), Kullback–Leibler divergences for each sample within the group are computed, smaller value indicates less difference within group.

  • Leibovici entropy: You can specific the distance threshold to determine co-occurrence events.

  • Altieri entropy: You can specific the distance interval to determine co-occurrence events.

Parameters
  • data (anndata._core.anndata.AnnData) – AnnData object to perform analysis

  • method (Optional[str]) – “shannon”, “leibovici” and “altieri” (Default: “leibovici”)

  • base (Optional[Union[int, float]]) – The log base

  • d (Optional[int]) – The distance threshold to determine co-occurrence events (method=”leibovici”)

  • cut (Optional[Union[int, List, Tuple, pandas.core.series.Series, numpy.ndarray]]) – Distance interval (method=”altieri”)

  • compare (Optional[str]) – Compute Kullback-Leibler divergences based on which level (method=”shannon”)

  • **kwargs – Pass to spatialtis.abc.AnalysisBase

hotspot

class spatialtis.hotspot(data, selected_types=None, search_level=1, grid_size=50, pval=0.01, **kwargs)[source]

Getis-ord hotspot detection

Used to identify cells that cluster together.

Parameters
  • data (anndata._core.anndata.AnnData) – AnnData object to perform analysis

  • selected_types (Optional[Union[List, Tuple, pandas.core.series.Series, numpy.ndarray]]) – Select your interested cell types

  • search_level (int) – How deep the search level to reach

  • grid_size (int) – Length of the side of square grid

  • pval (float) – The p-value threshold to determine significance

  • kwargs – Pass to spatialtis.abc.AnalysisBase

find_neighbors

class spatialtis.find_neighbors(data, expand=None, scale=None, count=True, use_shape=False, **kwargs)[source]

To find the neighbors of each cell

KD-tree is used when cells are points

R-tree is used when cells are polygons (use_shape=True)

Parameters
  • data (anndata._core.anndata.AnnData) – AnnData object to perform analysis

  • expand (Optional[Union[int, float]]) – If the cell has shape, it means how much units to expand each cell; If the cell is point, it’s the search radius;

  • scale (Optional[Union[int, float]]) – How much to scale each cell, only if cell has shape

  • count (Optional[bool]) – Count the number of neighbors for each cell (Default: True)

  • use_shape (Optional[bool]) – Cell representation is shape instead of point (Default: False)

  • **kwargs – Pass to spatialtis.abc.AnalysisBase

neighborhood_analysis

class spatialtis.neighborhood_analysis(data, method='pval', resample=1000, pval=0.01, order=False, **kwargs)[source]

Profiling cell-cell interaction using permutation test

Neighborhood analysis tells you the relationship between different type of cells

  • Association (1)

  • Avoidance (-1)

  • No relationship (0)

This method is implemented in Rust, it executes in parallel automatically.

Parameters
  • data (anndata._core.anndata.AnnData) – AnnData object to perform analysis

  • method (Optional[str]) – “pval” and “zscore” (Default: “pval”)

  • resample (int) – Number of times to perform resample

  • pval (float) – The p-value threshold to determine significance

  • order (bool) – If False, (Cell_A, Cell_B) and (Cell_B, Cell_A) are the same interaction (Default: False)

  • **kwargs – Pass to spatialtis.abc.AnalysisBase

spatial_enrichment_analysis

class spatialtis.spatial_enrichment_analysis(data, threshold=None, layers_key=None, selected_markers=None, resample=500, pval=0.01, order=False, **kwargs)[source]

Profiling markers spatial enrichment using permutation test

Similar to neighborhood analysis which tells you the relationship between different type of cells. This analysis tells you the spatial relationship between markers.

This method is implemented in Rust, it executes in parallel automatically.

Parameters
  • data (anndata._core.anndata.AnnData) – AnnData object to perform analysis

  • threshold (Optional[float]) – The expression level to determine whether a marker is positive

  • layers_key (Optional[str]) – The layer in AnnData to perform analysis

  • selected_markers (Optional[Union[List, Tuple, pandas.core.series.Series, numpy.ndarray]]) – Select your interested markers

  • resample (int) – Number of times to perform resample

  • pval (float) – The p-value threshold to determine significance

  • order (bool) – If False, (Cell_A, Cell_B) and (Cell_B, Cell_A) are the same interaction (Default: False)

  • **kwargs – Pass to spatialtis.abc.AnalysisBase

spatial_co_expression

class spatialtis.spatial_co_expression(data, method='spearman', use_cell_type=False, selected_types=None, selected_markers=None, layers_key=None, exp_std_cutoff=1.0, pval=0.01, corr_cutoff=0.5, **kwargs)[source]

Identifying spatial co-expression markers using correlation

The correlation is calculated within pairs of neighbor cells

Parameters
  • data (anndata._core.anndata.AnnData) – AnnData object to perform analysis

  • method (Optional[str]) – “spearman” or “pearson” (Default: “spearman”)

  • selected_markers (Optional[Union[List, Tuple, pandas.core.series.Series, numpy.ndarray]]) – Select your interested markers

  • layers_key (Optional[str]) – The layer in AnnData to perform analysis

  • pval (Union[int, float]) – The p-value threshold to determine significance

  • use_cell_type (bool) –

  • selected_types (Optional[Union[List, Tuple, pandas.core.series.Series, numpy.ndarray]]) –

  • exp_std_cutoff (float) –

  • corr_cutoff (Union[int, float]) –

cell_community

class spatialtis.cell_community(data, partition_type=None, partition_kwargs=None, **kwargs)[source]

Spatial communities detection

Here we use Leiden graph cluster algorithm

Parameters
  • data (anndata._core.anndata.AnnData) – AnnData object to perform analysis

  • partition_type (Optional[Any]) – The leidenalg partition type

  • partition_kwargs (Optional[Dict]) – Pass to leidenalg.find_partition

  • **kwargs – Pass to spatialtis.abc.AnalysisBase

NCDMarkers

class spatialtis.NCDMarkers(data, use_cell_type=False, importance_cutoff=0.5, exp_std_cutoff=1.0, pval=0.01, selected_markers=None, layers_key=None, tree_kwargs=None, **kwargs)[source]

Identify neighbor cells dependent marker

This method tells you the dependency and correlation between markers and its neighbor cell type. The dependency is calculated by building a gradiant boosting tree (in here XGBoost) to determine the feature importance. And the the spearman correlation is calculated.

A reasonable std cutoff should be set, the marker expression need to have certain degree of variance.

Parameters
  • data (anndata._core.anndata.AnnData) – AnnData object to perform analysis

  • exp_std_cutoff (Union[int, float]) – Standard deviation, threshold to filter out markers that are not variant enough

  • pval (Union[int, float]) – The p-value threshold to determine significance

  • selected_markers (Optional[Union[List, Tuple, pandas.core.series.Series, numpy.ndarray]]) – Select your interested markers

  • layers_key (Optional[str]) – The layer in AnnData to perform analysis

  • tree_kwargs (Optional[Dict]) – The keyword arguments that pass to the boosting tree class, (Default: n_jobs=-1, random_state=0)

  • **kwargs – Pass to spatialtis.abc.AnalysisBase

  • use_cell_type (bool) –

  • importance_cutoff (Union[int, float]) –

NMDMarkers

class spatialtis.NMDMarkers(data, exp_std_cutoff=1.0, pval=0.01, selected_markers=None, layers_key=None, tree_kwargs=None, **kwargs)[source]

Identify neighbor markers dependent marker

Similar to NCDMarkers

Parameters
  • data (anndata._core.anndata.AnnData) – AnnData object to perform analysis

  • exp_std_cutoff (Union[int, float]) – Standard deviation, threshold to filter out markers that are not variant enough

  • pval (float) – The p-value threshold to determine significance

  • selected_markers (Optional[Union[List, Tuple, pandas.core.series.Series, numpy.ndarray]]) – Select your interested markers

  • layers_key (Optional[str]) – The layer in AnnData to perform analysis

  • tree_kwargs (Optional[Dict]) – The keyword arguments that pass to the boosting tree class, (Default: n_jobs=-1, random_state=0)

  • **kwargs – Pass to spatialtis.abc.AnalysisBase