Skip to content

Commit

Permalink
Merge pull request #26 from mzmine/library-handler
Browse files Browse the repository at this point in the history
Implemented return metadata in return user validation info
  • Loading branch information
niekdejonge authored Nov 2, 2023
2 parents 92ae6ee + 8b1b4d8 commit 1079e3b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
11 changes: 6 additions & 5 deletions library_spectra_validation/library_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from matchms.importing import load_spectra
from matchms.filtering.SpectrumProcessor import SpectrumProcessor
from filters import PRIMARY_FILTERS
from validation_pipeline import Modification, SpectrumRepairer, SpectrumValidator
from validation_pipeline import METADATA_FIELDS_OF_INTEREST, Modification, SpectrumRepairer, SpectrumValidator
import pandas as pd

class LibraryHandler:
"""Stores the 3 different types of spectra. Correct, repaired, wrong.
Expand Down Expand Up @@ -69,8 +70,10 @@ def return_user_validation_info(self, spectrum_id):

modifications = self.modifications[spectrum_id]
failed_requirements = self.failed_requirements[spectrum_id]

return modifications, failed_requirements, self.spectra[spectrum_id]
metadata = pd.DataFrame(self.spectra[spectrum_id].metadata, index=["Value"])
metadata = metadata.T
metadata = metadata.loc[metadata.index.isin(METADATA_FIELDS_OF_INTEREST)]
return modifications, failed_requirements, metadata

def approve_repair(self, spectrum_id, field_name):
"""Accepts every modification done to a field_name"""
Expand Down Expand Up @@ -157,7 +160,5 @@ def user_rerun_repair(self, spectrum_id, rerun: bool):
if rerun: #todo do we even need it??
self.modifications[spectrum_id] = self.spectrum_repairer.process_spectrum_store_modifications(self.spectra[spectrum_id])
self.failed_requirements[spectrum_id] = self.spectrum_validator.process_spectrum_store_failed_filters(self.spectra[spectrum_id])




7 changes: 7 additions & 0 deletions library_spectra_validation/tests/test_library_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ def test_user_metadata_change():
spectrum_id = 0
library_handler.user_metadata_change(spectrum_id=spectrum_id, field_name="smiles", user_input="CCC")
assert library_handler.spectra[spectrum_id].get("smiles") == "CCC"

def test_return_():
library_handler = LibraryHandler("./examples/test_case_correct.mgf")
spectrum_id = 0
modifications, failed_requirements, metadata = library_handler.return_user_validation_info(spectrum_id=spectrum_id)
# todo add checks that tests that the expected output is given.
print(metadata)
7 changes: 4 additions & 3 deletions library_spectra_validation/validation_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

logger = logging.getLogger("matchms")

METADATA_FIELDS_OF_INTEREST = ["parent_mass", "precursor_mz", "adduct", "smiles",
"compound_name", "inchi", "inchikey", "charge", "ionmode"]


class Modification:
def __init__(self, metadata_field, before, after, logging_message, validated_by_user):
Expand All @@ -34,9 +37,7 @@ def __init__(self, metadata_field, logging_message):
def find_modifications(spectrum_old, spectrum_new, logging_message: str):
"""Checks which modifications have been made in a filter step"""
modifications = []
metadata_fields_to_check = ["parent_mass", "precursor_mz", "adduct", "smiles",
"compound_name", "inchi", "inchikey", "charge", "ionmode"]
for metadata_field in metadata_fields_to_check:
for metadata_field in METADATA_FIELDS_OF_INTEREST:
if spectrum_old.get(metadata_field) != spectrum_new.get(metadata_field):
modifications.append(
Modification(metadata_field=metadata_field,
Expand Down

0 comments on commit 1079e3b

Please sign in to comment.