2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
44 #include "gromacs/legacyheaders/typedefs.h"
45 #include "gromacs/fileio/confio.h"
46 #include "gromacs/math/vec.h"
47 #include "gromacs/utility/futil.h"
54 #include "gromacs/legacyheaders/network.h"
56 #include "gromacs/topology/symtab.h"
57 #include "gromacs/utility/cstringutil.h"
58 #include "gromacs/utility/fatalerror.h"
59 #include "gromacs/utility/smalloc.h"
61 static void copy_atom(t_atoms *atoms1, int a1, t_atoms *atoms2, int a2)
63 atoms2->atom[a2] = atoms1->atom[a1];
64 snew(atoms2->atomname[a2], 1);
65 *atoms2->atomname[a2] = gmx_strdup(*atoms1->atomname[a1]);
68 static atom_id pdbasearch_atom(const char *name, int resind, t_atoms *pdba,
69 const char *searchtype, gmx_bool bAllowMissing)
73 for (i = 0; (i < pdba->nr) && (pdba->atom[i].resind != resind); i++)
78 return search_atom(name, i, pdba,
79 searchtype, bAllowMissing);
82 static void hacksearch_atom(int *ii, int *jj, char *name,
83 int nab[], t_hack *ab[],
84 int resind, t_atoms *pdba)
94 for (i = 0; (i < pdba->nr) && (pdba->atom[i].resind != resind); i++)
98 for (; (i < pdba->nr) && (pdba->atom[i].resind == resind) && (*ii < 0); i++)
100 for (j = 0; (j < nab[i]) && (*ii < 0); j++)
102 if (ab[i][j].nname && strcmp(name, ab[i][j].nname) == 0)
113 void dump_ab(FILE *out, int natom, int nab[], t_hack *ab[], gmx_bool bHeader)
117 #define SS(s) (s) ? (s) : "-"
121 fprintf(out, "ADDBLOCK (t_hack) natom=%d\n"
122 "%4s %2s %-4s %-4s %2s %-4s %-4s %-4s %-4s %1s %s\n",
123 natom, "atom", "nr", "old", "new", "tp", "ai", "aj", "ak", "al", "a", "x");
125 for (i = 0; i < natom; i++)
127 for (j = 0; j < nab[i]; j++)
129 fprintf(out, "%4d %2d %-4s %-4s %2d %-4s %-4s %-4s %-4s %s %g %g %g\n",
130 i+1, ab[i][j].nr, SS(ab[i][j].oname), SS(ab[i][j].nname),
132 SS(ab[i][j].AI), SS(ab[i][j].AJ),
133 SS(ab[i][j].AK), SS(ab[i][j].AL),
134 ab[i][j].atom ? "+" : "",
135 ab[i][j].newx[XX], ab[i][j].newx[YY], ab[i][j].newx[ZZ]);
141 static t_hackblock *get_hackblocks(t_atoms *pdba, int nah, t_hackblock ah[],
143 t_hackblock **ntdb, t_hackblock **ctdb,
147 t_hackblock *hb, *ahptr;
150 snew(hb, pdba->nres);
151 /* first the termini */
152 for (i = 0; i < nterpairs; i++)
156 copy_t_hackblock(ntdb[i], &hb[rN[i]]);
160 merge_t_hackblock(ctdb[i], &hb[rC[i]]);
163 /* then the whole hdb */
164 for (rnr = 0; rnr < pdba->nres; rnr++)
166 ahptr = search_h_db(nah, ah, *pdba->resinfo[rnr].rtp);
169 if (hb[rnr].name == NULL)
171 hb[rnr].name = gmx_strdup(ahptr->name);
173 merge_hacks(ahptr, &hb[rnr]);
179 static void expand_hackblocks_one(t_hackblock *hbr, char *atomname,
180 int *nabi, t_hack **abi, gmx_bool bN, gmx_bool bC)
185 /* we'll recursively add atoms to atoms */
186 for (j = 0; j < hbr->nhack; j++)
188 /* first check if we're in the N- or C-terminus, then we should ignore
189 all hacks involving atoms from resp. previous or next residue
190 (i.e. which name begins with '-' (N) or '+' (C) */
192 if (bN) /* N-terminus: ignore '-' */
194 for (k = 0; k < 4 && hbr->hack[j].a[k] && !bIgnore; k++)
196 bIgnore = hbr->hack[j].a[k][0] == '-';
199 if (bC) /* C-terminus: ignore '+' */
201 for (k = 0; k < 4 && hbr->hack[j].a[k] && !bIgnore; k++)
203 bIgnore = hbr->hack[j].a[k][0] == '+';
206 /* must be either hdb entry (tp>0) or add from tdb (oname==NULL)
207 and first control aton (AI) matches this atom or
208 delete/replace from tdb (oname!=NULL) and oname matches this atom */
211 fprintf(debug, " %s", hbr->hack[j].oname ? hbr->hack[j].oname : hbr->hack[j].AI);
215 ( ( ( hbr->hack[j].tp > 0 || hbr->hack[j].oname == NULL ) &&
216 strcmp(atomname, hbr->hack[j].AI) == 0 ) ||
217 ( hbr->hack[j].oname != NULL &&
218 strcmp(atomname, hbr->hack[j].oname) == 0) ) )
220 /* now expand all hacks for this atom */
223 fprintf(debug, " +%dh", hbr->hack[j].nr);
225 srenew(*abi, *nabi + hbr->hack[j].nr);
226 for (k = 0; k < hbr->hack[j].nr; k++)
228 copy_t_hack(&hbr->hack[j], &(*abi)[*nabi + k]);
229 (*abi)[*nabi + k].bXSet = FALSE;
230 /* if we're adding (oname==NULL) and don't have a new name (nname)
231 yet, build it from atomname */
232 if ( (*abi)[*nabi + k].nname == NULL)
234 if ( (*abi)[*nabi + k].oname == NULL)
236 (*abi)[*nabi + k].nname = gmx_strdup(atomname);
237 (*abi)[*nabi + k].nname[0] = 'H';
244 fprintf(debug, "Hack '%s' %d, replacing nname '%s' with '%s' (old name '%s')\n",
246 (*abi)[*nabi + k].nname, hbr->hack[j].nname,
247 (*abi)[*nabi + k].oname ? (*abi)[*nabi + k].oname : "");
249 sfree((*abi)[*nabi + k].nname);
250 (*abi)[*nabi + k].nname = gmx_strdup(hbr->hack[j].nname);
253 if (hbr->hack[j].tp == 10 && k == 2)
255 /* This is a water virtual site, not a hydrogen */
256 /* Ugly hardcoded name hack */
257 (*abi)[*nabi + k].nname[0] = 'M';
259 else if (hbr->hack[j].tp == 11 && k >= 2)
261 /* This is a water lone pair, not a hydrogen */
262 /* Ugly hardcoded name hack */
263 srenew((*abi)[*nabi + k].nname, 4);
264 (*abi)[*nabi + k].nname[0] = 'L';
265 (*abi)[*nabi + k].nname[1] = 'P';
266 (*abi)[*nabi + k].nname[2] = '1' + k - 2;
267 (*abi)[*nabi + k].nname[3] = '\0';
269 else if (hbr->hack[j].nr > 1)
271 /* adding more than one atom, number them */
272 l = strlen((*abi)[*nabi + k].nname);
273 srenew((*abi)[*nabi + k].nname, l+2);
274 (*abi)[*nabi + k].nname[l] = '1' + k;
275 (*abi)[*nabi + k].nname[l+1] = '\0';
278 (*nabi) += hbr->hack[j].nr;
280 /* add hacks to atoms we've just added */
281 if (hbr->hack[j].tp > 0 || hbr->hack[j].oname == NULL)
283 for (k = 0; k < hbr->hack[j].nr; k++)
285 expand_hackblocks_one(hbr, (*abi)[*nabi-hbr->hack[j].nr+k].nname,
293 static void expand_hackblocks(t_atoms *pdba, t_hackblock hb[],
294 int nab[], t_hack *ab[],
295 int nterpairs, int *rN, int *rC)
300 for (i = 0; i < pdba->nr; i++)
303 for (j = 0; j < nterpairs && !bN; j++)
305 bN = pdba->atom[i].resind == rN[j];
308 for (j = 0; j < nterpairs && !bC; j++)
310 bC = pdba->atom[i].resind == rC[j];
313 /* add hacks to this atom */
314 expand_hackblocks_one(&hb[pdba->atom[i].resind], *pdba->atomname[i],
315 &nab[i], &ab[i], bN, bC);
319 fprintf(debug, "\n");
323 static int check_atoms_present(t_atoms *pdba, int nab[], t_hack *ab[])
325 int i, j, k, d, rnr, nadd;
328 for (i = 0; i < pdba->nr; i++)
330 rnr = pdba->atom[i].resind;
331 for (j = 0; j < nab[i]; j++)
333 if (ab[i][j].oname == NULL)
336 if (ab[i][j].nname == NULL)
338 gmx_incons("ab[i][j].nname not allocated");
340 /* check if the atom is already present */
341 k = pdbasearch_atom(ab[i][j].nname, rnr, pdba, "check", TRUE);
344 /* We found the added atom. */
345 ab[i][j].bAlreadyPresent = TRUE;
348 fprintf(debug, "Atom '%s' in residue '%s' %d is already present\n",
350 *pdba->resinfo[rnr].name, pdba->resinfo[rnr].nr);
355 ab[i][j].bAlreadyPresent = FALSE;
356 /* count how many atoms we'll add */
360 else if (ab[i][j].nname == NULL)
371 static void calc_all_pos(t_atoms *pdba, rvec x[], int nab[], t_hack *ab[],
372 gmx_bool bCheckMissing)
374 int i, j, ii, jj, m, ia, d, rnr, l = 0;
376 rvec xa[4]; /* control atoms for calc_h_pos */
377 rvec xh[MAXH]; /* hydrogen positions from calc_h_pos */
382 for (i = 0; i < pdba->nr; i++)
384 rnr = pdba->atom[i].resind;
385 for (j = 0; j < nab[i]; j += ab[i][j].nr)
387 /* check if we're adding: */
388 if (ab[i][j].oname == NULL && ab[i][j].tp > 0)
391 for (m = 0; (m < ab[i][j].nctl && bFoundAll); m++)
393 ia = pdbasearch_atom(ab[i][j].a[m], rnr, pdba,
394 bCheckMissing ? "atom" : "check",
398 /* not found in original atoms, might still be in t_hack (ab) */
399 hacksearch_atom(&ii, &jj, ab[i][j].a[m], nab, ab, rnr, pdba);
402 copy_rvec(ab[ii][jj].newx, xa[m]);
409 gmx_fatal(FARGS, "Atom %s not found in residue %s %d"
411 " while adding hydrogens",
413 *pdba->resinfo[rnr].name,
414 pdba->resinfo[rnr].nr,
415 *pdba->resinfo[rnr].rtp);
421 copy_rvec(x[ia], xa[m]);
426 for (m = 0; (m < MAXH); m++)
428 for (d = 0; d < DIM; d++)
440 calc_h_pos(ab[i][j].tp, xa, xh, &l);
441 for (m = 0; m < ab[i][j].nr; m++)
443 copy_rvec(xh[m], ab[i][j+m].newx);
444 ab[i][j+m].bXSet = TRUE;
452 static void free_ab(int natoms, int *nab, t_hack **ab)
456 for (i = 0; i < natoms; i++)
458 free_t_hack(nab[i], &ab[i]);
464 static int add_h_low(t_atoms **pdbaptr, rvec *xptr[],
465 int nah, t_hackblock ah[],
466 int nterpairs, t_hackblock **ntdb, t_hackblock **ctdb,
467 int *rN, int *rC, gmx_bool bCheckMissing,
468 int **nabptr, t_hack ***abptr,
469 gmx_bool bUpdate_pdba, gmx_bool bKeep_old_pdba)
471 t_atoms *newpdba = NULL, *pdba = NULL;
473 int i, newi, j, d, natoms, nalreadypresent;
480 /* set flags for adding hydrogens (according to hdb) */
486 /* the first time these will be pointers to NULL, but we will
487 return in them the completed arrays, which we will get back
494 fprintf(debug, "pointer to ab found\n");
504 /* WOW, everything was already figured out */
505 bUpdate_pdba = FALSE;
508 fprintf(debug, "pointer to non-null ab found\n");
513 /* We'll have to do all the hard work */
515 /* first get all the hackblocks for each residue: */
516 hb = get_hackblocks(pdba, nah, ah, nterpairs, ntdb, ctdb, rN, rC);
519 dump_hb(debug, pdba->nres, hb);
522 /* expand the hackblocks to atom level */
525 expand_hackblocks(pdba, hb, nab, ab, nterpairs, rN, rC);
526 free_t_hackblock(pdba->nres, &hb);
531 fprintf(debug, "before calc_all_pos\n");
532 dump_ab(debug, natoms, nab, ab, TRUE);
535 /* Now calc the positions */
536 calc_all_pos(pdba, *xptr, nab, ab, bCheckMissing);
540 fprintf(debug, "after calc_all_pos\n");
541 dump_ab(debug, natoms, nab, ab, TRUE);
546 /* we don't have to add atoms that are already present in pdba,
547 so we will remove them from the ab (t_hack) */
548 nadd = check_atoms_present(pdba, nab, ab);
551 fprintf(debug, "removed add hacks that were already in pdba:\n");
552 dump_ab(debug, natoms, nab, ab, TRUE);
553 fprintf(debug, "will be adding %d atoms\n", nadd);
556 /* Copy old atoms, making space for new ones */
558 init_t_atoms(newpdba, natoms+nadd, FALSE);
559 newpdba->nres = pdba->nres;
560 sfree(newpdba->resinfo);
561 newpdba->resinfo = pdba->resinfo;
569 fprintf(debug, "snew xn for %d old + %d new atoms %d total)\n",
570 natoms, nadd, natoms+nadd);
575 /* There is nothing to do: return now */
578 free_ab(natoms, nab, ab);
584 snew(xn, natoms+nadd);
586 for (i = 0; (i < natoms); i++)
588 /* check if this atom wasn't scheduled for deletion */
589 if (nab[i] == 0 || (ab[i][0].nname != NULL) )
591 if (newi >= natoms+nadd)
593 /*gmx_fatal(FARGS,"Not enough space for adding atoms");*/
595 srenew(xn, natoms+nadd);
598 srenew(newpdba->atom, natoms+nadd);
599 srenew(newpdba->atomname, natoms+nadd);
605 fprintf(debug, "(%3d) %3d %4s %4s%3d %3d",
606 i+1, newi+1, *pdba->atomname[i],
607 *pdba->resinfo[pdba->atom[i].resind].name,
608 pdba->resinfo[pdba->atom[i].resind].nr, nab[i]);
612 copy_atom(pdba, i, newpdba, newi);
614 copy_rvec((*xptr)[i], xn[newi]);
615 /* process the hacks for this atom */
617 for (j = 0; j < nab[i]; j++)
619 if (ab[i][j].oname == NULL) /* add */
622 if (newi >= natoms+nadd)
624 /* gmx_fatal(FARGS,"Not enough space for adding atoms");*/
626 srenew(xn, natoms+nadd);
629 srenew(newpdba->atom, natoms+nadd);
630 srenew(newpdba->atomname, natoms+nadd);
636 newpdba->atom[newi].resind = pdba->atom[i].resind;
640 fprintf(debug, " + %d", newi+1);
643 if (ab[i][j].nname != NULL &&
644 (ab[i][j].oname == NULL ||
645 strcmp(ab[i][j].oname, *newpdba->atomname[newi]) == 0))
648 if (ab[i][j].oname == NULL && ab[i][j].bAlreadyPresent)
650 /* This atom is already present, copy it from the input. */
654 copy_atom(pdba, i+nalreadypresent, newpdba, newi);
656 copy_rvec((*xptr)[i+nalreadypresent], xn[newi]);
664 fprintf(debug, "Replacing %d '%s' with (old name '%s') %s\n",
666 (newpdba->atomname[newi] && *newpdba->atomname[newi]) ? *newpdba->atomname[newi] : "",
667 ab[i][j].oname ? ab[i][j].oname : "",
670 snew(newpdba->atomname[newi], 1);
671 *newpdba->atomname[newi] = gmx_strdup(ab[i][j].nname);
672 if (ab[i][j].oname != NULL && ab[i][j].atom) /* replace */
673 { /* newpdba->atom[newi].m = ab[i][j].atom->m; */
674 /* newpdba->atom[newi].q = ab[i][j].atom->q; */
675 /* newpdba->atom[newi].type = ab[i][j].atom->type; */
680 copy_rvec(ab[i][j].newx, xn[newi]);
683 if (bUpdate_pdba && debug)
685 fprintf(debug, " %s %g %g", *newpdba->atomname[newi],
686 newpdba->atom[newi].m, newpdba->atom[newi].q);
691 i += nalreadypresent;
694 fprintf(debug, "\n");
711 free_ab(natoms, nab, ab);
718 for (i = 0; i < natoms; i++)
720 /* Do not free the atomname string itself, it might be in symtab */
721 /* sfree(*(pdba->atomname[i])); */
722 /* sfree(pdba->atomname[i]); */
724 sfree(pdba->atomname);
726 sfree(pdba->pdbinfo);
742 void deprotonate(t_atoms *atoms, rvec *x)
747 for (i = 0; i < atoms->nr; i++)
749 if ( (*atoms->atomname[i])[0] != 'H')
751 atoms->atomname[j] = atoms->atomname[i];
752 atoms->atom[j] = atoms->atom[i];
753 copy_rvec(x[i], x[j]);
760 int add_h(t_atoms **pdbaptr, rvec *xptr[],
761 int nah, t_hackblock ah[],
762 int nterpairs, t_hackblock **ntdb, t_hackblock **ctdb,
763 int *rN, int *rC, gmx_bool bAllowMissing,
764 int **nabptr, t_hack ***abptr,
765 gmx_bool bUpdate_pdba, gmx_bool bKeep_old_pdba)
767 int nold, nnew, niter;
769 /* Here we loop to be able to add atoms to added atoms.
770 * We should not check for missing atoms here.
777 nnew = add_h_low(pdbaptr, xptr, nah, ah, nterpairs, ntdb, ctdb, rN, rC, FALSE,
778 nabptr, abptr, bUpdate_pdba, bKeep_old_pdba);
782 gmx_fatal(FARGS, "More than 100 iterations of add_h. Maybe you are trying to replace an added atom (this is not supported)?");
789 /* Call add_h_low once more, now only for the missing atoms check */
790 add_h_low(pdbaptr, xptr, nah, ah, nterpairs, ntdb, ctdb, rN, rC, TRUE,
791 nabptr, abptr, bUpdate_pdba, bKeep_old_pdba);
797 int protonate(t_atoms **atomsptr, rvec **xptr, t_protonate *protdata)
801 gmx_bool bUpdate_pdba, bKeep_old_pdba;
802 int nntdb, nctdb, nt, ct;
806 if (!protdata->bInit)
810 fprintf(debug, "protonate: Initializing protdata\n");
813 /* set forcefield to use: */
814 strcpy(protdata->FF, "oplsaa.ff");
816 /* get the databases: */
817 protdata->nah = read_h_db(protdata->FF, &protdata->ah);
818 open_symtab(&protdata->tab);
819 protdata->atype = read_atype(protdata->FF, &protdata->tab);
820 nntdb = read_ter_db(protdata->FF, 'n', &protdata->ntdb, protdata->atype);
823 gmx_fatal(FARGS, "no N-terminus database");
825 nctdb = read_ter_db(protdata->FF, 'c', &protdata->ctdb, protdata->atype);
828 gmx_fatal(FARGS, "no C-terminus database");
831 /* set terminus types: -NH3+ (different for Proline) and -COO- */
833 snew(protdata->sel_ntdb, NTERPAIRS);
834 snew(protdata->sel_ctdb, NTERPAIRS);
836 if (nntdb >= 4 && nctdb >= 2)
838 /* Yuk, yuk, hardcoded default termini selections !!! */
839 if (strncmp(*atoms->resinfo[atoms->atom[atoms->nr-1].resind].name, "PRO", 3) == 0)
854 protdata->sel_ntdb[0] = &(protdata->ntdb[nt]);
855 protdata->sel_ctdb[0] = &(protdata->ctdb[ct]);
857 /* set terminal residue numbers: */
858 snew(protdata->rN, NTERPAIRS);
859 snew(protdata->rC, NTERPAIRS);
861 protdata->rC[0] = atoms->atom[atoms->nr-1].resind;
863 /* keep unprotonated topology: */
864 protdata->upatoms = atoms;
865 /* we don't have these yet: */
866 protdata->patoms = NULL;
868 bKeep_old_pdba = TRUE;
870 /* clear hackblocks: */
871 protdata->nab = NULL;
874 /* set flag to show we're initialized: */
875 protdata->bInit = TRUE;
881 fprintf(debug, "protonate: using available protdata\n");
883 /* add_h will need the unprotonated topology again: */
884 atoms = protdata->upatoms;
885 bUpdate_pdba = FALSE;
886 bKeep_old_pdba = FALSE;
890 nadd = add_h(&atoms, xptr, protdata->nah, protdata->ah,
891 NTERPAIRS, protdata->sel_ntdb, protdata->sel_ctdb,
892 protdata->rN, protdata->rC, TRUE,
893 &protdata->nab, &protdata->ab, bUpdate_pdba, bKeep_old_pdba);
894 if (!protdata->patoms)
896 /* store protonated topology */
897 protdata->patoms = atoms;
899 *atomsptr = protdata->patoms;
902 fprintf(debug, "natoms: %d -> %d (nadd=%d)\n",
903 protdata->upatoms->nr, protdata->patoms->nr, nadd);