Skip to content

[BUG] Error loading model from BioModels repository #1466

@abates20

Description

@abates20

Is there an existing issue for this?

  • I have searched the existing issues

Problem description

I am trying to use cobra.io.load_model to get models from the BioModels repository, but I keep getting a UnicodeDecodeError. My expectation is that using cobra.io.load_model should load the model from BioModels without raising an error.

Code sample

Code run:

import cobra
cobra.io.load_model("BIOMD0000001089")

Traceback:

Traceback (most recent call last):
  File "/Users/fbg3yp/repos/reconstructor/dev/test.py", line 2, in <module>
    cobra.io.load_model("BIOMD0000001089")
  File "/Users/fbg3yp/Library/Caches/pypoetry/virtualenvs/reconstructor-rVg7mL4q-py3.9/lib/python3.9/site-packages/cobra/io/web/load.py", line 94, in load_model
    return get_model_from_gzip_sbml(data)
  File "/Users/fbg3yp/Library/Caches/pypoetry/virtualenvs/reconstructor-rVg7mL4q-py3.9/lib/python3.9/site-packages/cobra/io/web/load.py", line 199, in get_model_from_gzip_sbml
    libsbml.readSBMLFromString(gzip.decompress(stream).decode("utf-8"))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 10: invalid start byte

Environment

Package Information

Package Version
cobra 0.30.0

Dependency Information

Package Version
appdirs~ missing
black 25.11.0
bumpversion missing
depinfo~ missing
diskcache~ missing
future 1.0.0
httpx~ missing
isort missing
numpy 1.24.4
optlang~ missing
pandas 2.3.3
pydantic 2.11.10
python-libsbml~ missing
rich 14.1.0
ruamel.yaml~ missing
scipy missing
swiglpk 5.0.12
tox missing

Build Tools Information

Package Version
pip 25.3

Platform Information

Darwin 25.1.0-x86_64
CPython 3.9.24

Anything else?

It appears that the file downloaded from BioModels using BioModels.get_sbml is actually a gzipped zip archive. For example, if you decompress the file contents with gzip.decompress, you can then open the archive with zipfile.ZipFile:

import cobra
import gzip
import zipfile

biomodels = cobra.io.BioModels()
raw_contents = biomodels.get_sbml("BIOMD0000001089")
with open("model.sbml.zip", "wb") as file:
    file.write(gzip.decompress(raw_contents))

with zipfile.ZipFile("model.sbml.zip") as file:
    print(file.namelist())

Output from running the above code:

['manifest.xml', '253_2019_9630_MOESM3_ESM.xml', 'FROG/01_objective.tsv', 'FROG/02_fva.tsv', 'FROG/03_gene_deletion.tsv', 'FROG/04_reaction_deletion.tsv', 'FROG/metadata.json']

I haven't checked thoroughly but I would suspect that '253_2019_9630_MOESM3_ESM.xml' is the actual SBML file for the model.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions