cellspec.tl.compute_spectrum_from_mask

cellspec.tl.compute_spectrum_from_mask#

cellspec.tl.compute_spectrum_from_mask(adata=None, variant_mask=None, trinuc_contexts=None, key=None)#

Compute 96-channel mutation spectrum from a boolean mask of variants.

Simple function to count trinucleotide contexts for a subset of variants. Works with AnnData objects or directly with trinucleotide context series.

Parameters:
  • adata (ad.AnnData, optional) – AnnData with annotated variants (must have ‘trinuc_type’ in .var)

  • variant_mask (str, pd.Series, or np.ndarray, optional) –

    Boolean mask specifying which variants to count:

    • str: Column name in adata.var

    • pd.Series: Boolean series

    • np.ndarray: Boolean array

    If None, counts all variants.

  • trinuc_contexts (pd.Series, optional) – Trinucleotide contexts directly (if not using adata). Should contain 96-type labels (e.g., ‘ACA>AAA’)

  • key (str, optional) – If provided, stores result in adata.uns[f’spectrum_{key}’]

Return type:

Series

Returns:

pd.Series Counts for each of the 96 mutation types

Examples

>>> import cellspec as spc
>>>
>>> # From AnnData with column name
>>> adata.var["is_somatic"] = adata.var["VAF"] > 0.1
>>> spectrum = spc.tl.compute_spectrum_from_mask(adata, "is_somatic")
>>>
>>> # From AnnData with boolean series
>>> high_qual = (adata.var["QUAL"] > 30) & (adata.var["DP"] > 100)
>>> spectrum = spc.tl.compute_spectrum_from_mask(adata, high_qual)
>>>
>>> # Directly from trinuc_type series
>>> contexts = adata.var.loc[mask, "trinuc_type"]
>>> spectrum = spc.tl.compute_spectrum_from_mask(trinuc_contexts=contexts)
>>>
>>> # Plot with spc.pl.spectrum_from_df
>>> spc.pl.spectrum_from_df(spectrum, normalize=True)

See also

compute_spectrum

Full spectrum computation with more options

pl.spectrum_from_df

Plot spectrum from Series/DataFrame