comparison 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
comparison
equal deleted inserted replaced
3:9dfcee100f48 4:0cf68b536cd1
1 import argparse 1 import argparse
2 import itertools 2 import itertools
3 import os 3 import os
4 from typing import List 4 from typing import List
5 5
6 from matchms import Spectrum
7 from matchms.exporting import save_as_msp 6 from matchms.exporting import save_as_msp
8 from matchms.importing import load_from_msp 7 from matchms.importing import load_from_msp
9
10
11 def read_spectra(filename: str) -> List[Spectrum]:
12 """Read spectra from file.
13
14 Args:
15 filename (str): Path to .msp file from which to load the spectra.
16
17 Returns:
18 List[Spectrum]: Spectra contained in the file.
19 """
20 return list(load_from_msp(filename, True))
21 8
22 9
23 def get_spectra_names(spectra: list) -> List[str]: 10 def get_spectra_names(spectra: list) -> List[str]:
24 """Read the keyword 'compound_name' from a spectra. 11 """Read the keyword 'compound_name' from a spectra.
25 12
65 outfile = str(filename) + ".msp" 52 outfile = str(filename) + ".msp"
66 outpath = os.path.join(outdir, outfile) 53 outpath = os.path.join(outdir, outfile)
67 return outpath 54 return outpath
68 55
69 56
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 def split_round_robin(iterable, num_chunks): 57 def split_round_robin(iterable, num_chunks):
82 chunks = [list() for _ in range(num_chunks)] 58 chunks = [list() for _ in range(num_chunks)]
83 index = itertools.cycle(range(num_chunks)) 59 index = itertools.cycle(range(num_chunks))
84 for value in iterable: 60 for value in iterable:
85 chunks[next(index)].append(value) 61 chunks[next(index)].append(value)
98 method = args.method 74 method = args.method
99 parameter = args.parameter 75 parameter = args.parameter
100 76
101 77
102 if __name__ == "__main__": 78 if __name__ == "__main__":
103 spectra = load_from_msp(filename) 79 spectra = load_from_msp(filename, metadata_harmonization=True)
104 make_outdir(outdir) 80 make_outdir(outdir)
105 81
106 if method == "one-per-file": 82 if method == "one-per-file":
107 write_spectra(list(spectra), outdir) 83 write_spectra(list(spectra), outdir)
108 else: 84 else: