Skip to content

map: quantification score with an end fit for reads without a poly(A) tail, and antisense evidence - #39

Merged
rob-p merged 4 commits into
mainfrom
end-fit-qscore
Nov 14, 2026
Merged

rob-p merged 4 commits into
mainfrom
end-fit-qscore

Conversation

@rob-p

@rob-p rob-p commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What

A candidate for 0.3.0. Every hit gets a second score, the quantification score (qscore). It adds an end fit for reads without a poly(A) tail. Mapping output is unchanged; only the weights a quantifier gives a read's hits change.

The poly(A) tail is spearfish's only evidence for:

  • where a read ends: --tail-bonus for the candidate whose annotated 3' end lies where the tail starts;
  • which of two near-tied orientations is right: --orient-tail.

Without a tail (poly(A)-trimmed reads, or reads that lost their tail), two kinds of transcripts tie:

  • isoforms that differ only in their 3' end, e.g. one ending at an internal poly(A) site of a longer sibling;
  • antisense transcripts overlapping on opposite strands.

The EM then tends to give such reads to whichever transcript has more unique reads, and the shorter or antisense transcript can drop to about zero.

Rule. The quantification score is the mapping score plus --end-fit points (default 5) for the candidates whose annotated 3' end the read reaches. It applies only when all of these hold:

  • the read has no tail in either orientation;
  • at least two hits lie within the tie-break margin, in either orientation;
  • the candidates' 3' ends lie farther apart than --end-fit-tol (4 compressed bases), so ends that can't be told apart get no relative adjustment.

Why a separate score. A first version broke ties with this evidence in the mapper. It lowered top-1 accuracy on simulated reads: reads truncated at a sibling's end, and reads whose measured end was shifted by indels, were sent to the wrong isoform. Read by read that is ambiguous; across reads, the quantifier can resolve it. So the mapping score, and with it the ranks, best set, primary hit and orientation, stays exactly as in 0.2.1. The evidence only reaches the quantifier, as a soft preference: about 12 to 1 in oarfish's weights at the default scale.

How the read end is measured

  • Default: from the alignment of the read's right end. This is the transcript position of the last aligned base, not the count of leftover read bases.
  • drna preset: its spans are not extended, so aligning would be done for this alone. It takes each candidate's chain, continued without gaps from its last anchor, instead (--end-fit-chain).
  • --no-end-fit-chain aligns instead, at a higher mapping cost.
  • Span extension (--extend-ends) implies alignment, unless --end-fit-chain is given.

Settings

setting flag default (ont, drna, hifi) -fast presets
end_fit --end-fit SCORE 5 (0: off) 0
end_fit_tol --end-fit-tol BASES 4 unused
end_fit_chain --end-fit-chain / --no-end-fit-chain off, on, off unused

All three are also in the library (MapOptions) and Python (Mapper(..., end_fit=..., end_fit_tol=..., end_fit_chain=...)).

Output

Each addition below appears only when the end fit is on, and is backward compatible:

  • TSV: a 15th column, qscore, named in the header. Columns 1–14 are unchanged.
  • PAF: a tag qs:f. AS:i and sc:f stay the mapping score.
  • RAD: an optional alignment tag qscore (f32), with the file tag quant_scores = 1. Readers that don't know it skip it.
  • Library: Hit::quant_score and Mapper::quant_scores().
  • Python: Hit.quant_score, a quant_score column in hit_columns() / to_pandas(), and Mapper.tsv_header.

With --end-fit 0 or a -fast preset, every output is byte-identical to 0.2.1.

Effect

Measured on internal benchmarks.

Unchanged by construction, and checked:

  • top-1, in-set and best-set output on simulated ONT and HiFi reads: identical to 0.2.1;
  • the -fast presets: byte-identical.

Poly(A)-trimmed real spike-in data: much of the isoform accuracy lost to trimming comes back on two recent ONT PCR cDNA runs. Reads with tails are about the same.

Quantification on simulated reads (full ONT cDNA set, hard sets at several error rates, identity sweep, other simulated ONT and HiFi sets): better, with fewer reads on unexpressed transcripts.

Real data against short-read quantification: union Spearman improves. Expressed-transcript Spearman is neutral at the default score denominator and slightly lower at a wider one.

Spike-ins on older runs: direct RNA improves, PacBio is unchanged, and cDNA is mixed but small.

Instruction counts:

change
ONT cDNA +0.2%
HiFi +0.2%
direct RNA (chain) +0.7%
direct RNA with --no-end-fit-chain +2.5%

A wall-time comparison on a quiet machine is still to be run before this merges.

Implementation notes

  • Core: end_fit and right_reach in crates/map/src/lib.rs. Candidates whose 3' end is out of reach are aligned only when a bound from the band says the result could matter.
  • Plumbing: settings and presets in crates/spearfish/src/settings.rs, CLI flags in crates/spearfish-cli, output in crates/spearfish/src/output.rs, RAD tag in crates/rad (long-read-rad), Python in crates/py-spearfish.
  • Docs: a new page, "Mapping and quantification scores" (website/src/content/docs/concepts/scores.md). The output guide documents the new column and tags.
  • CHANGELOG: an entry under 0.3.0 (unreleased). The version number is open.
  • Tests: new tests for the end fit (both measurement paths), the RAD round trip, the output formats and the Python bindings. The toy fixture's output is unchanged.
  • oarfish: a companion oarfish change reads qscore, in process and from RAD, and exposes the three settings. It will follow once this is released.

Checks

See the PR checks; the same steps were run locally first.

Second change: antisense pairs told apart through the quantification score

Two further commits (--orient-same-exact, then --orient-quant) build on the end fit. In short: when a read's two orientations tie on a short antisense isoform contained in a longer one, the read's own evidence beyond the shared sequence now moves the quantification score, again leaving the mapping output unchanged.

What

When the best hits of a read's two orientations are one sequence on opposite strands (a short antisense isoform contained in a longer one, a pseudogene antisense in a host gene), spearfish reports the two orientations tied, and only the poly(A) tail term can separate them. The read often holds more than the shared sequence:

  • bases one transcript has of its own, such as a short antisense isoform's first bases;
  • a poly(A) tail the detector misses, because the read kept its RT primer and sequencing adapter after the tail: more than the 80 bases any A-run may have after it.

The first commit scores that evidence in the orientation decision (as drafted earlier for 0.2.1):

  1. Mirrored pairs are compared by exact local alignment of the whole read against both hits (--orient-same-exact). The shared sequence scores the same in both, so the difference is exactly what the read holds beyond it. Where the two transcripts are one sequence under the whole read, the alignments are skipped.
  2. Per-side end bonus (--orient-same-end-cut): in those alignments a read end earns the end bonus only where both transcripts reach it, as --end-bonus-cut does within an orientation.
  3. The pair stands at the better chain's score: it is one sequence, so the lower chain is seed luck.
  4. Longer adapters after a tail, at the transcript end only (--orient-tail-after, 120): in every orientation comparison, a poly(A) followed by up to 120 read bases counts where the A-run starts at the hit transcript's 3' end. Elsewhere the 80-base limit stands.

The second commit routes that evidence through the quantification score by default (--orient-quant). The orientation, the reported hits, their ranks and their mapping scores are decided as on end-fit-qscore. Each hit's qscore moves by what the rules above would have moved its mapping score.

Why through the quantification score. Applied to the mapping score, the rules decide a few close calls on simulated reads the wrong way: random bases at a read's end align by chance a point or two into the longer transcript's continuation. That lowers tie-aware top-1 slightly (by 1 and 24 reads on two simulated sets). Weighed across reads by the quantifier, the same evidence keeps its whole effect on quantification. The mapping output, best sets included, stays exactly as with the end fit alone. --no-orient-quant applies the rules to the mapping score instead.

Settings

setting CLI default -fast presets
orient_same_exact --orient-same-exact / --no-orient-same-exact on off
orient_same_end_cut --orient-same-end-cut / --no-orient-same-end-cut on off
orient_tail_after --orient-tail-after=<BASES> 120 80
orient_quant --orient-quant / --no-orient-quant on off
  • All four are in MapSettings (builder, JSON, Python keyword arguments), the CLI, the mapping guide, the scores page and the changelog.
  • Options recorded without them deserialize to the old behaviour.
  • The -fast presets are byte-identical to 0.2.1.
  • Quantification scores are written whenever the end fit or orient_quant has something to change. With --end-fit 0 alone a qscore is still written. --end-fit 0 --no-orient-quant gives the old formats.
  • Turning the change off: --no-orient-same-exact --orient-tail-after=80 maps exactly as end-fit-qscore.
  • The end fit's no-tail check keeps the 80-base limit, so the end fit fires on the same reads as before.

Effect

Measured on internal benchmarks, against end-fit-qscore.

  • Mapping output: identical to end-fit-qscore in columns 1-14 by construction (checked on spike-in reads). Top-1, in-set and best sets on the simulated ONT and HiFi sets are identical.
  • Short antisense spike-in isoforms gain the reads they lost to tied orientations. One spike-in isoform of 229 bases goes from 675 to 1,200 reads on a PCR cDNA run, and from 1 to 89 on a poly(A)-trimmed run, where the end fit alone could not separate it. Spike-in isoform MARD improves (by up to 0.014) or is unchanged on every ONT cDNA run, and is unchanged on direct RNA and HiFi.
  • Read by read, the reads that move carry the evidence they move on:
    • a poly(A) that starts at the short isoform's own 3' end, with 81-120 adapter bases after it (no such A-run lies in the longer transcript there);
    • or bases of the short isoform's own.
      The long isoforms that lose reads were credited with them through the tie.
  • Simulated quantification: expressed Spearman and false-positive mass improve slightly on the full ONT cDNA set. Union Spearman at the default denominator moves by −5e-5. Read by read, that comes from transcripts at zero truth that pick up tiny EM mass (often below 0.01 reads). The one traced case: a read moves to its true orientation, and the EM then shifts mass within a large, otherwise unchanged group of tied isoforms.
  • Real data against short-read quantification: neutral to slightly better.
  • Instruction counts: +0.4% on ONT cDNA mapping over end-fit-qscore; +0.0% direct RNA and +0.1% HiFi.

Tests

  • a_mirrored_pair_is_told_apart_by_what_lies_beyond_the_shared_sequence
  • a_mirrored_pair_earns_the_end_bonus_only_where_both_transcripts_reach_the_read_end
  • a_long_adapter_after_the_tail_counts_only_at_the_transcript_end
  • a_mirrored_pair_stands_at_the_better_chain_score
  • the_quantification_score_takes_the_orientation_scores_the_rules_would_give: ranked_tops agrees with rank_orientations over leads, orders and levels.
  • Property tests of local_band_ends against the naive definition and the full-matrix recurrence.
  • settings_override_and_validate: preset defaults and each setting off on its own. every_setting_has_a_flag covers the new flags.
  • Python: settings and qscore output with the orientation evidence on and off.

cargo fmt --check, clippy --workspace --all-targets -D warnings, cargo test --workspace, the toy fixture, the docs build, the package step, the Python job, the allocator stress and shadow tests and the MSRV check pass locally. The Miri job could not build its sysroot locally (a permission error).

🤖 Generated with Claude Code

https://claude.ai/code/session_013PdRfyfQgt9mi8eYLo2VqT

… tail

Every hit gets a second score, the quantification score (`qscore`): the
mapping score plus `--end-fit` points (5) for the candidates whose
annotated 3' end a read without a poly(A) tail reaches, when at least two
hits lie within the tie-break margin and their 3' ends lie farther apart
than `--end-fit-tol` (4 compressed bases). Without a tail, isoforms that
differ only in their 3' end and antisense transcripts on opposite strands
tie, and a quantifier tends to give their reads to the transcript with
more unique reads; the quantification score tilts them toward the
candidates the read fits.

The mapping score, ranks, best set, primary hit and orientation are
unchanged, so mapping output is the same as in 0.2.1; only the score a
quantifier weighs hits by differs. The read end is measured from the
alignment of the read's right end, or, in the drna preset (spans not
extended), from each candidate's chain (`--end-fit-chain`;
`--no-end-fit-chain` aligns). Off in the -fast presets.

Output: TSV column `qscore`, PAF tag `qs:f`, optional RAD alignment tag
`qscore` (file tag `quant_scores`), `Hit::quant_score`, and in Python
`Hit.quant_score` and `Mapper.tsv_header`; all left out when the end fit
is off. New docs page "Mapping and quantification scores"; CHANGELOG
entry under 0.3.0 (unreleased).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rob-p and others added 2 commits November 14, 2026 09:37
…orient-same-exact)

When the best hits of a read's two orientations are one sequence on
opposite strands under the chains (a short antisense isoform contained
in a longer one, a pseudogene antisense in a host gene), the two
orientations were reported tied, and only the poly(A) tail term could
separate them. The read often holds more than the shared sequence:
bases one transcript has of its own (a short isoform's first bases),
and a tail the detector missed because the read kept its primer and
sequencing adapter after it.

Such a mirrored pair is now compared by exact local alignments of the
whole read against both hits, as orient_exact compares other near-tied
orientations. The shared sequence scores the same in both, whatever the
chains, so the difference is what the read holds beyond it. Three rules
go with it:

- The alignments are skipped where the two transcripts are one
  sequence under the whole read's projection as well: both would score
  the same.
- A read end earns the end bonus in them only where both transcripts,
  projected through their hits, reach it (orient_same_end_cut). A read
  that ends at one transcript's end, with a junk base or two after it,
  reaches its end in the other only through those bases aligned into
  its continuation, which is no evidence for it; end_bonus_cut applies
  the same rule within an orientation.
- The pair stands at the better of the two chain scores, whichever
  orientation the alignments favour: it is one sequence, and the lower
  chain is seed luck.

The orientation comparison's tail term (orient_tail) also accepts a
poly(A) tail followed by more than TAIL_AFTER read bases, up to
orient_tail_after, where the A-run starts at the hit transcript's 3'
end (within the tie-break's end tolerance). cDNA reads that kept their
adapters carry that much after the tail. An A-run that far from the
read's end anywhere else is mostly the transcript's own sequence, so
the old limit stands there; the tie-break's own tail search and
tail-first are unchanged.

`local_band_ends` is `local_band` with a bonus of its own for each read
end; `local_band` is unchanged.

Settings `orient_same_exact` (`--orient-same-exact` /
`--no-orient-same-exact`), `orient_same_end_cut` (`--orient-same-end-cut`
/ `--no-orient-same-end-cut`) and `orient_tail_after`
(`--orient-tail-after`, 120), on in every preset except the three -fast
ones, which keep 0.1.0's hits and scores. Options recorded without them
deserialize to the old behaviour. `--no-orient-same-exact
--orient-tail-after=80` maps as before.

Rebased onto the end fit (the quantification score): the end fit's
check that a read has no tail keeps the TAIL_AFTER limit, so it fires
on the same reads as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013PdRfyfQgt9mi8eYLo2VqT
…e only (--orient-quant)

The two orientation rules of the previous commit, the exact comparison
of mirrored pairs (orient_same_exact) and the longer adapter after a
tail at the transcript end (orient_tail_after), now act through the
quantification score by default. The orientation, the reported hits,
their ranks and their mapping scores are decided as without them, and
each hit's quantification score moves by what the two would have moved
its mapping score.

The evidence they add is sound where it fires: a poly(A) that starts
at the short isoform's 3' end, or bases the short isoform has of its
own. But what it changes is how a read's weight is split between two
transcripts that are one sequence under the read, which is the
quantifier's call. Kept out of the mapping score, it leaves the mapping
output, best sets included, exactly as with the end fit alone, while a
quantifier that reads qscore weighs the read by it as before.

ranked_tops gives the best score rank_orientations leaves each
orientation; the difference between the two leads (with and without the
rules) is added to the end fit's adjustment of each hit. The exact
alignments of orient_exact are computed once for both leads.

Setting orient_quant (--orient-quant / --no-orient-quant), on in every
preset except the -fast ones; recorded options without it deserialize
to off. quant_scores() is also on when it has something to route, so a
qscore is written with --end-fit 0 unless --no-orient-quant is given
too. --no-orient-quant maps exactly as the previous commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013PdRfyfQgt9mi8eYLo2VqT
@rob-p rob-p changed the title map: quantification score with an end fit for reads without a poly(A) tail map: quantification score with an end fit for reads without a poly(A) tail, and antisense evidence Nov 14, 2026
Both changes add entries to the 0.3.0 (unreleased) changelog section; both are kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013PdRfyfQgt9mi8eYLo2VqT
@rob-p
rob-p merged commit e00eb5c into main Nov 14, 2026
13 checks passed
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