changeset 244:e9757480e74a draft

Uploaded
author luca_milaz
date Mon, 08 Jul 2024 13:33:51 +0000
parents 5939073bafa6
children 59830a48b19d
files marea_2_0/model_generator.py
diffstat 1 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/marea_2_0/model_generator.py	Mon Jul 08 13:30:02 2024 +0000
+++ b/marea_2_0/model_generator.py	Mon Jul 08 13:33:51 2024 +0000
@@ -84,6 +84,35 @@
     pass
 
 
+
+################################- INPUT DATA LOADING -################################
+def load_custom_model(file_path :utils.FilePath, ext :Optional[utils.FileFormat] = None) -> cobra.Model:
+    """
+    Loads a custom model from a file, either in JSON or XML format.
+
+    Args:
+        file_path : The path to the file containing the custom model.
+        ext : explicit file extension. Necessary for standard use in galaxy because of its weird behaviour.
+
+    Raises:
+        DataErr : if the file is in an invalid format or cannot be opened for whatever reason.    
+    
+    Returns:
+        cobra.Model : the model, if successfully opened.
+    """
+    ext = ext if ext else file_path.ext
+    try:
+        if ext is utils.FileFormat.XML:
+            return cobra.io.read_sbml_model(file_path.show())
+        
+        if ext is utils.FileFormat.JSON:
+            return cobra.io.load_json_model(file_path.show())
+
+    except Exception as e: raise utils.DataErr(file_path, e.__str__())
+    raise utils.DataErr(file_path,
+        f"Fomat \"{file_path.ext}\" is not recognized, only JSON and XML files are supported.")
+
+
 ############################# main ###########################################
 def main() -> None:
     """
@@ -112,7 +141,9 @@
 
     model_type :utils.Model = ARGS.model_selector
     if model_type is utils.Model.Custom:
-        model = model_type.getCOBRAmodel(customPath = utils.FilePath.fromStrPath(ARGS.model_name), customExtension = utils.FilePath.fromStrPath(ARGS.model_name).ext)
+        #model = model_type.getCOBRAmodel(customPath = utils.FilePath.fromStrPath(ARGS.model_name), customExtension = utils.FilePath.fromStrPath(ARGS.model_name).ext)
+        model = load_custom_model(
+        utils.FilePath.fromStrPath(ARGS.model_name), utils.FilePath.fromStrPath(ARGS.model_name).ext)
     else:
         model = model_type.getCOBRAmodel()
     '''for index, row in ras.iterrows(): #iterate over cells RAS