-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrasp.c
215 lines (193 loc) · 5.51 KB
/
grasp.c
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
/* grasp.c
* search a space of torsion's using the
* current potentials and greedy algorithms
*
* SINCE AMMP ONLY HAS ONE COORDINATE SET
* ONLY THE BEST IS KEPT
*
* uses the routines in tset.c and random.c
* these routines require atom dot notation (x.ca)
* and use the pdb mode
*
*
*/
/*
* copyright 1993,1995,1996 Robert W. Harrison
*
* This notice may not be removed
* This program may be copied for scientific use
* It may not be sold for profit without explicit
* permission of the author(s) who retain any
* commercial rights including the right to modify
* this notice
*/
#define ANSI 1
/* misc includes - ANSI and some are just to be safe */
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#ifdef ANSI
#include <stdlib.h>
#endif
#include "ammp.h"
int grasp( op,echo,vfs,ffs,nfs, nstep,nkeep,nsearch, imin,imax,whom)
FILE *op;
int echo;
int nfs,(*vfs[])(),(*ffs[])();
int nstep,nkeep,nsearch,imin,imax;
char *whom;
{
int natom,i,j,k,l,lmax,a_number();
float x,delta,randf();
int run;
ATOM *(*apa)[],*a_next(),*ap;
char *cp;
float the_best_energy;
float (*the_best_xyz)[];
/* defined in tset.c */
float get_torsion_value();
int tset_bond_build();
int set_torsion();
int grasp_valid_coordinate();
if( imax < imin ) { j = imax; imax = imin;imin = j;}
the_best_energy = 1.e10;
j = imax-imin;
if( j < 4 || a_number() < 4) {
aaerror(" you need at least four atoms for grasp");
return 1==1;
}
apa = malloc( j*sizeof( ATOM * ));
if( apa == NULL){ goto ERROR ; }
the_best_xyz = malloc( 3*a_number()*sizeof( float ) );
if( the_best_xyz == NULL ) {goto ERROR ;}
/* loop through the atoms and find all in the range
* from imin to imax which are of type *whom
*/
for( i=0; i< j; i++)
(*apa)[i] = NULL;
ap = a_next(-1);
natom = 0;
lmax = a_number();
for( i=0; i< j; i++)
{
(*apa)[natom] = NULL;
for( l=0; l< lmax; l++){
k = ap->serial/100;
if( k == i+imin){
cp = &ap->name[0];
while(*cp != '.' && *cp != '\0') cp++;
if(*cp == '.') cp++;
if( strcmp(cp,whom) == 0 )
{
(*apa)[natom] = ap;
natom++;
break;
}
}/* the residue is the right one */
if( ap->next == ap || ap == NULL || ap->next == NULL)
ap = a_next(-1);
else
ap = ap->next;
}/* l */
}/* i */
/* now the array apa has the atom pointers, in order and
* natom is the number of atoms
*/
for( i=0; i< nstep; i++)
{
for( j=0; j< natom-3; )
{
run = 10*randf() + 1;
/*
delta = 2*3.141592653589793*randf() ;
*/
delta = 3.141592653589793*(2*randf()-1.)/10. ;
/*
for( l=0; l< run; l++)
{
*/
if( (*apa)[j] == NULL ) break;
if( (*apa)[j+1] == NULL ) break;
if( (*apa)[j+2] == NULL ) break;
if( (*apa)[j+3] == NULL ) break;
/*
x = get_torsion_value( (*apa)[j],(*apa)[j+1],(*apa)[j+2],(*apa)[j+3]);
tset_bond_build( (*apa)[j],(*apa)[j+1],(*apa)[j+2],(*apa)[j+3]);
set_torsion( (*apa)[j],(*apa)[j+1],(*apa)[j+2],(*apa)[j+3],delta-x);
*/
tset_bond_build( (*apa)[j],(*apa)[j+1],(*apa)[j+2],(*apa)[j+3]);
set_torsion( (*apa)[j],(*apa)[j+1],(*apa)[j+2],(*apa)[j+3],delta-x);
j += run;
/*
j += 1;
if( j > natom-4) break;
}*//*l*/
}/*j*/
if( !grasp_valid_coordinate()) continue ;
cngdel( vfs,ffs, nfs, nsearch,nsearch, 0.,0);
delta = 0.;
for( j=0; j< nfs; j++)
(*vfs[j])( &delta,0.);
if(echo)fprintf(op,"step %d the best energy is %f %f\n",i,
the_best_energy,delta);
if( delta < the_best_energy)
{
if(echo)fprintf(op,"step %d the best energy is %f\n",i,delta);
the_best_energy = delta;
for( l=0; l < lmax; l++)
{
ap = a_next(l);
(*the_best_xyz)[3*l] = ap->x;
(*the_best_xyz)[3*l+1] = ap->y;
(*the_best_xyz)[3*l+2] = ap->z;
}
}
}
for( l=0; l < lmax; l++)
{
ap = a_next(l);
ap->x = (*the_best_xyz)[3*l];
ap->y = (*the_best_xyz)[3*l+1];
ap->z = (*the_best_xyz)[3*l+2];
}
free( the_best_xyz);
free(apa);
return 1==1;
ERROR: ;
aaerror("cannot allocate memory in grasp");
if( the_best_xyz != NULL ) free(the_best_xyz);
if( apa != NULL ) free(apa);
return 1==0;
}/* end of grasp */
int grasp_valid_coordinate()
{
int na,a_number();
ATOM *ap1,*ap2,*a_next();
int i,j;
float x,y,z;
na = a_number();
if( na < 1) return ;
ap1 = a_next(-1);
ap1 = ap1->next;
for( i=1; i< na; i++)
{
for( j=0; j< i; j++)
{
ap2 = a_next(j);
x = fabs(ap1->x -ap2->x);
if( x < 1.e-1)
{
y = fabs(ap1->y -ap2->y);
if( y < 1.e-1)
{
z = fabs(ap1->z -ap2->z);
if( z< 1.e-1)
{
return 1==0;
}}}
}
ap1 = ap1->next;
}
return 1==1;
}