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,2021, 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"
50 #include "gromacs/fileio/gmxfio.h"
51 #include "gromacs/math/units.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/pbcutil/pbc.h"
54 #include "gromacs/topology/atomprop.h"
55 #include "gromacs/topology/ifunc.h"
56 #include "gromacs/topology/residuetypes.h"
57 #include "gromacs/topology/symtab.h"
58 #include "gromacs/topology/topology.h"
59 #include "gromacs/utility/coolstuff.h"
60 #include "gromacs/utility/cstringutil.h"
61 #include "gromacs/utility/enumerationhelpers.h"
62 #include "gromacs/utility/fatalerror.h"
63 #include "gromacs/utility/futil.h"
64 #include "gromacs/utility/smalloc.h"
65 #include "gromacs/utility/snprintf.h"
66 #include "gromacs/utility/stringtoenumvalueconverter.h"
73 typedef struct gmx_conect_t
77 gmx_conection_t* conect;
80 const char* enumValueToString(PdbRecordType enumValue)
82 static constexpr gmx::EnumerationArray<PdbRecordType, const char*> pdbRecordTypeName = {
83 "ATOM ", "HETATM", "ANISOU", "CRYST1", "COMPND", "MODEL",
84 "ENDMDL", "TER", "HEADER", "TITLE", "REMARK", "CONECT"
86 return pdbRecordTypeName[enumValue];
89 #define REMARK_SIM_BOX "REMARK THIS IS A SIMULATION BOX"
91 void gmx_write_pdb_box(FILE* out, PbcType pbcType, const matrix box)
93 real alpha, beta, gamma;
95 if (pbcType == PbcType::Unset)
97 pbcType = guessPbcType(box);
100 if (pbcType == PbcType::No)
105 if (norm2(box[YY]) * norm2(box[ZZ]) != 0)
107 alpha = RAD2DEG * gmx_angle(box[YY], box[ZZ]);
113 if (norm2(box[XX]) * norm2(box[ZZ]) != 0)
115 beta = RAD2DEG * gmx_angle(box[XX], box[ZZ]);
121 if (norm2(box[XX]) * norm2(box[YY]) != 0)
123 gamma = RAD2DEG * gmx_angle(box[XX], box[YY]);
129 fprintf(out, "REMARK THIS IS A SIMULATION BOX\n");
130 if (pbcType != PbcType::Screw)
133 "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n",
145 /* Double the a-vector length and write the correct space group */
147 "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n",
159 static void read_cryst1(char* line, PbcType* pbcType, matrix box)
162 char sa[12], sb[12], sc[12], sg[SG_SIZE + 1], ident;
163 double fa, fb, fc, alpha, beta, gamma, cosa, cosb, cosg, sing;
164 int syma, symb, symc;
167 sscanf(line, "%*s%s%s%s%lf%lf%lf", sa, sb, sc, &alpha, &beta, &gamma);
169 pbcTypeFile = PbcType::Unset;
170 if (strlen(line) >= 55)
172 strncpy(sg, line + 55, SG_SIZE);
178 sscanf(sg, "%c %d %d %d", &ident, &syma, &symb, &symc);
179 if (ident == 'P' && syma == 1 && symb <= 1 && symc <= 1)
181 fc = strtod(sc, nullptr) * 0.1;
182 pbcTypeFile = (fc > 0 ? PbcType::Xyz : PbcType::XY);
184 if (ident == 'P' && syma == 21 && symb == 1 && symc == 1)
186 pbcTypeFile = PbcType::Screw;
191 *pbcType = pbcTypeFile;
196 fa = strtod(sa, nullptr) * 0.1;
197 fb = strtod(sb, nullptr) * 0.1;
198 fc = strtod(sc, nullptr) * 0.1;
199 if (pbcTypeFile == PbcType::Screw)
205 if ((alpha != 90.0) || (beta != 90.0) || (gamma != 90.0))
209 cosa = std::cos(alpha * DEG2RAD);
217 cosb = std::cos(beta * DEG2RAD);
225 cosg = std::cos(gamma * DEG2RAD);
226 sing = std::sin(gamma * DEG2RAD);
233 box[YY][XX] = fb * cosg;
234 box[YY][YY] = fb * sing;
235 box[ZZ][XX] = fc * cosb;
236 box[ZZ][YY] = fc * (cosa - cosb * cosg) / sing;
237 box[ZZ][ZZ] = std::sqrt(fc * fc - box[ZZ][XX] * box[ZZ][XX] - box[ZZ][YY] * box[ZZ][YY]);
247 static int gmx_fprintf_pqr_atomline(FILE* fp,
248 PdbRecordType record,
250 const char* atom_name,
251 const char* res_name,
260 GMX_RELEASE_ASSERT(record == PdbRecordType::Atom || record == PdbRecordType::Hetatm,
261 "Can only print PQR atom lines as ATOM or HETATM records");
263 /* Check atom name */
264 GMX_RELEASE_ASSERT(atom_name != nullptr, "Need atom information to print pqr");
266 /* Check residue name */
267 GMX_RELEASE_ASSERT(res_name != nullptr, "Need residue information to print pqr");
269 /* Truncate integers so they fit */
270 atom_seq_number = atom_seq_number % 100000;
271 res_seq_number = res_seq_number % 10000;
274 "%-6s%5d %-4.4s%4.4s%c%4d %8.3f %8.3f %8.3f %6.2f %6.2f\n",
275 enumValueToString(record),
290 void write_pdbfile_indexed(FILE* out,
292 const t_atoms* atoms,
303 gmx_conect_t* gc = static_cast<gmx_conect_t*>(conect);
310 fprintf(out, "TITLE %s\n", (title && title[0]) ? title : gmx::bromacs().c_str());
311 if (box && ((norm2(box[XX]) != 0.0F) || (norm2(box[YY]) != 0.0F) || (norm2(box[ZZ]) != 0.0F)))
313 gmx_write_pdb_box(out, pbcType, box);
315 if (atoms->havePdbInfo)
317 /* Check whether any occupancies are set, in that case leave it as is,
318 * otherwise set them all to one
321 for (int ii = 0; (ii < nindex) && bOccup; ii++)
324 bOccup = bOccup && (atoms->pdbinfo[i].occup == 0.0);
332 fprintf(out, "MODEL %8d\n", model_nr > 0 ? model_nr : 1);
335 for (int ii = 0; ii < nindex; ii++)
338 int resind = atoms->atom[i].resind;
339 std::string resnm = *atoms->resinfo[resind].name;
340 std::string nm = *atoms->atomname[i];
342 int resnr = atoms->resinfo[resind].nr;
343 unsigned char resic = atoms->resinfo[resind].ic;
351 ch = atoms->resinfo[resind].chainid;
360 resnr = resnr % 10000;
363 if (atoms->pdbinfo != nullptr)
365 pdbinfo = atoms->pdbinfo[i];
369 gmx_pdbinfo_init_default(&pdbinfo);
372 altloc = pdbinfo.altloc;
373 if (!isalnum(altloc))
377 occup = bOccup ? 1.0 : pdbinfo.occup;
381 gmx_fprintf_pdb_atomline(out,
395 atoms->atom[i].elem);
397 if (atoms->pdbinfo && atoms->pdbinfo[i].bAnisotropic)
400 "ANISOU%5d %-4.4s%4.4s%c%4d%c %7d%7d%7d%7d%7d%7d\n",
406 (resic == '\0') ? ' ' : resic,
407 atoms->pdbinfo[i].uij[0],
408 atoms->pdbinfo[i].uij[1],
409 atoms->pdbinfo[i].uij[2],
410 atoms->pdbinfo[i].uij[3],
411 atoms->pdbinfo[i].uij[4],
412 atoms->pdbinfo[i].uij[5]);
417 gmx_fprintf_pqr_atomline(out,
432 fprintf(out, "TER\n");
433 fprintf(out, "ENDMDL\n");
437 /* Write conect records */
438 for (int i = 0; (i < gc->nconect); i++)
440 fprintf(out, "CONECT%5d%5d\n", gc->conect[i].ai + 1, gc->conect[i].aj + 1);
445 void write_pdbfile(FILE* out,
447 const t_atoms* atoms,
457 snew(index, atoms->nr);
458 for (i = 0; i < atoms->nr; i++)
462 write_pdbfile_indexed(
463 out, title, atoms, x, pbcType, box, chainid, model_nr, atoms->nr, index, conect, false);
467 static void read_anisou(char line[], int natom, t_atoms* atoms)
471 char anr[12], anm[12];
475 for (k = 0; (k < 5); k++, j++)
481 for (k = 0; (k < 4); k++, j++)
488 /* Strip off spaces */
491 /* Search backwards for number and name only */
492 atomnr = std::strtol(anr, nullptr, 10);
493 for (i = natom - 1; (i >= 0); i--)
495 if ((std::strcmp(anm, *(atoms->atomname[i])) == 0) && (atomnr == atoms->pdbinfo[i].atomnr))
502 fprintf(stderr, "Skipping ANISOU record (atom %s %d not found)\n", anm, atomnr);
506 if (sscanf(line + 29,
508 &atoms->pdbinfo[i].uij[U11],
509 &atoms->pdbinfo[i].uij[U22],
510 &atoms->pdbinfo[i].uij[U33],
511 &atoms->pdbinfo[i].uij[U12],
512 &atoms->pdbinfo[i].uij[U13],
513 &atoms->pdbinfo[i].uij[U23])
516 atoms->pdbinfo[i].bAnisotropic = TRUE;
520 fprintf(stderr, "Invalid ANISOU record for atom %d\n", i);
521 atoms->pdbinfo[i].bAnisotropic = FALSE;
526 void get_pdb_atomnumber(const t_atoms* atoms, AtomProperties* aps)
528 int i, atomnumber, len;
530 char anm[6], anm_copy[6];
536 gmx_incons("Trying to deduce atomnumbers when no pdb information is present");
538 for (i = 0; (i < atoms->nr); i++)
540 std::strcpy(anm, atoms->pdbinfo[i].atomnm);
541 std::strcpy(anm_copy, atoms->pdbinfo[i].atomnm);
542 bool atomNumberSet = false;
544 if ((anm[0] != ' ') && ((len <= 2) || !std::isdigit(anm[2])))
547 if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
549 atomnumber = gmx::roundToInt(eval);
550 atomNumberSet = true;
555 if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
557 atomnumber = gmx::roundToInt(eval);
558 atomNumberSet = true;
565 while ((k < std::strlen(anm)) && (std::isspace(anm[k]) || std::isdigit(anm[k])))
569 anm_copy[0] = anm[k];
571 if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
573 atomnumber = gmx::roundToInt(eval);
574 atomNumberSet = true;
580 atoms->atom[i].atomnumber = atomnumber;
581 buf = aps->elementFromAtomNumber(atomnumber);
584 fprintf(debug, "Atomnumber for atom '%s' is %d\n", anm, atomnumber);
588 std::strncpy(atoms->atom[i].elem, buf.c_str(), 4);
593 read_atom(t_symtab* symtab, const char line[], PdbRecordType type, int natom, t_atoms* atoms, rvec x[], int chainnum)
598 char anr[12], anm[12], anm_copy[12], altloc, resnm[12], rnr[12], elem[3];
599 char xc[12], yc[12], zc[12], occup[12], bfac[12];
602 int resnr, atomnumber;
604 if (natom >= atoms->nr)
606 gmx_fatal(FARGS, "\nFound more atoms (%d) in pdb file than expected (%d)", natom + 1, atoms->nr);
611 for (k = 0; (k < 5); k++, j++)
618 for (k = 0; (k < 4); k++, j++)
623 std::strcpy(anm_copy, anm);
629 for (k = 0; (k < 4); k++, j++)
639 for (k = 0; (k < 4); k++, j++)
645 resnr = std::strtol(rnr, nullptr, 10);
649 /* X,Y,Z Coordinate */
650 for (k = 0; (k < 8); k++, j++)
655 for (k = 0; (k < 8); k++, j++)
660 for (k = 0; (k < 8); k++, j++)
667 for (k = 0; (k < 6); k++, j++)
674 for (k = 0; (k < 7); k++, j++)
684 for (k = 0; (k < 2); k++, j++)
693 atomn = &(atoms->atom[natom]);
694 if ((natom == 0) || atoms->resinfo[atoms->atom[natom - 1].resind].nr != resnr
695 || atoms->resinfo[atoms->atom[natom - 1].resind].ic != resic
696 || (strcmp(*atoms->resinfo[atoms->atom[natom - 1].resind].name, resnm) != 0))
704 atomn->resind = atoms->atom[natom - 1].resind + 1;
706 atoms->nres = atomn->resind + 1;
707 t_atoms_set_resinfo(atoms, natom, symtab, resnm, resnr, resic, chainnum, chainid);
711 atomn->resind = atoms->atom[natom - 1].resind;
713 atoms->atomname[natom] = put_symtab(symtab, anm);
716 atomn->atomnumber = atomnumber;
717 strncpy(atomn->elem, elem, 4);
719 x[natom][XX] = strtod(xc, nullptr) * 0.1;
720 x[natom][YY] = strtod(yc, nullptr) * 0.1;
721 x[natom][ZZ] = strtod(zc, nullptr) * 0.1;
724 atoms->pdbinfo[natom].type = type;
725 atoms->pdbinfo[natom].atomnr = strtol(anr, nullptr, 10);
726 atoms->pdbinfo[natom].altloc = altloc;
727 strcpy(atoms->pdbinfo[natom].atomnm, anm_copy);
728 atoms->pdbinfo[natom].bfac = strtod(bfac, nullptr);
729 atoms->pdbinfo[natom].occup = strtod(occup, nullptr);
736 gmx_bool is_hydrogen(const char* nm)
740 std::strcpy(buf, nm);
743 return ((buf[0] == 'H') || ((std::isdigit(buf[0]) != 0) && (buf[1] == 'H')));
746 gmx_bool is_dummymass(const char* nm)
750 std::strcpy(buf, nm);
753 return (buf[0] == 'M') && (std::isdigit(buf[strlen(buf) - 1]) != 0);
756 static void gmx_conect_addline(gmx_conect_t* con, char* line)
760 std::string form2 = "%*s";
761 std::string format = form2 + "%d";
762 if (sscanf(line, format.c_str(), &ai) == 1)
767 format = form2 + "%d";
768 n = sscanf(line, format.c_str(), &aj);
771 gmx_conect_add(con, ai - 1, aj - 1); /* to prevent duplicated records */
777 void gmx_conect_dump(FILE* fp, gmx_conect conect)
779 gmx_conect_t* gc = static_cast<gmx_conect_t*>(conect);
782 for (i = 0; (i < gc->nconect); i++)
784 fprintf(fp, "%6s%5d%5d\n", "CONECT", gc->conect[i].ai + 1, gc->conect[i].aj + 1);
788 gmx_conect gmx_conect_init()
797 void gmx_conect_done(gmx_conect conect)
799 gmx_conect_t* gc = conect;
804 gmx_bool gmx_conect_exist(gmx_conect conect, int ai, int aj)
806 gmx_conect_t* gc = conect;
812 for (i = 0; (i < gc->nconect); i++)
814 if (((gc->conect[i].ai == ai) && (gc->conect[i].aj == aj))
815 || ((gc->conect[i].aj == ai) && (gc->conect[i].ai == aj)))
823 void gmx_conect_add(gmx_conect conect, int ai, int aj)
825 gmx_conect_t* gc = static_cast<gmx_conect_t*>(conect);
830 if (!gmx_conect_exist(conect, ai, aj))
832 srenew(gc->conect, ++gc->nconect);
833 gc->conect[gc->nconect - 1].ai = ai;
834 gc->conect[gc->nconect - 1].aj = aj;
838 int read_pdbfile(FILE* in,
848 gmx_conect_t* gc = conect;
850 gmx_bool bConnWarn = FALSE;
851 char line[STRLEN + 1];
854 gmx_bool bStop = FALSE;
858 /* Only assume pbc when there is a CRYST1 entry */
859 *pbcType = PbcType::No;
866 atoms->haveMass = FALSE;
867 atoms->haveCharge = FALSE;
868 atoms->haveType = FALSE;
869 atoms->haveBState = FALSE;
870 atoms->havePdbInfo = (atoms->pdbinfo != nullptr);
876 static const gmx::StringToEnumValueConverter<PdbRecordType, enumValueToString, gmx::StringCompareType::Exact, gmx::StripStrings::Yes>
877 sc_pdbRecordTypeIdentifier;
878 while (!bStop && (fgets2(line, STRLEN, in) != nullptr))
880 // Convert line to a null-terminated string, then take a substring of at most 6 chars
881 std::string lineAsString(line);
882 std::optional<PdbRecordType> line_type =
883 sc_pdbRecordTypeIdentifier.valueFrom(lineAsString.substr(0, 6));
886 // Skip this line because it does not start with a
887 // recognized leading string describing a PDB record type.
891 switch (line_type.value())
893 case PdbRecordType::Atom:
894 case PdbRecordType::Hetatm:
895 natom = read_atom(symtab, line, line_type.value(), natom, atoms, x, chainnum);
898 case PdbRecordType::Anisou:
899 if (atoms->havePdbInfo)
901 read_anisou(line, natom, atoms);
905 case PdbRecordType::Cryst1: read_cryst1(line, pbcType, box); break;
907 case PdbRecordType::Title:
908 case PdbRecordType::Header:
909 if (std::strlen(line) > 6)
912 /* skip HEADER or TITLE and spaces */
921 /* truncate after title */
922 d = std::strstr(c, " ");
927 if (std::strlen(c) > 0)
929 std::strcpy(title, c);
934 case PdbRecordType::Compound:
935 if ((!std::strstr(line, ": ")) || (std::strstr(line + 6, "MOLECULE:")))
937 if (!(c = std::strstr(line + 6, "MOLECULE:")))
941 /* skip 'MOLECULE:' and spaces */
950 /* truncate after title */
954 while ((d[-1] == ';') && d > c)
964 std::strcat(title, "; ");
965 std::strcat(title, c);
969 std::strcpy(title, c);
976 case PdbRecordType::Ter: chainnum++; break;
978 case PdbRecordType::Model:
981 sscanf(line, "%*s%d", model_nr);
985 case PdbRecordType::EndModel: bStop = TRUE; break;
986 case PdbRecordType::Conect:
989 gmx_conect_addline(gc, line);
993 fprintf(stderr, "WARNING: all CONECT records are ignored\n");
1005 void get_pdb_coordnum(FILE* in, int* natoms)
1010 while (fgets2(line, STRLEN, in))
1012 if (std::strncmp(line, "ENDMDL", 6) == 0)
1016 if ((std::strncmp(line, "ATOM ", 6) == 0) || (std::strncmp(line, "HETATM", 6) == 0))
1023 void gmx_pdb_read_conf(const char* infile, t_symtab* symtab, char** name, t_atoms* atoms, rvec x[], PbcType* pbcType, matrix box)
1025 FILE* in = gmx_fio_fopen(infile, "r");
1027 read_pdbfile(in, title, nullptr, atoms, symtab, x, pbcType, box, nullptr);
1028 if (name != nullptr)
1030 *name = gmx_strdup(title);
1035 gmx_conect gmx_conect_generate(const t_topology* top)
1040 /* Fill the conect records */
1041 gc = gmx_conect_init();
1043 for (f = 0; (f < F_NRE); f++)
1047 for (i = 0; (i < top->idef.il[f].nr); i += interaction_function[f].nratoms + 1)
1049 gmx_conect_add(gc, top->idef.il[f].iatoms[i + 1], top->idef.il[f].iatoms[i + 2]);
1056 int gmx_fprintf_pdb_atomline(FILE* fp,
1057 PdbRecordType record,
1058 int atom_seq_number,
1059 const char* atom_name,
1060 char alternate_location,
1061 const char* res_name,
1064 char res_insertion_code,
1070 const char* element)
1072 char tmp_atomname[6], tmp_resname[6];
1073 gmx_bool start_name_in_col13;
1076 if (record != PdbRecordType::Atom && record != PdbRecordType::Hetatm)
1078 gmx_fatal(FARGS, "Can only print PDB atom lines as ATOM or HETATM records");
1081 /* Format atom name */
1082 if (atom_name != nullptr)
1084 /* If the atom name is an element name with two chars, it should start already in column 13.
1085 * Otherwise it should start in column 14, unless the name length is 4 chars.
1087 if ((element != nullptr) && (std::strlen(element) >= 2)
1088 && (gmx_strncasecmp(atom_name, element, 2) == 0))
1090 start_name_in_col13 = TRUE;
1094 start_name_in_col13 = (std::strlen(atom_name) >= 4);
1096 snprintf(tmp_atomname, sizeof(tmp_atomname), start_name_in_col13 ? "" : " ");
1097 std::strncat(tmp_atomname, atom_name, 4);
1098 tmp_atomname[5] = '\0';
1102 tmp_atomname[0] = '\0';
1105 /* Format residue name */
1106 std::strncpy(tmp_resname, (res_name != nullptr) ? res_name : "", 4);
1107 /* Make sure the string is terminated if strlen was > 4 */
1108 tmp_resname[4] = '\0';
1109 /* String is properly terminated, so now we can use strcat. By adding a
1110 * space we can write it right-justified, and if the original name was
1111 * three characters or less there will be a space added on the right side.
1113 std::strcat(tmp_resname, " ");
1115 /* Truncate integers so they fit */
1116 atom_seq_number = atom_seq_number % 100000;
1117 res_seq_number = res_seq_number % 10000;
1120 "%-6s%5d %-4.4s%c%4.4s%c%4d%c %8.3f%8.3f%8.3f%6.2f%6.2f %2s\n",
1121 enumValueToString(record),
1134 (element != nullptr) ? element : "");