monkeybread.calc.cell_neighbors_from_masks

monkeybread.calc.cell_neighbors_from_masks(adata, mask_group1, mask_group2, basis='X_spatial', radius=None)

Calculate cell neighbors.

For each cell within a given group of cells, calculate all neighbors that are members of a second group of cells (e.g., for all T cells, calculate all neighbors that are B cells). This function differs from cell_neighbors based on how the two groups are defined. This function accepts two masks (Boolean valued iterables). The first mask defines the cells in the first group (Those indices set to True) and the second mask defines the cells in the second group.

The output takes the form of a dictionary mapping from cells in group1 to cells in group2 that are neighbors. Note, if a cell is present in both group1 and group2, the mapping will be reflexive - the cell may both appear in the keys and the values.

The output takes the form of a dictionary mapping from cells in group1 to cells in group2 that the cell contacts. If a cell is present in both group1 and group2, the mapping will be reflexive - the cell may both appear in the keys and the values.

Parameters:
  • adata (AnnData) – Annotated data matrix.

  • mask_group1 (Iterable[bool]) – An iterable of Boolean values specifying cells in the first group (all indices set to True).

  • mask_group2 (Iterable[bool]) – An iterable of Boolean values specifying cells in the second group (all indices set to True).

  • basis (default: 'X_spatial') – Coordinates in adata.obsm[basis] to use. Defaults to X_spatial.

  • radius (default: None) – The radius in which cells are considered touching. If not provided, will be calculated using half of the average radius of group1 + half of the average radius of group2. This requires width and height columns to be present in adata.obs.

Return type:

Dict[str, Set[str]]

Returns:

: A mapping from cell ids in group1 to sets of cell ids in group2 that are its neighbors (i.e., within radius distance).