coloc module

Focused on differential co-localization

nichesphere.coloc.OvsE_coloc_test(observedColocProbs, expectedColocProbs, cell_types, testDistribution, oneCTinteractions, p=0.05)

Observed vs Expected log2 ratios filtered by p-value obtained from comparing them against a background distribution :param observedColocProbs: observed cell type pair co-localization probabilities in a sample/condition :type observedColocProbs: pd.Series :param expectedColocProbs: expected cell type pair co-localization probabilities in a sample/condition :type expectedColocProbs: pd.Series :param cell_types: list of cell types (sorted as in observedColocProbs) :type cell_types: list :param testDistribution: distribution of log2 observed vs expected ratios obtained from random sampling pairs of single cells

from scRNA data from the same sample/condition (obtained generally from function ‘PIC_OEratios_BGdist’) from the tl module

Parameters:
  • oneCTinteractions (list) – list of single cell interactions (celltype-celltype)

  • p (float (default: 0.05)) – values must have a p-value lower than this to be considered significant

Returns:

OvsE_HMdf – cell types x cell types data frame of log2 observed/expected significant values (scores) for each cell cell interaction

Return type:

pd.DataFrame

nichesphere.coloc.cellCatContained(pair, cellCat)

Check if a cell group (niche/type/category) is contained in a cell type pair

Parameters:
  • pair (list) – cell type list (usually cell type pairs in the form [cellTypeA,cellTypeB])

  • cellCat (list) – list of cell types in a cell group (niche/type/category)

Return type:

True or False

nichesphere.coloc.colocNW(x_diff, adj, cell_group, group=None, group_cmap='tab20', ncols=20, clist=None, nodeSize=None, legend_ax=[0.7, 0.05, 0.15, 0.2], layout='neato', lab_spacing=9, thr=0, alpha=1, fsize=(8, 8), pos=None, edge_scale=1)

(Differential) co-localization network

Parameters:
  • xdiff (pd.DataFrame) – cell types x cell types data frame of significant scores for each cell cell interaction

  • adj (pd.DataFrame) – cell types x cell types adjacency matrix (calculated from the cell cell interaction scores)

  • cell_group (dict) – dictionary with niche names as keys and lists of their corresponding cell types as values

  • group (list (default: None)) – list of nodes whose interaction will be highlighted

  • group_cmap (str (default: 'tab20')) – name of the color map from which the niche colors will be taken

  • ncols (int (default: 20)) – number of colors for the group_cmap

  • clist (list (default: None)) – alternatively , one can input a list of niche colors

  • nodeSize (str (default: None)) – value that will define the size of the nodes. Options are ‘betweeness’, ‘pagerank’ (network statistics)

  • legend_ax (list (default: [0.7, 0.05, 0.15, 0.2])) – legend position in the form [x0, y0, width, height]

  • layout (str (default: 'neato')) – name of the layout to be used. Options are ‘neato’, ‘dot’, ‘kamada_kawai’, ‘spring’, ‘spectral’, ‘circular’, ‘force_atlas2’, ‘fruchterman_reingold’ and ‘random’)

  • lab_spacing (int (default: 9)) – spacing between labels and nodes

  • thr (float (default: 0)) – edge weights absolute value must be above this value for the edge to be shown

  • alpha (float (default: 1)) – edge transparency (from 0 to 1)

  • fsize (tuple) – figure size in the form (x,y)

  • pos (dict) – dictionary containing the calculated 2D positions (x, y coordinates) for every node

  • edge_scale (float) – factor to scale the edge width

Returns:

  • gCol (nx.Graph) – Graph object with cell cell interaction scores as weights

  • Network plot

nichesphere.coloc.diffColoc_test(coloc_pair_sample, sampleTypes, exp_condition, ctrl_condition)

Differential co-localization test with table of scores and p-values as output

Parameters:
  • coloc_pair_sample (pd.DataFrame) – coloc per cell type pair per sample table

  • exp_condition (string) – non control phenotype to test

  • ctrl_condition (string) – control phenotype

  • sampleTypes (pd.DataFrame) – dataframe with sample names and sample types columns named “sample” and “sampleType”

Returns:

df – Dataframe of ranksums test statistic and p-value per cell type pair

Return type:

pd.DataFrame

nichesphere.coloc.getColocProbs(CTprobs, spotSamples)

Get co-localization probabilities (sum across spots of probabilities of each cell type pair being in the same spot)

Parameters:
  • CTprobs (pd.DataFrame) – Dataframe of cell type probabilities per spot

  • spotSamples (pd.Series) – Series indicating the sample to which each spot belongs, with spot ids as index.

Returns:

CTcolocalizationP – concatenated dataframes of cell type pairs co-localization probabilities per sample

Return type:

pd.DataFrame

nichesphere.coloc.reshapeColoc(CTcoloc, oneCTinteractions='', complete=1)

Transforms matrix obtained with getColocProbs into a matrix of CT pairs x samples CTcoloc=previously obtained colocalisation matrix from getColocprobs complete=list with repeated values (ct1_x_ct2 and ct2_x_ct1)

Parameters:
  • CTcoloc (pd.DataFrame) – Concatenated dataframes of cell type pairs co-localization probabilities per sample (obtained via getColocProbs())

  • oneCTinteractions (list) – list of single cell interactions (celltype-celltype)

  • complete (flag (default: 1)) – indicates if the co-localization matrices are complete (1) (not just half) , with repeated values at ct1_x_ct2 and ct2_x_ct1 or half matrices are used as input (0)

Returns:

colocPerSample1 – Probabilities of each cell type pair per sample

Return type:

pd.DataFrame

nichesphere.coloc.spatialCTPlot(adata, cell_types, CTprobs=None, maxCT_col=None, spot_size=1, legend_fontsize=7, title='', legend_loc='right margin', save_name='test.pdf', ax=None)

Plot cell types in spatial data (MERFISH / visium slices)

Parameters:
  • adata (AnnData) – sample specific spatial anndata object

  • cell_types (pd.Series) – categorical series of cell types

  • CTprobs (pd.DataFrame (default: None)) – sample specific cell type probabilities per spot (not needed if there is a cell type column (maxCT_col) in the anndata obs)

  • maxCT_col (string (default: None)) – cell type column in anndata object

  • spot_size (int (default: 1)) – size of spots in the spatial plot

  • legend_fontsize (int (default: 7))

  • title (str)

  • legend_loc (str (default: 'right margin')) – legend location , can switch to ‘on data’

  • save_name (str (default: 'test.pdf'))

  • ax (matplotlib.axes.Axes (default: None)) – The axes object to draw the plot onto.

Return type:

Spatial plot where spots are colored by cell type with highest proportion

nichesphere.coloc.spatialNichePlot(adata, niche_dict, CTprobs, spot_size=1, legend_fontsize=7, title='', legend_loc='right margin', save_name='test.pdf', niche_colors=None, ax=None, vmin=0, vmax=None)

Plot niches in spatial data (MERFISH / visium slices)

Parameters:
  • adata (AnnData) – sample specific spatial anndata object

  • niche_dict (dict) – dictionary with niche names as keys and lists of their corresponding cell types as values

  • CTprobs (pd.DataFrame) – sample specific cell type probabilities per spot

  • spot_size (int (default: 1)) – size of spots in the spatial plot

  • legend_fontsize (int (default: 7))

  • title (str)

  • legend_loc (str (default: 'right margin')) – legend location , can switch to ‘on data’

  • save_name (str (default: 'test.pdf'))

  • niche_colors (pd.Series) – series of colors with niche names as indexes

  • ax (matplotlib.axes.Axes (default: None)) – The axes object to draw the plot onto.

  • vmin (int (default: 0)) – Minimum value in the color scale

  • vmax (int (default: None)) – Maximum value in the color scale

Return type:

Spatial plot where spots are colored by cell type with highest proportion

nichesphere.coloc.spatialSingleCTPlot(adata, cell_type, CTprobs, spot_size=1, legend_fontsize=7, title='', legend_loc='right margin', save_name='test.pdf', ax=None, vmin=0, vmax=None, cmap='magma')

Plot a single cell type proportions in spatial data (MERFISH / visium slices)

Parameters:
  • adata (AnnData) – sample specific spatial anndata object

  • CTprobs (pd.DataFrame (default: None)) – sample specific cell type probabilities per spot (not needed if there is a cell type column (maxCT_col) in the anndata obs)

  • cell_type (str) – cell type to plot

  • spot_size (int (default: 1)) – size of spots in the spatial plot

  • legend_fontsize (int (default: 7))

  • title (str)

  • legend_loc (str (default: 'right margin')) – legend location , can switch to ‘on data’

  • save_name (str (default: 'test.pdf'))

  • ax (matplotlib.axes.Axes (default: None)) – The axes object to draw the plot onto.

  • vmin (int (default: 0)) – Minimum value in the color scale

  • vmax (int (default: None)) – Maximum value in the color scale

  • cmap (str (default: 'magma')) – Name of the color map to be used

Return type:

Spatial plot where spots are colored by probability of the selected cell type