changeset 124:2590e2f60307 draft

Uploaded
author luca_milaz
date Sun, 21 Jul 2024 19:48:27 +0000
parents ee770fb7beef
children 4f889150f278
files marea_2/utils/general_utils.py
diffstat 1 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/marea_2/utils/general_utils.py	Sun Jul 21 19:40:32 2024 +0000
+++ b/marea_2/utils/general_utils.py	Sun Jul 21 19:48:27 2024 +0000
@@ -10,6 +10,7 @@
 from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar, Union
 
 import pandas as pd
+import cobra
 
 # FILES
 class FileFormat(Enum):
@@ -547,5 +548,24 @@
         path = customPath if self is Model.Custom else FilePath(f"{self.name}_map", FileFormat.SVG, prefix = f"{toolDir}/local/svg metabolic maps/")
         self.__raiseMissingPathErr(path)
         return readSvg(path, customErr = DataErr(path, f"custom map in wrong format"))
+    
+    def getCOBRAmodel(self, toolDir = ".", customPath :Optional[FilePath] = None, customExtension :Optional[FilePath]=None)->cobra.Model:
+        if(self is Model.Custom):
+            return self.load_custom_model(customPath, customExtension)
+        else:
+            return cobra.io.read_sbml_model(FilePath(f"{self.name}", FileFormat.XML, prefix = f"{toolDir}/local/models/").show())
+        
+    def load_custom_model(self, file_path :FilePath, ext :Optional[FileFormat] = None) -> cobra.Model:
+        ext = ext if ext else file_path.ext
+        try:
+            if ext is FileFormat.XML:
+                return cobra.io.read_sbml_model(file_path.show())
+            
+            if ext is FileFormat.JSON:
+                return cobra.io.load_json_model(file_path.show())
+
+        except Exception as e: raise DataErr(file_path, e.__str__())
+        raise DataErr(file_path,
+            f"Fomat \"{file_path.ext}\" is not recognized, only JSON and XML files are supported.")
 
     def __str__(self) -> str: return self.value
\ No newline at end of file