processligand.cpp 61.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*

OCLADock, an OpenCL implementation of AutoDock 4.2 running a Lamarckian Genetic Algorithm
Copyright (C) 2017 TU Darmstadt, Embedded Systems and Applications Group, Germany. All rights reserved.

AutoDock is a Trade Mark of the Scripps Research Institute.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/


Leonardo Solis's avatar
Leonardo Solis committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "processligand.h"

int init_liganddata(const char* ligfilename,
		    Liganddata* myligand,
		    Gridinfo*   mygrid)
//The functions first parameter is an empty Liganddata, the second a variable of
//Gridinfo type. The function fills the num_of_atypes and atom_types fields of
//myligand according to the num_of_atypes and grid_types fields of mygrid. In
//this case it is supposed, that the ligand and receptor described by the two
//parameters correspond to each other.
//If the operation was successful, the function returns 0, if not, it returns 1.
{
	FILE* fp;
	int num_of_atypes, i, new_type;
	char atom_types [14][3];
	char tempstr [256];

42
	fp = fopen(ligfilename, "rb"); // fp = fopen(ligfilename, "r");
Leonardo Solis's avatar
Leonardo Solis committed
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
	if (fp == NULL)
	{
		printf("Error: can't open ligand data file %s!\n", ligfilename);
		return 1;
	}

	num_of_atypes = 0;

	//reading the whole ligand pdbqt file
	while (fscanf(fp, "%s", tempstr) != EOF)
	{
		if ((strcmp(tempstr, "HETATM") == 0) || (strcmp(tempstr, "ATOM") == 0))
		{
			new_type = 1;	//supposing this will be a new atom type

			if ((strcmp(tempstr, "HETATM") == 0))	//seeking to the first coordinate value
				fseek(fp, 25, SEEK_CUR);
			else
				fseek(fp, 27, SEEK_CUR);
			fscanf(fp, "%*f");		//skipping fields
			fscanf(fp, "%*f");
			fscanf(fp, "%*f");
			fscanf(fp, "%*s");
			fscanf(fp, "%*s");
			fscanf(fp, "%*f");
			fscanf(fp, "%s", tempstr);	//reading atom type

			tempstr[2] = '\0';	//just to be sure strcpy wont fail even if something is wrong with position

			//checking if this atom has been already found
			for (i=0; i<num_of_atypes; i++)
			{
				if (strcmp(atom_types[i], tempstr) == 0)
					new_type = 0;	//this is not a new type
			}

			if (new_type == 1)	//if new type, copying string...
			{
				if (num_of_atypes >= 14)	//checking if atom type number doesn't exceed 14
				{
					printf("Error: too many types of ligand atoms!\n");
					return 1;
				}

				strcpy(atom_types[num_of_atypes], tempstr);
				num_of_atypes++;
			}
		}
	}

	//copying field to ligand and grid data
	myligand->num_of_atypes = num_of_atypes;
	mygrid->num_of_atypes   = num_of_atypes;

	for (i=0; i<num_of_atypes; i++)
	{
		strcpy(myligand->atom_types[i], atom_types[i]);
		strcpy(mygrid->grid_types[i], atom_types[i]);
	}

	//adding the two other grid types to mygrid
	strcpy(mygrid->grid_types[num_of_atypes],   "e");
	strcpy(mygrid->grid_types[num_of_atypes+1], "d");

	return 0;
}

int set_liganddata_typeid(Liganddata* myligand,
			  int 	      atom_id,
			  const char* typeof_new_atom)
//The function sets the type index of the atom_id-th atom of myligand (in atom_idxyzq field),
//that is, it looks for the row in the atom_types field of myligand which is the same as
//typeof_new_atom, and sets the type index according to the row index.
//If the operation was successful, the function returns 0, if not, it returns 1.
{
	int i;
	int type;

	type = myligand->num_of_atypes;		//setting type to an unvalid index
	for (i=0; i < myligand->num_of_atypes; i++)
	{
		if (strcmp(myligand->atom_types[i], typeof_new_atom) == 0)
			type = i;
	}

	if (type < myligand->num_of_atypes)
	{
		myligand->atom_idxyzq[atom_id][0] = type;
		return 0;
	}
	else		//if typeof_new_atom hasn't been found
	{
		printf("Error: no grid for ligand atom type %s!\n", typeof_new_atom);
		return 1;
	}
}

void get_intraE_contributors(Liganddata* myligand)
//The function fills the intraE_contributors field of the myligand parameter according
//to its bonds and rigid_structures field, which must contain valid data when calling
//this function.
{

	int  atom_id1, atom_id2, atom_id3, rotb_id1, rotb_id2;
	char atom_neighbours [256];
	char atom_neighbours_temp [256];
	int  atom_id_a, atom_id_b, structure_id_A, structure_id_B;
	int  atom_id_a2, atom_id_b2;

	for (atom_id1=0; atom_id1 < myligand->num_of_atoms; atom_id1++)
		for (atom_id2=atom_id1; atom_id2 < myligand->num_of_atoms; atom_id2++)
			//initially, all the values are 1, that is, all the atom pairs
			if (atom_id1 != atom_id2)
			{
				//are contributors
				myligand->intraE_contributors[atom_id1][atom_id2] = 1;
				myligand->intraE_contributors[atom_id2][atom_id1] = 1;
			}
			//except if they are the same
			else
				myligand->intraE_contributors[atom_id1][atom_id2] = 0;

	//There are 4 cases when the atom pair's energy contribution
	//has not to be included in intramolecular energy calculation
	//(that is, when the distance of the atoms are constant during docking)

	//CASE 1
	//if the two atoms are members of the same rigid structure, they aren't contributors
	//printf("\n\n Members of the same rigid structure: \n\n");
	for (atom_id1=0; atom_id1 < myligand->num_of_atoms-1; atom_id1++)
		for (atom_id2=atom_id1+1; atom_id2 < myligand->num_of_atoms; atom_id2++)
			if (myligand->atom_rigid_structures[atom_id1] == myligand->atom_rigid_structures[atom_id2])
			{
				myligand->intraE_contributors[atom_id1][atom_id2] = 0;
				myligand->intraE_contributors[atom_id2][atom_id1] = 0;
				//printf("%d, %d\n", atom_id1+1, atom_id2+1);
			}
		//}

	//CASE2
	//if the atom pair represents a 1-2, 1-3 or 1-4 interaction, they aren't contributors
	//the following algorithm will find the first, second and third neighbours of each atom
	//(so the algorithm is redundant, several atoms will be found more than once)
	for (atom_id1=0; atom_id1 < myligand->num_of_atoms; atom_id1++)
	{
		//if atom_neighbours[i] is one,
		//it will indicate that the atom with id i is the neighbour of the atom with id atom_id1
		for (atom_id2=0; atom_id2 < myligand->num_of_atoms; atom_id2++)
			if (myligand->bonds[atom_id1][atom_id2] == 1)
				atom_neighbours[atom_id2] = 1;		//neighbour
			else
				atom_neighbours[atom_id2] = 0;		//not neighbour

		for (atom_id2=0; atom_id2 < myligand->num_of_atoms; atom_id2++)
			atom_neighbours_temp[atom_id2] = atom_neighbours [atom_id2];	//storing in a temp array as well

		for (atom_id2=0; atom_id2 < myligand->num_of_atoms; atom_id2++)
			if (atom_neighbours[atom_id2] == 1)			//for each neighbour of atom_id1
				for (atom_id3=0; atom_id3 < myligand->num_of_atoms; atom_id3++)
					if (myligand->bonds[atom_id2][atom_id3] == 1)		//if atom_id3 is second neighbour of atom_id1
						atom_neighbours_temp[atom_id3] = 1;			//changing the temporary array

		for (atom_id2=0; atom_id2 < myligand->num_of_atoms; atom_id2++)
				atom_neighbours[atom_id2] = atom_neighbours_temp[atom_id2];

		//now ones of atom_neighbours indicate the first and second neighbours of atom_id1

		//the same code as above
		for (atom_id2=0; atom_id2 < myligand->num_of_atoms; atom_id2++)
			if (atom_neighbours[atom_id2] == 1)			//for each neighbour or second neighbour of atom_id1
				for (atom_id3=0; atom_id3 < myligand->num_of_atoms; atom_id3++)
					if (myligand->bonds[atom_id2][atom_id3] == 1)		//if atom_id3 is second or third neighbour of atom_id1
						atom_neighbours_temp[atom_id3] = 1;

		for (atom_id2=0; atom_id2 < myligand->num_of_atoms; atom_id2++)
			atom_neighbours[atom_id2] = atom_neighbours_temp[atom_id2];

		//now atom_neighbours [i] is one for atom_id1, its first, second and third neighbours, pairs consisting of
		//these atoms aren't contributors
		for (atom_id2=0; atom_id2 < myligand->num_of_atoms; atom_id2++)
			if ((atom_neighbours[atom_id1] == 1) && (atom_neighbours[atom_id2] == 1))
			{
				myligand->intraE_contributors[atom_id1][atom_id2] = 0;
				myligand->intraE_contributors[atom_id2][atom_id1] = 0;
			}
	}

	//CASE3
	//Let atom a and atom b be the endpoints of the same rotatable bond,
	//and A and B the rigid structures connected
	//to the rotatable bond's a and b atoms, respectively.
	//The atom pairs consisting of a and any atom of B aren't contributors.
	//Similarly, atom pairs consisting of b and any atom of A aren't, either.

	for (rotb_id1=0; rotb_id1 < myligand->num_of_rotbonds; rotb_id1++)
	{
		atom_id_a = myligand->rotbonds[rotb_id1][0];
		atom_id_b = myligand->rotbonds[rotb_id1][1];

		structure_id_A = myligand->atom_rigid_structures[atom_id_a];
		structure_id_B = myligand->atom_rigid_structures[atom_id_b];

		for (atom_id1=0; atom_id1 < myligand->num_of_atoms; atom_id1++)
		{
			//if atom_id1 is member of structure A
			if (myligand->atom_rigid_structures[atom_id1] == structure_id_A)
			{
				myligand->intraE_contributors[atom_id1][atom_id_b] = 0;
				myligand->intraE_contributors[atom_id_b][atom_id1] = 0;
			}

			//if atom_id1 is member of structure B
			if (myligand->atom_rigid_structures[atom_id1] == structure_id_B)
			{
				myligand->intraE_contributors[atom_id1][atom_id_a] = 0;
				myligand->intraE_contributors[atom_id_a][atom_id1] = 0;
			}
		}
	}

	//CASE4
	//If one end of two different rotatable bonds are connected to the same rigid structure, the other end, that is,
	//atoms of the bonds aren't contributors.

	for (rotb_id1=0; rotb_id1 < myligand->num_of_rotbonds-1; rotb_id1++)
		for (rotb_id2=rotb_id1+1; rotb_id2 < myligand->num_of_rotbonds; rotb_id2++)
		{
			atom_id_a  = myligand->rotbonds[rotb_id1][0];
			atom_id_b  = myligand->rotbonds[rotb_id1][1];
			atom_id_a2 = myligand->rotbonds[rotb_id2][0];
			atom_id_b2 = myligand->rotbonds[rotb_id2][1];

			if (myligand->atom_rigid_structures[atom_id_a] == myligand->atom_rigid_structures[atom_id_a2])
			{
				myligand->intraE_contributors[atom_id_b][atom_id_b2] = 0;
				myligand->intraE_contributors[atom_id_b2][atom_id_b] = 0;
			}
			if (myligand->atom_rigid_structures[atom_id_a] == myligand->atom_rigid_structures[atom_id_b2])
			{
				myligand->intraE_contributors[atom_id_b][atom_id_a2] = 0;
				myligand->intraE_contributors[atom_id_a2][atom_id_b] = 0;
			}
			if (myligand->atom_rigid_structures[atom_id_b] == myligand->atom_rigid_structures[atom_id_a2])
			{
				myligand->intraE_contributors[atom_id_a][atom_id_b2] = 0;
				myligand->intraE_contributors[atom_id_b2][atom_id_a] = 0;
			}
			if (myligand->atom_rigid_structures[atom_id_b] == myligand->atom_rigid_structures[atom_id_b2])
			{
				myligand->intraE_contributors[atom_id_a][atom_id_a2] = 0;
				myligand->intraE_contributors[atom_id_a2][atom_id_a] = 0;
			}
		}

}

int get_bonds(Liganddata* myligand)
//The function fills the bonds field of myligand based on the distance of the ligand's atoms,
//which can be calculated from the atom_idxyzq field, so this field must contain valid data
//when calling this function.
{
	char atom_names [16][3];

	//atom type indexes which refer to the atom type's bond type (length range)
	char bondtype_id [16] = {0, 0, 3,
				 1, 2, 4,
				 4, 5, 6,
				 4, 4, 4,
				 4, 4, 4,
				 4};

	double mindist[7][7];
	double maxdist[7][7];

	double temp_point1 [3];
	double temp_point2 [3];
	double temp_dist;

	int atom_id1, atom_id2, i, j;
	int atom_typeid1, atom_typeid2;
	int atom_nameid1, atom_nameid2;
	int bondtype_id1, bondtype_id2;

	strcpy(atom_names[0], "C");
	strcpy(atom_names[1], "A");
	strcpy(atom_names[2], "Hx");
	strcpy(atom_names[3], "Nx");
	strcpy(atom_names[4], "Ox");
	strcpy(atom_names[5], "F");
	strcpy(atom_names[6], "MG");
	strcpy(atom_names[7], "P");
	strcpy(atom_names[8], "Sx");
	strcpy(atom_names[9], "CL");
	strcpy(atom_names[10], "CA");
	strcpy(atom_names[11], "MN");
	strcpy(atom_names[12], "FE");
	strcpy(atom_names[13], "ZN");
	strcpy(atom_names[14], "BR");
	strcpy(atom_names[15], "I");

	//Filling the mindist and maxdist tables (as in Autodock, see AD4_parameters.dat and mdist.h).
	//It is supposed that the bond length of atoms with bondtype_id1 and bondtype_id2 is
	//between mindist[bondtype_id1][bondtype_id2] and maxdist[bondtype_id1][bondtype_id2]
	for (i=0; i<7; i++)
	{
		for (j=0; j<7; j++)
		{
			mindist[i][j] = 0.9;
			maxdist[i][j] = 2.1;
		}
	}

	//0=C, 3=H
    	mindist[0][3] = 1.07; mindist[3][0] = mindist[0][3];
    	maxdist[0][3] = 1.15; maxdist[3][0] = maxdist[0][3];

    	//1=N
    	mindist[1][3] = 0.99; mindist[3][1] = mindist[1][3];
    	maxdist[1][3] = 1.10; maxdist[3][1] = maxdist[1][3];

    	//2=O
    	mindist[2][3] = 0.94; mindist[3][2] = mindist[2][3];
   	maxdist[2][3] = 1.10; maxdist[3][2] = maxdist[2][3];

    	//6=S
    	mindist[6][3] = 1.316; mindist[3][6] = mindist[6][3];
    	maxdist[6][3] = 1.356; maxdist[3][6] = maxdist[6][3];

    	//5=P
    	mindist[5][3] = 1.35; mindist[3][5] = mindist[5][3];
	maxdist[5][3] = 1.40; maxdist[3][5] = maxdist[5][3];

    	mindist[1][2] = 1.11;  // N=O is ~ 1.21 A, minus 0.1A error
    	maxdist[1][2] = 1.50;  // N-O is ~ 1.40 A, plus 0.1 A error
    	mindist[2][1] = mindist[1][2];  // N=O is ~ 1.21 A, minus 0.1A error
    	maxdist[2][1] = maxdist[1][2];  // N-O is ~ 1.40 A, plus 0.1 A error

    	//There is no bond between two hydrogenes (does not derive from Autodock)
    	mindist[3][3] = 2;
    	maxdist[3][3] = 1;

    for (atom_id1=0; atom_id1 < myligand->num_of_atoms-1; atom_id1++)
    	for (atom_id2=atom_id1; atom_id2 < myligand->num_of_atoms; atom_id2++)
    	{
		temp_point1[0] = myligand->atom_idxyzq[atom_id1][1];
		temp_point1[1] = myligand->atom_idxyzq[atom_id1][2];
		temp_point1[2] = myligand->atom_idxyzq[atom_id1][3];
		temp_point2[0] = myligand->atom_idxyzq[atom_id2][1];
    		temp_point2[1] = myligand->atom_idxyzq[atom_id2][2];
    		temp_point2[2] = myligand->atom_idxyzq[atom_id2][3];
    		temp_dist = distance(temp_point1, temp_point2);

    		atom_nameid1 = 16;
    		atom_nameid2 = 16;

    		//identifying atom types
    		for (i=0; i<16; i++)
    		{
    			atom_typeid1 = myligand->atom_idxyzq[atom_id1][0];
    			if (atom_names[i][1] == 'x')
    			{
    				if (atom_names[i][0] == toupper(myligand->atom_types[atom_typeid1][0]))
    					atom_nameid1 = i;
    			}
    			else
    			{
Leonardo Solis's avatar
Leonardo Solis committed
409
					// OCLADock
Leonardo Solis's avatar
Leonardo Solis committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
    					if (stricmp(atom_names[i], myligand->atom_types[atom_typeid1]) == 0)
					//if (_stricmp(atom_names[i], myligand->atom_types[atom_typeid1]) == 0)
    					atom_nameid1 = i;
    			}
    		}
  			for (i=0; i<16; i++)
  			{
  				atom_typeid2 = myligand->atom_idxyzq[atom_id2][0];
 				if (atom_names[i][1] == 'x')
 				{
    				if (atom_names[i][0] == toupper(myligand->atom_types[atom_typeid2][0]))
    					atom_nameid2 = i;
    			}
    			else
    			{
Leonardo Solis's avatar
Leonardo Solis committed
425
					// OCLADock
Leonardo Solis's avatar
Leonardo Solis committed
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
    					if (stricmp(atom_names[i], myligand->atom_types[atom_typeid2]) == 0)
					//if (_stricmp(atom_names[i], myligand->atom_types[atom_typeid2]) == 0)
    					atom_nameid2 = i;
    			}
  			}

    		if ((atom_nameid1 == 16) || (atom_nameid2 == 16))
    		{
    			printf("Error: Ligand includes atom with unknown type: %s!\n", myligand->atom_types[atom_typeid1]);
    			return 1;
    		}

    		bondtype_id1 = bondtype_id[atom_nameid1];
    		bondtype_id2 = bondtype_id[atom_nameid2];

    		if (((temp_dist >= mindist [bondtype_id1][bondtype_id2]) && (temp_dist <= maxdist [bondtype_id1][bondtype_id2])) || (atom_id1 == atom_id2))
    		{
    			myligand->bonds [atom_id1][atom_id2] = 1;
    			myligand->bonds [atom_id2][atom_id1] = 1;
    		}
    		else
    		{
       			myligand->bonds [atom_id1][atom_id2] = 0;
        		myligand->bonds [atom_id2][atom_id1] = 0;
    		}

    	}
    return 0;
}

int get_VWpars(Liganddata* myligand, const double AD4_coeff_vdW, const double AD4_coeff_hb)
//The function calculates the Van der Waals parameters for each pair of atom
//types of the ligand given by the first parameter, and fills the VWpars_A, _B,
//_C and _D fields according to the result as well as the solvation parameters
//and atomic volumes (desolv and volume fields) for each atom type.
{
	char atom_names [ATYPE_NUM][3];

	//Sum of vdW radii of two like atoms (A)
	double reqm [ATYPE_NUM] = {2.00, 2.00, 2.00, 4.00, 4.00,
							   3.50, 3.50, 3.50, 3.20, 3.20,
							   3.09, 1.30, 4.20, 4.00, 4.00,
							   4.09, 1.98, 1.30, 1.30, 1.48,
							   4.33, 4.72};

	//cdW well depth (kcal/mol)
	double eps [ATYPE_NUM] = {0.020, 0.020, 0.020, 0.150, 0.150,
						      0.160, 0.160, 0.160, 0.200, 0.200,
						      0.080, 0.875, 0.200, 0.200, 0.200,
						      0.276, 0.550, 0.875, 0.010, 0.550,
						      0.389, 0.550};

	//Sum of vdW radii of two like atoms (A) in case of hydrogen bond
	double reqm_hbond [ATYPE_NUM] = {0.0, 0.0, 0.0, 0.0, 0.0,
								     0.0, 1.9, 1.9, 1.9, 1.9,
								     0.0, 0.0, 0.0, 2.5, 0.0,
								     0.0, 0.0, 0.0, 0.0, 0.0,
								     0.0, 0.0};

	//cdW well depth (kcal/mol) in case of hydrogen bond
	double eps_hbond [ATYPE_NUM] = {0.0, 1.0, 1.0, 0.0, 0.0, 	//HD and HS value is 1 so that it is not necessary to decide which atom_typeid
								    0.0, 5.0, 5.0, 5.0, 5.0, 	//corresponds to the hydrogen when reading eps_hbond...
								    0.0, 0.0, 0.0, 1.0, 0.0,
								    0.0, 0.0, 0.0, 0.0, 0.0,
								    0.0, 0.0};

	//volume of atoms
	double volume [ATYPE_NUM] = {0.0000,  0.0000,  0.0000,  33.5103, 33.5103,
								 22.4493, 22.4493, 22.4493, 17.1573, 17.1573,
								 15.4480, 1.5600,  38.7924, 33.5103, 33.5103,
								 35.8235, 2.7700,  2.1400,  1.8400,  1.7000,
								 42.5661, 55.0585};

	//atomic solvation parameters
	double solpar [ATYPE_NUM] = {0.00051,  0.00051,  0.00051,  -0.00143, -0.00052,
								 -0.00162, -0.00162, -0.00162, -0.00251, -0.00251,
								 -0.00110, -0.00110, -0.00110, -0.00214, -0.00214,
								 -0.00110, -0.00110, -0.00110, -0.00110, -0.00110,
								 -0.00110, -0.00110};


	int atom_typeid1, atom_typeid2, VWid_atype1, VWid_atype2, i;
	double eps12, reqm12;




	strcpy(atom_names [0], "H");
	strcpy(atom_names [1], "HD");
	strcpy(atom_names [2], "HS");
	strcpy(atom_names [3], "C");
	strcpy(atom_names [4], "A");
	strcpy(atom_names [5], "N");
	strcpy(atom_names [6], "NA");
	strcpy(atom_names [7], "NS");
	strcpy(atom_names [8], "OA");
	strcpy(atom_names [9], "OS");
	strcpy(atom_names [10], "F");
	strcpy(atom_names [11], "MG");
	strcpy(atom_names [12], "P");
	strcpy(atom_names [13], "SA");
	strcpy(atom_names [14], "S");
	strcpy(atom_names [15], "CL");
	strcpy(atom_names [16], "CA");
	strcpy(atom_names [17], "MN");
	strcpy(atom_names [18], "FE");
	strcpy(atom_names [19], "ZN");
	strcpy(atom_names [20], "BR");
	strcpy(atom_names [21], "I");

	for (atom_typeid1 = 0; atom_typeid1 < myligand->num_of_atypes; atom_typeid1++)
		for (atom_typeid2 = 0; atom_typeid2 < myligand->num_of_atypes; atom_typeid2++)
		{
			VWid_atype1 = ATYPE_NUM;
			VWid_atype2 = ATYPE_NUM;

			//identifying atom types
			for (i=0; i<ATYPE_NUM; i++)
Leonardo Solis's avatar
Leonardo Solis committed
544
				// OCLADock
Leonardo Solis's avatar
Leonardo Solis committed
545
546
547
548
549
				if (stricmp(atom_names [i], myligand->atom_types [atom_typeid1]) == 0)
				//if (_stricmp(atom_names[i], myligand->atom_types[atom_typeid1]) == 0)
					VWid_atype1 = i;

			for (i=0; i<ATYPE_NUM; i++)
Leonardo Solis's avatar
Leonardo Solis committed
550
				// OCLADock
Leonardo Solis's avatar
Leonardo Solis committed
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
				if (stricmp(atom_names[i], myligand->atom_types[atom_typeid2]) == 0)
				//if (_stricmp(atom_names[i], myligand->atom_types[atom_typeid2]) == 0)
					VWid_atype2 = i;

			if (VWid_atype1 == ATYPE_NUM)
			{
				printf("Error: Ligand includes atom with unknown type: %s!\n", myligand->atom_types [atom_typeid1]);
				return 1;
			}

			if  (VWid_atype2 == ATYPE_NUM)
			{
				printf("Error: Ligand includes atom with unknown type: %s!\n", myligand->atom_types [atom_typeid2]);
				return 1;
			}

			//calculating van der Waals parameters
			if (is_H_bond(myligand->atom_types [atom_typeid1], myligand->atom_types [atom_typeid2]) != 0)
			{
				eps12 = AD4_coeff_hb * eps_hbond [VWid_atype1] * eps_hbond [VWid_atype2];	//The hydrogen's eps is 1, doesn't change the value...
				reqm12 = reqm_hbond [VWid_atype1] + reqm_hbond [VWid_atype2];		//The hydrogen's is 0, doesn't change the value...
				myligand->VWpars_C [atom_typeid1][atom_typeid2] = 5*eps12*pow(reqm12, 12);
				myligand->VWpars_D [atom_typeid1][atom_typeid2] = 6*eps12*pow(reqm12, 10);
				myligand->VWpars_A [atom_typeid1][atom_typeid2] = 0;
				myligand->VWpars_B [atom_typeid1][atom_typeid2] = 0;
			}
			else
			{
				eps12 = AD4_coeff_vdW * sqrt(eps [VWid_atype1]*eps [VWid_atype2]);		//weighting with coefficient for van der Waals term
				reqm12 = 0.5*(reqm [VWid_atype1]+reqm [VWid_atype2]);
				myligand->VWpars_A [atom_typeid1][atom_typeid2] = eps12*pow(reqm12, 12);
				myligand->VWpars_B [atom_typeid1][atom_typeid2] = 2*eps12*pow(reqm12, 6);
				myligand->VWpars_C [atom_typeid1][atom_typeid2] = 0;
				myligand->VWpars_D [atom_typeid1][atom_typeid2] = 0;
			}
		}

	for (atom_typeid1 = 0; atom_typeid1 < myligand->num_of_atypes; atom_typeid1++)
	{
		VWid_atype1 = ATYPE_NUM;

		//identifying atom type
		for (i=0; i<ATYPE_NUM; i++)
Leonardo Solis's avatar
Leonardo Solis committed
594
			// OCLADock
Leonardo Solis's avatar
Leonardo Solis committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
			if (stricmp(atom_names [i], myligand->atom_types [atom_typeid1]) == 0)
			//if (_stricmp(atom_names[i], myligand->atom_types[atom_typeid1]) == 0)
				VWid_atype1 = i;

		if (VWid_atype1 == ATYPE_NUM)
		{
			printf("Error: Ligand includes atom with unknown type: %s\n", myligand->atom_types [atom_typeid1]);
			return 1;
		}

		myligand->volume [atom_typeid1] = volume [VWid_atype1];
		myligand->solpar [atom_typeid1] = solpar [VWid_atype1];
	}

	return 0;
}

void get_moving_and_unit_vectors(Liganddata* myligand)
//The function calculates and fills the
//rotbonds_moving_vectors and rotbonds_unit_vectors fields of the myligand parameter.
{
	int rotb_id, i;
	int atom_id_pointA, atom_id_pointB;
	double origo [3];
	double movvec [3];
	double unitvec [3];
	double pointA [3];
	double pointB [3];
	double dist;


	for (rotb_id=0; rotb_id<myligand->num_of_rotbonds; rotb_id++)
	{
		//capturing unitvector's direction
		atom_id_pointA = myligand->rotbonds [rotb_id][0];			//capturing indexes of the two atoms
		atom_id_pointB = myligand->rotbonds [rotb_id][1];
		for (i=0; i<3; i++)												//capturing coordinates of the two atoms
		{
			pointA [i] = myligand->atom_idxyzq [atom_id_pointA][i+1];
			pointB [i] = myligand->atom_idxyzq [atom_id_pointB][i+1];
			unitvec [i] = pointB [i] - pointA [i];
		}

		//normalize unitvector
		dist = distance(pointA, pointB);
		for (i=0; i<3; i++)												//capturing coordinates of the two atoms
		{
			unitvec [i] = unitvec [i]/dist;
			if (unitvec [i] >= 1)		//although it is not too probable...
				unitvec [i] = 0.999999;
		}

		for (i=0; i<3; i++)
			origo [i] = 0;

		//capturing moving vector
		vec_point2line(origo, pointA, pointB, movvec);

		for (i=0; i<3; i++)
		{
			myligand->rotbonds_moving_vectors [rotb_id][i] = movvec [i];
			myligand->rotbonds_unit_vectors [rotb_id][i] = unitvec [i];
		}
	}

}

int get_liganddata(const char* ligfilename, Liganddata* myligand, const double AD4_coeff_vdW, const double AD4_coeff_hb)
//The functions second parameter is a Liganddata variable whose num_of_atypes
//and atom_types fields must contain valid data.
//The function opens the file ligfilename, which is supposed to be an AutoDock4 pdbqt file,
//and fills the other fields of myligand according to the content of the file.
//If the operation was successful, the function returns 0, if not, it returns 1.
{
	FILE* fp;
	char tempstr [128];
	int atom_counter;
	int branch_counter;
	int endbranch_counter;
	int branches [32][3];
	int i,j,k;
	char atom_rotbonds_temp [256][32];
	int current_rigid_struct_id, reserved_highest_rigid_struct_id;

	atom_counter = 0;

681
	fp = fopen(ligfilename, "rb"); // fp = fopen(ligfilename, "r");
Leonardo Solis's avatar
Leonardo Solis committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
	if (fp == NULL)
	{
		printf("Error: can't open ligand data file %s!\n", ligfilename);
		return 1;
	}

	//reading atomic coordinates, charges and atom types, and writing
	//data to myligand->atom_idxyzq
	while (fscanf(fp, "%s", tempstr) != EOF)
	{
		if ((strcmp(tempstr, "HETATM") == 0) || (strcmp(tempstr, "ATOM") == 0))
		{
			if (atom_counter > MAX_NUM_OF_ATOMS-1)
			{
				printf("Error: ligand consists of too many atoms'\n");
				printf("Maximal allowed number of atoms is %d!\n", MAX_NUM_OF_ATOMS);
				return 1;
			}
			if ((strcmp(tempstr, "HETATM") == 0))	//seeking to the first coordinate value
				fseek(fp, 25, SEEK_CUR);
			else
				fseek(fp, 27, SEEK_CUR);
			fscanf(fp, "%lf", &(myligand->atom_idxyzq [atom_counter][1]));
			fscanf(fp, "%lf", &(myligand->atom_idxyzq [atom_counter][2]));
			fscanf(fp, "%lf", &(myligand->atom_idxyzq [atom_counter][3]));
			fscanf(fp, "%s", tempstr);	//skipping the next two fields
			fscanf(fp, "%s", tempstr);
			fscanf(fp, "%lf", &(myligand->atom_idxyzq [atom_counter][4]));	//reading charge
			fscanf(fp, "%s", tempstr);	//reading atom type
			if (set_liganddata_typeid(myligand, atom_counter, tempstr) != 0)	//the function sets the type index
				return 1;
			atom_counter++;
		}
	}

	myligand->num_of_atoms = atom_counter;

	fclose(fp);

	//filling atom_rotbonds_temp with 0s
	for (i=0; i<myligand->num_of_atoms; i++)
	{
		for (j=0; j<16; j++)
			atom_rotbonds_temp [i][j] = 0;
	}

728
	fp = fopen(ligfilename, "rb"); // fp = fopen(ligfilename, "r");	//re-open the file
Leonardo Solis's avatar
Leonardo Solis committed
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
	if (fp == NULL)
	{
		printf("Error: can't open ligand data file %s!\n", ligfilename);
		return 1;
	}

	branch_counter = 0;
	atom_counter = 0;
	endbranch_counter = 0;

	current_rigid_struct_id = 1;
	reserved_highest_rigid_struct_id = 1;

	//reading data for rotbonds and atom_rotbonds fields
	while (fscanf(fp, "%s", tempstr) != EOF)
	{
		if ((strcmp(tempstr, "HETATM") == 0) || (strcmp(tempstr, "ATOM") == 0))		//if new atom, looking for open rotatable bonds
		{
			for (i=0; i<branch_counter; i++)	//for all branches found until now
				if (branches [i][2] == 0)	//in this case the rotatable bond is not open yet (this atom hasn't
					branches [i][2] = 1;	//to be rotated, but the next has to), so let's make it open
				else
					if (branches [i][2] == 1)	//if it is open, the atom has to be rotated
						atom_rotbonds_temp [atom_counter][i] = 1;	//modifying atom_rotbonds_temp
					/*else it is 2, so it is closed, so nothing to be done...*/

			myligand->atom_rigid_structures [atom_counter] = current_rigid_struct_id;	//using the id of the current rigid structure

			atom_counter++;
Leonardo Solis's avatar
Leonardo Solis committed
758
#if 0
lvs's avatar
lvs committed
759
760
761
762
763
764
			// include last atom
			if (atom_counter == myligand->num_of_atoms) {
				for (i=0; i<branch_counter; i++)	//for all branches found until now
					if (branches [i][2] == 1)	//if it is open, the atom has to be rotated
						atom_rotbonds_temp [atom_counter][i] = 1;	//modifying atom_rotbonds_temp
			}
Leonardo Solis's avatar
Leonardo Solis committed
765
#endif
Leonardo Solis's avatar
Leonardo Solis committed
766
		}
lvs's avatar
lvs committed
767

Leonardo Solis's avatar
Leonardo Solis committed
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
		if (strcmp(tempstr, "BRANCH") == 0)	//if new branch, stroing atom indexes into branches [][]
		{
			if (branch_counter >= MAX_NUM_OF_ROTBONDS)
			{
				printf("Error: ligand includes too many rotatable bonds.\n");
				printf("Maximal allowed number is %d.\n", MAX_NUM_OF_ROTBONDS);
				fclose(fp);
				return 1;
			}
			fscanf(fp, "%d", &(branches [branch_counter][0]));
			fscanf(fp, "%d", &(branches [branch_counter][1]));
			(branches [branch_counter][0])--;	//atom IDs start from 0 instead of 1
			(branches [branch_counter][1])--;
			branches [branch_counter][2] = 0;	//0 indicates, that the next atom that will be found in the file
												//hasn't to be rotated around this bond (it is the bond's atom),
												//so the branch is not open yet
			branch_counter++;

			reserved_highest_rigid_struct_id++;		//next ID is reserved
			current_rigid_struct_id = reserved_highest_rigid_struct_id;		//New branch means new rigid structure, and a new id as well
		}
lvs's avatar
lvs committed
789

Leonardo Solis's avatar
Leonardo Solis committed
790
791
792
793
794
795
		if (strcmp(tempstr, "ENDBRANCH") == 0)
		{
			fscanf(fp, "%d", &(myligand->rotbonds [endbranch_counter][0]));	//rotatable bonds have to be stored in the order
			fscanf(fp, "%d", &(myligand->rotbonds [endbranch_counter][1])); //of endbranches
			(myligand->rotbonds [endbranch_counter][0])--;
			(myligand->rotbonds [endbranch_counter][1])--;
Leonardo Solis's avatar
Leonardo Solis committed
796
#if 0
lvs's avatar
lvs committed
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
			// include last atom before ENDBRANCH
			//if (atom_counter == myligand->num_of_atoms) {
				for (i=0; i<branch_counter; i++)	//for all branches found until now
					if (branches [i][2] == 1)	//if it is open, the atom has to be rotated
						atom_rotbonds_temp [atom_counter][i] = 1;	//modifying atom_rotbonds_temp

			// ---------------------------
			/*
			// print torsions
			printf("%s", "\n");
			for (int a=0;a<atom_counter+2;a++) {
				printf("%-3u: ", a);
				for (i=0;i<branch_counter;i++) {
					printf("%u", atom_rotbonds_temp[a][i]);
				}
				printf("%s", "\n");
			}
			*/
			// ---------------------------
Leonardo Solis's avatar
Leonardo Solis committed
816
#endif
Leonardo Solis's avatar
Leonardo Solis committed
817
818
819
820
821
822
823
			for (i=0; i<branch_counter; i++)	//the branch have to be closed
				if ((branches [i][0] == myligand->rotbonds [endbranch_counter][0]) &&
				    (branches [i][1] == myligand->rotbonds [endbranch_counter][1]))
					branches [i][2] = 2;
			endbranch_counter++;

			current_rigid_struct_id--;	//probably unnecessary since there is a new branch after every endbranch...
lvs's avatar
lvs committed
824
825
826
827
828





Leonardo Solis's avatar
Leonardo Solis committed
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
		}
	}

	myligand->num_of_rotbonds = branch_counter;

	//Now the rotbonds field contains the rotatable bonds (that is, the corresponding two atom's indexes) in the proper order
	//(this will be the order of rotations if an atom have to be rotated around more then one rotatable bond.) However, the
	//atom_rotbonds_temp, whose column indexes correspond to rotatable bond indexes, contains data according to the order of
	//branches (that is, according to branches [][] array), instead of endbranches. Columns of atom_rotbonds_temp have to be
	//copied now to myligand->atom_rotbonds, but in the proper order.
	for (i=0; i<branch_counter; i++)
		for (j=0; j<branch_counter; j++)
			if ((myligand->rotbonds [i][0] == branches [j][0]) && (myligand->rotbonds [i][1] == branches [j][1]))
				for (k=0; k<myligand->num_of_atoms; k++)
					myligand->atom_rotbonds [k][i] = atom_rotbonds_temp [k][j];		//rearrange the columns

	if (get_bonds(myligand) == 1)
		return 1;

	get_intraE_contributors(myligand);

	if (get_VWpars(myligand, AD4_coeff_vdW, AD4_coeff_hb) == 1)
		return 1;

	get_moving_and_unit_vectors(myligand);

	return 0;
}

int gen_new_pdbfile(const char* oldpdb, const char* newpdb, const Liganddata* myligand)
//The funciton opens old pdb file, which is supposed to be an AutoDock4 pdbqt file, and
//copies it to newpdb file, but switches the coordinate values to the atomic coordinates
//of myligand, so newpdb file will be identical to oldpdb except the coordinate values.
//Myligand has to be the ligand which was originally read from oldpdb.
//If the operation was successful, the function returns 0, if not, it returns 1.
{
	FILE* fp_old;
	FILE* fp_new;
	char tempstr [256];
	char tempstr_short [32];
	int acnt_oldlig, acnt_newlig;
	int i,j;

	acnt_oldlig = 0;
	acnt_newlig = 0;

875
	fp_old = fopen(oldpdb, "rb"); // fp_old = fopen(oldpdb, "r");
Leonardo Solis's avatar
Leonardo Solis committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
	if (fp_old == NULL)
	{
		printf("Error: can't open old pdb file %s!\n", oldpdb);
		return 1;
	}

	fp_new = fopen(newpdb, "w");
	if (fp_new == NULL)
	{
		printf("Error: can't create new pdb file %s!\n", newpdb);
		fclose(fp_old);
		return 1;
	}

	while (fgets(tempstr, 255, fp_old) != NULL)		//reading a whole row from oldpdb
	{
		sscanf(tempstr, "%s", tempstr_short);
		if ((strcmp(tempstr_short, "HETATM") == 0) || (strcmp(tempstr_short, "ATOM") == 0))	//if the row begins with HETATM/ATOM, coordinates must be switched
		{
			if (acnt_oldlig >= myligand->num_of_atoms)
			{
				printf("Error: ligand in old pdb file includes more atoms than new one.\n");
				fclose(fp_old);
				fclose(fp_new);
				return 1;
			}
			for (i=0; i<3; i++)
			{
				sprintf(tempstr_short, "%7.3lf", myligand->atom_idxyzq [acnt_oldlig][1+i]);
				for (j=0; j<7; j++)
					tempstr [31+8*i+j] = tempstr_short [j];
			}
			acnt_oldlig++;
		}
		fprintf(fp_new, "%s", tempstr);		//writing the row to newpdb
	}

	if (acnt_oldlig != myligand->num_of_atoms)
	{
		printf("%d %d \n", acnt_oldlig, myligand->num_of_atoms);
		printf("Warning: new lingand consists more atoms than old one.\n");
		printf("Not all the atoms have been written to file!\n");
	}

	fclose(fp_old);
	fclose(fp_new);

	return 0;
}

void get_movvec_to_origo(const Liganddata* myligand, double movvec [])
//The function returns the moving vector in the second parameter which moves the ligand
//(that is, its geometrical center point) given by the first parameter to the origo).
{
	double tmp_x, tmp_y, tmp_z;
	int i;

	tmp_x = 0;
	tmp_y = 0;
	tmp_z = 0;

	for (i=0; i < myligand->num_of_atoms; i++)
	{
		tmp_x += myligand->atom_idxyzq [i][1];
		tmp_y += myligand->atom_idxyzq [i][2];
		tmp_z += myligand->atom_idxyzq [i][3];
	}

	movvec [0] = -1*tmp_x/myligand->num_of_atoms;
	movvec [1] = -1*tmp_y/myligand->num_of_atoms;
	movvec [2] = -1*tmp_z/myligand->num_of_atoms;
}

void move_ligand(Liganddata* myligand, const double movvec [])
lvs's avatar
lvs committed
950
//The function moves the ligand given by the first parameter according to
Leonardo Solis's avatar
Leonardo Solis committed
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//the vector given by the second one.
{
	int i;

	for (i=0; i < myligand->num_of_atoms; i++)
	{
		myligand->atom_idxyzq [i][1] += movvec [0];
		myligand->atom_idxyzq [i][2] += movvec [1];
		myligand->atom_idxyzq [i][3] += movvec [2];
	}
}

void scale_ligand(Liganddata* myligand, const double scale_factor)
//The function scales the ligand given by the first parameter according to the factor
//given by the second (that is, all the ligand atom coordinates will be multiplied by
//scale_factor).
{
	int i,j;

	for (i=0; i < myligand->num_of_atoms; i++)
		for (j=1; j<4; j++)
			myligand->atom_idxyzq [i][j] = myligand->atom_idxyzq [i][j]*scale_factor;
}

double calc_rmsd(const Liganddata* myligand_ref, const Liganddata* myligand, const int handle_symmetry)
//The function calculates the RMSD value (root mean square deviation of the
//atomic distances for two conformations of the same ligand) and returns it.
//If the handle_symmetry parameter is 0, symmetry is not handled, and the
//distances are calculated between atoms with the same atom id. If it is not
//0, one atom from myligand will be compared to the closest atom with the same
//type from myligand_ref and this will be accumulated during rmsd calculation
//(which is a silly method but this is applied in AutoDock, too).
//The two positions must be given by the myligand and myligand_ref parameters.
{
	int i,j;
	double sumdist2;
	double mindist2;

	if (myligand_ref->num_of_atoms != myligand->num_of_atoms)
	{
		printf("Warning: RMSD can't be calculated, atom number mismatch!\n");
		return 100000;	//returning unreasonable value
	}

	sumdist2 = 0;

	if (handle_symmetry == 0)
	{
		for (i=0; i<myligand->num_of_atoms; i++)
		{