annotate matchms_split.py @ 0:169c72b2ce79 draft

planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
author recetox
date Thu, 27 Apr 2023 12:02:44 +0000
parents
children 0cf68b536cd1
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 import Spectrum
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
7 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
8 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
9
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
10
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
11 def read_spectra(filename: str) -> List[Spectrum]:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
12 """Read spectra from file.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
13
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
14 Args:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
15 filename (str): Path to .msp file from which to load the spectra.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
16
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
17 Returns:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
18 List[Spectrum]: Spectra contained in the file.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
19 """
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
20 return list(load_from_msp(filename, True))
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
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
23 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
24 """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
25
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
26 Args:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
27 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
28
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
29 Returns:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
30 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
31 """
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
32 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
33
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
34
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
35 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
36 """Create 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 Args:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
39 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
40 """
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
41 return os.mkdir(outdir)
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 write_spectra(spectra, outdir):
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
45 """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
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 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
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 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
52 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
53 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
54 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
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 assemble_outpath(name, outdir):
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
58 """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
59
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
60 Args:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
61 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
62 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
63 """
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
64 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
65 outfile = str(filename) + ".msp"
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
66 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
67 return outpath
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
68
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
69
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
70 def split_spectra(filename, outdir):
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
71 """Save individual MSP spectra files in the destination directory.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
72
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
73 Args:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
74 filename (str): MSP file that contains the spectra.
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
75 outdir (str): Path to destination directory where split spectra files are saved.
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 make_outdir(outdir)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
78 return write_spectra(filename, outdir)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
79
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
80
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
81 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
82 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
83 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
84 for value in iterable:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
85 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
86 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
87 return chunks
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
88
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
89
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
90 listarg = argparse.ArgumentParser()
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
91 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
92 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
93 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
94 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
95 args = listarg.parse_args()
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
96 outdir = args.outdir
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
97 filename = args.filename
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
98 method = args.method
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
99 parameter = args.parameter
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
100
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
101
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
102 if __name__ == "__main__":
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
103 spectra = load_from_msp(filename)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
104 make_outdir(outdir)
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
105
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
106 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
107 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
108 else:
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
109 if method == "chunk-size":
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
110 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
111 elif method == "num-chunks":
169c72b2ce79 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
recetox
parents:
diff changeset
112 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
113 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
114 save_as_msp(x, os.path.join(outdir, f"chunk_{i}.msp"))