-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize.cpp
executable file
·433 lines (364 loc) · 10.9 KB
/
visualize.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <iostream>
#include <iomanip>
#include <set>
#include <openbabel/obconversion.h>
#include <openbabel/obiter.h>
#include <openbabel/mol.h>
#include "visualize.hpp"
#include <boost/filesystem.hpp>
//#include "cnn_scorer.h"
//#include "molgetter.h"
//#include "model.h"
using namespace OpenBabel;
/*
ColoredMol::ColoredMol (std::string inLigName, std::string inRecName, std::string inModel, std::string inWeights, float inSize, std::string inOutRec, std::string inOutLig, const cnn_options& cnnopts, FlexInfo& finfo, tee& log, const vec& center, bool inNo_frag, bool inVerbose)
*/
ColoredMol::ColoredMol (std::string inLigName, std::string inRecName, std::string inModel, std::string inWeights, float inSize, std::string inOutRec, std::string inOutLig, bool inNo_frag, bool inVerbose)
{
ligName = inLigName;
recName = inRecName;
cnnmodel = inModel;
weights = inWeights;
size = inSize;
outRec = inOutRec;
outLig = inOutLig;
no_frag = inNo_frag;
verbose = inVerbose;
/*
MolGetter mols;
mols.create_init_model(recName, "", finfo, log);
model* testrec = new model;
model* testlig = new model;
CNNScorer cnn_scorer(cnnopts, center, mols.getInitModel());
mols.setInputFile(ligName);
bool worked = mols.readMoleculeIntoModel(*testlig);
std::cout << "Worked: " << worked << '\n';
float theScore = cnn_scorer.score(*testlig);
std::cout << "SCORE: " << theScore << '\n';
std::cout << "END COLOR HERE" << '\n';
*/
}
void ColoredMol::color()
{
OBConversion conv;
std::string ext = boost::filesystem::extension(ligName);
if(ext.compare(".pdb") == 0)
{
conv.SetInFormat("PDB");
}
else if(ext.compare(".pdbqt") == 0)
{
conv.SetInFormat("PDBQT");
}
else
{
std::cout << "File extension not supported: " << ligName << '\n';
std::cout << "Please use .pdb or .pdbqt for ligand\n";
exit(0);
}
conv.ReadFile(&ligMol, ligName);
ext = boost::filesystem::extension(recName);
if(ext.compare(".pdb") == 0)
{
conv.SetInFormat("PDB");
}
else
{
std::cout << "File extension not supported: " << recName << '\n';
std::cout << "Please use .pdb for receptor\n";
exit(0);
}
conv.ReadFile(&recMol, recName);
addHydrogens();
ligCenter();
removeResidues();
removeEachAtom();
}
void ColoredMol::print()
{
std::cout << "ligName: " << ligName << '\n';
std::cout << "recName: " << recName << '\n';
std::cout << "cnnmodel: " << cnnmodel << '\n';
std::cout << "size: " << size << '\n';
std::cout << "outRec: " << outRec << '\n';
std::cout << "outLig: " << outLig << '\n';
std::cout << "no_frag: " << no_frag << '\n';
std::cout << "verbose: " << verbose << '\n';
}
float ColoredMol::removeAndScore(std::vector<bool> removeList, bool isRec)
{
std::string molString;
OBMol mol;
if(isRec)
{
molString = hRec;
mol = hRecMol;
}
else
{
molString = hLig;
mol = hLigMol;
}
if(!(isRec)) //if ligand
{
OBAtom* atom;
for(int i = 0;i < removeList.size(); ++i)
{
if (removeList[i]) //index is in removeList
{
atom = mol.GetAtom(i);
FOR_NBORS_OF_ATOM(neighbor, atom)
{
if(neighbor->GetAtomicNum() == 1)
{
//std::cout << "adding: " << neighbor->GetIdx() << '\n';
removeList[neighbor->GetIdx()] = true;
}
}
}
}
}
else //if receptor
{
//make set for inRange test
std::set<int> removeSet;
for (int i = 0; i < removeList.size(); ++i)
{
if (removeList[i])
{
removeSet.insert(i);
}
}
if (!(inRange(removeSet)))
{
return 0.00;
}
}
std::stringstream ss;
std::stringstream molStream(molString);
ss << "ROOT\n"; //add necessary lines for gnina parsing
std::string line;
while(std::getline(molStream, line))
{
if((line.find("HETATM") < std::string::npos) ||
(line.find("ATOM") < std::string::npos))
{
std::string firstNumString = line.substr(7,5);
int firstNum = std::stoi(firstNumString);
if (!(removeList[firstNum])) //don't write line if in list
{
ss << line << '\n';
}
}
}
ss << "ENDROOT\n";
ss << "TORSDOF 0\n";
float scoreVal = score();
}
void ColoredMol::addHydrogens()
{
recMol.AddHydrogens();
ligMol.AddHydrogens();
OBConversion conv;
conv.SetInFormat("PDB");
conv.SetOutFormat("PDB");
recPDB = conv.WriteString(&recMol); //store rec pdb to preserve residue info
conv.SetOutFormat("PDBQT"); //use pdbqt to make passing to parse_pdbqt easier
conv.AddOption("r",OBConversion::OUTOPTIONS);
conv.AddOption("c",OBConversion::OUTOPTIONS);
hLig = conv.WriteString(&ligMol);
hRec = conv.WriteString(&recMol);
conv.SetInFormat("PDBQT");
conv.ReadString(&hRecMol, hRec);
conv.ReadString(&hLigMol, hLig);
}
float ColoredMol::score(){return (float)1.11;}
void ColoredMol::writeScores(std::vector<float> scoreList, bool isRec)
{
std::string filename;
std::string molString;
if(isRec)
{
filename = outRec;
molString = hRec;
}
else
{
filename = outLig;
molString = hLig;
}
std::ofstream outFile; outFile.open(filename);
outFile << "CNN MODEL: " << cnnmodel << '\n';
outFile << "CNN WEIGHTS: " << weights << '\n';
std::stringstream molStream(molString);
std::string line;
std::string indexString;
int index;
std::stringstream scoreStream;
std::string scoreString;
while(std::getline(molStream, line))
{
if ((line.find("ATOM") < std::string::npos) ||
(line.find("HETATM") < std::string::npos))
{
scoreStream.str(""); //clear stream for next score
indexString = line.substr(6,5);
index = std::stoi(indexString);
if ((scoreList[index] > 0.001) || (scoreList[index] < -0.001)) //ignore very small scores
{
scoreStream << std::fixed << std::setprecision(5) << scoreList[index];
outFile << line.substr(0,61);
scoreString = scoreStream.str();
scoreString.resize(5);
outFile.width(5);
outFile.fill('.');
outFile << std::right << scoreString;
outFile << line.substr(66) << '\n';
}
else
{
outFile << line << '\n';
}
}
else
{
outFile << line << '\n';
}
}
}
bool ColoredMol::inRange(std::set<int> atomList)
{
float x = cenCoords[0];
float y = cenCoords[1];
float z = cenCoords[2];
float allowedDist = size / 2;
int numAtoms = recMol.NumAtoms();
OBAtom* atom;
for( auto i = atomList.begin(); i != atomList.end(); ++i)
{
if (*i >= numAtoms)
{
return false;
}
atom = recMol.GetAtom(*i);
if(atom->GetX() < x + allowedDist)
if (atom->GetY() < y + allowedDist)
if (atom->GetZ() < z + allowedDist)
if (atom->GetX() > x - allowedDist)
if (atom->GetY() > y - allowedDist)
if (atom->GetZ() > z - allowedDist)
return true;
}
return false;
}
void ColoredMol::ligCenter()
{
vector3 cen = hLigMol.Center(0);
cenCoords[0] = cen.GetX();
cenCoords[1] = cen.GetY();
cenCoords[2] = cen.GetZ();
}
std::vector<float> ColoredMol::transform(std::vector<float> inList)
{
std::vector<float> outList (inList.size());
float tempVal;
for (int i = 0; i < inList.size(); ++i)
{
tempVal = inList[i];
if(tempVal < 0)
{
tempVal = 0 - std::sqrt(std::abs(tempVal));
}
else
{
tempVal = std::sqrt(tempVal);
}
tempVal = tempVal * 100;
std::cout << inList[i] << " : " << tempVal << '\n';
outList[i] = tempVal;
}
for (int i = 0; i < outList.size(); ++i)
{
std::cout << outList[i] << '\n';
}
return outList;
}
void ColoredMol::removeResidues()
{
std::vector<float> scoreDict(hRecMol.NumAtoms() + 1, 0.00);
std::string lastRes = "";
std::string currRes;
std::vector<bool> atomList (hRecMol.NumAtoms() + 1, false);
std::set<std::string> resList;
std::string molString = hRec;
std::stringstream molStream(molString);
std::string line;
while(std::getline(molStream, line))
{
if((line.find("ATOM") < std::string::npos) ||
(line.find("HETATM") < std::string::npos))
{
currRes = line.substr(23,4);
if(line.substr(23,4) != lastRes)
{
resList.insert(currRes);
lastRes = currRes;
}
}
}
for( auto i = resList.begin(); i != resList.end(); ++i)
{
molStream.clear();
molStream.str(molString);
while(std::getline(molStream, line))
{
if((line.find("ATOM") < std::string::npos) ||
(line.find("HETATM") < std::string::npos))
{
if(line.substr(23,4) == *i)
{
std::string indexString = line.substr(6,5);
int index = std::stoi(indexString);
atomList[index] = true;
}
}
}
float scoreVal = removeAndScore(atomList, true);
for ( auto f : atomList)
{
if(f)
{
scoreDict[f] = scoreVal;
}
}
atomList.clear();
atomList = std::vector<bool>(hRecMol.NumAtoms() + 1, false);
}
writeScores(scoreDict, true);
}
void ColoredMol::removeEachAtom()
{
std::vector<float> scoreDict(hLigMol.NumAtoms());
std::stringstream ss (hLig);
std::string line;
std::string indexString;
int index;
std::vector<bool> removeList (hLigMol.NumAtoms() + 1);
float scoreVal;
while(std::getline(ss, line))
{
if ((line.find("ATOM") < std::string::npos) ||
(line.find("HETATM") < std::string::npos))
{
indexString = line.substr(6, 5);
index = std::stoi(indexString);
if (hLigMol.GetAtom(index)->GetAtomicNum() != 1)
{
removeList[index] = true;
scoreVal = removeAndScore(removeList, false);
removeList[index] = false;
scoreDict[index] = scoreVal;
}
}
}
}