Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions deeplc/_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,24 +373,25 @@ def _apply_modifications(
for i, token in enumerate(parsed_seq):
if token[1] is None:
continue
try:
mod_comp = token[1][0].composition
except Exception:
warnings.warn(
f"Skipping modification without known composition: {token[1]}", stacklevel=2
for tag in token[1]:
try:
mod_comp = tag.composition
except Exception:
warnings.warn(
f"Skipping modification without known composition: {tag}", stacklevel=2
)
continue
_apply_composition_to_matrices(
mat,
pos_mat,
mod_comp,
i,
seq_len,
dict_index,
dict_index_pos,
positions,
legacy_positional_deltas,
)
continue
_apply_composition_to_matrices(
mat,
pos_mat,
mod_comp,
i,
seq_len,
dict_index,
dict_index_pos,
positions,
legacy_positional_deltas,
)


def _apply_terminal_modifications(
Expand Down
73 changes: 73 additions & 0 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,3 +673,76 @@ def test_bundled_multitask_model_declares_its_encoding():
spec = blob["feature_spec"]
assert spec["legacy_positional_deltas"] is False
assert _feature_kwargs_from_spec(spec)["legacy_positional_deltas"] is False


class TestMultipleModificationsPerResidue:
"""Tests encoding when a single residue carries multiple ProForma modifications."""

def test_all_modifications_on_a_residue_are_applied_order_independently(self):
unmod = encode_peptidoform("PEPTIDK")
acetyl = encode_peptidoform("PEPTIDK[Acetyl]")
methyl = encode_peptidoform("PEPTIDK[Methyl]")
both_am = encode_peptidoform("PEPTIDK[Acetyl][Methyl]")
both_ma = encode_peptidoform("PEPTIDK[Methyl][Acetyl]")

for key in ("matrix", "matrix_sum", "matrix_global", "matrix_hc"):
np.testing.assert_array_equal(both_am[key], both_ma[key])

expected_k = (
unmod["matrix"][6]
+ (acetyl["matrix"][6] - unmod["matrix"][6])
+ (methyl["matrix"][6] - unmod["matrix"][6])
)
np.testing.assert_array_equal(both_am["matrix"][6], expected_k)

base = len(DEFAULT_DICT_INDEX) + 1
pos_unmod = unmod["matrix_global"][base:].reshape(_POS_ROWS, len(DEFAULT_DICT_INDEX_POS))
pos_acetyl = acetyl["matrix_global"][base:].reshape(_POS_ROWS, len(DEFAULT_DICT_INDEX_POS))
pos_methyl = methyl["matrix_global"][base:].reshape(_POS_ROWS, len(DEFAULT_DICT_INDEX_POS))
pos_both = both_am["matrix_global"][base:].reshape(_POS_ROWS, len(DEFAULT_DICT_INDEX_POS))
expected_pos = pos_unmod + (pos_acetyl - pos_unmod) + (pos_methyl - pos_unmod)
np.testing.assert_array_equal(pos_both, expected_pos)

def test_isotope_label_followed_by_ptm_matches_ptm_alone(self):
acetyl = encode_peptidoform("PEPTIDK[Acetyl]")
with warnings.catch_warnings():
warnings.simplefilter("ignore")
label_first = encode_peptidoform("PEPTIDK[Label:13C(6)15N(2)][Acetyl]")
ptm_first = encode_peptidoform("PEPTIDK[Acetyl][Label:13C(6)15N(2)]")

for key in ("matrix", "matrix_sum", "matrix_global", "matrix_hc"):
np.testing.assert_array_equal(label_first[key], acetyl[key])
np.testing.assert_array_equal(ptm_first[key], acetyl[key])

def test_unknown_modification_does_not_skip_subsequent_known_modification(self):
acetyl = encode_peptidoform("PEPTIDK[Acetyl]")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
mixed = encode_peptidoform("PEPTIDK[+100.0][Acetyl]")

assert any(
"Skipping modification without known composition" in str(w.message) for w in caught
)
for key in ("matrix", "matrix_sum", "matrix_global", "matrix_hc"):
np.testing.assert_array_equal(mixed[key], acetyl[key])

def test_batch_encoding_applies_all_modifications_per_residue(self):
from deeplc._batch_features import encode_batch_features

peptidoforms = ["PEPTIDK[Acetyl][Methyl]", "PEPTIDK[Methyl][Acetyl]"]
batched = encode_batch_features(
peptidoforms,
add_terminal_composition=False,
legacy_positional_deltas=False,
)
for row, proforma in enumerate(peptidoforms):
single = encode_peptidoform(
proforma,
add_terminal_composition=False,
legacy_positional_deltas=False,
include_rolling_sum=False,
)
for key in ("matrix", "matrix_global", "matrix_hc"):
np.testing.assert_array_equal(batched[key][row], single[key])
np.testing.assert_array_equal(batched["matrix"][0], batched["matrix"][1])
np.testing.assert_array_equal(batched["matrix_global"][0], batched["matrix_global"][1])
Loading