Data model and conventions#

Most analysis errors in single-cell notebooks come from using the wrong matrix or experimental unit. Establish these conventions immediately after loading data.

AnnData matrix roles#

Location

Expected content

Typical consumers

adata.layers["counts"]

raw, finite, non-negative integer counts

QC, HVG with seurat_v3, pseudobulk, scVI, scDblFinder

adata.layers["log1p_norm"]

library-size normalized, natural-log transformed expression

visualization, marker scoring, LIANA

adata.layers["scale"]

optional scaled expression

selected visualization/modeling only

adata.X

explicitly chosen working representation

Scanpy functions that do not receive layer=

adata.raw

optional immutable gene-expression snapshot

external tools that explicitly request use_raw=True

Never infer that .X contains counts solely because it is sparse. sctk validates count inputs before count-based analysis and rejects negative or non-integer values.

import scanpy as sc
import sctk

adata = sc.read_h5ad("input.h5ad")

# Do this only when the input specification confirms that X is raw counts.
if "counts" not in adata.layers:
    adata.layers["counts"] = adata.X.copy()

Required observation metadata#

Use one column for each experimental concept. Do not encode several concepts in a single string unless an external file format requires it.

Concept

Recommended key

Example

biological sample

sample

patient/library identifier

experimental condition

condition

control, treated

technical batch

batch

sequencing run

paired subject/block

patient

donor identifier

cell identity

cell_type

curated annotation

cluster

leiden_0.8

unsupervised cluster

Before analysis:

required = ["sample", "condition"]
missing = [key for key in required if key not in adata.obs]
if missing:
    raise KeyError(f"Missing metadata: {missing}")

assert adata.obs_names.is_unique
assert adata.var_names.is_unique

Experimental unit#

Cells from the same biological sample are not independent replicates.

  • Cell-level plots and marker exploration can use individual cells.

  • Differential expression between conditions should normally aggregate to sample-level pseudobulk profiles.

  • Detection-rate stratification should first calculate one fraction per sample.

  • Paired studies should include the subject/block before condition in the statistical design.

Missing values#

Missing grouping or design metadata is not silently imputed. Resolve it before:

  • per-sample QC;

  • sample-level expression splitting;

  • scVI covariate registration;

  • DESeq2 or limma-voom;

  • LIANA cell-identity grouping.

Sparse, backed, and large objects#

Most functions accept dense or SciPy sparse matrices. For large or backed objects:

  • use create_pseudobulk(..., chunk_size=...);

  • avoid copying the full AnnData unless necessary;

  • write intermediate .h5ad checkpoints;

  • keep count validation enabled unless the input has already been verified by a trusted import pipeline.

Suggested checkpoints#

adata.write_h5ad("01_qc_annotated.h5ad")
adata_qc.write_h5ad("02_qc_filtered.h5ad")
adata_qc.write_h5ad("03_preprocessed.h5ad")
pb.write_h5ad("04_pseudobulk.h5ad")

Save the code, environment file, thresholds, and sample metadata together with these objects.