comparison COBRAxy/ras_to_bounds.py @ 94:e844f7dab6fe draft

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