changeset 144:01bcab2f69a0 draft

Uploaded
author luca_milaz
date Mon, 22 Jul 2024 09:37:47 +0000
parents 8996762c35cb
children 647b22c7aca0
files marea_2/ras_to_bounds.py
diffstat 1 files changed, 39 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/marea_2/ras_to_bounds.py	Mon Jul 22 09:05:28 2024 +0000
+++ b/marea_2/ras_to_bounds.py	Mon Jul 22 09:37:47 2024 +0000
@@ -100,7 +100,7 @@
         sys.exit('Execution aborted: wrong format of ' + name + '\n')
     return dataset
 
-def generate_bounds(model:cobra.Model, medium:dict, ras=None) -> pd.DataFrame:
+'''def generate_bounds(model:cobra.Model, medium:dict, ras=None) -> pd.DataFrame:
     rxns_ids = []
     for rxn in model.reactions:
         rxns_ids.append(rxn.id)
@@ -152,6 +152,44 @@
         bounds.to_csv(ARGS.output_folder + "bounds.csv", sep = '\t', index = False)
 
     
+    pass'''
+
+def generate_bounds(model: cobra.Model, medium: dict, ras=None) -> pd.DataFrame:
+    rxns_ids = [rxn.id for rxn in model.reactions]
+    
+    # Set medium conditions
+    for reaction, value in medium.items():
+        if value is not None:
+            model.reactions.get_by_id(reaction).lower_bound = -float(value)
+    
+    # Perform Flux Variability Analysis (FVA)
+    df_FVA = cobra.flux_analysis.flux_variability_analysis(model, fraction_of_optimum=0, processes=1).round(8)
+    
+    # Set FVA bounds
+    for reaction in rxns_ids:
+        rxn = model.reactions.get_by_id(reaction)
+        rxn.lower_bound = float(df_FVA.loc[reaction, "minimum"])
+        rxn.upper_bound = float(df_FVA.loc[reaction, "maximum"])
+
+    def apply_ras_bounds(model, ras_row, rxns_ids):
+        for reaction in rxns_ids:
+            if reaction in ras_row.index and pd.notna(ras_row[reaction]):
+                rxn = model.reactions.get_by_id(reaction)
+                scaling_factor = ras_row[reaction]
+                rxn.lower_bound *= scaling_factor
+                rxn.upper_bound *= scaling_factor
+
+    if ras is not None:
+        for cellName, ras_row in ras.iterrows():
+            model_new = model.copy()
+            apply_ras_bounds(model_new, ras_row, rxns_ids)
+            bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"])
+            bounds.to_csv(ARGS.output_folder + cellName + ".csv", sep='\t', index=False)
+    else:
+        model_new = model.copy()
+        apply_ras_bounds(model_new, pd.Series([1]*len(rxns_ids), index=rxns_ids), rxns_ids)
+        bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"])
+        bounds.to_csv(ARGS.output_folder + "bounds.csv", sep='\t', index=False)
     pass