-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
50 lines (35 loc) · 1.33 KB
/
test.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
#!/usr/bin/env python3
import bz2
import json
import sqlite3
import sys
def build_db_merged(fname):
"""
Build a dictionary of snp to merged snp. We must track revision because there may be multiple
instances of the same snp so we take the highest revision.
"""
db = {}
with bz2.open(fname) as f:
for line in f:
j = json.loads(line)
refsnp_id = int(j["refsnp_id"])
for entry in j["dbsnp1_merges"]:
merged_rsid = int(entry["merged_rsid"])
revision = int(entry["revision"])
if merged_rsid in db:
if revision > db[merged_rsid][1]:
db[merged_rsid] = (refsnp_id, revision)
else:
db[merged_rsid] = (refsnp_id, revision)
return db
db = build_db_merged(
"/shares/hii/bioinfo/ref/ncbi/snp/archive/b156/JSON/refsnp-merged.json.bz2"
)
print("start")
c = sqlite3.connect("tmp/snptk2.db")
c.execute("DROP TABLE IF EXISTS merged")
c.execute("CREATE TABLE merged (rsid INTEGER PRIMARY KEY, rsid_merged INTEGER)")
c.executemany("INSERT INTO merged VALUES (?, ?)", ((k, db[k][0]) for k in sorted(db)))
c.commit()
# /shares/hii/bioinfo/ref/ncbi/snp/archive/b156/JSON/refsnp-merged.json.bz2
# /shares/hii/bioinfo/ref/ncbi/snp/archive/b156/JSON/refsnp-chr21.json.bz2