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/utilities.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/pbcutil/pbc.h"
55 #include "gromacs/topology/atomprop.h"
56 #include "gromacs/topology/ifunc.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 void gmx_write_pdb_box(FILE* out, PbcType pbcType, const matrix box)
91 real alpha, beta, gamma;
93 if (pbcType == PbcType::Unset)
95 pbcType = guessPbcType(box);
98 if (pbcType == PbcType::No)
103 if (norm2(box[YY]) * norm2(box[ZZ]) != 0)
105 alpha = gmx::c_rad2Deg * gmx_angle(box[YY], box[ZZ]);
111 if (norm2(box[XX]) * norm2(box[ZZ]) != 0)
113 beta = gmx::c_rad2Deg * gmx_angle(box[XX], box[ZZ]);
119 if (norm2(box[XX]) * norm2(box[YY]) != 0)
121 gamma = gmx::c_rad2Deg * gmx_angle(box[XX], box[YY]);
127 fprintf(out, "REMARK THIS IS A SIMULATION BOX\n");
128 if (pbcType != PbcType::Screw)
131 "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n",
143 /* Double the a-vector length and write the correct space group */
145 "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n",
157 static void read_cryst1(char* line, PbcType* pbcType, matrix box)
160 char sa[12], sb[12], sc[12], sg[SG_SIZE + 1], ident;
161 double fa, fb, fc, alpha, beta, gamma, cosa, cosb, cosg, sing;
162 int syma, symb, symc;
165 sscanf(line, "%*s%s%s%s%lf%lf%lf", sa, sb, sc, &alpha, &beta, &gamma);
167 pbcTypeFile = PbcType::Unset;
168 if (strlen(line) >= 55)
170 strncpy(sg, line + 55, SG_SIZE);
176 sscanf(sg, "%c %d %d %d", &ident, &syma, &symb, &symc);
177 if (ident == 'P' && syma == 1 && symb <= 1 && symc <= 1)
179 fc = strtod(sc, nullptr) * 0.1;
180 pbcTypeFile = (fc > 0 ? PbcType::Xyz : PbcType::XY);
182 if (ident == 'P' && syma == 21 && symb == 1 && symc == 1)
184 pbcTypeFile = PbcType::Screw;
189 *pbcType = pbcTypeFile;
194 fa = strtod(sa, nullptr) * 0.1;
195 fb = strtod(sb, nullptr) * 0.1;
196 fc = strtod(sc, nullptr) * 0.1;
197 if (pbcTypeFile == PbcType::Screw)
203 if ((alpha != 90.0) || (beta != 90.0) || (gamma != 90.0))
207 cosa = std::cos(alpha * gmx::c_deg2Rad);
215 cosb = std::cos(beta * gmx::c_deg2Rad);
223 cosg = std::cos(gamma * gmx::c_deg2Rad);
224 sing = std::sin(gamma * gmx::c_deg2Rad);
231 box[YY][XX] = fb * cosg;
232 box[YY][YY] = fb * sing;
233 box[ZZ][XX] = fc * cosb;
234 box[ZZ][YY] = fc * (cosa - cosb * cosg) / sing;
235 box[ZZ][ZZ] = std::sqrt(fc * fc - box[ZZ][XX] * box[ZZ][XX] - box[ZZ][YY] * box[ZZ][YY]);
245 static int gmx_fprintf_pqr_atomline(FILE* fp,
246 PdbRecordType record,
248 const char* atom_name,
249 const char* res_name,
258 GMX_RELEASE_ASSERT(record == PdbRecordType::Atom || record == PdbRecordType::Hetatm,
259 "Can only print PQR atom lines as ATOM or HETATM records");
261 /* Check atom name */
262 GMX_RELEASE_ASSERT(atom_name != nullptr, "Need atom information to print pqr");
264 /* Check residue name */
265 GMX_RELEASE_ASSERT(res_name != nullptr, "Need residue information to print pqr");
267 /* Truncate integers so they fit */
268 atom_seq_number = atom_seq_number % 100000;
269 res_seq_number = res_seq_number % 10000;
272 "%-6s%5d %-4.4s%4.4s%c%4d %8.3f %8.3f %8.3f %6.2f %6.2f\n",
273 enumValueToString(record),
288 void write_pdbfile_indexed(FILE* out,
290 const t_atoms* atoms,
301 gmx_conect_t* gc = static_cast<gmx_conect_t*>(conect);
308 fprintf(out, "TITLE %s\n", (title && title[0]) ? title : gmx::bromacs().c_str());
309 if (box && ((norm2(box[XX]) != 0.0F) || (norm2(box[YY]) != 0.0F) || (norm2(box[ZZ]) != 0.0F)))
311 gmx_write_pdb_box(out, pbcType, box);
313 if (atoms->havePdbInfo)
315 /* Check whether any occupancies are set, in that case leave it as is,
316 * otherwise set them all to one
319 for (int ii = 0; (ii < nindex) && bOccup; ii++)
322 bOccup = bOccup && (atoms->pdbinfo[i].occup == 0.0);
330 fprintf(out, "MODEL %8d\n", model_nr > 0 ? model_nr : 1);
332 for (int ii = 0; ii < nindex; ii++)
335 int resind = atoms->atom[i].resind;
336 std::string resnm = *atoms->resinfo[resind].name;
337 std::string nm = *atoms->atomname[i];
339 int resnr = atoms->resinfo[resind].nr;
340 unsigned char resic = atoms->resinfo[resind].ic;
348 ch = atoms->resinfo[resind].chainid;
357 resnr = resnr % 10000;
360 if (atoms->pdbinfo != nullptr)
362 pdbinfo = atoms->pdbinfo[i];
366 gmx_pdbinfo_init_default(&pdbinfo);
369 altloc = pdbinfo.altloc;
370 if (!isalnum(altloc))
374 occup = bOccup ? 1.0 : pdbinfo.occup;
378 gmx_fprintf_pdb_atomline(out,
392 atoms->atom[i].elem);
394 if (atoms->pdbinfo && atoms->pdbinfo[i].bAnisotropic)
397 "ANISOU%5d %-4.4s%4.4s%c%4d%c %7d%7d%7d%7d%7d%7d\n",
403 (resic == '\0') ? ' ' : resic,
404 atoms->pdbinfo[i].uij[0],
405 atoms->pdbinfo[i].uij[1],
406 atoms->pdbinfo[i].uij[2],
407 atoms->pdbinfo[i].uij[3],
408 atoms->pdbinfo[i].uij[4],
409 atoms->pdbinfo[i].uij[5]);
414 gmx_fprintf_pqr_atomline(out,
429 fprintf(out, "TER\n");
430 fprintf(out, "ENDMDL\n");
434 /* Write conect records */
435 for (int i = 0; (i < gc->nconect); i++)
437 fprintf(out, "CONECT%5d%5d\n", gc->conect[i].ai + 1, gc->conect[i].aj + 1);
442 void write_pdbfile(FILE* out,
444 const t_atoms* atoms,
454 snew(index, atoms->nr);
455 for (i = 0; i < atoms->nr; i++)
459 write_pdbfile_indexed(
460 out, title, atoms, x, pbcType, box, chainid, model_nr, atoms->nr, index, conect, false);
464 static void read_anisou(char line[], int natom, t_atoms* atoms)
468 char anr[12], anm[12];
472 for (k = 0; (k < 5); k++, j++)
478 for (k = 0; (k < 4); k++, j++)
485 /* Strip off spaces */
488 /* Search backwards for number and name only */
489 atomnr = std::strtol(anr, nullptr, 10);
490 for (i = natom - 1; (i >= 0); i--)
492 if ((std::strcmp(anm, *(atoms->atomname[i])) == 0) && (atomnr == atoms->pdbinfo[i].atomnr))
499 fprintf(stderr, "Skipping ANISOU record (atom %s %d not found)\n", anm, atomnr);
503 if (sscanf(line + 29,
505 &atoms->pdbinfo[i].uij[U11],
506 &atoms->pdbinfo[i].uij[U22],
507 &atoms->pdbinfo[i].uij[U33],
508 &atoms->pdbinfo[i].uij[U12],
509 &atoms->pdbinfo[i].uij[U13],
510 &atoms->pdbinfo[i].uij[U23])
513 atoms->pdbinfo[i].bAnisotropic = TRUE;
517 fprintf(stderr, "Invalid ANISOU record for atom %d\n", i);
518 atoms->pdbinfo[i].bAnisotropic = FALSE;
523 void get_pdb_atomnumber(const t_atoms* atoms, AtomProperties* aps)
525 int i, atomnumber, len;
527 char anm[6], anm_copy[6];
533 gmx_incons("Trying to deduce atomnumbers when no pdb information is present");
535 for (i = 0; (i < atoms->nr); i++)
537 std::strcpy(anm, atoms->pdbinfo[i].atomnm);
538 std::strcpy(anm_copy, atoms->pdbinfo[i].atomnm);
539 bool atomNumberSet = false;
541 if ((anm[0] != ' ') && ((len <= 2) || !std::isdigit(anm[2])))
544 if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
546 atomnumber = gmx::roundToInt(eval);
547 atomNumberSet = true;
552 if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
554 atomnumber = gmx::roundToInt(eval);
555 atomNumberSet = true;
562 while ((k < std::strlen(anm)) && (std::isspace(anm[k]) || std::isdigit(anm[k])))
566 anm_copy[0] = anm[k];
568 if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
570 atomnumber = gmx::roundToInt(eval);
571 atomNumberSet = true;
574 static constexpr size_t sc_maxElementNameLength = 3;
575 static_assert(sizeof(atoms->atom[i].elem) >= sc_maxElementNameLength + 1);
579 atoms->atom[i].atomnumber = atomnumber;
580 element = aps->elementFromAtomNumber(atomnumber);
583 fprintf(debug, "Atomnumber for atom '%s' is %d\n", anm, atomnumber);
586 element.resize(sc_maxElementNameLength);
587 std::strcpy(atoms->atom[i].elem, element.c_str());
592 read_atom(t_symtab* symtab, const char line[], PdbRecordType type, int natom, t_atoms* atoms, rvec x[], int chainnum)
597 char anr[12], anm[12], anm_copy[12], altloc, resnm[12], rnr[12], elem[3];
598 char xc[12], yc[12], zc[12], occup[12], bfac[12];
601 int resnr, atomnumber;
603 if (natom >= atoms->nr)
605 gmx_fatal(FARGS, "\nFound more atoms (%d) in pdb file than expected (%d)", natom + 1, atoms->nr);
610 for (k = 0; (k < 5); k++, j++)
617 for (k = 0; (k < 4); k++, j++)
622 std::strcpy(anm_copy, anm);
628 for (k = 0; (k < 4); k++, j++)
638 for (k = 0; (k < 4); k++, j++)
644 resnr = std::strtol(rnr, nullptr, 10);
648 /* X,Y,Z Coordinate */
649 for (k = 0; (k < 8); k++, j++)
654 for (k = 0; (k < 8); k++, j++)
659 for (k = 0; (k < 8); k++, j++)
666 for (k = 0; (k < 6); k++, j++)
673 for (k = 0; (k < 7); k++, j++)
683 for (k = 0; (k < 2); k++, j++)
692 atomn = &(atoms->atom[natom]);
693 if ((natom == 0) || atoms->resinfo[atoms->atom[natom - 1].resind].nr != resnr
694 || atoms->resinfo[atoms->atom[natom - 1].resind].ic != resic
695 || (strcmp(*atoms->resinfo[atoms->atom[natom - 1].resind].name, resnm) != 0))
703 atomn->resind = atoms->atom[natom - 1].resind + 1;
705 atoms->nres = atomn->resind + 1;
706 t_atoms_set_resinfo(atoms, natom, symtab, resnm, resnr, resic, chainnum, chainid);
710 atomn->resind = atoms->atom[natom - 1].resind;
712 atoms->atomname[natom] = put_symtab(symtab, anm);
715 atomn->atomnumber = atomnumber;
716 strncpy(atomn->elem, elem, 4);
718 x[natom][XX] = strtod(xc, nullptr) * 0.1;
719 x[natom][YY] = strtod(yc, nullptr) * 0.1;
720 x[natom][ZZ] = strtod(zc, nullptr) * 0.1;
723 atoms->pdbinfo[natom].type = type;
724 atoms->pdbinfo[natom].atomnr = strtol(anr, nullptr, 10);
725 atoms->pdbinfo[natom].altloc = altloc;
726 strcpy(atoms->pdbinfo[natom].atomnm, anm_copy);
727 atoms->pdbinfo[natom].bfac = strtod(bfac, nullptr);
728 atoms->pdbinfo[natom].occup = strtod(occup, nullptr);
735 gmx_bool is_hydrogen(const char* nm)
739 std::strcpy(buf, nm);
742 return ((buf[0] == 'H') || ((std::isdigit(buf[0]) != 0) && (buf[1] == 'H')));
745 gmx_bool is_dummymass(const char* nm)
749 std::strcpy(buf, nm);
752 return (buf[0] == 'M') && (std::isdigit(buf[strlen(buf) - 1]) != 0);
755 static void gmx_conect_addline(gmx_conect_t* con, char* line)
759 std::string form2 = "%*s";
760 std::string format = form2 + "%d";
761 if (sscanf(line, format.c_str(), &ai) == 1)
766 format = form2 + "%d";
767 n = sscanf(line, format.c_str(), &aj);
770 gmx_conect_add(con, ai - 1, aj - 1); /* to prevent duplicated records */
776 void gmx_conect_dump(FILE* fp, gmx_conect conect)
778 gmx_conect_t* gc = static_cast<gmx_conect_t*>(conect);
781 for (i = 0; (i < gc->nconect); i++)
783 fprintf(fp, "%6s%5d%5d\n", "CONECT", gc->conect[i].ai + 1, gc->conect[i].aj + 1);
787 gmx_conect gmx_conect_init()
796 void gmx_conect_done(gmx_conect conect)
798 gmx_conect_t* gc = conect;
803 gmx_bool gmx_conect_exist(gmx_conect conect, int ai, int aj)
805 gmx_conect_t* gc = conect;
811 for (i = 0; (i < gc->nconect); i++)
813 if (((gc->conect[i].ai == ai) && (gc->conect[i].aj == aj))
814 || ((gc->conect[i].aj == ai) && (gc->conect[i].ai == aj)))
822 void gmx_conect_add(gmx_conect conect, int ai, int aj)
824 gmx_conect_t* gc = static_cast<gmx_conect_t*>(conect);
829 if (!gmx_conect_exist(conect, ai, aj))
831 srenew(gc->conect, ++gc->nconect);
832 gc->conect[gc->nconect - 1].ai = ai;
833 gc->conect[gc->nconect - 1].aj = aj;
837 int read_pdbfile(FILE* in,
847 gmx_conect_t* gc = conect;
849 gmx_bool bConnWarn = FALSE;
850 char line[STRLEN + 1];
853 gmx_bool bStop = FALSE;
857 /* Only assume pbc when there is a CRYST1 entry */
858 *pbcType = PbcType::No;
865 atoms->haveMass = FALSE;
866 atoms->haveCharge = FALSE;
867 atoms->haveType = FALSE;
868 atoms->haveBState = FALSE;
869 atoms->havePdbInfo = (atoms->pdbinfo != nullptr);
875 static const gmx::StringToEnumValueConverter<PdbRecordType, enumValueToString, gmx::StringCompareType::Exact, gmx::StripStrings::Yes>
876 sc_pdbRecordTypeIdentifier;
877 while (!bStop && (fgets2(line, STRLEN, in) != nullptr))
879 // Convert line to a null-terminated string, then take a substring of at most 6 chars
880 std::string lineAsString(line);
881 std::optional<PdbRecordType> line_type =
882 sc_pdbRecordTypeIdentifier.valueFrom(lineAsString.substr(0, 6));
885 // Skip this line because it does not start with a
886 // recognized leading string describing a PDB record type.
890 switch (line_type.value())
892 case PdbRecordType::Atom:
893 case PdbRecordType::Hetatm:
894 natom = read_atom(symtab, line, line_type.value(), natom, atoms, x, chainnum);
897 case PdbRecordType::Anisou:
898 if (atoms->havePdbInfo)
900 read_anisou(line, natom, atoms);
904 case PdbRecordType::Cryst1: read_cryst1(line, pbcType, box); break;
906 case PdbRecordType::Title:
907 case PdbRecordType::Header:
908 if (std::strlen(line) > 6)
911 /* skip HEADER or TITLE and spaces */
920 /* truncate after title */
921 d = std::strstr(c, " ");
926 if (std::strlen(c) > 0)
928 std::strcpy(title, c);
933 case PdbRecordType::Compound:
934 if ((!std::strstr(line, ": ")) || (std::strstr(line + 6, "MOLECULE:")))
936 if (!(c = std::strstr(line + 6, "MOLECULE:")))
940 /* skip 'MOLECULE:' and spaces */
949 /* truncate after title */
953 while ((d[-1] == ';') && d > c)
963 std::strcat(title, "; ");
964 std::strcat(title, c);
968 std::strcpy(title, c);
975 case PdbRecordType::Ter: chainnum++; break;
977 case PdbRecordType::Model:
980 sscanf(line, "%*s%d", model_nr);
984 case PdbRecordType::EndModel: bStop = TRUE; break;
985 case PdbRecordType::Conect:
988 gmx_conect_addline(gc, line);
992 fprintf(stderr, "WARNING: all CONECT records are ignored\n");
1004 void get_pdb_coordnum(FILE* in, int* natoms)
1009 while (fgets2(line, STRLEN, in))
1011 if (std::strncmp(line, "ENDMDL", 6) == 0)
1015 if ((std::strncmp(line, "ATOM ", 6) == 0) || (std::strncmp(line, "HETATM", 6) == 0))
1022 void gmx_pdb_read_conf(const char* infile, t_symtab* symtab, char** name, t_atoms* atoms, rvec x[], PbcType* pbcType, matrix box)
1024 FILE* in = gmx_fio_fopen(infile, "r");
1026 read_pdbfile(in, title, nullptr, atoms, symtab, x, pbcType, box, nullptr);
1027 if (name != nullptr)
1029 *name = gmx_strdup(title);
1034 gmx_conect gmx_conect_generate(const t_topology* top)
1039 /* Fill the conect records */
1040 gc = gmx_conect_init();
1042 for (f = 0; (f < F_NRE); f++)
1046 for (i = 0; (i < top->idef.il[f].nr); i += interaction_function[f].nratoms + 1)
1048 gmx_conect_add(gc, top->idef.il[f].iatoms[i + 1], top->idef.il[f].iatoms[i + 2]);
1055 int gmx_fprintf_pdb_atomline(FILE* fp,
1056 PdbRecordType record,
1057 int atom_seq_number,
1058 const char* atom_name,
1059 char alternate_location,
1060 const char* res_name,
1063 char res_insertion_code,
1069 const char* element)
1071 char tmp_atomname[6], tmp_resname[6];
1072 gmx_bool start_name_in_col13;
1075 if (record != PdbRecordType::Atom && record != PdbRecordType::Hetatm)
1077 gmx_fatal(FARGS, "Can only print PDB atom lines as ATOM or HETATM records");
1080 /* Format atom name */
1081 if (atom_name != nullptr)
1083 /* If the atom name is an element name with two chars, it should start already in column 13.
1084 * Otherwise it should start in column 14, unless the name length is 4 chars.
1086 if ((element != nullptr) && (std::strlen(element) >= 2)
1087 && (gmx_strncasecmp(atom_name, element, 2) == 0))
1089 start_name_in_col13 = TRUE;
1093 start_name_in_col13 = (std::strlen(atom_name) >= 4);
1095 snprintf(tmp_atomname, sizeof(tmp_atomname), start_name_in_col13 ? "" : " ");
1096 std::strncat(tmp_atomname, atom_name, 4);
1097 tmp_atomname[5] = '\0';
1101 tmp_atomname[0] = '\0';
1104 /* Format residue name */
1105 std::strncpy(tmp_resname, (res_name != nullptr) ? res_name : "", 4);
1106 /* Make sure the string is terminated if strlen was > 4 */
1107 tmp_resname[4] = '\0';
1108 /* String is properly terminated, so now we can use strcat. By adding a
1109 * space we can write it right-justified, and if the original name was
1110 * three characters or less there will be a space added on the right side.
1112 std::strcat(tmp_resname, " ");
1114 /* Truncate integers so they fit */
1115 atom_seq_number = atom_seq_number % 100000;
1116 res_seq_number = res_seq_number % 10000;
1119 "%-6s%5d %-4.4s%c%4.4s%c%4d%c %8.3f%8.3f%8.3f%6.2f%6.2f %2s\n",
1120 enumValueToString(record),
1133 (element != nullptr) ? element : "");