comparison COBRAxy/utils/model_utils.py @ 430:f49c951c9fe6 draft

Uploaded
author francesco_lapi
date Wed, 10 Sep 2025 14:53:36 +0000
parents 4a385fdb9e58
children 06564187fba3
comparison
equal deleted inserted replaced
429:0485c4b1943d 430:f49c951c9fe6
252 reactions_added += 1 252 reactions_added += 1
253 253
254 254
255 print(f"Aggiunte {reactions_added} reazioni, saltate {reactions_skipped} reazioni") 255 print(f"Aggiunte {reactions_added} reazioni, saltate {reactions_skipped} reazioni")
256 256
257 # Imposta l'obiettivo di biomassa 257 # set objective function
258 set_biomass_objective(model) 258 set_objective_from_csv(model, df, obj_col="ObjectiveCoefficient")
259 259
260 # Imposta il medium 260 # Imposta il medium
261 set_medium_from_data(model, df) 261 set_medium_from_data(model, df)
262 262
263 print(f"Modello completato: {len(model.reactions)} reazioni, {len(model.metabolites)} metaboliti") 263 print(f"Modello completato: {len(model.reactions)} reazioni, {len(model.metabolites)} metaboliti")
264 264
359 359
360 return metabolites 360 return metabolites
361 361
362 362
363 363
364 def set_biomass_objective(model: cobraModel): 364 def set_objective_from_csv(model: cobra.Model, df: pd.DataFrame, obj_col: str = "ObjectiveCoefficient"):
365 """ 365 """
366 Imposta la reazione di biomassa come obiettivo. 366 Sets the model's objective function based on a column of coefficients in the CSV.
367 """ 367 Can be any reaction(s), not necessarily biomass.
368 biomass_reactions = [r for r in model.reactions if 'biomass' in r.id.lower()] 368 """
369 369 obj_dict = {}
370 if biomass_reactions: 370
371 model.objective = biomass_reactions[0].id 371 for idx, row in df.iterrows():
372 print(f"Obiettivo impostato su: {biomass_reactions[0].id}") 372 reaction_id = str(row['ReactionID']).strip()
373 else: 373 coeff = float(row[obj_col]) if pd.notna(row[obj_col]) else 0.0
374 print("Nessuna reazione di biomassa trovata") 374 if coeff != 0:
375 # Assicuriamoci che la reazione esista nel modello
376 if reaction_id in model.reactions:
377 obj_dict[model.reactions.get_by_id(reaction_id)] = coeff
378 else:
379 print(f"Warning: reaction {reaction_id} not found in model, skipping for objective.")
380
381 if not obj_dict:
382 raise ValueError("No reactions found with non-zero objective coefficient.")
383
384 # Imposta la funzione obiettivo
385 model.objective = obj_dict
386 print(f"Objective set with {len(obj_dict)} reactions.")
387
388
375 389
376 390
377 def set_medium_from_data(model: cobraModel, df: pd.DataFrame): 391 def set_medium_from_data(model: cobraModel, df: pd.DataFrame):
378 """ 392 """
379 Imposta il medium basato sulla colonna InMedium. 393 Imposta il medium basato sulla colonna InMedium.