1
- '''
1
+ """
2
2
ASI2 Parser definition
3
- '''
3
+ """
4
4
5
5
from functools import reduce , total_ordering
6
6
from pyparsing import (Literal , nums , Word , Forward , Optional , Regex ,
7
7
infixNotation , delimitedList , opAssoc )
8
8
from pyvdrm .drm import AsiExpr , AsiBinaryExpr , AsiUnaryExpr , DRMParser
9
9
from pyvdrm .vcf import MutationSet
10
10
11
+
11
12
def maybe_foldl (func , noneable ):
12
- ''' Safely fold a function over a potentially empty list of
13
- potentially null values'''
13
+ """ Safely fold a function over a potentially empty list of
14
+ potentially null values"""
14
15
if noneable is None :
15
16
return None
16
17
clean = [x for x in noneable if x is not None ]
17
- if clean == [] :
18
+ if not clean :
18
19
return None
19
20
return reduce (func , clean )
20
21
22
+
21
23
def maybe_map (func , noneable ):
22
24
if noneable is None :
23
25
return None
@@ -29,14 +31,14 @@ def maybe_map(func, noneable):
29
31
if result is None :
30
32
continue
31
33
r_list .append (result )
32
- if r_list == [] :
34
+ if not r_list :
33
35
return None
34
36
return r_list
35
37
36
38
37
39
@total_ordering
38
40
class Score (object ):
39
- ''' Encapsulate a score and the residues that support it'''
41
+ """ Encapsulate a score and the residues that support it"""
40
42
41
43
residues = set ([])
42
44
score = None
@@ -68,32 +70,32 @@ def __bool__(self):
68
70
69
71
70
72
class Negate (AsiUnaryExpr ):
71
- ''' Unary negation of boolean child'''
73
+ """ Unary negation of boolean child"""
72
74
def __call__ (self , mutations ):
73
75
arg = self .children (mutations )
74
76
return Score (not arg .score , arg .residues )
75
77
76
78
77
79
class AndExpr (AsiExpr ):
78
- ''' Fold boolean AND on children'''
80
+ """ Fold boolean AND on children"""
79
81
80
82
def __call__ (self , mutations ):
81
83
scores = map (lambda f : f (mutations ), self .children [0 ])
82
84
scores = [Score (False , []) if s is None else s for s in scores ]
83
- if scores == [] :
85
+ if not scores :
84
86
raise ValueError
85
87
86
88
residues = set ([])
87
89
for s in scores :
88
- if s .score == False :
90
+ if not s .score :
89
91
return Score (False , [])
90
92
residues = residues | s .residues
91
93
92
94
return Score (True , residues )
93
95
94
96
95
97
class OrExpr (AsiBinaryExpr ):
96
- ''' Boolean OR on children (binary only)'''
98
+ """ Boolean OR on children (binary only)"""
97
99
98
100
def __call__ (self , mutations ):
99
101
arg1 , arg2 = self .children
@@ -111,9 +113,10 @@ def __call__(self, mutations):
111
113
112
114
113
115
class EqualityExpr (AsiExpr ):
114
- ''' ASI2 inequality expressions'''
116
+ """ ASI2 inequality expressions"""
115
117
116
118
def __init__ (self , label , pos , children ):
119
+ super ().__init__ (label , pos , children )
117
120
self .operation , limit = children
118
121
self .limit = int (limit )
119
122
@@ -132,10 +135,9 @@ def __repr__(self):
132
135
133
136
134
137
class ScoreExpr (AsiExpr ):
135
- ''' Score expressions propagate DRM scores'''
138
+ """ Score expressions propagate DRM scores"""
136
139
137
140
def __call__ (self , mutations ):
138
- operation , score = (None , None )
139
141
if len (self .children ) == 3 :
140
142
operation , minus , score = self .children
141
143
if minus != '-' :
@@ -162,23 +164,23 @@ def __repr__(self):
162
164
163
165
164
166
class ScoreList (AsiExpr ):
165
- ''' Lists of scores are either summed or maxed'''
167
+ """ Lists of scores are either summed or maxed"""
166
168
167
169
def __call__ (self , mutations ):
168
170
operation , * rest = self .children
169
171
if operation == 'MAX' :
170
172
return maybe_foldl (max , [f (mutations ) for f in rest ])
171
173
172
174
# the default operation is sum
173
- return maybe_foldl (lambda x ,y : x + y , [f (mutations ) for f in self .children ])
175
+ return maybe_foldl (lambda x , y : x + y , [f (mutations ) for f in self .children ])
174
176
175
177
176
178
class SelectFrom (AsiExpr ):
177
- ''' Return True if some number of mutations match'''
179
+ """ Return True if some number of mutations match"""
178
180
179
181
def typecheck (self , tokens ):
180
- # if type(tokens[0]) != EqualityExpr:
181
- # raise TypeError
182
+ # if type(tokens[0]) != EqualityExpr:
183
+ # raise TypeError()
182
184
pass
183
185
184
186
def __call__ (self , mutations ):
@@ -190,27 +192,27 @@ def __call__(self, mutations):
190
192
191
193
if operation (passing ):
192
194
return Score (True , maybe_foldl (
193
- lambda x ,y : x .residues .union (y .residues ), scored ))
195
+ lambda x , y : x .residues .union (y .residues ), scored ))
194
196
else :
195
197
return None
196
198
197
199
198
200
class AsiScoreCond (AsiExpr ):
199
- ''' Score condition'''
201
+ """ Score condition"""
200
202
201
203
label = "ScoreCond"
202
204
203
205
def __call__ (self , args ):
204
- ''' Score conditions evaluate a list of expressions and sum scores'''
205
- return maybe_foldl (lambda x ,y : x + y , map (lambda x : x (args ), self .children ))
206
+ """ Score conditions evaluate a list of expressions and sum scores"""
207
+ return maybe_foldl (lambda x , y : x + y , map (lambda x : x (args ), self .children ))
206
208
207
209
208
210
class AsiMutations (object ):
209
- ''' List of mutations given an ambiguous pattern'''
211
+ """ List of mutations given an ambiguous pattern"""
210
212
211
213
def __init__ (self , pos , label , args ):
212
- ''' Initialize set of mutations from a potentially ambiguous residue
213
- '''
214
+ """ Initialize set of mutations from a potentially ambiguous residue
215
+ """
214
216
if pos and label :
215
217
pass
216
218
self .mutations = MutationSet .from_string ('' .join (args ))
@@ -226,7 +228,7 @@ def __call__(self, env):
226
228
227
229
228
230
class ASI2 (DRMParser ):
229
- ''' ASI2 Syntax definition'''
231
+ """ ASI2 Syntax definition"""
230
232
231
233
def parser (self , rule ):
232
234
@@ -241,7 +243,7 @@ def parser(self, rule):
241
243
242
244
and_ = Literal ('AND' ).suppress ()
243
245
or_ = Literal ('OR' ).suppress ()
244
- #min_ = Literal('MIN')
246
+ # min_ = Literal('MIN')
245
247
246
248
notmorethan = Literal ('NOTMORETHAN' )
247
249
l_par = Literal ('(' ).suppress ()
@@ -256,8 +258,8 @@ def parser(self, rule):
256
258
not_ .setParseAction (Negate )
257
259
258
260
residue = mutation | not_
259
- # integer + l_par + not_ + Regex(r'[A-Z]+') + r_par
260
- # roll this next rule into the mutation object
261
+ # integer + l_par + not_ + Regex(r'[A-Z]+') + r_par
262
+ # roll this next rule into the mutation object
261
263
262
264
# Syntax of ASI expressions
263
265
excludestatement = except_ + residue
@@ -286,11 +288,11 @@ def parser(self, rule):
286
288
scoreitem = booleancondition + mapper + Optional (Literal ('-' )) + integer
287
289
scoreitem .setParseAction (ScoreExpr )
288
290
scorelist = max_ + l_par + delimitedList (scoreitem ) + r_par | \
289
- delimitedList (scoreitem )
291
+ delimitedList (scoreitem )
290
292
scorelist .setParseAction (ScoreList )
291
293
292
294
scorecondition = Literal ('SCORE FROM' ).suppress () + \
293
- l_par + delimitedList (scorelist ) + r_par
295
+ l_par + delimitedList (scorelist ) + r_par
294
296
295
297
scorecondition .setParseAction (AsiScoreCond )
296
298
0 commit comments