-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsave_csv_qtdb_aws.py
125 lines (95 loc) · 3.68 KB
/
save_csv_qtdb_aws.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
"""
deltaQT Database - Save Partial Database, Updated July 18, 2017
Copyright (C) 2017, Tatonetti Lab
Tal Lorberbaum <[email protected]>
Victor Nwankwo <[email protected]>
Nicholas P. Tatonetti <[email protected]>
All rights reserved.
This script is released under a CC BY-NC-SA 4.0 license.
For full license details see LICENSE.txt or go to:
http://creativecommons.org/licenses/by-nc-sa/4.0/
------------------------------------------------------------------------
deltaQT Database script for saving selected drugs to CSV.
See www.deltaqt.org for more information about the database.
"""
import pymysql
def gen_partial_db(drugs):
# Connect to the database
conn = pymysql.connect(read_default_file='CONFIG_FILE',
db='qtdb',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
cur = conn.cursor()
table_suffix = ""
if len(drugs) == 1:
if drugs[0].startswith('('):
drug_select = 'in'
else:
drug_select = '='
SQL = '''select pm.pt_id_era, pm.pt_id
from qtdb.Patient{suffix} pm
join
(select pt_id,
max(delta_qt) as max_delta
from qtdb.Patient{suffix} p
join qtdb.Patient2Drug{suffix} d1 using (pt_id_era)
where d1.drug_concept_id {drug_select} {drugs0}\n'''.format(suffix=table_suffix, drug_select=drug_select, drugs0=drugs[0])
else:
SQL = '''select pm.pt_id_era, pm.pt_id
from qtdb.Patient{suffix} pm
join
(select pt_id,
max(delta_qt) as max_delta
from qtdb.Patient{suffix} p\n'''.format(suffix=table_suffix)
for i in range(len(drugs)):
SQL += ''' join qtdb.Patient2Drug%s d%d using (pt_id_era)\n''' %(table_suffix, (i+1))
for i in range(len(drugs)):
if i == 0:
where_statement = 'where'
else:
where_statement = 'and'
if drugs[i].startswith('('):
drug_select = 'in'
else:
drug_select = '='
SQL += ''' %s d%d.drug_concept_id %s %s\n''' %(where_statement, i+1, drug_select, drugs[i])
delta_limit = 150
SQL += ''' and abs(delta_qt) <= %d\n''' %delta_limit
SQL += ''' group by pt_id) m
on pm.pt_id = m.pt_id
and pm.delta_qt = m.max_delta;'''
print SQL
cur.execute(SQL)
results = cur.fetchall()
pt_ids_seen = set()
pt_id_eras = []
for result in results:
if result['pt_id'] in pt_ids_seen:
continue
pt_ids_seen.add(result['pt_id'])
pt_id_eras.append(str(result['pt_id_era']))
print len(pt_id_eras),"results to write to CSV"
# print pt_id_eras
if len(pt_id_eras) > 10000:
return "Too many patients"
# Query to get all data
SQL = '''select pt_id_era, pt_id, era, age, sex, race, num_drugs, drug_concept_id, drug_name, pre_qt_500, post_qt_500, delta_qt,
electrolyte_imbalance, cardiac_comorbidity
from Patient
join Patient2Drug using (pt_id_era)
join Drug using (drug_concept_id)
where pt_id_era in %s
#order by pt_id_era;''' %str(tuple(pt_id_eras))
cur.execute(SQL)
results = cur.fetchall()
header = ['pt_id_era', 'pt_id', 'era', 'age', 'sex', 'race', 'num_drugs', 'drug_concept_id', 'drug_name',
'pre_qt_500', 'post_qt_500', 'delta_qt', 'electrolyte_imbalance', 'cardiac_comorbidity']
partial_db = []
partial_db.append(header)
for result in results:
partial_db.append([str(result[key]) for key in header])
cur.close()
conn.close()
# Adapted from http://stackoverflow.com/a/898404
partial_csv = "\n".join(",".join('"'+str(element)+'"' for element in line) for line in partial_db)
return partial_csv