annotate matchms_split.py @ 4:0cf68b536cd1 draft

planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
author recetox
date Tue, 27 Jun 2023 14:25:30 +0000
parents 169c72b2ce79
children 114617e6ad33
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
1 import argparse
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
2 import itertools
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
3 import os
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
4 from typing import List
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
5
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
6 from matchms.exporting import save_as_msp
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
7 from matchms.importing import load_from_msp
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
8
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
9
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
10 def get_spectra_names(spectra: list) -> List[str]:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
11 """Read the keyword 'compound_name' from a spectra.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
12
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
13 Args:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
14 spectra (list): List of individual spectra.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
15
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
16 Returns:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
17 List[str]: List with 'compoud_name' of individual spectra.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
18 """
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
19 return [x.get("compound_name") for x in spectra]
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
20
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
21
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
22 def make_outdir(outdir: str):
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
23 """Create destination directory.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
24
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
25 Args:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
26 outdir (str): Path to destination directory where split spectra files are generated.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
27 """
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
28 return os.mkdir(outdir)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
29
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
30
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
31 def write_spectra(spectra, outdir):
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
32 """Generates MSP files of individual spectra.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
33
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
34 Args:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
35 spectra (List[Spectrum]): Spectra to write to file
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
36 outdir (str): Path to destination directory.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
37 """
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
38 names = get_spectra_names(spectra)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
39 for i in range(len(spectra)):
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
40 outpath = assemble_outpath(names[i], outdir)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
41 save_as_msp(spectra[i], outpath)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
42
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
43
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
44 def assemble_outpath(name, outdir):
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
45 """Filter special chracteres from name.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
46
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
47 Args:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
48 name (str): Name to be filetered.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
49 outdir (str): Path to destination directory.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
50 """
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
51 filename = ''.join(filter(str.isalnum, name))
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
52 outfile = str(filename) + ".msp"
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
53 outpath = os.path.join(outdir, outfile)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
54 return outpath
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
55
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
56
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
57 def split_round_robin(iterable, num_chunks):
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
58 chunks = [list() for _ in range(num_chunks)]
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
59 index = itertools.cycle(range(num_chunks))
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
60 for value in iterable:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
61 chunks[next(index)].append(value)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
62 chunks = filter(lambda x: len(x) > 0, chunks)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
63 return chunks
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
64
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
65
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
66 listarg = argparse.ArgumentParser()
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
67 listarg.add_argument('--filename', type=str)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
68 listarg.add_argument('--method', type=str)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
69 listarg.add_argument('--outdir', type=str)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
70 listarg.add_argument('--parameter', type=int)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
71 args = listarg.parse_args()
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
72 outdir = args.outdir
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
73 filename = args.filename
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
74 method = args.method
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
75 parameter = args.parameter
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
76
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
77
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
78 if __name__ == "__main__":
4
0cf68b536cd1 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit f79a5b51599254817727bc9028b9797ea994cb4e
recetox
parents: 0
diff changeset
79 spectra = load_from_msp(filename, metadata_harmonization=True)
0
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
80 make_outdir(outdir)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
81
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
82 if method == "one-per-file":
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
83 write_spectra(list(spectra), outdir)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
84 else:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
85 if method == "chunk-size":
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
86 chunks = iter(lambda: list(itertools.islice(spectra, parameter)), [])
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
87 elif method == "num-chunks":
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
88 chunks = split_round_robin(spectra, parameter)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
89 for i, x in enumerate(chunks):
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
90 save_as_msp(x, os.path.join(outdir, f"chunk_{i}.msp"))