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