Skip to content

Commit

Permalink
More bug fixes in parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Old-Shatterhand committed Feb 21, 2025
1 parent c2d7e37 commit 5a7f82d
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 17 deletions.
4 changes: 2 additions & 2 deletions glyles/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from joblib import Parallel, delayed, parallel_backend

from glyles.glycans.poly.gwb_glycan import GWBGlycan
from glyles.glycans.utils import ParseError
from glyles.glycans.poly.glycan import Glycan
from glyles.glycans.utils import ParseError


def preprocess_glycans(glycan, glycan_list, glycan_file):
Expand Down Expand Up @@ -207,4 +207,4 @@ def generate(glycan, full):

if __name__ == "__main__":
# print(convert("GalNAc"))
print(Glycan("GalNAc").get_smiles())
print(Glycan("GalNAc").to_iupac())
4 changes: 0 additions & 4 deletions glyles/glycans/poly/glycan.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,3 @@ def parse(self):
f"Error message: {msg}")
self.glycan_smiles = ""
raise e


if __name__ == "__main__":
print(Glycan("dTal").get_smiles())
10 changes: 3 additions & 7 deletions glyles/glycans/poly/gwb_glycan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from antlr4 import CommonTokenStream, InputStream
import networkx as nx

from glyles.glycans.poly.gwb_walker import GWBTreeWalker
from glyles.glycans.poly.glycan import Glycan, prepare
from glyles.glycans.poly.merger import Merger
from glyles.glycans.poly.gwb_walker import GWBTreeWalker
from glyles.glycans.utils import ParseError
from glyles.gwb.GWBLexer import GWBLexer
from glyles.gwb.GWBParser import GWBParser
from glyles.glycans.poly.glycan import Glycan, prepare


def graph_to_string_int(graph: nx.DiGraph, node2label: callable) -> str:
Expand Down Expand Up @@ -78,7 +78,7 @@ def parse(self):
if "$" in self.iupac:
self.iupac = self.iupac[:self.iupac.index("$")]
self.iupac += "$"
prepare(self.iupac)
self.iupac = prepare(self.iupac)
stream = InputStream(data=self.iupac)
lexer = GWBLexer(stream)
token = CommonTokenStream(lexer)
Expand Down Expand Up @@ -106,7 +106,3 @@ def parse(self):
f"Error message: {msg}")
self.glycan_smiles = ""
raise e


if __name__ == "__main__":
print(GWBGlycan("freeEnd--?b1D-GlcNAc,p(--6a1L-Fuc,p)--4b1D-GlcNAc,p--4b1D-Man,p(--3a1D-Man,p(--??1D-GlcNAc,p--??1D-Gal,p--??2D-NeuAc,p)--??1D-GlcNAc,p--??1D-Gal,p--??2D-NeuAc,p--??2D-NeuAc,p)--6a1D-Man,p(--??1D-Man,p)--??1D-Man,p$MONO,Und,-2H,0,freeEnd").to_iupac(slim=True))
2 changes: 1 addition & 1 deletion glyles/glycans/poly/gwb_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def add_edge(self, parent, child, con, floating: bool = False) -> bool:
return True
self.g.add_edge(parent, child, type=con)
return "?" not in con and "/" not in con
return False
return True # parent == -1 and not floating

def update_node(self, node_id):
recipe = self.g.nodes[node_id]["update"]
Expand Down
7 changes: 4 additions & 3 deletions glyles/glycans/poly/walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from antlr4 import ErrorNode, TerminalNode

from glyles.glycans.utils import UnreachableError, ketoses2
from glyles.gwb.GWBParser import GWBParser
from glyles.iupac.IUPACLexer import IUPACLexer
from glyles.iupac.IUPACParser import IUPACParser
from glyles.gwb.GWBLexer import GWBLexer
Expand Down Expand Up @@ -165,12 +166,12 @@ def build_recipe(self, node):
recipe = []
for c in node.getChildren():
if isinstance(c, TerminalNode):
recipe.append((str(c), IUPACLexer.SAC if isinstance(node, IUPACParser.SaciContext) else c.symbol.type))
recipe.append((str(c), IUPACLexer.SAC if isinstance(node, (IUPACParser.SaciContext, GWBParser.SaciContext)) else c.symbol.type))
else:
tmp = self.build_recipe(c)
if isinstance(c, IUPACParser.ModiContext):
if isinstance(c, (IUPACParser.ModiContext, GWBParser.ModiContext)):
recipe.append(("".join([x[0] for x in tmp]), IUPACLexer.MOD))
elif isinstance(c, IUPACParser.SaciContext):
elif isinstance(c, (IUPACParser.SaciContext, GWBParser.SaciContext)):
recipe += [(x[0], IUPACLexer.SAC) for x in tmp]
else:
recipe += tmp
Expand Down

0 comments on commit 5a7f82d

Please sign in to comment.