I have been working on a metabolic model in matlab, but due to functionality restrictions decided to switch to python. In Matlab I could always just load the metabolic reactions in an excel file in matlab. I have been trying to do the same in python, but I can't get it to recognize the metabolites from an excel file. This is the code that I am currently using, which leaves the model empty. Can't figure out whether its even possible to do it this way in python or should take a different approach. Would be super grateful for any tips!
**#Import necessary toolboxes**
from cobra import Model, Metabolite, Reaction
import pandas as pd
# Load your DataFrame from the Excel file
df = pd.read_excel(r"filename")
print(df)
# Converting columns dataframe to stringkeys
RXN_ID_KEY = "ID"
RXN_STR_KEY = "Reaction"
RXN_LB_KEY = "Lowerbound"
RXN_UB_KEY = "Upperbound"
MET_ID_KEY = "Metabolites"
# Defining model
model = Model('my_model')
# Add reactions to model
for index, row in df.iterrows():
reaction = Reaction(row[RXN_ID_KEY])
reaction.name = row[RXN_STR_KEY]
reaction.lower_bound = row[RXN_LB_KEY]
reaction.upper_bound = row[RXN_UB_KEY]
# Add metabolites to model
metabolite_name = row[MET_ID_KEY]
if isinstance(metabolite_name, str) and metabolite_name.strip():
# Check if the "Metabolites" column is not empty or null
metabolite = Metabolite(metabolite_name)
model.add_metabolites([metabolite])
else:
# Handle cases where "Metabolites" column is empty or invalid
print(f"Invalid or missing metabolite name in row {index}")
model.add_reactions([reaction])
# Print overview model
print(f'{len(model.reactions)} reactions')
print(f'{len(model.metabolites)} metabolites')
# Set objective model
model.objective = model.reactions.get_by_id('Ex_BiomassEx')
print(model.objective.expression)
print(model.objective.direction)
#solve model
solution = model.optimize()
flux_distribution = solution.fluxes
print(flux_distribution)
df['Flux'] = flux_distribution.fillna(0)
print(df)
Want to calculate fluxes to optimize cellular growth in model