diff fill_ctd_clargs.py @ 8:4d2b77fd7a28 draft

planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/openms commit 3d1e5f37fd16524a415f707772eeb7ead848c5e3
author galaxyp
date Thu, 01 Dec 2022 18:56:34 +0000
parents a2a842b00f9c
children
line wrap: on
line diff
--- a/fill_ctd_clargs.py	Fri Nov 06 19:52:36 2020 +0000
+++ b/fill_ctd_clargs.py	Thu Dec 01 18:56:34 2022 +0000
@@ -1,40 +1,70 @@
 #!/usr/bin/env python3
+
+import operator
 from argparse import ArgumentParser
+from functools import reduce  # forward compatibility for Python 3
 from io import StringIO
 
 from CTDopts.CTDopts import (
+    _Null,
     CTDModel,
     ModelTypeError,
     Parameters
 )
 
+
+def getFromDict(dataDict, mapList):
+    return reduce(operator.getitem, mapList, dataDict)
+
+
+def setInDict(dataDict, mapList, value):
+    getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value
+
+
 if __name__ == "__main__":
     # note add_help=False since otherwise arguments starting with -h will
     # trigger an error (despite allow_abbreviate)
     parser = ArgumentParser(prog="fill_ctd_clargs",
                             description="fill command line arguments"
-                            "into a CTD file and write the CTD file to",
+                            "into a CTD file and write the CTD file to stdout",
                             add_help=False, allow_abbrev=False)
-    parser.add_argument("--ctd", dest="ctd", help="input ctd file",
-                        metavar='CTD', default=None, required=True)
+    parser.add_argument("--ini_file", dest="ini_file", help="input ini file",
+                        metavar='INI', default=None, required=True)
+    parser.add_argument("--ctd_file", dest="ctd_file", help="input ctd file"
+                        "if given then optional parameters from the ini file"
+                        "will be filled with the defaults from this CTD file",
+                        metavar='CTD', default=None, required=False)
     args, cliargs = parser.parse_known_args()
+
     # load CTDModel
-    model = None
+    ini_model = None
     try:
-        model = CTDModel(from_file=args.ctd)
+        ini_model = CTDModel(from_file=args.ini_file)
     except ModelTypeError:
         pass
     try:
-        model = Parameters(from_file=args.ctd)
+        ini_model = Parameters(from_file=args.ini_file)
     except ModelTypeError:
         pass
-    assert model is not None, "Could not parse %s, seems to be no CTD/PARAMS" % (args.ctd)
+    assert ini_model is not None, "Could not parse %s, seems to be no CTD/PARAMS" % (args.ini_file)
 
     # get a dictionary of the ctd arguments where the values of the parameters
     # given on the command line are overwritten
-    margs = model.parse_cl_args(cl_args=cliargs, ignore_required=True)
+    ini_values = ini_model.parse_cl_args(cl_args=cliargs, ignore_required=True)
+
+    if args.ctd_file:
+        ctd_model = CTDModel(from_file=args.ctd_file)
+        ctd_values = ctd_model.get_defaults()
+        for param in ini_model.get_parameters():
+            if not param.required and (param.default is None or type(param.default) is _Null):
+                lineage = param.get_lineage(name_only=True)
+                try:
+                    default = getFromDict(ctd_values, lineage)
+                except KeyError:
+                    continue
+                setInDict(ini_values, lineage, default)
 
     # write the ctd with the values taken from the dictionary
     out = StringIO()
-    ctd_tree = model.write_ctd(out, margs)
+    ctd_tree = ini_model.write_ctd(out, ini_values)
     print(out.getvalue())