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