-
Notifications
You must be signed in to change notification settings - Fork 5
/
ANM.cpp
454 lines (376 loc) · 11.5 KB
/
ANM.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// ANM.cpp
// Performs Normal Mode Analysis using the ANM tool
// Author: Caroline Ross: caroross299@gmail.com
// August 2017
//Performs Normal Mode Analysis using the ANM tool
//The script uses the alglib library and the following must be included for the script to run. These are contained in src folder in a cpp folder of the alglib package. When complying this code use g++ -I path/cpp/src as an option
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <string>
#include <sstream>
#include <limits>
#include <specialfunctions.h>
#include <specialfunctions.cpp>
#include <iostream>
#include <linalg.h>
#include <linalg.cpp>
#include <alglibinternal.h>
#include <alglibinternal.cpp>
#include <alglibmisc.h>
#include <alglibmisc.cpp>
#include <ap.h>
#include <ap.cpp>
#include <vector>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
using namespace std;
using namespace alglib;
int countAtoms()//counts Carbon Atoms (Beta carbons for all residues but Alpha carbons for Glycine)
{
vector< vector<int> > row;
}//countAtoms
vector< vector<double> > getCoOrds(string pdbInput,string atype)// gets the x,y,z cordinates for all the carbon atoms in PDB file. Returns 3 X numAtoms Matrix
{
vector< vector<double> > C;
vector<double> atomC;
ifstream mfile (pdbInput.c_str());// This is the PDB file that will be coarse grained.
string li;
// file is open and not empty
while (! mfile.eof() )
{
getline (mfile,li);
istringstream iss(li);
string atom, temp, type,res,x,y,z;
iss>>atom;
iss>>temp;
iss>>type;
iss>>res;
iss>>temp;
iss>>temp;
iss>>x;
iss>>y;
iss>>z;
if (atom=="ATOM")
{
if((res=="GLY"&&type=="CA")||type==atype)// This selects only CA atoms for CB atoms use if((res=="GLY"&&type=="CA")||type=="CB") such that CA are selected in the case of Glycine
{
atomC.push_back(atof(x.c_str()));
atomC.push_back(atof(y.c_str()));
atomC.push_back(atof(z.c_str()));
C.push_back(atomC);
atomC.clear();
}//if GLY
}//if ATOM
}//while
mfile.close();
return C;
}//cords
vector< vector<double> > getForceConstants(vector<double> atom1,vector<double> atom2, double cutoff)// returns a 9x9 vector for the interaction between two nodes
{
vector< vector<double> > dv2k;
vector<double> dv2kROW;
//calculate distance squared
double dist2 =0.0;
for (int i =0; i<3; i++)
{
dist2 = dist2+((atom2[i]-atom1[i])*(atom2[i]-atom1[i]));
}//for to instantiate distance
double dv2ka;
//if outside cutoff or atom1=atom2
if (dist2>cutoff)//parameter
{
for(int i=0;i<3;i++)
{
dv2kROW.push_back(0.0);
dv2kROW.push_back(0.0);
dv2kROW.push_back(0.0);
dv2k.push_back(dv2kROW);
//reset dv2kROW
dv2kROW.clear();
}//for to populate row by row: times 3 rows
}//if
else
{
for (int i=0; i<3; i++)
{
for (int j=0; j<3; j++)
{
dv2ka = -((atom2[i]-atom1[i])*(atom2[j]-atom1[j]))/dist2;
dv2kROW.push_back(dv2ka);
}//for
dv2k.push_back(dv2kROW);
dv2kROW.clear();
}//for
}//else
return dv2k;
}// getForceConstants
vector< vector<double> > getHessian(vector< vector<double> > C, double cutoff)// this calls the getForceConstants to set up the Hessian Matrix, we manipulate these force constants and the populate the hessian
{
vector< vector<double> > Hessian; //Full 3Nx3N Hessian
vector<double> rowX;
vector<double> rowY;
vector<double> rowZ;
vector< vector<double> > interaction; //9x9 vector for each atom-atom interaction
vector< vector<double> > diagonal; //holds 9x9 vector of values on a diagonal
vector<double> diagonalROW; //holds 9x9 vector of values on a diagonal
vector<double> atom1;
vector<double> atom2;
for (int i=0; i < C.size(); i++)
{
//reset the diagonal
for (int d = 0; d<3; d++)
{
diagonalROW.push_back(0.0);
diagonalROW.push_back(0.0);
diagonalROW.push_back(0.0);
diagonal.push_back(diagonalROW);
diagonalROW.clear();
}// set each element = 0
//get x,y,z of atom1
atom1 = C[i];
//Nested loop for atom-atom interactions
for (int j=0; j < C.size(); j++)
{
//get x,y,z of interacting atom
atom2 = C[j];
if(i!=j)
{
interaction = getForceConstants(atom1,atom2,cutoff);
for(int ir = 0; ir<3; ir++)
{
rowX.push_back(interaction[0][ir]);
rowY.push_back(interaction[1][ir]);
rowZ.push_back(interaction[2][ir]);
}//itterate RowX,Y,Z
//itterate summation of digonals
for (int ir = 0; ir<3; ir++)
{
for (int ic = 0; ic<3; ic++)
{
diagonal[ir][ic] = diagonal[ir][ic]-interaction[ir][ic];
}// increase each element
}// increase each element
interaction.clear();
}//if i!=j
else
{
for(int ir = 0; ir<3; ir++)
{
rowX.push_back(0.0);
rowY.push_back(0.0);
rowZ.push_back(0.0);
}//itterate RowX,Y,Z
}//set diagonal
}//for atoms
//Update diagonal
for(int ir = 0; ir<3; ir++)
{
rowX[i*3+ir] = diagonal[0][ir];
rowY[i*3+ir] = diagonal[1][ir];
rowZ[i*3+ir] = diagonal[2][ir];
}//itterate RowX,Y,Z
//Reset diagonal
diagonal.clear();
//Update Hessian by row
Hessian.push_back(rowX);
rowX.clear();
Hessian.push_back(rowY);
rowY.clear();
Hessian.push_back(rowZ);
rowZ.clear();
}//for atoms
return Hessian;
}// getHessian
// Get current date/time, format is YYYY-MM-DD HH:mm:ss
const std::string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
return buf;
}
int main(int argc, char *argv[])
{
//Init vars
double cutoff = 15;// this must be user define
string pdbInput, outdir = "output";
string atype="X";
bool hasPdb = false;
// Welcome message
cout<< "============================================================\n"<<endl;
cout<< "\t:-) >>------->"<<argv[0]<<"<-------<< (-:\t\n"<<endl;
cout<< "\tAuthor(s): Caroline Ross (caroross299@gmail.com)\t\t\t\t"<<endl;
cout<< "\tResearch Unit in Bioinformatics (RUBi)\t\t"<<endl;
cout<< "\tRhodes University, 2017\t\t\t\t"<<endl;
cout<< "\tDistributed under GNU GPL 3.0\t\t\t\n"<<endl;
cout<< "\thttps://github.com/RUBi-ZA/MODE-TASK\t\n"<<endl;
cout<< "============================================================"<<endl;
// Begin parameter handling
// Add more else if statements for further parameters
int i;
for(i=0; i<argc; ++i)
{
if (strcmp(argv[i], "-h") == 0)
{
cout<<"usage: ANM [-h] [--pdb PDB] [--cutoff INTEGER]"<<endl;
cout<<" [--outdir DIRECTORY] [--atomType (CA/CB)]"<<endl;
cout<<"arguments:"<<endl;
cout<<" -h, --help Show this help message and exit"<<endl;
cout<<" --pdb PDB input file"<<endl;
cout<<" --cutoff Cuttoff radius in Angstroms. Default: 15"<<endl;
cout<<" --outdir Directory to generate output to"<<endl;
cout<<" --atomType Select 'CA' or 'CB' atoms"<<endl;
return -1;
}
else if(strcmp(argv[i], "--pdb") == 0)
{
pdbInput = argv[i+1];
hasPdb = true;
}
else if(strcmp(argv[i], "--cutoff") == 0)
{
cutoff = atof(argv[i+1]);
//hasCutoff = true;
}
else if(strcmp(argv[i], "--outdir") == 0)
{
outdir = argv[i+1];
//hasOutdir = true;
}
else if(strcmp(argv[i], "--atomType") == 0)
{
atype = argv[i+1];
//hasOutdir = true;
}
}
// Check if output directory exists and create it
struct stat statStruct;
stat(outdir.c_str(), &statStruct);
if (!S_ISDIR(statStruct.st_mode))
cout<<"Creating directory '"<<outdir<<"'"<<endl;
const int dir_err = mkdir(outdir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if(!hasPdb)
{
cout<<"A PDB file is required, use '-h' to view help"<<endl;
return -1;
}
if (atype=="X") {
cout<<endl<<"**************************************"<<endl<<"ERROR: ATOM TYPE MUST BE SPECIFIED"<<endl<<"Input Options:"<<endl<<"CA: to select alpha carbon atoms"<<endl<<"CB: to select beta carbon atoms"<<endl<<"Exiting..."<<endl<<"**************************************"<<endl;
return -1;
}//if
ifstream mfile (pdbInput.c_str());// This is the PDB file that will be coarse grained.
if (mfile.good() == false) {
cout<<endl<<"**************************************"<<endl<<"ERROR: Specified PDB file does not exist. Exiting..."<<endl<<"**************************************"<<endl;
return -1;
}//if
mfile.close();
cutoff = cutoff * cutoff;
string eigenvalueMatrixFile = outdir + "/W_values.txt";
string eigenvalueVTFile = outdir + "/VT_values.txt";
string eigenvalueUFile = outdir + "/U_values.txt";
// End parameter handling
// Start cronometer
const int ONE_HOUR = 60 * 60;
const int ONE_MINUE = 60;
int hour;
int min;
int sec;
std::cout << "Started at: " << currentDateTime() << std::endl;
clock_t tStart = clock();
if (atype!="CA" && atype!="CB")
{
cout<<endl<<"**************************************"<<endl<<"Unrecognised atom type"<<endl<<"Input Options:"<<endl<<"CA: to select alpha carbon atoms"<<endl<<"CB: to select beta carbon atoms"<<endl<<"**************************************"<<endl;
return -1;
}//if
string str2=".pdb";
size_t found = pdbInput.find(str2);
if (found==std::string::npos)
{
cout<<endl<<"**************************************"<<endl<<"ERROR: INPUT FILE MUST BE A PDB FILE!"<<endl<<"**************************************"<<endl;
return -1;
}//if not a pdbfile
vector< vector<double> > C = getCoOrds(pdbInput,atype);
vector< vector<double> > Hessian = getHessian(C, cutoff);
int size = Hessian.size();
alglib::real_2d_array Hes;
Hes.setlength(size,size);
for(int i =size-1; i>=0; i--)
{
for(int j =size-1; j>=0; j--)
{
Hes[i][j]= Hessian[i][j];
Hessian[i].erase(Hessian[i].begin()+j);
}
}
Hessian.clear();
cout<<"Starting Decomposition"<<endl;
alglib::real_1d_array w;
alglib::real_2d_array u;
alglib::real_2d_array vt;
alglib::rmatrixsvd(Hes,size,size,2, 2,0,w,u,vt);
ofstream outputFileW;
int r = vt.rows();
int c = vt.cols();
outputFileW.open(eigenvalueMatrixFile.c_str());// this is the eigenvalue matrix.
int count = 0;
for (int i=r-1; i>=0; i--)
{
count++;
double e = w(i);
outputFileW<<count<<" "<<e<<endl;
}
outputFileW.close();
ofstream outputFileVT;
outputFileVT.open(eigenvalueVTFile.c_str());
for (int i=r-1; i>=0; i--)
{
for (int j=0; j<c; j++)
{
double e = vt(i,j); //this is the eigenvector matrix - eigenvectors are rows.
outputFileVT<<e<<" ";
}//for U
outputFileVT<<endl;
}// for vt
outputFileVT.close();
ofstream outputFileU;
outputFileU.open(eigenvalueUFile.c_str());
for (int i=r-1; i>=0; i--)
{
for (int j=0; j<c; j++)
{
double e = u(i,j); //this is the eigenvector matrix - eigenvectors are columns, U and VT are square matrics.
outputFileU<<e<<" ";
}//for U
outputFileU<<endl;
}// for vt
outputFileU.close();
// End cronometer
std::cout << "Completed at: " << currentDateTime() << std::endl;
int time_target=(clock() - tStart)/CLOCKS_PER_SEC;
hour=time_target/ONE_HOUR;
time_target-=hour*ONE_HOUR;
min=time_target/ONE_MINUE;
time_target-=min*ONE_HOUR;
sec=time_target;
if (min<10 && sec<10)
{
printf("- Total time: %d:0%d:0%d\n",hour,min,sec);
}
else if (min<10)
{
printf("- Total time: %d:0%d:%d\n",hour,min,sec);
}
else if (sec<10)
{
printf("- Total time: %d:%d:0%d\n",hour,min,sec);
}
else
{
printf("- Total time: %d:%d:%d\n",hour,min,sec);
}
return 0;
}//main