-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.py
692 lines (526 loc) · 22.5 KB
/
schema.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# Schema for firecloud api to graphql
import json
from collections import OrderedDict
import graphene
from graphene.types import generic
import utilities
import query_firecloud
## Temp utility schema functions ##
def filter_noauth(return_data,token,data):
# if a valid token is not provided, filter out those items that
# we would never want to serve without authentication
FILTER_KEYS = data.participant_metadata_columns+data.sample_metadata_columns
SUBSTITUTE = "0"
# check if the token is for a valid user
if data.valid_token(token):
return return_data
def filter_metadata(return_data):
if not isinstance(return_data, OrderedDict):
return None
for key in return_data.keys():
if isinstance(return_data[key], OrderedDict):
filter_metadata(return_data[key])
elif isinstance(return_data[key], list):
for item in return_data[key]:
filter_metadata(item)
elif key in FILTER_KEYS or "metadataValue" in key:
return_data[key]=SUBSTITUTE
filter_metadata(return_data)
def add_attributes(instance, keys, values):
for key in keys:
try:
setattr(instance, key, values[key])
except KeyError:
continue
## Portal API ##
class Custom(graphene.ObjectType):
pass
class Sort(graphene.String):
pass
class FiltersArgument(generic.GenericScalar):
pass
class Count(graphene.ObjectType):
programs = graphene.String()
projects = graphene.String()
participants = graphene.String()
samples = graphene.String()
dataFormats = graphene.String()
totalData = graphene.String()
rawFiles = graphene.String()
processedFiles = graphene.String()
class User(graphene.ObjectType):
username = graphene.String()
class Program(graphene.ObjectType):
name = graphene.String()
program_id = graphene.String()
id = graphene.String()
def resolve_program_id(self, info):
return self.name
def resolve_id(self, info):
return self.name
class DataCategories(graphene.ObjectType):
case_count = graphene.Int()
file_count = graphene.Int()
data_category = graphene.String()
class ExperimentalStrategies(graphene.ObjectType):
case_count = graphene.Int()
file_count = graphene.Int()
experimental_strategy = graphene.String()
class Summary(graphene.ObjectType):
case_count = graphene.Int()
file_count = graphene.Int()
file_size = graphene.Float()
data_categories = graphene.List(DataCategories)
experimental_strategies = graphene.List(ExperimentalStrategies)
class Project(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
project_id = graphene.String()
name = graphene.String()
program = graphene.Field(Program)
summary = graphene.Field(Summary)
primary_site = graphene.List(graphene.String)
class ProgramNode(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
name = graphene.String()
summary = graphene.Field(Summary)
primary_site = graphene.List(graphene.String)
class Demographic(graphene.ObjectType):
id = graphene.String()
class CaseSample(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
sample_id = graphene.String()
class Bucket(graphene.ObjectType):
doc_count = graphene.Int()
key = graphene.String()
key_as_string = graphene.String()
def resolve_key_as_string(self, info):
return str(key)
class Stats(graphene.ObjectType):
max = graphene.Int()
min = graphene.Int()
count = graphene.Int()
class Aggregations(graphene.ObjectType):
buckets = graphene.List(Bucket)
stats = graphene.Field(Stats)
class AggregationAnnotation(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
metadataKey = graphene.String()
metadataTitle = graphene.String()
metadataType = graphene.String()
metadataValue = graphene.Field(Aggregations)
class AggregationConnection(graphene.relay.Connection):
class Meta:
node = AggregationAnnotation
total = graphene.Int()
def resolve_total(self, info):
return len(self.iterable)
class MetadataAggregations(graphene.ObjectType):
hits = graphene.relay.ConnectionField(AggregationConnection,
first=graphene.Int(),
offset=graphene.Int(),
score=graphene.String(),
sort=graphene.List(Sort),
filters=FiltersArgument())
class MetadataCaseAnnotation(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
metadataKey = graphene.String()
metadataValue = graphene.String()
class MetadataCaseConnection(graphene.relay.Connection):
class Meta:
node = MetadataCaseAnnotation
total = graphene.Int()
def resolve_total(self, info):
return len(self.iterable)
class MetadataCase(graphene.ObjectType):
hits = graphene.relay.ConnectionField(MetadataCaseConnection,
first=graphene.Int(),
offset=graphene.Int(),
score=graphene.String(),
sort=graphene.List(Sort),
filters=FiltersArgument())
metadata_count = graphene.String()
class MetadataSampleAnnotation(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
metadataKey = graphene.String()
metadataValue = graphene.String()
class MetadataSampleConnection(graphene.relay.Connection):
class Meta:
node = MetadataSampleAnnotation
total = graphene.Int()
def resolve_total(self, info):
return len(self.iterable)
class MetadataSample(graphene.ObjectType):
hits = graphene.relay.ConnectionField(MetadataSampleConnection,
first=graphene.Int(),
offset=graphene.Int(),
score=graphene.String(),
sort=graphene.List(Sort),
filters=FiltersArgument())
metadata_count = graphene.String()
class FileCase(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
case_id = graphene.String()
project = graphene.Field(Project)
metadataCase = graphene.Field(MetadataCase)
demographic = graphene.Field(Demographic)
primary_site = graphene.String()
samples = graphene.List(CaseSample)
class FileCaseConnection(graphene.relay.Connection):
class Meta:
node = FileCase
total = graphene.Int()
def resolve_total(self, info):
return len(self.iterable)
class FileCases(graphene.ObjectType):
hits = graphene.relay.ConnectionField(FileCaseConnection,
first=graphene.Int(),
offset=graphene.Int(),
sort=graphene.List(Sort),
filters=FiltersArgument())
class File(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
name = graphene.String()
participant = graphene.String()
sample = graphene.String()
access = graphene.String()
file_size = graphene.Float()
data_category = graphene.String()
data_version = graphene.String()
data_merged = graphene.String()
data_format = graphene.String()
platform = graphene.String()
experimental_strategy = graphene.String()
file_name = graphene.String()
generic_file_name = graphene.String()
file_url = graphene.String()
cases = graphene.Field(FileCases)
file_id = graphene.String()
type = graphene.String()
def resolve_file_id(self, info):
return self.id
def resolve_type(self, info):
return self.data_format
def resolve_name(self, info):
return self.generic_file_name
def resolve_file_name(self, info):
return self.generic_file_name
class ProgramConnection(graphene.relay.Connection):
class Meta:
node = ProgramNode
total = graphene.Int()
def resolve_total(self, info):
return len(self.iterable)
class ProjectConnection(graphene.relay.Connection):
class Meta:
node = Project
total = graphene.Int()
def resolve_total(self, info):
return len(self.iterable)
class FileConnection(graphene.relay.Connection):
class Meta:
node = File
total = graphene.Int()
def resolve_total(self, info):
return info.context.get("files_hits_total")
class FileAggregations(graphene.ObjectType):
data_category = graphene.Field(Aggregations)
data_version = graphene.Field(Aggregations)
data_merged = graphene.Field(Aggregations)
file_size = graphene.Field(Aggregations)
experimental_strategy = graphene.Field(Aggregations)
data_format = graphene.Field(Aggregations)
platform = graphene.Field(Aggregations)
access = graphene.Field(Aggregations)
cases__project__project_id = graphene.Field(Aggregations)
cases__primary_site = graphene.Field(Aggregations)
class Files(graphene.ObjectType):
hits = graphene.relay.ConnectionField(FileConnection,
first=graphene.Int(),
offset=graphene.Int(),
score=graphene.String(),
sort=graphene.List(Sort),
filters=FiltersArgument())
facets = graphene.types.json.JSONString(filters=FiltersArgument(),
facets=graphene.List(graphene.String))
aggregations = graphene.Field(FileAggregations,
filters=FiltersArgument(),
aggregations_filter_themselves=graphene.Boolean())
def resolve_hits(self, info, first=None, score=None, offset=None, sort=None, filters=None):
all_files = info.context.get("user_data").get_current_files()
filtered_files = utilities.filter_hits(all_files, filters, "files", info.context.get("user_data").project_access)
# set total for hits
info.context["files_hits_total"] = len(filtered_files)
# allow all users to filter all files (this requires all visible categorical metadata to be public access)
#all_files = info.context.get("user_data").get_current_files(filters=info.context.get("user_data").get_project_access_filters())
#filtered_files = utilities.filter_hits(all_files, filters, "files", info.context.get("user_data").project_access)
sorted_files = utilities.sort_hits(filtered_files, sort)
return utilities.offset_hits(sorted_files, offset)
def resolve_aggregations(self, info, filters=None, aggregations_filter_themselves=None):
all_files = info.context.get("user_data").get_current_files()
filtered_files = utilities.filter_hits(all_files, filters, "files", info.context.get("user_data").project_access)
return info.context.get("user_data").get_file_aggregations(filtered_files)
def resolve_facets(self, info, filters=None, facets=None):
return info.context.get("user_data").get_facets()
class CaseAggregations(graphene.ObjectType):
metadataAggregations=graphene.Field(MetadataAggregations)
primary_site = graphene.Field(Aggregations)
project__project_id = graphene.Field(Aggregations)
project__program__name = graphene.Field(Aggregations)
class CaseAnnotation(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
annotation_id = graphene.String()
def resolve_annotation_id(self, info):
return self.id
class CaseAnnotationConnection(graphene.relay.Connection):
class Meta:
node = CaseAnnotation
total = graphene.Int()
def resolve_total(self, info):
return len(self.iterable)
class CaseAnnotations(graphene.ObjectType):
hits = graphene.relay.ConnectionField(CaseAnnotationConnection,
first=graphene.Int(),
offset=graphene.Int(),
score=graphene.String(),
sort=graphene.List(Sort),
filters=FiltersArgument())
def resolve_hits(self, info, first=None, score=None, offset=None, sort=None, filters=None):
# filters are not currently in use
return info.context.get("user_data").get_case_annotation()
class CaseFile(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
case_id = graphene.String()
experimental_strategy = graphene.String()
data_category = graphene.String()
data_version = graphene.String()
data_merged = graphene.String()
data_format = graphene.String()
platform = graphene.String()
access = graphene.String()
file_size = graphene.Float()
class CaseFileConnection(graphene.relay.Connection):
class Meta:
node = CaseFile
total = graphene.Int()
def resolve_total(self, info):
return len(self.iterable)
class CaseFiles(graphene.ObjectType):
hits = graphene.relay.ConnectionField(CaseFileConnection,
first=graphene.Int(),
offset=graphene.Int(),
sort=graphene.List(Sort),
filters=FiltersArgument())
class CaseSampleConnection(graphene.relay.Connection):
class Meta:
node = CaseSample
total = graphene.Int()
def resolve_total(self, info):
return len(self.iterable)
class CaseSamples(graphene.ObjectType):
hits = graphene.relay.ConnectionField(CaseSampleConnection,
first=graphene.Int(),
offset=graphene.Int(),
sort=graphene.List(Sort),
filters=FiltersArgument())
class Case(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
case_id = graphene.String()
primary_site = graphene.String()
submitter_id = graphene.String()
metadataCase = graphene.Field(MetadataCase)
demographic = graphene.Field(Demographic)
project = graphene.Field(Project)
summary = graphene.Field(Summary)
annotations = graphene.Field(CaseAnnotations)
files = graphene.Field(CaseFiles)
samples = graphene.Field(CaseSamples)
def resolve_submitter_id(self, info):
return self.case_id
class CaseConnection(graphene.relay.Connection):
class Meta:
node = Case
total = graphene.Int()
def resolve_total(self, info):
return info.context.get("cases_hits_total")
class Sample(graphene.ObjectType):
class Meta:
interfaces = (graphene.relay.Node,)
sample_id = graphene.String()
primary_site = graphene.String()
submitter_id = graphene.String()
metadataCase = graphene.Field(MetadataCase)
demographic = graphene.Field(Demographic)
project = graphene.Field(Project)
summary = graphene.Field(Summary)
annotations = graphene.Field(CaseAnnotations)
metadataSample = graphene.Field(MetadataSample)
files = graphene.Field(CaseFiles)
cases = graphene.Field(FileCases)
def resolve_submitter_id(self, info):
return self.sample_id
class SampleAggregations(graphene.ObjectType):
metadataAggregations=graphene.Field(MetadataAggregations)
primary_site = graphene.Field(Aggregations)
project__project_id = graphene.Field(Aggregations)
project__program__name = graphene.Field(Aggregations)
class SampleConnection(graphene.relay.Connection):
class Meta:
node = Sample
total = graphene.Int()
def resolve_total(self, info):
return info.context.get("samples_hits_total")
class RepositorySamples(graphene.ObjectType):
hits = graphene.relay.ConnectionField(SampleConnection,
first=graphene.Int(),
offset=graphene.Int(),
sort=graphene.List(Sort),
score=graphene.String(),
filters=FiltersArgument())
aggregations = graphene.Field(SampleAggregations,
filters=FiltersArgument(),
aggregations_filter_themselves=graphene.Boolean())
facets = graphene.types.json.JSONString(filters=FiltersArgument(),
facets=graphene.List(graphene.String))
def resolve_hits(self, info, first=None, score=None, offset=None, sort=None, filters=None):
all_samples = info.context.get("user_data").get_current_samples()
filtered_samples = utilities.filter_hits(all_samples, filters, "samples", info.context.get("user_data").project_access)
# set total for sample hits
info.context["samples_hits_total"]=len(filtered_samples)
all_samples = info.context.get("user_data").get_current_samples(filters=info.context.get("user_data").get_project_access_filters())
filtered_samples = utilities.filter_hits(all_samples, filters, "samples", info.context.get("user_data").project_access)
sorted_samples = utilities.sort_hits(filtered_samples, sort)
return utilities.offset_hits(sorted_samples, offset)
def resolve_aggregations(self, info, filters=None, aggregations_filter_themselves=None):
all_samples = info.context.get("user_data").get_current_samples()
filtered_samples = utilities.filter_hits(all_samples, filters, "samples", info.context.get("user_data").project_access)
sample_aggregations = info.context.get("user_data").get_sample_aggregations(filtered_samples)
return sample_aggregations
def resolve_facets(self, info, filters=None, facets=None):
return info.context.get("user_data").get_facets()
class RepositoryCases(graphene.ObjectType):
hits = graphene.relay.ConnectionField(CaseConnection,
first=graphene.Int(),
offset=graphene.Int(),
sort=graphene.List(Sort),
score=graphene.String(),
filters=FiltersArgument())
aggregations = graphene.Field(CaseAggregations,
filters=FiltersArgument(),
aggregations_filter_themselves=graphene.Boolean())
facets = graphene.types.json.JSONString(filters=FiltersArgument(),
facets=graphene.List(graphene.String))
def resolve_hits(self, info, first=None, score=None, offset=None, sort=None, filters=None):
all_cases = info.context.get("user_data").get_current_cases()
filtered_cases = utilities.filter_hits(all_cases, filters, "cases", info.context.get("user_data").project_access)
# set total for hits
info.context["cases_hits_total"] = len(filtered_cases)
# compute limited hits
all_cases = info.context.get("user_data").get_current_cases(filters=info.context.get("user_data").get_project_access_filters())
filtered_cases = utilities.filter_hits(all_cases, filters, "cases", info.context.get("user_data").project_access)
sorted_cases = utilities.sort_hits(filtered_cases, sort)
return utilities.offset_hits(sorted_cases, offset)
def resolve_aggregations(self, info, filters=None, aggregations_filter_themselves=None):
all_cases = info.context.get("user_data").get_current_cases()
filtered_cases = utilities.filter_hits(all_cases, filters, "cases", info.context.get("user_data").project_access)
case_aggregations = info.context.get("user_data").get_case_aggregations(filtered_cases,filters)
return case_aggregations
def resolve_facets(self, info, filters=None, facets=None):
return info.context.get("user_data").get_facets()
class Repository(graphene.ObjectType):
files = graphene.Field(Files)
cases = graphene.Field(RepositoryCases)
samples = graphene.Field(RepositorySamples)
def resolve_files(self, info):
return Files(self)
def resolve_cases(self, info):
return RepositoryCases(self)
def resolve_samples(self, info):
return RepositorySamples(self)
class ProjectAggregations(graphene.ObjectType):
primary_site = graphene.Field(Aggregations)
program__name = graphene.Field(Aggregations)
name = graphene.Field(Aggregations)
project_id = graphene.Field(Aggregations)
summary__experimental_strategies__experimental_strategy = graphene.Field(Aggregations)
summary__data_categories__data_category = graphene.Field(Aggregations)
class Projects(graphene.ObjectType):
aggregations = graphene.Field(ProjectAggregations,
filters=FiltersArgument(),
aggregations_filter_themselves=graphene.Boolean())
hits = graphene.relay.ConnectionField(ProjectConnection,
first=graphene.Int(),
offset=graphene.Int(),
sort=graphene.List(Sort),
filters=FiltersArgument())
def resolve_hits(self, info, first=None, offset=None, sort=None, filters=None):
projects = info.context.get("user_data").get_current_projects(filters)
filtered_projects = utilities.filter_hits(projects, filters, "projects", True)
return filtered_projects
def resolve_aggregations(self, info, filters=None, aggregations_filter_themselves=None):
projects = info.context.get("user_data").get_current_projects()
filtered_projects = utilities.filter_hits(projects, filters, "projects", True)
project_aggregations = info.context.get("user_data").get_project_aggregations(filtered_projects)
return project_aggregations
class Programs(graphene.ObjectType):
aggregations = graphene.Field(ProjectAggregations,
filters=FiltersArgument(),
aggregations_filter_themselves=graphene.Boolean())
hits = graphene.relay.ConnectionField(ProgramConnection,
first=graphene.Int(),
offset=graphene.Int(),
sort=graphene.List(Sort),
filters=FiltersArgument())
def resolve_hits(self, info, first=None, offset=None, sort=None, filters=None):
programs = info.context.get("user_data").get_current_programs(filters)
filtered_programs = utilities.filter_hits(programs, filters, "programs", True)
return filtered_programs
def resolve_aggregations(self, info, filters=None, aggregations_filter_themselves=None):
programs = info.context.get("user_data").get_current_programs()
filtered_programs = utilities.filter_hits(programs, filters, "programs", True)
program_aggregations = info.context.get("user_data").get_project_aggregations(filtered_programs)
return program_aggregations
class FileSize(graphene.ObjectType):
value = graphene.Float()
class CartSummaryAggs(graphene.ObjectType):
fs = graphene.Field(FileSize)
class CartSummary(graphene.ObjectType):
aggregations = graphene.Field(CartSummaryAggs,
filters=FiltersArgument())
def resolve_aggregations(self, info, filters=None):
return info.context.get("user_data").get_cart_file_size(filters=filters)
class Root(graphene.ObjectType):
user = graphene.Field(User)
count = graphene.Field(Count)
repository = graphene.Field(Repository)
projects = graphene.Field(Projects)
programs = graphene.Field(Programs)
cart_summary = graphene.Field(CartSummary)
def resolve_user(self, info):
return info.context.get("user_data").get_user()
def resolve_count(self, info):
return info.context.get("user_data").get_current_counts()
def resolve_repository(self, info):
return Repository(self)
def resolve_projects(self, info):
return Projects(self)
def resolve_programs(self, info):
return Programs(self)
def resolve_cart_summary(self, info):
return CartSummary(self)
class Query(graphene.ObjectType):
## Portal API ##
viewer = graphene.Field(Root)
node = graphene.relay.Node.Field()
def resolve_viewer(self, info):
return Root(self,info)