comparison msmetaenhancer_wrapper.py @ 8:2ada1099f42e draft

planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/msmetaenhancer commit 538d6c063bb049fe7a08d7d4bd88af91c0d696e8
author recetox
date Thu, 18 May 2023 12:57:50 +0000
parents f387ebd6b734
children 791bf4d9b06c
comparison
equal deleted inserted replaced
7:37afe368348b 8:2ada1099f42e
1 import argparse 1 import argparse
2 import asyncio 2 import asyncio
3 import os
4 import shutil
3 import sys 5 import sys
4 6
5 from matchms import set_matchms_logger_level 7 from matchms import set_matchms_logger_level
6 from MSMetaEnhancer import Application 8 from MSMetaEnhancer import Application
7 9
8 10
11 def handle_xlsx_file(app, filename):
12 basename = os.path.splitext(filename)[0]
13 temp_file = basename + '.xlsx'
14 app.save_data(temp_file, file_format='xlsx')
15 shutil.copyfile(temp_file, filename)
16
17
9 def main(argv): 18 def main(argv):
10 parser = argparse.ArgumentParser(description="Annotate MSP file.") 19 parser = argparse.ArgumentParser(description="Annotate spectra file.")
11 parser.add_argument("--input_file", type=str, help="Path to query spectra file in MSP format.") 20 parser.add_argument("--input_file", type=str, help="Path to query spectra file.")
21 parser.add_argument("--file_format", type=str, help="Format of the input and the output files.")
12 parser.add_argument("--output_file", type=str, help="Path to output spectra file.") 22 parser.add_argument("--output_file", type=str, help="Path to output spectra file.")
13 parser.add_argument("--jobs", type=str, help="Sequence of conversion jobs to be used.") 23 parser.add_argument("--jobs", type=str, help="Sequence of conversion jobs to be used.")
14 parser.add_argument("--log_file", type=str, help="Path to log with details of the annotation process.") 24 parser.add_argument("--log_file", type=str, help="Path to log with details of the annotation process.")
15 parser.add_argument("--log_level", type=str, default='info', 25 parser.add_argument("--log_level", type=str, default='info',
16 help="Severity of log messages present in the log file.") 26 help="Severity of log messages present in the log file.")
18 28
19 app = Application(log_level=args.log_level, log_file=args.log_file) 29 app = Application(log_level=args.log_level, log_file=args.log_file)
20 30
21 # set matchms logging level to avoid extensive messages in stdout while reading file 31 # set matchms logging level to avoid extensive messages in stdout while reading file
22 set_matchms_logger_level("ERROR") 32 set_matchms_logger_level("ERROR")
23 # import .msp file 33 # import spectra file
24 app.load_spectra(args.input_file, file_format='msp') 34 app.load_data(args.input_file, file_format=args.file_format)
25 35
26 # set matchms logging level back to warning 36 # set matchms logging level back to warning
27 set_matchms_logger_level("WARNING") 37 set_matchms_logger_level("WARNING")
28 38
29 # curate given metadata 39 # curate given metadata
30 app.curate_spectra() 40 app.curate_metadata()
31 41
32 # specify requested services and jobs 42 # specify requested services and jobs
33 services = ['PubChem', 'CTS', 'CIR', 'NLM', 'RDKit', 'IDSM', 'BridgeDB'] 43 services = ['PubChem', 'CTS', 'CIR', 'RDKit', 'IDSM', 'BridgeDb']
34 44
35 if len(args.jobs) != 0: 45 if len(args.jobs) != 0:
36 jobs = [] 46 jobs = []
37 for job in args.jobs.split(","): 47 for job in args.jobs.split(","):
38 if len(job) != 0: 48 if len(job) != 0:
40 asyncio.run(app.annotate_spectra(services, jobs)) 50 asyncio.run(app.annotate_spectra(services, jobs))
41 else: 51 else:
42 # execute without jobs parameter to run all possible jobs 52 # execute without jobs parameter to run all possible jobs
43 asyncio.run(app.annotate_spectra(services)) 53 asyncio.run(app.annotate_spectra(services))
44 54
45 # export .msp file 55 # export spectra file
46 app.save_spectra(args.output_file, file_format="msp") 56 if args.file_format == 'xlsx':
57 handle_xlsx_file(app, args.output_file)
58 else:
59 app.save_data(args.output_file, file_format=args.file_format)
47 return 0 60 return 0
48 61
49 62
50 if __name__ == "__main__": 63 if __name__ == "__main__":
51 main(argv=sys.argv[1:]) 64 main(argv=sys.argv[1:])