-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjchemspider.py
235 lines (182 loc) · 6.71 KB
/
jchemspider.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
#chemspider code
import requests
"""
obabel and babel can be used in the command line
$ obabel ws_no_smiles.inchi -O ws_no_smiles.smi
$ babel ws_no_smiles.inchi -O ws_no_smiles.smi
"""
def post_i2s( inchi_list, flag_confirm = True):
"""
post_i2s is more stable than inchi_to_smi
"""
tot_err = 0
smi_list = []
# text_list = {}
for ix, ic in enumerate(inchi_list):
data = {"Content-Type": "application/x-www-form-urlencoded", "inchi": ic}
r = requests.post( "http://www.chemspider.com/InChI.asmx/InChIToSMILES", data=data)
# test_list[ ix] = r.content
smi_list.append( r.content[83:-9])
if flag_confirm:
print ix, '>SMILES<', r.content[82:-8],
#Confirmation of data
if r.content[82] == '>' and r.content[-9] == '<':
print 'O.k'
else:
print 'Error!!'
print r.content
tot_err += 1
if flag_confirm:
print "#Total Error is", tot_err
return smi_list
def inchi_to_smi( inchi_list, flag_confirm = True):
"""
[Reference]
https://www.chemspider.com/InChI.asmx?op=InChIToSMILES
Since it is web protocol,
this should be confirmed for later use --> r.content[83:-9]
"""
smi_list = {}
# text_list = {}
for ix, ic in enumerate(inchi_list):
r = requests.get( 'http://www.chemspider.com/InChI.asmx/InChIToSMILES?inchi={} HTTP/1.1'.format( ic))
# test_list[ ix] = r.content
smi_list[ ix] = r.content[83:-9]
if flag_confirm:
print ix, '>SMILES<', r.content[82:-8],
#Confirmation of data
if r.content[82] == '>' and r.content[-9] == '<':
print 'O.k'
else:
print 'Error!!'
print r.content
return smi_list
def inchi_to_smi_l( inchi_list, flag_confirm = True):
"""
[Reference]
https://www.chemspider.com/InChI.asmx?op=InChIToSMILES
Since it is web protocol,
this should be confirmed for later use --> r.content[83:-9]
"""
smi_list = {}
# text_list = {}
for ix, ic in enumerate(inchi_list):
r = requests.get( 'http://www.chemspider.com/InChI.asmx/InChIToSMILES?inchi={} HTTP/1.1'.format( ic))
# test_list[ ix] = r.content
smi_list[ ix] = r.content[83:-9]
if flag_confirm:
print ix, '>SMILES<', r.content[82:-8],
#Confirmation of data
if r.content[82] == '>' and r.content[-9] == '<':
print 'O.k'
else:
print 'Error!!'
print r.content
return smi_list
def casno_to_inchi( casno_l, flag_confirm = True, engine = 'nist'):
inch_l = []
for ii, casno in enumerate(casno_l):
inchi = casno_to_inchi_each( casno, engine = engine)
if inchi.startswith('InChI='):
inch_l.append( inchi)
print "{} - success".format( ii)
else:
inch_l.append( "")
print "{} - error".format( ii)
return inch_l
def _casno_to_inchi_each_r0( casno):
"""http://webbook.nist.gov/chemistry/"""
url_cmd = 'http://webbook.nist.gov/cgi/cbook.cgi?ID={}&Units=SI'.format( casno)
r = requests.post( url_cmd)
st = r.content.find('<tt>InChI') + 4 #<tt> is added in front of string
ed = r.content[st:].find('</tt>') + st
return r.content[st:ed]
def casno_to_inchi_each( casno, engine = 'nist'):
"""
http://webbook.nist.gov/chemistry/
http://www.chemnet.com/cas/supplier.cgi?terms=127-07-1&l=en&exact=dict&f=plist&mark=&submit.x=0&submit.y=0
http://www.chemnet.com/cas/supplier.cgi?terms=541-48-0&l=en&exact=dict&f=plist&mark=&submit.x=0&submit.y=0
"""
if engine == "chemnet":
url_cmd = 'http://www.chemnet.com/cas/supplier.cgi?terms={}&l=en&exact=dict&f=plist&mark=&submit.x=0&submit.y=0'.format( casno)
r = requests.get( url_cmd)
st = r.content.find('InChI=')
ed = r.content[st:].find('</td>') + st
else: # engine == "nist" or else
url_cmd = 'http://webbook.nist.gov/cgi/cbook.cgi?ID={}&Units=SI'.format( casno)
r = requests.post( url_cmd)
st = r.content.find('<tt>InChI') + 4 #<tt> is added in front of string
ed = r.content[st:].find('</tt>') + st
return r.content[st:ed]
def name_to_inchi_each( name):
url_cmd = 'http://webbook.nist.gov/cgi/cbook.cgi?Name={}&Units=SI'.format( name)
r = requests.post( url_cmd)
st = r.content.find('<tt>InChI') + 4 #<tt> is added in front of string
ed = r.content[st:].find('</tt>') + st
return r.content[st:ed]
def name_to_inchi_each( name, engine = 'nist'):
if engine == 'nih':
# http://cactus.nci.nih.gov/chemical/structure/Carbanilide/stdinchi
url_cmd = 'http://cactus.nci.nih.gov/chemical/structure/{}/stdinchi'.format( name)
r = requests.post( url_cmd)
st, ed = 0, None
else: #engine = 'nist'
url_cmd = 'http://webbook.nist.gov/cgi/cbook.cgi?Name={}&Units=SI'.format( name)
r = requests.post( url_cmd)
st = r.content.find('<tt>InChI') + 4 #<tt> is added in front of string
ed = r.content[st:].find('</tt>') + st
return r.content[st:ed]
def name_to_inchi( name_l, flag_confirm = True, engine = 'nist'):
inch_l = []
for ii, name in enumerate( name_l):
inchi = name_to_inchi_each( name, engine = engine)
if inchi.startswith('InChI='):
inch_l.append( inchi)
if flag_confirm: print "{} - success".format( ii)
else:
inch_l.append( "")
if flag_confirm: print "{} - error".format( ii)
return inch_l
def inchi_to_casno( inchi_l, flag_confirm = True):
casno_l = []
for ii, name in enumerate( inchi_l):
casno = inchi_to_casno_each( name)
if all ([x.isdigit() for x in casno.split('-')] ):
casno_l.append( casno)
if flag_confirm: print "{} - success".format( ii)
else:
casno_l.append( "")
if flag_confirm: print "{} - error".format( ii)
return casno_l
def inchi_to_casno_each( inchi):
# inchi = 'InChI=1S/C14H8O2/c15-13-9-5-1-2-6-10(9)14(16)12-8-4-3-7-11(12)13/h1-8H'
# http://webbook.nist.gov/cgi/cbook.cgi?InChI=
url_cmd = 'http://webbook.nist.gov/cgi/cbook.cgi?InChI={}&Units=SI'.format( inchi)
r = requests.post( url_cmd)
st_keyword = '<li><strong>CAS Registry Number:</strong>'
st = r.content.find('<li><strong>CAS Registry Number:</strong>') + len(st_keyword)
ed = r.content[st:].find('</li>') + st
return r.content[st:ed].strip()
def inchi_to_casno_each_test( inchi):
# inchi = 'InChI=1S/C14H8O2/c15-13-9-5-1-2-6-10(9)14(16)12-8-4-3-7-11(12)13/h1-8H'
# http://webbook.nist.gov/cgi/cbook.cgi?InChI=
url_cmd = 'http://webbook.nist.gov/cgi/cbook.cgi?InChI={}&Units=SI'.format( inchi)
r = requests.post( url_cmd)
print r.content
st_keyword = '<li><strong>CAS Registry Number:</strong>'
st = r.content.find('<li><strong>CAS Registry Number:</strong>') + len(st_keyword)
ed = r.content[st:].find('</li>') + st
return r.content[st:ed].strip()
def inchi_to_csid( inchi_l):
csid_ll = list()
for ID, inchi in enumerate(inchi_l):
print ID, inchi,
csid_each_l = list()
for result in cs.search( inchi):
print( result, result.csid)
csid_each_l.append( result.csid)
if len(csid_each_l) == 1:
csid_ll.append( csid_each_l[0])
else:
csid_ll.append( csid_each_l)
return csid_ll