Merge branch 'master' of git://git.gromacs.org/gromacs
[gromacs/adressmacs.git] / src / tools / hxprops.c
blob8bd04d4062f2b6f81f26dd9777e3aab24f87a383
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * VERSION 3.2.0
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * Green Red Orange Magenta Azure Cyan Skyblue
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include <math.h>
40 #include <string.h>
42 #include "typedefs.h"
43 #include "macros.h"
44 #include "physics.h"
45 #include "vec.h"
46 #include "index.h"
47 #include "hxprops.h"
48 #include "smalloc.h"
49 #include "bondf.h"
51 int nhelix(int nres,t_bb bb[])
53 int i,n;
55 for(i=n=0; (i<nres); i++)
56 if (bb[i].bHelix)
57 n++;
58 return n;
61 real ellipticity(int nres,t_bb bb[])
63 typedef struct {
64 real phi,psi,w;
65 } t_ppwstr;
67 static const t_ppwstr ppw[] = {
68 { -67, -44, 0.31 },
69 { -66, -41, 0.31 },
70 { -59, -44, 0.44 },
71 { -57, -47, 0.56 },
72 { -53, -52, 0.78 },
73 { -48, -57, 1.00 },
74 { -70.5,-35.8,0.15 },
75 { -57, -79, 0.23 },
76 { -38, -78, 1.20 },
77 { -60, -30, 0.24 },
78 { -54, -28, 0.46 },
79 { -44, -33, 0.68 }
81 #define NPPW asize(ppw)
83 int i,j;
84 const real Xi=1.0;
85 real ell,pp2,phi,psi;
87 ell=0;
88 for(i=0; (i<nres); i++) {
89 phi=bb[i].phi;
90 psi=bb[i].psi;
91 for(j=0; (j<NPPW); j++) {
92 pp2=sqr(phi-ppw[j].phi)+sqr(psi-ppw[j].psi);
93 if (pp2 < 64) {
94 bb[i].nhx++;
95 ell+=ppw[j].w;
96 break;
100 return ell;
103 real ahx_len(int gnx,atom_id index[],rvec x[],matrix box)
104 /* Assume we have a list of Calpha atoms only! */
106 rvec dx;
108 rvec_sub(x[index[0]],x[index[gnx-1]],dx);
110 return norm(dx);
113 real radius(FILE *fp,int nca,atom_id ca_index[],rvec x[])
114 /* Assume we have all the backbone */
116 real dl2,dlt;
117 int i,ai;
119 dlt=0;
120 for(i=0; (i<nca); i++) {
121 ai=ca_index[i];
122 dl2=sqr(x[ai][XX])+sqr(x[ai][YY]);
124 if (fp)
125 fprintf(fp," %10g",dl2);
127 dlt+=dl2;
129 if (fp)
130 fprintf(fp,"\n");
132 return sqrt(dlt/nca);
135 static real rot(rvec x1,rvec x2)
137 real phi1,dphi,cp,sp;
138 real xx,yy;
140 phi1=atan2(x1[YY],x1[XX]);
141 cp=cos(phi1);
142 sp=sin(phi1);
143 xx= cp*x2[XX]+sp*x2[YY];
144 yy=-sp*x2[XX]+cp*x2[YY];
146 dphi=RAD2DEG*atan2(yy,xx);
148 return dphi;
151 real twist(FILE *fp,int nca,atom_id caindex[],rvec x[])
153 real pt,dphi;
154 int i,a0,a1;
156 pt=0;
157 a0=caindex[0];
158 for(i=1; (i<nca); i++) {
159 a1=caindex[i];
161 dphi=rot(x[a0],x[a1]);
162 if (dphi < -90)
163 dphi+=360;
164 pt+=dphi;
165 a0=a1;
168 return (pt/(nca-1));
171 real ca_phi(int gnx,atom_id index[],rvec x[],matrix box)
172 /* Assume we have a list of Calpha atoms only! */
174 real phi,phitot;
175 int i,ai,aj,ak,al,t1,t2,t3;
176 rvec r_ij,r_kj,r_kl,m,n;
177 real sign;
179 if (gnx <= 4)
180 return 0;
182 phitot=0;
183 for(i=0; (i<gnx-4); i++) {
184 ai=index[i+0];
185 aj=index[i+1];
186 ak=index[i+2];
187 al=index[i+3];
188 phi=RAD2DEG*
189 dih_angle(x[ai],x[aj],x[ak],x[al],NULL,
190 r_ij,r_kj,r_kl,m,n,
191 &sign,&t1,&t2,&t3);
192 phitot+=phi;
195 return (phitot/(gnx-4.0));
198 real dip(int nbb,atom_id bbind[],rvec x[],t_atom atom[])
200 int i,m,ai;
201 rvec dipje;
202 real q;
204 clear_rvec(dipje);
205 for(i=0; (i<nbb); i++) {
206 ai=bbind[i];
207 q=atom[ai].q;
208 for(m=0; (m<DIM); m++)
209 dipje[m]+=x[ai][m]*q;
211 return norm(dipje);
214 real rise(int gnx,atom_id index[],rvec x[])
215 /* Assume we have a list of Calpha atoms only! */
217 real z,z0,ztot;
218 int i,ai;
220 ai=index[0];
221 z0=x[ai][ZZ];
222 ztot=0;
223 for(i=1; (i<gnx); i++) {
224 ai=index[i];
225 z=x[ai][ZZ];
226 ztot+=(z-z0);
227 z0=z;
230 return (ztot/(gnx-1.0));
233 void av_hblen(FILE *fp3,FILE *fp3a,
234 FILE *fp4,FILE *fp4a,
235 FILE *fp5,FILE *fp5a,
236 real t,int nres,t_bb bb[])
238 int i,n3=0,n4=0,n5=0;
239 real d3=0,d4=0,d5=0;
241 for(i=0; (i<nres-3); i++)
242 if (bb[i].bHelix) {
243 fprintf(fp3a, "%10g",bb[i].d3);
244 n3++;
245 d3+=bb[i].d3;
246 if (i<nres-4) {
247 fprintf(fp4a,"%10g",bb[i].d4);
248 n4++;
249 d4+=bb[i].d4;
251 if (i<nres-5) {
252 fprintf(fp5a,"%10g",bb[i].d5);
253 n5++;
254 d5+=bb[i].d5;
257 fprintf(fp3,"%10g %10g\n",t,d3/n3);
258 fprintf(fp4,"%10g %10g\n",t,d4/n4);
259 fprintf(fp5,"%10g %10g\n",t,d5/n5);
260 fprintf(fp3a,"\n");
261 fprintf(fp4a,"\n");
262 fprintf(fp5a,"\n");
266 void av_phipsi(FILE *fphi,FILE *fpsi,FILE *fphi2,FILE *fpsi2,
267 real t,int nres,t_bb bb[])
269 int i,n=0;
270 real phi=0,psi=0;
272 fprintf(fphi2,"%10g",t);
273 fprintf(fpsi2,"%10g",t);
274 for(i=0; (i<nres); i++)
275 if (bb[i].bHelix) {
276 phi+=bb[i].phi;
277 psi+=bb[i].psi;
278 fprintf(fphi2," %10g",bb[i].phi);
279 fprintf(fpsi2," %10g",bb[i].psi);
280 n++;
282 fprintf(fphi,"%10g %10g\n",t,(phi/n));
283 fprintf(fpsi,"%10g %10g\n",t,(psi/n));
284 fprintf(fphi2,"\n");
285 fprintf(fpsi2,"\n");
288 static void set_ahcity(int nbb,t_bb bb[])
290 real pp2;
291 int n;
293 for(n=0; (n<nbb); n++) {
294 pp2=sqr(bb[n].phi-PHI_AHX)+sqr(bb[n].psi-PSI_AHX);
296 bb[n].bHelix=FALSE;
297 if (pp2 < 2500) {
298 if ((bb[n].d4 < 0.36) || ((n > 0) && bb[n-1].bHelix))
299 bb[n].bHelix=TRUE;
304 t_bb *mkbbind(const char *fn,int *nres,int *nbb,int res0,
305 int *nall,atom_id **index,
306 char ***atomname,t_atom atom[],
307 t_resinfo *resinfo)
309 static const char * bb_nm[] = { "N", "H", "CA", "C", "O" };
310 #define NBB asize(bb_nm)
311 t_bb *bb;
312 char *grpname;
313 int ai,i,i0,i1,j,k,ri,rnr,gnx,r0,r1;
315 fprintf(stderr,"Please select a group containing the entire backbone\n");
316 rd_index(fn,1,&gnx,index,&grpname);
317 *nall=gnx;
318 fprintf(stderr,"Checking group %s\n",grpname);
319 r0=r1=atom[(*index)[0]].resind;
320 for(i=1; (i<gnx); i++) {
321 r0=min(r0,atom[(*index)[i]].resind);
322 r1=max(r1,atom[(*index)[i]].resind);
324 rnr=r1-r0+1;
325 fprintf(stderr,"There are %d residues\n",rnr);
326 snew(bb,rnr);
327 for(i=0; (i<rnr); i++)
328 bb[i].N=bb[i].H=bb[i].CA=bb[i].C=bb[i].O=-1,bb[i].resno=res0+i;
330 for(i=j=0; (i<gnx); i++) {
331 ai=(*index)[i];
332 ri=atom[ai].resind-r0;
333 if (strcmp(*(resinfo[ri].name),"PRO") == 0) {
334 if (strcmp(*(atomname[ai]),"CD") == 0)
335 bb[ri].H=ai;
337 for(k=0; (k<NBB); k++)
338 if (strcmp(bb_nm[k],*(atomname[ai])) == 0) {
339 j++;
340 break;
342 switch (k) {
343 case 0:
344 bb[ri].N=ai;
345 break;
346 case 1:
347 bb[ri].H=ai;
348 break;
349 case 2:
350 bb[ri].CA=ai;
351 break;
352 case 3:
353 bb[ri].C=ai;
354 break;
355 case 4:
356 bb[ri].O=ai;
357 break;
358 default:
359 break;
363 for(i0=0; (i0<rnr); i0++) {
364 if ((bb[i0].N != -1) && (bb[i0].H != -1) &&
365 (bb[i0].CA != -1) &&
366 (bb[i0].C != -1) && (bb[i0].O != -1))
367 break;
369 for(i1=rnr-1; (i1>=0); i1--) {
370 if ((bb[i1].N != -1) && (bb[i1].H != -1) &&
371 (bb[i1].CA != -1) &&
372 (bb[i1].C != -1) && (bb[i1].O != -1))
373 break;
375 if (i0 == 0)
376 i0++;
377 if (i1 == rnr-1)
378 i1--;
380 for(i=i0; (i<i1); i++) {
381 bb[i].Cprev=bb[i-1].C;
382 bb[i].Nnext=bb[i+1].N;
384 rnr=max(0,i1-i0+1);
385 fprintf(stderr,"There are %d complete backbone residues (from %d to %d)\n",
386 rnr,bb[i0].resno,bb[i1].resno);
387 if (rnr==0)
388 gmx_fatal(FARGS,"rnr==0");
389 for(i=0; (i<rnr); i++,i0++)
390 bb[i]=bb[i0];
392 /* Set the labels */
393 for(i=0; (i<rnr); i++) {
394 ri=atom[bb[i].CA].resind;
395 sprintf(bb[i].label,"%s%d",*(resinfo[ri].name),ri+res0);
398 *nres=rnr;
399 *nbb=rnr*asize(bb_nm);
401 return bb;
404 real pprms(FILE *fp,int nbb,t_bb bb[])
406 int i,n;
407 real rms,rmst,rms2;
409 rmst=rms2=0;
410 for(i=n=0; (i<nbb); i++) {
411 if (bb[i].bHelix) {
412 rms=sqrt(bb[i].pprms2);
413 rmst+=rms;
414 rms2+=bb[i].pprms2;
415 fprintf(fp,"%10g ",rms);
416 n++;
419 fprintf(fp,"\n");
420 rms=sqrt(rms2/n-sqr(rmst/n));
422 return rms;
425 void calc_hxprops(int nres,t_bb bb[],rvec x[],matrix box)
427 int i,ao,an,t1,t2,t3;
428 rvec dx,r_ij,r_kj,r_kl,m,n;
429 real sign;
431 for(i=0; (i<nres); i++) {
432 ao=bb[i].O;
433 bb[i].d4=bb[i].d3=bb[i].d5=0;
434 if (i < nres-3) {
435 an=bb[i+3].N;
436 rvec_sub(x[ao],x[an],dx);
437 bb[i].d3=norm(dx);
439 if (i < nres-4) {
440 an=bb[i+4].N;
441 rvec_sub(x[ao],x[an],dx);
442 bb[i].d4=norm(dx);
444 if (i < nres-5) {
445 an=bb[i+5].N;
446 rvec_sub(x[ao],x[an],dx);
447 bb[i].d5=norm(dx);
450 bb[i].phi=RAD2DEG*
451 dih_angle(x[bb[i].Cprev],x[bb[i].N],x[bb[i].CA],x[bb[i].C],NULL,
452 r_ij,r_kj,r_kl,m,n,
453 &sign,&t1,&t2,&t3);
454 bb[i].psi=RAD2DEG*
455 dih_angle(x[bb[i].N],x[bb[i].CA],x[bb[i].C],x[bb[i].Nnext],NULL,
456 r_ij,r_kj,r_kl,m,n,
457 &sign,&t1,&t2,&t3);
458 bb[i].pprms2=sqr(bb[i].phi-PHI_AHX)+sqr(bb[i].psi-PSI_AHX);
460 bb[i].jcaha+=
461 1.4*sin((bb[i].psi+138.0)*DEG2RAD) -
462 4.1*cos(2.0*DEG2RAD*(bb[i].psi+138.0)) +
463 2.0*cos(2.0*DEG2RAD*(bb[i].phi+30.0));
467 static void check_ahx(int nres,t_bb bb[],rvec x[],
468 int *hstart,int *hend)
470 int h0,h1,h0sav,h1sav;
472 set_ahcity(nres,bb);
473 h0=h0sav=h1sav=0;
474 do {
475 for(; (!bb[h0].bHelix) && (h0<nres-4); h0++)
477 for(h1=h0; bb[h1+1].bHelix && (h1<nres-1); h1++)
479 if (h1 > h0) {
480 /*fprintf(stderr,"Helix from %d to %d\n",h0,h1);*/
481 if (h1-h0 > h1sav-h0sav) {
482 h0sav=h0;
483 h1sav=h1;
486 h0=h1+1;
487 } while (h1 < nres-1);
488 *hstart=h0sav;
489 *hend=h1sav;
492 void do_start_end(int nres,t_bb bb[],rvec x[],int *nbb,atom_id bbindex[],
493 int *nca,atom_id caindex[],
494 gmx_bool bRange,int rStart,int rEnd)
496 int i,j,hstart=0,hend=0;
498 if (bRange) {
499 for(i=0; (i<nres); i++) {
500 if ((bb[i].resno >= rStart) && (bb[i].resno <= rEnd))
501 bb[i].bHelix=TRUE;
502 if (bb[i].resno == rStart)
503 hstart=i;
504 if (bb[i].resno == rEnd)
505 hend=i;
508 else {
509 /* Find start and end of longest helix fragment */
510 check_ahx(nres,bb,x,&hstart,&hend);
512 fprintf(stderr,"helix from: %d through %d\n",
513 bb[hstart].resno,bb[hend].resno);
515 for(j=0,i=hstart; (i<=hend); i++) {
516 bbindex[j++]=bb[i].N;
517 bbindex[j++]=bb[i].H;
518 bbindex[j++]=bb[i].CA;
519 bbindex[j++]=bb[i].C;
520 bbindex[j++]=bb[i].O;
521 caindex[i-hstart]=bb[i].CA;
523 *nbb=j;
524 *nca=(hend-hstart+1);
527 void pr_bb(FILE *fp,int nres,t_bb bb[])
529 int i;
531 fprintf(fp,"\n");
532 fprintf(fp,"%3s %3s %3s %3s %3s %7s %7s %7s %7s %7s %3s\n",
533 "AA","N","Ca","C","O","Phi","Psi","D3","D4","D5","Hx?");
534 for(i=0; (i<nres); i++) {
535 fprintf(fp,"%3d %3d %3d %3d %3d %7.2f %7.2f %7.3f %7.3f %7.3f %3s\n",
536 bb[i].resno,bb[i].N,bb[i].CA,bb[i].C,bb[i].O,
537 bb[i].phi,bb[i].psi,bb[i].d3,bb[i].d4,bb[i].d5,
538 bb[i].bHelix ? "Yes" : "No");
540 fprintf(fp,"\n");