Skip to main content
🔍

Statescope API Reference

1 · Construction helpers

Initialize_Statescope

Initialise a Statescope model — always your first call.

ParameterTypeDefaultDescription
Bulkpd.DataFramerequiredLinear counts, library-size corrected to 10 k per sample.
Signaturepd.DataFrame | AnnData | NoneNoneCustom single-cell signature. When None, a pre-built signature is downloaded (see TumorType/Ncelltypes).
TumorTypestr(empty)Folder key for a pre-built signature (e.g. NSCLC, PDAC).
Ncelltypesint | (empty)(empty)Choose one of the available cell-type resolutions.
MarkerListlist[str] | NoneNoneUse only these genes as markers instead of running AutoGeneS.
celltype_keystrcelltypeColumn holding cell-type labels inside an AnnData signature.
n_highly_variableint | None3000How many HVGs AutoGeneS should receive (None → let AutoGeneS decide).
fixed_n_featuresint | NoneNoneHard cap on the number of marker genes selected by AutoGeneS.
drop_sigdiffboolFalseIf True, drop genes with large bulk vs scRNA-seq expression differences.
Ncoresint10Maximum parallel workers passed down to BLADE / cNMF.

Returns → a fully initialised Statescope object.


2 · Core class — Statescope

2.1 Deconvolution

Statescope.Deconvolution(
Ind_Marker=None,
Alpha=1, Alpha0=1000, Kappa0=1, sY=1,
Nrep=10, Njob=10, fsel=0, Update_SigmaY=False,
Init_Trust=10, Expectation=None,
Temperature=None, IterMax=100,
)
ParameterTypeDefaultDescription
Ind_Markerlist[int] | NoneNoneExplicit marker-gene indices to force into the model.
Alphaint1Dirichlet prior on cell-type fractions.
Alpha0int1000Prior on gene-level means.
Kappa0int1Prior on gene-level variances.
sYint1Prior scaling of bulk variance.
Nrepint10Random BLADE restarts (best objective kept).
Njobint10Parallel jobs (capped at Ncores).
fselint0Marker-subset selection flag (experimental).
Update_SigmaYboolFalseRe-estimate ΣY every EM step.
Init_Trustint10Weight of fraction priors when provided.
Expectationpd.DataFrame | dict | NoneNonePrior fractions (cell-level DF) or grouped dict {"Group": …, "Expectation": …}.
Temperature[float,float] | NoneNoneStart/stop temperatures for simulated annealing.
IterMaxint100Upper limit on optimisation iterations.

Adds Fractions and BLADE fields to the object.


2.2 Refinement

Statescope.Refinement(weight=100, GeneList=None)
ParameterTypeDefaultDescription
weightint100Down-weights the fraction objective when purifying expression.
GeneListlist[str] | NoneNoneRestrict refinement to this gene subset.

Adds GEX, Omega and BLADE_final.


2.3 State discovery

Statescope.StateDiscovery(
celltype=None,
K=None,
weighing="Omega",
n_iter=10,
n_final_iter=100,
min_cophenetic=0.9,
max_clusters=10,
)
ParameterTypeDefaultDescription
celltypestr | list[str] | NoneNoneWhich cell types to analyse (None → all).
Kint | list[int] | dict[str,int] | NoneNoneFixed k per cell type (None = auto).
weighingstrOmega"Omega" | "OmegaFractions" | "centering" | "no_weighing".
n_iterint10Initial random restarts for cNMF.
n_final_iterint100Additional restarts for the chosen k.
min_copheneticfloat0.9Cut-off to accept a candidate k.
max_clustersint10Upper bound when searching for k.

Adds StateScores, StateLoadings, cNMF, CopheneticCoefficients.


3 · Utility helpers

FunctionParametersDescription
Extract_GEX(model, celltype)celltype :strReturn purified expression (samples × genes) for one cell type.
Extract_StateScores(model, celltype=None)celltype :str|NoneConcatenate all state-score matrices or just one cell type.
Extract_StateLoadings(model, celltype=None)celltype :str|NoneReturn gene-loading matrix (all states or one ct).
Create_Cluster_Matrix(GEX, Omega, Fractions, celltype, weighing=Omega)weighing options aboveBuild the scaled matrix used by clustering / t-SNE.
Check_Bulk_Format(Bulk)Assert / repair bulk matrix (10 k normalisation, duplicates, negatives).
Check_Signature_validity(Signature)Ensure the IsMarker column exists when passing a DF.
Check_Expectation_validity(Expectation, …)Validate prior fraction matrices or grouped priors.

4 · Plotting helpers

All functions draw directly with matplotlib + seaborn; nothing is returned.

FunctionKey parametersWhat you get
Heatmap_Fractions(model)Clustered heat-map of sample × cell-type fractions.
Heatmap_GEX(model, celltype)celltype :strHeat-map of purified expression for a single cell type.
Heatmap_StateScores(model, col_width=0.35, …)Autoscale & labelledState-score heat-map with state numbers colour-boxed.
Heatmap_StateLoadings(model, top_genes=None)top_genes :int|NoneGene × state loadings, grouped by cell type.
Plot_CopheneticCoefficients(model)k vs cophenetic coefficient (red dot = chosen k).
BarPlot_StateLoadings(model, top_genes=1)top_genes :intHorizontal bars; annotate top genes per state.
TSNE_AllStates(model, weighing=Omega, perplexity=5, …)show_samples :bool etc.Global t-SNE with dominant state numbers inside dots.
TSNE_CellTypes(model, celltype=None, weighing=Omega, …)show_samples :bool etc.Per-cell-type t-SNE; prints sample names optionally.

Common t-SNE parameters

NameDefaultDescription
perplexity5Auto-capped at n samples – 1.
n_iter1000Gradient-descent steps.
point_size80 / 150Scatter-marker size (global / per-ct).
random_state42Re-producible layout.

5 · Mini cheat-sheet

model = Initialize_Statescope(Bulk_df, TumorType="NSCLC")
model.Deconvolution()
model.Refinement()
model.StateDiscovery(K=3) # force k = 3 everywhere

Heatmap_Fractions(model)
Heatmap_StateScores(model)
TSNE_AllStates(model, show_samples=True)