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,2015,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
40 #include "gromacs/fileio/pdbio.h"
49 #include "gromacs/fileio/gmxfio.h"
50 #include "gromacs/math/units.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/pbcutil/pbc.h"
53 #include "gromacs/topology/atomprop.h"
54 #include "gromacs/topology/ifunc.h"
55 #include "gromacs/topology/residuetypes.h"
56 #include "gromacs/topology/symtab.h"
57 #include "gromacs/topology/topology.h"
58 #include "gromacs/utility/coolstuff.h"
59 #include "gromacs/utility/cstringutil.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/futil.h"
62 #include "gromacs/utility/smalloc.h"
63 #include "gromacs/utility/snprintf.h"
70 typedef struct gmx_conect_t
74 gmx_conection_t* conect;
77 static const char* pdbtp[epdbNR] = { "ATOM ", "HETATM", "ANISOU", "CRYST1", "COMPND", "MODEL",
78 "ENDMDL", "TER", "HEADER", "TITLE", "REMARK", "CONECT" };
80 #define REMARK_SIM_BOX "REMARK THIS IS A SIMULATION BOX"
82 void gmx_write_pdb_box(FILE* out, PbcType pbcType, const matrix box)
84 real alpha, beta, gamma;
86 if (pbcType == PbcType::Unset)
88 pbcType = guessPbcType(box);
91 if (pbcType == PbcType::No)
96 if (norm2(box[YY]) * norm2(box[ZZ]) != 0)
98 alpha = RAD2DEG * gmx_angle(box[YY], box[ZZ]);
104 if (norm2(box[XX]) * norm2(box[ZZ]) != 0)
106 beta = RAD2DEG * gmx_angle(box[XX], box[ZZ]);
112 if (norm2(box[XX]) * norm2(box[YY]) != 0)
114 gamma = RAD2DEG * gmx_angle(box[XX], box[YY]);
120 fprintf(out, "REMARK THIS IS A SIMULATION BOX\n");
121 if (pbcType != PbcType::Screw)
124 "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n",
136 /* Double the a-vector length and write the correct space group */
138 "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n",
150 static void read_cryst1(char* line, PbcType* pbcType, matrix box)
153 char sa[12], sb[12], sc[12], sg[SG_SIZE + 1], ident;
154 double fa, fb, fc, alpha, beta, gamma, cosa, cosb, cosg, sing;
155 int syma, symb, symc;
158 sscanf(line, "%*s%s%s%s%lf%lf%lf", sa, sb, sc, &alpha, &beta, &gamma);
160 pbcTypeFile = PbcType::Unset;
161 if (strlen(line) >= 55)
163 strncpy(sg, line + 55, SG_SIZE);
169 sscanf(sg, "%c %d %d %d", &ident, &syma, &symb, &symc);
170 if (ident == 'P' && syma == 1 && symb <= 1 && symc <= 1)
172 fc = strtod(sc, nullptr) * 0.1;
173 pbcTypeFile = (fc > 0 ? PbcType::Xyz : PbcType::XY);
175 if (ident == 'P' && syma == 21 && symb == 1 && symc == 1)
177 pbcTypeFile = PbcType::Screw;
182 *pbcType = pbcTypeFile;
187 fa = strtod(sa, nullptr) * 0.1;
188 fb = strtod(sb, nullptr) * 0.1;
189 fc = strtod(sc, nullptr) * 0.1;
190 if (pbcTypeFile == PbcType::Screw)
196 if ((alpha != 90.0) || (beta != 90.0) || (gamma != 90.0))
200 cosa = std::cos(alpha * DEG2RAD);
208 cosb = std::cos(beta * DEG2RAD);
216 cosg = std::cos(gamma * DEG2RAD);
217 sing = std::sin(gamma * DEG2RAD);
224 box[YY][XX] = fb * cosg;
225 box[YY][YY] = fb * sing;
226 box[ZZ][XX] = fc * cosb;
227 box[ZZ][YY] = fc * (cosa - cosb * cosg) / sing;
228 box[ZZ][ZZ] = std::sqrt(fc * fc - box[ZZ][XX] * box[ZZ][XX] - box[ZZ][YY] * box[ZZ][YY]);
238 static int gmx_fprintf_pqr_atomline(FILE* fp,
239 enum PDB_record record,
241 const char* atom_name,
242 const char* res_name,
251 GMX_RELEASE_ASSERT(record == epdbATOM || record == epdbHETATM,
252 "Can only print PQR atom lines as ATOM or HETATM records");
254 /* Check atom name */
255 GMX_RELEASE_ASSERT(atom_name != nullptr, "Need atom information to print pqr");
257 /* Check residue name */
258 GMX_RELEASE_ASSERT(res_name != nullptr, "Need residue information to print pqr");
260 /* Truncate integers so they fit */
261 atom_seq_number = atom_seq_number % 100000;
262 res_seq_number = res_seq_number % 10000;
265 "%-6s%5d %-4.4s%4.4s%c%4d %8.3f %8.3f %8.3f %6.2f %6.2f\n",
281 void write_pdbfile_indexed(FILE* out,
283 const t_atoms* atoms,
294 gmx_conect_t* gc = static_cast<gmx_conect_t*>(conect);
295 enum PDB_record type;
301 fprintf(out, "TITLE %s\n", (title && title[0]) ? title : gmx::bromacs().c_str());
302 if (box && ((norm2(box[XX]) != 0.0F) || (norm2(box[YY]) != 0.0F) || (norm2(box[ZZ]) != 0.0F)))
304 gmx_write_pdb_box(out, pbcType, box);
306 if (atoms->havePdbInfo)
308 /* Check whether any occupancies are set, in that case leave it as is,
309 * otherwise set them all to one
312 for (int ii = 0; (ii < nindex) && bOccup; ii++)
315 bOccup = bOccup && (atoms->pdbinfo[i].occup == 0.0);
323 fprintf(out, "MODEL %8d\n", model_nr > 0 ? model_nr : 1);
326 for (int ii = 0; ii < nindex; ii++)
329 int resind = atoms->atom[i].resind;
330 std::string resnm = *atoms->resinfo[resind].name;
331 std::string nm = *atoms->atomname[i];
333 int resnr = atoms->resinfo[resind].nr;
334 unsigned char resic = atoms->resinfo[resind].ic;
342 ch = atoms->resinfo[resind].chainid;
351 resnr = resnr % 10000;
354 if (atoms->pdbinfo != nullptr)
356 pdbinfo = atoms->pdbinfo[i];
360 gmx_pdbinfo_init_default(&pdbinfo);
362 type = static_cast<enum PDB_record>(pdbinfo.type);
363 altloc = pdbinfo.altloc;
364 if (!isalnum(altloc))
368 occup = bOccup ? 1.0 : pdbinfo.occup;
372 gmx_fprintf_pdb_atomline(out,
386 atoms->atom[i].elem);
388 if (atoms->pdbinfo && atoms->pdbinfo[i].bAnisotropic)
391 "ANISOU%5d %-4.4s%4.4s%c%4d%c %7d%7d%7d%7d%7d%7d\n",
397 (resic == '\0') ? ' ' : resic,
398 atoms->pdbinfo[i].uij[0],
399 atoms->pdbinfo[i].uij[1],
400 atoms->pdbinfo[i].uij[2],
401 atoms->pdbinfo[i].uij[3],
402 atoms->pdbinfo[i].uij[4],
403 atoms->pdbinfo[i].uij[5]);
408 gmx_fprintf_pqr_atomline(out,
423 fprintf(out, "TER\n");
424 fprintf(out, "ENDMDL\n");
428 /* Write conect records */
429 for (int i = 0; (i < gc->nconect); i++)
431 fprintf(out, "CONECT%5d%5d\n", gc->conect[i].ai + 1, gc->conect[i].aj + 1);
436 void write_pdbfile(FILE* out,
438 const t_atoms* atoms,
448 snew(index, atoms->nr);
449 for (i = 0; i < atoms->nr; i++)
453 write_pdbfile_indexed(
454 out, title, atoms, x, pbcType, box, chainid, model_nr, atoms->nr, index, conect, false);
458 static int line2type(const char* line)
463 for (k = 0; (k < 6); k++)
469 for (k = 0; (k < epdbNR); k++)
471 if (std::strncmp(type, pdbtp[k], strlen(pdbtp[k])) == 0)
480 static void read_anisou(char line[], int natom, t_atoms* atoms)
484 char anr[12], anm[12];
488 for (k = 0; (k < 5); k++, j++)
494 for (k = 0; (k < 4); k++, j++)
501 /* Strip off spaces */
504 /* Search backwards for number and name only */
505 atomnr = std::strtol(anr, nullptr, 10);
506 for (i = natom - 1; (i >= 0); i--)
508 if ((std::strcmp(anm, *(atoms->atomname[i])) == 0) && (atomnr == atoms->pdbinfo[i].atomnr))
515 fprintf(stderr, "Skipping ANISOU record (atom %s %d not found)\n", anm, atomnr);
519 if (sscanf(line + 29,
521 &atoms->pdbinfo[i].uij[U11],
522 &atoms->pdbinfo[i].uij[U22],
523 &atoms->pdbinfo[i].uij[U33],
524 &atoms->pdbinfo[i].uij[U12],
525 &atoms->pdbinfo[i].uij[U13],
526 &atoms->pdbinfo[i].uij[U23])
529 atoms->pdbinfo[i].bAnisotropic = TRUE;
533 fprintf(stderr, "Invalid ANISOU record for atom %d\n", i);
534 atoms->pdbinfo[i].bAnisotropic = FALSE;
539 void get_pdb_atomnumber(const t_atoms* atoms, AtomProperties* aps)
541 int i, atomnumber, len;
543 char anm[6], anm_copy[6];
549 gmx_incons("Trying to deduce atomnumbers when no pdb information is present");
551 for (i = 0; (i < atoms->nr); i++)
553 std::strcpy(anm, atoms->pdbinfo[i].atomnm);
554 std::strcpy(anm_copy, atoms->pdbinfo[i].atomnm);
555 bool atomNumberSet = false;
557 if ((anm[0] != ' ') && ((len <= 2) || !std::isdigit(anm[2])))
560 if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
562 atomnumber = gmx::roundToInt(eval);
563 atomNumberSet = true;
568 if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
570 atomnumber = gmx::roundToInt(eval);
571 atomNumberSet = true;
578 while ((k < std::strlen(anm)) && (std::isspace(anm[k]) || std::isdigit(anm[k])))
582 anm_copy[0] = anm[k];
584 if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
586 atomnumber = gmx::roundToInt(eval);
587 atomNumberSet = true;
593 atoms->atom[i].atomnumber = atomnumber;
594 buf = aps->elementFromAtomNumber(atomnumber);
597 fprintf(debug, "Atomnumber for atom '%s' is %d\n", anm, atomnumber);
601 std::strncpy(atoms->atom[i].elem, buf.c_str(), 4);
605 static int read_atom(t_symtab* symtab, const char line[], int type, int natom, t_atoms* atoms, rvec x[], int chainnum)
610 char anr[12], anm[12], anm_copy[12], altloc, resnm[12], rnr[12], elem[3];
611 char xc[12], yc[12], zc[12], occup[12], bfac[12];
614 int resnr, atomnumber;
616 if (natom >= atoms->nr)
618 gmx_fatal(FARGS, "\nFound more atoms (%d) in pdb file than expected (%d)", natom + 1, atoms->nr);
623 for (k = 0; (k < 5); k++, j++)
630 for (k = 0; (k < 4); k++, j++)
635 std::strcpy(anm_copy, anm);
641 for (k = 0; (k < 4); k++, j++)
651 for (k = 0; (k < 4); k++, j++)
657 resnr = std::strtol(rnr, nullptr, 10);
661 /* X,Y,Z Coordinate */
662 for (k = 0; (k < 8); k++, j++)
667 for (k = 0; (k < 8); k++, j++)
672 for (k = 0; (k < 8); k++, j++)
679 for (k = 0; (k < 6); k++, j++)
686 for (k = 0; (k < 7); k++, j++)
696 for (k = 0; (k < 2); k++, j++)
705 atomn = &(atoms->atom[natom]);
706 if ((natom == 0) || atoms->resinfo[atoms->atom[natom - 1].resind].nr != resnr
707 || atoms->resinfo[atoms->atom[natom - 1].resind].ic != resic
708 || (strcmp(*atoms->resinfo[atoms->atom[natom - 1].resind].name, resnm) != 0))
716 atomn->resind = atoms->atom[natom - 1].resind + 1;
718 atoms->nres = atomn->resind + 1;
719 t_atoms_set_resinfo(atoms, natom, symtab, resnm, resnr, resic, chainnum, chainid);
723 atomn->resind = atoms->atom[natom - 1].resind;
725 atoms->atomname[natom] = put_symtab(symtab, anm);
728 atomn->atomnumber = atomnumber;
729 strncpy(atomn->elem, elem, 4);
731 x[natom][XX] = strtod(xc, nullptr) * 0.1;
732 x[natom][YY] = strtod(yc, nullptr) * 0.1;
733 x[natom][ZZ] = strtod(zc, nullptr) * 0.1;
736 atoms->pdbinfo[natom].type = type;
737 atoms->pdbinfo[natom].atomnr = strtol(anr, nullptr, 10);
738 atoms->pdbinfo[natom].altloc = altloc;
739 strcpy(atoms->pdbinfo[natom].atomnm, anm_copy);
740 atoms->pdbinfo[natom].bfac = strtod(bfac, nullptr);
741 atoms->pdbinfo[natom].occup = strtod(occup, nullptr);
748 gmx_bool is_hydrogen(const char* nm)
752 std::strcpy(buf, nm);
755 return ((buf[0] == 'H') || ((std::isdigit(buf[0]) != 0) && (buf[1] == 'H')));
758 gmx_bool is_dummymass(const char* nm)
762 std::strcpy(buf, nm);
765 return (buf[0] == 'M') && (std::isdigit(buf[strlen(buf) - 1]) != 0);
768 static void gmx_conect_addline(gmx_conect_t* con, char* line)
772 std::string form2 = "%*s";
773 std::string format = form2 + "%d";
774 if (sscanf(line, format.c_str(), &ai) == 1)
779 format = form2 + "%d";
780 n = sscanf(line, format.c_str(), &aj);
783 gmx_conect_add(con, ai - 1, aj - 1); /* to prevent duplicated records */
789 void gmx_conect_dump(FILE* fp, gmx_conect conect)
791 gmx_conect_t* gc = static_cast<gmx_conect_t*>(conect);
794 for (i = 0; (i < gc->nconect); i++)
796 fprintf(fp, "%6s%5d%5d\n", "CONECT", gc->conect[i].ai + 1, gc->conect[i].aj + 1);
800 gmx_conect gmx_conect_init()
809 void gmx_conect_done(gmx_conect conect)
811 gmx_conect_t* gc = conect;
816 gmx_bool gmx_conect_exist(gmx_conect conect, int ai, int aj)
818 gmx_conect_t* gc = conect;
824 for (i = 0; (i < gc->nconect); i++)
826 if (((gc->conect[i].ai == ai) && (gc->conect[i].aj == aj))
827 || ((gc->conect[i].aj == ai) && (gc->conect[i].ai == aj)))
835 void gmx_conect_add(gmx_conect conect, int ai, int aj)
837 gmx_conect_t* gc = static_cast<gmx_conect_t*>(conect);
842 if (!gmx_conect_exist(conect, ai, aj))
844 srenew(gc->conect, ++gc->nconect);
845 gc->conect[gc->nconect - 1].ai = ai;
846 gc->conect[gc->nconect - 1].aj = aj;
850 int read_pdbfile(FILE* in,
860 gmx_conect_t* gc = conect;
862 gmx_bool bConnWarn = FALSE;
863 char line[STRLEN + 1];
867 gmx_bool bStop = FALSE;
871 /* Only assume pbc when there is a CRYST1 entry */
872 *pbcType = PbcType::No;
879 atoms->haveMass = FALSE;
880 atoms->haveCharge = FALSE;
881 atoms->haveType = FALSE;
882 atoms->haveBState = FALSE;
883 atoms->havePdbInfo = (atoms->pdbinfo != nullptr);
889 while (!bStop && (fgets2(line, STRLEN, in) != nullptr))
891 line_type = line2type(line);
897 natom = read_atom(symtab, line, line_type, natom, atoms, x, chainnum);
901 if (atoms->havePdbInfo)
903 read_anisou(line, natom, atoms);
907 case epdbCRYST1: read_cryst1(line, pbcType, box); break;
911 if (std::strlen(line) > 6)
914 /* skip HEADER or TITLE and spaces */
923 /* truncate after title */
924 d = std::strstr(c, " ");
929 if (std::strlen(c) > 0)
931 std::strcpy(title, c);
937 if ((!std::strstr(line, ": ")) || (std::strstr(line + 6, "MOLECULE:")))
939 if (!(c = std::strstr(line + 6, "MOLECULE:")))
943 /* skip 'MOLECULE:' and spaces */
952 /* truncate after title */
956 while ((d[-1] == ';') && d > c)
966 std::strcat(title, "; ");
967 std::strcat(title, c);
971 std::strcpy(title, c);
978 case epdbTER: chainnum++; break;
983 sscanf(line, "%*s%d", model_nr);
987 case epdbENDMDL: bStop = TRUE; break;
991 gmx_conect_addline(gc, line);
995 fprintf(stderr, "WARNING: all CONECT records are ignored\n");
1007 void get_pdb_coordnum(FILE* in, int* natoms)
1012 while (fgets2(line, STRLEN, in))
1014 if (std::strncmp(line, "ENDMDL", 6) == 0)
1018 if ((std::strncmp(line, "ATOM ", 6) == 0) || (std::strncmp(line, "HETATM", 6) == 0))
1025 void gmx_pdb_read_conf(const char* infile, t_symtab* symtab, char** name, t_atoms* atoms, rvec x[], PbcType* pbcType, matrix box)
1027 FILE* in = gmx_fio_fopen(infile, "r");
1029 read_pdbfile(in, title, nullptr, atoms, symtab, x, pbcType, box, nullptr);
1030 if (name != nullptr)
1032 *name = gmx_strdup(title);
1037 gmx_conect gmx_conect_generate(const t_topology* top)
1042 /* Fill the conect records */
1043 gc = gmx_conect_init();
1045 for (f = 0; (f < F_NRE); f++)
1049 for (i = 0; (i < top->idef.il[f].nr); i += interaction_function[f].nratoms + 1)
1051 gmx_conect_add(gc, top->idef.il[f].iatoms[i + 1], top->idef.il[f].iatoms[i + 2]);
1058 int gmx_fprintf_pdb_atomline(FILE* fp,
1059 enum PDB_record record,
1060 int atom_seq_number,
1061 const char* atom_name,
1062 char alternate_location,
1063 const char* res_name,
1066 char res_insertion_code,
1072 const char* element)
1074 char tmp_atomname[6], tmp_resname[6];
1075 gmx_bool start_name_in_col13;
1078 if (record != epdbATOM && record != epdbHETATM)
1080 gmx_fatal(FARGS, "Can only print PDB atom lines as ATOM or HETATM records");
1083 /* Format atom name */
1084 if (atom_name != nullptr)
1086 /* If the atom name is an element name with two chars, it should start already in column 13.
1087 * Otherwise it should start in column 14, unless the name length is 4 chars.
1089 if ((element != nullptr) && (std::strlen(element) >= 2)
1090 && (gmx_strncasecmp(atom_name, element, 2) == 0))
1092 start_name_in_col13 = TRUE;
1096 start_name_in_col13 = (std::strlen(atom_name) >= 4);
1098 snprintf(tmp_atomname, sizeof(tmp_atomname), start_name_in_col13 ? "" : " ");
1099 std::strncat(tmp_atomname, atom_name, 4);
1100 tmp_atomname[5] = '\0';
1104 tmp_atomname[0] = '\0';
1107 /* Format residue name */
1108 std::strncpy(tmp_resname, (res_name != nullptr) ? res_name : "", 4);
1109 /* Make sure the string is terminated if strlen was > 4 */
1110 tmp_resname[4] = '\0';
1111 /* String is properly terminated, so now we can use strcat. By adding a
1112 * space we can write it right-justified, and if the original name was
1113 * three characters or less there will be a space added on the right side.
1115 std::strcat(tmp_resname, " ");
1117 /* Truncate integers so they fit */
1118 atom_seq_number = atom_seq_number % 100000;
1119 res_seq_number = res_seq_number % 10000;
1122 "%-6s%5d %-4.4s%c%4.4s%c%4d%c %8.3f%8.3f%8.3f%6.2f%6.2f %2s\n",
1136 (element != nullptr) ? element : "");