-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexport.py
216 lines (191 loc) · 7.22 KB
/
export.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
import sys
import os
from time import sleep
import psycopg2
import utils
# Constants
# utils.NEWLINE_REPLACEMENT = " " # Can be uncommented and edited
# utils.SUBARRAY_SEPARATOR = ";" # Can be uncommented and edited
if len(sys.argv) != 6:
print "Passed arguments were: " + str(sys.argv)
print "Arguments must be: export.py outputDir dbHost dbName dbUser dbPassword"
exit(0)
output_dir = "./" + sys.argv[1]
output_dir = output_dir if output_dir[-1] == "/" else output_dir + "/"
connection_string = "dbname={1} host={0} user={2} password={3}".format(*sys.argv[2:])
print "Connecting to " + connection_string
conn = psycopg2.connect(connection_string)
cursor = conn.cursor()
print "Connected\n"
sleep(2)
#########################
### Individuals_##### ###
#########################
print "Exporting Individuals"
# array_agg + array_to_string = Like SUM(), but for text fields. Concatenates strings.
cursor.execute("""
SELECT
I.id, I.dob, I.sex, I.ethnic_code, I.centre_name, I.region_name,
I.country_name, I.comments, COALESCE(B.batches, '')
FROM "public"."person" I
LEFT JOIN (
SELECT B.person_id, array_to_string(array_agg(B.batch_id::text), '{0}') AS batches
FROM "public"."batches" B
GROUP BY B.person_id
ORDER BY B.person_id ASC
) B ON I.id = B.person_id
ORDER BY I.id ASC
""".format(utils.SUBARRAY_SEPARATOR))
print "Writing " + str(cursor.rowcount) + " files\n"
# This section will need to create the headers and the rows dynamically because
# the number of columns is not known ahead of time! See utils.py for more info.
# 8 is where the batches subarray is located
subarrayPos = 8
nbBatches = utils.size_of_subarray(cursor, subarrayPos)
batchPlaceholders = utils.make_placeholders(subarrayPos, nbBatches)
headerPlaceholders = utils.make_headers("batch", 0, nbBatches)
f = cursor.fetchone()
current_id = -1
while f:
current_id = f[0]
os.makedirs(output_dir + str(current_id))
with open("{0}{1}/Individuals_{1}.tsv".format(output_dir, current_id), "w") as file:
file.write("individualId,familyId,paternalId,maternalId,dateOfBirth,gender,ethnicCode,centreName,region,country,notes,{0}\n".format(headerPlaceholders))
li = utils.to_prepared_list(f)
li = utils.break_subarray(li, subarrayPos, nbBatches)
file.write(("""{0},,,,{1},{2},{3},{4},{5},{6},{7},"""+batchPlaceholders+"""\n""").format(*li))
f = cursor.fetchone()
################################
### Individuals_#####_Visits ###
################################
print "Exporting Individuals Visits"
cursor.execute("""
SELECT V.person_id, V.id, V.visit_date, V.form_id, V.fasting, V.descr
FROM "public"."visit" AS V
ORDER BY V.person_id ASC, V.id ASC
""")
print "Writing " + str(cursor.rowcount) + " files\n"
f = cursor.fetchone()
current_id = -1
while f:
if f[0] != current_id: # We only close the file when we switch to a new Person
try: # Ugly, but Python doesn't let me create ad hoc mocks like JS
file.close()
except:
pass
current_id = f[0]
file = open("{0}{1}/Individuals_{1}_Visits.tsv".format(output_dir, current_id), "a")
file.write("visitId,visitDate,studyFormId,fasting,description\n")
li = utils.to_prepared_list(f)
file.write("""{1},{2},{3},{4},{5}\n""".format(*li))
f = cursor.fetchone()
####################################
### Individuals_#####_Phenotypes ###
####################################
print "Exportung Individuals Phenotypes"
cursor.execute("""
SELECT V.person_id, V.id, P.name, M.value, P.um, P.descr, V.visit_date
FROM "public"."phenotype" AS P
LEFT JOIN (
SELECT * FROM "public"."measure" M
) AS M ON P.id = M.phenotype_id
LEFT JOIN "public"."visit" V ON M.visit_id = V.id
WHERE V.person_id IS NOT NULL -- There's 1 buggy row, exclude it
ORDER BY V.person_id ASC
""")
print "Writing " + str(cursor.rowcount) + " files\n"
f = cursor.fetchone()
current_id = -1
while f:
if f[0] != current_id: # We only close the file when we switch to a new Person
try: # Ugly, but Python doesn't let me create ad hoc mocks like JS
file.close()
except:
pass
current_id = f[0]
file = open("{0}{1}/Individuals_{1}_Phenotypes.tsv".format(output_dir, current_id), "a")
file.write("visitId,phenotypeType,phenotypeGroup,name,measureDataType,measure,units,description,diagnosisDate\n")
li = utils.to_prepared_list(f)
file.write("""{1},,,{2},,{3},{4},{5},{6}\n""".format(*li))
f = cursor.fetchone()
###################################
### Individuals_#####_Genotypes ###
###################################
print "Exporting Individuals Genotypes"
mapping = {
"1": "AA",
"2": "BB",
"3": "AB",
"4": "00",
"5": "A0", # Doesn't happen in practice
"6": "B0", # Doesn't happen in practice
"X": "00",
"x": "00" # Saves a call to lower()
}
# See Simon_Grondin_PFE.pdf for more information about why there's 2 cursors and how they work together
# It's too much information to write it all here.
cursor1 = conn.cursor()
cursor2 = conn.cursor()
# The first query loads data from the "snp_genotype" table, joined with the "person" table
print "Executing SQL query 1/2..."
cursor1.execute("""
SELECT G.person_id, G.genotype_version, P.sample, G.genotype
FROM "public"."snp_genotype" G
INNER JOIN "public"."person" P on G.person_id = P.id
ORDER BY G.person_id ASC, G.genotype_version ASC LIMIT 4
""")
# The second query loads data from the "snp_annotation" table, joined with the "vcf" table
print "Executing SQL query 2/2..."
cursor2.execute("""
SELECT
A.genotype_index, A.annotation_version, A.rs,
V.chromosome, V.position, (V.position + CHAR_LENGTH(ref)),
V.ref, V.alt, A.allele_a, A.allele_b
FROM "public"."snp_annotation" A
LEFT JOIN "public"."vcf" V ON A.rs = V.rs
ORDER BY A.annotation_version ASC, A.genotype_index ASC
""")
print "Writing " + str(cursor1.rowcount) + " files\n"
f1 = cursor1.fetchone()
f2 = cursor2.fetchone()
while f1:
current_id = f1[0]
current_version = f1[1]
filename = "{0}{1}/Individuals_{1}_v{2}_Genotypes.tsv".format(output_dir, current_id, current_version)
print "Writing " + filename
file = open(filename, "a")
file.write("personId,genotypeVersion,sample,letter,rs,chromosome,start,end,ref,alt,allele_a,allele_b\n")
buffer = []
while f2 and f2[1] == current_version:
li1 = utils.to_prepared_list(f1[:3])
li2 = utils.to_prepared_list(f2[2:])
letter = mapping[f1[3][f2[0]-1]]
buffer.append("""{0},{1},{2},{3},{4},{5},{6},{7},{8},"{9}",{10},{11}\n""".format(*(li1+[letter]+li2)))
f2 = cursor2.fetchone()
file.write("".join(buffer))
if f2 is None: # rewind
cursor2.scroll(0, "absolute")
f2 = cursor2.fetchone()
file.close()
f1 = cursor1.fetchone()
###############
### Batches ###
###############
print "Exporting Batches"
with open(output_dir + "Batches.tsv", "w") as file:
file.write("batchId,batchDate,batchType,description,studyName\n")
# First do the batch table
cursor.execute("""SELECT B.id, B.batch_date, B.description FROM "public"."batch" AS B""")
f = cursor.fetchone()
while f:
li = utils.to_prepared_list(f)
file.write("""{0},{1},ADVANCE-ON,{2},Genotyping Batch\n""".format(*li))
f = cursor.fetchone()
# Then do the samples table
cursor.execute("""SELECT B.id, B.sample_date, B.description FROM "public"."samples" AS B""")
f = cursor.fetchone()
while f:
li = utils.to_prepared_list(f)
file.write("""S{0},{1},ADVANCE-ON,{2},Enrollment Batch\n""".format(*li)) # Note the "S" before the Sample ID
f = cursor.fetchone()
print "Done"