comparison fill_ctd_clargs.py @ 12:4d189842ebfc draft default tip

planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
author galaxyp
date Thu, 01 Dec 2022 19:03:12 +0000
parents 28e96ae21a44
children
comparison
equal deleted inserted replaced
11:76adea1e0fde 12:4d189842ebfc
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2
3 import operator
2 from argparse import ArgumentParser 4 from argparse import ArgumentParser
5 from functools import reduce # forward compatibility for Python 3
3 from io import StringIO 6 from io import StringIO
4 7
5 from CTDopts.CTDopts import ( 8 from CTDopts.CTDopts import (
9 _Null,
6 CTDModel, 10 CTDModel,
7 ModelTypeError, 11 ModelTypeError,
8 Parameters 12 Parameters
9 ) 13 )
14
15
16 def getFromDict(dataDict, mapList):
17 return reduce(operator.getitem, mapList, dataDict)
18
19
20 def setInDict(dataDict, mapList, value):
21 getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value
22
10 23
11 if __name__ == "__main__": 24 if __name__ == "__main__":
12 # note add_help=False since otherwise arguments starting with -h will 25 # note add_help=False since otherwise arguments starting with -h will
13 # trigger an error (despite allow_abbreviate) 26 # trigger an error (despite allow_abbreviate)
14 parser = ArgumentParser(prog="fill_ctd_clargs", 27 parser = ArgumentParser(prog="fill_ctd_clargs",
15 description="fill command line arguments" 28 description="fill command line arguments"
16 "into a CTD file and write the CTD file to", 29 "into a CTD file and write the CTD file to stdout",
17 add_help=False, allow_abbrev=False) 30 add_help=False, allow_abbrev=False)
18 parser.add_argument("--ctd", dest="ctd", help="input ctd file", 31 parser.add_argument("--ini_file", dest="ini_file", help="input ini file",
19 metavar='CTD', default=None, required=True) 32 metavar='INI', default=None, required=True)
33 parser.add_argument("--ctd_file", dest="ctd_file", help="input ctd file"
34 "if given then optional parameters from the ini file"
35 "will be filled with the defaults from this CTD file",
36 metavar='CTD', default=None, required=False)
20 args, cliargs = parser.parse_known_args() 37 args, cliargs = parser.parse_known_args()
38
21 # load CTDModel 39 # load CTDModel
22 model = None 40 ini_model = None
23 try: 41 try:
24 model = CTDModel(from_file=args.ctd) 42 ini_model = CTDModel(from_file=args.ini_file)
25 except ModelTypeError: 43 except ModelTypeError:
26 pass 44 pass
27 try: 45 try:
28 model = Parameters(from_file=args.ctd) 46 ini_model = Parameters(from_file=args.ini_file)
29 except ModelTypeError: 47 except ModelTypeError:
30 pass 48 pass
31 assert model is not None, "Could not parse %s, seems to be no CTD/PARAMS" % (args.ctd) 49 assert ini_model is not None, "Could not parse %s, seems to be no CTD/PARAMS" % (args.ini_file)
32 50
33 # get a dictionary of the ctd arguments where the values of the parameters 51 # get a dictionary of the ctd arguments where the values of the parameters
34 # given on the command line are overwritten 52 # given on the command line are overwritten
35 margs = model.parse_cl_args(cl_args=cliargs, ignore_required=True) 53 ini_values = ini_model.parse_cl_args(cl_args=cliargs, ignore_required=True)
54
55 if args.ctd_file:
56 ctd_model = CTDModel(from_file=args.ctd_file)
57 ctd_values = ctd_model.get_defaults()
58 for param in ini_model.get_parameters():
59 if not param.required and (param.default is None or type(param.default) is _Null):
60 lineage = param.get_lineage(name_only=True)
61 try:
62 default = getFromDict(ctd_values, lineage)
63 except KeyError:
64 continue
65 setInDict(ini_values, lineage, default)
36 66
37 # write the ctd with the values taken from the dictionary 67 # write the ctd with the values taken from the dictionary
38 out = StringIO() 68 out = StringIO()
39 ctd_tree = model.write_ctd(out, margs) 69 ctd_tree = ini_model.write_ctd(out, ini_values)
40 print(out.getvalue()) 70 print(out.getvalue())