excel to sbml file

887 views
Skip to first unread message

met mod

unread,
Jan 5, 2018, 2:47:25 PM1/5/18
to cobra pie
Hi I am new to cobrapy. Is there a method to convert excel sheets to sbml file?
Any help would be thankful.

Christian Diener

unread,
Jan 9, 2018, 11:49:23 AM1/9/18
to cobra pie
No, unfortunately there is no such thing because there is no standard on what a an excel model should look like (required columns etc).

You could read the excel sheet using pandas and write your own parser. The reaction object has a `build_reaction_from_string` that could help you there.

Cheers
Christian

met mod

unread,
Jan 9, 2018, 1:13:35 PM1/9/18
to cobra pie
Hi Christian
Thank you for replying.
So does it mean, If I have my data in excel sheet I type all of the information in python. That is how I create a model? Is this the method everyone uses?

Christian Diener

unread,
Jan 9, 2018, 6:18:38 PM1/9/18
to cobra pie
No, I would start with reading the excel file into python using pandas. For instance with

```
import pandas as pd

excel_model = pd.read_excel("model.xslx")
```

This will give you a pandas DataFrame with the columns from your excel sheet. Let's assume that one of your columns contains the reactions as reactions strings ("reaction") and another one the id ("id") than you can do something like

```
import cobra

mod = cobra.Model("my model")
for _, row in excel_model.iterrows():
    r = cobra.Reaction(row.id)
    mod.add_reaction(r)
    r.build_reaction_from_string(row.reaction)
```

You could also set additional properties of the reaction from other columns such as bounds etc.

After you build your model you can write it to SBML 3 using

```
from cobra.io import write_sbml_model

write_sbml_model(mod, "my_model.xml")
```

Most people either use an already defined model and only modify some parts of it using cobrapy and save it again in SBML. Building models completely de novo is not
super common and if is done it is usually done based on genomic data and uses methods like ModelSeed, CARVEME, Raven Toolbox etc.

Hope that helps.

Cheers
Christian

Moritz Beber

unread,
Jan 9, 2018, 7:25:18 PM1/9/18
to cobr...@googlegroups.com
Hello,


On Friday, January 5, 2018 at 2:47:25 PM UTC-5, met mod wrote:
Hi I am new to cobrapy. Is there a method to convert excel sheets to sbml file?
Any help would be thankful.

I'm afraid there is no direct method as Christian told you but you can get some inspiration from this repository: https://github.com/opencobra/m_model_collection especially the following Python script should be interesting: https://github.com/opencobra/m_model_collection/blob/master/read_excel.py

Best of luck,
Moritz
Message has been deleted

met mod

unread,
Jan 10, 2018, 11:50:00 AM1/10/18
to cobra pie
Hi Christian and Moritz
Thanks for the suggestions!
As you guys suggested I tried the codes. Every thing works fine. But the .xml file I get its empty. It does not have the reactions and metabolites present in the excel sheet.

The python codes I used are

>>> excel_model=pd.read_excel("C:\Python34\Lib\site-packages\excel.xlsx")
>>> sbml_filename=("C:\Python34\LENOVO.xml")
>>> M=io.sbml.write_cobra_model_to_sbml_file(cobra_model,sbml_filename,sbml_level=2,sbml_version=1, print_time=False, use_fbc_package=True)

I am also attaching the LENOVO.xml file.

Am I missing something?


On Friday, January 5, 2018 at 2:47:25 PM UTC-5, met mod wrote:
LENOVO.xml

Christian Diener

unread,
Jan 10, 2018, 11:59:35 AM1/10/18
to cobra pie
You did not paste your code in which you actually construct the model, so it is pretty hard to debug it... How did you add the reactions (and maybe metabolites) to the model? Also it is recommended to use the `io.write_sbml_file` function rather than the one you used and which is reserved for internal usage. What does `M.reactions` give you?

Cheers
Christian

met mod

unread,
Jan 10, 2018, 12:28:44 PM1/10/18
to cobra pie
Hi
I am sure I missed something.

>>> M.reactions
Traceback (most recent call last):
  File "<pyshell#110>", line 1, in <module>
    M.reactions
AttributeError: 'NoneType' object has no attribute 'reactions'

 

On Friday, January 5, 2018 at 2:47:25 PM UTC-5, met mod wrote:

Christian Diener

unread,
Jan 15, 2018, 12:23:08 PM1/15/18
to cobra pie
Ah sorry, I meant `cobra_model.reactions`. Either way you need some code that actually parses the reactions from the excel and adds them to the model (for instance like the example code I posted earlier)...
Reply all
Reply to author
Forward
0 new messages