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.
45 #include "gromacs/utility/cstringutil.h"
47 #include "gromacs/utility/smalloc.h"
53 #include "gromacs/utility/futil.h"
59 #include "gromacs/utility/fatalerror.h"
65 typedef struct gmx_conect_t {
68 gmx_conection_t *conect;
71 static const char *pdbtp[epdbNR] = {
72 "ATOM ", "HETATM", "ANISOU", "CRYST1",
73 "COMPND", "MODEL", "ENDMDL", "TER", "HEADER", "TITLE", "REMARK",
78 /* this is not very good,
79 but these are only used in gmx_trjconv and gmx_editconv */
80 static gmx_bool bWideFormat = FALSE;
81 #define REMARK_SIM_BOX "REMARK THIS IS A SIMULATION BOX"
83 void set_pdb_wide_format(gmx_bool bSet)
88 static void xlate_atomname_pdb2gmx(char *name)
93 length = strlen(name);
94 if (length > 3 && isdigit(name[0]))
97 for (i = 1; i < length; i++)
101 name[length-1] = temp;
105 static void xlate_atomname_gmx2pdb(char *name)
110 length = strlen(name);
111 if (length > 3 && isdigit(name[length-1]))
113 temp = name[length-1];
114 for (i = length-1; i > 0; --i)
123 void gmx_write_pdb_box(FILE *out, int ePBC, matrix box)
125 real alpha, beta, gamma;
129 ePBC = guess_ePBC(box);
132 if (ePBC == epbcNONE)
137 if (norm2(box[YY])*norm2(box[ZZ]) != 0)
139 alpha = RAD2DEG*acos(cos_angle_no_table(box[YY], box[ZZ]));
145 if (norm2(box[XX])*norm2(box[ZZ]) != 0)
147 beta = RAD2DEG*acos(cos_angle_no_table(box[XX], box[ZZ]));
153 if (norm2(box[XX])*norm2(box[YY]) != 0)
155 gamma = RAD2DEG*acos(cos_angle_no_table(box[XX], box[YY]));
161 fprintf(out, "REMARK THIS IS A SIMULATION BOX\n");
162 if (ePBC != epbcSCREW)
164 fprintf(out, "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n",
165 10*norm(box[XX]), 10*norm(box[YY]), 10*norm(box[ZZ]),
166 alpha, beta, gamma, "P 1", 1);
170 /* Double the a-vector length and write the correct space group */
171 fprintf(out, "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n",
172 20*norm(box[XX]), 10*norm(box[YY]), 10*norm(box[ZZ]),
173 alpha, beta, gamma, "P 21 1 1", 1);
178 static void read_cryst1(char *line, int *ePBC, matrix box)
181 char sa[12], sb[12], sc[12], sg[SG_SIZE+1], ident;
182 double fa, fb, fc, alpha, beta, gamma, cosa, cosb, cosg, sing;
183 int syma, symb, symc;
186 sscanf(line, "%*s%s%s%s%lf%lf%lf", sa, sb, sc, &alpha, &beta, &gamma);
189 if (strlen(line) >= 55)
191 strncpy(sg, line+55, SG_SIZE);
197 sscanf(sg, "%c %d %d %d", &ident, &syma, &symb, &symc);
198 if (ident == 'P' && syma == 1 && symb <= 1 && symc <= 1)
200 fc = strtod(sc, NULL)*0.1;
201 ePBC_file = (fc > 0 ? epbcXYZ : epbcXY);
203 if (ident == 'P' && syma == 21 && symb == 1 && symc == 1)
205 ePBC_file = epbcSCREW;
215 fa = strtod(sa, NULL)*0.1;
216 fb = strtod(sb, NULL)*0.1;
217 fc = strtod(sc, NULL)*0.1;
218 if (ePBC_file == epbcSCREW)
224 if ((alpha != 90.0) || (beta != 90.0) || (gamma != 90.0))
228 cosa = cos(alpha*DEG2RAD);
236 cosb = cos(beta*DEG2RAD);
244 cosg = cos(gamma*DEG2RAD);
245 sing = sin(gamma*DEG2RAD);
252 box[YY][XX] = fb*cosg;
253 box[YY][YY] = fb*sing;
254 box[ZZ][XX] = fc*cosb;
255 box[ZZ][YY] = fc*(cosa - cosb*cosg)/sing;
256 box[ZZ][ZZ] = sqrt(fc*fc
257 - box[ZZ][XX]*box[ZZ][XX] - box[ZZ][YY]*box[ZZ][YY]);
267 void write_pdbfile_indexed(FILE *out, const char *title,
268 t_atoms *atoms, rvec x[],
269 int ePBC, matrix box, char chainid,
270 int model_nr, atom_id nindex, const atom_id index[],
271 gmx_conect conect, gmx_bool bTerSepChains)
273 gmx_conect_t *gc = (gmx_conect_t *)conect;
274 char resnm[6], nm[6], pdbform[128], pukestring[100];
276 int resind, resnr, type;
277 unsigned char resic, ch;
281 int chainnum, lastchainnum;
282 int lastresind, lastchainresind;
283 gmx_residuetype_t rt;
284 const char *p_restype;
285 const char *p_lastrestype;
287 gmx_residuetype_init(&rt);
289 bromacs(pukestring, 99);
290 fprintf(out, "TITLE %s\n", (title && title[0]) ? title : pukestring);
293 fprintf(out, "REMARK This file does not adhere to the PDB standard\n");
294 fprintf(out, "REMARK As a result of, some programs may not like it\n");
296 if (box && ( norm2(box[XX]) || norm2(box[YY]) || norm2(box[ZZ]) ) )
298 gmx_write_pdb_box(out, ePBC, box);
302 /* Check whether any occupancies are set, in that case leave it as is,
303 * otherwise set them all to one
306 for (ii = 0; (ii < nindex) && bOccup; ii++)
309 bOccup = bOccup && (atoms->pdbinfo[i].occup == 0.0);
317 fprintf(out, "MODEL %8d\n", model_nr > 0 ? model_nr : 1);
319 lastchainresind = -1;
324 for (ii = 0; ii < nindex; ii++)
328 resind = atoms->atom[i].resind;
329 chainnum = atoms->resinfo[resind].chainnum;
330 p_lastrestype = p_restype;
331 gmx_residuetype_get_type(rt, *atoms->resinfo[resind].name, &p_restype);
333 /* Add a TER record if we changed chain, and if either the previous or this chain is protein/DNA/RNA. */
334 if (bTerSepChains && ii > 0 && chainnum != lastchainnum)
336 /* Only add TER if the previous chain contained protein/DNA/RNA. */
337 if (gmx_residuetype_is_protein(rt, p_lastrestype) || gmx_residuetype_is_dna(rt, p_lastrestype) || gmx_residuetype_is_rna(rt, p_lastrestype))
339 fprintf(out, "TER\n");
341 lastchainnum = chainnum;
342 lastchainresind = lastresind;
345 strncpy(resnm, *atoms->resinfo[resind].name, sizeof(resnm)-1);
346 strncpy(nm, *atoms->atomname[i], sizeof(nm)-1);
347 /* rename HG12 to 2HG1, etc. */
348 xlate_atomname_gmx2pdb(nm);
349 resnr = atoms->resinfo[resind].nr;
350 resic = atoms->resinfo[resind].ic;
357 ch = atoms->resinfo[resind].chainid;
366 resnr = resnr % 10000;
370 type = atoms->pdbinfo[i].type;
371 occup = bOccup ? 1.0 : atoms->pdbinfo[i].occup;
372 bfac = atoms->pdbinfo[i].bfac;
383 "%-6s%5u %-4.4s %3.3s %c%4d%c %10.5f%10.5f%10.5f%8.4f%8.4f %2s\n");
387 /* Check whether atomname is an element name */
388 if ((strlen(nm) < 4) && (gmx_strcasecmp(nm, atoms->atom[i].elem) != 0))
390 strcpy(pdbform, get_pdbformat());
394 strcpy(pdbform, get_pdbformat4());
398 if (nlongname < maxwln)
400 fprintf(stderr, "WARNING: Writing out atom name (%s) longer than 4 characters to .pdb file\n", nm);
402 else if (nlongname == maxwln)
404 fprintf(stderr, "WARNING: More than %d long atom names, will not write more warnings\n", maxwln);
409 strcat(pdbform, "%6.2f%6.2f %2s\n");
411 fprintf(out, pdbform, pdbtp[type], (i+1)%100000, nm, resnm, ch, resnr,
412 (resic == '\0') ? ' ' : resic,
413 10*x[i][XX], 10*x[i][YY], 10*x[i][ZZ], occup, bfac, atoms->atom[i].elem);
414 if (atoms->pdbinfo && atoms->pdbinfo[i].bAnisotropic)
416 fprintf(out, "ANISOU%5u %-4.4s%3.3s %c%4d%c %7d%7d%7d%7d%7d%7d\n",
417 (i+1)%100000, nm, resnm, ch, resnr,
418 (resic == '\0') ? ' ' : resic,
419 atoms->pdbinfo[i].uij[0], atoms->pdbinfo[i].uij[1],
420 atoms->pdbinfo[i].uij[2], atoms->pdbinfo[i].uij[3],
421 atoms->pdbinfo[i].uij[4], atoms->pdbinfo[i].uij[5]);
425 fprintf(out, "TER\n");
426 fprintf(out, "ENDMDL\n");
430 /* Write conect records */
431 for (i = 0; (i < gc->nconect); i++)
433 fprintf(out, "CONECT%5d%5d\n", gc->conect[i].ai+1, gc->conect[i].aj+1);
437 gmx_residuetype_destroy(rt);
440 void write_pdbfile(FILE *out, const char *title, t_atoms *atoms, rvec x[],
441 int ePBC, matrix box, char chainid, int model_nr, gmx_conect conect, gmx_bool bTerSepChains)
445 snew(index, atoms->nr);
446 for (i = 0; i < atoms->nr; i++)
450 write_pdbfile_indexed(out, title, atoms, x, ePBC, box, chainid, model_nr,
451 atoms->nr, index, conect, bTerSepChains);
455 int line2type(char *line)
460 for (k = 0; (k < 6); k++)
466 for (k = 0; (k < epdbNR); k++)
468 if (strncmp(type, pdbtp[k], strlen(pdbtp[k])) == 0)
477 static void read_anisou(char line[], int natom, t_atoms *atoms)
481 char anr[12], anm[12];
485 for (k = 0; (k < 5); k++, j++)
491 for (k = 0; (k < 4); k++, j++)
498 /* Strip off spaces */
501 /* Search backwards for number and name only */
502 atomnr = strtol(anr, NULL, 10);
503 for (i = natom-1; (i >= 0); i--)
505 if ((strcmp(anm, *(atoms->atomname[i])) == 0) &&
506 (atomnr == atoms->pdbinfo[i].atomnr))
513 fprintf(stderr, "Skipping ANISOU record (atom %s %d not found)\n",
518 if (sscanf(line+29, "%d%d%d%d%d%d",
519 &atoms->pdbinfo[i].uij[U11], &atoms->pdbinfo[i].uij[U22],
520 &atoms->pdbinfo[i].uij[U33], &atoms->pdbinfo[i].uij[U12],
521 &atoms->pdbinfo[i].uij[U13], &atoms->pdbinfo[i].uij[U23])
524 atoms->pdbinfo[i].bAnisotropic = TRUE;
528 fprintf(stderr, "Invalid ANISOU record for atom %d\n", i);
529 atoms->pdbinfo[i].bAnisotropic = FALSE;
534 void get_pdb_atomnumber(t_atoms *atoms, gmx_atomprop_t aps)
536 int i, atomnumber, len;
538 char anm[6], anm_copy[6], *ptr;
544 gmx_incons("Trying to deduce atomnumbers when no pdb information is present");
546 for (i = 0; (i < atoms->nr); i++)
548 strcpy(anm, atoms->pdbinfo[i].atomnm);
549 strcpy(anm_copy, atoms->pdbinfo[i].atomnm);
552 if ((anm[0] != ' ') && ((len <= 2) || ((len > 2) && !isdigit(anm[2]))))
555 if (gmx_atomprop_query(aps, epropElement, "???", anm_copy, &eval))
557 atomnumber = gmx_nint(eval);
562 if (gmx_atomprop_query(aps, epropElement, "???", anm_copy, &eval))
564 atomnumber = gmx_nint(eval);
568 if (atomnumber == NOTSET)
571 while ((k < strlen(anm)) && (isspace(anm[k]) || isdigit(anm[k])))
575 anm_copy[0] = anm[k];
577 if (gmx_atomprop_query(aps, epropElement, "???", anm_copy, &eval))
579 atomnumber = gmx_nint(eval);
582 atoms->atom[i].atomnumber = atomnumber;
583 ptr = gmx_atomprop_element(aps, atomnumber);
584 strncpy(atoms->atom[i].elem, ptr == NULL ? "" : ptr, 4);
587 fprintf(debug, "Atomnumber for atom '%s' is %d\n", anm, atomnumber);
592 static int read_atom(t_symtab *symtab,
593 char line[], int type, int natom,
594 t_atoms *atoms, rvec x[], int chainnum, gmx_bool bChange)
599 char anr[12], anm[12], anm_copy[12], altloc, resnm[12], rnr[12];
600 char xc[12], yc[12], zc[12], occup[12], bfac[12];
603 int resnr, atomnumber;
605 if (natom >= atoms->nr)
607 gmx_fatal(FARGS, "\nFound more atoms (%d) in pdb file than expected (%d)",
613 for (k = 0; (k < 5); k++, j++)
620 for (k = 0; (k < 4); k++, j++)
625 strcpy(anm_copy, anm);
631 for (k = 0; (k < 4); k++, j++)
641 for (k = 0; (k < 4); k++, j++)
647 resnr = strtol(rnr, NULL, 10);
651 /* X,Y,Z Coordinate */
652 for (k = 0; (k < 8); k++, j++)
657 for (k = 0; (k < 8); k++, j++)
662 for (k = 0; (k < 8); k++, j++)
669 for (k = 0; (k < 6); k++, j++)
676 for (k = 0; (k < 7); k++, j++)
684 atomn = &(atoms->atom[natom]);
686 atoms->resinfo[atoms->atom[natom-1].resind].nr != resnr ||
687 atoms->resinfo[atoms->atom[natom-1].resind].ic != resic ||
688 (strcmp(*atoms->resinfo[atoms->atom[natom-1].resind].name, resnm) != 0))
696 atomn->resind = atoms->atom[natom-1].resind + 1;
698 atoms->nres = atomn->resind + 1;
699 t_atoms_set_resinfo(atoms, natom, symtab, resnm, resnr, resic, chainnum, chainid);
703 atomn->resind = atoms->atom[natom-1].resind;
707 xlate_atomname_pdb2gmx(anm);
709 atoms->atomname[natom] = put_symtab(symtab, anm);
712 atomn->atomnumber = atomnumber;
713 atomn->elem[0] = '\0';
715 x[natom][XX] = strtod(xc, NULL)*0.1;
716 x[natom][YY] = strtod(yc, NULL)*0.1;
717 x[natom][ZZ] = strtod(zc, NULL)*0.1;
720 atoms->pdbinfo[natom].type = type;
721 atoms->pdbinfo[natom].atomnr = strtol(anr, NULL, 10);
722 atoms->pdbinfo[natom].altloc = altloc;
723 strcpy(atoms->pdbinfo[natom].atomnm, anm_copy);
724 atoms->pdbinfo[natom].bfac = strtod(bfac, NULL);
725 atoms->pdbinfo[natom].occup = strtod(occup, NULL);
732 gmx_bool is_hydrogen(const char *nm)
743 else if ((isdigit(buf[0])) && (buf[1] == 'H'))
750 gmx_bool is_dummymass(const char *nm)
757 if ((buf[0] == 'M') && isdigit(buf[strlen(buf)-1]))
765 static void gmx_conect_addline(gmx_conect_t *con, char *line)
768 char format[32], form2[32];
770 sprintf(form2, "%%*s");
771 sprintf(format, "%s%%d", form2);
772 if (sscanf(line, format, &ai) == 1)
776 strcat(form2, "%*s");
777 sprintf(format, "%s%%d", form2);
778 n = sscanf(line, format, &aj);
781 srenew(con->conect, ++con->nconect);
782 con->conect[con->nconect-1].ai = ai-1;
783 con->conect[con->nconect-1].aj = aj-1;
790 void gmx_conect_dump(FILE *fp, gmx_conect conect)
792 gmx_conect_t *gc = (gmx_conect_t *)conect;
795 for (i = 0; (i < gc->nconect); i++)
797 fprintf(fp, "%6s%5d%5d\n", "CONECT",
798 gc->conect[i].ai+1, gc->conect[i].aj+1);
802 gmx_conect gmx_conect_init()
808 return (gmx_conect) gc;
811 void gmx_conect_done(gmx_conect conect)
813 gmx_conect_t *gc = (gmx_conect_t *)conect;
818 gmx_bool gmx_conect_exist(gmx_conect conect, int ai, int aj)
820 gmx_conect_t *gc = (gmx_conect_t *)conect;
826 for (i = 0; (i < gc->nconect); i++)
828 if (((gc->conect[i].ai == ai) &&
829 (gc->conect[i].aj == aj)) ||
830 ((gc->conect[i].aj == ai) &&
831 (gc->conect[i].ai == aj)))
839 void gmx_conect_add(gmx_conect conect, int ai, int aj)
841 gmx_conect_t *gc = (gmx_conect_t *)conect;
847 if (!gmx_conect_exist(conect, ai, aj))
849 srenew(gc->conect, ++gc->nconect);
850 gc->conect[gc->nconect-1].ai = ai;
851 gc->conect[gc->nconect-1].aj = aj;
855 int read_pdbfile(FILE *in, char *title, int *model_nr,
856 t_atoms *atoms, rvec x[], int *ePBC, matrix box, gmx_bool bChange,
859 gmx_conect_t *gc = (gmx_conect_t *)conect;
862 gmx_bool bConnWarn = FALSE;
866 int natom, chainnum, nres_ter_prev = 0;
868 gmx_bool bStop = FALSE;
872 /* Only assume pbc when there is a CRYST1 entry */
880 open_symtab(&symtab);
886 while (!bStop && (fgets2(line, STRLEN, in) != NULL))
888 line_type = line2type(line);
894 natom = read_atom(&symtab, line, line_type, natom, atoms, x, chainnum, bChange);
900 read_anisou(line, natom, atoms);
905 read_cryst1(line, ePBC, box);
910 if (strlen(line) > 6)
913 /* skip HEADER or TITLE and spaces */
922 /* truncate after title */
936 if ((!strstr(line, ": ")) || (strstr(line+6, "MOLECULE:")))
938 if (!(c = strstr(line+6, "MOLECULE:")) )
942 /* skip 'MOLECULE:' and spaces */
951 /* truncate after title */
955 while ( (d[-1] == ';') && d > c)
984 sscanf(line, "%*s%d", model_nr);
994 gmx_conect_addline(gc, line);
998 fprintf(stderr, "WARNING: all CONECT records are ignored\n");
1008 free_symtab(&symtab);
1012 void get_pdb_coordnum(FILE *in, int *natoms)
1017 while (fgets2(line, STRLEN, in))
1019 if (strncmp(line, "ENDMDL", 6) == 0)
1023 if ((strncmp(line, "ATOM ", 6) == 0) || (strncmp(line, "HETATM", 6) == 0))
1030 void read_pdb_conf(const char *infile, char *title,
1031 t_atoms *atoms, rvec x[], int *ePBC, matrix box, gmx_bool bChange,
1036 in = gmx_fio_fopen(infile, "r");
1037 read_pdbfile(in, title, NULL, atoms, x, ePBC, box, bChange, conect);
1041 gmx_conect gmx_conect_generate(t_topology *top)
1046 /* Fill the conect records */
1047 gc = gmx_conect_init();
1049 for (f = 0; (f < F_NRE); f++)
1053 for (i = 0; (i < top->idef.il[f].nr); i += interaction_function[f].nratoms+1)
1055 gmx_conect_add(gc, top->idef.il[f].iatoms[i+1],
1056 top->idef.il[f].iatoms[i+2]);
1063 const char* get_pdbformat()
1065 static const char *pdbformat = "%-6s%5u %-4.4s%3.3s %c%4d%c %8.3f%8.3f%8.3f";
1069 const char* get_pdbformat4()
1071 static const char *pdbformat4 = "%-6s%5u %-4.4s %3.3s %c%4d%c %8.3f%8.3f%8.3f";