-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·149 lines (134 loc) · 4.42 KB
/
manage.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
#!/usr/bin/env python3
import db
from phenvar import get_complete_rsids, get_pmids, get_publication
import time
from sys import argv
from sqlalchemy import select
help_text = """USAGE:
./manage.py <command>
commands:
- initialize creates tables in database, should only be run once
- update_snps gets rsids and populates database
- update_publications gets publications and populates database
- dump_publication prints publication table
- dump_snp prints snp table
- remove_duplicates_snps finds duplicate snp rows and removes them
"""
def help():
print(help_text)
def initialize():
# engine, session = db.create("sqlite:///db.sqlite3")
# db.create_tables(engine)
update_snps()
update_publications()
# db.close(session)
def init_session():
engine, session = db.create("sqlite:///db.sqlite3")
db.create_tables(engine)
return(session)
def dump_publication():
session = init_session()
db.table_dump(session, 'publication')
db.close(session)
return()
def dump_snp():
session = init_session()
db.table_dump(session, 'snp')
db.close(session)
return()
def remove_duplicates_snps():
print("remove_duplicates called")
session = init_session()
rows = db.get_snp_rows(session)
for row in rows:
# print("row type", type(row))
# print(row.rsid)
# print(row.publications)
entries = db.check_snp_duplicates(session, row.rsid, row.publications)
if entries > 1:
print("duplicate found")
items = db.all_filtered_snps(session, row.rsid, row.publications)
count = 0
for item in items:
# keep the first entry found and delete all others
if count == 0:
count += 1
else:
print("deleting ", item)
session.delete(item)
session.commit()
# else:
# print("not a duplicate", entries)
return()
def update_snps():
session = init_session()
p_snps = 0
snps = get_complete_rsids()
# for each rsid, get pmids
for s in snps:
time.sleep(.1)
pubs = get_pmids(s)
if len(pubs) > 0:
p_snps += 1
print("Processed {} snps".format(p_snps))
# if entries not in db, add
for p in pubs:
if not db.check_snp(session=session, id=s, pub=p):
db.add_snp(session, s, p)
# db.add_snp(session, s, p)
# print("snp not in database; adding")
db.close(session)
return()
def update_publications():
session = init_session()
p_pubs = 0
# for row in snps_publications get pmid
rows = db.get_snp_rows(session)
for row in rows:
pmid = row.publications
# check if pmid in db, add if not
if not db.check_publication(session=session, id=pmid):
time.sleep(.1)
info = get_publication(pmid)
db.add_publication(session, id=pmid, title=info["title"], abstract=info["abstract"])
# print("publication not in database; adding")
p_pubs += 1
print("Processed {} pubs".format(p_pubs))
db.close(session)
return()
def old_initialize():
engine, session = db.create("sqlite:///db.sqlite3")
db.create_tables(engine)
p_snps = 0
p_pubs = 0
snps = get_complete_rsids()
for s in snps:
time.sleep(.1)
pubs = get_pmids(s)
if len(pubs) > 0:
p_snps = p_snps + 1
print("Processed {} snps".format(p_snps))
for p in pubs:
if not db.check_snp(session=session, id=s, pub=p):
db.add_snp(session, s, p)
if not db.check_publication(session=session, id=p):
info = get_publication(p)
db.add_publication(session, id=p, title=info["title"], abstract=info["abstract"])
p_pubs = p_pubs + 1
print("Processed {} pubs".format(p_pubs))
db.close(session)
return()
commands = {
"--help": help,
"-h": help,
"initialize": initialize,
"update_snps": update_snps,
"update_publications": update_publications,
"dump_publication": dump_publication,
"dump_snp": dump_snp,
"remove_duplicates_snps": remove_duplicates_snps
}
if len(argv) == 2 and argv[1] in commands:
commands[argv[1]]()
else:
help()