comparison splitMSP.py @ 0:ae0263faa819 draft default tip

"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_split commit 0d9dfeae375a6761b52be79111e228d950e2902e"
author recetox
date Fri, 25 Mar 2022 15:38:00 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ae0263faa819
1 import argparse
2 import os
3 from typing import List
4
5 from matchms import Spectrum
6 from matchms.exporting import save_as_msp
7 from matchms.importing import load_from_msp
8
9
10 def read_spectra(filename: str) -> List[Spectrum]:
11 """Read spectra from file.
12
13 Args:
14 filename (str): Path to .msp file from which to load the spectra.
15
16 Returns:
17 List[Spectrum]: Spectra contained in the file.
18 """
19 return list(load_from_msp(filename, True))
20
21
22 def get_spectra_names(spectra: list) -> List[str]:
23 """Read the keyword 'compound_name' from a spectra.
24
25 Args:
26 spectra (list): List of individual spectra.
27
28 Returns:
29 List[str]: List with 'compoud_name' of individual spectra.
30 """
31 return [x.get("compound_name") for x in spectra]
32
33
34 def make_outdir(outdir: str):
35 """Create destination directory.
36
37 Args:
38 outdir (str): Path to destination directory where split spectra files are generated.
39 """
40 return os.mkdir(outdir)
41
42
43 def write_spectra(filename, outdir):
44 """Generates MSP files of individual spectra. Structure of filename is 'compound_name.msp'.
45
46 Args:
47 filename (str): MSP file that contains the spectra.
48 outdir (str): Path to destination directory.
49 """
50 spectra = read_spectra(filename)
51 names = get_spectra_names(spectra)
52 for i in range(len(spectra)):
53 outpath = assemble_outpath(names[i], outdir)
54 save_as_msp(spectra[i], outpath)
55
56
57 def assemble_outpath(name, outdir):
58 """Filter special chracteres from name.
59
60 Args:
61 name (str): Name to be filetered.
62 outdir (str): Path to destination directory.
63 """
64 filename = ''.join(filter(str.isalnum, name))
65 outfile = str(filename) + ".msp"
66 outpath = os.path.join(outdir, outfile)
67 return outpath
68
69
70 def split_spectra(filename, outdir):
71 """Save individual MSP spectra files in the destination directory.
72
73 Args:
74 filename (str): MSP file that contains the spectra.
75 outdir (str): Path to destination directory where split spectra files are saved.
76 """
77 make_outdir(outdir)
78 return write_spectra(filename, outdir)
79
80
81 listarg = argparse.ArgumentParser()
82 listarg.add_argument('--filename', type=str)
83 listarg.add_argument('--outdir', type=str)
84 args = listarg.parse_args()
85 outdir = args.outdir
86 filename = args.filename
87
88
89 if __name__ == "__main__":
90 split_spectra(filename, outdir)