Mercurial > repos > recetox > matchms_split
comparison matchms_split.py @ 14:114617e6ad33 draft
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit da193865f41a3a840ecc4ba0afab1d358554998a
| author | recetox |
|---|---|
| date | Mon, 05 Feb 2024 10:35:49 +0000 |
| parents | 0cf68b536cd1 |
| children | 7676700d7c4f |
comparison
equal
deleted
inserted
replaced
| 13:fc1bc38ede0b | 14:114617e6ad33 |
|---|---|
| 1 import argparse | 1 import argparse |
| 2 import itertools | 2 import itertools |
| 3 import os | 3 import os |
| 4 from typing import List | |
| 5 | 4 |
| 5 import matchms | |
| 6 from matchms.exporting import save_as_msp | 6 from matchms.exporting import save_as_msp |
| 7 from matchms.importing import load_from_msp | 7 from matchms.importing import load_from_msp |
| 8 | 8 |
| 9 | 9 |
| 10 def get_spectra_names(spectra: list) -> List[str]: | 10 matchms.Metadata.set_key_replacements({}) |
| 11 """Read the keyword 'compound_name' from a spectra. | |
| 12 | |
| 13 Args: | |
| 14 spectra (list): List of individual spectra. | |
| 15 | |
| 16 Returns: | |
| 17 List[str]: List with 'compoud_name' of individual spectra. | |
| 18 """ | |
| 19 return [x.get("compound_name") for x in spectra] | |
| 20 | 11 |
| 21 | 12 |
| 22 def make_outdir(outdir: str): | 13 def make_outdir(outdir: str): |
| 23 """Create destination directory. | 14 """Create destination directory. |
| 24 | 15 |
| 33 | 24 |
| 34 Args: | 25 Args: |
| 35 spectra (List[Spectrum]): Spectra to write to file | 26 spectra (List[Spectrum]): Spectra to write to file |
| 36 outdir (str): Path to destination directory. | 27 outdir (str): Path to destination directory. |
| 37 """ | 28 """ |
| 38 names = get_spectra_names(spectra) | |
| 39 for i in range(len(spectra)): | 29 for i in range(len(spectra)): |
| 40 outpath = assemble_outpath(names[i], outdir) | 30 save_as_msp(spectra[i], os.path.join(outdir, f"{i}.msp")) |
| 41 save_as_msp(spectra[i], outpath) | |
| 42 | |
| 43 | |
| 44 def assemble_outpath(name, outdir): | |
| 45 """Filter special chracteres from name. | |
| 46 | |
| 47 Args: | |
| 48 name (str): Name to be filetered. | |
| 49 outdir (str): Path to destination directory. | |
| 50 """ | |
| 51 filename = ''.join(filter(str.isalnum, name)) | |
| 52 outfile = str(filename) + ".msp" | |
| 53 outpath = os.path.join(outdir, outfile) | |
| 54 return outpath | |
| 55 | 31 |
| 56 | 32 |
| 57 def split_round_robin(iterable, num_chunks): | 33 def split_round_robin(iterable, num_chunks): |
| 58 chunks = [list() for _ in range(num_chunks)] | 34 chunks = [list() for _ in range(num_chunks)] |
| 59 index = itertools.cycle(range(num_chunks)) | 35 index = itertools.cycle(range(num_chunks)) |
| 74 method = args.method | 50 method = args.method |
| 75 parameter = args.parameter | 51 parameter = args.parameter |
| 76 | 52 |
| 77 | 53 |
| 78 if __name__ == "__main__": | 54 if __name__ == "__main__": |
| 79 spectra = load_from_msp(filename, metadata_harmonization=True) | 55 spectra = load_from_msp(filename, metadata_harmonization=False) |
| 80 make_outdir(outdir) | 56 make_outdir(outdir) |
| 81 | 57 |
| 82 if method == "one-per-file": | 58 if method == "one-per-file": |
| 83 write_spectra(list(spectra), outdir) | 59 write_spectra(list(spectra), outdir) |
| 84 else: | 60 else: |
