Skip to content

M15b: SCTransform's vars.to.regress and the workflow's percent.mt - #21

Merged
adamfreedman merged 7 commits into
mainfrom
m15b_dev
Nov 30, 2026
Merged

adamfreedman merged 7 commits into
mainfrom
m15b_dev

Conversation

@adamfreedman

Copy link
Copy Markdown
Contributor

Summary

M15b ports SCTransform's vars.to.regress and Seurat's PercentageFeatureSet() (docs/PLAN.md C3j). scRust can now regress per-cell covariates out of scale.data, as the workflow will be able to again. There is no regression by default: without the new options, every output is M15's, byte for byte.

  • The user's request (2026-11-19): "many researchers might want to regress percent.mt (or even some other features). [...] Can we create an option for regressing on percent.mt, and an option to either use the current regex, or supply a list of mitochondrial gene ids? The behavior of not using vars.to.regress should be the default".
    • The workflow gets the same options, with the same names and rules, on the pipeline's branch vars_to_regress_dev.
  • The options:
    • Where: scrust cluster, scrust sctransform, and the [sctransform] section of scrust doubletfinder's --cluster-params and --recluster-params.
    • --vars-to-regress <name>[,...]: percent.mt, nCount_RNA, nFeature_RNA, or a numeric column of --metadata.
      • Refused: a variable given twice, a name that isn't syntactic in R, a name shared with a feature, and missing or non-finite values.
    • --mt-genes <file>: gene IDs, one per line, matched against column 1 of features.tsv. Blank lines and # lines are skipped.
      • IDs not found are reported and skipped. If none is found, it's an error.
      • Without it, percent.mt uses the workflow's pattern "(?i)^mt-", matched on Seurat's feature names.
    • --percent-mt: writes the column without regressing it.
      • percent.mt is written only when asked for, so default outputs stay M15's.
    • Refusal: regressing percent.mt when no feature matches the pattern is refused, and the message names --mt-genes.
  • The code path (C3j): Seurat 5.5.1's ScaleData → RegressOutMatrix.
    • lm(GENE ~ vars)$qr runs once, then qr.resid() per row, after clipping and before centring.
    • rcompat::linpack ports LINPACK's dqrdc2 (tolerance 1e-7, limited pivoting) and dqrsl statement by statement. They call the same OpenBLAS level-1 routines R calls (dnrm2, dscal, ddot, daxpy).
    • percent.mt = the RNA counts of the mitochondrial features / nCount_RNA × 100.
  • Grading across kernels (decided 2026-11-19):
    • Why a new floor: with the regression, R's own runs differ across OpenBLAS kernels in most of scale.data. Each row's qr.resid sums over all cells, in an order that depends on the kernel. The differences go beyond [clustering.floor.kernel].
    • The new floor: [clustering.floor.kernel_regressed] is R against R across kernels with the regression, measured on all 134 cases. It applies only when both runs record the same non-empty vars.to.regress.
    • Unchanged: [clustering.floor.kernel] and every unregressed grading. Exactness against R's run of the same profile stays the main criterion.
  • percent.mt in the saved DoubletFinder objects: doubletfinder.R and scdblfinder.R computed it on their input's default assay, SCT (SCT corrected counts / nCount_SCT).
    • The user decided (2026-11-19) that the workflow will compute it from the RNA assay everywhere, as scRust does.
    • The saved objects keep the old column, which accounts for the 30 differing objects below.
  • Loaders: both type percent.mt. The Seurat loader adds it with the RNA metadata.
  • Records:
    • Tolerances: K14 in C5, and in validation/tolerances.toml the tables [clustering.K14] and [clustering.floor.kernel_regressed] and three kernel-gating keys. No existing entry changes.
    • Report: the M15b section of docs/validation/clustering.md, and docs/validation/clustering_m15b.tsv.
    • Manifest: M15b's section of validation/manifest.toml.
    • Benchmark: new rows in docs/benchmarks.md.
    • Plan corrections: A3, A5, A5b, C1, C3, C3g, C3h, the new C3j, C5's K14, C6's M15b row, the order (M15b before M14b) and Step 2.
    • Docs: docs/output_manifest.md, the README, NOTICE and the commands' --help.

Results (the session's)

  • Main criterion, scrust against R's run of the same profile:

    • Exact on 134 of 134 cases in production and 134 of 134 in portable: 108 tier T, 20 tier P, and 6 on the zebrafish and the bearded dragon.
    • 889,756,126 values per profile, none differing. scale.data was compared value by value in 130 cases.
    • The 26 portable pairs that ran on different CPU types are exact too.
  • The 31 cases where the pattern finds no mitochondrial gene:

    • R regresses an all-zero percent.mt (a rank-deficient QR). scrust reproduces it through --mt-genes with features that have no counts.
    • scrust refused the pattern alone in all 62 runs, naming --mt-genes.
  • Across kernels: 134 of 134 pass in both directions.

    • 815 checks per direction fall within the regressed floor, none outside.
    • Labels, levels, graphs, percent.mt and the regression's rank and pivot are identical.
  • The two floors (the regressed floor's bounds are R-vs-R maxima over the 134 cases, rounded up):

    array existing floor regressed floor R-vs-R maximum (T · P · other two) case that sets it
    scale.data, max abs 4.3e-14 5.6e-13 9.6e-15 · 5.58e-13 · 4.55e-13 soupx_emptydrops_4500871lane, ids_ncount
    scale.data, share of entries differing 0.0072 0.72 0.7145 · 0.6572 · 0.6953 posthocfilt_threshold_scdblfinder_decontx_raw_tenx_test, ids_ncount
    embeddings 1:30, max abs 1.6e-10 6.2e-10 3.79e-11 · 6.18e-10 · 2.23e-10 soupx_emptydrops_sscovelli, pm
    embeddings 1:30, max scaled 2.4e-12 2.6e-12 2.54e-12 · 2.32e-12 · 8.56e-13 decontx_raw_tenx_test, ids
    embeddings 31:50, max abs 4.3e-6 5.5e-6 5.46e-6 · 5.13e-10 · 1.04e-10 posthocfilt_threshold_scdblfinder_cellbender_fromraw_test, pm
    loadings, max abs / scaled 1.2e-7 / 4.5e-7 1.7e-7 / 6.0e-7 1.67e-7 / 5.95e-7 (T) same
    standard deviations, max abs / scaled 1.9e-13 / 3.6e-14 2.7e-13 / 5.0e-14 2.62e-13 / 4.96e-14 (T) same
    total variance exact 1.2e-13 / 2.2e-16 1.14e-13 (one case) doubletfinder_soupx_tenx_test, meta
    silhouette widths, max abs 3.3e-13 2.0e-12 6.4e-14 · 1.95e-12 · 6.6e-14 soupx_emptydrops_sscovelli, pm
    purities, cells / max abs 19 / 0.019 34 / 0.026 30 · 4 · 0 cells; 0.0258 cells: total over 18 cases; max: doubletfinder_cellbender_fromraw_test, ids
  • percent.mt without the regression, against the saved objects:

    • The 34 clustering objects are identical (277,413 cells).
    • 120 of the 150 DoubletFinder objects are identical.
    • The other 30 (six samples with mitochondrial genes, five inputs each) hold the SCT assay's value. R's recomputation on SCT reproduces all 150 saved columns, and its RNA-based value equals scrust's in all 30 (296,410 cells).
  • Without the options: 120 of 120 of M15's routes are byte-identical, on T and P in both profiles.

  • scrust doubletfinder with the options, on 20 of 20 runs:

    • in process equals the two commands;
    • clustering on the fly equals scrust cluster;
    • DoubletFinder's own files equal M15's.
  • Benchmark (seven P objects, against the workflow's scripts with the regression):

    • scrust uses 2.7–6.7× less CPU time at one thread (2.1–3.7× with --export) and 8.8–44× less memory, and finishes 14–20× sooner at 32 threads.
    • The regression costs scrust nothing measurable (0.98–1.01× the CPU time on the same node). It raises R's peak memory by up to 12%.
  • Tests (the session's, at aed8f74): cargo test --workspace 316 of 316 in 47 suites, debug and release, no warnings. Loaders: R 87 and Python 14, all passing.

Coordinator's verification

From a fresh clone of m15b_dev at 0fc4ffd. The final commit, 99add46, changes only the manifest and two check scripts, so these results stand for it. Records in data/clustering/logs/verify-m15b/.

  • Tests:
    • cargo test --workspace: 316 of 316 in 47 suites, debug and release, none ignored, no warnings (portable kernel).
    • The loaders' tests, with the clone's binary: R 87 of 87, Python 14 of 14.
  • K14, re-run on all 134 cases in both profiles with the clone's binary, through the clone's m15b_cluster.sbatch, against the session's R runs:
    • The session's validator:
      • Same profile: 134 of 134 identical in each.
      • Across kernels: 134 of 134 in both directions, with 1,630 checks within the regressed floor (815 per direction) and none beyond.
      • The 31 refusals in each profile name --mt-genes.
    • An independent checker (indep_m15b.py). It uses none of M15b's comparison code and reads the runs' files itself.
      • Same profile: scrust against R's run, over my runs and the session's. 846,218,469 numeric values per run, none differing: metadata columns, PCA embeddings, loadings, standard deviations, total variance, and scale.data with rows matched by name.
      • Also identical: the levels, the cluster ids, the SNN and NN graphs, and the regression's record (variables, rank, pivot, columns).
      • Across kernels: 815 checks per direction within the floor, none beyond; everything else exact.
      • R against R, recomputed on all 134 cases: every maximum the floor records is reproduced, set by the same case. Each bound is that maximum rounded up to two significant digits.
    • Reproduction: my 268 runs against the session's. Every file is byte-identical except summary.json, which differs only in the command line, the output directory, timings, memory and the node.
  • Without the options: M15's routes re-run with the clone's binary on T and P in both profiles (m15_recluster.sbatch, 20 runs).
    • 120 of 120 routes match M15's final label (3,000 files). Every data file matches byte for byte; summary.json differs only in timings, memory, the node, and the nested summary's digest.
    • m15b_unchanged.sbatch flags these routes only because summary.json records input paths, and my clone's root differs. With the root swapped back, nothing else differs.
  • scrust doubletfinder with the options (m15b_recluster.sbatch, T and P, both profiles): 100 of 100 checks identical.
  • percent.mt, read straight from the saved .rds files (my own R script, not M15b's):
    • Two DoubletFinder objects with mitochondrial genes (mouse, 10,185 cells; zebrafish, 7,164):
      • re-running doubletfinder.R's own call (subset(), then PercentageFeatureSet() with no assay, so SCT) reproduces the saved column in every cell;
      • the RNA-based value equals scrust's in every cell (scrust cluster --percent-mt on the saved singlets).
    • Two clustering objects (11,831 and 26,598 cells): the saved column equals scrust's pattern-based percent.mt in every cell.
  • Against the workflow's own scripts (pipeline branch vars_to_regress_dev at 1168432): tenx2seuratrds.R and doubletfinder.R were run directly, in the production R env, on 10x_mousebrain (11,831 cells), regressing percent.mt and nCount_RNA.
    • The two cases: percent.mt by the pattern, and by a gene-ID list with two IDs that aren't features.
    • scrust with the same options (scrust cluster --counts, then scrust doubletfinder and scrust cluster on its singlets) is identical in every value: 36.3 million per tenx2seuratrds.R run and 31.3 million per doubletfinder.R run, including scale.data. DoubletFinder kept the same cells.
  • Code:
    • The regressed floor applies only when the candidate's recorded vars.to.regress is non-empty and equals the reference's. An array that floor doesn't list is graded exactly.
    • The features check is not applicable only where R used the pattern and scrust a list; the values are still compared.
    • The pattern is matched on Seurat's names (make.unique, _→-); IDs are matched on column 1.
  • tolerances.toml: additions only; no existing entry is removed or changed.
  • Manifest: validation/manifest.toml at 99add46 parses whole with tomllib: 29,339 entries, no duplicate paths, every file present at its size, and all 10,743 M15b entries match their SHA-256.
    • At 0fc4ffd one entry was stale: tests/fixtures/kernels.tsv, which M15b extended.
    • 99add46 updates that entry and adds a whole-manifest check (validation/python/check_manifest.py).
  • Benchmark harness: regenerated from the clone over a copy of data/benchmarks/. All six tables are byte-identical to the shared ones.

Notes

  • The workflow's side: the pipeline's vars_to_regress_dev was merged into version_harmonize_dev as Add vars_to_regress and mt_genes; compute percent.mt on the RNA counts in every step scRNAseq-preprocessing#15. Its scripts agree with scrust, as shown above.
  • A validator limitation, from M11: scrust's feature files name features by features.tsv's column 2, and scrust validate cluster matches R's rows by name. Seurat's row names are unique (make.unique, _→-), so on a raw 10x input with a repeated symbol (10x_mousebrain has two Pakap genes), the loadings-features check and the new scale.data check report false differences. The validation panel's inputs are exports with unique names, so it is unaffected. The cross-check above matched rows by ID. This is a follow-up.
  • User docs: M14b, next, will document M15's and M15b's options.
  • For a future scDblFinder port: the workflow's scdblfinder.R currently runs scDblFinder on SCT's corrected counts, because as.SingleCellExperiment() makes the default assay (SCT) the main experiment. The user decided to fix this on a separate pipeline branch.

🤖 Generated with Claude Code

adamfreedman and others added 7 commits November 19, 2026 07:18
…rted

- rcompat::linpack: R's LINPACK QR as lm() makes it (dqrdc2, tol 1e-7, limited pivoting and
  rank) and qr.resid() applies it (dqrrsd -> dqrsl, job 10), through OpenBLAS's dnrm2, dscal,
  ddot and daxpy (new FFI: daxpy, dscal); bit for bit against R's vectors, rank-deficient
  models included (tests/fixtures/rcompat/linpack_*.tsv.gz, validation/R/regress_vectors.R).
- sctransform::regress: ScaleData's RegressOutMatrix on scale.data's clipped rows, before the
  centring and ScaleData's NA-to-0; SctParams::vars_to_regress; finish_full. Without it the
  code path is unchanged.
- scio::features: Seurat's row names (make.unique, _ and | to -), the workflow's pattern
  "(?i)^mt-" as TRE matches it, id lists, Counts::percent_feature_set (PercentageFeatureSet).
- cluster::covariates: percent.mt's definition and record, the covariates' rules (names,
  values read as R reads them); chain::cluster_counts_with (the covariates in memory);
  chain_params: --vars-to-regress, --mt-genes, --percent-mt and their TOML keys; metadata.tsv
  and cell_metadata.tsv carry percent.mt when asked; validate: percent.mt, the regressed
  column, the regression's rank and pivot, scale.data value by value.
- scrust sctransform, cluster (--counts; the staged starts refuse the options) and
  doubletfinder (through --cluster-params/--recluster-params, --cluster-metadata,
  --recluster-metadata); DoubletFinder's own SCTransform never regresses.
- Loaders: percent.mt typed as a double; their not-rebuilt lists; tests.
- Validation: the R reference runs' options (validation/R/lib/clustering.R cl_covariates,
  reference_clustering.R), the cases and their inputs (validation/python/m15b_cases.py,
  validation/m15b_cases.tsv), the saved objects' percent.mt export
  (validation/R/export_percent_mt.R), sbatch wrappers, cargo tests and fixtures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rder, the checks' scripts

- [clustering.floor.kernel_regressed] (validation/tolerances.toml): R against R across OpenBLAS
  kernels with SCTransform's vars.to.regress, measured on M15b's 134 cases in both directions
  with the grader's statistics (validation/python/m15b_floor.py ports scio::grade::stats), each
  bound the maximum rounded up, as [clustering.floor.kernel] was derived. scrust validate
  cluster grades against it only when both runs record the same non-empty vars.to.regress
  (validate::floor_for); every other comparison, the saved object's included, keeps
  [clustering.floor.kernel]. Reports name the floor. Tests: crates/cluster/tests/m15b_floor.rs.
- The validator: percent.mt's features are compared only when both runs define them the same
  way (R's pattern against scrust's list of features without counts is n/a; the column is
  compared).
- The Seurat loader adds percent.mt with the RNA assay, before the SCT assay's columns, where
  the workflow's objects hold it.
- compare_runs.py --nested-summaries: a nested summary.json's size and digest in its parent's
  manifest set aside (the file holds timings; it is compared as JSON itself).
- The checks' scripts: validation runs, re-validation, percent.mt against the saved objects and
  how the DoubletFinder objects made theirs (the SCT assay), the defaults against M15's runs,
  scrust doubletfinder with the options, the loaders' tests, the manifest's section.
- The benchmark's tool cluster_regress (the workflow's scripts at 8d34de1^, with
  vars.to.regress = "percent.mt"; variants n, x and b, the same binary without the regression).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- [clustering.K14] (validation/tolerances.toml): exact against R's run of the same profile on
  all 134 cases in both profiles (889,756,126 values each, none differing); across kernels
  134 of 134 in both directions within [clustering.floor.kernel_regressed]; percent.mt against
  the saved objects (the 34 clustering objects identical; the DoubletFinder objects' 30
  differences are the SCT assay's); the defaults (120 of 120 routes) and scrust doubletfinder
  with the options (20 of 20).
- docs/validation/clustering_m15b.tsv (validation/python/report_m15b.py): every case's four
  comparisons, scrust's runs (label m15b-25fe846) validated by 390342d's binary.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- docs/validation/clustering.md: M15b's outcome and its section: what was built, the regression,
  the options, how the checks work; the results (exact against R's run of the same profile on all
  134 cases in both profiles; across kernels within [clustering.floor.kernel_regressed]; how the
  floor was measured and the two floors side by side; percent.mt against the saved objects, the
  30 DoubletFinder objects' SCT-based column listed with R's checks; the defaults; scrust
  doubletfinder with the options; the loaders); the tests, findings, corrections, deviations and
  how to reproduce it. The benchmark's rows follow.
- docs/PLAN.md: A3, A5, A5b, C1, C3 (the refused list, the commands, the outputs, C3g, C3h, C3j),
  C5's K14, C6's M15b row, the order and Step 2.
- validation/manifest.toml: M15b's section (validation/python/m15b_manifest.py): R's 268
  reference runs and their files, the inputs, the fixtures, the saved objects' percent.mt, the
  checks, the pinned binaries, the tests.
- README.md, docs/output_manifest.md: percent.mt and the options.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- docs/benchmarks.md: the chain with SCTransform's vars.to.regress on the seven tier-P
  clustering objects, against R's scripts as they ran with the regression (the pipeline before
  8d34de1): 2.7-6.7x less CPU time at one thread (2.1-3.7x with --export), 8.8-44x less memory,
  14-20x sooner at 32 threads. The regression costs Rust nothing measurable (0.98-1.01x on the
  same node, against the same binary without it); it raises R's peak by up to 12%. Findings and
  date updated.
- docs/validation/clustering.md: M15b's benchmark section and outcome row.
- validation/benchmark/: the findings; report.py gives the regression's cost to three decimals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The user decided (2026-11-19) that the workflow computes percent.mt on the
RNA assay everywhere, with or without the regression, as scrust does; the
saved objects keep the SCT assay's column. The report and the plan (C1,
C3j) say so, and say why doubletfinder.R's and scdblfinder.R's call, though
it comes before their own SCTransform, reads the SCT assay: their input was
transformed by the scripts that built it, which left SCT its default assay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ifest check

25fe846 appended the two linpack fixtures' rows to tests/fixtures/kernels.tsv
(now 3,585 bytes, sha256 e509427c...), but its one [[file]] entry, M11's,
still recorded 3,294 bytes and sha256 9025993d.... The entry now records the
file as it is, keeps milestone M11 and says M15b extended it; the M11 and
M15b section headers say so too.

validation/python/check_manifest.py checks the whole manifest, not only one
milestone's section: it parses, no path twice, every entry complete and its
file present, every entry's size, the SHA-256 of every committed file it
lists, and with --hash <milestone> (or all) that milestone's entries' too.
Before the fix it failed on exactly this entry (size and SHA-256) of 29,339
entries and 551 committed files; after it, none fails (with --hash M11).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adamfreedman
adamfreedman merged commit 3faf74c into main Nov 30, 2026
@adamfreedman
adamfreedman deleted the m15b_dev branch November 30, 2026 16:31
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