-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
169 lines (141 loc) · 4.48 KB
/
db.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
#!/usr/bin/env python3
"""Imputation challenge sqlite3 DB functions
Author:
Jin Lee ([email protected])
"""
import time
import sys
import sqlite3
from collections import namedtuple
from score_metrics import Score
from logger import log
ScoreDBRecord = namedtuple(
'ScoreDBRecord',
('submission_id', 'team_id', 'submission_fname', 'cell', 'assay',
'bootstrap_id') + Score._fields
)
DB_TABLE_SCORE = 'score'
DB_QUERY_INSERT = 'INSERT INTO {table} ({cols}) VALUES ({values});'
#DB_QUERY_GET = 'SELECT * FROM {table} ORDER BY bootstrap_id, submission_id;'
# to select latest submission
DB_QUERY_GET = 'SELECT t.* FROM {table} t \
INNER JOIN ( \
SELECT team_id, cell, assay, bootstrap_id, max(submission_id) as MaxSID \
FROM {table} \
GROUP BY team_id, cell, assay, bootstrap_id \
) tm WHERE t.team_id = tm.team_id AND t.cell = tm.cell \
AND t.assay = tm.assay AND t.bootstrap_id = tm.bootstrap_id \
AND t.submission_id = tm.MaxSID \
ORDER BY t.bootstrap_id, t.submission_id;'
SCORE_DB_RECORD_VAR_TYPE = ScoreDBRecord(
submission_id='integer NOT NULL',
team_id='integer NOT NULL',
submission_fname='text NOT NULL',
cell='text NOT NULL',
assay='text NOT NULL',
bootstrap_id='integer NOT NULL',
mse='double NOT NULL',
gwcorr='double NOT NULL',
gwspear='double NOT NULL',
mseprom='double NOT NULL',
msegene='double NOT NULL',
mseenh='double NOT NULL',
msevar='double NOT NULL',
mse1obs='double NOT NULL',
mse1imp='double NOT NULL'
)
def write_to_db(score_db_record, db_file):
cols = []
values = []
for attr in score_db_record._fields:
cols.append(str(attr))
val = getattr(score_db_record, attr)
if isinstance(val, str):
val = '"' + val + '"'
else:
val = str(val)
values.append(val)
query = DB_QUERY_INSERT.format(
table=DB_TABLE_SCORE, cols=','.join(cols), values=','.join(values))
log.info('SQL query: {}'.format(query))
while True:
try:
conn = sqlite3.connect(db_file)
c = conn.cursor()
c.execute(query)
c.close()
conn.commit()
conn.close()
except sqlite3.OperationalError as e:
print(e)
conn.close()
time.sleep(1)
continue
else:
break
def read_scores_from_db(db_file, chroms):
"""Read all rows by matching chromosomes
Args:
chroms:
List of chromosome used for scoring. This will be
converted into a comma-separated string and only
rows with matching "chroms" field will be retrieved.
This is to filter out records scored with different
set of chromosomes.
Returns:
All rows ordered by bootstrap_id and team_id
"""
def score_record_factory(cursor, row):
return ScoreDBRecord(*row)
query = DB_QUERY_GET.format(table=DB_TABLE_SCORE) #, chroms=valid_chrs_str)
log.info(query)
while True:
try:
conn = sqlite3.connect(db_file)
conn.row_factory = score_record_factory
c = conn.cursor()
c.execute(query)
result = c.fetchall()
c.close()
conn.close()
except sqlite3.OperationalError as e:
print(e)
conn.close()
time.sleep(1)
continue
else:
break
#if len(result) == 0:
# print('No records found. '
# 'Did you forget to specify "--chrom"?')
return result
def create_db(db_file):
log.info('Creating database...')
try:
conn = sqlite3.connect(db_file)
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS {} ({});'.format(
DB_TABLE_SCORE,
','.join([attr + ' ' + getattr(SCORE_DB_RECORD_VAR_TYPE, attr)
for attr in SCORE_DB_RECORD_VAR_TYPE._fields])))
except Exception as e:
print(e)
sys.exit(1)
finally:
conn.close()
log.info('All done.')
def parse_arguments():
import argparse
import os
parser = argparse.ArgumentParser(
description='ENCODE Imputation Challenge SQLite3 Database creator.')
parser.add_argument('db_file', help='DB file.')
args = parser.parse_args()
if os.path.exists(args.db_file):
raise ValueError('DB file already exists.')
return args
def main():
args = parse_arguments()
create_db(args.db_file)
if __name__ == '__main__':
main()