# HG changeset patch # User luca_milaz # Date 1720445631 0 # Node ID e9757480e74aa4ccdcd853526ad07f90db5ff6a4 # Parent 5939073bafa635ee7ea4d46a625db86d43ee18b9 Uploaded diff -r 5939073bafa6 -r e9757480e74a marea_2_0/model_generator.py --- 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