diff COBRAxy/ras_to_bounds.py @ 48:fac6930e6385 draft

Uploaded
author luca_milaz
date Sat, 12 Oct 2024 19:57:09 +0000
parents 41f35c2f0c7b
children 47153fe3d59e
line wrap: on
line diff
--- a/COBRAxy/ras_to_bounds.py	Tue Oct 08 17:08:02 2024 +0000
+++ b/COBRAxy/ras_to_bounds.py	Sat Oct 12 19:57:09 2024 +0000
@@ -59,6 +59,14 @@
                         type=utils.Bool("using_RAS"),
                         help = 'ras selector')
     
+    parser.add_argument('-c', '--classes',
+                    type = str,
+                    help = 'input classes')
+
+    parser.add_argument('-cc', '--cell_class',
+                    type = str,
+                    help = 'output of cell class')
+    
     ARGS = parser.parse_args()
     return ARGS
 
@@ -115,11 +123,20 @@
         None
     """
     for reaction in rxns_ids:
-        if reaction in ras_row.index and pd.notna(ras_row[reaction]):
-            rxn = model.reactions.get_by_id(reaction)
+        if reaction in ras_row.index:
             scaling_factor = ras_row[reaction]
-            rxn.lower_bound *= scaling_factor
-            rxn.upper_bound *= scaling_factor
+            lower_bound=model.reactions.get_by_id(reaction).lower_bound
+            upper_bound=model.reactions.get_by_id(reaction).upper_bound
+            valMax=float((upper_bound)*scaling_factor)
+            valMin=float((lower_bound)*scaling_factor)
+            if upper_bound!=0 and lower_bound==0:
+                model.reactions.get_by_id(reaction).upper_bound=valMax
+            if upper_bound==0 and lower_bound!=0:
+                model.reactions.get_by_id(reaction).lower_bound=valMin
+            if upper_bound!=0 and lower_bound!=0:
+                model.reactions.get_by_id(reaction).lower_bound=valMin
+                model.reactions.get_by_id(reaction).upper_bound=valMax
+    pass
 
 def process_ras_cell(cellName, ras_row, model, rxns_ids, output_folder):
     """
@@ -139,6 +156,7 @@
     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=True)
+    pass
 
 def generate_bounds(model: cobra.Model, medium: dict, ras=None, output_folder='output/') -> pd.DataFrame:
     """
@@ -147,7 +165,7 @@
     Args:
         model (cobra.Model): The metabolic model for which bounds will be generated.
         medium (dict): A dictionary where keys are reaction IDs and values are the medium conditions.
-        ras (pd.DataFrame, optional): A DataFrame with RAS scaling factors for different cell types. Defaults to None.
+        ras (pd.DataFrame, optional): RAS pandas dataframe. Defaults to None.
         output_folder (str, optional): Folder path where output CSV files will be saved. Defaults to 'output/'.
 
     Returns:
@@ -176,6 +194,8 @@
         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(output_folder + "bounds.csv", sep='\t', index=True)
+    pass
+
 
 
 ############################# main ###########################################
@@ -196,11 +216,30 @@
     ARGS.output_folder = 'ras_to_bounds/'
 
     if(ARGS.ras_selector == True):
-        ras = read_dataset(ARGS.input_ras, "ras dataset")
-        ras.replace("None", None, inplace=True)
-        ras.set_index("Reactions", drop=True, inplace=True)
-        ras = ras.T
-        ras = ras.astype(float)
+        ras_file_list = ARGS.ras_selector.split(",")
+        if(len(ras_list)>1):
+            ras_class_names = [cls.strip() for cls in ARGS.classes.split(',')]
+        else:
+            ras_class_names = ["placeHolder"]
+        ras_list = []
+        class_assignments = pd.DataFrame(columns=["Patient_ID", "Class"])
+        for ras_matrix, ras_class_name in ras_file_list, ras_class_names:
+            ras = read_dataset(ras_matrix, "ras dataset")
+            ras.replace("None", None, inplace=True)
+            ras.set_index("Reactions", drop=True, inplace=True)
+            ras = ras.T
+            ras = ras.astype(float)
+            ras_list.append(ras)
+            for patient_id in ras.index:
+                class_assignments = class_assignments.append({"Patient_ID": patient_id, "Class": ras_class_name}, ignore_index=True)
+        
+        # Concatenate all ras DataFrames into a single DataFrame
+        ras_combined = pd.concat(ras_list, axis=1)
+        # Normalize the RAS values by max RAS
+        ras_combined = ras_combined.div(ras_combined.max(axis=0))
+        ras_combined = ras_combined.fillna(0)
+
+
     
     model_type :utils.Model = ARGS.model_selector
     if model_type is utils.Model.Custom:
@@ -220,7 +259,9 @@
         medium = medium[ARGS.medium_selector].to_dict()
 
     if(ARGS.ras_selector == True):
-        generate_bounds(model, medium, ras = ras, output_folder=ARGS.output_folder)
+        generate_bounds(model, medium, ras = ras_combined, output_folder=ARGS.output_folder)
+        if(len(ras_list)>1):
+            class_assignments.to_csv(ARGS.cell_class, sep = '\t', index = False)
     else:
         generate_bounds(model, medium, output_folder=ARGS.output_folder)