Skip to content

Commit

Permalink
added , standalone_mode=False to click based test to fetch the actual…
Browse files Browse the repository at this point in the history
… values
  • Loading branch information
oolonek committed Apr 12, 2024
1 parent 68bed87 commit 93182af
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
55 changes: 54 additions & 1 deletion mgf_filterer/mgf_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
from typing import List

import click
import matchms.filtering as ms_filters
from matchms import Spectrum
from matchms.importing import load_from_mgf

from mgf_filterer.exceptions import MGFFileNotFoundError


@click.command()
@click.argument("filepath", type=click.Path(exists=True))
def load_mgf_files(filepath: str) -> List:
def load_mgf_files(filepath: str) -> List[Spectrum]:
"""
Load spectra from a .mgf file.
Expand All @@ -25,11 +27,62 @@ def load_mgf_files(filepath: str) -> List:
if not Path(filepath).exists():
raise MGFFileNotFoundError(filepath)

# Debug print
spectra = list(load_from_mgf(filepath))
click.echo(f"Loaded {len(spectra)} spectra from the file.")
return spectra


def metadata_processing(spectrum: Spectrum) -> Spectrum:
"""
Process metadata of a mass spectrometry spectrum by applying a series of filters.
This function applies several filters to the input Spectrum object to harmonize,
repair, and derive chemical identifiers and other metadata. It ensures that the
spectrum's metadata are standardized and enriched for further analysis.
Parameters:
spectrum (Spectrum): The input Spectrum object to be processed.
Returns:
Spectrum: The processed Spectrum object with updated metadata.
Filters applied in order:
- Default filters to clean and standardize the spectrum.
- Repair InChI, InChIKey, and SMILES strings.
- Derive InChI from SMILES and vice versa.
- Derive InChIKey from InChI.
- Harmonize undefined chemical identifiers.
- Add precursor m/z (mass-to-charge ratio) information.
"""
spectrum = ms_filters.default_filters(spectrum)
# More processing steps as previously defined
return spectrum


@click.command()
@click.argument("filepath", type=click.Path(exists=True))
@click.option("--process-metadata", is_flag=True, help="Apply metadata processing to each spectrum.")
def process_mgf_file(filepath: str, process_metadata: bool) -> None:
"""
Load spectra from a .mgf file and optionally process their metadata.
Parameters:
filepath (str): The path to the .mgf file to be loaded.
process_metadata (bool): Flag to determine if metadata processing should be applied.
"""
try:
spectra = load_mgf_files(filepath)
if process_metadata:
spectra = [metadata_processing(spectrum) for spectrum in spectra]
click.echo(
f"Loaded {len(spectra)} spectra from the file, with metadata processing {'enabled' if process_metadata else 'disabled'}."
)
except Exception as e:
click.echo(f"Error processing file: {e}", err=True)


# Example usage:
if __name__ == "__main__":
# process_mgf_file()
load_mgf_files()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ build-backend = "poetry.core.masonry.api"
[tool.mypy]
files = ["mgf_filterer"]
disallow_untyped_defs = "True"
disallow_any_unimported = "True"
disallow_any_unimported = "False"
no_implicit_optional = "True"
check_untyped_defs = "True"
warn_return_any = "True"
Expand Down
16 changes: 14 additions & 2 deletions tests/test_mgf_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@ def mgf_file_path():
return Path(__file__).parent / "testdata" / "erythroxylum_coca.mgf"


# def test_load_mgf_files_classic(mgf_file_path):
# """Test loading a valid .mgf file and check the correct number of spectra."""
# spectra = load_mgf_files("tests/testdata/erythroxylum_coca.mgf")
# expected_number_of_spectra = 1098 # Set this to the number you expect for erythroxylum_coca.mgf
# assert len(spectra) == expected_number_of_spectra


def test_load_mgf_files_valid(runner, mgf_file_path):
"""Test loading a valid .mgf file and check the correct number of spectra."""
result = runner.invoke(load_mgf_files, [str(mgf_file_path)])
# Here we use the standalone_mode=False parameter to get the return value
# See https://stackoverflow.com/a/73511685
result = runner.invoke(load_mgf_files, [str(mgf_file_path)], standalone_mode=False)
print("Output:", result.output)
print("Exception:", result.exception)
assert result.exit_code == 0
expected_number_of_spectra = 1098 # Set this to the number you expect for erythroxylum_coca.mgf
assert f"Loaded {expected_number_of_spectra} spectra from the file" in result.output
assert len(result.return_value) == expected_number_of_spectra
# assert f"Loaded {expected_number_of_spectra} spectra from the file" in result.output


def test_load_mgf_files_nonexistent(runner):
Expand Down

0 comments on commit 93182af

Please sign in to comment.