Mercurial > repos > recetox > msp_merge
annotate msp_merge.py @ 0:c2862090e321 draft
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
author | recetox |
---|---|
date | Thu, 19 May 2022 12:04:25 +0000 |
parents | |
children |
rev | line source |
---|---|
0
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
1 import argparse |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
2 from itertools import chain |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
3 from typing import List |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
4 |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
5 from matchms import Spectrum |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
6 from matchms.exporting import save_as_msp |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
7 from matchms.importing import load_from_msp |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
8 |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
9 |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
10 def read_spectra(filenames: str) -> List[Spectrum]: |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
11 """Read spectra from files. |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
12 |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
13 Args: |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
14 filenames (str): Paths to MSP files from which to load each spectrum. |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
15 |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
16 Returns: |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
17 List[Spectrum]: Spectra stored in the file. |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
18 """ |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
19 spectra = list(chain(*[load_from_msp(file) for file in filenames])) |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
20 return spectra |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
21 |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
22 |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
23 listarg = argparse.ArgumentParser() |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
24 listarg.add_argument('--filenames', nargs='+', type=str) |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
25 listarg.add_argument('--outfilename', type=str) |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
26 args = listarg.parse_args() |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
27 |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
28 if __name__ == "__main__": |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
29 spectra = read_spectra(args.filenames) |
c2862090e321
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msp_merge commit 51ff658aecc8738ef57af512229cd155763082d1
recetox
parents:
diff
changeset
|
30 save_as_msp(spectra, args.outfilename) |