Skip to content

Commit

Permalink
removed def in
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafakalhor committed Apr 11, 2024
1 parent 4147606 commit 698ceaa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 52 deletions.
69 changes: 18 additions & 51 deletions spectrum_io/search_result/xisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np
import pandas as pd
import spectrum_fundamentals.constants as c
from spectrum_fundamentals.mod_string import xisearch_to_internal

from .search_results import SearchResults

Expand All @@ -16,23 +17,6 @@

class Xisearch(SearchResults):
"""Handle search results from xisearch."""

def __init__(self, xl: str, *args, **kwargs):
"""
Init XISearchResults object.
:param xl: type of crosslinker used. Can be 'DSSO' or 'DSVO'.
:param args: additional positional arguments forwarded to SearchResults constructor.
:param kwargs: additional keyword arguments forwarded to SearchResults constructor.
:raises ValueError: if the type of crosslinker is unknown.
"""
if xl.lower() in ["dsso", "dsvo"]:
self.xl = xl.lower()
else:
raise ValueError(f"Unknown crosslinker type provided: {xl}. Only 'DSSO' and 'DSVO' are supported.")

super().__init__(*args, **kwargs)

def read_result(self, tmt_labeled: str = "") -> pd.DataFrame:
"""
Function to read a csv of CSMs and perform some basic formatting.
Expand Down Expand Up @@ -108,36 +92,9 @@ def filter_xisearch_result(df: pd.DataFrame) -> pd.DataFrame:

return df

@staticmethod
def apply_modifications(split_seq: list, mods: str, mod_positions: str):
"""
Apply modifications to the peptide sequence.
:param split_seq: List containing the sequence characters
:param mods: String containing modifications
:param mod_positions: String containing positions of modifications
"""
mod_positions = str(mod_positions)

if mod_positions in ["nan", "null"]:
return

split_mod = mods.split(";")
for idx, mod in enumerate(split_mod):
modification = ""
if mod == "ox":
modification = "M[UNIMOD:35]"
elif mod == "cm":
modification = "C[UNIMOD:4]"
if modification:
try:
pos_mod = int(mod_positions.split(";")[idx])
split_seq[pos_mod - 1] = modification
except (IndexError, ValueError):
print(f"Error occurred with mod_positions value: {mod_positions}")

@staticmethod
def add_mod_sequence(
xl: str,
seq_a: str,
seq_b: str,
mod_a: str,
Expand All @@ -149,7 +106,8 @@ def add_mod_sequence(
):
"""
Function adds modification in peptide sequence for xl-prosit.
:param xl: type of crosslinker used. Can be 'DSSO' or 'DSBU'.
:param seq_a: unmodified peptide a
:param seq_b: unmodified peptide b
:param mod_a: all modifications of pep a
Expand All @@ -160,18 +118,26 @@ def add_mod_sequence(
:param mod_b_positions: position of all modifications of peptide b
:return: modified sequence a and b
"""
# Check the crosslinker type and apply modification accordingly
if xl.lower() == "dsso":
modification = "K[UNIMOD:1896]"
elif xl.lower() == "dsbu":
modification = "K[UNIMOD:1884]"
else:
raise ValueError(f"Unknown crosslinker type provided: {xl}. Only 'DSSO' and 'DSBU' are supported.")

mod_a_positions = str(mod_a_positions) # Ensure it's a string
mod_b_positions = str(mod_b_positions) # Ensure it's a string

split_seq_a = [x for x in seq_a]
split_seq_b = [x for x in seq_b]
xisearch_to_internal(split_seq_a, mod_a, mod_a_positions)
xisearch_to_internal(split_seq_b, mod_b, mod_b_positions)

Xisearch.apply_modifications(split_seq_a, mod_a, mod_a_positions)
Xisearch.apply_modifications(split_seq_b, mod_b, mod_b_positions)

split_seq_a[int(crosslinker_position_a) - 1] = "K[UNIMOD:1896]"
split_seq_b[int(crosslinker_position_b) - 1] = "K[UNIMOD:1896]"


split_seq_a[int(crosslinker_position_a) - 1] = modification
split_seq_b[int(crosslinker_position_b) - 1] = modification
seq_mod_a = "".join(split_seq_a)
seq_mod_b = "".join(split_seq_b)

Expand Down Expand Up @@ -214,6 +180,7 @@ def update_columns_for_prosit(df: pd.DataFrame) -> pd.DataFrame:

df[["MODIFIED_SEQUENCE_A", "MODIFIED_SEQUENCE_B"]] = df.apply(
lambda row: Xisearch.add_mod_sequence(
row["CROSSLINKER_TYPE"],
row["SEQUENCE_A"],
row["SEQUENCE_B"],
row["Modifications_A"],
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_xisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_read_xisearch(self):
expected_xisearch_internal_path = Path(__file__).parent / "data" / "xisearch_output_internal.tsv"

internal_search_results_df = Xisearch(
xl="DSSO", path=Path(__file__).parent / "data" / "xisearch_output.tsv"
path=Path(__file__).parent / "data" / "xisearch_output.tsv"
).read_result()
internal_search_results_df.reset_index(drop=True, inplace=True)
expected_df = pd.read_csv(expected_xisearch_internal_path)
Expand Down

0 comments on commit 698ceaa

Please sign in to comment.