changeset 145:647b22c7aca0 draft

Uploaded
author luca_milaz
date Mon, 22 Jul 2024 09:46:21 +0000
parents 01bcab2f69a0
children 2b0d34f21596
files marea_2/ras_to_bounds.py
diffstat 1 files changed, 16 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/marea_2/ras_to_bounds.py	Mon Jul 22 09:37:47 2024 +0000
+++ b/marea_2/ras_to_bounds.py	Mon Jul 22 09:46:21 2024 +0000
@@ -7,6 +7,7 @@
 import cobra
 import sys
 import csv
+from joblib import Parallel, delayed, cpu_count
 
 ################################# process args ###############################
 def process_args(args :List[str]) -> argparse.Namespace:
@@ -100,61 +101,22 @@
         sys.exit('Execution aborted: wrong format of ' + name + '\n')
     return dataset
 
-'''def generate_bounds(model:cobra.Model, medium:dict, ras=None) -> pd.DataFrame:
-    rxns_ids = []
-    for rxn in model.reactions:
-        rxns_ids.append(rxn.id)
-    for reaction in medium.keys():
-        if(medium[reaction] != None):
-            model.reactions.get_by_id(reaction).lower_bound=-float(medium[reaction])
-    df_FVA = cobra.flux_analysis.flux_variability_analysis(model,fraction_of_optimum=0,processes=1).round(8)
-    for reaction in rxns_ids:
-        model.reactions.get_by_id(reaction).lower_bound=float(df_FVA.loc[reaction,"minimum"])
-        model.reactions.get_by_id(reaction).upper_bound=float(df_FVA.loc[reaction,"maximum"])
 
-    if(ras is not  None):
-        for cellName, ras in ras.iterrows():
-            model_new = model.copy()
-            for reaction in rxns_ids:
-                if (reaction in ras.keys() and ras[reaction] != None and pd.notna(ras[reaction])):
-                    lower_bound=model_new.reactions.get_by_id(reaction).lower_bound
-                    upper_bound=model_new.reactions.get_by_id(reaction).upper_bound
-                    valMax=float((upper_bound)*ras[reaction])
-                    valMin=float((lower_bound)*ras[reaction])
-                    if upper_bound!=0 and lower_bound==0:
-                        model_new.reactions.get_by_id(reaction).upper_bound=valMax
-                    if upper_bound==0 and lower_bound!=0:
-                        model_new.reactions.get_by_id(reaction).lower_bound=valMin
-                    if upper_bound!=0 and lower_bound!=0:
-                        model_new.reactions.get_by_id(reaction).lower_bound=valMin
-                        model_new.reactions.get_by_id(reaction).upper_bound=valMax
-            bounds = pd.DataFrame(columns = ["lower_bound", "upper_bound"], index=rxns_ids)
-            for reaction in model_new.reactions:
-                bounds.loc[reaction.id] = [reaction.lower_bound, reaction.upper_bound]
-            bounds.to_csv(ARGS.output_folder + cellName + ".csv", sep = '\t', index = False)
-    else:
-        model_new = model.copy()
-        for reaction in rxns_ids:
-            lower_bound=model_new.reactions.get_by_id(reaction).lower_bound
-            upper_bound=model_new.reactions.get_by_id(reaction).upper_bound
-            valMax = float((upper_bound)*1)
-            valMin=float((lower_bound)*1)
-            if upper_bound!=0 and lower_bound==0:
-                model_new.reactions.get_by_id(reaction).upper_bound=valMax
-            if upper_bound==0 and lower_bound!=0:
-                model_new.reactions.get_by_id(reaction).lower_bound=valMin
-            if upper_bound!=0 and lower_bound!=0:
-                model_new.reactions.get_by_id(reaction).lower_bound=valMin
-                model_new.reactions.get_by_id(reaction).upper_bound=valMax
-        bounds = pd.DataFrame(columns = ["lower_bound", "upper_bound"], index=rxns_ids)
-        for reaction in model_new.reactions:
-            bounds.loc[reaction.id] = [reaction.lower_bound, reaction.upper_bound]
-        bounds.to_csv(ARGS.output_folder + "bounds.csv", sep = '\t', index = False)
+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
 
-    
-    pass'''
+def process_ras_cell(cellName, ras_row, model, rxns_ids, output_folder):
+    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(output_folder + cellName + ".csv", sep='\t', index=False)
 
-def generate_bounds(model: cobra.Model, medium: dict, ras=None) -> pd.DataFrame:
+def generate_bounds(model: cobra.Model, medium: dict, ras=None, output_folder='output/') -> pd.DataFrame:
     rxns_ids = [rxn.id for rxn in model.reactions]
     
     # Set medium conditions
@@ -171,26 +133,13 @@
         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)
+        Parallel(n_jobs=cpu_count())(delayed(process_ras_cell)(cellName, ras_row, model, rxns_ids, output_folder) for cellName, ras_row in ras.iterrows())
     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
+        bounds.to_csv(output_folder + "bounds.csv", sep='\t', index=False)
 
 
 ############################# main ###########################################