From 8b1b4d834d338e28c170006804959982f3880859 Mon Sep 17 00:00:00 2001 From: niekdejonge Date: Thu, 2 Nov 2023 15:11:24 +0100 Subject: [PATCH] Implemented return metadata in return user validation info --- library_spectra_validation/library_handler.py | 11 ++++++----- .../tests/test_library_handler.py | 7 +++++++ library_spectra_validation/validation_pipeline.py | 7 ++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/library_spectra_validation/library_handler.py b/library_spectra_validation/library_handler.py index 315a8f4..9d36978 100644 --- a/library_spectra_validation/library_handler.py +++ b/library_spectra_validation/library_handler.py @@ -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. @@ -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""" @@ -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]) - - \ No newline at end of file diff --git a/library_spectra_validation/tests/test_library_handler.py b/library_spectra_validation/tests/test_library_handler.py index 733a490..c8c5be7 100644 --- a/library_spectra_validation/tests/test_library_handler.py +++ b/library_spectra_validation/tests/test_library_handler.py @@ -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) \ No newline at end of file diff --git a/library_spectra_validation/validation_pipeline.py b/library_spectra_validation/validation_pipeline.py index 6cc327c..f0d5138 100644 --- a/library_spectra_validation/validation_pipeline.py +++ b/library_spectra_validation/validation_pipeline.py @@ -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): @@ -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,