Skip to content

Add vars_to_regress and mt_genes; compute percent.mt on the RNA counts in every step - #15

Merged
adamfreedman merged 4 commits into
version_harmonize_devfrom
vars_to_regress_dev
Nov 19, 2026
Merged

adamfreedman merged 4 commits into
version_harmonize_devfrom
vars_to_regress_dev

Conversation

@adamfreedman

Copy link
Copy Markdown
Contributor

Summary

  • vars_to_regress (config, default []): the cell metadata columns every SCTransform call regresses out (Seurat's vars.to.regress), any of percent.mt, nCount_RNA, nFeature_RNA. It is validated with the config and reaches every script that runs SCTransform, each downsampling replicate included.
  • mt_genes (optional sample-sheet column): a file of the sample's mitochondrial gene IDs, matched against column 1 of features.tsv.gz, for annotations whose mitochondrial genes the "(?i)^mt-" pattern cannot find. Blank or absent keeps the pattern. Listed IDs that are not features are reported and skipped; a list naming none of the features, or regressing percent.mt for a sample with no mitochondrial feature, stops the run. max_mtdna stays unsupported; its error now points to vars_to_regress.
  • percent.mt on the RNA counts in every step (1168432): doubletfinder.R and scdblfinder.R computed it on their input's SCT assay, and posthocfilter_threshold.R kept that value. add_percent_mt() is now the only caller of PercentageFeatureSet() and passes assay = "RNA"; the threshold filter recomputes it.

Documented in the README, "Mitochondrial genes and regression".

Default outputs

With the default config, the only change is the percent.mt column of the DoubletFinder, scDblFinder and threshold-filter objects, and only for samples with mitochondrial features. Old (ea053fa) and new scripts, single-threaded in the same job:

  • test data (no mitochondrial genes, so percent.mt is 0 either way): 0 of 46 outputs differ, and tests/reference_outputs (unchanged) still match;
  • a copy of the test inputs with five genes renamed mt-...: 7 of 52 outputs differ, each only in meta.data$percent.mt (old values up to 2.4 points off the RNA-based share); the entry objects, MAD-filter objects, downsampling table and cluster files are identical.

workflow/scripts/regression_utils.R is an input of every R rule, so an existing results directory reruns every step after this change.

Tests

  • Config and sample-sheet validation, and dry runs checking that the options reach every script.
  • tests/test_vars_to_regress.py: tests/check_percent_mt.R recomputes percent.mt independently and checks that a regression leaves every residual uncorrelated with it. scdblfinder.R and doubletfinder.R run from SCT-default inputs, with the pattern and with a list, with and without regression; the threshold filter runs from an input carrying an SCT-based percent.mt; a static guard checks that only add_percent_mt() computes percent.mt. The doubletfinder.R cases need an Rscript with DoubletFinder on PATH and are skipped without one (the R env Snakemake builds lacks it).
  • pytest tests --run-downsample-scaling: 100 passed, 6 skipped (opt-in only), with an Rscript that has DoubletFinder.

Not in this PR

scDblFinder running on the SCT counts (scdblfinder.R) and the MAD filter taking its outliers from the SCT totals (posthocfilter_mad.R) go to a separate PR.

🤖 Generated with Claude Code

adamfreedman and others added 4 commits November 19, 2026 07:05
… lists

Two options, both off by default (with the defaults every script takes its previous code path:
percent.mt from the "(?i)^mt-" pattern, SCTransform with vars.to.regress = NULL, its default):

- vars_to_regress (config, default []): the cell metadata columns every SCTransform call regresses
  out (Seurat's vars.to.regress), any of percent.mt, nCount_RNA, nFeature_RNA. A non-list, another
  value or a duplicate is a config error, in every workflow mode (downsampling runs SCTransform too).
- mt_genes (optional sample-sheet column): a file of the sample's mitochondrial gene IDs, one per
  line (blank lines and # lines ignored), matched against column 1 of its features.tsv; resolved
  like tenx_datadir. Blank or absent keeps the pattern. percent.mt stays PercentageFeatureSet():
  pattern = as before, or features = the listed IDs' row names.

The scripts take --vars-to-regress, --mt-genes and --sample after their positional arguments
(workflow/scripts/regression_utils.R); the rules pass them through params, and the mt_genes file is
a rule input. Each step that builds an object from the sample's features maps the IDs by position
in its own feature source (Read10X's features file, emptyDrops' rowData$ID, CellBender's h5 feature
IDs, the raw features for SoupX and decontX) and takes the row names from the object, so they are
what Seurat named those rows (make.unique, "_" and "|" -> "-"). The object records them in
misc$percent_mt_features, which the doublet, post-hoc filter and downsampling steps read: a saved
object has no feature IDs.

IDs that are not features are reported and skipped; a list naming no feature stops the sample's
job, and so does regressing percent.mt for a sample without a mitochondrial feature (by the pattern
or its list), with a message that names the sample and the mt_genes remedy. Both are also caught
at parse time, from each sample's features file. max_mtdna's error no longer says the pipeline
cannot regress percent.mt. Documented in config.yaml and the README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Config and sample-sheet validation: vars_to_regress types, values and duplicates (also in
  downsample_only mode); the reworded max_mtdna error; mt_genes files that are missing, list no ID
  or name no feature; relative paths; blank cells; an excluded sample without mitochondrial
  features; and, from dry runs, that every script calling SCTransform gets --vars-to-regress
  (empty by default) and every object-building script its --mt-genes.
- tests/test_vars_to_regress.py runs the R scripts on testdata with testdata/mt_genes_test.txt,
  five nuclear-encoded respiratory-chain genes standing in for the mitochondrial genes the test
  data lack (plus two IDs that are not features): the tenx2seuratrds rule with percent.mt
  regressed out, then scdblfinder.R, posthocfilter_threshold.R and downsample_clusters.R from its
  object; the failures without a mitochondrial feature, for a list naming no feature and for an
  unknown variable; and the mapping of duplicated names and names containing "_" or "|".
  tests/check_percent_mt.R recomputes percent.mt from the matrix files and checks that the
  regression leaves every SCTransform residual uncorrelated with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When percent.mt is in vars_to_regress, every script computes the percent.mt it regresses out on
the RNA assay - the counts SCTransform models - from the sample's mapped mt_genes features or the
"(?i)^mt-" pattern, whatever its object's default assay, and checks for a mitochondrial feature
there. Before, doubletfinder.R and scdblfinder.R computed it on their input's default assay, the
SCT assay (SCT-corrected counts over nCount_SCT), and posthocfilter_threshold.R and
downsample_clusters.R regressed the column they carried from their input; those two now recompute
it (refresh_regressed_percent_mt()).

Without regression nothing changes: each script computes percent.mt on its object's default assay
as before (on testdata, the old and new scripts' outputs are identical with the default options).

Tests: scdblfinder.R, from an input whose default assay is SCT, regresses the RNA-based value
(which differs there from the SCT-based one) and fails on RNA when no feature matches the pattern;
posthocfilter_threshold.R and downsampling recompute it on RNA.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
percent.mt is now, in every saved object, the share of each cell's RNA counts (nCount_RNA) in its
mitochondrial features, with or without regression. Without regression, doubletfinder.R and
scdblfinder.R, whose inputs default to their SCT assay, computed it on that assay, and
posthocfilter_threshold.R kept that value from its input.

- add_percent_mt(), the workflow's only caller of PercentageFeatureSet(), passes assay = "RNA" whatever
  the object's default assay (4d1ac92 did so only when percent.mt was regressed out).
- posthocfilter_threshold.R recomputes percent.mt, as posthocfilter_mad.R does, instead of keeping the
  upstream column, so an upstream object made before this change cannot pass an SCT-based value on.
- downsample_clusters.R saves no object and still recomputes percent.mt only when it regresses it out.

Default outputs that change: only the percent.mt column of the doubletfinder, scdblfinder and
posthocfilter_threshold objects, and only for samples with mitochondrial features. The test data have
none (percent.mt is 0 either way), so tests/reference_outputs are unchanged.

Tests: a static check that no script but add_percent_mt() calls PercentageFeatureSet() or stores
percent.mt, and that its calls pass assay = "RNA"; scdblfinder.R and doubletfinder.R from inputs that
default to their SCT assay, with the "(?i)^mt-" pattern (on a copy of the test matrix whose five
stand-in genes are named mt-/MT-) and with the list, with and without regression;
posthocfilter_threshold.R from an upstream object carrying an SCT-based percent.mt. The doubletfinder.R
cases need an Rscript with DoubletFinder on PATH and are skipped without one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adamfreedman
adamfreedman merged commit 662f8f7 into version_harmonize_dev Nov 19, 2026
12 of 13 checks passed
@adamfreedman
adamfreedman deleted the vars_to_regress_dev branch November 20, 2026 03:09
@adamfreedman
adamfreedman restored the vars_to_regress_dev branch November 20, 2026 03:10
@adamfreedman
adamfreedman deleted the vars_to_regress_dev branch November 20, 2026 03:31
adamfreedman added a commit that referenced this pull request Nov 20, 2026
The check failed on PR #15 and PR #16 because GitHub's runner computes
SCTransform slightly differently from the cluster, not because of the
workflow. The failing runs came during GitHub's move of ubuntu-latest to
Ubuntu 26.04 (October 19 to November 19, 2026); the last passing run,
on October 14, still carried the migration notice that 24.04 runs show.

In run 44668646672's artifact, the RNA counts of all 27 objects match
the references exactly. What differs, in every object, is SCTransform's
first-step dispersion estimate for a few nearly Poisson genes: infinite
on one side and finite on the other in the worst cases. In 2 of the 27
objects, that changed 21 rounded SCT counts and moved 2 cells to
another cluster. Rerunning those two steps from GitHub's own inputs on
the cluster (Rocky 8, glibc 2.28) reproduces the references, with every
SCT model value identical; in Ubuntu 24.04 (glibc 2.39) they match to
about 1e-10.

The job now runs in rockylinux:8.10 with LANG=en_US.UTF-8, as on the
cluster. Its CellBender shim path comes from $GITHUB_WORKSPACE, because
${{ github.workspace }} names the runner's directory in a container
job, and its Snakemake environments are cached under their own key,
because Snakemake hashes each environment's path into its directory
name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant