From 29357eccde69a00a889034882c7c38d771ff1042 Mon Sep 17 00:00:00 2001 From: cjarmstrong97 Date: Tue, 21 Sep 2021 13:21:19 -0400 Subject: [PATCH 1/2] Beginning of parsing branch --- bionetgen/modelapi/pattern.py | 4 ++++ bionetgen/modelapi/xmlparsers.py | 22 +++++++++++++++++----- tests/models/hybrid_test.bngl | 7 +++++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/bionetgen/modelapi/pattern.py b/bionetgen/modelapi/pattern.py index 0205453..810233b 100644 --- a/bionetgen/modelapi/pattern.py +++ b/bionetgen/modelapi/pattern.py @@ -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): @@ -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): diff --git a/bionetgen/modelapi/xmlparsers.py b/bionetgen/modelapi/xmlparsers.py index ff9f9e3..f607098 100644 --- a/bionetgen/modelapi/xmlparsers.py +++ b/bionetgen/modelapi/xmlparsers.py @@ -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(pattern.quantity) + f = float(pattern.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): @@ -262,7 +274,7 @@ def parse_xml(self, xml) -> list: return patterns -###### Parsers ###### +###### Parsers ###### class ParameterBlockXML(XMLObj): """ PatternBlock XML parser, derived from XMLObj. Creates diff --git a/tests/models/hybrid_test.bngl b/tests/models/hybrid_test.bngl index a8bf61a..6887904 100644 --- a/tests/models/hybrid_test.bngl +++ b/tests/models/hybrid_test.bngl @@ -31,6 +31,9 @@ begin observables Species R3 R==3 Species R4 R==4 Species R5 R>4 + Species R5le R<=4 + Species R5l R<4 + Species R5ge R>=4 end observables begin reaction rules R(l!1).L(r!1) -> R(l) + L(r) koff @@ -45,5 +48,5 @@ 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()"]}) +writeXML() From 2de69a1dd70070970d092c8ebfe3d8b466a3b5a2 Mon Sep 17 00:00:00 2001 From: cjarmstrong97 Date: Tue, 21 Sep 2021 15:10:04 -0400 Subject: [PATCH 2/2] Ensured parsing of every element of PatternQuantifier --- bionetgen/modelapi/xmlparsers.py | 4 ++-- tests/models/hybrid_test.bngl | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/bionetgen/modelapi/xmlparsers.py b/bionetgen/modelapi/xmlparsers.py index f607098..2f3f5fa 100644 --- a/bionetgen/modelapi/xmlparsers.py +++ b/bionetgen/modelapi/xmlparsers.py @@ -167,8 +167,8 @@ def parse_xml(self, xml) -> Pattern: quantity = xml["@quantity"] pattern.relation = relation try: - n = int(pattern.quantity) - f = float(pattern.quantity) + n = int(quantity) + f = float(quantity) if n == f: pattern.quantity = quantity except ValueError as e: diff --git a/tests/models/hybrid_test.bngl b/tests/models/hybrid_test.bngl index 6887904..ba381de 100644 --- a/tests/models/hybrid_test.bngl +++ b/tests/models/hybrid_test.bngl @@ -30,10 +30,7 @@ begin observables Species R2 R==2 Species R3 R==3 Species R4 R==4 - Species R5 R>4 - Species R5le R<=4 - Species R5l R<4 - Species R5ge R>=4 + Species R5 R<4 end observables begin reaction rules R(l!1).L(r!1) -> R(l) + L(r) koff @@ -48,5 +45,4 @@ end population maps end model ## actions ## -#generate_hybrid_model({overwrite=>1, execute=>1, verbose=>1, safe=>0, actions=>["writeXML()"]}) -writeXML() +generate_hybrid_model({overwrite=>1, execute=>1, verbose=>1, safe=>0, actions=>["writeXML()"]}) \ No newline at end of file