scdecon.preprocessing¶
preprocessing ¶
Single-cell preprocessing: QC and normalisation of in-memory AnnData objects.
This layer operates purely on AnnData objects already loaded by
:mod:scdecon.io; it performs no file I/O of its own.
PreprocessConfig
dataclass
¶
PreprocessConfig(min_genes: int = 200, min_cells: int = 3, max_pct_mito: float = 20.0, mito_prefix: str = 'MT-', target_sum: float | None = 10000.0, counts_layer: str = 'counts')
Immutable configuration for QC filtering and normalisation.
Attributes:
| Name | Type | Description |
|---|---|---|
min_genes |
int
|
Minimum number of genes a cell must express to be kept
( |
min_cells |
int
|
Minimum number of cells a gene must be detected in to be kept
( |
max_pct_mito |
float
|
Maximum percentage of counts from mitochondrial genes allowed per cell,
in the range |
mito_prefix |
str
|
Gene-name prefix identifying mitochondrial genes (e.g. |
target_sum |
float | None
|
Per-cell total count after library-size normalisation
( |
counts_layer |
str
|
Name of the |
QCSummary
dataclass
¶
QCSummary(n_cells_before: int, n_cells_after: int, n_genes_before: int, n_genes_after: int, n_cells_removed_by_min_genes: int, n_cells_removed_by_max_pct_mito: int, n_genes_removed_by_min_cells: int)
Typed, immutable record of a QC filtering step.
Attributes:
| Name | Type | Description |
|---|---|---|
n_cells_before, n_cells_after |
Cell counts before and after filtering. |
|
n_genes_before, n_genes_after |
Gene counts before and after filtering. |
|
n_cells_removed_by_min_genes |
int
|
Cells dropped for expressing fewer than |
n_cells_removed_by_max_pct_mito |
int
|
Cells dropped for exceeding |
n_genes_removed_by_min_cells |
int
|
Genes dropped for being detected in fewer than |
preprocess ¶
Run the full preprocessing pipeline: QC metrics, filtering, normalisation.
The supplied adata is annotated in place with QC metadata (matching
Scanpy conventions and avoiding an unnecessary copy); callers who need the
original untouched should pass adata.copy(). Filtering then produces a
new object, which is normalised and returned.
QC metrics are always recomputed and overwritten deterministically -- if
compute_qc_metrics has already been run on adata, its columns are
regenerated, not skipped -- so the result never depends on prior state.
The QC summary is stored in serialisable form at
result.uns[QC_SUMMARY_KEY] (a dict of integer counts) so it travels with
the processed object (e.g. through write_h5ad) without changing this
function's return type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
Raw-count single-cell data. Annotated in place with QC metadata; its expression values and dimensions are not modified. |
required |
config
|
PreprocessConfig
|
Thresholds and options for QC filtering and normalisation. |
required |
Returns:
| Type | Description |
|---|---|
AnnData
|
A new filtered and normalised dataset, with |
compute_qc_metrics ¶
Annotate per-cell and per-gene QC metrics in place.
Flags mitochondrial genes by config.mito_prefix and delegates metric
computation to sc.pp.calculate_qc_metrics. No filtering is performed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
The dataset to annotate. Modified in place ( |
required |
config
|
PreprocessConfig
|
Supplies |
required |
Returns:
| Type | Description |
|---|---|
AnnData
|
The same |
filter_cells_and_genes ¶
filter_cells_and_genes(adata: AnnData, config: PreprocessConfig) -> tuple[anndata.AnnData, QCSummary]
Filter low-quality cells and rarely-detected genes into a new AnnData.
Applies, in this order: min_genes per cell, max_pct_mito per cell,
then min_cells per gene (gene support counted after cell filtering).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
A dataset already annotated by :func: |
required |
config
|
PreprocessConfig
|
Supplies |
required |
Returns:
| Type | Description |
|---|---|
tuple[AnnData, QCSummary]
|
The filtered copy and a summary of what was removed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If QC metrics are absent ( |