ecotyper.recovery_steps

Cell state and ecotype recovery (pipeline/state_recovery_*.R, ecotypes_recovery*.R, spatial_states.R, spatial_ecotypes.R).

All functions take and return pandas objects laid out like the R objects (genes x samples matrices, data frames with an ID column) so every intermediate file of the R pipeline can be reproduced. r_exact=True reproduces the places where the R pipeline writes a table with 15 significant digits and the next script reads it back, which is necessary for bit-identical results downstream.

Functions

align_rows(expr, genes)

data[match(genes, rownames(data)), ]; rownames = genes; NA -> 0.

average_fc_marker_genes(data, gene_info, ...)

averge_FC_marker_genes (lib/ecotyper.R).

prepare_bulk_expression(expr)

data[is.na(data)] = 0; log2(x + 1) if all values >= 0 and max > 50.

read_clinical(sample_list, annotation)

read_clinical (lib/misc.R): annotation rows matched to the samples.

recover_ecotypes(state_results, ...[, r_exact])

ecotypes_recovery.R (bulk).

recover_ecotypes_scrna(state_results, model, ...)

ecotypes_recovery_scRNA.R: ecotype abundance per sample (column Sample of the cell annotation) from per-cell initial state assignments.

recover_states(expr, ct, rng[, annotation, ...])

Recover the cell states of one cell type (state_recovery_{bulk,visium,scRNA}.R).

spatial_ecotypes(wide, ecotypes, *[, r_exact])

spatial_ecotypes.R: per-spot ecotype abundances (ecotype_abundances.txt).

spatial_states(state_results, cell_types, ...)

spatial_states.R: per-spot state abundances weighted by CSx fractions.

Classes

EcotypeRecovery(combined_state_abundances, ...)

StateRecovery(cell_type, expression, scaled, ...)

ecotyper.recovery_steps.prepare_bulk_expression(expr)[source]

data[is.na(data)] = 0; log2(x + 1) if all values >= 0 and max > 50.

Parameters:

expr (DataFrame)

Return type:

DataFrame

ecotyper.recovery_steps.align_rows(expr, genes)[source]

data[match(genes, rownames(data)), ]; rownames = genes; NA -> 0.

Parameters:

expr (DataFrame)

Return type:

DataFrame

ecotyper.recovery_steps.read_clinical(sample_list, annotation)[source]

read_clinical (lib/misc.R): annotation rows matched to the samples.

Parameters:

annotation (DataFrame | None)

Return type:

DataFrame

class ecotyper.recovery_steps.StateRecovery(cell_type: 'str', expression: 'pd.DataFrame', scaled: 'pd.DataFrame', H: 'pd.DataFrame', initial_abundances: 'pd.DataFrame', initial_assignment: 'pd.DataFrame', abundances: 'pd.DataFrame', assignment: 'pd.DataFrame', niter: 'int', annotation: 'pd.DataFrame | None' = None, heatmap_data: 'pd.DataFrame | None' = None, heatmap_top_ann: 'pd.DataFrame | None' = None, extra: 'dict' = <factory>)[source]

Bases: object

Parameters:
cell_type: str
expression: DataFrame
scaled: DataFrame
H: DataFrame
initial_abundances: DataFrame
initial_assignment: DataFrame
abundances: DataFrame
assignment: DataFrame
niter: int
annotation: DataFrame | None = None
heatmap_data: DataFrame | None = None
heatmap_top_ann: DataFrame | None = None
extra: dict
ecotyper.recovery_steps.recover_states(expr, ct, rng, annotation=None, *, kind='bulk', exact=True, threads=1)[source]

Recover the cell states of one cell type (state_recovery_{bulk,visium,scRNA}.R).

expr is the genes x samples matrix after the dataset-level preparation (NA -> 0 and optional log2 for bulk/Visium via prepare_bulk_expression(); per-cell-type log2 for scRNA). rng is consumed exactly as R consumes its RNG (one rnmf draw). exact=False runs NMFpredict with BLAS updates (see ecotyper.nmf.run_brunet()). annotation is the result of read_clinical() (bulk/Visium) or the cell annotation (scRNA).

Parameters:
Return type:

StateRecovery

ecotyper.recovery_steps.average_fc_marker_genes(data, gene_info, assignment)[source]

averge_FC_marker_genes (lib/ecotyper.R).

Parameters:
Return type:

Series

class ecotyper.recovery_steps.EcotypeRecovery(combined_state_abundances: 'pd.DataFrame', ecotype_abundance: 'pd.DataFrame', assignment_p_vals: 'pd.DataFrame', initial_assignment: 'pd.DataFrame', assignment: 'pd.DataFrame', ecotypes: 'pd.DataFrame')[source]

Bases: object

Parameters:
combined_state_abundances: DataFrame
ecotype_abundance: DataFrame
assignment_p_vals: DataFrame
initial_assignment: DataFrame
assignment: DataFrame
ecotypes: DataFrame
ecotyper.recovery_steps.recover_ecotypes(state_results, model_ecotypes, cell_types, annotation, *, r_exact=True)[source]

ecotypes_recovery.R (bulk). state_results: cell type -> StateRecovery.

Parameters:
Return type:

EcotypeRecovery

ecotyper.recovery_steps.recover_ecotypes_scrna(state_results, model, cell_annotation, top_cols=('Ecotype',))[source]

ecotypes_recovery_scRNA.R: ecotype abundance per sample (column Sample of the cell annotation) from per-cell initial state assignments.

Parameters:
Return type:

EcotypeRecovery

ecotyper.recovery_steps.spatial_states(state_results, cell_types, fractions, coords, models, malignant_cell, sample_name, *, r_exact=True)[source]

spatial_states.R: per-spot state abundances weighted by CSx fractions.

Returns (long, wide) as state_abundances_long.txt / state_abundances.txt.

Parameters:
ecotyper.recovery_steps.spatial_ecotypes(wide, ecotypes, *, r_exact=True)[source]

spatial_ecotypes.R: per-spot ecotype abundances (ecotype_abundances.txt).

Parameters:
Return type:

DataFrame