-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
1129 lines (914 loc) · 54.5 KB
/
database.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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Calls to obtain values from the data structures, populated by the local cache database
import sqlalchemy
import sys
import time
import threading
import utilities
import schema
RAW_FILE_TYPE = "rawFiles"
PROCESSED_FILE_TYPE = "processedFiles"
GENERIC_FILE_NAME_OFFSET = 1000000
CASE_DEFAULT_COLUMNS = set(['entity_participant_id', 'updated', 'participant_name', 'participant_id', 'id'])
SAMPLE_DEFAULT_COLUMNS = set(['sample', 'updated', 'participant', 'sample_id', 'id','project'])
# metadata keys to move to the front of the list to show on the site (in aggregations and as first in the tables)
METADATA_DEMOGRAPHICS_PROMOTE=["project","state","age","weight","race","ethnicity","diagnosis","alcohol","caffiene","smoking"]
METADATA_SAMPLES_PROMOTE=["time","week","fiber","fat","b12","carbs","protein","folate","calories","iron"]
# optional white list of metadata keys to use
METADATA_DEMOGRAPHICS_WHITELIST="demographic_metadata_whitelist.txt"
METADATA_SAMPLES_WHITELIST="sample_metadata_whitelist.txt"
class Cache(object):
def __init__(self):
self.expires={}
# expires every 3 months
self.expires_offset=60*60*24*30*3
self.lock=threading.Lock()
self.cache={}
def get_cache(self,cache_type,filters):
self.lock.acquire()
cache_name=cache_type+filters
#if ( self.expires.get(cache_name,0) + self.expires_offset ) > time.time() and cache_name in self.cache:
if cache_name in self.cache:
value = self.cache[cache_name]
self.lock.release()
have_lock=False
else:
have_lock=True
value = ""
return value, have_lock
def update_cache(self,cache_type,new_object,filters,have_lock):
if not have_lock:
self.lock.acquire()
cache_name=cache_type+filters
self.cache[cache_name]=new_object
self.expires[cache_name]=time.time()
self.lock.release()
# all data objects share the same cache
cache = Cache()
class Data(object):
def __init__(self):
# get the database environment variables
username, password, database = utilities.get_database_variables()
# set defaults for metadata names
self.sample_metadata_columns=[]
self.participant_metadata_columns=[]
# create a pool of connections, pre-ping to prevent stale connections
database_url = "mysql://{username}:{password}@localhost/{database}".format(username = username,
password = password, database = database)
try:
self.engine = sqlalchemy.create_engine(database_url, pool_size=32, pool_pre_ping=True)
except EnvironmentError as e:
print("Unable to connect to local database")
print("Database url {}".format(database_url))
sys.exit(e)
self.cache=cache
# set the no access group
self.no_access_group = "'None'"
# check for optional whitelist files
self.metadata_demographics_whitelist=utilities.read_whitelist(METADATA_DEMOGRAPHICS_WHITELIST)
self.metadata_samples_whitelist=utilities.read_whitelist(METADATA_SAMPLES_WHITELIST)
def query_database(self, query, fetchall=False, no_results=False):
# obtain connection from pool, run query
# then release connection back to pool
connection = self.engine.connect()
if no_results:
connection.execute(query)
connection.close()
return None
else:
results = connection.execute(query)
# if fetchall then get all results (to close cursor)
# and then close connection
# if not, then return connction so function can
# iterate on results and then close connection
if fetchall:
results = results.fetchall()
connection.close()
return results
else:
return connection, results
def add_token(self, email, token):
# add or update the token for the user to 48 hours from now plus 5 min (to allow for UI time diff from server)
expires = time.time()+2*24*60*60+5*60
db_results = self.query_database("UPDATE users SET token='{0}', expires='{1}' WHERE email='{2}'".format(token,expires,email), no_results=True)
def create_access_query_restriction(self, projects):
return " WHERE project in ({}) ".format(projects)
def set_project_access_filters(self, projects):
self.project_access_filters=self.create_access_query_restriction(projects)
if projects == self.no_access_group:
self.project_access = False
else:
self.project_access = True
def get_project_access_filters(self):
return self.project_access_filters
def valid_token(self, token):
access_groups = self.get_project_access(token)
if access_groups == self.no_access_group:
return False
else:
return True
def get_project_access(self, token):
db_results = self.query_database("SELECT email, expires, projects FROM users WHERE token='{}'".format(token), fetchall=True)
if db_results:
# check if the token has expired
current_time = time.time()
try:
expires = float(db_results[0][1])
except ValueError:
expires = current_time
if current_time > expires:
db_results = self.query_database("UPDATE users SET token='{0}', expires='{1}' WHERE email='{2}'".format("None","None",db_results[0][0]), no_results=True)
return self.no_access_group
elif current_time < expires:
return db_results[0][2]
else:
return self.no_access_group
else:
return self.no_access_group
def valid_user(self, email):
db_results = self.query_database("SELECT token FROM users WHERE email='{}'".format(email), fetchall=True)
if db_results:
return True
else:
return False
def get_current_project_info(self,filters=None):
query = "SELECT file_sample.id as file_id, file_sample.participant, " +\
"file_sample.file_size, file_sample.data_category, " +\
"file_sample.experimental_strategy, file_sample.project, project.id as project_id, project.primary_site, " +\
"participant.id as participant_id, project.program " +\
"FROM file_sample INNER JOIN project ON file_sample.project=project.project_id " +\
"INNER JOIN participant ON file_sample.participant=participant.entity_participant_id"
connection, db_results = self.query_database(query)
# create a set of files for filtering
save_db_results=[]
all_files=[]
saved_files=set()
for row in db_results:
data_category, data_version, data_merged = utilities.get_data_category_software_version(row['data_category'])
all_files.append(schema.File(id=row['file_id'], data_category=data_category, experimental_strategy=row['experimental_strategy']))
save_db_results.append(row)
saved_files.add(row['file_id'])
query = "SELECT file_sample.id as file_id, file_sample.participant, " +\
"file_sample.file_size, file_sample.data_category, " +\
"file_sample.experimental_strategy, file_sample.project, project.id as project_id, project.primary_site, " +\
"project.program, project.total_participants " +\
"FROM file_sample INNER JOIN project ON file_sample.project=project.project_id "
connection, db_results = self.query_database(query)
# create a set of files for filtering
for row in db_results:
if not row['file_id'] in saved_files:
data_category, data_version, data_merged = utilities.get_data_category_software_version(row['data_category'])
all_files.append(schema.File(id=row['file_id'], data_category=data_category, experimental_strategy=row['experimental_strategy']))
save_db_results.append(row)
saved_files.add(row['file_id'])
# apply filtering only for summary/file filters
if filters and "content" in filters:
for content in filters["content"]:
field=content["content"]["field"]
if "summary" in field:
content["content"]["field"]="files."+field.split(".")[-1]
filtered_files = utilities.filter_hits(all_files, filters, "files", True)
selected_file_ids = [file.id for file in filtered_files]
else:
selected_file_ids = [file.id for file in all_files]
project_info = {}
for row in save_db_results:
id = row['project_id']
if not row['file_id'] in selected_file_ids:
continue
if not id in project_info:
project_info[id]={"file_size":0, "file_count":0, "participants":set(), "data_category": {}, "experimental_strategy": {}}
project_info[id]['name']=row['project']
project_info[id]['program']=row['program']
project_info[id]['primary_site']=row['primary_site']
# compile file size
project_info[id]['file_size']+=int(row['file_size'])
# compile cases
if "participant_id" in row:
project_info[id]['participants'].add(row['participant_id'])
# add NA participants for counts only for projects without participant metadata
elif "total_participants" in row and row['total_participants']:
project_info[id]['participants'].update(map(lambda x: "NA_"+row['project']+"_"+str(x), list(xrange(int(row['total_participants'])))))
# compile file count and count data category and experimental strategy
if row['file_size'] != "0":
project_info[id]['file_count']+=1
for count_type in ["data_category", "experimental_strategy"]:
if count_type == "data_category":
data_category, data_version, data_merged = utilities.get_data_category_software_version(row[count_type])
project_info[id][count_type][data_category]=project_info[id][count_type].get(data_category,0)+1
elif count_type == "experimental_strategy":
project_info[id][count_type][row[count_type]]=project_info[id][count_type].get(row[count_type],0)+1
connection.close()
query = "SELECT sample.id as sample_id, sample.participant as participant_id, sample.project, " +\
"project.id as project_id, project.primary_site, " +\
"project.program " +\
"FROM sample INNER JOIN project ON sample.project=project.project_id"
connection, db_results = self.query_database(query)
for row in db_results:
id = row['project_id']
if not id in project_info:
project_info[id]={"file_size":0, "file_count":0, "participants":set(), "data_category": {}, "experimental_strategy": {}}
project_info[id]['name']=row['project']
project_info[id]['program']=row['program']
project_info[id]['primary_site']=row['primary_site']
# compile cases
project_info[id]['participants'].add(row['participant_id'])
connection.close()
query = "SELECT project_id, total_participants FROM project"
connection, db_results = self.query_database(query)
total_participants={}
for row in db_results:
total_participants[row['project_id']]=int(row['total_participants'])
connection.close()
return project_info,total_participants
def get_current_projects(self, filters=None):
# first gather all of the organized project information
project_info,total_participants=self.get_current_project_info(filters)
projects = []
for id, info in project_info.items():
# create a set of data categories as counts, after reformatting the category key
data_categories=[]
for key,value in info['data_category'].items():
category, data_version, data_merged = utilities.get_data_category_software_version(key)
data_categories+=[schema.DataCategories(case_count=value, data_category=category)]
projects.append(schema.Project(
id=id,
project_id=info['name'],
name=info['name'],
program=schema.Program(name=info['program']),
summary=schema.Summary(case_count=total_participants.get(info['name'],0),
file_count=info['file_count'],
data_categories=data_categories,
experimental_strategies=[schema.ExperimentalStrategies(file_count=value, experimental_strategy=key) for key,value in info['experimental_strategy'].items()],
file_size=info['file_size']),
primary_site=[info['primary_site']]))
return projects
def get_current_programs(self, filters=None):
# first gather all of the origanized project information
project_info,total_participants=self.get_current_project_info(filters)
# now reorganize the information to programs
program_info = {}
for id, info in project_info.items():
program_name=info['program']
if not program_name in program_info:
program_info[program_name]={"total_participants":0, "file_count":0, "data_category":{}, "experimental_strategy": {}, "file_size": 0, "primary_site": []}
program_info[program_name]["total_participants"]=program_info[program_name]["total_participants"]+total_participants.get(info['name'])
program_info[program_name]["file_count"]=program_info[program_name]["file_count"]+info['file_count']
program_info[program_name]["data_category"].update(info['data_category'])
program_info[program_name]["experimental_strategy"].update(info['experimental_strategy'])
program_info[program_name]["file_size"]=program_info[program_name]["file_size"]+info['file_size']
program_info[program_name]["primary_site"].append(info['primary_site'])
programs = []
for id, info in program_info.items():
# create a set of data categories as counts, after reformatting the category key
data_categories=[]
software_versions=[]
for key,value in info['data_category'].items():
category, data_version, data_merged = utilities.get_data_category_software_version(key)
data_categories+=[schema.DataCategories(case_count=value, data_category=category)]
programs.append(schema.ProgramNode(
id=id,
name=id,
summary=schema.Summary(case_count=info["total_participants"],
file_count=info["file_count"],
data_categories=data_categories,
experimental_strategies=[schema.ExperimentalStrategies(file_count=value, experimental_strategy=key) for key,value in info["experimental_strategy"].items()],
file_size=info["file_size"]),
# only use one of the primary site values
primary_site=[info["primary_site"][0]]))
return programs
@staticmethod
def get_generic_file_name(file_id, extension):
return "{0}.{1}".format(int(file_id)+GENERIC_FILE_NAME_OFFSET, extension.lower())
def get_all_cases(self,rows=False,filters=""):
query = "SELECT participant.* from participant INNER JOIN sample ON participant.entity_participant_id = sample.participant "+filters;
connection, db_results_participant = self.query_database(query)
# organize based on participant id
participant_data = {}
for rowprox in db_results_participant:
row = dict(rowprox.items())
row['participant_id']=row['id']
row['participant_name']=row['entity_participant_id']
participant_data[row['entity_participant_id']]=row
metadata_columns=[]
if rows:
participant_data = participant_data.values()
try:
metadata_columns = list(set(participant_data[0].keys()).difference(CASE_DEFAULT_COLUMNS))
except IndexError:
metadata_columns=[]
else:
try:
metadata_columns = list(set(participant_data[participant_data.keys()[0]].keys()).difference(CASE_DEFAULT_COLUMNS))
except IndexError:
metadata_columns=[]
if metadata_columns and participant_data:
self.participant_metadata_columns=metadata_columns
return metadata_columns, participant_data
def get_all_samples(self,rows=False,filters=""):
query = "SELECT * from sample "+filters;
connection, db_results_sample = self.query_database(query)
# organize based on sample id
sample_data = {}
for rowprox in db_results_sample:
row = dict(rowprox.items())
# add custom name changes
row['sample_id']=row['id']
sample_data[row['sample']]=row
if rows:
sample_data=sample_data.values()
try:
metadata_columns = list(set(sample_data[0].keys()).difference(SAMPLE_DEFAULT_COLUMNS))
except IndexError:
metadata_columns=[]
else:
try:
metadata_columns = list(set(sample_data[sample_data.keys()[0]].keys()).difference(SAMPLE_DEFAULT_COLUMNS))
except IndexError:
metadata_columns=[]
if metadata_columns and sample_data:
self.sample_metadata_columns=metadata_columns
return metadata_columns, sample_data
def get_all_projects(self):
query = "SELECT * FROM project"
connection, db_results = self.query_database(query)
projects_results={}
for rowprox in db_results:
row = dict(rowprox.items())
row['project_name']=row['project_id']
row['project_id']=row['id']
row['program_name']=row['program']
projects_results[row['project_name']]=row
return projects_results
def get_current_files(self, filters=""):
cache_files, have_lock=self.cache.get_cache("files",filters)
if cache_files:
return cache_files
# get all cases and samples data
participant_metadata_columns, participant_data = self.get_all_cases(filters=filters)
sample_metadata_columns, sample_data = self.get_all_samples(filters=filters)
query = "SELECT file_sample.id as file_id, file_sample.file_id as file_url, file_sample.file_name as file_name, file_sample.participant, file_sample.sample, " +\
"file_sample.access, file_sample.file_size, file_sample.data_category, file_sample.data_format, " +\
"file_sample.platform, file_sample.experimental_strategy, file_sample.project, project.id as project_id, project.primary_site, " +\
"project.program " +\
"FROM file_sample INNER JOIN project ON file_sample.project=project.project_id WHERE file_sample.file_id !='NA'"
connection, db_results = self.query_database(query)
files = []
for row in db_results:
# add in the sample and participant data
db_case=participant_data.get(row['participant'],{})
db_sample=sample_data.get(row['sample'],{})
metadataCase_hits=[]
if db_case:
for demo_item in participant_metadata_columns:
metadataCase_hits.append(schema.MetadataCaseAnnotation(id=str(db_case['participant_id'])+demo_item,metadataKey=demo_item[0].upper()+demo_item[1:],metadataValue=db_case[demo_item]))
metadataCase_counts=len(list(filter(lambda x: x.metadataValue != 'NA' and x.metadataValue != "Not_available", metadataCase_hits)))
demographic_instance=None
if db_case:
demographic_instance=schema.Custom()
demographic_keys=participant_metadata_columns
schema.add_attributes(demographic_instance, demographic_keys, db_case)
casesample_instance=None
if db_sample:
casesample_instance=schema.CaseSample(id=db_sample.get('sample_id',1))
casesample_keys=sample_metadata_columns
schema.add_attributes(casesample_instance, casesample_keys, db_sample)
# get additional file information from the file data category
data_category, data_version, data_merged = utilities.get_data_category_software_version(row['data_category'])
files.append(schema.File(
id=row['file_id'],
file_url=row['file_url'],
participant=row['participant'],
sample=row['sample'],
access=row['access'],
file_size=row['file_size'],
data_category=data_category,
data_version=data_version,
data_merged=data_merged,
data_format=row['data_format'],
platform=row['platform'],
experimental_strategy=row['experimental_strategy'],
generic_file_name=row['file_name'],
cases=schema.FileCases(
hits=[schema.FileCase(
id=row['participant'],
case_id=row['participant'],
project=schema.Project(
id=row['project_id'],
project_id=row['project'],
name=row['project'],
program=schema.Program(name=row['program']),
primary_site=[row['primary_site']]),
demographic=demographic_instance,
metadataCase=schema.MetadataCase(
hits=metadataCase_hits,
metadata_count=metadataCase_counts),
primary_site=row['primary_site'],
samples=[casesample_instance] if casesample_instance else []
)]
),
file_id=row['file_id'],
type=row['data_format']
))
connection.close()
self.cache.update_cache("files",files,filters,have_lock)
return files
def get_current_samples(self,filters=""):
cache_samples, have_lock = self.cache.get_cache("samples", filters)
if cache_samples:
return cache_samples
# gather file data for participants
query = "SELECT id, participant, file_size, data_category, experimental_strategy, " +\
"data_format, platform, access, project from file_sample WHERE file_id !='NA'"
connection, db_results = self.query_database(query)
case_files = {}
merged_case_files = {}
for row in db_results:
if row['participant'] == "NA":
if not row['project'] in merged_case_files:
merged_case_files[row['project']] = []
merged_case_files[row['project']].append(dict(row.items()))
if not row['participant'] in case_files:
case_files[row['participant']] = []
case_files[row['participant']].append(dict(row.items()))
connection.close()
# get all cases and samples data
participant_metadata_columns, participant_data = self.get_all_cases(filters=filters)
projects_data = self.get_all_projects()
sample_metadata_columns, db_results = self.get_all_samples(rows=True,filters=filters)
samples = []
for row in db_results:
db_case = participant_data.get(row['participant'],False)
db_projects = projects_data.get(row['project'],False)
if not ( db_case and db_projects):
continue
current_case_files = case_files.get(row['participant'],[])
# create data categories
data_categories_counts={}
for case_row in current_case_files:
data_category, data_version, data_merged = utilities.get_data_category_software_version(case_row['data_category'])
data_categories_counts[data_category]=data_categories_counts.get(data_category,0)+1
data_categories = [schema.DataCategories(case_count=value, data_category=key) for key, value in data_categories_counts.items()]
# create participant summary
summary=schema.Summary(case_count=1,
file_count=len(current_case_files),
file_size=sum(map(int, [case_row['file_size'] for case_row in current_case_files])),
data_categories=data_categories)
# create participant casefiles
casefiles=[]
all_case_files=merged_case_files.get(row['project'],[])+current_case_files
for index, file_info in enumerate(all_case_files):
data_category, data_version, data_merged = utilities.get_data_category_software_version(file_info['data_category'])
casefiles.append(schema.CaseFile(
id=index,
data_category=data_category,
data_version=data_version,
data_merged=data_merged,
experimental_strategy=file_info['experimental_strategy'],
data_format=file_info['data_format'],
platform=file_info['platform'],
access=file_info['access'],
file_size=file_info['file_size']))
metadataCase_hits=[]
for demo_item in participant_metadata_columns:
schema.MetadataCaseAnnotation(id=str(db_case['participant_id'])+demo_item,metadataKey=demo_item[0].upper()+demo_item[1:],metadataValue=db_case[demo_item]),
metadataCase_counts=len(list(filter(lambda x: x.metadataValue != 'NA' and x.metadataValue != "Not_available", metadataCase_hits)))
metadataSample_hits=[]
for metadata_key in sample_metadata_columns:
metadataSample_hits.append(schema.MetadataSampleAnnotation(id=str(row['id'])+metadata_key,metadataKey=metadata_key.title(),metadataValue=row[metadata_key]))
metadataSample_counts=len(list(filter(lambda x: x.metadataValue != 'NA' and x.metadataValue != "Not_available", metadataSample_hits)))
demographic_instance=schema.Custom()
demographic_keys=participant_metadata_columns
schema.add_attributes(demographic_instance, demographic_keys,db_case)
sample_instance=schema.Sample(
id=row['id'],
sample_id=row['sample'],
primary_site=db_projects['primary_site'],
demographic=demographic_instance,
metadataCase=schema.MetadataCase(
hits=metadataCase_hits,
metadata_count=metadataCase_counts),
project=schema.Project(
id=db_projects['project_id'],
project_id=db_projects['project_name'],
name=db_projects['project_name'],
program=schema.Program(name=db_projects['program_name']),
primary_site=[db_projects['primary_site']]),
summary=summary,
metadataSample=schema.MetadataSample(
hits=metadataSample_hits,
metadata_count=metadataSample_counts),
files=schema.CaseFiles(hits=casefiles),
cases=schema.FileCases(hits=[schema.FileCase(case_id=db_case['participant_id'], primary_site=db_projects['primary_site'])])
)
sample_keys=sample_metadata_columns
schema.add_attributes(sample_instance, sample_keys, row)
samples.append(sample_instance)
connection.close()
self.cache.update_cache("samples",samples,filters,have_lock)
return samples
def get_current_cases(self,filters=""):
cache_cases, have_lock = self.cache.get_cache("cases", filters)
if cache_cases:
return cache_cases
# gather file data for participants
query = "SELECT id, participant, file_size, data_category, experimental_strategy, " +\
"data_format, platform, access, project from file_sample WHERE file_id != 'NA'"
connection, db_results = self.query_database(query)
case_files = {}
merged_case_files = {}
for row in db_results:
if row['participant'] == "NA":
if not row['project'] in merged_case_files:
merged_case_files[row['project']] = []
merged_case_files[row['project']].append(dict(row.items()))
if not row['participant'] in case_files:
case_files[row['participant']] = []
case_files[row['participant']].append(dict(row.items()))
connection.close()
# gather sample data for participants
sample_metadata_columns, db_results = self.get_all_samples(rows=True,filters=filters)
case_samples = {}
for row in db_results:
if not row['participant'] in case_samples:
case_samples[row['participant']] = []
case_samples[row['participant']].append(dict(row.items()))
connection.close()
# gather participant data
projects_data=self.get_all_projects()
participant_metadata_columns, db_results=self.get_all_cases(rows=True,filters=filters)
cases = []
completed_cases = set()
for row in db_results:
db_sample=case_samples.get(row['participant_name'],[""])[0]
if not db_sample:
continue
db_projects=projects_data.get(db_sample['project'],False)
if not db_projects:
continue
if row['participant_id'] in completed_cases:
continue
current_case_files = case_files.get(row['participant_name'],[])
# create data categories
data_categories_counts={}
for case_row in current_case_files:
data_category, data_version, data_merged = utilities.get_data_category_software_version(case_row['data_category'])
data_categories_counts[data_category]=data_categories_counts.get(data_category,0)+1
data_categories = [schema.DataCategories(case_count=value, data_category=key) for key, value in data_categories_counts.items()]
# create participant summary
summary=schema.Summary(case_count=1,
file_count=len(current_case_files),
file_size=sum(map(int, [case_row['file_size'] for case_row in current_case_files])),
data_categories=data_categories)
# create participant casefiles
casefiles=[]
all_case_files=merged_case_files.get(db_sample['project'],[])+current_case_files
for index, file_info in enumerate(all_case_files):
data_category, data_version, data_merged = utilities.get_data_category_software_version(file_info['data_category'])
casefiles.append(schema.CaseFile(
id=index,
data_category=data_category,
data_version=data_version,
data_merged=data_merged,
experimental_strategy=file_info['experimental_strategy'],
data_format=file_info['data_format'],
platform=file_info['platform'],
access=file_info['access'],
file_size=file_info['file_size']))
# create casesamples
casesamples=[]
for index, sample_info in enumerate(case_samples[row['participant_name']]):
casesample_instance=schema.CaseSample(id=index)
casesample_keys=sample_metadata_columns
schema.add_attributes(casesample_instance, casesample_keys, sample_info)
casesamples.append(casesample_instance)
metadataCase_hits=[]
for demo_item in participant_metadata_columns:
metadataCase_hits.append(schema.MetadataCaseAnnotation(id=str(row['participant_id'])+demo_item,metadataKey=demo_item[0].upper()+demo_item[1:],metadataValue=row[demo_item]))
metadataCase_counts=len(list(filter(lambda x: x.metadataValue != 'NA', metadataCase_hits)))
demographic_instance=schema.Custom()
demographic_keys=participant_metadata_columns
schema.add_attributes(demographic_instance, demographic_keys, row)
cases.append(schema.Case(
id=row['participant_id'],
case_id=row['participant_name'],
primary_site=db_projects['primary_site'],
demographic=demographic_instance,
metadataCase=schema.MetadataCase(
hits=metadataCase_hits,
metadata_count=metadataCase_counts),
project=schema.Project(
id=db_projects['project_id'],
project_id=db_projects['project_name'],
name=db_projects['project_name'],
program=schema.Program(name=db_projects['program_name']),
primary_site=[db_projects['primary_site']]),
summary=summary,
files=schema.CaseFiles(hits=casefiles),
samples=schema.CaseSamples(hits=casesamples),
))
completed_cases.add(row['participant_id'])
connection.close()
self.cache.update_cache("cases",cases,filters,have_lock)
return cases
def get_cart_file_size(self, filters=""):
all_files = self.get_current_files()
filtered_files = utilities.filter_hits(all_files, filters, "files", True)
# get the size from the filtered files
total_size = utilities.get_total_file_size(filtered_files)
return schema.CartSummaryAggs(fs=schema.FileSize(value=total_size))
def get_current_counts(self):
query = "SELECT COUNT(distinct project), COUNT(distinct participant), COUNT(distinct sample), " +\
"COUNT(distinct IF(data_format!='NA',1,data_format)), COUNT(IF(type='"+RAW_FILE_TYPE+"',1,NULL)), " +\
"COUNT(IF(type='"+PROCESSED_FILE_TYPE+"',1,NULL)) FROM file_sample"
db_results = self.query_database(query, fetchall=True)[0]
query = "SELECT SUM(total_participants) FROM project"
db_results_2 = self.query_database(query, fetchall=True)[0]
# the count for data_format above does not sync up so using this alternative
query = "SELECT COUNT(distinct data_format) FROM file_sample where data_format!='NA'"
db_results_3 = self.query_database(query, fetchall=True)[0]
query = "SELECT COUNT(distinct program) FROM project"
db_results_4 = self.query_database(query, fetchall=True)[0]
all_files = self.get_current_files()
total_size_files = str(utilities.convert_kb_to_TB(utilities.get_total_file_size(all_files)))+" TB"
counts = schema.Count(
programs=int(db_results_4[0]),
projects=db_results[0],
participants=int(db_results_2[0]),
samples=db_results[2],
dataFormats=int(db_results_3[0]),
totalData=total_size_files,
rawFiles=db_results[4],
processedFiles=db_results[5]
)
return counts
def get_version(self):
db_results = self.query_database("SELECT commit, data_release, status, tag, version FROM version", fetchall=True)[0]
return dict(db_results.items())
#############################################################################
## Aggregations section
## These functions create aggregations of the object lists they are provided.
#############################################################################
def get_project_aggregations(self, projects):
# compile aggregations from project
aggregates = {"primary_site": {}, "program__name": {},
"project_id": {},
"summary__data_categories__data_category": {},
"summary__experimental_strategies__experimental_strategy": {}}
for project in projects:
utilities.add_key_increment(aggregates["primary_site"], project.primary_site[0])
utilities.add_key_increment(aggregates["project_id"], project.project_id)
utilities.add_key_increment(aggregates["program__name"], project.program.name)
for item in project.summary.data_categories:
utilities.add_key_increment(aggregates["summary__data_categories__data_category"], item.data_category)
for item in project.summary.experimental_strategies:
utilities.add_key_increment(aggregates["summary__experimental_strategies__experimental_strategy"], item.experimental_strategy)
project_aggregates=schema.ProjectAggregations(
primary_site=schema.Aggregations(
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates["primary_site"].items()]),
project_id=schema.Aggregations(
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates["project_id"].items()]),
program__name=schema.Aggregations(
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates["program__name"].items()]),
summary__data_categories__data_category=schema.Aggregations(
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates["summary__data_categories__data_category"].items()]),
summary__experimental_strategies__experimental_strategy=schema.Aggregations(
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates["summary__experimental_strategies__experimental_strategy"].items()]))
return project_aggregates
def get_file_aggregations(self, files):
def get_schema_aggregations(variable_name,schema_type="buckets"):
if schema_type == "buckets":
return schema.Aggregations(
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates[variable_name].items()])
else:
return schema.Aggregations(
stats=schema.Stats(max=stats[variable_name].get("max",0), min=stats[variable_name].get("min",0)))
# aggregate file data
aggregates = {"data_version": {}, "data_merged" : {}, "data_category": {}, "experimental_strategy": {},
"data_format": {}, "platform": {}, "cases__primary_site": {},
"cases__project__project_id": {}, "access": {}, "file_size": {}}
stats = {"file_size": {}}
for file in files:
utilities.add_key_increment(aggregates["data_version"], file.data_version)
utilities.add_key_increment(aggregates["data_merged"], file.data_merged)
utilities.add_key_increment(aggregates["data_category"], file.data_category)
utilities.add_key_increment(aggregates["experimental_strategy"], file.experimental_strategy)
utilities.add_key_increment(aggregates["data_format"], file.data_format)
utilities.add_key_increment(aggregates["platform"], file.platform)
utilities.add_key_increment(aggregates["access"], file.access)
utilities.add_key_increment(aggregates["file_size"], utilities.Range.create_custom(utilities.bytes_to_gb(file.file_size),2))
try:
project = file.cases.hits[0].project
utilities.add_key_increment(aggregates["cases__primary_site"], project.primary_site[0])
utilities.add_key_increment(aggregates["cases__project__project_id"], project.project_id)
except IndexError:
continue
utilities.update_max_min(stats["file_size"], utilities.bytes_to_gb(file.file_size))
file_aggregates = schema.FileAggregations(
data_version=get_schema_aggregations("data_version"),
data_merged=get_schema_aggregations("data_merged"),
data_category=get_schema_aggregations("data_category"),
experimental_strategy=get_schema_aggregations("experimental_strategy"),
data_format=get_schema_aggregations("data_format"),
platform=get_schema_aggregations("platform"),
cases__primary_site=get_schema_aggregations("cases__primary_site"),
access=get_schema_aggregations("access"),
cases__project__project_id=get_schema_aggregations("cases__project__project_id"),
file_size=get_schema_aggregations("file_size",schema_type="stats"))
return file_aggregates
def apply_whitelist(self, keep_list, store_fields):
if keep_list:
return list(keep_list.intersection(set(store_fields)))
def get_sample_aggregations(self, samples):
def get_stats_aggregations(variable_name):
# do not serve buckets with just NA
keys=list(aggregates[variable_name].keys())
if len(keys) == 1 and keys[0] == "NA":
return schema.Aggregations(
stats=schema.Stats(max=stats[variable_name].get("max",0), min=stats[variable_name].get("min",0)))
else:
return schema.Aggregations(
stats=schema.Stats(max=stats[variable_name].get("max",0), min=stats[variable_name].get("min",0)),
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates[variable_name].items()])
sample_metadata_fields=utilities.order_metadata_keys(list(set(dir(samples[0])).difference(set(dir(schema.Sample())))), METADATA_SAMPLES_PROMOTE)
demographic_metadata_fields=utilities.order_metadata_keys(list(set(dir(samples[0].demographic)).difference(set(dir(schema.Demographic())))), METADATA_DEMOGRAPHICS_PROMOTE)
# if whitelist provided, then apply
sample_metadata_fields=self.apply_whitelist(self.metadata_samples_whitelist,sample_metadata_fields)
demographic_metadata_fields=self.apply_whitelist(self.metadata_demographics_whitelist,demographic_metadata_fields)
# aggregate sample data
aggregates = {"primary_site": {}, "project__project_id": {}, "project__program__name": {}}
sample_lists = {}
for demo_key in demographic_metadata_fields:
aggregates["demographic__"+demo_key]={}
stats = {}
for key in sample_metadata_fields:
aggregates[key]={}
stats[key]={}
sample_lists[key]=[]
for sample in samples:
for demo_key in demographic_metadata_fields:
utilities.add_key_increment(aggregates["demographic__"+demo_key], getattr(sample.demographic, demo_key))
utilities.add_key_increment(aggregates["primary_site"], sample.primary_site)
utilities.add_key_increment(aggregates["project__project_id"], sample.project.project_id)
utilities.add_key_increment(aggregates["project__program__name"], sample.project.program.name)
for key in sample_metadata_fields:
sample_lists[key].append(getattr(sample,key))
# get the min/max/offset for each sample metadata
self.compute_min_max_offset(stats, sample_metadata_fields, sample_lists)
for key in sample_metadata_fields:
for value in sample_lists[key]:
utilities.add_key_increment(aggregates[key], utilities.Range.create(value,offset=stats[key].get("offset",1)))
all_aggregations=[]
for typename in sample_metadata_fields:
if stats[typename].get("max",0) > 0:
all_aggregations.append(schema.AggregationAnnotation(id="sample"+typename,metadataKey=typename,metadataType="stats",
metadataValue=schema.Aggregations(stats=schema.Stats(max=stats[typename].get("max",0), min=stats[typename].get("min",0)))))
sample_aggregates=schema.SampleAggregations(
metadataAggregations=schema.MetadataAggregations(hits=all_aggregations),
primary_site=schema.Aggregations(
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates["primary_site"].items()]),
project__project_id=schema.Aggregations(
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates["project__project_id"].items()]),
project__program__name=schema.Aggregations(
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates["project__program__name"].items()]))
for demo_key in demographic_metadata_fields:
keys=list(aggregates["demographic__"+demo_key].keys())
if not (len(keys) == 1 and keys[0] == "NA"):
new_aggregations=schema.Aggregations(
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates["demographic__"+demo_key].items()])
setattr(sample_aggregates,"demographic__"+demo_key, new_aggregations)
for key in sample_metadata_fields:
setattr(sample_aggregates, key, get_stats_aggregations(key))
return sample_aggregates
def get_metadata_title(self,field):
if "demographic" in field:
return field.split("demographic__")[-1]
elif "program" in field:
return "program"
elif field.startswith("project"):
return "project"
elif field.startswith("sample"):
return field.split("sample__")[-1]
else:
return field
def compute_min_max_offset(self, stats, sample_metadata_fields, sample_lists, key_init=""):
for key in sample_metadata_fields:
try:
stats[key_init+key]["max"]=max(filter(None,map(lambda x: float(x) if x.replace(".","").replace("-","").isdigit() else None, sample_lists[key])))
stats[key_init+key]["min"]=min(filter(None,map(lambda x: float(x) if x.replace(".","").replace("-","").isdigit() else None, sample_lists[key])))
stats[key_init+key]["offset"]=len(str(int(stats[key_init+key]["max"]-stats[key_init+key]["min"])))-1
except ValueError:
pass
def get_case_aggregations(self, cases, filters):
def get_schema_aggregations(variable_name):
return schema.Aggregations(
buckets=[schema.Bucket(doc_count=count, key=key) for key,count in aggregates[variable_name].items()])
try:
sample_metadata_fields=utilities.order_metadata_keys(list(set(dir(cases[0].samples.hits[0])).difference(set(dir(schema.Sample())))), METADATA_SAMPLES_PROMOTE)
sample_metadata_fields=self.apply_whitelist(self.metadata_samples_whitelist,sample_metadata_fields)
except IndexError:
sample_metadata_fields=[]
try:
demographic_metadata_fields=utilities.order_metadata_keys(list(set(dir(cases[0].demographic)).difference(set(dir(schema.Demographic())))), METADATA_DEMOGRAPHICS_PROMOTE)
demographic_metadata_fields=self.apply_whitelist(self.metadata_demographics_whitelist,demographic_metadata_fields)
except IndexError: