comparison COBRAxy/ras_to_bounds.py @ 84:0446929eb06b draft

Uploaded
author luca_milaz
date Sun, 13 Oct 2024 11:06:47 +0000
parents be82f87848d0
children
comparison
equal deleted inserted replaced
83:bfb17674d13b 84:0446929eb06b
52 parser.add_argument('-ir', '--input_ras', 52 parser.add_argument('-ir', '--input_ras',
53 type=str, 53 type=str,
54 required = False, 54 required = False,
55 help = 'input ras') 55 help = 'input ras')
56 56
57 parser.add_argument('-rn', '--names',
58 type=str,
59 help = 'ras class names')
60
61 parser.add_argument('-rs', '--ras_selector', 57 parser.add_argument('-rs', '--ras_selector',
62 required = True, 58 required = True,
63 type=utils.Bool("using_RAS"), 59 type=utils.Bool("using_RAS"),
64 help = 'ras selector') 60 help = 'ras selector')
61
62 parser.add_argument('-c', '--classes',
63 type = str,
64 required = False,
65 help = 'input classes')
65 66
66 parser.add_argument('-cc', '--cell_class', 67 parser.add_argument('-cc', '--cell_class',
67 type = str, 68 type = str,
68 help = 'output of cell class') 69 help = 'output of cell class')
69
70 70
71 ARGS = parser.parse_args() 71 ARGS = parser.parse_args()
72 return ARGS 72 return ARGS
73 73
74 ########################### warning ########################################### 74 ########################### warning ###########################################
126 for reaction in rxns_ids: 126 for reaction in rxns_ids:
127 if reaction in ras_row.index: 127 if reaction in ras_row.index:
128 scaling_factor = ras_row[reaction] 128 scaling_factor = ras_row[reaction]
129 lower_bound=model.reactions.get_by_id(reaction).lower_bound 129 lower_bound=model.reactions.get_by_id(reaction).lower_bound
130 upper_bound=model.reactions.get_by_id(reaction).upper_bound 130 upper_bound=model.reactions.get_by_id(reaction).upper_bound
131 #warning("Reaction: "+reaction+" Lower Bound: "+str(lower_bound)+" Upper Bound: "+str(upper_bound)+" Scaling Factor: "+str(scaling_factor))
132 valMax=float((upper_bound)*scaling_factor) 131 valMax=float((upper_bound)*scaling_factor)
133 valMin=float((lower_bound)*scaling_factor) 132 valMin=float((lower_bound)*scaling_factor)
134 if upper_bound!=0 and lower_bound==0: 133 if upper_bound!=0 and lower_bound==0:
135 model.reactions.get_by_id(reaction).upper_bound=valMax 134 model.reactions.get_by_id(reaction).upper_bound=valMax
136 if upper_bound==0 and lower_bound!=0: 135 if upper_bound==0 and lower_bound!=0:
189 rxn.lower_bound = float(df_FVA.loc[reaction, "minimum"]) 188 rxn.lower_bound = float(df_FVA.loc[reaction, "minimum"])
190 rxn.upper_bound = float(df_FVA.loc[reaction, "maximum"]) 189 rxn.upper_bound = float(df_FVA.loc[reaction, "maximum"])
191 190
192 if ras is not None: 191 if ras is not None:
193 Parallel(n_jobs=cpu_count())(delayed(process_ras_cell)(cellName, ras_row, model, rxns_ids, output_folder) for cellName, ras_row in ras.iterrows()) 192 Parallel(n_jobs=cpu_count())(delayed(process_ras_cell)(cellName, ras_row, model, rxns_ids, output_folder) for cellName, ras_row in ras.iterrows())
194 #for cellName, ras_row in ras.iterrows():
195 #process_ras_cell(cellName, ras_row, model, rxns_ids, output_folder)
196 else: 193 else:
197 model_new = model.copy() 194 model_new = model.copy()
198 apply_ras_bounds(model_new, pd.Series([1]*len(rxns_ids), index=rxns_ids), rxns_ids) 195 apply_ras_bounds(model_new, pd.Series([1]*len(rxns_ids), index=rxns_ids), rxns_ids)
199 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"]) 196 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model_new.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"])
200 bounds.to_csv(output_folder + "bounds.csv", sep='\t', index=True) 197 bounds.to_csv(output_folder + "bounds.csv", sep='\t', index=True)
219 216
220 ARGS.output_folder = 'ras_to_bounds/' 217 ARGS.output_folder = 'ras_to_bounds/'
221 218
222 if(ARGS.ras_selector == True): 219 if(ARGS.ras_selector == True):
223 ras_file_list = ARGS.input_ras.split(",") 220 ras_file_list = ARGS.input_ras.split(",")
224 ras_file_names = ARGS.names.split(",") 221 if(len(ras_file_list)>1):
225 ras_class_names = [] 222 ras_class_names = [cls.strip() for cls in ARGS.classes.split(',')]
226 for file in ras_file_names: 223 else:
227 ras_class_names.append(file.split(".")[0]) 224 ras_class_names = ["placeHolder"]
228 ras_list = [] 225 ras_list = []
229 class_assignments = pd.DataFrame(columns=["Patient_ID", "Class"]) 226 class_assignments = pd.DataFrame(columns=["Patient_ID", "Class"])
230 for ras_matrix, ras_class_name in zip(ras_file_list, ras_class_names): 227 for ras_matrix, ras_class_name in zip(ras_file_list, ras_class_names):
231 ras = read_dataset(ras_matrix, "ras dataset") 228 ras = read_dataset(ras_matrix, "ras dataset")
232 ras.replace("None", None, inplace=True) 229 ras.replace("None", None, inplace=True)
233 ras.set_index("Reactions", drop=True, inplace=True) 230 ras.set_index("Reactions", drop=True, inplace=True)
234 ras = ras.T 231 ras = ras.T
235 ras = ras.astype(float) 232 ras = ras.astype(float)
236 ras_list.append(ras) 233 ras_list.append(ras)
237 for patient_id in ras.index: 234 for patient_id in ras.index:
238 class_assignments.loc[class_assignments.shape[0]] = [patient_id, ras_class_name] 235 class_assignments = class_assignments.append({"Patient_ID": patient_id, "Class": ras_class_name}, ignore_index=True)
239
240 236
241 # Concatenate all ras DataFrames into a single DataFrame 237 # Concatenate all ras DataFrames into a single DataFrame
242 ras_combined = pd.concat(ras_list, axis=0) 238 ras_combined = pd.concat(ras_list, axis=1)
243 # Normalize the RAS values by max RAS 239 # Normalize the RAS values by max RAS
244 ras_combined = ras_combined.div(ras_combined.max(axis=0)) 240 ras_combined = ras_combined.div(ras_combined.max(axis=0))
245 ras_combined = ras_combined.fillna(0) 241 ras_combined = ras_combined.fillna(0)
246 242
247 243
263 medium = df_mediums[[ARGS.medium_selector]] 259 medium = df_mediums[[ARGS.medium_selector]]
264 medium = medium[ARGS.medium_selector].to_dict() 260 medium = medium[ARGS.medium_selector].to_dict()
265 261
266 if(ARGS.ras_selector == True): 262 if(ARGS.ras_selector == True):
267 generate_bounds(model, medium, ras = ras_combined, output_folder=ARGS.output_folder) 263 generate_bounds(model, medium, ras = ras_combined, output_folder=ARGS.output_folder)
268 class_assignments.to_csv(ARGS.cell_class, sep = '\t', index = False) 264 if(len(ras_list)>1):
265 class_assignments.to_csv(ARGS.cell_class, sep = '\t', index = False)
269 else: 266 else:
270 generate_bounds(model, medium, output_folder=ARGS.output_folder) 267 generate_bounds(model, medium, output_folder=ARGS.output_folder)
271 268
272 pass 269 pass
273 270