Skip to content

Add near-mass mutation decoys - #468

Draft
GeorgWa wants to merge 8 commits into
mainfrom
near-mass-decoys
Draft

GeorgWa wants to merge 8 commits into
mainfrom
near-mass-decoys

Conversation

@GeorgWa

@GeorgWa GeorgWa commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Adds NearMassMutationDecoyGenerator ("near_mass_mutation"): each decoy replaces one inner residue of the target by a residue of similar mass.

  • Site: drawn uniformly among the inner residues (not first or last) that have a replacement.
  • Replacement: a residue differing by 0.05–25 Da, drawn in proportion to its inner-residue frequency in the targets (fit).
  • Never replaced or drawn: C, K, R, and P at the second-to-last position. Excluded swaps: I↔L, N↔D, Q↔E.
  • Deterministic: draws come from a hash of seed and sequence, so a sequence gets the same decoy in any batch.
  • Fallback: peptides with no replaceable residue get the DIA-NN decoy (≥4 residues) or are dropped.
  • Decoys equal to a target up to I/L are dropped for this scheme only.

Vectorised in numpy (near_mass_mutation.py), one pass over unique sequences: 1M PSMs decoy in ~1.5 s.

Known gaps: the DIA-NN fallback can still mutate a P at the second-to-last position, and mods are not moved or removed when a modified residue is mutated.

cc @Desperadus

🤖 Generated with Claude Code

GeorgWa and others added 5 commits November 2, 2026 14:17
… of similar mass

DIA-NN decoys send 9 of 20 residues to L at positions 2 and n-1, so their fragments pile up on a
few b2/y2 masses and fragment-sharing features tell them apart from targets. The new
`near_mass_mutation` decoy replaces one random inner residue by a residue within (0.05, 25] Da,
drawn from the targets' inner-residue composition; C and K stay, and I/L, N->D and Q->E are never
drawn. The choice depends only on seed and sequence, so batches and processes agree.

Generators get a `fit` hook, called with the target sequences before decoying. Decoys equal to a
target up to I/L are now dropped for every decoy type, as fragments cannot tell them apart.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Move the mutation into near_mass_mutation.py, decoy each unique sequence
once, compare unique sequences when removing targets, and skip the process
pool for vectorised generators. Also exclude D->N and E->Q.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@GeorgWa
GeorgWa requested a review from Desperadus November 4, 2026 10:23
# Conflicts:
#	alphabase/spectral_library/decoy.py
#	tests/unit/spectral_library/test_decoy.py
Comment thread alphabase/spectral_library/decoy.py Outdated
Comment on lines +320 to +329
"""Drops decoys equal to a target up to I/L, which fragments cannot tell apart, and dropped sequences."""
target_seqs = set(
_equate_isoleucine_leucine(
pd.unique(self.target_lib._precursor_df.sequence.to_numpy())
)
)
decoy_index, decoy_seqs = pd.factorize(self._precursor_df.sequence.to_numpy())
drop_unique = _equate_isoleucine_leucine(decoy_seqs).isin(target_seqs) | (
decoy_seqs == DROPPED_SEQUENCE
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so if i get this right we apply this to all of our decoy schemes which in theory changes old behaviour of diann decoys and other ones - do we want it this way? maybe make it toggleable somehow?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree it makes sense overall to have this ig - just it also changes (??) behaviour of other schemes

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, we maybe want to restrict it to the new scheme

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, it is now restricted to the near-mass scheme via a drops_isoleucine_leucine_equivalents flag on the generator (off by default), so the DIA-NN and pseudo-reverse decoys behave as before.

@Desperadus Desperadus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice - just look at the comment about how this also possibly effects other schemes

Comment thread alphabase/spectral_library/decoy.py Outdated
Comment on lines +320 to +329
"""Drops decoys equal to a target up to I/L, which fragments cannot tell apart, and dropped sequences."""
target_seqs = set(
_equate_isoleucine_leucine(
pd.unique(self.target_lib._precursor_df.sequence.to_numpy())
)
)
decoy_index, decoy_seqs = pd.factorize(self._precursor_df.sequence.to_numpy())
drop_unique = _equate_isoleucine_leucine(decoy_seqs).isin(target_seqs) | (
decoy_seqs == DROPPED_SEQUENCE
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree it makes sense overall to have this ig - just it also changes (??) behaviour of other schemes

Comment on lines +19 to +21
FROZEN_AAS = "CK"
# A proline before the C-terminal residue suppresses cleavage there and shapes the fragment pattern,
# so the second-to-last position neither loses nor gains one

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice that we keep proline :]

@GeorgWa
GeorgWa requested a review from WassimG November 4, 2026 10:48
Older schemes keep their behaviour of dropping only decoys identical to a
target.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
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.

2 participants