-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadrmcfiles.py
executable file
·318 lines (247 loc) · 8.52 KB
/
readrmcfiles.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/python3
# this is designed to run from the RMC version directory, e.g. 201857.
# it further assumes that the loader information is in a directory ../loader from
# that directory
# owner of database or db to connect to
debug = False
CHUNKSIZE = 100000
import xml.etree.ElementTree as ET
import psycopg2 as psql
from psycopg2.extensions import AsIs
import glob
import gzip
import rdkit
from rdkit import Chem
from rdkit import RDLogger
from pathlib import Path
import os
from dbconnect import getConnection
# global db of hash of all lines added to databae
lines = set()
# shared cache of sql to execute
insertcache = set()
schema = 'RMC_TEMP'
def readfile(fname, key, sql):
"""
read an xml file into the designated database
fname - xml file to read
key - field for the object key
sql - template sql statement to execute for storing the data
"""
print(fname)
tree = ET.parse(gzip.open(fname));
root = tree.getroot()
cur = conn.cursor()
for elem in root:
id = elem.attrib.get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about')
id = id[id.find('#')+1:].strip() # the number is after the pound sign
data = {}
data[key] = id
for subelem in elem:
prefix, has_namespace, tag = subelem.tag.partition('}')
value = subelem.text
if has_namespace:
tag = tag.strip() # strip all namespaces
if tag.startswith('has'):
tag = tag[tag.find('has')+3:]
value = subelem.attrib.get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource')
value = value[value.find('#')+1:].strip() # the number is after the pound sign
data[tag] = value;
if (tag != 'Copyright'):
data[tag] = value
# some file have sub-sub elements
for selm in subelem:
for subsubelem in selm:
prefix, has_namespace, postfix = subsubelem.tag.partition('}')
value = subsubelem.text.strip();
if has_namespace:
tag = postfix.strip() # strip all namespaces
# create array of data when multiple values are present
if tag in data:
newlist = data[tag]
newlist.append(value)
data[tag] = newlist
else:
newlist = list()
newlist.append(value)
data[tag] = newlist
writedb(conn, data, sql)
flush(conn)
print("\t%i records" % (len(lines)))
def sqlfromfile(schemafile):
try:
conn2 = getConnection()
with open(schemafile, 'r') as schema:
print('executing sql from', schemafile)
command = schema.read()
with conn2.cursor() as cur:
cur.execute(command)
conn2.commit()
conn2.close()
except:
print('caught error')
def initdb():
""" initialize the database"""
drop = "drop schema " + schema + " cascade;"
mydir = os.path.dirname(os.path.realpath(__file__))
schemafile = mydir + '/rmc_schema'
with conn.cursor() as cur:
try :
cur.execute(drop)
print("dropped existing schema")
except:
print("failed to drop schema")
conn.commit()
sqlfromfile(schemafile)
def readassay():
""" read the assay data files into the assay table """
sql = 'insert into ' + schema + '.assay (%s) values %s;'
for filepath in glob.iglob('./*_assays_*.xml.gz'):
readfile(filepath, 'aid', sql)
def readcitation():
""" read the citation files into the citation table """
sql = 'insert into ' + schema + '.citation (%s) values %s;'
for filepath in glob.iglob('./*_citations_*.xml.gz'):
readfile(filepath, 'cid', sql)
def readdatapoint():
""" read the datapoint files into the datapoint table """
sql = 'insert into ' + schema + '.datapoint (%s) values %s;'
for filepath in glob.iglob( './*_datapoints_*.xml.gz'):
readfile(filepath, 'did', sql)
def readfact():
""" read the facts file into the fact table """
sql = 'insert into ' + schema + '.fact (%s) values %s;'
for filepath in glob.iglob('./*_facts_*.xml.gz'):
readfile(filepath, 'rxid', sql)
def readtarget():
""" read the targets file into the target table
"""
sql = 'insert into ' + schema + '.target (%s) values %s;'
for filepath in glob.iglob('./*_targets*.xml.gz'):
readfile(filepath, 'tid', sql)
nl = '\n' # newline; cound be \r\n if necessary
def readnextSDfile(file):
""" read the next SDFile from the file of concatenated SDfiles
return a dictionary with the SDFile tags and data, and the SDFile
"""
line = '' # file.readline()
global notEof
sdfile = 'Molname'
tags = {}
blanklinecount = 0
while line != '$$$$':
if line.startswith('> <'): # data tag
tag = line[8:len(line)-1] # skip IDE. part of tag
data = file.readline().strip().decode()
sdfile = sdfile + nl + data
file.readline() # blank line at end
sdfile = sdfile + nl
if tag != 'RIGHT': # skip copyright
tags[tag] = data
line = file.readline().rstrip().decode()
if (len(line.strip()) == 0):
blanklinecount += 1
if blanklinecount > 3:
return 'EOF'
else:
blanklinecount = 0
sdfile = sdfile + line+ nl
sdfile = sdfile.strip()[:sdfile.find('M END')+6]
# create smiles as it is much more compact than the sdfile
# one can add the sdfile too if you want
smiles = ''
try:
mol = Chem.MolFromMolBlock(sdfile)
if mol:
smiles = Chem.MolToSmiles(mol, isomericSmiles=True,canonical=True)
tags['smiles'] = smiles
else:
mol = ''
except:
mol = ''
if debug:
print(tags)
return tags # dictionary
def readsdfiles(fname):
""" read all of the individual SDFiles from the concatenated SDFile """
print('readsdfile ', fname)
sql = 'insert into ' + schema + '.sdfile (%s) values %s;'
lg = RDLogger.logger()
lg.setLevel(RDLogger.CRITICAL)
count = 0
with gzip.open(fname, 'r') as file:
while True:
sdrecord = readnextSDfile(file)
if sdrecord != 'EOF':
writedb(conn, sdrecord, sql)
count += 1
if (count % 50000 == 0):
print('readsdfiles records',count)
else:
break
flush(conn)
print("wrote ", count, " records")
def writedb(conn, data, sql):
""" write a SDFile record the database """
# if not 'XRN' in data.keys() or data['XRN'] == None or data['XRN'] == '':
# return
with conn.cursor() as cur:
columns = data.keys()
values = [data[column] for column in columns]
cmd = cur.mogrify(sql, (AsIs(','.join(columns)), tuple(values))).decode('utf-8')
h = hash(cmd)
if not h in lines:
lines.add(h)
insertcache.add(cmd)
if len(insertcache) >= CHUNKSIZE:
flush(conn)
def flush(conn):
""" flush the data cache to the database """
if (debug):
print('flushed cache of ', len(insertcache) )
statement = '\n'.join(insertcache)
with conn.cursor() as cur:
if len(statement) > 1:
cur.execute(statement)
conn.commit()
insertcache.clear()
def readsdfile():
""" read the SDFiles. This requires special functions because this is not an XML file
"""
path = './*.sdf.gz'
print(path)
for filepath in glob.iglob(path):
readsdfiles(filepath)
#
# import the set of RMC files into a postgres database.
# this assumes that the current directory is the one with the file set.
#
# reads the unzipped gz file as supplied by Elsevier
#
def load():
path = Path('.')
version = os.path.basename(path.parent.absolute())
print('loading version', version)
with conn.cursor() as cur:
cur.execute('insert into ' + schema + '.version (version) values (%s);', (version,))
conn.commit()
readassay()
readcitation()
readdatapoint()
readfact()
readtarget()
readsdfile()
conn=getConnection()
initdb()
load()
mydir = os.path.dirname(os.path.realpath(__file__))
# apply indices
sqlfromfile(mydir + '/rmc_index')
with conn.cursor() as cur:
print('switching schema names')
cur.execute('drop schema if exists rmc cascade')
conn.commit()
cur.execute('alter schema ' + schema + ' rename to rmc')
conn.commit()
conn.close()
print('completed')