Mercurial > repos > galaxyp > openms_maprttransformer
annotate fill_ctd_clargs.py @ 13:aec26bf377fb draft
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
| author | galaxyp | 
|---|---|
| date | Thu, 01 Dec 2022 19:14:37 +0000 | 
| parents | fd6de4cdb9e9 | 
| children | 
| rev | line source | 
|---|---|
| 9 
fd6de4cdb9e9
"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 | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 2 | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 3 import operator | 
| 9 
fd6de4cdb9e9
"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 | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 5 from functools import reduce # forward compatibility for Python 3 | 
| 9 
fd6de4cdb9e9
"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 | 
| 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 7 | 
| 
fd6de4cdb9e9
"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 ( | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 9 _Null, | 
| 9 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 10 CTDModel, | 
| 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 11 ModelTypeError, | 
| 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 12 Parameters | 
| 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 13 ) | 
| 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 14 | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 15 | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 16 def getFromDict(dataDict, mapList): | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 17 return reduce(operator.getitem, mapList, dataDict) | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 18 | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 19 | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 20 def setInDict(dataDict, mapList, value): | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 21 getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 22 | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 23 | 
| 9 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 24 if __name__ == "__main__": | 
| 
fd6de4cdb9e9
"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 | 
| 
fd6de4cdb9e9
"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) | 
| 
fd6de4cdb9e9
"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", | 
| 
fd6de4cdb9e9
"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" | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 29 "into a CTD file and write the CTD file to stdout", | 
| 9 
fd6de4cdb9e9
"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) | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 31 parser.add_argument("--ini_file", dest="ini_file", help="input ini file", | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 32 metavar='INI', default=None, required=True) | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 33 parser.add_argument("--ctd_file", dest="ctd_file", help="input ctd file" | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 34 "if given then optional parameters from the ini file" | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 35 "will be filled with the defaults from this CTD file", | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 36 metavar='CTD', default=None, required=False) | 
| 9 
fd6de4cdb9e9
"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() | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 38 | 
| 9 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 39 # load CTDModel | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 40 ini_model = None | 
| 9 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 41 try: | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 42 ini_model = CTDModel(from_file=args.ini_file) | 
| 9 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 43 except ModelTypeError: | 
| 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 44 pass | 
| 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 45 try: | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 46 ini_model = Parameters(from_file=args.ini_file) | 
| 9 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 47 except ModelTypeError: | 
| 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 48 pass | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 49 assert ini_model is not None, "Could not parse %s, seems to be no CTD/PARAMS" % (args.ini_file) | 
| 9 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 50 | 
| 
fd6de4cdb9e9
"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 | 
| 
fd6de4cdb9e9
"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 | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 53 ini_values = ini_model.parse_cl_args(cl_args=cliargs, ignore_required=True) | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 54 | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 55 if args.ctd_file: | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 56 ctd_model = CTDModel(from_file=args.ctd_file) | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 57 ctd_values = ctd_model.get_defaults() | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 58 for param in ini_model.get_parameters(): | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 59 if not param.required and (param.default is None or type(param.default) is _Null): | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 60 lineage = param.get_lineage(name_only=True) | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 61 try: | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 62 default = getFromDict(ctd_values, lineage) | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 63 except KeyError: | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 64 continue | 
| 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 65 setInDict(ini_values, lineage, default) | 
| 9 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 66 | 
| 
fd6de4cdb9e9
"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 | 
| 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 68 out = StringIO() | 
| 13 
aec26bf377fb
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
 galaxyp parents: 
9diff
changeset | 69 ctd_tree = ini_model.write_ctd(out, ini_values) | 
| 9 
fd6de4cdb9e9
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit ddf41e8bda1ba065f5cdec98e93dee8165ffc1b9"
 galaxyp parents: diff
changeset | 70 print(out.getvalue()) | 
