Mercurial > repos > recetox > matchms_split
annotate matchms_split.py @ 2:5d9d107485ec draft
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 701733d99742a8e567ed6db7384be66c5ecb4a58
| author | recetox | 
|---|---|
| date | Thu, 18 May 2023 13:23:04 +0000 | 
| parents | 0b2ea1dc4c8c | 
| children | 49f9aa5cfa21 | 
| rev | line source | 
|---|---|
| 0 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 1 import argparse | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 2 import itertools | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 3 import os | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 4 from typing import List | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 5 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 6 from matchms import Spectrum | 
| 
0b2ea1dc4c8c
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 | 
| 
0b2ea1dc4c8c
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 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 9 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 10 | 
| 
0b2ea1dc4c8c
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]: | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 12 """Read spectra from file. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 13 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 14 Args: | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 16 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 17 Returns: | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 19 """ | 
| 
0b2ea1dc4c8c
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)) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 21 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 22 | 
| 
0b2ea1dc4c8c
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]: | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 25 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 26 Args: | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 28 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 29 Returns: | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 31 """ | 
| 
0b2ea1dc4c8c
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] | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 33 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 34 | 
| 
0b2ea1dc4c8c
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): | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 36 """Create destination directory. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 37 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 38 Args: | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 40 """ | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 41 return os.mkdir(outdir) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 42 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 43 | 
| 
0b2ea1dc4c8c
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): | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 46 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 47 Args: | 
| 
0b2ea1dc4c8c
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 | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 50 """ | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
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)): | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 55 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 56 | 
| 
0b2ea1dc4c8c
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): | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 59 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 60 Args: | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 63 """ | 
| 
0b2ea1dc4c8c
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)) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 65 outfile = str(filename) + ".msp" | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 67 return outpath | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 68 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 69 | 
| 
0b2ea1dc4c8c
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): | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 72 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 73 Args: | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
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. | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 76 """ | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 77 make_outdir(outdir) | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 79 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 80 | 
| 
0b2ea1dc4c8c
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): | 
| 
0b2ea1dc4c8c
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)] | 
| 
0b2ea1dc4c8c
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)) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 84 for value in iterable: | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 87 return chunks | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 88 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 89 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 90 listarg = argparse.ArgumentParser() | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 95 args = listarg.parse_args() | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 96 outdir = args.outdir | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 97 filename = args.filename | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 98 method = args.method | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 99 parameter = args.parameter | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 100 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 101 | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 102 if __name__ == "__main__": | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 104 make_outdir(outdir) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 105 | 
| 
0b2ea1dc4c8c
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": | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 108 else: | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 109 if method == "chunk-size": | 
| 
0b2ea1dc4c8c
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)), []) | 
| 
0b2ea1dc4c8c
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 5888b20035c9c782b7c94495b0760134f82f4c2e
 recetox parents: diff
changeset | 111 elif method == "num-chunks": | 
| 
0b2ea1dc4c8c
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) | 
| 
0b2ea1dc4c8c
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): | 
| 
0b2ea1dc4c8c
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")) | 
