-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_db.py
More file actions
78 lines (69 loc) · 2.03 KB
/
make_db.py
File metadata and controls
78 lines (69 loc) · 2.03 KB
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
import sqlite3
import json
import os, sys
from sqlite3 import Error
import pypdb
import json
from tqdm import tqdm
def create_connection(db_file):
""" create a database connection to a SQLite database """
conn = None
conn = sqlite3.connect(db_file)
print(sqlite3.version)
#except Error as e:
#print(e)
if conn:
conn.close()
print("closed connection")
def create_tables(f):
conn = sqlite3.connect(f)
cursor = conn.cursor()
cursor.execute("drop table if exists Structures")
sql = '''
create table Structures(
PDB_ID char(4) NOT NULL,
AUTHORS text,
TITLE text,
YEAR INT,
DOI text,
PubMed INT
)
'''
cursor.execute(sql)
conn.commit()
conn.close()
def add_data(conn, table_name, data):
query = ("insert into {} values {}".format(table_name, tuple(data)))
print(query)
conn.execute(query)
conn.commit()
if __name__ == '__main__':
dbf = "./sqldb/test.db"
create_tables(dbf)
conn = sqlite3.connect(dbf)
for item in tqdm(os.listdir("output")):
if "pca_graph" not in item:
continue
else:
try:
prefix = item.split("_")[0]
info = pypdb.get_info(prefix)
data = [prefix]
data.append(",".join(info['citation'][0]['rcsb_authors']))
data.append(info['citation'][0]['title'].replace("\'", "single_quote"))
try:
data.append(info['citation'][0]['year'])
except:
data.append("NULL")
try:
data.append(info['citation'][0]['pdbx_database_id_pub_med'])
except:
data.append("NULL")
try:
data.append(info['citation'][0]['pdbx_database_id_doi'])
except:
data.append("NULL")
add_data(conn, "Structures", data)
except Exception as e:
print(e)
conn.close()