changeset 430:f49c951c9fe6 draft

Uploaded
author francesco_lapi
date Wed, 10 Sep 2025 14:53:36 +0000
parents 0485c4b1943d
children 5d9c220b3238
files COBRAxy/flux_simulation_beta.py COBRAxy/flux_simulation_beta.xml COBRAxy/utils/model_utils.py
diffstat 3 files changed, 52 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/COBRAxy/flux_simulation_beta.py	Wed Sep 10 13:26:03 2025 +0000
+++ b/COBRAxy/flux_simulation_beta.py	Wed Sep 10 14:53:36 2025 +0000
@@ -80,6 +80,12 @@
                         required = True,
                         help = 'choose how many batches')
     
+    parser.add_argument('-opt', '--perc_opt',
+                        type = float,
+                        default=0.9,
+                        required = False,
+                        help = 'choose the fraction of optimality for FVA (0-1)')
+    
     parser.add_argument('-ot', '--output_type', 
                         type = str,
                         required = True,
@@ -359,7 +365,7 @@
             df_pFBA.index = [model_name]
             df_pFBA = df_pFBA.astype(float).round(6)
         elif(output_type == "FVA"):
-            fva = cobra.flux_analysis.flux_variability_analysis(model, fraction_of_optimum=0, processes=1).round(8)
+            fva = cobra.flux_analysis.flux_variability_analysis(model, fraction_of_optimum=ARGS.perc_opt, processes=1).round(8)
             columns = []
             for rxn in fva.index.to_list():
                 columns.append(rxn + "_min")
--- a/COBRAxy/flux_simulation_beta.xml	Wed Sep 10 13:26:03 2025 +0000
+++ b/COBRAxy/flux_simulation_beta.xml	Wed Sep 10 14:53:36 2025 +0000
@@ -44,8 +44,11 @@
         --n_batches $n_batches
         --n_samples $n_samples
         --seed $seed
+        #if $output_types_analysis_cond.fva_fraction == 'FVA'
+            --perc_opt $output_types_analysis_cond.fva_fraction
+        #end if
         --output_type "${",".join(map(str, $output_types))}"
-        --output_type_analysis "${",".join(map(str, $output_types_analysis))}"
+        --output_type_analysis "${",".join(map(str, $output_types_analysis_cond.output_types_analysis))}"
         --out_log $log
         ]]>
     </command>
@@ -95,11 +98,22 @@
             <option value="fluxes" selected="false">All fluxes</option>
         </param>
 
-        <param type="select" argument="--output_types_analysis" multiple="true" name="output_types_analysis" label="Desired outputs from flux analysis">
-            <option value="pFBA" selected="false">pFBA</option>
-            <option value="FVA" selected="false">FVA</option>
-            <option value="sensitivity" selected="false">Sensitivity reaction knock-out (Biomass)</option>
-        </param>
+        <conditional name="output_types_analysis_cond">
+            <param type="select" argument="--output_types_analysis" multiple="true" name="output_types_analysis" label="Desired outputs from flux analysis">
+                <option value="pFBA" selected="false">pFBA</option>
+                <option value="FVA" selected="false">FVA</option>
+                <option value="sensitivity" selected="false">Sensitivity reaction knock-out (Biomass)</option>
+            </param>
+
+            <when value="FVA">
+                <param name="fva_fraction" argument="--fva_fraction" type="float"
+                        label="FVA fraction of optimality (0-1):"
+                        value="0.9"
+                        min="0.0" max="1.0"
+                        help="Fraction of the optimal objective value to consider for FVA. 0.9 = 90%"/>
+            </when>
+
+        </conditional>
     </inputs>
 
     <outputs>
--- a/COBRAxy/utils/model_utils.py	Wed Sep 10 13:26:03 2025 +0000
+++ b/COBRAxy/utils/model_utils.py	Wed Sep 10 14:53:36 2025 +0000
@@ -254,9 +254,9 @@
     
     print(f"Aggiunte {reactions_added} reazioni, saltate {reactions_skipped} reazioni")
     
-    # Imposta l'obiettivo di biomassa
-    set_biomass_objective(model)
-    
+    # set objective function
+    set_objective_from_csv(model, df, obj_col="ObjectiveCoefficient")
+
     # Imposta il medium
     set_medium_from_data(model, df)
     
@@ -361,17 +361,31 @@
 
 
 
-def set_biomass_objective(model: cobraModel):
+def set_objective_from_csv(model: cobra.Model, df: pd.DataFrame, obj_col: str = "ObjectiveCoefficient"):
     """
-    Imposta la reazione di biomassa come obiettivo.
+    Sets the model's objective function based on a column of coefficients in the CSV.
+    Can be any reaction(s), not necessarily biomass.
     """
-    biomass_reactions = [r for r in model.reactions if 'biomass' in r.id.lower()]
+    obj_dict = {}
     
-    if biomass_reactions:
-        model.objective = biomass_reactions[0].id
-        print(f"Obiettivo impostato su: {biomass_reactions[0].id}")
-    else:
-        print("Nessuna reazione di biomassa trovata")
+    for idx, row in df.iterrows():
+        reaction_id = str(row['ReactionID']).strip()
+        coeff = float(row[obj_col]) if pd.notna(row[obj_col]) else 0.0
+        if coeff != 0:
+            # Assicuriamoci che la reazione esista nel modello
+            if reaction_id in model.reactions:
+                obj_dict[model.reactions.get_by_id(reaction_id)] = coeff
+            else:
+                print(f"Warning: reaction {reaction_id} not found in model, skipping for objective.")
+
+    if not obj_dict:
+        raise ValueError("No reactions found with non-zero objective coefficient.")
+
+    # Imposta la funzione obiettivo
+    model.objective = obj_dict
+    print(f"Objective set with {len(obj_dict)} reactions.")
+
+
 
 
 def set_medium_from_data(model: cobraModel, df: pd.DataFrame):