tl module

Helpful tools

nichesphere.tl.PIC_BGdoubletsOEratios(adata_singlets, annot_col)

Generate distribution of O/E ratios for colocalization probabilities of cell type pairs in randomly generated doublets (background dist) for tests in PIC-seq data

Parameters:
  • adata_singlets (AnnData) – anndata object containing singlets (scRNA-seq) data

  • annot_col (str) – name of the cell type annotation column in the anndata object obs

Returns:

OEratios – distribution of O/E ratios for colocalization probabilities of cell type pairs in randomly generated doublets

Return type:

pd.Series

nichesphere.tl.assign_properties(g, communities, colors, pos=None, node_coord_sf=200, simmilarity_weights=False, g_unsigned=None, alpha=1)

Assign properties to network g to visualize with gravis interactive networks

Parameters:
  • g (nx.Graph) – Graph object with cell cell interaction scores as weights

  • communities (list of sets) – cell type communities

  • colors (list) – list of colors (one per community)

  • pos (dict (default: None)) – dictionary of node positions in a layout. Keys are cell types and values are arrays of x,y coordinates

  • node_coord_sf (int (default: 200)) – node coordinate scale factor to adapt to gravis

  • simmilarity_weights (bool (default: False)) – whether or not to have the simmilarity based weights (from the unsigned network)

  • g_unsigned (nx.Graph (default: None)) – Unsigned network generated directly from the adjacency matrix derived from the differential co-localization scores (with nx.from_pandas_adjacency)

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

Returns:

g – Graph object with cell cell interaction scores as weights and added properties (different options for node size scaling, like ‘size_betweeness’, ‘size_pagerank’ and ‘size_pagerank_uw’, node shape, color, x and y coordinates, options for edge weights, like ‘weight_signed’ (from co-localization/communication statistical test), ‘weight_abs’ (absolute values without signs), ‘weight_exp’ (exponentially scaled) and ‘weight_simmilarity’, edge color and size). Edges and nodes visualization options can be managed interactively with th gravis library

Return type:

nx.Graph

nichesphere.tl.cellCatContained(pair, cellCat)

Generate boolean element indicating whether the specified element (cell pair) is contained in a list (category)

Parameters:
  • pair (str) – Evaluated element (cell type pair usually in this context)

  • cellCat (list) – Category to test (EG: list containing a cell type category)

Return type:

True or False

nichesphere.tl.cells_niche_colors(CTs, niche_colors, niche_dict)

Make dataframe of cell types with their corresponding niches and colors

Parameters:
  • CTs (pd.Series or list) – list of cell types

  • niche_colors (pd.Series) – Series of colors in hexadecimal format with niche names as index

  • niche_dict (dictionary) – Dictionary with niche names as keys and cell types as elements

Returns:

niche_df – Dataframe of cell types with their corresponding niches and colors

Return type:

pd.DataFrame

nichesphere.tl.getColocFilter(pairCatDF, adj, oneCTints)

Get filtering object indicating which cell pairs are differentially co-localized

Parameters:
  • pairCatDF (pd.DataFrame) – dataframe of cell pairs and corresponding niche pairs

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

  • oneCTints (list) – list of single cell interactions (celltype->celltype)

Returns:

colocFilt – Dataframe with cell type pairs in the form ‘celltype->celltype’ as index and a ‘filter’ column indicating which cell pairs are differentially co-localized with a 1 (or 0 if they are not)

Return type:

pd.DataFrame

nichesphere.tl.getExpectedColocProbsFromSCs(sc_adata, sample, cell_types, sc_data_sampleCol, sc_adata_annotationCol)

Compute the expected probability of each cell type pair to occur in a specific condition by multiplying cell type proportions from the single cell data

Parameters:
  • sc_adata (AnnData) – anndata object containing singlets (scRNA-seq) data

  • sample (str) – name of the sample/condition to be tested

  • cell_types (pd.Series or list) – list/series of cell types

  • sc_data_sampleCol (str) – name of the column in the obs of the anndata object where the sample/condition is indicated

  • sc_adata_annotationCol (str) – name of the cell type column in the obs of the anndata object

Returns:

scCTpairsProbs – Dataframe of expected co-localization probabilities per cell type pair. Cell type pairs as indexes and a ‘count’ column

Return type:

pd.DataFrame

nichesphere.tl.get_pairCatDFdir(niches_df)

Get dataframe of cell pair to niche pair correspondence

Parameters:

niches_df (pd.Dataframe) – Dataframe of cells, corresponding niche and corresponding niche color

Returns:

pairCatDFdir – dataframe of cell pairs and corresponding niche pairs

Return type:

pd.DataFrame

nichesphere.tl.get_spot_ct_props(spot_cell_props, sc_ct)

Get cell type proportions per spot by summing the probabilities of cells of the same kind in each spot

Parameters:
  • spot_cell_props (pd.DataFrame) – Dataframe containing probabilities of mapping each cell to each spot (spot ids = index , cell ids = columns)

  • sc_ct (pd.Series) – Series of cell type per cell with cell ids as indexes

Returns:

spot_mapped_cts – Dataframe containing probabilities of mapping each cell type to each spot (spot ids = index , cell types = columns)

Return type:

pd.DataFrame

nichesphere.tl.processCTKRoutput(ctkrTbl)

Remove suffixes ‘|L’ , ‘|R’ and ‘|TF’ from cell - cell communication (CrossTalkeR) tables

Parameters:

ctkrTbl (pd.DataFrame) – Condition specific cell - cell communication table (output from CrossTalkeR)

Returns:

ctkrTbl – Same cell - cell communication table without the suffixes mentioned above

Return type:

pd.DataFrame

nichesphere.tl.pval_filtered_HMdf(testDF, oneCTinteractions, p, cell_types)

Get dataframe for scores heatmap from dataframe obtained from diffColoc_test()

Parameters:
  • testDF (pd.DataFrame) – dataframe with cell pairs as indexes and columns ‘statistic’ and ‘p-value’ (often resulting from diffColoc_test())

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

  • p (float) – threshold p-value to filter

  • cell_types (pd.Series or list) – list/series of cell types

Returns:

testHM – Dataframe of cell types x cell types with significant ‘statistic’ values

Return type:

pd.DataFrame

nichesphere.tl.unique(array)

get unique elements in array without re-sorting

Parameters:

array (np.array)

Returns:

uniq[index.argsort()]

Return type:

np.array of unique elements of the original array in original order