-
Notifications
You must be signed in to change notification settings - Fork 4
/
populate_database.py
executable file
·234 lines (189 loc) · 7.77 KB
/
populate_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
#!/usr/bin/python
#
# populate_database.py
#
# Chiu Laboratory
# University of California, San Francisco
#
# Copyright (C) 2015 Scot Federman - All Rights Reserved
# metaPORE has been released under a modified BSD license.
# Please see license file for details.
import sqlite3
import sys
import os
usage = "populate_database.py <project> <batch>"
if len(sys.argv) != 3:
print usage
sys.exit(0)
project = sys.argv[1]
batch = sys.argv[2]
database = project + ".db"
conn = sqlite3.connect(database)
c = conn.cursor()
species_file = project + "_" + batch + ".human_subtracted.species.count"
genus_file = project + "_" + batch + ".human_subtracted.genus.count"
family_file = project + "_" + batch + ".human_subtracted.family.count"
human_species_file = project + "_" + batch + ".human.species.count"
human_genus_file = project + "_" + batch + ".human.genus.count"
human_family_file = project + "_" + batch + ".human.family.count"
pipeline_file = project + "_" + batch + ".count"
representative_reads_file = project + "_" + batch + ".rep.count"
all_reads_file = project + "_" + batch + ".all_reads.count"
virus_species = project + "_" + batch + ".human_subtracted.species.viruses.count"
bacteria_species = project + "_" + batch + ".human_subtracted.species.bacteria.count"
if os.path.isfile(virus_species):
with open(virus_species, 'r') as map_file:
for line in map_file:
item = line.lstrip().split(" ", 1)
species_name = item[1].strip()
count = int(item[0])
c.execute("SELECT count FROM virus_species WHERE name = ?", [species_name])
existing_value = c.fetchall()
if len(existing_value)==0:
c.execute('INSERT INTO virus_species VALUES (?,?)', (species_name, count))
else:
newcount = count + int(existing_value[0][0])
c.execute("update virus_species set count = ? where name = ?", (newcount, species_name))
if os.path.isfile(bacteria_species):
with open(bacteria_species, 'r') as map_file:
for line in map_file:
item = line.lstrip().split(" ", 1)
species_name = item[1].strip()
count = int(item[0])
c.execute("SELECT count FROM bacteria_species WHERE name = ?", [species_name])
existing_value = c.fetchall()
if len(existing_value)==0:
c.execute('INSERT INTO bacteria_species VALUES (?,?)', (species_name, count))
else:
newcount = count + int(existing_value[0][0])
c.execute("update bacteria_species set count = ? where name = ?", (newcount, species_name))
if os.path.isfile(pipeline_file):
with open(pipeline_file, 'r') as map_file:
for line in map_file:
item = line.lstrip().split(" ", 1)
name = item[1].strip()
count = int(item[0])
c.execute("SELECT count FROM pipeline WHERE name = ?", [name])
existing_value = c.fetchall()
if len(existing_value)==0:
c.execute('INSERT INTO pipeline VALUES (?,?)', (name, count))
else:
newcount = count + int(existing_value[0][0])
c.execute("update pipeline set count = ? where name = ?", (newcount, name))
if os.path.isfile(representative_reads_file):
with open(representative_reads_file, 'r') as map_file:
for line in map_file:
item = line.lstrip().split(" ", 1)
name = item[1].strip()
count = int(item[0])
c.execute("SELECT count FROM representative_sequence WHERE name = ?", [name])
existing_value = c.fetchall()
if len(existing_value)==0:
c.execute('INSERT INTO representative_sequence VALUES (?,?)', (name, count))
else:
newcount = count + int(existing_value[0][0])
c.execute("update representative_sequence set count = ? where name = ?", (newcount, name))
if os.path.isfile(all_reads_file):
with open(all_reads_file, 'r') as map_file:
for line in map_file:
item = line.lstrip().split(" ", 1)
name = item[1].strip()
count = int(item[0])
c.execute("SELECT count FROM all_reads WHERE name = ?", [name])
existing_value = c.fetchall()
c.execute("SELECT color FROM all_reads WHERE name = ?", [name])
existing_color = c.fetchall()
if len(existing_value)==0:
c.execute('INSERT INTO all_reads VALUES (?,?,?)', (name, count, existing_color[0].strip()))
else:
newcount = count + int(existing_value[0][0])
c.execute("update all_reads set count = ? where name = ?", (newcount, name))
if os.path.isfile(human_species_file):
with open(human_species_file, 'r') as map_file:
for line in map_file:
item = line.lstrip().split(" ", 1)
species_name = item[1].strip()
count = int(item[0])
c.execute("SELECT count FROM species WHERE name = ?", [species_name])
existing_value = c.fetchall()
if len(existing_value)==0:
c.execute('INSERT INTO species VALUES (?,?)', (species_name, count))
else:
newcount = count + int(existing_value[0][0])
c.execute("update species set count = ? where name = ?", (newcount, species_name))
if os.path.isfile(human_genus_file):
with open(human_genus_file, 'r') as map_file:
for line in map_file:
item = line.lstrip().split(" ", 1)
genus_name = item[1].strip()
count = int(item[0])
c.execute("SELECT count FROM genus WHERE name = ?", [genus_name])
existing_value = c.fetchall()
if len(existing_value)==0:
c.execute('INSERT INTO genus VALUES (?,?)', (genus_name, count))
else:
newcount = count + int(existing_value[0][0])
c.execute("update genus set count = ? where name = ?", (newcount, genus_name))
if os.path.isfile(human_family_file):
with open(human_family_file, 'r') as map_file:
for line in map_file:
item = line.lstrip().split(" ", 1)
family_name = item[1].strip()
count = int(item[0])
c.execute("SELECT count FROM family WHERE name = ?", [family_name])
existing_value = c.fetchall()
if len(existing_value)==0:
c.execute('INSERT INTO family VALUES (?,?)', (family_name, count))
else:
newcount = count + int(existing_value[0][0])
c.execute("update family set count = ? where name = ?", (newcount, family_name))
if os.path.isfile(species_file):
with open(species_file, 'r') as map_file:
for line in map_file:
item = line.lstrip().split(" ", 1)
species_name = item[1].strip()
count = int(item[0])
c.execute("SELECT count FROM species WHERE name = ?", [species_name])
existing_value = c.fetchall()
if len(existing_value)==0:
c.execute('INSERT INTO species VALUES (?,?)', (species_name, count))
else:
newcount = count + int(existing_value[0][0])
c.execute("update species set count = ? where name = ?", (newcount, species_name))
if os.path.isfile(genus_file):
with open(genus_file, 'r') as map_file:
for line in map_file:
item = line.lstrip().split(" ", 1)
genus_name = item[1].strip()
count = int(item[0])
c.execute("SELECT count FROM genus WHERE name = ?", [genus_name])
existing_value = c.fetchall()
if len(existing_value)==0:
c.execute('INSERT INTO genus VALUES (?,?)', (genus_name, count))
else:
newcount = count + int(existing_value[0][0])
c.execute("update genus set count = ? where name = ?", (newcount, genus_name))
if os.path.isfile(family_file):
with open(family_file, 'r') as map_file:
for line in map_file:
item = line.lstrip().split(" ", 1)
family_name = item[1].strip()
count = int(item[0])
c.execute("SELECT count FROM family WHERE name = ?", [family_name])
existing_value = c.fetchall()
if len(existing_value)==0:
c.execute('INSERT INTO family VALUES (?,?)', (family_name, count))
else:
newcount = count + int(existing_value[0][0])
c.execute("update family set count = ? where name = ?", (newcount, family_name))
# Update metadata table, last_update field (flags web page to reload to help throttle down browser)
c.execute("SELECT last_update FROM Metadata")
existing_value = c.fetchall()
if len(existing_value)==0:
initial_value = 1
c.execute('INSERT INTO Metadata VALUES (?)', (initial_value,))
else:
new_stamp = int(existing_value[0][0]) + 1
c.execute("update Metadata set last_update = ? ", (new_stamp,))
conn.commit()
conn.close()