Skip to content

Commit

Permalink
Added tests for initialize_peaks, initialize_peaks_xl and annotation …
Browse files Browse the repository at this point in the history
…for xl peptides
  • Loading branch information
ErBarb committed Dec 18, 2023
1 parent fa0a8d7 commit 7b2c126
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 3 deletions.
5 changes: 3 additions & 2 deletions spectrum_fundamentals/annotation/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ def parallel_annotate(
:return: a tuple containing intensity values (np.ndarray), masses (np.ndarray), calculated mass (float),
and any removed peaks (List[str])
"""

mod_seq_column = "MODIFIED_SEQUENCE"
if "MODIFIED_SEQUENCE_MSA" in index_columns:
mod_seq_column = "MODIFIED_SEQUENCE_MSA"
Expand Down Expand Up @@ -389,8 +390,8 @@ def parallel_annotate(
else:
raise ValueError(f"Unsupported crosslinker type provided: {crosslinker_type}")

fragments_meta_data_a, tmt_n_term_a, unmod_sequence_a, calc_mass_a = initialize_peaks_xl(inputs_a)
fragments_meta_data_b, tmt_n_term_b, unmod_sequence_b, calc_mass_b = initialize_peaks_xl(inputs_b)
fragments_meta_data_a, tmt_n_term_a, unmod_sequence_a, calc_mass_a = initialize_peaks_xl(*inputs_a)
fragments_meta_data_b, tmt_n_term_b, unmod_sequence_b, calc_mass_b = initialize_peaks_xl(*inputs_b)

if not unmod_sequence_a:
return None
Expand Down
1 change: 1 addition & 0 deletions spectrum_fundamentals/fragments.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ def initialize_peaks_xl(
mass = compute_peptide_mass(sequence_without_crosslinker)

elif crosslinker_type in ["BS3", "DSS"]: # non-cleavable XL
charge=3 # generate only peaks with charge 1, 2 and 3

sequence_without_crosslinker = sequence.replace("[UNIMOD:1898]", "")
sequence_beta_without_crosslinker = sequence_beta.replace("[UNIMOD:1898]", "")
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/data/annotation_xl_input.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/unit_tests/data/annotation_xl_output.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/unit_tests/data/fragments_meta_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"ion_type": "b", "no": 1, "charge": 3, "mass": 24.686314467000003, "min_mass": 24.685820740710664, "max_mass": 24.68680819328934}, {"ion_type": "y", "no": 1, "charge": 3, "mass": 30.689836023666672, "min_mass": 30.6892222269462, "max_mass": 30.690449820387144}, {"ion_type": "b", "no": 1, "charge": 2, "mass": 36.525833467, "min_mass": 36.525102950330655, "max_mass": 36.52656398366934}, {"ion_type": "y", "no": 1, "charge": 2, "mass": 45.531115802, "min_mass": 45.53020517968396, "max_mass": 45.532026424316044}, {"ion_type": "b", "no": 2, "charge": 3, "mass": 48.365352467, "min_mass": 48.36438515995066, "max_mass": 48.366319774049344}, {"ion_type": "y", "no": 2, "charge": 3, "mass": 54.36887402366667, "min_mass": 54.3677866461862, "max_mass": 54.369961401147144}, {"ion_type": "b", "no": 1, "charge": 1, "mass": 72.044390467, "min_mass": 72.04294957919066, "max_mass": 72.04583135480934}, {"ion_type": "b", "no": 2, "charge": 2, "mass": 72.044390467, "min_mass": 72.04294957919066, "max_mass": 72.04583135480934}, {"ion_type": "b", "no": 3, "charge": 3, "mass": 72.044390467, "min_mass": 72.04294957919066, "max_mass": 72.04583135480934}, {"ion_type": "y", "no": 3, "charge": 3, "mass": 78.04791202366667, "min_mass": 78.0463510654262, "max_mass": 78.04947298190714}, {"ion_type": "y", "no": 2, "charge": 2, "mass": 81.049672802, "min_mass": 81.04805180854396, "max_mass": 81.05129379545605}, {"ion_type": "y", "no": 1, "charge": 1, "mass": 90.05495513700001, "min_mass": 90.05315403789727, "max_mass": 90.05675623610274}, {"ion_type": "b", "no": 4, "charge": 3, "mass": 95.723428467, "min_mass": 95.72151399843067, "max_mass": 95.72534293556934}, {"ion_type": "y", "no": 4, "charge": 3, "mass": 101.72695002366667, "min_mass": 101.7249154846662, "max_mass": 101.72898456266715}, {"ion_type": "b", "no": 3, "charge": 2, "mass": 107.562947467, "min_mass": 107.56079620805066, "max_mass": 107.56509872594934}, {"ion_type": "y", "no": 3, "charge": 2, "mass": 116.568229802, "min_mass": 116.56589843740396, "max_mass": 116.57056116659605}, {"ion_type": "b", "no": 2, "charge": 1, "mass": 143.081504467, "min_mass": 143.07864283691066, "max_mass": 143.08436609708934}, {"ion_type": "b", "no": 4, "charge": 2, "mass": 143.081504467, "min_mass": 143.07864283691066, "max_mass": 143.08436609708934}, {"ion_type": "y", "no": 4, "charge": 2, "mass": 152.086786802, "min_mass": 152.08374506626396, "max_mass": 152.08982853773605}, {"ion_type": "y", "no": 2, "charge": 1, "mass": 161.092069137, "min_mass": 161.08884729561726, "max_mass": 161.09529097838276}, {"ion_type": "b", "no": 3, "charge": 1, "mass": 214.118618467, "min_mass": 214.11433609463066, "max_mass": 214.12290083936935}, {"ion_type": "y", "no": 3, "charge": 1, "mass": 232.129183137, "min_mass": 232.12454055333728, "max_mass": 232.13382572066274}, {"ion_type": "b", "no": 4, "charge": 1, "mass": 285.155732467, "min_mass": 285.15002935235066, "max_mass": 285.16143558164936}, {"ion_type": "y", "no": 4, "charge": 1, "mass": 303.166297137, "min_mass": 303.16023381105725, "max_mass": 303.1723604629428}]
1 change: 1 addition & 0 deletions tests/unit_tests/data/fragments_meta_data_xl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"ion_type": "b", "no": 1, "charge": 3, "mass": 24.686314467000003, "min_mass": 24.685820740710664, "max_mass": 24.68680819328934}, {"ion_type": "b", "no": 1, "charge": 2, "mass": 36.525833467, "min_mass": 36.525102950330655, "max_mass": 36.52656398366934}, {"ion_type": "y", "no": 1, "charge": 3, "mass": 41.34719302366667, "min_mass": 41.346366079806195, "max_mass": 41.348019967527144}, {"ion_type": "y", "no": 1, "charge": 2, "mass": 61.517151302, "min_mass": 61.515920958973965, "max_mass": 61.51838164502604}, {"ion_type": "b", "no": 1, "charge": 1, "mass": 72.044390467, "min_mass": 72.04294957919066, "max_mass": 72.04583135480934}, {"ion_type": "y", "no": 1, "charge": 1, "mass": 122.02702613700001, "min_mass": 122.02458559647727, "max_mass": 122.02946667752275}, {"ion_type": "b-xl", "no": 2, "charge": 3, "mass": 163.44455402366668, "min_mass": 163.44128513258622, "max_mass": 163.44782291474715}, {"ion_type": "y-xl", "no": 2, "charge": 3, "mass": 180.10543258033337, "min_mass": 180.10183047168175, "max_mass": 180.109034688985}, {"ion_type": "b-xl", "no": 3, "charge": 3, "mass": 197.78094902366664, "min_mass": 197.77699340468615, "max_mass": 197.78490464264712}, {"ion_type": "y-xl", "no": 3, "charge": 3, "mass": 203.78447058033336, "min_mass": 203.78039489092177, "max_mass": 203.78854626974496}, {"ion_type": "b-xl", "no": 2, "charge": 2, "mass": 244.66319280200003, "min_mass": 244.65829953814398, "max_mass": 244.66808606585607}, {"ion_type": "y-xl", "no": 2, "charge": 2, "mass": 269.65451063700004, "min_mass": 269.6491175467873, "max_mass": 269.6599037272128}, {"ion_type": "b-xl", "no": 3, "charge": 2, "mass": 296.16778530199997, "min_mass": 296.1618619462939, "max_mass": 296.173708657706}, {"ion_type": "y-xl", "no": 3, "charge": 2, "mass": 305.17306763700003, "min_mass": 305.1669641756473, "max_mass": 305.17917109835275}, {"ion_type": "b-xl", "no": 2, "charge": 1, "mass": 488.31910913700005, "min_mass": 488.3093427548173, "max_mass": 488.3288755191828}, {"ion_type": "y-xl", "no": 2, "charge": 1, "mass": 538.3017448070001, "min_mass": 538.2909787721039, "max_mass": 538.3125108418963}, {"ion_type": "b-xl", "no": 3, "charge": 1, "mass": 591.328294137, "min_mass": 591.3164675711172, "max_mass": 591.3401207028827}, {"ion_type": "y-xl", "no": 3, "charge": 1, "mass": 609.3388588070001, "min_mass": 609.326672029824, "max_mass": 609.3510455841762}]
17 changes: 17 additions & 0 deletions tests/unit_tests/test_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ def test_annotate_spectra(self):
result = annotation.annotate_spectra(spectrum_input)
pd.testing.assert_frame_equal(expected_result, result)

def test_annotate_spectra_noncl_xl(self):
"""Test annotate spectra."""

spectrum_input = pd.read_json(Path(__file__).parent / "data" / "annotation_xl_input.json", orient='records')
expected_result = pd.read_json(Path(__file__).parent / "data" / "annotation_xl_output.json", orient='records')

result = annotation.annotate_spectra(spectrum_input)
pd.testing.assert_frame_equal(expected_result, result)

def test_handle_multiple_matches(self):
"""Test handle_multiple_matches function."""
# Example input data with multiple matches. They don't make biological sense but it tests the mathematical correctness.
Expand Down Expand Up @@ -112,3 +121,11 @@ def test_handle_multiple_matches(self):
matched_peaks,
sort_by="illegal",
)

# Create a test suite containing only the test_sqrt test
suite = unittest.TestSuite()
suite.addTest(TestAnnotationPipeline('test_annotate_spectra_noncl_xl'))

# Create a test runner that will run our suite
runner = unittest.TextTestRunner()
runner.run(suite)
51 changes: 50 additions & 1 deletion tests/unit_tests/test_fragments.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import unittest

from pathlib import Path
import pandas as pd
import json
from numpy.testing import assert_almost_equal

import spectrum_fundamentals.fragments as fragments
Expand Down Expand Up @@ -105,3 +107,50 @@ def test_mass_tol_with_defaults(self):
self.assertEqual(window_ftms, (9.9998, 10.0002))
self.assertEqual(window_tof, (9.9996, 10.0004))
self.assertEqual(window_itms, (9.65, 10.35))


class TestInitializePeaks(unittest.TestCase):
"""Class to test initialize_peaks function."""

def test_initialize_peaks(self):
"""Test initialize_peaks_xl with basic input."""
fragments_input = {"sequence": "AAAA",
"mass_analyzer": "FTMS",
"charge": 3,
"noncl_xl": 0
}

with open(Path(__file__).parent / "data/fragments_meta_data.json", 'r') as file:
expected_list_out = json.load(file)
expected_tmt_nt_term = 1
expected_peptide_sequence = "AAAA"
expected_mass_s = 302.15902

actual_list_out, actual_tmt_n_term, actual_peptide_sequence, actual_calc_mass_s = fragments.initialize_peaks(**fragments_input)

self.assertEqual(actual_list_out, expected_list_out)
self.assertEqual(actual_tmt_n_term, expected_tmt_nt_term)
self.assertEqual(actual_peptide_sequence, expected_peptide_sequence)
assert_almost_equal(actual_calc_mass_s, expected_mass_s, decimal=5)

def test_initialize_peaks_xl(self):
"""Test initialize_peaks_xl with basic input."""

initialize_peaks_xl_input = {"sequence": "AKC",
"mass_analyzer": "FTMS",
"crosslinker_position": 2,
"crosslinker_type": "BS3",
"sequence_beta": "AKA"
}

with open(Path(__file__).parent / "data/fragments_meta_data_xl.json", 'r') as file:
expected_fragments_meta_data = json.load(file)
expected_tmt_nt_term = 1
expected_peptide_sequence = "AKC"
expected_mass = 320.15182

actual_fragments_meta_data, actual_tmt_n_term, actual_peptide_sequence, actual_mass = fragments.initialize_peaks_xl(**initialize_peaks_xl_input)
self.assertEqual(actual_fragments_meta_data, expected_fragments_meta_data)
self.assertEqual(actual_tmt_n_term, expected_tmt_nt_term)
self.assertEqual(actual_peptide_sequence, expected_peptide_sequence)
assert_almost_equal(actual_mass, expected_mass, decimal=5)

0 comments on commit 7b2c126

Please sign in to comment.