comparison fill_ctd_clargs.py @ 6:907fda39b333 draft default tip

planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 5c080b1e2b99f1c88f4557e9fec8c45c9d23b906
author galaxyp
date Fri, 14 Jun 2024 21:28:16 +0000
parents 9866e1170611
children
comparison
equal deleted inserted replaced
5:9866e1170611 6:907fda39b333
1 #!/usr/bin/env python3
2
3 import operator
4 from argparse import ArgumentParser
5 from functools import reduce # forward compatibility for Python 3
6 from io import StringIO
7
8 from CTDopts.CTDopts import (
9 _Null,
10 CTDModel,
11 ModelTypeError,
12 Parameters
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
23
24 if __name__ == "__main__":
25 # note add_help=False since otherwise arguments starting with -h will
26 # trigger an error (despite allow_abbreviate)
27 parser = ArgumentParser(prog="fill_ctd_clargs",
28 description="fill command line arguments"
29 "into a CTD file and write the CTD file to stdout",
30 add_help=False, allow_abbrev=False)
31 parser.add_argument("--ini_file", dest="ini_file", help="input ini file",
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)
37 args, cliargs = parser.parse_known_args()
38
39 # load CTDModel
40 ini_model = None
41 try:
42 ini_model = CTDModel(from_file=args.ini_file)
43 except ModelTypeError:
44 pass
45 try:
46 ini_model = Parameters(from_file=args.ini_file)
47 except ModelTypeError:
48 pass
49 assert ini_model is not None, "Could not parse %s, seems to be no CTD/PARAMS" % (args.ini_file)
50
51 # get a dictionary of the ctd arguments where the values of the parameters
52 # given on the command line are overwritten
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)
66
67 # write the ctd with the values taken from the dictionary
68 out = StringIO()
69 ctd_tree = ini_model.write_ctd(out, ini_values)
70 print(out.getvalue())