Skip to content

Commit

Permalink
Merge pull request #237 from jonls/sbml-updates
Browse files Browse the repository at this point in the history
By default, ignore boundary species in SBMLReader
  • Loading branch information
jonls committed May 16, 2017
2 parents 403792c + 7f1ba4a commit edace45
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
20 changes: 17 additions & 3 deletions psamm/datasource/sbml.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,18 @@ class SBMLReader(object):
If ``ignore_boundary`` is ``True``, the species that are marked as
boundary conditions will simply be dropped from the species list and from
the reaction equations, and any boundary compartment will be dropped too.
Otherwise the boundary species will be retained. Retaining these is only
useful to extract specific information from those species objects.
Args:
file: File-like object to parse XML SBML content from.
strict: Indicating whether strict parsing is enabled.
ignore_boundary: Indicating whether boundary species are dropped.
context: Optional file parsing context from
:mod:`psamm.datasource.context`.
"""

def __init__(self, file, strict=False, ignore_boundary=False,
def __init__(self, file, strict=False, ignore_boundary=True,
context=None):
# Parse SBML file
tree = ET.parse(file)
Expand Down Expand Up @@ -832,7 +841,7 @@ def create_model(self):


class SBMLWriter(object):
"""Writer of SBML files"""
"""Writer of SBML files."""

def __init__(self, cobra_flux_bounds=False):
self._namespace = SBML_NS_L3_V1_CORE
Expand Down Expand Up @@ -970,8 +979,13 @@ def _indent(self, elem, level=0):
elem.tail = i

def write_model(self, file, model, pretty=False):
"""Write a given model to file"""
"""Write a given model to file.
Args:
file: File-like object open for writing.
model: Instance of :class:`NativeModel` to write.
pretty: Whether to format the XML output for readability.
"""
ET.register_namespace('mathml', MATHML_NS)
ET.register_namespace('xhtml', XHTML_NS)
ET.register_namespace('fbc', FBC_V2)
Expand Down
36 changes: 18 additions & 18 deletions psamm/tests/test_datasource_sbml.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with PSAMM. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2014-2015 Jon Lund Steffensen <jon_steffensen@uri.edu>
# Copyright 2014-2017 Jon Lund Steffensen <jon_steffensen@uri.edu>

import unittest

Expand Down Expand Up @@ -77,7 +77,7 @@ def test_model_name(self):
self.assertEqual(reader.name, 'Test model')

def test_compartment_exists(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
compartments = {entry.id: entry for entry in reader.compartments}
self.assertEqual(len(compartments), 2)
self.assertEqual(compartments['cell'].id, 'cell')
Expand All @@ -93,7 +93,7 @@ def test_compartment_exists_with_ignore_boundary(self):
self.assertEqual(compartments['cell'].name, 'cell')

def test_compounds_exist(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
species = {entry.id: entry for entry in reader.species}
self.assertEqual(len(species), 5)

Expand Down Expand Up @@ -132,7 +132,7 @@ def test_g6pase_reaction_exists(self):
self.assertEqual(reaction.equation, actual_equation)

def test_biomass_reaction_exists(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
reaction = reader.get_reaction('Biomass')
self.assertFalse(reaction.reversible)

Expand All @@ -144,7 +144,7 @@ def test_biomass_reaction_exists(self):
self.assertEqual(reaction.equation, actual_equation)

def test_reaction_xml_notes(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
reaction = reader.get_reaction('G6Pase')
notes = reaction.xml_notes

Expand Down Expand Up @@ -217,7 +217,7 @@ def test_model_name(self):
self.assertEqual(reader.name, 'Test model')

def test_compartment_exists(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
compartments = {entry.id: entry for entry in reader.compartments}
self.assertEqual(len(compartments), 2)
self.assertEqual(compartments['C_c'].id, 'C_c')
Expand All @@ -233,7 +233,7 @@ def test_compartment_exists_with_ignore_boundary(self):
self.assertEqual(compartments['C_c'].name, 'cell')

def test_compounds_exist(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
species = {entry.id: entry for entry in reader.species}
self.assertEqual(len(species), 5)

Expand Down Expand Up @@ -272,7 +272,7 @@ def test_g6pase_reaction_exists(self):
self.assertEqual(reaction.equation, actual_equation)

def test_biomass_reaction_exists(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
reaction = reader.get_reaction('R_Biomass')
self.assertFalse(reaction.reversible)

Expand Down Expand Up @@ -357,7 +357,7 @@ def test_model_name(self):
self.assertEqual(reader.name, 'Test model')

def test_compartment_exists(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
compartments = {entry.id: entry for entry in reader.compartments}
self.assertEqual(len(compartments), 2)
self.assertEqual(compartments['C_c'].id, 'C_c')
Expand All @@ -373,7 +373,7 @@ def test_compartment_exists_with_ignore_boundary(self):
self.assertEqual(compartments['C_c'].name, 'cell')

def test_compounds_exist(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
species = {entry.id: entry for entry in reader.species}
self.assertEqual(len(species), 5)

Expand Down Expand Up @@ -409,7 +409,7 @@ def test_g6pase_reaction_exists(self):
self.assertEqual(reaction.equation, actual_equation)

def test_biomass_reaction_exists(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
reaction = reader.get_reaction('R_Biomass')
self.assertFalse(reaction.reversible)

Expand Down Expand Up @@ -506,7 +506,7 @@ def test_model_name(self):
self.assertEqual(reader.name, 'Test model')

def test_compartment_exists(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
compartments = {entry.id: entry for entry in reader.compartments}
self.assertEqual(len(compartments), 2)
self.assertEqual(compartments['C_c'].id, 'C_c')
Expand All @@ -515,7 +515,7 @@ def test_compartment_exists(self):
self.assertEqual(compartments['C_b'].name, 'boundary')

def test_compounds_exist(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
species = {entry.id: entry for entry in reader.species}
self.assertEqual(len(species), 5)

Expand Down Expand Up @@ -547,7 +547,7 @@ def test_compounds_exist(self):
self.assertIsNone(species['M_Biomass'].charge)

def test_g6pase_reaction_exists(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
reaction = reader.get_reaction('R_G6Pase')
self.assertTrue(reaction.reversible)

Expand All @@ -563,7 +563,7 @@ def test_g6pase_reaction_exists(self):
self.assertEqual(reaction.properties['upper_flux'], 1000)

def test_biomass_reaction_exists(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
reaction = reader.get_reaction('R_Biomass')
self.assertFalse(reaction.reversible)

Expand Down Expand Up @@ -675,7 +675,7 @@ def test_model_name(self):
self.assertEqual(reader.name, 'Test model')

def test_compartment_exists(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
compartments = {entry.id: entry for entry in reader.compartments}
self.assertEqual(len(compartments), 2)
self.assertEqual(compartments['C_c'].id, 'C_c')
Expand All @@ -684,7 +684,7 @@ def test_compartment_exists(self):
self.assertEqual(compartments['C_b'].name, 'boundary')

def test_compounds_exist(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
species = {entry.id: entry for entry in reader.species}
self.assertEqual(len(species), 5)

Expand Down Expand Up @@ -732,7 +732,7 @@ def test_g6pase_reaction_exists(self):
self.assertEqual(reaction.properties['upper_flux'], 1000)

def test_biomass_reaction_exists(self):
reader = sbml.SBMLReader(self.doc)
reader = sbml.SBMLReader(self.doc, ignore_boundary=False)
reaction = reader.get_reaction('R_Biomass')
self.assertFalse(reaction.reversible)

Expand Down

0 comments on commit edace45

Please sign in to comment.