changeset 368:5443289f6edb draft

Uploaded
author luca_milaz
date Thu, 19 Sep 2024 08:24:51 +0000
parents b2b2312b8f77
children c24bc5bd4a93
files marea_2/custom_data_generator.py
diffstat 1 files changed, 38 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/marea_2/custom_data_generator.py	Wed Sep 18 12:03:47 2024 +0000
+++ b/marea_2/custom_data_generator.py	Thu Sep 19 08:24:51 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"
@@ -147,9 +145,9 @@
 
 
 ###############################- FILE SAVING -################################
-def save_as_csv(data :dict, file_path :utils.FilePath, fieldNames :Tuple[str, str]) -> None:
+def save_as_csv_filePath(data :dict, file_path :utils.FilePath, fieldNames :Tuple[str, str]) -> None:
     """
-    Saves any dictionary-shaped data in a .csv file created at the given file_path.
+    Saves any dictionary-shaped data in a .csv file created at the given file_path as FilePath.
 
     Args:
         data : the data to be written to the file.
@@ -160,7 +158,26 @@
         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():
+            writer.writerow({ fieldNames[0] : key, fieldNames[1] : value })
+
+def save_as_csv(data :dict, file_path :str, fieldNames :Tuple[str, str]) -> None:
+    """
+    Saves any dictionary-shaped data in a .csv file created at the given file_path as string.
+
+    Args:
+        data : the data to be written to the file.
+        file_path : the path to the .csv file.
+        fieldNames : the names of the fields (columns) in the .csv file.
+    
+    Returns:
+        None
+    """
+    with open(file_path, 'w', newline='') as csvfile:
+        writer = csv.DictWriter(csvfile, fieldnames = fieldNames, dialect="excel-tab")
         writer.writeheader()
 
         for key, value in data.items():
@@ -184,38 +201,18 @@
     # 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)
 
-    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())
+    # generate data
+    rules = generate_rules(model, asParsed = False)
+    reactions = generate_reactions(model, asParsed = False)
+    bounds = generate_bounds(model)
+    medium = get_medium(model)
 
-
-    # ^ Please if anyone works on this after updating python to 3.12 change the if/elif into a match statement!!
+    # save files out of collection: path coming from xml
+    save_as_csv(rules, ARGS.out_rules, ("ReactionID", "Rule"))
+    save_as_csv(reactions, ARGS.out_reactions, ("ReactionID", "Reaction"))
+    bounds.to_csv(ARGS.out_bounds, sep = '\t')
+    medium.to_csv(ARGS.out_medium, sep = '\t')
 
 if __name__ == '__main__':
     main()
\ No newline at end of file