Skip to content

Commit

Permalink
Replace message on ParseException.
Browse files Browse the repository at this point in the history
Increment version number.
  • Loading branch information
donkirkby committed Jan 17, 2018
1 parent bc65b7d commit d5da55b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ The `vcf` module has basic helper functions for construction lists of mutation
calls:

```
from pyvdrm.vcf import call_mutations
from pyvdrm.vcf import VariantCalls
from pyvdrm.asi2 import ASI2
ref = "APITAYAQQTRGLLGCIITSLTGRD"
sample = "APITAYAQQTRGLLTCIITSLTGRD"
calls = VariantCalls(reference="APITAYAQQTRGLLGCIITSLTGRD",
sample= "APITAYAQQTRGLLTCIITSLTGRD")
# mutation G15T ------------------------------^
score = rule(call_mutations(ref, sample))
rule = ASI2('SCORE FROM ( G15T => 5 )')
score = rule(calls)
print(score) # => 5
```
8 changes: 6 additions & 2 deletions pyvdrm/asi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from functools import reduce, total_ordering
from pyparsing import (Literal, nums, Word, Forward, Optional, Regex,
infixNotation, delimitedList, opAssoc)
infixNotation, delimitedList, opAssoc, ParseException)
from pyvdrm.drm import AsiExpr, AsiBinaryExpr, AsiUnaryExpr, DRMParser
from pyvdrm.vcf import MutationSet

Expand Down Expand Up @@ -299,4 +299,8 @@ def parser(self, rule):

statement = booleancondition | scorecondition

return statement.parseString(rule)
try:
return statement.parseString(rule)
except ParseException as ex:
ex.msg = 'Error in ASI2: ' + ex.markInputline()
raise
8 changes: 6 additions & 2 deletions pyvdrm/hcvr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from functools import reduce, total_ordering
from pyparsing import (Literal, nums, Word, Forward, Optional, Regex,
infixNotation, delimitedList, opAssoc, alphas)
infixNotation, delimitedList, opAssoc, alphas, ParseException)
from pyvdrm.drm import AsiExpr, AsiBinaryExpr, AsiUnaryExpr, DRMParser
from pyvdrm.vcf import MutationSet

Expand Down Expand Up @@ -329,4 +329,8 @@ def parser(self, rule):

statement = booleancondition | scorecondition

return statement.parseString(rule)
try:
return statement.parseString(rule)
except ParseException as ex:
ex.msg = 'Error in HCVR: ' + ex.markInputline()
raise
26 changes: 26 additions & 0 deletions pyvdrm/tests/test_asi2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import unittest

from pyparsing import ParseException

from pyvdrm.asi2 import ASI2, AsiMutations, Score
from pyvdrm.vcf import Mutation, MutationSet, VariantCalls

Expand Down Expand Up @@ -99,6 +102,29 @@ def test_score_from_exactly(self):
score = rule(VariantCalls("2T 7Y 1G"))
self.assertEqual(0, score)

def test_parse_exception(self):
expected_error_message = (
"Error in ASI2: SCORE FROM ( 10R => 2>!<;0 ) (at char 21), (line:1, col:22)")

with self.assertRaises(ParseException) as context:
ASI2("SCORE FROM ( 10R => 2;0 )")

self.assertEqual(expected_error_message, str(context.exception))

def test_parse_exception_multiline(self):
rule = """\
SCORE FROM (
10R => 2;0
)
"""
expected_error_message = (
"Error in ASI2: 10R => 2>!<;0 (at char 25), (line:2, col:13)")

with self.assertRaises(ParseException) as context:
ASI2(rule)

self.assertEqual(expected_error_message, str(context.exception))


class TestActualRules(unittest.TestCase):
def test_hivdb_rules_parse(self):
Expand Down
29 changes: 28 additions & 1 deletion pyvdrm/tests/test_hcvr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys
import unittest

from pyparsing import ParseException

from pyvdrm.hcvr import HCVR, AsiMutations, Score
from pyvdrm.vcf import Mutation, MutationSet, VariantCalls

Expand Down Expand Up @@ -120,6 +122,30 @@ def test_score_comment(self):
self.assertEqual(result.score, 3)
self.assertTrue("flag1 with_space" in result.flags)

def test_parse_exception(self):
expected_error_message = (
"Error in HCVR: SCORE FROM ( 10R => 2>!<;0 ) (at char 21), (line:1, col:22)")

with self.assertRaises(ParseException) as context:
HCVR("SCORE FROM ( 10R => 2;0 )")

self.assertEqual(expected_error_message, str(context.exception))

def test_parse_exception_multiline(self):
rule = """\
SCORE FROM (
10R => 2;0
)
"""
expected_error_message = (
"Error in HCVR: 10R => 2>!<;0 (at char 25), (line:2, col:13)")

with self.assertRaises(ParseException) as context:
HCVR(rule)

self.assertEqual(expected_error_message, str(context.exception))


class TestActualRules(unittest.TestCase):
def test_hivdb_rules_parse(self):
for line in open("pyvdrm/tests/HIVDB.rules"):
Expand Down Expand Up @@ -210,5 +236,6 @@ def test_true_positive(self):
expected_repr = "[Mutation('Q54H'), Mutation('444H')]"
self.assertEqual(expected_repr, repr(sorted(dtree.residues)))


if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='pyvdrm',
version='0.1.1',
version='0.2.0',
description='',

url='',
Expand Down

0 comments on commit d5da55b

Please sign in to comment.