/
plugins.py
296 lines (263 loc) · 11.1 KB
/
plugins.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
from django.core.urlresolvers import reverse
from projectroles.constants import get_sodar_constants
from projectroles.plugins import ProjectAppPluginPoint
from bgjobs.plugins import BackgroundJobsPluginPoint
from .models import (
Case,
CASE_STATUS_CHOICES,
SmallVariantComment,
SmallVariantFlags,
ExportFileBgJob,
ExportProjectCasesFileBgJob,
CaddSubmissionBgJob,
SpanrSubmissionBgJob,
DistillerSubmissionBgJob,
ComputeProjectVariantsStatsBgJob,
FilterBgJob,
ProjectCasesFilterBgJob,
SyncCaseListBgJob,
ImportVariantsBgJob,
CaseComments,
ClearExpiredExportedFilesBgJob,
ClearInactiveVariantSetsBgJob,
ClearOldKioskCasesBgJob,
RefreshSmallVariantSummaryBgJob,
)
from .urls import urlpatterns
from .templatetags.variants_tags import case_status_to_color
# Global SODAR constants
SODAR_CONSTANTS = get_sodar_constants()
class ProjectAppPlugin(ProjectAppPluginPoint):
"""Plugin for registering app with Projectroles"""
name = "variants"
title = "Cases"
urls = urlpatterns
# ...
icon = "hospital-o"
entry_point_url_id = "variants:case-list"
description = "Cases"
#: Required permission for accessing the app
app_permission = "variants.view_data"
#: Enable or disable general search from project title bar
search_enable = True
#: List of search object types for the app
search_types = ["case"]
#: Search results template
search_template = "variants/_search_results.html"
#: App card template for the project details page
details_template = "variants/_details_card.html"
#: App card title for the project details page
details_title = "Cases Overview (top 5 most recently updated)"
#: Position in plugin ordering
plugin_ordering = 10
#: The user settings for this app.
app_settings = {
"umd_predictor_api_token": {
"scope": SODAR_CONSTANTS["APP_SETTING_SCOPE_USER"],
"type": "STRING", # STRING/INTEGER/BOOLEAN
"default": "",
"label": "UMD Predictor API Token",
"placeholder": "paste your API token here",
"description": (
"In order to use the UMD score, you will need to create an account in the UMD Predictor site. "
"Afterwards, you can obtain your token from your account settings page."
),
},
"ga4gh_beacon_network_widget_enabled": {
"scope": SODAR_CONSTANTS["APP_SETTING_SCOPE_USER"],
"type": "BOOLEAN", # STRING/INTEGER/BOOLEAN
"default": False,
"label": "GA4GH Beacon Network Widget",
"description": (
"Enable GA4GH beacon widget. Please note that your variant query will currently be "
"sent to all beacons on the network."
),
},
"latest_version_seen_changelog": {
"scope": SODAR_CONSTANTS["APP_SETTING_SCOPE_USER"],
"type": "STRING",
"default": "",
"label": "Changelog seen in version",
},
"user_defined_tags": {
"scope": SODAR_CONSTANTS["APP_SETTING_SCOPE_PROJECT"],
"type": "STRING",
"default": "",
"label": "User defined tags to attach to cases",
"description": (
"Define tags to attach to a case. Deleted tags will stay attached to "
"the case until the tags in the case are updated. To add a tag, take away "
"the focus of the input field or hit <kbd>Enter</kbd>. <kbd>;</kbd> is "
"the separator and will separate after the tag is entered."
),
},
"disable_pedigree_sex_check": {
"scope": SODAR_CONSTANTS["APP_SETTING_SCOPE_PROJECT"],
"type": "BOOLEAN",
"default": False,
"label": "Disable pedigree sex check",
"description": (
"Disable sex check in pedigree. This way, no warning will be displayed when the "
"reported sex in the pedigree doesn't match the molecular sex signature. "
"Use this if the sex is unknown."
),
},
}
#: Additional columns to display for the projects.
project_list_columns = {
"states": {
"title": "States",
"width": 75,
"description": "Distribution of case states",
"align": "center",
"active": True,
},
"cases": {
"title": "Cases",
"width": 75,
"description": "Number of cases in this project",
"align": "right",
"active": True,
},
"donors": {
"title": "Donors",
"width": 75,
"description": "Number of donors in this project",
"align": "right",
"active": True,
},
"actions": {
"title": "Action",
"width": 75,
"description": "Execute action",
"align": "center",
"active": True,
},
}
def get_project_list_value(self, column_id, project, user):
if column_id == "cases":
return Case.objects.filter(project=project).count()
elif column_id == "donors":
return sum((len(c.pedigree) for c in Case.objects.filter(project=project)))
elif column_id == "states":
return self._get_state_bar_html(project)
elif column_id == "actions":
return self._get_action_buttons(project)
else:
return "-"
def _get_state_bar_html(self, project):
counts = {key: 0 for key, _ in CASE_STATUS_CHOICES}
for case in Case.objects.filter(project=project):
counts[case.status] = counts.get(case.status, 0) + 1
total_count = sum(counts.values())
total_width = 70
arr = [
(
'<div style="width: {total_width}px; height: 1em; margin-top: .1em; margin-bottom: .1em; '
'white-space: nowrap;" data-toggle="tooltip" title="initial: %(initial)d, active: %(active)d, '
"closed (unsolved): %(closed-unsolved)d, closed (uncertain): %(closed-uncertain)d, "
'closed (solved): %(closed-solved)d">'
)
% {"total_width": total_width, **counts}
]
width_sum = 0
statuses = [k for k, _ in CASE_STATUS_CHOICES if counts.get(k)] # statuses with counts >0
if total_count:
for i, key in enumerate(statuses):
if i + 1 < len(statuses):
width = int(total_width * (counts.get(key, 0) / total_count))
width_sum += width
else:
width = total_width - width_sum
arr.append(
'<div style="width:%dpx; height: 100%%; display: inline-block" class="bg-%s"></div>'
% (width, case_status_to_color(key))
)
else:
arr.append(
'<div style="width:%dpx; height: 100%%; display: inline-block" class="bg-%s"></div>'
% (total_width, case_status_to_color("initial"))
)
arr.append("</div>")
return "".join(arr)
def _get_action_buttons(self, project):
tpl = """
<a href="%s" title="joint filtration " class="btn btn-primary sodar-list-btn sodar-ss-irods-btn">
<i class="fa fa-filter"></i>
</a>
"""
url = reverse("variants:project-cases-filter", kwargs={"project": project.sodar_uuid})
return tpl % url
def get_statistics(self):
return {
"case_count": {"label": "Cases", "value": Case.objects.all().count()},
"donor_count": {
"label": "Donors",
"value": sum((len(c.pedigree) for c in Case.objects.all())),
},
}
def search(self, search_term, user, search_type=None, keywords=None):
"""
Return app items based on a search term, user, optional type and
optional keywords
:param search_term: String
:param user: User object for user initiating the search
:param search_type: String
:param keywords: List (optional)
:return: Dict
"""
items = []
if not search_type:
cases = Case.objects.find(search_term, keywords)
items = [case for case in cases if user.has_perm("variants.view_data", case.project)]
items.sort(key=lambda x: x.name.lower())
elif search_type == "case":
items = Case.objects.find(search_term, keywords).order_by("name")
return {"all": {"title": "Cases", "search_types": ["case"], "items": items}}
def get_extra_data_link(self, extra_data, name):
"""Return link for the given label that started with ``"extra-"``."""
if name == "extra-flag_values":
return extra_data["flag_values"]
else:
return "(unknown %s)" % name
def get_object_link(self, model_str, uuid):
"""
Return URL for referring to a object used by the app, along with a
label to be shown to the user for linking.
:param model_str: Object class (string)
:param uuid: sodar_uuid of the referred object
:return: Dict or None if not found
"""
obj = self.get_object(eval(model_str), uuid)
if isinstance(obj, SmallVariantComment):
return {"url": obj.get_absolute_url(), "label": obj.shortened_text()}
elif isinstance(obj, SmallVariantFlags):
return {"url": obj.get_absolute_url(), "label": obj.human_readable()}
elif isinstance(obj, CaseComments):
return {"url": obj.get_absolute_url(), "label": obj.shortened_text()}
elif isinstance(obj, Case):
return {"url": obj.get_absolute_url(), "label": obj.name}
return None
class BackgroundJobsPlugin(BackgroundJobsPluginPoint):
"""Plugin for registering background jobs with ``bgjobs`` app."""
#: Slug used in URLs and similar places.
name = "variants"
#: Human-readable title.
title = "Variants Background Jobs"
#: Return name-to-class mapping for background job class specializations.
job_specs = {
ExportFileBgJob.spec_name: ExportFileBgJob,
CaddSubmissionBgJob.spec_name: CaddSubmissionBgJob,
SpanrSubmissionBgJob.spec_name: SpanrSubmissionBgJob,
DistillerSubmissionBgJob.spec_name: DistillerSubmissionBgJob,
ComputeProjectVariantsStatsBgJob.spec_name: ComputeProjectVariantsStatsBgJob,
ExportProjectCasesFileBgJob.spec_name: ExportProjectCasesFileBgJob,
FilterBgJob.spec_name: FilterBgJob,
ProjectCasesFilterBgJob.spec_name: ProjectCasesFilterBgJob,
SyncCaseListBgJob.spec_name: SyncCaseListBgJob,
ImportVariantsBgJob.spec_name: ImportVariantsBgJob,
ClearExpiredExportedFilesBgJob.spec_name: ClearExpiredExportedFilesBgJob,
ClearInactiveVariantSetsBgJob.spec_name: ClearInactiveVariantSetsBgJob,
ClearOldKioskCasesBgJob.spec_name: ClearOldKioskCasesBgJob,
RefreshSmallVariantSummaryBgJob.spec_name: RefreshSmallVariantSummaryBgJob,
}