grassp.tl.calculate_mgsa#
- calculate_mgsa(data, cluster_key='leiden', gene_name_key='Gene_name_canonical', gene_sets=None, species='hsap', obs_key_added='Cell_compartment_mgsa', method='auto', max_active=4, alpha_prior=None, beta_prior=None, min_posterior=0.5, deduplicate_terms=True, n_steps=1000000, n_restarts=5, seed=0, posterior_uns_key=None, return_result=True, inplace=True, verbose=True, **mgsa_kwargs)[source]#
Model-based gene-set analysis (MGSA) per cluster.
The MGSA analogue of
calculate_cluster_enrichment(): for each category indata.obs[cluster_key]it runsmgsa()with the cluster’s genes as the study set and all genes as the population, then records the most probable active compartment. Unlike a per-term hypergeometric test, MGSA explains each cluster’s genes jointly, so overlapping compartments compete and a redundant one (e.g. Lipid droplet vs. Endoplasmic reticulum) is only reported active if it explains genes the other cannot.- Parameters:
- data
AnnData AnnData with proteins as observations.
- cluster_key
str(default:'leiden') Categorical column in
data.obswith cluster labels.- gene_name_key
str(default:'Gene_name_canonical') Column in
data.obswith gene identifiers (matching the gene sets).- gene_sets
Union[str,Mapping[str,Sequence[str]],None] (default:None) Gene-set source passed to
load_gmt();Noneuses the bundled consolidated UniProt compartment sets forspecies.- species
Literal['hsap','mmus','scer'] (default:'hsap') Species code for the bundled gene sets when
gene_sets is None.- obs_key_added
str(default:'Cell_compartment_mgsa') Column to write the top active compartment per cluster to (
NaNif the top posterior is belowmin_posterior).- method
str(default:'auto') Forwarded to
mgsa().method="auto"(default) uses the exact enumeration for the small consolidated compartment vocabulary — fast and variance-free — and only falls back to MCMC for GO-sized gene-set libraries.max_activecaps the exact enumeration (seemgsa()).- max_active
int(default:4) Forwarded to
mgsa().method="auto"(default) uses the exact enumeration for the small consolidated compartment vocabulary — fast and variance-free — and only falls back to MCMC for GO-sized gene-set libraries.max_activecaps the exact enumeration (seemgsa()).- min_posterior
float(default:0.5) Posterior-activity threshold for assigning the top compartment label.
- deduplicate_terms
bool(default:True) Forwarded to
load_gmt(); ifTrue(default) sets with identical gene membership are collapsed to one term, so synonymous/duplicate compartments (e.g.PEROXISOME≡MICROBODYin fine ontologies) do not split the posterior mass and dilute the top compartment belowmin_posterior.- n_steps
int(default:1000000) Forwarded to
mgsa(); only used when the run falls back tomethod="mcmc"(ignored for the exact path).- n_restarts
int(default:5) Forwarded to
mgsa(); only used when the run falls back tomethod="mcmc"(ignored for the exact path).- seed
Optional[int] (default:0) Forwarded to
mgsa(); only used when the run falls back tomethod="mcmc"(ignored for the exact path).- posterior_uns_key
Optional[str] (default:None) unskey for the full (cluster x compartment) posterior activity matrix. Defaults tof"{obs_key_added}_posterior".- return_result
bool(default:True) If
Truereturn the posterior DataFrame.- inplace
bool(default:True) If
Trueannotatedatain place.- verbose
bool(default:True) Print per-cluster progress.
- **mgsa_kwargs
Extra keyword arguments forwarded to
mgsa()(e.g. grids,thin).- alpha_prior callable | None
- beta_prior callable | None
- data
- Return type:
- Returns:
Optional[pandas.DataFrame] The (cluster x compartment) posterior activity matrix if
return_resultelseNone. Wheninplacethe top-compartment labels are written todata.obs[obs_key_added], the marginal posterior matrix todata.uns[posterior_uns_key], and the per-cluster MAP active-set indicator (0/1) todata.uns[f"{obs_key_added}_map"]. The MAP matrix is the recommended input tomgsa_to_cluster_distribution()(use_map=True).