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