Skip to content

Commit

Permalink
updates to bel parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
wshayes committed Oct 29, 2020
1 parent 99bce14 commit c983a16
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bel/lang/ast.py
Expand Up @@ -759,7 +759,7 @@ def parse(self):
ValidationError(
type="Assertion",
severity="Error",
msg=f"Could not parse Assertion - bad relation: {self.args[1]}",
msg=f"Could not parse Assertion - bad relation? {self.args[1]}",
)
)
else:
Expand Down
15 changes: 9 additions & 6 deletions bel/lang/parse.py
Expand Up @@ -316,13 +316,16 @@ def find_functions(
"""

functions_list = bel.belspec.specifications.get_all_functions(version)
functions_list.sort(key=len)
functions_regex = "|".join(functions_list)

# matches start and everything but the following
potential_functions = re.compile(f"({functions_regex})\\(")
iter = re.finditer(potential_functions, assertion_str)
name_spans = [m.span(1) for m in iter]
iterator = re.finditer("([a-zA-Z]+)\(", assertion_str)

name_spans = []
for m in iterator:
matched = m.group(1)
if matched not in functions_list:
continue

name_spans.append(m.span(1))

# Filter quoted strings - can't have a relation in a quoted string
name_spans = [r for r in name_spans if not intersect(r[0], matched_quotes)]
Expand Down
20 changes: 10 additions & 10 deletions tests/lang/test_ast.py
Expand Up @@ -74,7 +74,7 @@ def test_ast_nested_orthologization():

ast.orthologize("TAX:10090").decanonicalize()

expected = ""
expected = "p(MGI:87986!Akt1) increases (p(MGI:87986!Akt1) increases p(MGI:95290!Egf))"

result = ast.to_string()
print("Result", result)
Expand Down Expand Up @@ -126,21 +126,20 @@ def test_get_species():
assert species == ["TAX:9606", "TAX:10090"]


@pytest.mark.skip("Figure out a better way to handle checks")
def test_get_orthologs():
"""Get all orthologs for any NSArgs in Assertion"""

assertion = AssertionStr(entire="p(MGI:Akt2) increases p(HGNC:391!AKT1)")

ast = bel.lang.ast.BELAst(assertion=assertion)

orthologs = ast.get_orthologs(simple_keys_flag=True)
orthologs = ast.get_orthologs()

print("Orthologs")
for ortholog in orthologs:
print(ortholog)

orthologs_str = json.dumps(orthologs, sort_keys=True)

expected = [
{
"TAX:10090": {"canonical": "EG:11652", "decanonical": "MGI:104874"},
Expand All @@ -154,6 +153,8 @@ def test_get_orthologs():
},
]

# orthologs - compares the string result of the NSVal object for the orthologs

assert orthologs == expected


Expand Down Expand Up @@ -197,7 +198,7 @@ def test_validate_missing_namespace():
def test_validate_strarg():

assertion = AssertionStr(subject='complex("missing")')
expected = ""
expected = "String argument not allowed as an optional or multiple argument. Probably missing a namespace."

ast = bel.lang.ast.BELAst(assertion=assertion)

Expand Down Expand Up @@ -410,17 +411,16 @@ def test_validation_tloc():
def test_validate_fus1():
"""Validate fus()"""

# HGNC:NPM isn't valid
# HGNC:NPM isn't valid - but it gets converted to updated entity
assertion = AssertionStr(subject='p(fus(HGNC:NPM, "1_117", HGNC:ALK, end))')
expected = "Unknown BEL Entity at argument position 0 for function fusion - cannot determine if correct entity type."

ast = bel.lang.ast.BELAst(assertion=assertion)

ast.validate()

print("Errors", ast.errors)

assert ast.errors[0].msg == expected
assert ast.errors == []


def test_validate_fus2():
Expand Down Expand Up @@ -628,7 +628,7 @@ def test_validate_complex_nsarg():
def test_validate_bad_relation():

assertion = AssertionStr(subject="p(HGNC:PTHLH) XXXincreases p(HGNC:PTHLH)")
expected = "Could not parse Assertion - bad relation: XXXincreases"
expected = "Could not parse Assertion - bad relation? XXXincreases"
ast = bel.lang.ast.BELAst(assertion=assertion)

ast.validate()
Expand All @@ -641,7 +641,7 @@ def test_validate_bad_relation():
def test_validate_bad_function():

assertion = AssertionStr(subject="ppp(HGNC:PTHLH)")
expected = "Could not parse Assertion - bad relation: XXXincreases"
expected = "Could not parse Assertion - bad relation? HGNC:9607!PTHLH"
ast = bel.lang.ast.BELAst(assertion=assertion)

ast.validate()
Expand Down

0 comments on commit c983a16

Please sign in to comment.