Mercurial > repos > bimib > cobraxy
comparison COBRAxy/metabolicModel2Tabular.py @ 498:df90f40a156c draft
Uploaded
| author | francesco_lapi |
|---|---|
| date | Tue, 30 Sep 2025 16:13:08 +0000 |
| parents | 36838126cc07 |
| children | 054c872e3880 |
comparison
equal
deleted
inserted
replaced
| 497:36838126cc07 | 498:df90f40a156c |
|---|---|
| 34 help="Output log file") | 34 help="Output log file") |
| 35 | 35 |
| 36 parser.add_argument("--model", type=str, | 36 parser.add_argument("--model", type=str, |
| 37 help="Built-in model identifier (e.g., ENGRO2, Recon, HMRcore)") | 37 help="Built-in model identifier (e.g., ENGRO2, Recon, HMRcore)") |
| 38 parser.add_argument("--input", type=str, | 38 parser.add_argument("--input", type=str, |
| 39 help="Custom model file (JSON or XML)") | 39 help="Custom model file (JSON, XML, MAT, YAML)") |
| 40 parser.add_argument("--name", nargs='*', required=True, | 40 parser.add_argument("--name", nargs='*', required=True, |
| 41 help="Model name (default or custom)") | 41 help="Model name (default or custom)") |
| 42 | 42 |
| 43 parser.add_argument("--medium_selector", type=str, required=True, | 43 parser.add_argument("--medium_selector", type=str, required=True, |
| 44 help="Medium selection option") | 44 help="Medium selection option") |
| 54 | 54 |
| 55 | 55 |
| 56 return parser.parse_args(args) | 56 return parser.parse_args(args) |
| 57 | 57 |
| 58 ################################- INPUT DATA LOADING -################################ | 58 ################################- INPUT DATA LOADING -################################ |
| 59 def detect_file_format(file_path: str) -> utils.FileFormat: | |
| 60 """ | |
| 61 Detect file format by examining file content and extension. | |
| 62 Handles Galaxy .dat files by looking at content. | |
| 63 """ | |
| 64 try: | |
| 65 with open(file_path, 'r') as f: | |
| 66 first_lines = ''.join([f.readline() for _ in range(5)]) | |
| 67 | |
| 68 # Check for XML (SBML) | |
| 69 if '<?xml' in first_lines or '<sbml' in first_lines: | |
| 70 return utils.FileFormat.XML | |
| 71 | |
| 72 # Check for JSON | |
| 73 if first_lines.strip().startswith('{'): | |
| 74 return utils.FileFormat.JSON | |
| 75 | |
| 76 # Check for YAML | |
| 77 if any(line.strip().endswith(':') for line in first_lines.split('\n')[:3]): | |
| 78 return utils.FileFormat.YML | |
| 79 | |
| 80 except: | |
| 81 pass | |
| 82 | |
| 83 # Fall back to extension-based detection | |
| 84 if file_path.endswith('.xml') or file_path.endswith('.sbml'): | |
| 85 return utils.FileFormat.XML | |
| 86 elif file_path.endswith('.json'): | |
| 87 return utils.FileFormat.JSON | |
| 88 elif file_path.endswith('.mat'): | |
| 89 return utils.FileFormat.MAT | |
| 90 elif file_path.endswith('.yml') or file_path.endswith('.yaml'): | |
| 91 return utils.FileFormat.YML | |
| 92 | |
| 93 # Default to XML for unknown extensions | |
| 94 return utils.FileFormat.XML | |
| 95 | |
| 59 def load_custom_model(file_path :utils.FilePath, ext :Optional[utils.FileFormat] = None) -> cobra.Model: | 96 def load_custom_model(file_path :utils.FilePath, ext :Optional[utils.FileFormat] = None) -> cobra.Model: |
| 60 """ | 97 """ |
| 61 Loads a custom model from a file, either in JSON, XML, MAT, or YML format. | 98 Loads a custom model from a file, either in JSON, XML, MAT, or YML format. |
| 62 | 99 |
| 63 Args: | 100 Args: |
| 184 # Convert name from list to string (handles names with spaces) | 221 # Convert name from list to string (handles names with spaces) |
| 185 if isinstance(ARGS.name, list): | 222 if isinstance(ARGS.name, list): |
| 186 ARGS.name = ' '.join(ARGS.name) | 223 ARGS.name = ' '.join(ARGS.name) |
| 187 | 224 |
| 188 if ARGS.input: | 225 if ARGS.input: |
| 189 # Load a custom model from file | 226 # Load a custom model from file with auto-detected format |
| 190 model = load_custom_model( | 227 detected_format = detect_file_format(ARGS.input) |
| 191 utils.FilePath.fromStrPath(ARGS.input), utils.FilePath.fromStrPath(ARGS.input).ext) | 228 model = load_custom_model(utils.FilePath.fromStrPath(ARGS.input), detected_format) |
| 192 else: | 229 else: |
| 193 # Load a built-in model | 230 # Load a built-in model |
| 194 if not ARGS.model: | 231 if not ARGS.model: |
| 195 raise utils.ArgsErr("model", "either --model or --input must be provided", "None") | 232 raise utils.ArgsErr("model", "either --model or --input must be provided", "None") |
| 196 | 233 |
