Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove charged frag types filtering #266

Open
wants to merge 3 commits into
base: tobase-oop
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions alphabase/peptide/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,51 @@ def get_charged_frag_types(
return sort_charged_frag_types(charged_frag_types)


def validate_charged_frag_types(
charged_frag_types: List[str],
) -> Tuple[List[str], List[bool]]:
"""
Validates a list of charged fragment types and returns the valid fragment types.
A valid charged fragment type must:
1. Follow the format: {fragment_type}_z{charge} (e.g. 'b_z1', 'y_modloss_z2')
2. Use a fragment type that exists in FRAGMENT_TYPES
3. Have a positive integer charge

Parameters
----------
charged_frag_types : List[str]
List of charged fragment types to validate (e.g. ['b_z1', 'y_z2', 'invalid_z1', 'b_modloss_z2'])

Returns
-------
List[str]
List of valid charged fragment types

Examples
--------
>>> valid_types = validate_charged_frag_types(['b_z1', 'y_z2', 'invalid_z1', 'b_modloss_z2'])
>>> valid_types
['b_z1', 'y_z2', 'b_modloss_z2']
"""
import warnings

valid_types = []

for frag_type in charged_frag_types:
base_type, charge = parse_charged_frag_type(frag_type)
if base_type not in FRAGMENT_TYPES:
warnings.warn(
f"Fragment type {frag_type} is not supported and will be ignored"
)
continue
elif charge <= 0:
warnings.warn(f"Charge state of fragment type {frag_type} is not positive")
continue
valid_types.append(frag_type)

return valid_types


def parse_charged_frag_type(charged_frag_type: str) -> Tuple[str, int]:
"""
Oppsite to `get_charged_frag_types`.
Expand Down
22 changes: 10 additions & 12 deletions alphabase/spectral_library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
filter_fragment_number,
join_left,
remove_unused_fragments,
sort_charged_frag_types,
validate_charged_frag_types,
)
from alphabase.peptide.precursor import (
calc_precursor_isotope_info,
Expand Down Expand Up @@ -717,21 +719,17 @@ def load_hdf(
self._precursor_df[cols] = mod_seq_df[cols]

self._fragment_mz_df = _hdf.library.fragment_mz_df.values
self._fragment_intensity_df = _hdf.library.fragment_intensity_df.values

self._fragment_mz_df = self._fragment_mz_df[
[
frag
for frag in self.charged_frag_types
if frag in self._fragment_mz_df.columns
]
sort_charged_frag_types(
validate_charged_frag_types(self._fragment_mz_df.columns.values)
)
]

self._fragment_intensity_df = _hdf.library.fragment_intensity_df.values
self._fragment_intensity_df = self._fragment_intensity_df[
[
frag
for frag in self.charged_frag_types
if frag in self._fragment_intensity_df.columns
]
sort_charged_frag_types(
validate_charged_frag_types(self._fragment_intensity_df.columns.values)
)
]

@staticmethod
Expand Down
14 changes: 14 additions & 0 deletions alphabase/spectral_library/flat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
_create_dense_matrices,
flatten_fragments,
remove_unused_fragments,
sort_charged_frag_types,
validate_charged_frag_types,
)
from alphabase.spectral_library.base import SpecLibBase

Expand Down Expand Up @@ -198,8 +200,20 @@ def load_hdf(self, hdf_file: str, load_mod_seq: bool = False):
)
self._fragment_df = _hdf.library.fragment_df.values
self._protein_df = _hdf.library.protein_df.values

self._fragment_mz_df = _hdf.library.fragment_mz_df.values
self._fragment_mz_df = self._fragment_mz_df[
sort_charged_frag_types(
validate_charged_frag_types(self._fragment_mz_df.columns.values)
)
]

self._fragment_intensity_df = _hdf.library.fragment_intensity_df.values
self._fragment_intensity_df = self._fragment_intensity_df[
sort_charged_frag_types(
validate_charged_frag_types(self._fragment_intensity_df.columns.values)
)
]

def get_full_charged_types(self, frag_df: pd.DataFrame) -> list:
"""
Expand Down
36 changes: 36 additions & 0 deletions tests/test_fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
_create_dense_matrices,
_start_stop_to_idx,
get_charged_frag_types,
validate_charged_frag_types,
)
from alphabase.spectral_library.base import SpecLibBase
from alphabase.spectral_library.flat import SpecLibFlat
Expand Down Expand Up @@ -725,3 +726,38 @@ def test_calc_dense_fragments():
np.testing.assert_array_equal(
speclib_flat._fragment_correlation_df.values, expected_correlation
)


def test_validate_charged_frag_types():
# Test valid fragment types
valid_types = ["b_z1", "y_z2", "b_modloss_z2", "y_H2O_z1"]
result = validate_charged_frag_types(valid_types)
assert result == valid_types

# Test invalid fragment base types
with pytest.warns(UserWarning, match="Fragment type invalid_z1 is not supported"):
result = validate_charged_frag_types(["invalid_z1"])
assert result == []

# Test invalid charge states
with pytest.warns(
UserWarning, match="Charge state of fragment type b_z0 is not positive"
):
result = validate_charged_frag_types(["b_z0"])
assert result == []

with pytest.warns(
UserWarning, match="Charge state of fragment type y_z-1 is not positive"
):
result = validate_charged_frag_types(["y_z-1"])
assert result == []

# Test mixed valid and invalid types
with pytest.warns(UserWarning) as record:
result = validate_charged_frag_types(["b_z1", "invalid_z1", "y_z2", "x_z0"])
assert result == ["b_z1", "y_z2"]
assert len(record) == 2 # Should have 2 warning messages

# Test empty list
result = validate_charged_frag_types([])
assert result == []
Loading