Skip to content

Commit

Permalink
Merge pull request #6 from cjarmstrong97/parsing
Browse files Browse the repository at this point in the history
Parsing - PatternQuantifier
  • Loading branch information
cjarmstrong97 authored Sep 21, 2021
2 parents f34e29a + 2de69a1 commit 3ae34bb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
4 changes: 4 additions & 0 deletions bionetgen/modelapi/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def __init__(self, molecules=[], bonds=None, compartment=None, label=None):
self._label = label
self.fixed = False
self.MatchOnce = False
self.relation = None
self.quantity = None

@property
def compartment(self):
Expand Down Expand Up @@ -73,6 +75,8 @@ def __str__(self):
if imol > 0:
sstr += "."
sstr += str(mol)
if self.relation is not None:
sstr += f"{self.relation}{self.quantity}"
return sstr

def __repr__(self):
Expand Down
22 changes: 17 additions & 5 deletions bionetgen/modelapi/xmlparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,29 @@ def parse_xml(self, xml) -> Pattern:
bonds = BondsXML()
pattern._bonds = bonds
self._bonds = bonds
#
# check for compartment and add if exists
if "@compartment" in xml:
pattern.compartment = xml["@compartment"]
#
# check for label and add if exists
if "@label" in xml:
pattern.label = xml["@label"]
#
# check to see if pattern is fixed
if "@Fixed" in xml:
if xml["@Fixed"] == "1":
pattern.fixed = True
#
# check for relation & quantity, add if exist
if ("@relation" in xml) and ("@quantity" in xml):
relation = xml["@relation"]
quantity = xml["@quantity"]
pattern.relation = relation
try:
n = int(quantity)
f = float(quantity)
if n == f:
pattern.quantity = quantity
except ValueError as e:
print("Quantity needs to be an integer")
# check for either list of molecules or single molecule, add if exist
mols = xml["ListOfMolecules"]["Molecule"]
molecules = []
if isinstance(mols, list):
Expand Down Expand Up @@ -262,7 +274,7 @@ def parse_xml(self, xml) -> list:
return patterns


###### Parsers ######
###### Parsers ######
class ParameterBlockXML(XMLObj):
"""
PatternBlock XML parser, derived from XMLObj. Creates
Expand Down
5 changes: 2 additions & 3 deletions tests/models/hybrid_test.bngl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ begin observables
Species R2 R==2
Species R3 R==3
Species R4 R==4
Species R5 R>4
Species R5 R<4
end observables
begin reaction rules
R(l!1).L(r!1) -> R(l) + L(r) koff
Expand All @@ -45,5 +45,4 @@ end population maps
end model

## actions ##
generate_hybrid_model({overwrite=>1, execute=>1, verbose=>1, safe=>0, actions=>["writeXML()"]})

generate_hybrid_model({overwrite=>1, execute=>1, verbose=>1, safe=>0, actions=>["writeXML()"]})

0 comments on commit 3ae34bb

Please sign in to comment.