-
Notifications
You must be signed in to change notification settings - Fork 7
/
managemols.py
245 lines (209 loc) · 8.63 KB
/
managemols.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
import pybel
import openbabel
import csv
from openmoldbmolecules.models import Molecule
"""
THIS IS OBSOLETE! USE add2db.py
"""
def add_mols(data):
namec = ""
altnamec = ""
suppc = ""
suppidc = ""
storc = ""
storidc = ""
amountc = ""
unitc = ""
casc = ""
altsupplierc = ""
altsupplierIDc = ""
commc = ""
classc = ""
counter = 0
head = data.next()
for c in head:
#Get the indexes of columns
cl = c.lower()
if cl == "name":
namec = counter
elif cl == "altname":
altnamec = counter
elif cl == "supplier":
suppc = counter
elif cl == "supplierid":
suppidc = counter
elif cl == "storage":
storc = counter
elif cl == "storageid":
storidc = counter
elif cl == "amount":
amountc = counter
elif cl == "unit":
unitc = counter
elif cl == "cas":
casc = counter
elif cl == "smiles":
smilesc = counter
elif cl == "supplier2":
altsupplierc = counter
elif cl == "supplierid2":
altsupplierIDc = counter
elif cl == "comment":
commc = counter
elif cl == "molclass":
classc = counter
counter += 1
if type(smilesc) == int or type(namec) == int:
# has to have either smiles or name column
print "Following columns were found and will be imported:"
if type(namec) == int:
print head[namec] + " ",
if type(altnamec) == int:
print head[altnamec] + " ",
if type(suppc) == int:
print head[suppc] + " ",
if type(suppidc) == int:
print head[suppidc] + " ",
if type(storc) == int:
print head[storc] + " ",
if type(storidc) == int:
print head[storidc] + " ",
if type(amountc) == int:
print head[amountc] + " ",
if type(unitc) == int:
print head[unitc] + " ",
if type(casc) == int:
print head[casc] + " ",
if type(smilesc) == int:
print head[smilesc] + " ",
if type(altsupplierc) == int:
print head[altsupplierc] + " ",
if type(altsupplierIDc) == int:
print head[altsupplierIDc] + " ",
if type(commc) == int:
print head[commc] + " ",
if type(classc) == int:
print head[classc] + " ",
print ""
userinput = raw_input("Is this ok? yes/no: ")
#Check with user if everything looks OK
if userinput == "yes":
for line in data:
#do some datachecks and encode in ACSII since some databases have problems with UTF-8
if type(namec) == int:
name = line[namec].decode("windows-1252").encode('utf-8','ignore')
else:
name = ""
if type(altnamec) == int:
altname = line[altnamec].decode("windows-1252").encode('utf-8','ignore')
else:
altname = ""
if type(suppc) == int:
supp = line[suppc].decode("windows-1252").encode('utf-8','ignore')
else:
supp = ""
if type(suppidc) == int:
suppid = line[suppidc].decode("windows-1252").encode('utf-8','ignore')
else:
suppid = ""
if type(storc) == int:
storage = line[storc].decode("windows-1252").encode('utf-8','ignore')
else:
storage = ""
if type(storidc) == int:
storageid = line[storidc].decode("windows-1252").encode('utf-8','ignore')
else:
storageid = ""
if type(amountc) == int:
amount = line[amountc]
else:
amount = ""
if type(unitc) == int:
unit = line[unitc].decode("windows-1252").encode('utf-8','ignore')
else:
unit = ""
if type(casc) == int:
cas = line[casc].decode("windows-1252").encode('utf-8','ignore')
else:
cas = ""
if type(smilesc) == int:
smiles = line[smilesc].decode("windows-1252").encode('utf-8','ignore')
else:
smiles = ""
if type(altsupplierc) == int:
altsupplier = line[altsupplierc].decode("windows-1252").encode('utf-8','ignore')
else:
altsupplier = ""
if type(altsupplierIDc) == int:
altsupplierID = line[altsupplierIDc].decode("windows-1252").encode('utf-8','ignore')
else:
altsupplierID = ""
if type(commc) == int:
comm = line[commc].decode("windows-1252").encode('utf-8','ignore')
else:
comm = ""
if type(classc) == int:
molclass = line[classc].decode("windows-1252").encode('utf-8','ignore')
else:
molclass = ""
try:
amount = float(line[amountc])
unit = line[unitc]
except:
amount = 0
unit = "X"
try:
mol = pybel.readstring("smi",smiles)
descs = mol.calcdesc()
#generate 2D coordinates, needs openbabel
obConversion = openbabel.OBConversion()
obConversion.SetInAndOutFormats("smi", "mdl")
obmol = openbabel.OBMol()
obConversion.ReadString(obmol, smiles)
gen2d = openbabel.OBOp.FindType("gen2d")
gen2d.Do(obmol)
MDL = obConversion.WriteString(obmol)
outMDL = MDL.replace("\n", r"\n")
CMW=descs["MW"]
CHN=mol.formula
HBA=descs["HBA1"]
HBD=descs["HBD"]
logP=descs["logP"]
tpsa=descs["TPSA"]
#Get number of rotatable bonds
smarts = pybel.Smarts("[!$([NH]!@C(=O))&!D1&!$(*#*)]\&!@[!$([NH]!@C(=O))&!D1&!$(*#*)]")
rb = smarts.findall(mol)
nrb = len(rb)
#Calculate Fsp3
sp3c = pybel.Smarts("[CX4]")
nsp3c = sp3c.findall(mol)
nsp3c = float(len(nsp3c))
allc = pybel.Smarts("[#6]")
nallc = allc.findall(mol)
nallc = float(len(nallc))
if nallc > 0:
fsp3 = nsp3c/nallc
print fsp3
else:
fsp3 = ""
#Get fingerprint and molecular complexity
fprint = mol.calcfp()
bitson = fprint.bits
nbitson = len(bitson)
print name
m = Molecule(name=name,SMILES=smiles, altname=altname, supplier=supp, supplierID=suppid, CMW=descs["MW"], CHN=CHN, HBA=HBA, HBD=HBD, logP=logP, tpsa=tpsa, amount=amount, unit=unit, CAS=cas, storage=storage, storageID=storageid, molfile=outMDL, nrb=nrb, fingerprint=bitson, complexity=nbitson, altsupplier=altsupplier, altsupplierID=altsupplierID , comment=comm, molclass=molclass, fsp3=fsp3)
m.save()
except:
m = Molecule(name=name,SMILES=smiles, altname=altname, supplier=supp, supplierID=suppid, amount=amount, unit=unit, CAS=cas, storage=storage, storageID=storageid, altsupplier=altsupplier, altsupplierID=altsupplierID , comment=comm, molclass=molclass)
m.save()
#Save data to database
else:
print "Exiting, no changes were made..."
return False
else:
print "No valid columns were found in the table"
if __name__ == '__main__':
import sys
datacard = sys.argv[1]
data = csv.reader(open(datacard), dialect='excel')
add_mols(data)