changeset 451:a4e8f3188813 draft

Uploaded
author francesco_lapi
date Thu, 11 Sep 2025 20:34:30 +0000
parents d9a9e909cd9e
children 40a8eeb5f614
files COBRAxy/fromCSVtoCOBRA_beta.py COBRAxy/fromCSVtoCOBRA_beta.xml
diffstat 2 files changed, 138 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/COBRAxy/fromCSVtoCOBRA_beta.py	Thu Sep 11 20:34:30 2025 +0000
@@ -0,0 +1,69 @@
+import os
+import csv
+import cobra
+import pickle
+import argparse
+import pandas as pd
+import utils.general_utils as utils
+from typing import Optional, Tuple, Union, List, Dict
+import logging
+import utils.rule_parsing  as rulesUtils
+import utils.reaction_parsing as reactionUtils
+import utils.model_utils as modelUtils
+
+ARGS : argparse.Namespace
+def process_args(args: List[str] = None) -> argparse.Namespace:
+    """
+    Parse command-line arguments for CustomDataGenerator.
+    """
+
+    parser = argparse.ArgumentParser(
+        usage="%(prog)s [options]",
+        description="Generate custom data from a given model"
+    )
+
+    parser.add_argument("--out_log", type=str, required=True,
+                        help="Output log file")
+
+    parser.add_argument("--input", type=str,
+                        help="Input tabular file")
+    
+    parser.add_argument("--format", type=str, required=True, choices=["sbml", "json", "mat", "yaml"],
+                        help="Model format (SBML, JSON, MATLAB, YAML)")
+
+    parser.add_argument("--output", type=str,
+                        help="Output model file")
+    
+    parser.add_argument("--tool_dir", type=str, default=os.path.dirname(__file__),
+                        help="Tool directory (passed from Galaxy as $__tool_directory__)")
+    
+
+
+    return parser.parse_args(args)
+
+###############################- ENTRY POINT -################################
+def main(args:List[str] = None) -> None:
+    """
+    Initializes everything and sets the program in motion based on the fronted input arguments.
+    
+    Returns:
+        None
+    """
+    # get args from frontend (related xml)
+    global ARGS
+    ARGS = process_args(args)
+
+    model = modelUtils.build_cobra_model_from_csv(ARGS.model_upload)
+
+
+    if ARGS.format == "sbml":
+        cobra.io.write_sbml_model(model, ARGS.output)
+    elif ARGS.format == "json":
+        cobra.io.save_json_model(model, ARGS.output)
+    elif ARGS.format == "mat":
+        cobra.io.save_matlab_model(model, ARGS.output)
+    elif ARGS.format == "yaml":
+        cobra.io.save_yaml_model(model, ARGS.output)
+
+if __name__ == '__main__':
+    main()
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/COBRAxy/fromCSVtoCOBRA_beta.xml	Thu Sep 11 20:34:30 2025 +0000
@@ -0,0 +1,69 @@
+<tool id="fromcsvtocobra" name="From CSV to COBRA model" version="1.0.0">
+    <description>Convert a tabular dataset to a COBRA model</description>
+
+    <!-- Python dependencies required for COBRApy -->
+    <requirements>
+        <requirement type="package" version="0.29.0">cobra</requirement>
+        <requirement type="package" version="1.24.4">numpy</requirement>
+        <requirement type="package" version="2.0.3">pandas</requirement>
+        <requirement type="package" version="5.2.2">lxml</requirement>
+    </requirements>
+
+    <!-- Import shared macros if available -->
+    <macros>
+        <import>marea_macros.xml</import>
+    </macros>
+
+    <!-- Command to run the Python script -->
+    <command detect_errors="exit_code"><![CDATA[
+        python $__tool_directory__/from_csv_to_cobra.py 
+            --tool_dir $__tool_directory__
+            --input $input 
+            --format $format 
+            --output $output
+            --out_log $log
+    ]]></command>
+
+    <!-- Tool inputs -->
+    <inputs>
+        <param name="input" type="data" format="tabular,csv,tsv" label="Input table"/>
+        <param name="format" type="select" label="Output COBRA model format">
+            <option value="sbml" selected="true">SBML (.xml)</option>
+            <option value="json">JSON (.json)</option>
+            <option value="mat">MATLAB (.mat)</option>
+            <option value="yaml">YAML (.yml)</option>
+        </param>
+    </inputs>
+
+    <!-- Tool outputs -->
+    <outputs>
+        <data name="log" format="txt" label="fromcsvtocobra - Log" />
+        <data name="output" format="xml" label="COBRA model">
+            <change_format>
+                <when input="format" value="sbml" format="xml"/>
+                <when input="format" value="json" format="json"/>
+                <when input="format" value="mat" format="mat"/>
+                <when input="format" value="yaml" format="yaml"/>
+            </change_format>
+        </data>
+    </outputs>
+
+    <!-- Help section -->
+    <help><![CDATA[
+This tool converts a tabular dataset into a COBRA model using COBRApy.
+
+**Input**
+- A tabular/CSV/TSV file describing reactions, metabolites, or stoichiometry.
+
+**Output**
+- A COBRA model in the chosen format:  
+  - SBML (.xml)  
+  - JSON (.json)  
+  - MATLAB (.mat)  
+  - YAML (.yml)  
+
+**Notes**
+- The exact table structure (columns required) depends on how you want to encode reactions and metabolites.  
+- You can extend the Python script to parse specific column formats.  
+    ]]></help>
+</tool>