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")
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)

Respuestas (1)

Siraj
Siraj el 6 de Dic. de 2023
Editada: Siraj el 6 de Dic. de 2023
Hi!
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: https://www.mathworks.com/help/matlab/call-python-libraries.html?s_tid=CRUX_lftnav https://www.mathworks.com/help/matlab/matlab_external/ways-to-call-python-from-matlab.html
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: https://www.mathworks.com/help/matlab/matlab-engine-for-python.html
To learn how to call MATLAB functions from Python, you can refer to this link: https://www.mathworks.com/help/matlab/matlab_external/call-matlab-functions-from-python.html
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: https://www.mathworks.com/help/matlab/matlab_external/pass-data-to-matlab-from-python.html https://www.mathworks.com/help/matlab/matlab_external/handle-data-returned-from-matlab-to-python.html
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
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
print("reactionNames")
print(reactionNames)
print()
print("reactionRates")
print(reactionRates)
# Quit the MATLAB engine
mat_eng.quit()
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.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by