-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
65,961 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[pytest] | ||
filterwarnings = | ||
ignore::DeprecationWarning:sparsestack.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.