-
Notifications
You must be signed in to change notification settings - Fork 3
/
filters.py
313 lines (264 loc) · 10.2 KB
/
filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from models import MC3Mutation, DrugGroup, Drug, Disease
from models import Cancer
from models import Mutation
from models import Site
from models import The1000GenomesMutation
from models import ExomeSequencingMutation
from models import ClinicalData
from database import has_or_any
from helpers.filters import Filter
from helpers.widgets import FilterWidget
class FiltersData:
"""State transfer object AsyncFiltersHandler from filters.js"""
def __init__(self, filter_manager):
from flask import request
self.query = filter_manager.url_string() or ''
self.expanded_query = filter_manager.url_string(expanded=True) or ''
self.checksum = request.args.get('checksum', '')
self.dynamic_widgets = ''
def to_json(self):
return self.__dict__
class ProteinFiltersData(FiltersData):
def __init__(self, filter_manager, protein):
super().__init__(filter_manager)
from flask import render_template
self.dynamic_widgets = render_template(
'widgets/widget_list.html',
widgets=create_dataset_specific_widgets(protein, filter_manager.filters),
collapse=True
)
def populations_labels(populations):
return [
population_name + ' (' + field_name[4:].upper() + ')'
for field_name, population_name
in populations.items()
]
class SourceDependentFilter(Filter):
def __init__(self, *args, **kwargs):
self.source = kwargs.pop('source')
super().__init__(*args, **kwargs)
@property
def visible(self):
return self.manager.get_value('Mutation.sources') == self.source
class MutationDetailsFilter(SourceDependentFilter):
"""Mutation details restrict returned mutations to those
which have at least on MutationDetails passing given criteria.
Example mutation details attributes include:
disease_name, cancer_type, population_name and so on
As there may be hundreds of mutations per protein, to improve speed
of filtering, the MutationDetails filters must be defined in a way
which allows conversion to database-side SQL 'where' clauses.
This means that 'as_sqlalchemy' will be set to True by default and
it's strongly discouraged to turned it off; Still, one can overwrite
as_sqlalchemy with a function defining custom sqlalchemy filter.
Also the filters should be constructed in a way that the default
value is equivalent to having the filter disabled. If it is not
possible to achieve, one need to provide 'skip_if_default=False'
"""
def __init__(
self, target_details_class, attribute,
nullable=False, as_sqlalchemy=True, skip_if_default=True,
**kwargs
):
target = [Mutation, target_details_class]
super().__init__(
target, attribute,
nullable=nullable, as_sqlalchemy=as_sqlalchemy, skip_if_default=skip_if_default,
**kwargs
)
def source_filter_to_sqlalchemy(source_filter, target):
"""Adapt mutation source filter to SQLAlchemy clause (for use in mutation query)"""
return source_to_sa_filter(source_filter.value, target)
def source_to_sa_filter(source_name, target=Mutation):
if source_name == 'user':
return True
field_name = Mutation.source_fields[source_name]
field = getattr(target, field_name)
return has_or_any(field)
class UserMutations:
pass
def create_dataset_labels():
# map dataset display names to dataset names
dataset_labels = {
dataset.name: dataset.display_name
for dataset in Mutation.source_specific_data
}
# hide user's mutations in dataset choice
# (there is separate widget for that, shown only if there are any user's datasets)
dataset_labels['user'] = None
return dataset_labels
class CachedQueries:
def __init__(self):
self.reload()
def reload(self):
"""Should be called after each cancer and public-dataset addition or change
(It should not happen during normal service, only after migrations and during tests)
"""
self.drug_groups = sorted([group.name for group in DrugGroup.query])
self.all_disease_names = sorted([disease.name for disease in Disease.query], key=str.lower)
self.all_cancer_codes_mc3 = [cancer.code for cancer in Cancer.query]
self.all_cancer_names = {
cancer.code: '%s (%s)' % (cancer.name, cancer.code)
for cancer in Cancer.query
}
self.dataset_labels = create_dataset_labels()
cached_queries = CachedQueries()
def common_filters(
protein,
default_source='MC3',
source_nullable=False,
custom_datasets_ids=[]
):
return [
Filter(
Mutation, 'sources', comparators=['in'],
choices=list(Mutation.visible_fields.keys()),
default=default_source, nullable=source_nullable,
as_sqlalchemy=source_filter_to_sqlalchemy
),
Filter(
UserMutations, 'sources', comparators=['in'],
choices=list(custom_datasets_ids),
default=None, nullable=True
),
Filter(
Mutation, 'is_ptm', comparators=['eq'],
is_attribute_a_method=True
),
Filter(
Drug, 'groups.name', comparators=['in'],
nullable=False,
choices=cached_queries.drug_groups,
default=['approved'],
multiple='all',
as_sqlalchemy=True
),
Filter(
Site, 'type', comparators=['in'],
choices=Site.types,
as_sqlalchemy=True
)
] + source_dependent_filters(protein)
def source_dependent_filters(protein=None):
if protein:
cancer_codes_mc3 = protein.cancer_codes(MC3Mutation)
disease_names = protein.disease_names
else:
cancer_codes_mc3 = cached_queries.all_cancer_codes_mc3
disease_names = cached_queries.all_disease_names
# Python 3.4: cast keys() to list
populations_1kg = list(The1000GenomesMutation.populations.values())
populations_esp = list(ExomeSequencingMutation.populations.values())
significances = list(ClinicalData.significance_codes.keys())
return [
MutationDetailsFilter(
MC3Mutation, 'mc3_cancer_code',
comparators=['in'],
choices=cached_queries.all_cancer_codes_mc3,
default=cancer_codes_mc3,
source='MC3',
multiple='any',
),
MutationDetailsFilter(
The1000GenomesMutation, 'populations_1KG',
comparators=['in'],
choices=populations_1kg,
default=populations_1kg,
source='1KGenomes',
multiple='any',
),
MutationDetailsFilter(
ExomeSequencingMutation, 'populations_ESP6500',
comparators=['in'],
choices=populations_esp,
default=populations_esp,
source='ESP6500',
multiple='any',
),
MutationDetailsFilter(
ClinicalData, 'sig_code',
comparators=['in'],
choices=significances,
default=significances,
source='ClinVar',
multiple='any',
),
MutationDetailsFilter(
ClinicalData, 'disease_name',
comparators=['in'],
choices=disease_names,
default=disease_names,
source='ClinVar',
multiple='any',
)
]
def create_dataset_specific_widgets(protein, filters_by_id, population_widgets=True):
cancer_codes_mc3 = protein.cancer_codes(MC3Mutation) if protein else []
widgets = [
FilterWidget(
'Cancer type', 'checkbox_multiple',
filter=filters_by_id['Mutation.mc3_cancer_code'],
labels=cached_queries.all_cancer_names,
choices=cancer_codes_mc3,
all_selected_label='Any cancer type'
),
FilterWidget(
'Clinical significance', 'checkbox_multiple',
filter=filters_by_id['Mutation.sig_code'],
all_selected_label='Any clinical significance class',
labels=ClinicalData.significance_codes.values()
),
FilterWidget(
'Disease name', 'checkbox_multiple',
filter=filters_by_id['Mutation.disease_name'],
all_selected_label='Any disease name'
)
]
if population_widgets:
widgets += [
FilterWidget(
'Ethnicity', 'checkbox_multiple',
filter=filters_by_id['Mutation.populations_1KG'],
labels=populations_labels(The1000GenomesMutation.populations),
all_selected_label='Any ethnicity'
),
FilterWidget(
'Ethnicity', 'checkbox_multiple',
filter=filters_by_id['Mutation.populations_ESP6500'],
labels=populations_labels(ExomeSequencingMutation.populations),
all_selected_label='Any ethnicity'
)
]
return widgets
def create_widgets(protein, filters_by_id, custom_datasets_names=None):
"""Widgets to be displayed on a bar above visualisation."""
return {
'dataset': FilterWidget(
'Mutation dataset', 'radio',
filter=filters_by_id['Mutation.sources'],
labels=cached_queries.dataset_labels,
class_name='dataset-widget'
),
'custom_dataset': FilterWidget(
'Custom mutation dataset', 'radio',
filter=filters_by_id['UserMutations.sources'],
labels=custom_datasets_names
),
'dataset_specific': create_dataset_specific_widgets(protein, filters_by_id),
'is_ptm': FilterWidget(
'PTM mutations only', 'checkbox',
filter=filters_by_id['Mutation.is_ptm'],
disabled_label='all mutations',
labels=['PTM mutations only']
),
'ptm_type': FilterWidget(
'Type of PTM site', 'radio',
filter=filters_by_id['Site.type'],
disabled_label='Any site'
),
'other': [FilterWidget(
'Drug group', 'checkbox_multiple',
filter=filters_by_id['Drug.groups.name'],
labels=[group.title() for group in cached_queries.drug_groups],
)]
}