diff matchms_split.py @ 23:34439ffa6a60 draft

planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit da193865f41a3a840ecc4ba0afab1d358554998a
author recetox
date Mon, 05 Feb 2024 10:39:25 +0000
parents 1b09315a3f87
children
line wrap: on
line diff
--- a/matchms_split.py	Mon Jan 15 12:25:49 2024 +0000
+++ b/matchms_split.py	Mon Feb 05 10:39:25 2024 +0000
@@ -1,22 +1,13 @@
 import argparse
 import itertools
 import os
-from typing import List
 
+import matchms
 from matchms.exporting import save_as_msp
 from matchms.importing import load_from_msp
 
 
-def get_spectra_names(spectra: list) -> List[str]:
-    """Read the keyword 'compound_name' from a spectra.
-
-    Args:
-        spectra (list): List of individual spectra.
-
-    Returns:
-        List[str]: List with 'compoud_name' of individual spectra.
-    """
-    return [x.get("compound_name") for x in spectra]
+matchms.Metadata.set_key_replacements({})
 
 
 def make_outdir(outdir: str):
@@ -35,23 +26,8 @@
         spectra (List[Spectrum]): Spectra to write to file
         outdir   (str): Path to destination directory.
     """
-    names = get_spectra_names(spectra)
     for i in range(len(spectra)):
-        outpath = assemble_outpath(names[i], outdir)
-        save_as_msp(spectra[i], outpath)
-
-
-def assemble_outpath(name, outdir):
-    """Filter special chracteres from name.
-
-    Args:
-        name   (str): Name to be filetered.
-        outdir (str): Path to destination directory.
-    """
-    filename = ''.join(filter(str.isalnum, name))
-    outfile = str(filename) + ".msp"
-    outpath = os.path.join(outdir, outfile)
-    return outpath
+        save_as_msp(spectra[i], os.path.join(outdir, f"{i}.msp"))
 
 
 def split_round_robin(iterable, num_chunks):
@@ -76,7 +52,7 @@
 
 
 if __name__ == "__main__":
-    spectra = load_from_msp(filename, metadata_harmonization=True)
+    spectra = load_from_msp(filename, metadata_harmonization=False)
     make_outdir(outdir)
 
     if method == "one-per-file":