Mercurial > repos > bimib > cobraxy
changeset 498:df90f40a156c draft
Uploaded
author | francesco_lapi |
---|---|
date | Tue, 30 Sep 2025 16:13:08 +0000 |
parents | 36838126cc07 |
children | a2f7a6dd9d0b |
files | COBRAxy/flux_to_map.py COBRAxy/marea.py COBRAxy/metabolicModel2Tabular.py COBRAxy/metabolicModel2Tabular.xml COBRAxy/tabular2MetabolicModel.py COBRAxy/tabular2MetabolicModel.xml |
diffstat | 6 files changed, 57 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/COBRAxy/flux_to_map.py Tue Sep 30 15:32:27 2025 +0000 +++ b/COBRAxy/flux_to_map.py Tue Sep 30 16:13:08 2025 +0000 @@ -400,7 +400,7 @@ continue if isinstance(foldChange, str): foldChange = float(foldChange) - if pValue >= ARGS.pValue: # pValue above tresh: dashed arrow + if pValue > ARGS.pValue: # pValue above tresh: dashed arrow INSIGNIFICANT_ARROW.styleReactionElements(metabMap, reactionId) INSIGNIFICANT_ARROW.styleReactionElements(metabMap, reactionId, mindReactionDir = False)
--- a/COBRAxy/marea.py Tue Sep 30 15:32:27 2025 +0000 +++ b/COBRAxy/marea.py Tue Sep 30 16:13:08 2025 +0000 @@ -491,7 +491,7 @@ if math.isnan(pValue) or (isinstance(foldChange, float) and math.isnan(foldChange)): continue if isinstance(foldChange, str): foldChange = float(foldChange) - if pValue >= ARGS.pValue: # pValue above tresh: dashed arrow + if pValue > ARGS.pValue: # pValue above tresh: dashed arrow INSIGNIFICANT_ARROW.styleReactionElements(metabMap, reactionId) continue
--- a/COBRAxy/metabolicModel2Tabular.py Tue Sep 30 15:32:27 2025 +0000 +++ b/COBRAxy/metabolicModel2Tabular.py Tue Sep 30 16:13:08 2025 +0000 @@ -36,7 +36,7 @@ parser.add_argument("--model", type=str, help="Built-in model identifier (e.g., ENGRO2, Recon, HMRcore)") parser.add_argument("--input", type=str, - help="Custom model file (JSON or XML)") + help="Custom model file (JSON, XML, MAT, YAML)") parser.add_argument("--name", nargs='*', required=True, help="Model name (default or custom)") @@ -56,6 +56,43 @@ return parser.parse_args(args) ################################- INPUT DATA LOADING -################################ +def detect_file_format(file_path: str) -> utils.FileFormat: + """ + Detect file format by examining file content and extension. + Handles Galaxy .dat files by looking at content. + """ + try: + with open(file_path, 'r') as f: + first_lines = ''.join([f.readline() for _ in range(5)]) + + # Check for XML (SBML) + if '<?xml' in first_lines or '<sbml' in first_lines: + return utils.FileFormat.XML + + # Check for JSON + if first_lines.strip().startswith('{'): + return utils.FileFormat.JSON + + # Check for YAML + if any(line.strip().endswith(':') for line in first_lines.split('\n')[:3]): + return utils.FileFormat.YML + + except: + pass + + # Fall back to extension-based detection + if file_path.endswith('.xml') or file_path.endswith('.sbml'): + return utils.FileFormat.XML + elif file_path.endswith('.json'): + return utils.FileFormat.JSON + elif file_path.endswith('.mat'): + return utils.FileFormat.MAT + elif file_path.endswith('.yml') or file_path.endswith('.yaml'): + return utils.FileFormat.YML + + # Default to XML for unknown extensions + return utils.FileFormat.XML + 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, XML, MAT, or YML format. @@ -186,9 +223,9 @@ ARGS.name = ' '.join(ARGS.name) if ARGS.input: - # Load a custom model from file - model = load_custom_model( - utils.FilePath.fromStrPath(ARGS.input), utils.FilePath.fromStrPath(ARGS.input).ext) + # Load a custom model from file with auto-detected format + detected_format = detect_file_format(ARGS.input) + model = load_custom_model(utils.FilePath.fromStrPath(ARGS.input), detected_format) else: # Load a built-in model if not ARGS.model:
--- a/COBRAxy/metabolicModel2Tabular.xml Tue Sep 30 15:32:27 2025 +0000 +++ b/COBRAxy/metabolicModel2Tabular.xml Tue Sep 30 16:13:08 2025 +0000 @@ -70,7 +70,7 @@ <!-- Custom model --> <when value="Custom_model"> - <param name="input" argument="--input" type="data" format="json,xml" label="Custom model file:" /> + <param name="input" argument="--input" type="data" format="json,xml,sbml" label="Custom model file:" /> <conditional name="cond_medium"> <param name="medium_selector" argument="--medium_selector" type="select" label="Medium"> <option value="Default" selected="true">Don't use a separate medium file (use model defaults)</option>
--- a/COBRAxy/tabular2MetabolicModel.py Tue Sep 30 15:32:27 2025 +0000 +++ b/COBRAxy/tabular2MetabolicModel.py Tue Sep 30 16:13:08 2025 +0000 @@ -41,7 +41,6 @@ parser.add_argument("--output", type=str, required=True, help="Output model file path") - parser.add_argument("--tool_dir", type=str, default=os.path.dirname(__file__), help="Tool directory (passed from Galaxy as $__tool_directory__)") @@ -86,8 +85,11 @@ logging.exception('Cannot create output directory: %s', out_dir) model = modelUtils.build_cobra_model_from_csv(ARGS.input) + + + logging.info('Created model with name: %s (ID: %s)', model.name, model.id) - # Save model in requested format + # Save model in requested format - Galaxy handles the filename if ARGS.format == "sbml": cobra.io.write_sbml_model(model, ARGS.output) elif ARGS.format == "json": @@ -98,14 +100,17 @@ cobra.io.save_yaml_model(model, ARGS.output) else: logging.error('Unknown format requested: %s', ARGS.format) - print(f"ERROR: Unknown format: {ARGS.format}") + raise ValueError(f"Unknown format: {ARGS.format}") logging.info('Model successfully written to %s (format=%s)', ARGS.output, ARGS.format) + print(f"Model created successfully in {ARGS.format.upper()} format") - except Exception: + except Exception as e: # Log full traceback to the out_log so Galaxy users/admins can see what happened logging.exception('Unhandled exception in fromCSVtoCOBRA') + print(f"ERROR: {str(e)}") + raise if __name__ == '__main__':
--- a/COBRAxy/tabular2MetabolicModel.xml Tue Sep 30 15:32:27 2025 +0000 +++ b/COBRAxy/tabular2MetabolicModel.xml Tue Sep 30 16:13:08 2025 +0000 @@ -19,7 +19,7 @@ python $__tool_directory__/tabular2MetabolicModel.py --tool_dir $__tool_directory__ --input $input - --format $format + --format $format --output $output --out_log $log ]]></command> @@ -27,6 +27,7 @@ <!-- Tool inputs --> <inputs> <param name="input" type="data" format="tabular,csv,tsv" label="Input table"/> + <param name="model_name" type="text" value="Converted_Model" label="Model name" help="Name for the created COBRA model"/> <param name="format" type="select" label="Output COBRA model format"> <option value="sbml" selected="true">SBML (.xml)</option> <option value="json">JSON (.json)</option> @@ -37,8 +38,8 @@ <!-- Tool outputs --> <outputs> - <data name="log" format="txt" label="fromcsvtocobra - Log" /> - <data name="output" format="xml" label="COBRA model"> + <data name="log" format="txt" label="Tabular to Model Conversion - Log" /> + <data name="output" format="xml" label="${model_name}.${format}"> <change_format> <when input="format" value="sbml" format="xml"/> <when input="format" value="json" format="json"/>