Merge branch release-2019 into release-2020
[alexxy/gromacs.git] / src / gromacs / fileio / pdbio.cpp
index e475e66a2c440dbbf37f238bea894808d56b0e8d..3466d8d4f1114644918dbe3c9fba228062ccc701 100644 (file)
 #include "gromacs/utility/smalloc.h"
 #include "gromacs/utility/snprintf.h"
 
-typedef struct {
+typedef struct
+{
     int ai, aj;
 } gmx_conection_t;
 
-typedef struct gmx_conect_t {
+typedef struct gmx_conect_t
+{
     int              nconect;
     gmx_bool         bSorted;
-    gmx_conection_t *conect;
+    gmx_conection_tconect;
 } gmx_conect_t;
 
-static const char *pdbtp[epdbNR] = {
-    "ATOM  ", "HETATM", "ANISOU", "CRYST1",
-    "COMPND", "MODEL", "ENDMDL", "TER", "HEADER", "TITLE", "REMARK",
-    "CONECT"
-};
-
+static const char* pdbtp[epdbNR] = { "ATOM  ", "HETATM", "ANISOU", "CRYST1", "COMPND", "MODEL",
+                                     "ENDMDL", "TER",    "HEADER", "TITLE",  "REMARK", "CONECT" };
 
 #define REMARK_SIM_BOX "REMARK    THIS IS A SIMULATION BOX"
 
-static void xlate_atomname_pdb2gmx(char *name)
+static void xlate_atomname_pdb2gmx(charname)
 {
     int  i, length;
     char temp;
@@ -91,9 +89,9 @@ static void xlate_atomname_pdb2gmx(char *name)
         temp = name[0];
         for (i = 1; i < length; i++)
         {
-            name[i-1] = name[i];
+            name[i - 1] = name[i];
         }
-        name[length-1] = temp;
+        name[length - 1] = temp;
     }
 }
 
@@ -101,12 +99,12 @@ static void xlate_atomname_pdb2gmx(char *name)
 static std::string xlate_atomname_gmx2pdb(std::string name)
 {
     size_t length = name.size();
-    if (length > 3 && std::isdigit(name[length-1]))
+    if (length > 3 && std::isdigit(name[length - 1]))
     {
-        char temp = name[length-1];
-        for (size_t i = length-1; i > 0; --i)
+        char temp = name[length - 1];
+        for (size_t i = length - 1; i > 0; --i)
         {
-            name[i] = name[i-1];
+            name[i] = name[i - 1];
         }
         name[0] = temp;
     }
@@ -114,7 +112,7 @@ static std::string xlate_atomname_gmx2pdb(std::string name)
 }
 
 
-void gmx_write_pdb_box(FILE *out, int ePBC, const matrix box)
+void gmx_write_pdb_box(FILEout, int ePBC, const matrix box)
 {
     real alpha, beta, gamma;
 
@@ -128,25 +126,25 @@ void gmx_write_pdb_box(FILE *out, int ePBC, const matrix box)
         return;
     }
 
-    if (norm2(box[YY])*norm2(box[ZZ]) != 0)
+    if (norm2(box[YY]) * norm2(box[ZZ]) != 0)
     {
-        alpha = RAD2DEG*gmx_angle(box[YY], box[ZZ]);
+        alpha = RAD2DEG * gmx_angle(box[YY], box[ZZ]);
     }
     else
     {
         alpha = 90;
     }
-    if (norm2(box[XX])*norm2(box[ZZ]) != 0)
+    if (norm2(box[XX]) * norm2(box[ZZ]) != 0)
     {
-        beta  = RAD2DEG*gmx_angle(box[XX], box[ZZ]);
+        beta = RAD2DEG * gmx_angle(box[XX], box[ZZ]);
     }
     else
     {
-        beta  = 90;
+        beta = 90;
     }
-    if (norm2(box[XX])*norm2(box[YY]) != 0)
+    if (norm2(box[XX]) * norm2(box[YY]) != 0)
     {
-        gamma = RAD2DEG*gmx_angle(box[XX], box[YY]);
+        gamma = RAD2DEG * gmx_angle(box[XX], box[YY]);
     }
     else
     {
@@ -155,24 +153,21 @@ void gmx_write_pdb_box(FILE *out, int ePBC, const matrix box)
     fprintf(out, "REMARK    THIS IS A SIMULATION BOX\n");
     if (ePBC != epbcSCREW)
     {
-        fprintf(out, "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n",
-                10*norm(box[XX]), 10*norm(box[YY]), 10*norm(box[ZZ]),
-                alpha, beta, gamma, "P 1", 1);
+        fprintf(out, "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n", 10 * norm(box[XX]),
+                10 * norm(box[YY]), 10 * norm(box[ZZ]), alpha, beta, gamma, "P 1", 1);
     }
     else
     {
         /* Double the a-vector length and write the correct space group */
-        fprintf(out, "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n",
-                20*norm(box[XX]), 10*norm(box[YY]), 10*norm(box[ZZ]),
-                alpha, beta, gamma, "P 21 1 1", 1);
-
+        fprintf(out, "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-11s%4d\n", 20 * norm(box[XX]),
+                10 * norm(box[YY]), 10 * norm(box[ZZ]), alpha, beta, gamma, "P 21 1 1", 1);
     }
 }
 
-static void read_cryst1(char *line, int *ePBC, matrix box)
+static void read_cryst1(char* line, int* ePBC, matrix box)
 {
 #define SG_SIZE 11
-    char   sa[12], sb[12], sc[12], sg[SG_SIZE+1], ident;
+    char   sa[12], sb[12], sc[12], sg[SG_SIZE + 1], ident;
     double fa, fb, fc, alpha, beta, gamma, cosa, cosb, cosg, sing;
     int    syma, symb, symc;
     int    ePBC_file;
@@ -182,16 +177,16 @@ static void read_cryst1(char *line, int *ePBC, matrix box)
     ePBC_file = -1;
     if (strlen(line) >= 55)
     {
-        strncpy(sg, line+55, SG_SIZE);
+        strncpy(sg, line + 55, SG_SIZE);
         sg[SG_SIZE] = '\0';
         ident       = ' ';
         syma        = 0;
         symb        = 0;
         symc        = 0;
         sscanf(sg, "%c %d %d %d", &ident, &syma, &symb, &symc);
-        if (ident == 'P' && syma ==  1 && symb <= 1 && symc <= 1)
+        if (ident == 'P' && syma == 1 && symb <= 1 && symc <= 1)
         {
-            fc        = strtod(sc, nullptr)*0.1;
+            fc        = strtod(sc, nullptr) * 0.1;
             ePBC_file = (fc > 0 ? epbcXYZ : epbcXY);
         }
         if (ident == 'P' && syma == 21 && symb == 1 && symc == 1)
@@ -206,9 +201,9 @@ static void read_cryst1(char *line, int *ePBC, matrix box)
 
     if (box)
     {
-        fa = strtod(sa, nullptr)*0.1;
-        fb = strtod(sb, nullptr)*0.1;
-        fc = strtod(sc, nullptr)*0.1;
+        fa = strtod(sa, nullptr) * 0.1;
+        fb = strtod(sb, nullptr) * 0.1;
+        fc = strtod(sc, nullptr) * 0.1;
         if (ePBC_file == epbcSCREW)
         {
             fa *= 0.5;
@@ -219,7 +214,7 @@ static void read_cryst1(char *line, int *ePBC, matrix box)
         {
             if (alpha != 90.0)
             {
-                cosa = std::cos(alpha*DEG2RAD);
+                cosa = std::cos(alpha * DEG2RAD);
             }
             else
             {
@@ -227,7 +222,7 @@ static void read_cryst1(char *line, int *ePBC, matrix box)
             }
             if (beta != 90.0)
             {
-                cosb = std::cos(beta*DEG2RAD);
+                cosb = std::cos(beta * DEG2RAD);
             }
             else
             {
@@ -235,20 +230,19 @@ static void read_cryst1(char *line, int *ePBC, matrix box)
             }
             if (gamma != 90.0)
             {
-                cosg = std::cos(gamma*DEG2RAD);
-                sing = std::sin(gamma*DEG2RAD);
+                cosg = std::cos(gamma * DEG2RAD);
+                sing = std::sin(gamma * DEG2RAD);
             }
             else
             {
                 cosg = 0;
                 sing = 1;
             }
-            box[YY][XX] = fb*cosg;
-            box[YY][YY] = fb*sing;
-            box[ZZ][XX] = fc*cosb;
-            box[ZZ][YY] = fc*(cosa - cosb*cosg)/sing;
-            box[ZZ][ZZ] = std::sqrt(fc*fc
-                                    - box[ZZ][XX]*box[ZZ][XX] - box[ZZ][YY]*box[ZZ][YY]);
+            box[YY][XX] = fb * cosg;
+            box[YY][YY] = fb * sing;
+            box[ZZ][XX] = fc * cosb;
+            box[ZZ][YY] = fc * (cosa - cosb * cosg) / sing;
+            box[ZZ][ZZ] = std::sqrt(fc * fc - box[ZZ][XX] * box[ZZ][XX] - box[ZZ][YY] * box[ZZ][YY]);
         }
         else
         {
@@ -258,74 +252,61 @@ static void read_cryst1(char *line, int *ePBC, matrix box)
     }
 }
 
-static int
-gmx_fprintf_pqr_atomline(FILE *            fp,
-                         enum PDB_record   record,
-                         int               atom_seq_number,
-                         const char *      atom_name,
-                         const char *      res_name,
-                         char              chain_id,
-                         int               res_seq_number,
-                         real              x,
-                         real              y,
-                         real              z,
-                         real              occupancy,
-                         real              b_factor)
+static int gmx_fprintf_pqr_atomline(FILE*           fp,
+                                    enum PDB_record record,
+                                    int             atom_seq_number,
+                                    const char*     atom_name,
+                                    const char*     res_name,
+                                    char            chain_id,
+                                    int             res_seq_number,
+                                    real            x,
+                                    real            y,
+                                    real            z,
+                                    real            occupancy,
+                                    real            b_factor)
 {
     GMX_RELEASE_ASSERT(record == epdbATOM || record == epdbHETATM,
                        "Can only print PQR atom lines as ATOM or HETATM records");
 
     /* Check atom name */
-    GMX_RELEASE_ASSERT(atom_name != nullptr,
-                       "Need atom information to print pqr");
+    GMX_RELEASE_ASSERT(atom_name != nullptr, "Need atom information to print pqr");
 
     /* Check residue name */
-    GMX_RELEASE_ASSERT(res_name != nullptr,
-                       "Need residue information to print pqr");
+    GMX_RELEASE_ASSERT(res_name != nullptr, "Need residue information to print pqr");
 
     /* Truncate integers so they fit */
     atom_seq_number = atom_seq_number % 100000;
     res_seq_number  = res_seq_number % 10000;
 
-    int n = fprintf(fp,
-                    "%-6s%5d %-4.4s%4.4s%c%4d %8.3f %8.3f %8.3f %6.2f %6.2f\n",
-                    pdbtp[record],
-                    atom_seq_number,
-                    atom_name,
-                    res_name,
-                    chain_id,
-                    res_seq_number,
-                    x, y, z,
-                    occupancy,
-                    b_factor);
+    int n = fprintf(fp, "%-6s%5d %-4.4s%4.4s%c%4d %8.3f %8.3f %8.3f %6.2f %6.2f\n", pdbtp[record],
+                    atom_seq_number, atom_name, res_name, chain_id, res_seq_number, x, y, z,
+                    occupancy, b_factor);
 
     return n;
 }
 
-void write_pdbfile_indexed(FILE *out, const char *title,
-                           const t_atoms *atoms, const rvec x[],
-                           int ePBC, const matrix box, char chainid,
-                           int model_nr, int nindex, const int index[],
-                           gmx_conect conect, gmx_bool bTerSepChains,
-                           bool usePqrFormat)
+void write_pdbfile_indexed(FILE*          out,
+                           const char*    title,
+                           const t_atoms* atoms,
+                           const rvec     x[],
+                           int            ePBC,
+                           const matrix   box,
+                           char           chainid,
+                           int            model_nr,
+                           int            nindex,
+                           const int      index[],
+                           gmx_conect     conect,
+                           bool           usePqrFormat)
 {
-    gmx_conect_t     *gc = static_cast<gmx_conect_t *>(conect);
-    int               i, ii;
-    int               resind, resnr;
-    enum PDB_record   type;
-    unsigned char     resic, ch;
-    char              altloc;
-    real              occup, bfac;
-    gmx_bool          bOccup;
-    int               chainnum, lastchainnum;
-    gmx_residuetype_t*rt;
-    const char       *p_restype;
-    const char       *p_lastrestype;
-
-    gmx_residuetype_init(&rt);
+    gmx_conect_t*   gc = static_cast<gmx_conect_t*>(conect);
+    enum PDB_record type;
+    char            altloc;
+    real            occup, bfac;
+    gmx_bool        bOccup;
+
 
     fprintf(out, "TITLE     %s\n", (title && title[0]) ? title : gmx::bromacs().c_str());
-    if (box && ( (norm2(box[XX]) != 0.0f) || (norm2(box[YY]) != 0.0f) || (norm2(box[ZZ]) != 0.0f) ) )
+    if (box && ((norm2(box[XX]) != 0.0F) || (norm2(box[YY]) != 0.0F) || (norm2(box[ZZ]) != 0.0F)))
     {
         gmx_write_pdb_box(out, ePBC, box);
     }
@@ -335,9 +316,9 @@ void write_pdbfile_indexed(FILE *out, const char *title,
          * otherwise set them all to one
          */
         bOccup = TRUE;
-        for (ii = 0; (ii < nindex) && bOccup; ii++)
+        for (int ii = 0; (ii < nindex) && bOccup; ii++)
         {
-            i      = index[ii];
+            int i  = index[ii];
             bOccup = bOccup && (atoms->pdbinfo[i].occup == 0.0);
         }
     }
@@ -348,35 +329,19 @@ void write_pdbfile_indexed(FILE *out, const char *title,
 
     fprintf(out, "MODEL %8d\n", model_nr > 0 ? model_nr : 1);
 
-    lastchainnum      = -1;
-    p_restype         = nullptr;
-
-    for (ii = 0; ii < nindex; ii++)
+    ResidueType rt;
+    for (int ii = 0; ii < nindex; ii++)
     {
-        i             = index[ii];
-        resind        = atoms->atom[i].resind;
-        chainnum      = atoms->resinfo[resind].chainnum;
-        p_lastrestype = p_restype;
-        gmx_residuetype_get_type(rt, *atoms->resinfo[resind].name, &p_restype);
-
-        /* Add a TER record if we changed chain, and if either the previous or this chain is protein/DNA/RNA. */
-        if (bTerSepChains && ii > 0 && chainnum != lastchainnum)
-        {
-            /* Only add TER if the previous chain contained protein/DNA/RNA. */
-            if (gmx_residuetype_is_protein(rt, p_lastrestype) || gmx_residuetype_is_dna(rt, p_lastrestype) || gmx_residuetype_is_rna(rt, p_lastrestype))
-            {
-                fprintf(out, "TER\n");
-            }
-            lastchainnum    = chainnum;
-        }
-
-        std::string resnm = *atoms->resinfo[resind].name;
-        std::string nm    = *atoms->atomname[i];
+        int         i      = index[ii];
+        int         resind = atoms->atom[i].resind;
+        std::string resnm  = *atoms->resinfo[resind].name;
+        std::string nm     = *atoms->atomname[i];
 
         /* rename HG12 to 2HG1, etc. */
-        nm    = xlate_atomname_gmx2pdb(nm);
-        resnr = atoms->resinfo[resind].nr;
-        resic = atoms->resinfo[resind].ic;
+        nm                  = xlate_atomname_gmx2pdb(nm);
+        int           resnr = atoms->resinfo[resind].nr;
+        unsigned char resic = atoms->resinfo[resind].ic;
+        unsigned char ch;
         if (chainid != ' ')
         {
             ch = chainid;
@@ -413,42 +378,22 @@ void write_pdbfile_indexed(FILE *out, const char *title,
         bfac  = pdbinfo.bfac;
         if (!usePqrFormat)
         {
-            gmx_fprintf_pdb_atomline(out,
-                                     type,
-                                     i+1,
-                                     nm.c_str(),
-                                     altloc,
-                                     resnm.c_str(),
-                                     ch,
-                                     resnr,
-                                     resic,
-                                     10*x[i][XX], 10*x[i][YY], 10*x[i][ZZ],
-                                     occup,
-                                     bfac,
-                                     atoms->atom[i].elem);
+            gmx_fprintf_pdb_atomline(out, type, i + 1, nm.c_str(), altloc, resnm.c_str(), ch, resnr,
+                                     resic, 10 * x[i][XX], 10 * x[i][YY], 10 * x[i][ZZ], occup,
+                                     bfac, atoms->atom[i].elem);
 
             if (atoms->pdbinfo && atoms->pdbinfo[i].bAnisotropic)
             {
-                fprintf(out, "ANISOU%5d  %-4.4s%4.4s%c%4d%c %7d%7d%7d%7d%7d%7d\n",
-                        (i+1)%100000, nm.c_str(), resnm.c_str(), ch, resnr,
-                        (resic == '\0') ? ' ' : resic,
-                        atoms->pdbinfo[i].uij[0], atoms->pdbinfo[i].uij[1],
-                        atoms->pdbinfo[i].uij[2], atoms->pdbinfo[i].uij[3],
-                        atoms->pdbinfo[i].uij[4], atoms->pdbinfo[i].uij[5]);
+                fprintf(out, "ANISOU%5d  %-4.4s%4.4s%c%4d%c %7d%7d%7d%7d%7d%7d\n", (i + 1) % 100000,
+                        nm.c_str(), resnm.c_str(), ch, resnr, (resic == '\0') ? ' ' : resic,
+                        atoms->pdbinfo[i].uij[0], atoms->pdbinfo[i].uij[1], atoms->pdbinfo[i].uij[2],
+                        atoms->pdbinfo[i].uij[3], atoms->pdbinfo[i].uij[4], atoms->pdbinfo[i].uij[5]);
             }
         }
         else
         {
-            gmx_fprintf_pqr_atomline(out,
-                                     type,
-                                     i+1,
-                                     nm.c_str(),
-                                     resnm.c_str(),
-                                     ch,
-                                     resnr,
-                                     10*x[i][XX], 10*x[i][YY], 10*x[i][ZZ],
-                                     occup,
-                                     bfac);
+            gmx_fprintf_pqr_atomline(out, type, i + 1, nm.c_str(), resnm.c_str(), ch, resnr,
+                                     10 * x[i][XX], 10 * x[i][YY], 10 * x[i][ZZ], occup, bfac);
         }
     }
 
@@ -458,17 +403,22 @@ void write_pdbfile_indexed(FILE *out, const char *title,
     if (nullptr != gc)
     {
         /* Write conect records */
-        for (i = 0; (i < gc->nconect); i++)
+        for (int i = 0; (i < gc->nconect); i++)
         {
-            fprintf(out, "CONECT%5d%5d\n", gc->conect[i].ai+1, gc->conect[i].aj+1);
+            fprintf(out, "CONECT%5d%5d\n", gc->conect[i].ai + 1, gc->conect[i].aj + 1);
         }
     }
-
-    gmx_residuetype_destroy(rt);
 }
 
-void write_pdbfile(FILE *out, const char *title, const t_atoms *atoms, const rvec x[],
-                   int ePBC, const matrix box, char chainid, int model_nr, gmx_conect conect, gmx_bool bTerSepChains)
+void write_pdbfile(FILE*          out,
+                   const char*    title,
+                   const t_atoms* atoms,
+                   const rvec     x[],
+                   int            ePBC,
+                   const matrix   box,
+                   char           chainid,
+                   int            model_nr,
+                   gmx_conect     conect)
 {
     int i, *index;
 
@@ -477,12 +427,12 @@ void write_pdbfile(FILE *out, const char *title, const t_atoms *atoms, const rve
     {
         index[i] = i;
     }
-    write_pdbfile_indexed(out, title, atoms, x, ePBC, box, chainid, model_nr,
-                          atoms->nr, index, conect, bTerSepChains, false);
+    write_pdbfile_indexed(out, title, atoms, x, ePBC, box, chainid, model_nr, atoms->nr, index,
+                          conect, false);
     sfree(index);
 }
 
-static int line2type(const char *line)
+static int line2type(const charline)
 {
     int  k;
     char type[8];
@@ -504,7 +454,7 @@ static int line2type(const char *line)
     return k;
 }
 
-static void read_anisou(char line[], int natom, t_atoms *atoms)
+static void read_anisou(char line[], int natom, t_atomsatoms)
 {
     int  i, j, k, atomnr;
     char nc = '\0';
@@ -530,23 +480,20 @@ static void read_anisou(char line[], int natom, t_atoms *atoms)
 
     /* Search backwards for number and name only */
     atomnr = std::strtol(anr, nullptr, 10);
-    for (i = natom-1; (i >= 0); i--)
+    for (i = natom - 1; (i >= 0); i--)
     {
-        if ((std::strcmp(anm, *(atoms->atomname[i])) == 0) &&
-            (atomnr == atoms->pdbinfo[i].atomnr))
+        if ((std::strcmp(anm, *(atoms->atomname[i])) == 0) && (atomnr == atoms->pdbinfo[i].atomnr))
         {
             break;
         }
     }
     if (i < 0)
     {
-        fprintf(stderr, "Skipping ANISOU record (atom %s %d not found)\n",
-                anm, atomnr);
+        fprintf(stderr, "Skipping ANISOU record (atom %s %d not found)\n", anm, atomnr);
     }
     else
     {
-        if (sscanf(line+29, "%d%d%d%d%d%d",
-                   &atoms->pdbinfo[i].uij[U11], &atoms->pdbinfo[i].uij[U22],
+        if (sscanf(line + 29, "%d%d%d%d%d%d", &atoms->pdbinfo[i].uij[U11], &atoms->pdbinfo[i].uij[U22],
                    &atoms->pdbinfo[i].uij[U33], &atoms->pdbinfo[i].uij[U12],
                    &atoms->pdbinfo[i].uij[U13], &atoms->pdbinfo[i].uij[U23])
             == 6)
@@ -561,11 +508,11 @@ static void read_anisou(char line[], int natom, t_atoms *atoms)
     }
 }
 
-void get_pdb_atomnumber(const t_atoms *atoms, gmx_atomprop_t aps)
+void get_pdb_atomnumber(const t_atoms* atoms, AtomProperties* aps)
 {
     int    i, atomnumber, len;
     size_t k;
-    char   anm[6], anm_copy[6], *ptr;
+    char   anm[6], anm_copy[6];
     char   nc = '\0';
     real   eval;
 
@@ -578,11 +525,11 @@ void get_pdb_atomnumber(const t_atoms *atoms, gmx_atomprop_t aps)
         std::strcpy(anm, atoms->pdbinfo[i].atomnm);
         std::strcpy(anm_copy, atoms->pdbinfo[i].atomnm);
         bool atomNumberSet = false;
-        len        = strlen(anm);
+        len                = strlen(anm);
         if ((anm[0] != ' ') && ((len <= 2) || !std::isdigit(anm[2])))
         {
             anm_copy[2] = nc;
-            if (gmx_atomprop_query(aps, epropElement, "???", anm_copy, &eval))
+            if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
             {
                 atomnumber    = gmx::roundToInt(eval);
                 atomNumberSet = true;
@@ -590,7 +537,7 @@ void get_pdb_atomnumber(const t_atoms *atoms, gmx_atomprop_t aps)
             else
             {
                 anm_copy[1] = nc;
-                if (gmx_atomprop_query(aps, epropElement, "???", anm_copy, &eval))
+                if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
                 {
                     atomnumber    = gmx::roundToInt(eval);
                     atomNumberSet = true;
@@ -606,35 +553,31 @@ void get_pdb_atomnumber(const t_atoms *atoms, gmx_atomprop_t aps)
             }
             anm_copy[0] = anm[k];
             anm_copy[1] = nc;
-            if (gmx_atomprop_query(aps, epropElement, "???", anm_copy, &eval))
+            if (aps->setAtomProperty(epropElement, "???", anm_copy, &eval))
             {
                 atomnumber    = gmx::roundToInt(eval);
                 atomNumberSet = true;
             }
         }
+        std::string buf;
         if (atomNumberSet)
         {
             atoms->atom[i].atomnumber = atomnumber;
-            ptr = gmx_atomprop_element(aps, atomnumber);
+            buf                       = aps->elementFromAtomNumber(atomnumber);
             if (debug)
             {
-                fprintf(debug, "Atomnumber for atom '%s' is %d\n",
-                        anm, atomnumber);
+                fprintf(debug, "Atomnumber for atom '%s' is %d\n", anm, atomnumber);
             }
         }
-        else
-        {
-            ptr = nullptr;
-        }
-        std::strncpy(atoms->atom[i].elem, ptr == nullptr ? "" : ptr, 4);
+        buf.resize(3);
+        std::strncpy(atoms->atom[i].elem, buf.c_str(), 4);
     }
 }
 
-static int read_atom(t_symtab *symtab,
-                     const char line[], int type, int natom,
-                     t_atoms *atoms, rvec x[], int chainnum, gmx_bool bChange)
+static int
+read_atom(t_symtab* symtab, const char line[], int type, int natom, t_atoms* atoms, rvec x[], int chainnum, gmx_bool bChange)
 {
-    t_atom       *atomn;
+    t_atom*       atomn;
     int           j, k;
     char          nc = '\0';
     char          anr[12], anm[12], anm_copy[12], altloc, resnm[12], rnr[12], elem[3];
@@ -645,8 +588,7 @@ static int read_atom(t_symtab *symtab,
 
     if (natom >= atoms->nr)
     {
-        gmx_fatal(FARGS, "\nFound more atoms (%d) in pdb file than expected (%d)",
-                  natom+1, atoms->nr);
+        gmx_fatal(FARGS, "\nFound more atoms (%d) in pdb file than expected (%d)", natom + 1, atoms->nr);
     }
 
     /* Skip over type */
@@ -687,7 +629,7 @@ static int read_atom(t_symtab *symtab,
     trim(rnr);
     resnr = std::strtol(rnr, nullptr, 10);
     resic = line[j];
-    j    += 4;
+    j += 4;
 
     /* X,Y,Z Coordinate */
     for (k = 0; (k < 8); k++, j++)
@@ -734,10 +676,9 @@ static int read_atom(t_symtab *symtab,
     if (atoms->atom)
     {
         atomn = &(atoms->atom[natom]);
-        if ((natom == 0) ||
-            atoms->resinfo[atoms->atom[natom-1].resind].nr != resnr ||
-            atoms->resinfo[atoms->atom[natom-1].resind].ic != resic ||
-            (strcmp(*atoms->resinfo[atoms->atom[natom-1].resind].name, resnm) != 0))
+        if ((natom == 0) || atoms->resinfo[atoms->atom[natom - 1].resind].nr != resnr
+            || atoms->resinfo[atoms->atom[natom - 1].resind].ic != resic
+            || (strcmp(*atoms->resinfo[atoms->atom[natom - 1].resind].name, resnm) != 0))
         {
             if (natom == 0)
             {
@@ -745,14 +686,14 @@ static int read_atom(t_symtab *symtab,
             }
             else
             {
-                atomn->resind = atoms->atom[natom-1].resind + 1;
+                atomn->resind = atoms->atom[natom - 1].resind + 1;
             }
             atoms->nres = atomn->resind + 1;
             t_atoms_set_resinfo(atoms, natom, symtab, resnm, resnr, resic, chainnum, chainid);
         }
         else
         {
-            atomn->resind = atoms->atom[natom-1].resind;
+            atomn->resind = atoms->atom[natom - 1].resind;
         }
         if (bChange)
         {
@@ -764,9 +705,9 @@ static int read_atom(t_symtab *symtab,
         atomn->atomnumber      = atomnumber;
         strncpy(atomn->elem, elem, 4);
     }
-    x[natom][XX] = strtod(xc, nullptr)*0.1;
-    x[natom][YY] = strtod(yc, nullptr)*0.1;
-    x[natom][ZZ] = strtod(zc, nullptr)*0.1;
+    x[natom][XX] = strtod(xc, nullptr) * 0.1;
+    x[natom][YY] = strtod(yc, nullptr) * 0.1;
+    x[natom][ZZ] = strtod(zc, nullptr) * 0.1;
     if (atoms->pdbinfo)
     {
         atoms->pdbinfo[natom].type   = type;
@@ -781,7 +722,7 @@ static int read_atom(t_symtab *symtab,
     return natom;
 }
 
-gmx_bool is_hydrogen(const char *nm)
+gmx_bool is_hydrogen(const charnm)
 {
     char buf[30];
 
@@ -799,19 +740,19 @@ gmx_bool is_hydrogen(const char *nm)
     return FALSE;
 }
 
-gmx_bool is_dummymass(const char *nm)
+gmx_bool is_dummymass(const charnm)
 {
     char buf[30];
 
     std::strcpy(buf, nm);
     trim(buf);
 
-    return (buf[0] == 'M') && (std::isdigit(buf[strlen(buf)-1]) != 0);
+    return (buf[0] == 'M') && (std::isdigit(buf[strlen(buf) - 1]) != 0);
 }
 
-static void gmx_conect_addline(gmx_conect_t *con, char *line)
+static void gmx_conect_addline(gmx_conect_t* con, char* line)
 {
-    int         n, ai, aj;
+    int n, ai, aj;
 
     std::string form2  = "%%*s";
     std::string format = form2 + "%%d";
@@ -826,26 +767,24 @@ static void gmx_conect_addline(gmx_conect_t *con, char *line)
             {
                 gmx_conect_add(con, ai - 1, aj - 1); /* to prevent duplicated records */
             }
-        }
-        while (n == 1);
+        } while (n == 1);
     }
 }
 
-void gmx_conect_dump(FILE *fp, gmx_conect conect)
+void gmx_conect_dump(FILEfp, gmx_conect conect)
 {
-    gmx_conect_t *gc = static_cast<gmx_conect_t *>(conect);
+    gmx_conect_t* gc = static_cast<gmx_conect_t*>(conect);
     int           i;
 
     for (i = 0; (i < gc->nconect); i++)
     {
-        fprintf(fp, "%6s%5d%5d\n", "CONECT",
-                gc->conect[i].ai+1, gc->conect[i].aj+1);
+        fprintf(fp, "%6s%5d%5d\n", "CONECT", gc->conect[i].ai + 1, gc->conect[i].aj + 1);
     }
 }
 
 gmx_conect gmx_conect_init()
 {
-    gmx_conect_t *gc;
+    gmx_conect_tgc;
 
     snew(gc, 1);
 
@@ -854,14 +793,14 @@ gmx_conect gmx_conect_init()
 
 void gmx_conect_done(gmx_conect conect)
 {
-    gmx_conect_t *gc = conect;
+    gmx_conect_tgc = conect;
 
     sfree(gc->conect);
 }
 
 gmx_bool gmx_conect_exist(gmx_conect conect, int ai, int aj)
 {
-    gmx_conect_t *gc = conect;
+    gmx_conect_tgc = conect;
     int           i;
 
     /* if (!gc->bSorted)
@@ -869,10 +808,8 @@ gmx_bool gmx_conect_exist(gmx_conect conect, int ai, int aj)
 
     for (i = 0; (i < gc->nconect); i++)
     {
-        if (((gc->conect[i].ai == ai) &&
-             (gc->conect[i].aj == aj)) ||
-            ((gc->conect[i].aj == ai) &&
-             (gc->conect[i].ai == aj)))
+        if (((gc->conect[i].ai == ai) && (gc->conect[i].aj == aj))
+            || ((gc->conect[i].aj == ai) && (gc->conect[i].ai == aj)))
         {
             return TRUE;
         }
@@ -882,7 +819,7 @@ gmx_bool gmx_conect_exist(gmx_conect conect, int ai, int aj)
 
 void gmx_conect_add(gmx_conect conect, int ai, int aj)
 {
-    gmx_conect_t *gc = static_cast<gmx_conect_t *>(conect);
+    gmx_conect_t* gc = static_cast<gmx_conect_t*>(conect);
 
     /* if (!gc->bSorted)
        sort_conect(gc);*/
@@ -890,23 +827,30 @@ void gmx_conect_add(gmx_conect conect, int ai, int aj)
     if (!gmx_conect_exist(conect, ai, aj))
     {
         srenew(gc->conect, ++gc->nconect);
-        gc->conect[gc->nconect-1].ai = ai;
-        gc->conect[gc->nconect-1].aj = aj;
+        gc->conect[gc->nconect - 1].ai = ai;
+        gc->conect[gc->nconect - 1].aj = aj;
     }
 }
 
-int read_pdbfile(FILE *in, char *title, int *model_nr,
-                 t_atoms *atoms, t_symtab *symtab, rvec x[], int *ePBC,
-                 matrix box, gmx_bool bChange, gmx_conect conect)
+int read_pdbfile(FILE*      in,
+                 char*      title,
+                 int*       model_nr,
+                 t_atoms*   atoms,
+                 t_symtab*  symtab,
+                 rvec       x[],
+                 int*       ePBC,
+                 matrix     box,
+                 gmx_bool   bChange,
+                 gmx_conect conect)
 {
-    gmx_conect_t *gc = conect;
+    gmx_conect_tgc = conect;
     gmx_bool      bCOMPND;
     gmx_bool      bConnWarn = FALSE;
-    char          line[STRLEN+1];
+    char          line[STRLEN + 1];
     int           line_type;
-    char         *c, *d;
+    char *        c, *d;
     int           natom, chainnum;
-    gmx_bool      bStop   = FALSE;
+    gmx_bool      bStop = FALSE;
 
     if (ePBC)
     {
@@ -946,15 +890,13 @@ int read_pdbfile(FILE *in, char *title, int *model_nr,
                 }
                 break;
 
-            case epdbCRYST1:
-                read_cryst1(line, ePBC, box);
-                break;
+            case epdbCRYST1: read_cryst1(line, ePBC, box); break;
 
             case epdbTITLE:
             case epdbHEADER:
                 if (std::strlen(line) > 6)
                 {
-                    c = line+6;
+                    c = line + 6;
                     /* skip HEADER or TITLE and spaces */
                     while (c[0] != ' ')
                     {
@@ -978,9 +920,9 @@ int read_pdbfile(FILE *in, char *title, int *model_nr,
                 break;
 
             case epdbCOMPND:
-                if ((!std::strstr(line, ": ")) || (std::strstr(line+6, "MOLECULE:")))
+                if ((!std::strstr(line, ": ")) || (std::strstr(line + 6, "MOLECULE:")))
                 {
-                    if (!(c = std::strstr(line+6, "MOLECULE:")) )
+                    if (!(c = std::strstr(line + 6, "MOLECULE:")))
                     {
                         c = line;
                     }
@@ -997,7 +939,7 @@ int read_pdbfile(FILE *in, char *title, int *model_nr,
                     d = strstr(c, "   ");
                     if (d)
                     {
-                        while ( (d[-1] == ';') && d > c)
+                        while ((d[-1] == ';') && d > c)
                         {
                             d--;
                         }
@@ -1019,9 +961,7 @@ int read_pdbfile(FILE *in, char *title, int *model_nr,
                 }
                 break;
 
-            case epdbTER:
-                chainnum++;
-                break;
+            case epdbTER: chainnum++; break;
 
             case epdbMODEL:
                 if (model_nr)
@@ -1030,9 +970,7 @@ int read_pdbfile(FILE *in, char *title, int *model_nr,
                 }
                 break;
 
-            case epdbENDMDL:
-                bStop = TRUE;
-                break;
+            case epdbENDMDL: bStop = TRUE; break;
             case epdbCONECT:
                 if (gc)
                 {
@@ -1045,15 +983,14 @@ int read_pdbfile(FILE *in, char *title, int *model_nr,
                 }
                 break;
 
-            default:
-                break;
+            default: break;
         }
     }
 
     return natom;
 }
 
-void get_pdb_coordnum(FILE *in, int *natoms)
+void get_pdb_coordnum(FILE* in, int* natoms)
 {
     char line[STRLEN];
 
@@ -1071,11 +1008,9 @@ void get_pdb_coordnum(FILE *in, int *natoms)
     }
 }
 
-void gmx_pdb_read_conf(const char *infile,
-                       t_symtab *symtab, char **name, t_atoms *atoms,
-                       rvec x[], int *ePBC, matrix box)
+void gmx_pdb_read_conf(const char* infile, t_symtab* symtab, char** name, t_atoms* atoms, rvec x[], int* ePBC, matrix box)
 {
-    FILE *in = gmx_fio_fopen(infile, "r");
+    FILEin = gmx_fio_fopen(infile, "r");
     char  title[STRLEN];
     read_pdbfile(in, title, nullptr, atoms, symtab, x, ePBC, box, TRUE, nullptr);
     if (name != nullptr)
@@ -1085,44 +1020,42 @@ void gmx_pdb_read_conf(const char *infile,
     gmx_fio_fclose(in);
 }
 
-gmx_conect gmx_conect_generate(const t_topology *top)
+gmx_conect gmx_conect_generate(const t_topologytop)
 {
     int        f, i;
     gmx_conect gc;
 
     /* Fill the conect records */
-    gc  = gmx_conect_init();
+    gc = gmx_conect_init();
 
     for (f = 0; (f < F_NRE); f++)
     {
         if (IS_CHEMBOND(f))
         {
-            for (i = 0; (i < top->idef.il[f].nr); i += interaction_function[f].nratoms+1)
+            for (i = 0; (i < top->idef.il[f].nr); i += interaction_function[f].nratoms + 1)
             {
-                gmx_conect_add(gc, top->idef.il[f].iatoms[i+1],
-                               top->idef.il[f].iatoms[i+2]);
+                gmx_conect_add(gc, top->idef.il[f].iatoms[i + 1], top->idef.il[f].iatoms[i + 2]);
             }
         }
     }
     return gc;
 }
 
-int
-gmx_fprintf_pdb_atomline(FILE *            fp,
-                         enum PDB_record   record,
-                         int               atom_seq_number,
-                         const char *      atom_name,
-                         char              alternate_location,
-                         const char *      res_name,
-                         char              chain_id,
-                         int               res_seq_number,
-                         char              res_insertion_code,
-                         real              x,
-                         real              y,
-                         real              z,
-                         real              occupancy,
-                         real              b_factor,
-                         const char *      element)
+int gmx_fprintf_pdb_atomline(FILE*           fp,
+                             enum PDB_record record,
+                             int             atom_seq_number,
+                             const char*     atom_name,
+                             char            alternate_location,
+                             const char*     res_name,
+                             char            chain_id,
+                             int             res_seq_number,
+                             char            res_insertion_code,
+                             real            x,
+                             real            y,
+                             real            z,
+                             real            occupancy,
+                             real            b_factor,
+                             const char*     element)
 {
     char     tmp_atomname[6], tmp_resname[6];
     gmx_bool start_name_in_col13;
@@ -1139,7 +1072,8 @@ gmx_fprintf_pdb_atomline(FILE *            fp,
         /* If the atom name is an element name with two chars, it should start already in column 13.
          * Otherwise it should start in column 14, unless the name length is 4 chars.
          */
-        if ( (element != nullptr) && (std::strlen(element) >= 2) && (gmx_strncasecmp(atom_name, element, 2) == 0) )
+        if ((element != nullptr) && (std::strlen(element) >= 2)
+            && (gmx_strncasecmp(atom_name, element, 2) == 0))
         {
             start_name_in_col13 = TRUE;
         }
@@ -1170,19 +1104,9 @@ gmx_fprintf_pdb_atomline(FILE *            fp,
     atom_seq_number = atom_seq_number % 100000;
     res_seq_number  = res_seq_number % 10000;
 
-    n = fprintf(fp,
-                "%-6s%5d %-4.4s%c%4.4s%c%4d%c   %8.3f%8.3f%8.3f%6.2f%6.2f          %2s\n",
-                pdbtp[record],
-                atom_seq_number,
-                tmp_atomname,
-                alternate_location,
-                tmp_resname,
-                chain_id,
-                res_seq_number,
-                res_insertion_code,
-                x, y, z,
-                occupancy,
-                b_factor,
+    n = fprintf(fp, "%-6s%5d %-4.4s%c%4.4s%c%4d%c   %8.3f%8.3f%8.3f%6.2f%6.2f          %2s\n",
+                pdbtp[record], atom_seq_number, tmp_atomname, alternate_location, tmp_resname,
+                chain_id, res_seq_number, res_insertion_code, x, y, z, occupancy, b_factor,
                 (element != nullptr) ? element : "");
 
     return n;