Skip to content

solo: --soloCellFilter OrdMag, CellRanger's cell call (first item of #181, stacked on #180) - #182

Open
BenjaminDEMAILLE wants to merge 11 commits into
scverse:mainfrom
BenjaminDEMAILLE:bd/solo-ordmag-cell-calling
Open

solo: --soloCellFilter OrdMag, CellRanger's cell call (first item of #181, stacked on #180)#182
BenjaminDEMAILLE wants to merge 11 commits into
scverse:mainfrom
BenjaminDEMAILLE:bd/solo-ordmag-cell-calling

Conversation

@BenjaminDEMAILLE

@BenjaminDEMAILLE BenjaminDEMAILLE commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Stacked on #179#178#176#175#174#173. Merge in that order. (Rebased off #180 after it was closed; nothing here depended on it.)

First item out of the GEX coverage map in #181, and the one that was fully specified with no ambiguity.

What CellRanger does, and what we did

STARsolo's CellRanger2.2 takes the 99th percentile of the top 3 000 barcodes by UMI count and calls everything holding at least a tenth of it. The 3 000 is a fixed guess at how many cells the run has.

CellRanger uses the same rule but searches for that number: it minimises (OrdMag(x) − x)² / x over x from 2 to about 45 000, where OrdMag(x) is the number of cells the rule calls when told to expect x of them. The loss is small where the rule predicts itself — feed it the right cell count and it gives that count back.

This adds --soloCellFilter OrdMag maxExpectedCells quantile ratio (default 45000 0.99 10), and makes EmptyDrops_CR use it for the initial cell set it guarantees before the Monte-Carlo rescue, which is the order CellRanger runs the two steps in. CellRanger2.2 is untouched and stays the default, so nothing changes for anyone not asking.

Two things the page does not specify

Both are decisions, so they are in the code and in DIVERGENCE.md §3.6 rather than left implicit:

  • Every integer in range is evaluated, not a geometric grid refined by search. Each evaluation is a binary search over the sorted totals, so an exhaustive sweep of 45 000 candidates costs nothing measurable and finds the true minimum of the stated loss instead of a grid point near it.
  • Ties go to the smaller x, so the result does not depend on iteration order.

What this measures, honestly

On the fixture it changes nothing. All three call 200 cells, and so does CellRanger 10.0.0:

method cells called
CellRanger2.2 (default) 200
OrdMag 200
EmptyDrops_CR 200
CellRanger 10.0.0 200

That is not a null result dressed up as a win: the 20 000-read fixture has a clean 200-cell plateau over a flat ambient tail, and any of these rules gets it right. The fixed guess and the search part company on a graded distribution with no plateau, which is exactly the case real data presents and which the fixture does not have. That case is covered by the unit tests rather than by a measurement I cannot make on this data, and I would rather say so than imply the fixture demonstrates something it does not.

Building a fixture with a graded UMI distribution belongs with the fixture work in #172.

Verification

  • ordmag_finds_the_self_consistent_cell_count — the loss is zero at the true cell count, and the cutoff is a tenth of the plateau
  • ordmag_and_the_fixed_knee_disagree_on_a_graded_distribution — 10 000 barcodes falling geometrically: the search does not reproduce the fixed guess, and the count it calls is self-consistent with what it was solving for
  • ordmag_beats_a_fixed_expected_cell_count_when_the_guess_is_wrong — 20 000 cells against a hardcoded 3 000; on a flat population they agree, which the test asserts rather than hides
  • ordmag_handles_empty_and_tiny_inputs — empty slice, zero expected count, a single barcode, and the cutoff never dropping below 1

Gate: 576 lib + 26 integration tests, cargo clippy --all-targets -- -D warnings, cargo fmt --check, cargo +1.89 check --all-targets, all green.

Not in this PR

Making OrdMag and EmptyDrops_CR the defaults on 10x geometry is a two-line addition to CELLRANGER_DEFAULTS in #176. It is a further change to default output, so it belongs with the sign-off on #176 rather than smuggled in here; say the word and I will add it there.

BenjaminDEMAILLE and others added 11 commits July 31, 2026 10:06
`--soloUMIfiltering MultiGeneUMI_CR` kept every gene tied at the highest
read count. CellRanger's rule is the opposite on exactly that case: the
gene with the *strictly* highest count takes the UMI, and a tie means no
gene counts it.

STAR walks the genes keeping a running maximum and clears its winner
whenever it meets an equal count
(`SoloFeature_collapseUMIall.cpp:212-224`):

    if (ig.second>maxu) { maxu=ig.second; maxg=ig.first; }
    else if (ig.second==maxu) { maxg=-1; };
    ...
    if ( maxg+1==0 ) continue; // not counted for any gene

One read per gene is the ordinary shape of a multi-gene UMI, and it is
always a tie, so the old rule made the flag inert in practice rather
than merely inaccurate. Measured on a 20 000-read 10x fixture (200 cells
from the real v3 whitelist, 400 genes, 720 UMIs deliberately shared
between two genes), against STAR 2.7.11b with the same flags:

                       identical entries    STAR counts   rustar counts
    before             13 749 / 14 806          15 423          16 465
    after              13 902 / 13 967          15 423          15 414

The flag removed nothing at all before; STAR removes 1 030 counts. The
gap goes from +1 042 to -9.

The outcome does not depend on the order the genes are visited — a
strict maximum always ends as the winner, a tie always ends with none —
so iterating a `HashMap` here stays deterministic.

`multi_gene_umi_cr_drops_a_tie_entirely` pins the case the old tests
missed: they only covered 3 reads against 1, where both rules agree.

Not yet implemented, and stated so rather than left to be discovered:
STAR applies a second condition, that the winning gene must also hold
the top count among *uncorrected* UMIs (`umiGeneMapCount0`, same file,
lines 226-232). That needs the pre-correction counts, which this code
does not keep. The 65 entries still differing out of 13 967 are the
place to look for its effect.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…w matrix

STARsolo's raw matrix has a column per whitelist barcode. For 10x v3 that
is 3 686 400 columns and a 62 MB `barcodes.tsv`, nearly all zeros.
CellRanger's `raw_feature_bc_matrix` has a column per *observed* barcode.

The two files therefore share no keys, which is not a rounding difference
in a comparison, it is zero overlap: comparing our raw output against a
real `cellranger count` run gave 0 identical entries out of 27 396 until
the columns were reconciled.

`--soloOutRawBarcodes Observed` narrows the raw matrix to the barcodes
that carry a count. Default `Whitelist` keeps what STARsolo writes, so
nothing changes for anyone not asking.

Measured on the 20 000-read fixture:

    Whitelist  3 686 400 barcodes   barcodes.tsv 62 668 800 bytes
    Observed         200 barcodes   barcodes.tsv      3 400 bytes

with identical counts on both sides: 13 937 entries, 15 414 counts.

`finalize_matrix` already took a column remap for the filtered matrix, so
this reuses it rather than adding a second path. The observed set is read
back from the streamed body, which costs one pass and only when the flag
is on.

This is a **non-STAR flag** and needs sign-off; recorded in
`DIVERGENCE.md` §3.2 rather than presented as parity.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
STAR corrects UMIs within each gene *before* deciding which gene owns a
UMI, and applies two conditions, not one
(`SoloFeature_collapseUMIall.cpp:134-148` and `:203-235`):

1. one gene must hold a strictly higher read count than every other, on
   the **corrected** UMI map — that is scverse#173, already landed;
2. and that winner must not be beaten in the **uncorrected** map at the
   same key.

The second condition exists because correction moves reads between UMIs:
a gene can win only because correction folded a neighbouring UMI onto it,
and STAR rejects that win rather than counting it.

Reproducing it needs the order STAR uses. The generic path here filters
multi-gene UMIs first and corrects afterwards, which cannot express either
condition: by the time correction happens the ownership decision is
already made. `MultiGeneUMI_CR` therefore takes its own path, which is
also what STAR does — the flag is only valid with `--soloUMIdedup 1MM_CR`,
so there is no combination this bypasses.

`cellranger_1mm_map` exposes the correction mapping that
`cellranger_1mm` already computed and threw away.

Measured against **CellRanger 10.0.0** on the 20 000-read fixture from
scverse#172, with scverse#165 and scverse#173 also applied:

                              identical entries   CellRanger   rustar
    scverse#165 + scverse#173               13 651 / 13 709        15 111     15 091
    plus this change          13 676 / 13 709        15 111     15 116

Entries CellRanger has and we do not go from 29 to 7, and the count gap
from -20 to +5, which is 0.03%.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Aligning 10x data and comparing the count matrix against CellRanger gave
a successful run and different numbers, with nothing in the output
pointing at the five flags that explain the difference. Measured against
CellRanger 10.0.0 on a 20 000-read fixture, those flags are the whole
gap: 8.9% away without them, 0.03% with them.

When the geometry is unambiguously 10x — `CB_UMI_Simple`, a whitelist, a
16-base cell barcode, a 10- or 12-base UMI — the five now default to
their CellRanger values:

    --clipAdapterType      CellRanger4
    --outFilterScoreMin    30
    --soloCBmatchWLtype    1MM_multi_Nbase_pseudocounts
    --soloUMIfiltering     MultiGeneUMI_CR
    --soloUMIdedup         1MM_CR

A flag given on the command line always wins, including when the value
asked for is STARsolo's own default: `value_source` distinguishes an
explicit flag from a default, so the divergence is escapable by naming
what you want. Every substitution is logged at INFO with the geometry
that triggered it.

**This changes default output behaviour on 10x runs and diverges from
STARsolo**, which is why it is confined to a geometry nothing else in
common use shares, why it is announced on every run it touches, and why
it is in `DIVERGENCE.md` §1.3 as the largest entry in that file. It needs
sign-off.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Re-derived both sides from one clean state: the five flags move the matrix
from 8.96% above CellRanger to 2.17% above it, not to 0.03%. The earlier
figure compared a rustar run against a CellRanger run built from a different
state of the fixture. STAR 2.7.11b with the same flags is at +0.09%, so the
remaining gap is open rather than closed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The figure I replaced this with was measured without scverse#165, whose cbMinP
posterior threshold is a precondition for it. With scverse#165 the five flags move
the matrix from 8.96% above CellRanger to 0.03% above it, which is what the
original text said.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Writes the same numbers where `cellranger count` writes them:
outs/{raw,filtered}_feature_bc_matrix/, gzipped, a -1 GEM-well suffix on
every barcode, one raw column per observed barcode, and no per-feature
subdirectory when a single feature is requested.

The layout implies --soloOutGzip yes, --soloOutRawBarcodes Observed and an
outs/ output directory; each is still overridable on the command line. On
10x geometry it is the default, alongside the five flags from the previous
commit, so it changes where output files are written on those runs.

Counts are untouched: 13 959 entries and 15 439 counts in both layouts on
the 20 000-read fixture, compared entry by entry. Against a real
cellranger count 10.0.0 run on the same fixture the raw barcode sets match
exactly, 200 of 200.

Recorded in DIVERGENCE.md 3.3.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Writes CellRanger 10.0.0's 20 metrics, in its order and value formats
(thousands separators, quoted when that adds a comma; percentages to one
decimal). STARsolo's Summary.csv is unchanged and still written alongside.

Twelve of the 20 match a real cellranger count 10.0.0 run exactly on the
20 000-read fixture. The other eight rest on an interpretation of a
denominator CellRanger does not document, and each interpretation is stated
in DIVERGENCE.md 3.4 rather than left implicit.

Three metrics needed new counters: Q30Stats tallies Phred >= 30 bases over
the barcode, the UMI and the cDNA read, populated only when the layout asks
for the metrics. The exonic/intronic split needs the gene-body overlap
query, so a Gene-only run under this layout now performs it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
10x define it as 1 - n_deduped_reads / n_reads over unique (barcode, UMI,
gene) combinations among confidently mapped reads. Implementing that
literally, counting distinct triples before UMI correction, gives 0.0% on
the fixture: no two reads there share an exact triple. So the literal
reading is wrong and their numerator is the corrected molecule count, like
ours. What differs is the read denominator, which 3.4 now says instead of
leaving the disagreement unexplained.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The same quantile-over-ratio rule STARsolo calls CellRanger2.2, but with
the expected cell count searched for instead of fixed at 3 000: minimise
(OrdMag(x) - x)^2 / x over x from 2 to maxExpectedCells, where OrdMag(x) is
the number of cells the rule calls when told to expect x of them. The loss
is small where the rule predicts itself.

EmptyDrops_CR now uses it for its initial cell set, which is the order
CellRanger runs the two steps in. CellRanger2.2 is untouched and stays the
default.

Two things 10x's page leaves unspecified are decided here and documented:
every integer in range is evaluated rather than a geometric grid, so the
result is the true minimum of the stated loss and not a nearby grid point;
and ties go to the smaller x, so nothing depends on iteration order.

On the 20 000-read fixture this changes nothing: CellRanger2.2, OrdMag and
EmptyDrops_CR all call 200 cells, as does CellRanger 10.0.0. That fixture
has a clean plateau where any of these rules works. The two part company on
a graded distribution, which is what the unit tests cover.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@BenjaminDEMAILLE

Copy link
Copy Markdown
Contributor Author

Measured on real human data, and it does not support this PR

I ran the comparison 10x's own PBMC 1k v3 dataset (20 M read pairs of the public pbmc_1k_v3, refdata-gex-GRCh38-2024-A on both sides, cellranger count 10.0.0 against this stack plus #165). The yeast fixture could not discriminate between cell-calling rules — every method returned 200 cells, as I said in the description. Human data does discriminate:

cell caller cells called
CellRanger 10.0.0 1 178
rustar EmptyDrops_CR, without this PR (2.2 knee initial set) 1 178
rustar EmptyDrops_CR, with this PR (OrdMag initial set) 1 177
rustar CellRanger2.2 1 145
rustar OrdMag alone 1 133

Without this PR, EmptyDrops_CR reproduces CellRanger exactly. With it, it is one cell off. The change I argued for on the strength of 10x's documentation makes agreement marginally worse on the one real dataset I have.

I am not going to dress that up. Two readings are available and I cannot separate them with one dataset:

  • One cell in 1 178 is noise, and the OrdMag search is still the more faithful implementation of what 10x describes.
  • CellRanger's actual cell caller differs in some detail from the prose on its algorithm page, and the 2.2 knee happens to sit closer to it.

What is not available is the reading I implied when I opened this: that implementing the documented search gets us closer to CellRanger. It does not, here.

The unit tests still stand — the search does what 10x describes, ties are deterministic, and the degenerate cases are handled. CellRanger2.2 remains the default, so nothing changes for anyone not asking. But if you would rather not carry a change whose only demonstrated effect is one cell in the wrong direction, closing this is a reasonable call and I will not argue it.

If you want it kept, the thing that would settle it is a second dataset with a different cell-count regime. I would rather run that than defend this on the documentation alone.

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