60baa774055ceace028fbc1a5ccae4b97b696414
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / specbond.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "config.h"
40
41 #include <ctype.h>
42 #include <math.h>
43 #include <string.h>
44
45 #include "gromacs/legacyheaders/typedefs.h"
46 #include "gromacs/fileio/pdbio.h"
47 #include "gromacs/fileio/strdb.h"
48 #include "gromacs/utility/cstringutil.h"
49 #include "gromacs/utility/smalloc.h"
50 #include "specbond.h"
51 #include "pdb2top.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/legacyheaders/macros.h"
54
55 gmx_bool yesno(void)
56 {
57     char c;
58
59     do
60     {
61         c = toupper(fgetc(stdin));
62     }
63     while ((c != 'Y') && (c != 'N'));
64
65     return (c == 'Y');
66 }
67
68 t_specbond *get_specbonds(int *nspecbond)
69 {
70     const char  *sbfile = "specbond.dat";
71
72     t_specbond  *sb = NULL;
73     char         r1buf[32], r2buf[32], a1buf[32], a2buf[32], nr1buf[32], nr2buf[32];
74     double       length;
75     int          nb1, nb2;
76     char       **lines;
77     int          nlines, i, n;
78
79     nlines = get_lines(sbfile, &lines);
80     if (nlines > 0)
81     {
82         snew(sb, nlines);
83     }
84
85     n = 0;
86     for (i = 0; (i < nlines); i++)
87     {
88         if (sscanf(lines[i], "%s%s%d%s%s%d%lf%s%s",
89                    r1buf, a1buf, &nb1, r2buf, a2buf, &nb2, &length, nr1buf, nr2buf) != 9)
90         {
91             fprintf(stderr, "Invalid line '%s' in %s\n", lines[i], sbfile);
92         }
93         else
94         {
95             sb[n].res1    = gmx_strdup(r1buf);
96             sb[n].res2    = gmx_strdup(r2buf);
97             sb[n].newres1 = gmx_strdup(nr1buf);
98             sb[n].newres2 = gmx_strdup(nr2buf);
99             sb[n].atom1   = gmx_strdup(a1buf);
100             sb[n].atom2   = gmx_strdup(a2buf);
101             sb[n].nbond1  = nb1;
102             sb[n].nbond2  = nb2;
103             sb[n].length  = length;
104             n++;
105         }
106         sfree(lines[i]);
107     }
108     if (nlines > 0)
109     {
110         sfree(lines);
111     }
112     fprintf(stderr, "%d out of %d lines of %s converted successfully\n",
113             n, nlines, sbfile);
114
115     *nspecbond = n;
116
117     return sb;
118 }
119
120 void done_specbonds(int nsb, t_specbond sb[])
121 {
122     int i;
123
124     for (i = 0; (i < nsb); i++)
125     {
126         sfree(sb[i].res1);
127         sfree(sb[i].res2);
128         sfree(sb[i].atom1);
129         sfree(sb[i].atom2);
130         sfree(sb[i].newres1);
131         sfree(sb[i].newres2);
132     }
133 }
134
135 static gmx_bool is_special(int nsb, t_specbond sb[], char *res, char *atom)
136 {
137     int i;
138
139     for (i = 0; (i < nsb); i++)
140     {
141         if (((strncmp(sb[i].res1, res, 3) == 0) &&
142              (gmx_strcasecmp(sb[i].atom1, atom) == 0)) ||
143             ((strncmp(sb[i].res2, res, 3) == 0) &&
144              (gmx_strcasecmp(sb[i].atom2, atom) == 0)))
145         {
146             return TRUE;
147         }
148     }
149     return FALSE;
150 }
151
152 static gmx_bool is_bond(int nsb, t_specbond sb[], t_atoms *pdba, int a1, int a2,
153                         real d, int *index_sb, gmx_bool *bSwap)
154 {
155     int   i;
156     char *at1, *at2, *res1, *res2;
157
158     at1  = *pdba->atomname[a1];
159     at2  = *pdba->atomname[a2];
160     res1 = *pdba->resinfo[pdba->atom[a1].resind].name;
161     res2 = *pdba->resinfo[pdba->atom[a2].resind].name;
162
163     if (debug)
164     {
165         fprintf(stderr, "Checking %s-%d %s-%d and %s-%d %s-%d: %g ",
166                 res1, pdba->resinfo[pdba->atom[a1].resind].nr, at1, a1+1,
167                 res2, pdba->resinfo[pdba->atom[a2].resind].nr, at2, a2+1, d);
168     }
169
170     for (i = 0; (i < nsb); i++)
171     {
172         *index_sb = i;
173         if (((strncmp(sb[i].res1, res1, 3) == 0)  &&
174              (gmx_strcasecmp(sb[i].atom1, at1) == 0) &&
175              (strncmp(sb[i].res2, res2, 3) == 0)  &&
176              (gmx_strcasecmp(sb[i].atom2, at2) == 0)))
177         {
178             *bSwap = FALSE;
179             if ((0.9*sb[i].length < d) && (1.1*sb[i].length > d))
180             {
181                 if (debug)
182                 {
183                     fprintf(stderr, "%g\n", sb[i].length);
184                 }
185                 return TRUE;
186             }
187         }
188         if (((strncmp(sb[i].res1, res2, 3) == 0)  &&
189              (gmx_strcasecmp(sb[i].atom1, at2) == 0) &&
190              (strncmp(sb[i].res2, res1, 3) == 0)  &&
191              (gmx_strcasecmp(sb[i].atom2, at1) == 0)))
192         {
193             *bSwap = TRUE;
194             if ((0.9*sb[i].length < d) && (1.1*sb[i].length > d))
195             {
196                 if (debug)
197                 {
198                     fprintf(stderr, "%g\n", sb[i].length);
199                 }
200                 return TRUE;
201             }
202         }
203     }
204     if (debug)
205     {
206         fprintf(stderr, "\n");
207     }
208     return FALSE;
209 }
210
211 static void rename_1res(t_atoms *pdba, int resind, char *newres, gmx_bool bVerbose)
212 {
213     if (bVerbose)
214     {
215         printf("Using rtp entry %s for %s %d\n",
216                newres,
217                *pdba->resinfo[resind].name,
218                pdba->resinfo[resind].nr);
219     }
220     /* this used to free *resname, which messes up the symtab! */
221     snew(pdba->resinfo[resind].rtp, 1);
222     *pdba->resinfo[resind].rtp = gmx_strdup(newres);
223 }
224
225 int mk_specbonds(t_atoms *pdba, rvec x[], gmx_bool bInteractive,
226                  t_ssbond **specbonds, gmx_bool bVerbose)
227 {
228     t_specbond *sb    = NULL;
229     t_ssbond   *bonds = NULL;
230     int         nsb;
231     int         nspec, nbonds;
232     int        *specp, *sgp;
233     gmx_bool    bDoit, bSwap;
234     int         i, j, b, e, e2;
235     int         ai, aj, index_sb;
236     real      **d;
237     char        buf[10];
238
239     nbonds = 0;
240     sb     = get_specbonds(&nsb);
241
242     if (nsb > 0)
243     {
244         snew(specp, pdba->nr);
245         snew(sgp, pdba->nr);
246
247         nspec = 0;
248         for (i = 0; (i < pdba->nr); i++)
249         {
250             /* Check if this atom is special and if it is not a double atom
251              * in the input that still needs to be removed.
252              */
253             if (is_special(nsb, sb, *pdba->resinfo[pdba->atom[i].resind].name,
254                            *pdba->atomname[i]) &&
255                 !(nspec > 0 &&
256                   pdba->atom[sgp[nspec-1]].resind == pdba->atom[i].resind &&
257                   gmx_strcasecmp(*pdba->atomname[sgp[nspec-1]],
258                                  *pdba->atomname[i]) == 0))
259             {
260                 specp[nspec] = pdba->atom[i].resind;
261                 sgp[nspec]   = i;
262                 nspec++;
263             }
264         }
265         /* distance matrix d[nspec][nspec] */
266         snew(d, nspec);
267         for (i = 0; (i < nspec); i++)
268         {
269             snew(d[i], nspec);
270         }
271
272         for (i = 0; (i < nspec); i++)
273         {
274             ai = sgp[i];
275             for (j = 0; (j < nspec); j++)
276             {
277                 aj      = sgp[j];
278                 d[i][j] = sqrt(distance2(x[ai], x[aj]));
279             }
280         }
281         if (nspec > 1)
282         {
283 #define MAXCOL 7
284             fprintf(stderr, "Special Atom Distance matrix:\n");
285             for (b = 0; (b < nspec); b += MAXCOL)
286             {
287                 /* print resname/number column headings */
288                 fprintf(stderr, "%8s%8s", "", "");
289                 e = min(b+MAXCOL, nspec-1);
290                 for (i = b; (i < e); i++)
291                 {
292                     sprintf(buf, "%s%d", *pdba->resinfo[pdba->atom[sgp[i]].resind].name,
293                             pdba->resinfo[specp[i]].nr);
294                     fprintf(stderr, "%8s", buf);
295                 }
296                 fprintf(stderr, "\n");
297                 /* print atomname/number column headings */
298                 fprintf(stderr, "%8s%8s", "", "");
299                 e = min(b+MAXCOL, nspec-1);
300                 for (i = b; (i < e); i++)
301                 {
302                     sprintf(buf, "%s%d", *pdba->atomname[sgp[i]], sgp[i]+1);
303                     fprintf(stderr, "%8s", buf);
304                 }
305                 fprintf(stderr, "\n");
306                 /* print matrix */
307                 e = min(b+MAXCOL, nspec);
308                 for (i = b+1; (i < nspec); i++)
309                 {
310                     sprintf(buf, "%s%d", *pdba->resinfo[pdba->atom[sgp[i]].resind].name,
311                             pdba->resinfo[specp[i]].nr);
312                     fprintf(stderr, "%8s", buf);
313                     sprintf(buf, "%s%d", *pdba->atomname[sgp[i]], sgp[i]+1);
314                     fprintf(stderr, "%8s", buf);
315                     e2 = min(i, e);
316                     for (j = b; (j < e2); j++)
317                     {
318                         fprintf(stderr, " %7.3f", d[i][j]);
319                     }
320                     fprintf(stderr, "\n");
321                 }
322             }
323         }
324
325         snew(bonds, nspec);
326
327         for (i = 0; (i < nspec); i++)
328         {
329             ai = sgp[i];
330             for (j = i+1; (j < nspec); j++)
331             {
332                 aj = sgp[j];
333                 if (is_bond(nsb, sb, pdba, ai, aj, d[i][j], &index_sb, &bSwap))
334                 {
335                     fprintf(stderr, "%s %s-%d %s-%d and %s-%d %s-%d%s",
336                             bInteractive ? "Link" : "Linking",
337                             *pdba->resinfo[pdba->atom[ai].resind].name,
338                             pdba->resinfo[specp[i]].nr,
339                             *pdba->atomname[ai], ai+1,
340                             *pdba->resinfo[pdba->atom[aj].resind].name,
341                             pdba->resinfo[specp[j]].nr,
342                             *pdba->atomname[aj], aj+1,
343                             bInteractive ? " (y/n) ?" : "...\n");
344                     bDoit = bInteractive ? yesno() : TRUE;
345
346                     if (bDoit)
347                     {
348                         /* Store the residue numbers in the bonds array */
349                         bonds[nbonds].res1 = specp[i];
350                         bonds[nbonds].res2 = specp[j];
351                         bonds[nbonds].a1   = gmx_strdup(*pdba->atomname[ai]);
352                         bonds[nbonds].a2   = gmx_strdup(*pdba->atomname[aj]);
353                         /* rename residues */
354                         if (bSwap)
355                         {
356                             rename_1res(pdba, specp[i], sb[index_sb].newres2, bVerbose);
357                             rename_1res(pdba, specp[j], sb[index_sb].newres1, bVerbose);
358                         }
359                         else
360                         {
361                             rename_1res(pdba, specp[i], sb[index_sb].newres1, bVerbose);
362                             rename_1res(pdba, specp[j], sb[index_sb].newres2, bVerbose);
363                         }
364                         nbonds++;
365                     }
366                 }
367             }
368         }
369
370         for (i = 0; (i < nspec); i++)
371         {
372             sfree(d[i]);
373         }
374         sfree(d);
375         sfree(sgp);
376         sfree(specp);
377
378         done_specbonds(nsb, sb);
379         sfree(sb);
380     }
381
382     *specbonds = bonds;
383
384     return nbonds;
385 }