Skip to content

Commit

Permalink
scripts and typed
Browse files Browse the repository at this point in the history
  • Loading branch information
oolonek committed Apr 12, 2024
1 parent 0181d32 commit 8ff5a73
Show file tree
Hide file tree
Showing 8 changed files with 65,961 additions and 3 deletions.
7 changes: 7 additions & 0 deletions mgf_filterer/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class MGFFileNotFoundError(FileNotFoundError):
"""Exception raised when an MGF file cannot be found."""

def __init__(self, filepath: str) -> None:
message = f"No such file or directory: '{filepath}'"
super().__init__(message)
self.filepath = filepath
33 changes: 33 additions & 0 deletions mgf_filterer/mgf_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from pathlib import Path
from typing import List

from matchms.importing import load_from_mgf

from mgf_filterer.exceptions import MGFFileNotFoundError


def load_mgf_files(filepath: str) -> List:
"""
Load spectra from a .mgf file.
Parameters:
filepath (str): The path to the .mgf file to be loaded.
Returns:
List[Spectrum]: A list of Spectrum objects loaded from the file.
Raises:
MGFFileNotFoundError: If the .mgf file cannot be found.
"""
if not Path(filepath).exists():
raise MGFFileNotFoundError(filepath)

spectra = list(load_from_mgf(filepath))
return spectra


# Example usage:
if __name__ == "__main__":
# Replace 'path_to_your_file.mgf' with the actual file path
spectra = load_mgf_files("path_to_your_file.mgf")
print(f"Loaded {len(spectra)} spectra from the file.")
1,765 changes: 1,763 additions & 2 deletions poetry.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ packages = [
]

[tool.poetry.dependencies]
python = ">=3.8,<4.0"
python = ">=3.8,<3.13"
matchms = "^0.24.3"

[tool.poetry.group.dev.dependencies]
pytest = "^7.2.0"
Expand All @@ -20,6 +21,7 @@ deptry = "^0.12.0"
mypy = "^1.5.1"
pre-commit = "^3.4.0"
tox = "^4.11.1"
ipykernel = "^6.29.4"

[tool.poetry.group.docs.dependencies]
mkdocs = "^1.4.2"
Expand All @@ -40,6 +42,10 @@ warn_return_any = "True"
warn_unused_ignores = "True"
show_error_codes = "True"

[[tool.mypy.overrides]]
module = "matchms.*"
ignore_missing_imports = "True"

[tool.pytest.ini_options]
testpaths = ["tests"]

Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
filterwarnings =
ignore::DeprecationWarning:sparsestack.*
26 changes: 26 additions & 0 deletions tests/test_mgf_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pathlib import Path

import pytest

from mgf_filterer.mgf_processing import load_mgf_files


@pytest.fixture
def mgf_file_path():
# Assuming the test data is in a subdirectory of the tests directory
return Path(__file__).parent / "testdata" / "erythroxylum_coca.mgf"


def test_load_mgf_files_valid(mgf_file_path):
"""Test loading a valid .mgf file."""
spectra = list(load_mgf_files(str(Path(__file__).parent / "testdata" / "erythroxylum_coca.mgf")))
assert len(spectra) == 1098, "No spectra loaded from a valid file."


def test_load_mgf_files_nonexistent():
"""Test loading from a nonexistent file path."""
with pytest.raises(FileNotFoundError):
load_mgf_files("nonexistent_file.mgf")


# More tests can be added for different scenarios
51 changes: 51 additions & 0 deletions tests/testdata/cocaine.mgf
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
BEGIN IONS
PEPMASS=304.151201163587
CHARGE=1
MSLEVEL=2
51.02326202392578 2706680.25
57.05759048461914 3669400.5
58.06536102294922 2941336.25
58.89093017578125 2128817.0
62.50955581665039 2552632.0
65.03856658935547 7410005.5
68.04956817626953 5514949.5
70.06505584716797 8248730.0
74.17985534667969 2559632.25
77.0383071899414 4350874.0
79.0539321899414 6849389.5
81.06940460205078 3283495.25
82.06478118896484 3.54302624E8
83.06818389892578 5227826.5
83.07249450683594 1.8868914E7
91.05368041992188 5.1169252E7
91.764404296875 2450964.75
93.03289031982422 2.487278E7
93.06930541992188 2.4062002E7
94.06431579589844 1.9296378E7
94.44011688232422 2585272.75
95.04850769042969 9126912.0
96.08019256591797 1.6715993E7
97.0640869140625 6438870.0
105.0325698852539 2.17488176E8
105.06919860839844 3719801.0
107.04859161376953 5381365.5
108.07981872558594 8.94284E7
108.087158203125 4666916.5
114.09021759033203 2537875.0
118.04029846191406 1.0775417E7
119.04790496826172 7.8783096E7
122.09513854980469 5.6339332E7
125.05862426757812 8981573.0
132.07960510253906 5560816.0
133.06361389160156 3509932.25
135.06613159179688 3572607.5
150.08990478515625 1.45036352E8
151.0742950439453 1.4068616E7
154.08468627929688 1.9900514E7
182.11578369140625 1.685805568E9
183.11920166015625 3.2352878E7
194.062744140625 2746359.0
272.1258239746094 1.6657989E7
304.1513671875 1.19192128E9
305.1551208496094 4.1532744E7
END IONS
Loading

0 comments on commit 8ff5a73

Please sign in to comment.