Mercurial > repos > bimib > cobraxy
comparison COBRAxy/utils/general_utils.py @ 380:03a7ba63813f draft
Uploaded
author | luca_milaz |
---|---|
date | Fri, 05 Sep 2025 08:27:04 +0000 |
parents | fe87d6fd7884 |
children | 0a3ca20848f3 |
comparison
equal
deleted
inserted
replaced
379:6f0aed06098c | 380:03a7ba63813f |
---|---|
37 PDF = ("pdf",) # this is also a common output format for images, as it's required in publications. | 37 PDF = ("pdf",) # this is also a common output format for images, as it's required in publications. |
38 | 38 |
39 # Updated to include compressed variants | 39 # Updated to include compressed variants |
40 XML = ("xml", "xml.gz", "xml.zip", "xml.bz2") # SBML files are XML files, sometimes compressed | 40 XML = ("xml", "xml.gz", "xml.zip", "xml.bz2") # SBML files are XML files, sometimes compressed |
41 JSON = ("json", "json.gz", "json.zip", "json.bz2") # COBRA models can be stored as JSON files, sometimes compressed | 41 JSON = ("json", "json.gz", "json.zip", "json.bz2") # COBRA models can be stored as JSON files, sometimes compressed |
42 | 42 MAT = ("mat", "mat.gz", "mat.zip", "mat.bz2") # COBRA models can be stored as MAT files, sometimes compressed |
43 TXT = ("txt",) # this is how most output data is written | 43 YML = ("yml", "yml.gz", "yml.zip", "yml.bz2") # COBRA models can be stored as YML files, sometimes compressed |
44 | |
45 TXT = ("txt",) # this is how most output data is written | |
44 PICKLE = ("pickle", "pk", "p") # this is how all runtime data structures are saved | 46 PICKLE = ("pickle", "pk", "p") # this is how all runtime data structures are saved |
45 | 47 |
46 def __init__(self, *extensions): | 48 def __init__(self, *extensions): |
47 self.extensions = extensions | 49 self.extensions = extensions |
48 # Store original extension when set via fromExt | 50 # Store original extension when set via fromExt |
76 """ | 78 """ |
77 (Private) converts to str representation. Good practice for usage with argparse. | 79 (Private) converts to str representation. Good practice for usage with argparse. |
78 Returns: | 80 Returns: |
79 str : the string representation of the file extension. | 81 str : the string representation of the file extension. |
80 """ | 82 """ |
81 # If we have an original extension stored (for compressed files), use it | 83 # If we have an original extension stored (for compressed files only), use it |
82 if hasattr(self, '_original_extension') and self._original_extension: | 84 if hasattr(self, '_original_extension') and self._original_extension: |
83 return self._original_extension | 85 return self._original_extension |
84 | 86 |
85 # For XML and JSON without original extension, use the base extension | 87 # For XML, JSON, MAT and YML without original extension, use the base extension |
86 if self == FileFormat.XML: | 88 if self == FileFormat.XML: |
87 return "xml" | 89 return "xml" |
88 elif self == FileFormat.JSON: | 90 elif self == FileFormat.JSON: |
89 return "json" | 91 return "json" |
92 elif self == FileFormat.MAT: | |
93 return "mat" | |
94 elif self == FileFormat.YML: | |
95 return "yml" | |
90 | 96 |
91 return self.value[-1] | 97 return self.value[-1] |
92 | 98 |
93 class FilePath(): | 99 class FilePath(): |
94 """ | 100 """ |
612 if str(ext) in FileFormat.JSON.value: | 618 if str(ext) in FileFormat.JSON.value: |
613 # Compressed files are not automatically handled by cobra | 619 # Compressed files are not automatically handled by cobra |
614 if(ext == "json"): | 620 if(ext == "json"): |
615 return cobra.io.load_json_model(file_path.show()) | 621 return cobra.io.load_json_model(file_path.show()) |
616 else: | 622 else: |
617 return self.extract_json_model(file_path, ext) | 623 return self.extract_model(file_path, ext, "json") |
624 | |
625 if str(ext) in FileFormat.MAT.value: | |
626 # Compressed files are not automatically handled by cobra | |
627 if(ext == "mat"): | |
628 return cobra.io.load_matlab_model(file_path.show()) | |
629 else: | |
630 return self.extract_model(file_path, ext, "mat") | |
631 | |
632 if str(ext) in FileFormat.YML.value: | |
633 # Compressed files are not automatically handled by cobra | |
634 if(ext == "yml"): | |
635 return cobra.io.load_yaml_model(file_path.show()) | |
636 else: | |
637 return self.extract_model(file_path, ext, "yml") | |
618 | 638 |
619 except Exception as e: raise DataErr(file_path, e.__str__()) | 639 except Exception as e: raise DataErr(file_path, e.__str__()) |
620 raise DataErr(file_path, | 640 raise DataErr(file_path, |
621 f"Fomat \"{file_path.ext}\" is not recognized, only JSON and XML files are supported.") | 641 f"Fomat \"{file_path.ext}\" is not recognized, only JSON, XML, MAT and YAML (.yml) files are supported.") |
622 | 642 |
623 | 643 |
624 def extract_json_model(file_path:FilePath, ext :FileFormat) -> cobra.Model: | 644 def extract_model(file_path:FilePath, ext :FileFormat, model_encoding:Literal["json", "mat", "yml"]) -> cobra.Model: |
625 """ | 645 """ |
626 Extract json COBRA model from a compressed file (zip, gz, bz2). | 646 Extract JSON, MAT and YAML COBRA model from a compressed file (zip, gz, bz2). |
627 | 647 |
628 Args: | 648 Args: |
629 file_path: File path of the model | 649 file_path: File path of the model |
630 ext: File extensions of class FileFormat (should be .zip, .gz or .bz2) | 650 ext: File extensions of class FileFormat (should be .zip, .gz or .bz2) |
631 | 651 |
640 try: | 660 try: |
641 if '.zip' in ext_str: | 661 if '.zip' in ext_str: |
642 with zipfile.ZipFile(file_path.show(), 'r') as zip_ref: | 662 with zipfile.ZipFile(file_path.show(), 'r') as zip_ref: |
643 with zip_ref.open(zip_ref.namelist()[0]) as json_file: | 663 with zip_ref.open(zip_ref.namelist()[0]) as json_file: |
644 content = json_file.read().decode('utf-8') | 664 content = json_file.read().decode('utf-8') |
645 return cobra.io.load_json_model(StringIO(content)) | 665 if model_encoding == "json": |
666 return cobra.io.load_json_model(StringIO(content)) | |
667 elif model_encoding == "mat": | |
668 return cobra.io.load_matlab_model(StringIO(content)) | |
669 elif model_encoding == "yml": | |
670 return cobra.io.load_yaml_model(StringIO(content)) | |
671 else: | |
672 raise ValueError(f"Unsupported model encoding: {model_encoding}. Supported: json, mat, yml") | |
646 elif '.gz' in ext_str: | 673 elif '.gz' in ext_str: |
647 with gzip.open(file_path.show(), 'rt', encoding='utf-8') as gz_ref: | 674 with gzip.open(file_path.show(), 'rt', encoding='utf-8') as gz_ref: |
648 return cobra.io.load_json_model(gz_ref) | 675 if model_encoding == "json": |
676 return cobra.io.load_json_model(gz_ref) | |
677 elif model_encoding == "mat": | |
678 return cobra.io.load_matlab_model(gz_ref) | |
679 elif model_encoding == "yml": | |
680 return cobra.io.load_yaml_model(gz_ref) | |
681 else: | |
682 raise ValueError(f"Unsupported model encoding: {model_encoding}. Supported: json, mat, yml") | |
649 elif '.bz2' in ext_str: | 683 elif '.bz2' in ext_str: |
650 with bz2.open(file_path.show(), 'rt', encoding='utf-8') as bz2_ref: | 684 with bz2.open(file_path.show(), 'rt', encoding='utf-8') as bz2_ref: |
651 return cobra.io.load_json_model(bz2_ref) | 685 if model_encoding == "json": |
686 return cobra.io.load_json_model(bz2_ref) | |
687 elif model_encoding == "mat": | |
688 return cobra.io.load_matlab_model(bz2_ref) | |
689 elif model_encoding == "yml": | |
690 return cobra.io.load_yaml_model(bz2_ref) | |
691 else: | |
692 raise ValueError(f"Unsupported model encoding: {model_encoding}. Supported: json, mat, yml") | |
652 else: | 693 else: |
653 raise ValueError(f"Compression format not supported: {ext_str}. Supported: .zip, .gz and .bz2") | 694 raise ValueError(f"Compression format not supported: {ext_str}. Supported: .zip, .gz and .bz2") |
654 | 695 |
655 except Exception as e: | 696 except Exception as e: |
656 raise Exception(f"Error during model extraction: {str(e)}") | 697 raise Exception(f"Error during model extraction: {str(e)}") |