Move remaining C files in utility to C++
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / specbond.cpp
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,2015, 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 "specbond.h"
40
41 #include <ctype.h>
42 #include <string.h>
43
44 #include <cmath>
45
46 #include <algorithm>
47
48 #include "gromacs/fileio/pdbio.h"
49 #include "gromacs/fileio/strdb.h"
50 #include "gromacs/gmxpreprocess/pdb2top.h"
51 #include "gromacs/legacyheaders/macros.h"
52 #include "gromacs/legacyheaders/typedefs.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/smalloc.h"
56
57 gmx_bool yesno(void)
58 {
59     char c;
60
61     do
62     {
63         c = toupper(fgetc(stdin));
64     }
65     while ((c != 'Y') && (c != 'N'));
66
67     return (c == 'Y');
68 }
69
70 t_specbond *get_specbonds(int *nspecbond)
71 {
72     const char  *sbfile = "specbond.dat";
73
74     t_specbond  *sb = NULL;
75     char         r1buf[32], r2buf[32], a1buf[32], a2buf[32], nr1buf[32], nr2buf[32];
76     double       length;
77     int          nb1, nb2;
78     char       **lines;
79     int          nlines, i, n;
80
81     nlines = get_lines(sbfile, &lines);
82     if (nlines > 0)
83     {
84         snew(sb, nlines);
85     }
86
87     n = 0;
88     for (i = 0; (i < nlines); i++)
89     {
90         if (sscanf(lines[i], "%s%s%d%s%s%d%lf%s%s",
91                    r1buf, a1buf, &nb1, r2buf, a2buf, &nb2, &length, nr1buf, nr2buf) != 9)
92         {
93             fprintf(stderr, "Invalid line '%s' in %s\n", lines[i], sbfile);
94         }
95         else
96         {
97             sb[n].res1    = gmx_strdup(r1buf);
98             sb[n].res2    = gmx_strdup(r2buf);
99             sb[n].newres1 = gmx_strdup(nr1buf);
100             sb[n].newres2 = gmx_strdup(nr2buf);
101             sb[n].atom1   = gmx_strdup(a1buf);
102             sb[n].atom2   = gmx_strdup(a2buf);
103             sb[n].nbond1  = nb1;
104             sb[n].nbond2  = nb2;
105             sb[n].length  = length;
106             n++;
107         }
108         sfree(lines[i]);
109     }
110     if (nlines > 0)
111     {
112         sfree(lines);
113     }
114     fprintf(stderr, "%d out of %d lines of %s converted successfully\n",
115             n, nlines, sbfile);
116
117     *nspecbond = n;
118
119     return sb;
120 }
121
122 void done_specbonds(int nsb, t_specbond sb[])
123 {
124     int i;
125
126     for (i = 0; (i < nsb); i++)
127     {
128         sfree(sb[i].res1);
129         sfree(sb[i].res2);
130         sfree(sb[i].atom1);
131         sfree(sb[i].atom2);
132         sfree(sb[i].newres1);
133         sfree(sb[i].newres2);
134     }
135 }
136
137 static gmx_bool is_special(int nsb, t_specbond sb[], char *res, char *atom)
138 {
139     int i;
140
141     for (i = 0; (i < nsb); i++)
142     {
143         if (((strncmp(sb[i].res1, res, 3) == 0) &&
144              (gmx_strcasecmp(sb[i].atom1, atom) == 0)) ||
145             ((strncmp(sb[i].res2, res, 3) == 0) &&
146              (gmx_strcasecmp(sb[i].atom2, atom) == 0)))
147         {
148             return TRUE;
149         }
150     }
151     return FALSE;
152 }
153
154 static gmx_bool is_bond(int nsb, t_specbond sb[], t_atoms *pdba, int a1, int a2,
155                         real d, int *index_sb, gmx_bool *bSwap)
156 {
157     int   i;
158     char *at1, *at2, *res1, *res2;
159
160     at1  = *pdba->atomname[a1];
161     at2  = *pdba->atomname[a2];
162     res1 = *pdba->resinfo[pdba->atom[a1].resind].name;
163     res2 = *pdba->resinfo[pdba->atom[a2].resind].name;
164
165     if (debug)
166     {
167         fprintf(stderr, "Checking %s-%d %s-%d and %s-%d %s-%d: %g ",
168                 res1, pdba->resinfo[pdba->atom[a1].resind].nr, at1, a1+1,
169                 res2, pdba->resinfo[pdba->atom[a2].resind].nr, at2, a2+1, d);
170     }
171
172     for (i = 0; (i < nsb); i++)
173     {
174         *index_sb = i;
175         if (((strncmp(sb[i].res1, res1, 3) == 0)  &&
176              (gmx_strcasecmp(sb[i].atom1, at1) == 0) &&
177              (strncmp(sb[i].res2, res2, 3) == 0)  &&
178              (gmx_strcasecmp(sb[i].atom2, at2) == 0)))
179         {
180             *bSwap = FALSE;
181             if ((0.9*sb[i].length < d) && (1.1*sb[i].length > d))
182             {
183                 if (debug)
184                 {
185                     fprintf(stderr, "%g\n", sb[i].length);
186                 }
187                 return TRUE;
188             }
189         }
190         if (((strncmp(sb[i].res1, res2, 3) == 0)  &&
191              (gmx_strcasecmp(sb[i].atom1, at2) == 0) &&
192              (strncmp(sb[i].res2, res1, 3) == 0)  &&
193              (gmx_strcasecmp(sb[i].atom2, at1) == 0)))
194         {
195             *bSwap = TRUE;
196             if ((0.9*sb[i].length < d) && (1.1*sb[i].length > d))
197             {
198                 if (debug)
199                 {
200                     fprintf(stderr, "%g\n", sb[i].length);
201                 }
202                 return TRUE;
203             }
204         }
205     }
206     if (debug)
207     {
208         fprintf(stderr, "\n");
209     }
210     return FALSE;
211 }
212
213 static void rename_1res(t_atoms *pdba, int resind, char *newres, gmx_bool bVerbose)
214 {
215     if (bVerbose)
216     {
217         printf("Using rtp entry %s for %s %d\n",
218                newres,
219                *pdba->resinfo[resind].name,
220                pdba->resinfo[resind].nr);
221     }
222     /* this used to free *resname, which messes up the symtab! */
223     snew(pdba->resinfo[resind].rtp, 1);
224     *pdba->resinfo[resind].rtp = gmx_strdup(newres);
225 }
226
227 int mk_specbonds(t_atoms *pdba, rvec x[], gmx_bool bInteractive,
228                  t_ssbond **specbonds, gmx_bool bVerbose)
229 {
230     t_specbond *sb    = NULL;
231     t_ssbond   *bonds = NULL;
232     int         nsb;
233     int         nspec, nbonds;
234     int        *specp, *sgp;
235     gmx_bool    bDoit, bSwap;
236     int         i, j, b, e, e2;
237     int         ai, aj, index_sb;
238     real      **d;
239     char        buf[10];
240
241     nbonds = 0;
242     sb     = get_specbonds(&nsb);
243
244     if (nsb > 0)
245     {
246         snew(specp, pdba->nr);
247         snew(sgp, pdba->nr);
248
249         nspec = 0;
250         for (i = 0; (i < pdba->nr); i++)
251         {
252             /* Check if this atom is special and if it is not a double atom
253              * in the input that still needs to be removed.
254              */
255             if (is_special(nsb, sb, *pdba->resinfo[pdba->atom[i].resind].name,
256                            *pdba->atomname[i]) &&
257                 !(nspec > 0 &&
258                   pdba->atom[sgp[nspec-1]].resind == pdba->atom[i].resind &&
259                   gmx_strcasecmp(*pdba->atomname[sgp[nspec-1]],
260                                  *pdba->atomname[i]) == 0))
261             {
262                 specp[nspec] = pdba->atom[i].resind;
263                 sgp[nspec]   = i;
264                 nspec++;
265             }
266         }
267         /* distance matrix d[nspec][nspec] */
268         snew(d, nspec);
269         for (i = 0; (i < nspec); i++)
270         {
271             snew(d[i], nspec);
272         }
273
274         for (i = 0; (i < nspec); i++)
275         {
276             ai = sgp[i];
277             for (j = 0; (j < nspec); j++)
278             {
279                 aj      = sgp[j];
280                 d[i][j] = std::sqrt(distance2(x[ai], x[aj]));
281             }
282         }
283         if (nspec > 1)
284         {
285 #define MAXCOL 7
286             fprintf(stderr, "Special Atom Distance matrix:\n");
287             for (b = 0; (b < nspec); b += MAXCOL)
288             {
289                 /* print resname/number column headings */
290                 fprintf(stderr, "%8s%8s", "", "");
291                 e = std::min(b+MAXCOL, nspec-1);
292                 for (i = b; (i < e); i++)
293                 {
294                     sprintf(buf, "%s%d", *pdba->resinfo[pdba->atom[sgp[i]].resind].name,
295                             pdba->resinfo[specp[i]].nr);
296                     fprintf(stderr, "%8s", buf);
297                 }
298                 fprintf(stderr, "\n");
299                 /* print atomname/number column headings */
300                 fprintf(stderr, "%8s%8s", "", "");
301                 e = std::min(b+MAXCOL, nspec-1);
302                 for (i = b; (i < e); i++)
303                 {
304                     sprintf(buf, "%s%d", *pdba->atomname[sgp[i]], sgp[i]+1);
305                     fprintf(stderr, "%8s", buf);
306                 }
307                 fprintf(stderr, "\n");
308                 /* print matrix */
309                 e = std::min(b+MAXCOL, nspec);
310                 for (i = b+1; (i < nspec); i++)
311                 {
312                     sprintf(buf, "%s%d", *pdba->resinfo[pdba->atom[sgp[i]].resind].name,
313                             pdba->resinfo[specp[i]].nr);
314                     fprintf(stderr, "%8s", buf);
315                     sprintf(buf, "%s%d", *pdba->atomname[sgp[i]], sgp[i]+1);
316                     fprintf(stderr, "%8s", buf);
317                     e2 = std::min(i, e);
318                     for (j = b; (j < e2); j++)
319                     {
320                         fprintf(stderr, " %7.3f", d[i][j]);
321                     }
322                     fprintf(stderr, "\n");
323                 }
324             }
325         }
326
327         snew(bonds, nspec);
328
329         for (i = 0; (i < nspec); i++)
330         {
331             ai = sgp[i];
332             for (j = i+1; (j < nspec); j++)
333             {
334                 aj = sgp[j];
335                 if (is_bond(nsb, sb, pdba, ai, aj, d[i][j], &index_sb, &bSwap))
336                 {
337                     fprintf(stderr, "%s %s-%d %s-%d and %s-%d %s-%d%s",
338                             bInteractive ? "Link" : "Linking",
339                             *pdba->resinfo[pdba->atom[ai].resind].name,
340                             pdba->resinfo[specp[i]].nr,
341                             *pdba->atomname[ai], ai+1,
342                             *pdba->resinfo[pdba->atom[aj].resind].name,
343                             pdba->resinfo[specp[j]].nr,
344                             *pdba->atomname[aj], aj+1,
345                             bInteractive ? " (y/n) ?" : "...\n");
346                     bDoit = bInteractive ? yesno() : TRUE;
347
348                     if (bDoit)
349                     {
350                         /* Store the residue numbers in the bonds array */
351                         bonds[nbonds].res1 = specp[i];
352                         bonds[nbonds].res2 = specp[j];
353                         bonds[nbonds].a1   = gmx_strdup(*pdba->atomname[ai]);
354                         bonds[nbonds].a2   = gmx_strdup(*pdba->atomname[aj]);
355                         /* rename residues */
356                         if (bSwap)
357                         {
358                             rename_1res(pdba, specp[i], sb[index_sb].newres2, bVerbose);
359                             rename_1res(pdba, specp[j], sb[index_sb].newres1, bVerbose);
360                         }
361                         else
362                         {
363                             rename_1res(pdba, specp[i], sb[index_sb].newres1, bVerbose);
364                             rename_1res(pdba, specp[j], sb[index_sb].newres2, bVerbose);
365                         }
366                         nbonds++;
367                     }
368                 }
369             }
370         }
371
372         for (i = 0; (i < nspec); i++)
373         {
374             sfree(d[i]);
375         }
376         sfree(d);
377         sfree(sgp);
378         sfree(specp);
379
380         done_specbonds(nsb, sb);
381         sfree(sb);
382     }
383
384     *specbonds = bonds;
385
386     return nbonds;
387 }