Repository navigation
Add vars_to_regress and mt_genes; compute percent.mt on the RNA counts in every step - #15
Merged
Merged
Conversation
… 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
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
vars_to_regress(config, default[]): the cell metadata columns everySCTransformcall regresses out (Seurat'svars.to.regress), any ofpercent.mt,nCount_RNA,nFeature_RNA. It is validated with the config and reaches every script that runsSCTransform, each downsampling replicate included.mt_genes(optional sample-sheet column): a file of the sample's mitochondrial gene IDs, matched against column 1 offeatures.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 regressingpercent.mtfor a sample with no mitochondrial feature, stops the run.max_mtdnastays unsupported; its error now points tovars_to_regress.percent.mton the RNA counts in every step (1168432):doubletfinder.Randscdblfinder.Rcomputed it on their input's SCT assay, andposthocfilter_threshold.Rkept that value.add_percent_mt()is now the only caller ofPercentageFeatureSet()and passesassay = "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.mtcolumn 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:percent.mtis 0 either way): 0 of 46 outputs differ, andtests/reference_outputs(unchanged) still match;mt-...: 7 of 52 outputs differ, each only inmeta.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.Ris an input of every R rule, so an existing results directory reruns every step after this change.Tests
tests/test_vars_to_regress.py:tests/check_percent_mt.Rrecomputespercent.mtindependently and checks that a regression leaves every residual uncorrelated with it.scdblfinder.Randdoubletfinder.Rrun 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-basedpercent.mt; a static guard checks that onlyadd_percent_mt()computespercent.mt. Thedoubletfinder.Rcases 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