sc-multi-count
Use from a step
from skills._sdk.notebook import load_skill, read_input, write_output
merge = load_skill("sc-multi-count")
samples = [read_input("data/sample_a.h5ad"), read_input("data/sample_b.h5ad")]
adata = merge.merge_samples(samples, sample_ids=["sample_a", "sample_b"])
write_output(adata, "intermediate/merged.h5ad")
write_output(merge.per_sample_summary(adata), "tables/samples.csv")
The API returns objects and does not write files. Existing sample labels are
kept even when sample_ids supplies different barcode prefixes. See
examples/example_step.py for a count-conservation check using two halves
of PBMC3k; those halves are demonstration samples, not biological replicates.
When to use
The user has run sc-count (or another counting backend) on multiple
samples separately and now needs them merged into one AnnData with a
canonical sample-label column for downstream batch-aware analysis.
This only joins matrices; it does not perform Cell Ranger's depth
normalization or recount reads.
Inputs & Outputs
Inputs
- Two or more AnnData objects with raw count-like values in
.X; the CLI
takes repeated --input H5AD paths. Features are aligned by name.
Outputs
tables/barcode_metrics.csvtables/per_sample_summary.csvfigures/barcode_rank.pngfigures/count_complexity_scatter.pngfigures/count_distributions.pngfigures/sample_composition.pngprocessed.h5adstandardized_input.h5adreport.mdresult.jsonfigures/manifest.json,figure_data/manifest.jsonand copies of the two tablesreproducibility/commands.sh,reproducibility/requirements.txt- The returned AnnData has sample labels,
layers["counts"]and a count snapshot in.raw.
Flow
- Collect per-sample AnnData paths from each
--input <path>flag (action="append"); paired--sample-id <id>flags assign sample labels. - Load and tag samples, preserving an existing
obs["sample_id"]. - Join features and cells, zero-fill absent features, then record the count contract.
- Write merged AnnData; emit per-sample / per-barcode summary tables.
- Render barcode-rank + composition figures.
- Emit
report.md+result.json.
Gotchas
--inputisaction="append"— repeat the flag, do not comma-split.sc_multi_count.pydeclares--inputwithaction="append". Pass--input s1.h5ad --input s2.h5ad --input s3.h5ad; a single comma-separated value (--input s1.h5ad,s2.h5ad) is treated as one literal path that does not exist and triggersFileNotFoundError. No directory expansion.- At least two
--inputpaths are required.sc_multi_count.pycallsparser.error("At least two --input paths required when not using --demo.")if you pass zero or one. For a single-sample run you don't need this skill — just use the upstreamsc-countoutput directly. - Missing input file → hard fail.
sc_multi_count.pyraisesFileNotFoundErrorwhen any individual--inputpath does not resolve. In batch pipelines, a single mistyped sample name aborts the whole merge — pre-flight your file list. --r-enhancedis accepted but produces no R plots. This skill emits Python figures only; the flag exists for CLI consistency.- No within-sample re-counting. This is a stitching skill — it stacks already-canonical AnnData objects. If a per-sample input has a non-canonical matrix layout, run
sc-standardize-inputon each before this; otherwise the merged contract may surface incoherent per-cell metrics downstream. _api.py:merge_samplesdoes not validate integer counts. It labels.Xand its copies as raw counts; passing normalized data gives a misleading matrix contract._api.py:merge_sampleskeeps existing sample labels.--sample-idchanges barcode prefixes but does not replace an existingsample_idcolumn; checktables/per_sample_summary.csv.sc_multi_count.py:_demo_adataignores--sample-idin demo mode. Its labels staysample_Aandsample_B.
Key CLI
# Demo (two halves of PBMC3k raw counts)
python skills/singlecell/scrna/sc-multi-count/sc_multi_count.py --demo --output /tmp/sc_multi_demo
# Three samples — repeat --input per file
python skills/singlecell/scrna/sc-multi-count/sc_multi_count.py \
--input s1.h5ad --input s2.h5ad --input s3.h5ad \
--output results/
# With explicit per-sample labels (paired with --input order)
python skills/singlecell/scrna/sc-multi-count/sc_multi_count.py \
--input s1.h5ad --sample-id ctrl_a \
--input s2.h5ad --sample-id ctrl_b \
--input s3.h5ad --sample-id treat_a \
--output results/
API
<!-- api:begin generated from _api.py; regenerate with run.py api <skill dir> --write -->
`merge_samples(adatas, *, sample_ids=None, sample_key='sample_id', join='outer')`
Merge .X counts, align features and return a standardized copy.
Inputs are not modified. Missing features in an outer join become zeros.
Existing sample labels are kept; sample_ids supplies missing labels and,
when given, prefixes each input's barcodes. With no IDs, existing barcodes
are kept and duplicate names get numeric suffixes; missing sample labels
become sample_1, sample_2, etc. Like the CLI, this does not test whether .X
holds integer counts. Standardize external inputs before merging them.
`barcode_metrics(adata, *, sample_key='sample_id')`
Return per-barcode .X counts and detected features, sorted by total counts.
`per_sample_summary(adata, *, sample_key='sample_id')`
Return cell counts, median counts/features and summed UMIs for each sample.
`sample_composition_figure(adata, *, sample_key='sample_id')`
Return a Figure showing each sample's cell count; do not write it.
`run_info(adata, *, keep: bool=True)`
Return merge diagnostics; keep=False removes the run record.
<!-- api:end -->
See also
references/parameters.md— every CLI flag and tuning hintreferences/methodology.md— sample-label derivation, contract harmonisation rulesreferences/output_contract.md— mergedobsschema, table layout- Adjacent skills:
sc-count(upstream — produces single-sample AnnData inputs),sc-standardize-input(per-sample contract canonicaliser, run before this when inputs are external),sc-batch-integration(downstream — corrects batch effects in the merged AnnData)
Dependencies
Python packages this skill's script needs. They are not installed for you — check before a long run.
anndata, matplotlib, numpy, pandas, scanpy, scipy, seaborn