Converting metabollic model in matlab to Cobrapy

9 visualizaciones (últimos 30 días)
Suzanne Angenent
Suzanne Angenent el 2 de Nov. de 2023
Editada: Siraj el 6 de Dic. de 2023
I have been working on a metabolic model with the cobratoolbox 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 python 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 on how to improve this python code or how to convert the matlab model to python code!
#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")
# Converting columns dataframe to stringkeys
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]) = 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)
# Handle cases where "Metabolites" column is empty or invalid
print(f"Invalid or missing metabolite name in row {index}")
# 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')
#solve model
solution = model.optimize()
flux_distribution = solution.fluxes
df['Flux'] = flux_distribution.fillna(0)

Respuestas (1)

Siraj el 6 de Dic. de 2023
Editada: Siraj el 6 de Dic. de 2023
It is my understanding that while working on a metabolic model using the cobratoolbox in MATLAB, you encountered functionality limitations, prompting you to switch to Python. However, in Python, you're facing challenges loading metabolic reactions from an Excel file because Python doesn't seem to recognize the associated metabolites. This is unlike in MATLAB, where you could easily load these metabolic reactions from an Excel file.
There are two ways to avoid such a situation. One way could be for you to continue using MATLAB and call any additional Python functions you need directly from MATLAB. You can achieve this by calling functionality from Python libraries or executing Python statements directly from MATLAB. To learn how to do so, you can refer to the following links:
However, there are certain limitations to calling Python functions from MATLAB, so make sure to review the above links and check for any applicable limitations.
Another approach involves reading data using MATLAB and passing that data to Python, allowing you to continue your workflow in Python. This can be achieved using the "MATLAB Engine API for Python," which enables you to call MATLAB functions and scripts from within Python. To install the MATLAB Engine API for Python, you can refer to the following link:
To learn how to call MATLAB functions from Python, you can refer to this link:
Since MATLAB and Python have different data types, you may need to convert data between the two languages when passing data back and forth. The following links will be helpful while trying to do this:
For a comprehensive example, you can refer to the following demonstration to better understand the process:
Create a dummy data
% Create a table of metabolic reactions data
reactionNames = ['Reaction 1'; 'Reaction 2'; 'Reaction 3'];
reactionRates = [0.1, 0.2, 0.3]';
reactionTable = table(reactionNames, reactionRates);
% Display
reactionTable = 3×2 table
reactionNames reactionRates _____________ _____________ Reaction 1 0.1 Reaction 2 0.2 Reaction 3 0.3
% Write the table to an Excel file
filename = 'metabolic_reactions.xlsx';
writetable(reactionTable, filename);
Read data in MATLAB and pass it to Python.
import matlab.engine
import numpy as np
import pandas as pd
# Start MATLAB engine
mat_eng = matlab.engine.start_matlab()
# Set the filename in MATLAB workspace
mat_eng.workspace['filename'] = 'metabolic_reactions.xlsx'
# Read the table from the Excel file using MATLAB
my_table = mat_eng.eval('readtable(filename)')
# Retrieve reactionNames and reactionRates from the MATLAB workspace
reactionNames = mat_eng.getfield(my_table, 'reactionNames')
reactionRates = mat_eng.getfield(my_table, 'reactionRates')
# Display the retrieved data
# Quit the MATLAB engine
Output of the above code
This approach will allow you to seamlessly transition between MATLAB and Python, leveraging the strengths of both languages for your workflow.
Hope this helps.


Más información sobre Call Python from MATLAB en Help Center y File Exchange.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by