Skip to content

Commit

Permalink
- fix an issue with importing SBML
Browse files Browse the repository at this point in the history
  • Loading branch information
fbergmann committed Oct 15, 2024
1 parent 14359d7 commit 5b4ee8f
Show file tree
Hide file tree
Showing 3 changed files with 543 additions and 3 deletions.
6 changes: 3 additions & 3 deletions basico/model_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ def load_model_from_string(content):
if type(content) is bytes:
content = content.decode("utf8")

if '<COPASI ' in content and model.loadModelFromString(content, os.getcwd()):
if '<sbml ' in content and model.importSBMLFromString(content):
return set_current_model(model)

if '<sbml ' in content and model.importSBMLFromString(content):
if '<COPASI ' in content and model.loadModelFromString(content, os.getcwd()):
return set_current_model(model)

if '<sedML ' in content and model.importSEDMLFromString(content):
Expand Down Expand Up @@ -332,7 +332,7 @@ def remove_annotations(content, annotations):
for annotation in root.iter('{%s}%s' % (namespace, element)):
annotation.getparent().remove(annotation)

return etree.tostring(root, pretty_print=True).decode('utf-8')
return etree.tostring(root, pretty_print=True, xml_declaration=True, encoding="UTF-8").decode('utf-8')


def load_model(location, remove_user_defined_functions=False):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_basico_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,26 @@ def test_import_sbml_with_pre_processing(self):

basico.remove_datamodel(dm)

@unittest.skipUnless(_have_lxml, "lxml not available")
def test_import_sbml_with_pre_processing(self):
filename = os.path.join(os.path.dirname(__file__), 'test_data', 'import_sbml_issue.xml')
self.assertTrue(os.path.exists(filename))

# this file contains special processing instructions
# since an initial value is used in an expression.

dm = basico.import_sbml(filename, annotations_to_remove=[
('initialValue', 'http://copasi.org/initialValue')],
ensure_unique_names=True
)

self.assertTrue(dm is not None)
species = basico.as_dict(basico.get_species())
self.assertEqual(len(species), 13)
params = basico.as_dict(basico.get_parameters())
self.assertEqual(len(params), 7)

basico.remove_datamodel(dm)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 5b4ee8f

Please sign in to comment.