changeset 23:20c30b1a032d draft

Uploaded
author luca_milaz
date Thu, 19 Sep 2024 08:03:27 +0000
parents f3b46a1361ea
children c5dbdbb64cef
files COBRAxy/custom_data_generator.py
diffstat 1 files changed, 21 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/COBRAxy/custom_data_generator.py	Wed Sep 18 12:53:50 2024 +0000
+++ b/COBRAxy/custom_data_generator.py	Thu Sep 19 08:03:27 2024 +0000
@@ -26,17 +26,15 @@
         description = "generate custom data from a given model")
     
     parser.add_argument("-ol", "--out_log", type = str, required = True, help = "Output log")
+
+    parser.add_argument("-orules", "--out_rules", type = str, required = True, help = "Output rules")
+    parser.add_argument("-orxns", "--out_reactions", type = str, required = True, help = "Output reactions")
+    parser.add_argument("-omedium", "--out_medium", type = str, required = True, help = "Output medium")
+    parser.add_argument("-obnds", "--out_bounds", type = str, required = True, help = "Output bounds")
+
     parser.add_argument("-id", "--input",   type = str, required = True, help = "Input model")
     parser.add_argument("-mn", "--name",    type = str, required = True, help = "Input model name")
     # ^ I need this because galaxy converts my files into .dat but I need to know what extension they were in
-
-    parser.add_argument(
-        "-of", "--output_format",
-        # vvv I have to use .fromExt because enums in python are the plague and have been implemented by a chimpanzee.
-        type = utils.FileFormat.fromExt, default = utils.FileFormat.PICKLE,
-        choices = [utils.FileFormat.CSV, utils.FileFormat.PICKLE],
-        # ^^^ Not all variants are valid here, otherwise list(utils.FileFormat) would be best.
-        required = True, help = "Extension of all output files")
     
     argsNamespace = parser.parse_args()
     argsNamespace.out_dir = "result"
@@ -160,7 +158,7 @@
         None
     """
     with open(file_path.show(), 'w', newline='') as csvfile:
-        writer = csv.DictWriter(csvfile, fieldnames = fieldNames)
+        writer = csv.DictWriter(csvfile, fieldnames = fieldNames, dialect="excel-tab")
         writer.writeheader()
 
         for key, value in data.items():
@@ -184,35 +182,21 @@
     # load custom model
     model = load_custom_model(
         utils.FilePath.fromStrPath(ARGS.input), utils.FilePath.fromStrPath(ARGS.name).ext)
-    
-    # generate data and save it in the desired format and in a location galaxy understands
-    # (it should show up as a collection in the history)
-    rulesPath     = utils.FilePath("rules",     ARGS.output_format, prefix = ARGS.out_dir)
-    reactionsPath = utils.FilePath("reactions", ARGS.output_format, prefix = ARGS.out_dir)
-    boundsPath = utils.FilePath("bounds",     ARGS.output_format, prefix = ARGS.out_dir)
-    mediumPath = utils.FilePath("medium",     ARGS.output_format, prefix = ARGS.out_dir)
+
+    rules = generate_rules(model, asParsed = False)
+    reactions = generate_reactions(model, asParsed = False)
+    bounds = generate_bounds(model)
+    medium = get_medium(model)
 
-    if ARGS.output_format is utils.FileFormat.PICKLE:
-        rules = generate_rules(model, asParsed = True)
-        reactions = generate_reactions(model, asParsed = True)
-        bounds = generate_bounds(model)
-        medium = get_medium(model)
-        utils.writePickle(rulesPath,     rules)
-        utils.writePickle(reactionsPath, reactions)
-        utils.writePickle(boundsPath, bounds)
-        utils.writePickle(mediumPath, medium)
-        bounds.to_pickle(boundsPath.show())
-        medium.to_pickle(mediumPath.show())
-    
-    elif ARGS.output_format is utils.FileFormat.CSV:
-        rules = generate_rules(model, asParsed = False)
-        reactions = generate_reactions(model, asParsed = False)
-        bounds = generate_bounds(model)
-        medium = get_medium(model)
-        save_as_csv(rules,     rulesPath,     ("ReactionID", "Rule"))
-        save_as_csv(reactions, reactionsPath, ("ReactionID", "Reaction"))
-        bounds.to_csv(boundsPath.show())
-        medium.to_csv(mediumPath.show())
+    rulesPath     = utils.FilePath("rules", "csv")
+    reactionsPath = utils.FilePath("reactions", "csv")
+    boundsPath = utils.FilePath("bounds", "csv")
+    mediumPath = utils.FilePath("medium", "csv")
+
+    save_as_csv(rules, rulesPath, ("ReactionID", "Rule"))
+    save_as_csv(reactions, reactionsPath, ("ReactionID", "Reaction"))
+    bounds.to_csv(boundsPath.show(), sep = '\t')
+    medium.to_csv(mediumPath.show(), sep = '\t')
 
 
     # ^ Please if anyone works on this after updating python to 3.12 change the if/elif into a match statement!!