Skip to content

[BUG] Gene.functional attribute not saved in SBML model #1422

@wshao1

Description

@wshao1

Is there an existing issue for this?

  • I have searched the existing issues

Problem description

When I knockout a gene, the gene.functional attribute is set to False. But when the model is saved/reloaded using write_sbml_model/read_sbml_model, the gene.functional attribute is reset to the default of True. I want the gene.functional attribute to be saved with each model.

A jerry-rigged solution is to save the attribute using _sbase_notes_dict in cobrapy/src/cobra/io/sbml.py:

_sbase_notes_dict(gp, {"functional": cobra_gene.functional}) --> after line 1344 in def _model_to_sbml

notes = _parse_notes_dict(gp)
if "functional" in notes:
    cobra_gene.functional = (notes["functional"] == "True")

--> after line 746 in def _sbml_to_model

I'm sure there's a better way to do this that aligns with SBML standards, so wondering if anyone who is familiar with SBML can fix this in a more elegant way.

Code sample

Code run:

# load textbook model and try KO'ing a gene
txtbook_model = load_model("textbook")
txtbook_model.genes.b0118.knock_out()
print(txtbook_model.genes.b0118.functional)

# save and reload textbook model
write_sbml_model(txtbook_model, 'txtbook_model.xml')
saved_txtbook_model = read_sbml_model('txtbook_model.xml')

# check if gene is still KO'ed (functional = False)
print(saved_txtbook_model.genes.b0118.functional)

Environment

### Package Information
Package Version
cobra 0.29.1

Dependency Information

Package Version
appdirs~ missing
black missing
bumpversion missing
depinfo~ missing
diskcache~ missing
future 1.0.0
httpx~ missing
importlib-resources 6.5.2
isort missing
numpy 2.2.1
optlang~ missing
pandas 2.2.3
pydantic 2.10.4
python-libsbml~ missing
rich 13.9.4
ruamel.yaml~ missing
scipy missing
swiglpk 5.0.12
tox missing

Build Tools Information

Package Version
pip 24.2
setuptools 75.1.0
wheel 0.44.0

Platform Information

Darwin 24.2.0-arm64
CPython 3.12.8

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions