Manually sort some includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_trjconv.c
index 88dfd0b524b044c6e1bb044fb67f7b9ec5f8a039..f0aa6b5d808eac396d8d787f63fc9cb64717fcf8 100644 (file)
@@ -1,80 +1,84 @@
 /*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *                This source code is part of
- *
- *                 G   R   O   M   A   C   S
- *
- *          GROningen MAchine for Chemical Simulations
- *
- *                        VERSION 3.2.0
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2004, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
+ * Copyright (c) 2001-2004, The GROMACS development team.
+ * Copyright (c) 2013,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
+ *
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
  * of the License, or (at your option) any later version.
  *
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
  *
- * For more info, check our website at http://www.gromacs.org
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
  *
- * And Hey:
- * Green Red Orange Magenta Azure Cyan Skyblue
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gmxpre.h"
 
-#include <string.h>
 #include <math.h>
-#include "macros.h"
-#include "sysstuff.h"
-#include "smalloc.h"
-#include "typedefs.h"
-#include "copyrite.h"
-#include "gmxfio.h"
-#include "tpxio.h"
-#include "trnio.h"
-#include "statutil.h"
-#include "futil.h"
-#include "pdbio.h"
-#include "confio.h"
-#include "names.h"
-#include "index.h"
-#include "vec.h"
-#include "xtcio.h"
-#include "do_fit.h"
-#include "rmpbc.h"
-#include "wgms.h"
-#include "pbc.h"
-#include "viewit.h"
-#include "xvgr.h"
-#include "gmx_ana.h"
-#include "gmx_sort.h"
+#include <stdlib.h>
+#include <string.h>
+
+#include "config.h"
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 
+#include "gromacs/commandline/pargs.h"
+#include "gromacs/fileio/confio.h"
+#include "gromacs/fileio/gmxfio.h"
+#include "gromacs/fileio/pdbio.h"
+#include "gromacs/fileio/tngio_for_tools.h"
+#include "gromacs/fileio/tpxio.h"
+#include "gromacs/fileio/trnio.h"
+#include "gromacs/fileio/trxio.h"
+#include "gromacs/fileio/xtcio.h"
+#include "gromacs/fileio/xvgr.h"
+#include "gromacs/gmxana/gmx_ana.h"
+#include "gromacs/legacyheaders/copyrite.h"
+#include "gromacs/legacyheaders/macros.h"
+#include "gromacs/legacyheaders/names.h"
+#include "gromacs/legacyheaders/typedefs.h"
+#include "gromacs/legacyheaders/viewit.h"
+#include "gromacs/math/do_fit.h"
+#include "gromacs/math/vec.h"
+#include "gromacs/pbcutil/pbc.h"
+#include "gromacs/pbcutil/rmpbc.h"
+#include "gromacs/topology/index.h"
+#include "gromacs/topology/topology.h"
+#include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/futil.h"
+#include "gromacs/utility/smalloc.h"
+
 enum {
     euSel, euRect, euTric, euCompact, euNR
 };
 
 
 static void calc_pbc_cluster(int ecenter, int nrefat, t_topology *top, int ePBC,
-                             rvec x[], atom_id index[],
-                             rvec clust_com, matrix box, rvec clustercenter)
+                             rvec x[], atom_id index[], matrix box)
 {
     int       m, i, j, j0, j1, jj, ai, aj;
     int       imin, jmin;
@@ -316,8 +320,9 @@ static void put_molecule_com_in_box(int unitcell_enum, int ecenter,
         {
             if (debug)
             {
-                fprintf (debug, "\nShifting position of molecule %d "
-                         "by %8.3f  %8.3f  %8.3f\n", i+1, PR_VEC(shift));
+                fprintf(debug, "\nShifting position of molecule %d "
+                        "by %8.3f  %8.3f  %8.3f\n", i+1,
+                        shift[XX], shift[YY], shift[ZZ]);
             }
             for (j = mols->index[i]; (j < mols->index[i+1] && j < natoms); j++)
             {
@@ -371,9 +376,9 @@ static void put_residue_com_in_box(int unitcell_enum, int ecenter,
             {
                 if (debug)
                 {
-                    fprintf (debug, "\nShifting position of residue %d (atoms %u-%u) "
-                             "by %g,%g,%g\n", atom[res_start].resind+1,
-                             res_start+1, res_end+1, PR_VEC(shift));
+                    fprintf(debug, "\nShifting position of residue %d (atoms %u-%u) "
+                            "by %g,%g,%g\n", atom[res_start].resind+1,
+                            res_start+1, res_end+1, shift[XX], shift[YY], shift[ZZ]);
                 }
                 for (j = res_start; j < res_end; j++)
                 {
@@ -464,7 +469,7 @@ static void mk_filenm(char *base, const char *ext, int ndigit, int file_nr,
 
 void check_trn(const char *fn)
 {
-    if ((fn2ftp(fn) != efTRJ)  && (fn2ftp(fn) != efTRR))
+    if (fn2ftp(fn) != efTRR)
     {
         gmx_fatal(FARGS, "%s is not a trajectory file, exiting\n", fn);
     }
@@ -487,7 +492,7 @@ void do_trunc(const char *fn, real t0)
         gmx_fatal(FARGS, "You forgot to set the truncation time");
     }
 
-    /* Check whether this is a .trj file */
+    /* Check whether this is a .trr file */
     check_trn(fn);
 
     in   = open_trn(fn, "r");
@@ -545,49 +550,83 @@ void do_trunc(const char *fn, real t0)
 }
 #endif
 
+/*! \brief Read a full molecular topology if useful and available.
+ *
+ * If the input trajectory file is not in TNG format, and the output
+ * file is in TNG format, then we want to try to read a full topology
+ * (if available), so that we can write molecule information to the
+ * output file. The full topology provides better molecule information
+ * than is available from the normal t_topology data used by GROMACS
+ * tools.
+ *
+ * Also, the t_topology is only read under (different) particular
+ * conditions. If both apply, then a .tpr file might be read
+ * twice. Trying to fix this redundancy while trjconv is still an
+ * all-purpose tool does not seem worthwhile.
+ *
+ * Because of the way gmx_prepare_tng_writing is implemented, the case
+ * where the input TNG file has no molecule information will never
+ * lead to an output TNG file having molecule information. Since
+ * molecule information will generally be present if the input TNG
+ * file was written by a GROMACS tool, this seems like reasonable
+ * behaviour. */
+static gmx_mtop_t *read_mtop_for_tng(const char *tps_file,
+                                     const char *input_file,
+                                     const char *output_file)
+{
+    gmx_mtop_t *mtop = NULL;
+
+    if (fn2bTPX(tps_file) &&
+        efTNG != fn2ftp(input_file) &&
+        efTNG == fn2ftp(output_file))
+    {
+        int temp_natoms = -1;
+        snew(mtop, 1);
+        read_tpx(tps_file, NULL, NULL, &temp_natoms,
+                 NULL, NULL, NULL, mtop);
+    }
+
+    return mtop;
+}
+
 int gmx_trjconv(int argc, char *argv[])
 {
     const char *desc[] = {
-        "[TT]trjconv[tt] can convert trajectory files in many ways:[BR]",
-        "[BB]1.[bb] from one format to another[BR]",
-        "[BB]2.[bb] select a subset of atoms[BR]",
-        "[BB]3.[bb] change the periodicity representation[BR]",
-        "[BB]4.[bb] keep multimeric molecules together[BR]",
-        "[BB]5.[bb] center atoms in the box[BR]",
-        "[BB]6.[bb] fit atoms to reference structure[BR]",
-        "[BB]7.[bb] reduce the number of frames[BR]",
-        "[BB]8.[bb] change the timestamps of the frames ",
+        "[THISMODULE] can convert trajectory files in many ways:[BR]",
+        "* from one format to another[BR]",
+        "* select a subset of atoms[BR]",
+        "* change the periodicity representation[BR]",
+        "* keep multimeric molecules together[BR]",
+        "* center atoms in the box[BR]",
+        "* fit atoms to reference structure[BR]",
+        "* reduce the number of frames[BR]",
+        "* change the timestamps of the frames ",
         "([TT]-t0[tt] and [TT]-timestep[tt])[BR]",
-        "[BB]9.[bb] cut the trajectory in small subtrajectories according",
+        "* cut the trajectory in small subtrajectories according",
         "to information in an index file. This allows subsequent analysis of",
         "the subtrajectories that could, for example, be the result of a",
         "cluster analysis. Use option [TT]-sub[tt].",
         "This assumes that the entries in the index file are frame numbers and",
         "dumps each group in the index file to a separate trajectory file.[BR]",
-        "[BB]10.[bb] select frames within a certain range of a quantity given",
+        "* select frames within a certain range of a quantity given",
         "in an [TT].xvg[tt] file.[PAR]",
 
-        "The program [TT]trjcat[tt] is better suited for concatenating multiple trajectory files.",
+        "[gmx-trjcat] is better suited for concatenating multiple trajectory files.",
         "[PAR]",
 
-        "Currently seven formats are supported for input and output:",
-        "[TT].xtc[tt], [TT].trr[tt], [TT].trj[tt], [TT].gro[tt], [TT].g96[tt],",
-        "[TT].pdb[tt] and [TT].g87[tt].",
+        "The following formats are supported for input and output:",
+        "[TT].xtc[tt], [TT].trr[tt], [TT].gro[tt], [TT].g96[tt]",
+        "and [TT].pdb[tt].",
         "The file formats are detected from the file extension.",
         "The precision of [TT].xtc[tt] and [TT].gro[tt] output is taken from the",
         "input file for [TT].xtc[tt], [TT].gro[tt] and [TT].pdb[tt],",
         "and from the [TT]-ndec[tt] option for other input formats. The precision",
         "is always taken from [TT]-ndec[tt], when this option is set.",
-        "All other formats have fixed precision. [TT].trr[tt] and [TT].trj[tt]",
+        "All other formats have fixed precision. [TT].trr[tt]",
         "output can be single or double precision, depending on the precision",
-        "of the [TT]trjconv[tt] binary.",
+        "of the [THISMODULE] binary.",
         "Note that velocities are only supported in",
-        "[TT].trr[tt], [TT].trj[tt], [TT].gro[tt] and [TT].g96[tt] files.[PAR]",
-
-        "Option [TT]-app[tt] can be used to",
-        "append output to an existing trajectory file.",
-        "No checks are performed to ensure integrity",
-        "of the resulting combined trajectory file.[PAR]",
+        "[TT].trr[tt], [TT].gro[tt] and [TT].g96[tt] files.[PAR]",
 
         "Option [TT]-sep[tt] can be used to write every frame to a separate",
         "[TT].gro, .g96[tt] or [TT].pdb[tt] file. By default, all frames all written to one file.",
@@ -658,20 +697,24 @@ int gmx_trjconv(int argc, char *argv[])
         "Use option [TT]-pbc mol[tt] in addition to [TT]-center[tt] when you",
         "want all molecules in the box after the centering.[PAR]",
 
+        "Option [TT]-box[tt] sets the size of the new box. If you want to"
+        "modify only some of the dimensions, e.g. when reading from a trajectory,"
+        "you can use -1 for those dimensions that should stay the same"
+
         "It is not always possible to use combinations of [TT]-pbc[tt],",
         "[TT]-fit[tt], [TT]-ur[tt] and [TT]-center[tt] to do exactly what",
-        "you want in one call to [TT]trjconv[tt]. Consider using multiple",
+        "you want in one call to [THISMODULE]. Consider using multiple",
         "calls, and check out the GROMACS website for suggestions.[PAR]",
 
         "With [TT]-dt[tt], it is possible to reduce the number of ",
         "frames in the output. This option relies on the accuracy of the times",
         "in your input trajectory, so if these are inaccurate use the",
         "[TT]-timestep[tt] option to modify the time (this can be done",
-        "simultaneously). For making smooth movies, the program [TT]g_filter[tt]",
+        "simultaneously). For making smooth movies, the program [gmx-filter]",
         "can reduce the number of frames while using low-pass frequency",
         "filtering, this reduces aliasing of high frequency motions.[PAR]",
 
-        "Using [TT]-trunc[tt] [TT]trjconv[tt] can truncate [TT].trj[tt] in place, i.e.",
+        "Using [TT]-trunc[tt] [THISMODULE] can truncate [TT].trr[tt] in place, i.e.",
         "without copying the file. This is useful when a run has crashed",
         "during disk I/O (i.e. full disk), or when two contiguous",
         "trajectories must be concatenated without having double frames.[PAR]",
@@ -727,7 +770,7 @@ int gmx_trjconv(int argc, char *argv[])
         "progressive", NULL
     };
 
-    static gmx_bool  bAppend       = FALSE, bSeparate = FALSE, bVels = TRUE, bForce = FALSE, bCONECT = FALSE;
+    static gmx_bool  bSeparate     = FALSE, bVels = TRUE, bForce = FALSE, bCONECT = FALSE;
     static gmx_bool  bCenter       = FALSE;
     static int       skip_nr       = 1, ndec = 3, nzero = 0;
     static real      tzero         = 0, delta_t = 0, timestep = 0, ttrunc = -1, tdump = -1, split_t = 0;
@@ -735,7 +778,6 @@ int gmx_trjconv(int argc, char *argv[])
     static char     *exec_command  = NULL;
     static real      dropunder     = 0, dropover = 0;
     static gmx_bool  bRound        = FALSE;
-    static rvec      clustercenter = {0, 0, 0};
 
     t_pargs
         pa[] =
@@ -767,9 +809,6 @@ int gmx_trjconv(int argc, char *argv[])
         { "-box", FALSE, etRVEC,
           { newbox },
           "Size for new cubic box (default: read from input)" },
-        { "-clustercenter", FALSE, etRVEC,
-          { clustercenter },
-          "Optional starting point for pbc cluster option" },
         { "-trans", FALSE, etRVEC,
           { trans },
           "All coordinates will be translated by trans. This "
@@ -798,8 +837,6 @@ int gmx_trjconv(int argc, char *argv[])
           { &exec_command },
           "Execute command for every output frame with the "
           "frame number as argument" },
-        { "-app", FALSE, etBOOL,
-          { &bAppend }, "Append output" },
         { "-split", FALSE, etTIME,
           { &split_t },
           "Start writing new file when t MOD split = first "
@@ -826,7 +863,7 @@ int gmx_trjconv(int argc, char *argv[])
 
     FILE            *out    = NULL;
     t_trxstatus     *trxout = NULL;
-    t_trxstatus     *status;
+    t_trxstatus     *trxin;
     int              ftp, ftpin = 0, file_nr;
     t_trxframe       fr, frout;
     int              flags;
@@ -836,6 +873,7 @@ int gmx_trjconv(int argc, char *argv[])
     int              m, i, d, frame, outframe, natoms, nout, ncent, nre, newstep = 0, model_nr;
 #define SKIP 10
     t_topology       top;
+    gmx_mtop_t      *mtop  = NULL;
     gmx_conect       gc    = NULL;
     int              ePBC  = -1;
     t_atoms         *atoms = NULL, useatoms;
@@ -885,11 +923,14 @@ int gmx_trjconv(int argc, char *argv[])
     };
 #define NFILE asize(fnm)
 
-    parse_common_args(&argc, argv,
-                      PCA_CAN_BEGIN | PCA_CAN_END | PCA_CAN_VIEW |
-                      PCA_TIME_UNIT | PCA_BE_NICE,
-                      NFILE, fnm, NPA, pa, asize(desc), desc,
-                      0, NULL, &oenv);
+    if (!parse_common_args(&argc, argv,
+                           PCA_CAN_BEGIN | PCA_CAN_END | PCA_CAN_VIEW |
+                           PCA_TIME_UNIT,
+                           NFILE, fnm, NPA, pa, asize(desc), desc,
+                           0, NULL, &oenv))
+    {
+        return 0;
+    }
 
     top_file = ftp2fn(efTPS, NFILE, fnm);
     init_top(&top);
@@ -991,9 +1032,10 @@ int gmx_trjconv(int argc, char *argv[])
         {
             /* check if velocities are possible in input and output files */
             ftpin = fn2ftp(in_file);
-            bVels = (ftp == efTRR || ftp == efTRJ || ftp == efGRO || ftp == efG96)
-                && (ftpin == efTRR || ftpin == efTRJ || ftpin == efGRO || ftpin == efG96 ||
-                    ftpin == efCPT);
+            bVels = (ftp == efTRR || ftp == efGRO ||
+                     ftp == efG96 || ftp == efTNG)
+                && (ftpin == efTRR || ftpin == efGRO ||
+                    ftpin == efG96 || ftpin == efTNG || ftpin == efCPT);
         }
         if (bSeparate || bSplit)
         {
@@ -1002,7 +1044,7 @@ int gmx_trjconv(int argc, char *argv[])
             {
                 gmx_fatal(FARGS, "Output file name '%s' does not contain a '.'", out_file);
             }
-            outf_base = strdup(out_file);
+            outf_base = gmx_strdup(out_file);
             outf_base[outf_ext - out_file] = '\0';
         }
 
@@ -1049,6 +1091,8 @@ int gmx_trjconv(int argc, char *argv[])
         {
         }
 
+        mtop = read_mtop_for_tng(top_file, in_file, out_file);
+
         /* Determine whether to read a topology */
         bTPS = (ftp2bSet(efTPS, NFILE, fnm) ||
                 bRmPBC || bReset || bPBCcomMol || bCluster ||
@@ -1083,7 +1127,7 @@ int gmx_trjconv(int argc, char *argv[])
             }
             if (bRmPBC)
             {
-                gpbc = gmx_rmpbc_init(&top.idef, ePBC, top.atoms.nr, top_box);
+                gpbc = gmx_rmpbc_init(&top.idef, ePBC, top.atoms.nr);
             }
         }
 
@@ -1144,12 +1188,12 @@ int gmx_trjconv(int argc, char *argv[])
         else
         {
             /* no index file, so read natoms from TRX */
-            if (!read_first_frame(oenv, &status, in_file, &fr, TRX_DONT_SKIP))
+            if (!read_first_frame(oenv, &trxin, in_file, &fr, TRX_DONT_SKIP))
             {
                 gmx_fatal(FARGS, "Could not read a frame from %s", in_file);
             }
             natoms = fr.natoms;
-            close_trj(status);
+            close_trj(trxin);
             sfree(fr.x);
             snew(index, natoms);
             for (i = 0; i < natoms; i++)
@@ -1205,20 +1249,26 @@ int gmx_trjconv(int argc, char *argv[])
         /* Make atoms struct for output in GRO or PDB files */
         if ((ftp == efGRO) || ((ftp == efG96) && bTPS) || (ftp == efPDB))
         {
-            /* get memory for stuff to go in .pdb file */
-            init_t_atoms(&useatoms, atoms->nr, FALSE);
+            /* get memory for stuff to go in .pdb file, and initialize
+             * the pdbinfo structure part if the input has it.
+             */
+            init_t_atoms(&useatoms, atoms->nr, (atoms->pdbinfo != NULL));
             sfree(useatoms.resinfo);
             useatoms.resinfo = atoms->resinfo;
             for (i = 0; (i < nout); i++)
             {
                 useatoms.atomname[i] = atoms->atomname[index[i]];
                 useatoms.atom[i]     = atoms->atom[index[i]];
+                if (atoms->pdbinfo != NULL)
+                {
+                    useatoms.pdbinfo[i]  = atoms->pdbinfo[index[i]];
+                }
                 useatoms.nres        = max(useatoms.nres, useatoms.atom[i].resind+1);
             }
             useatoms.nr = nout;
         }
         /* select what to read */
-        if (ftp == efTRR || ftp == efTRJ)
+        if (ftp == efTRR)
         {
             flags = TRX_READ_X;
         }
@@ -1236,7 +1286,7 @@ int gmx_trjconv(int argc, char *argv[])
         }
 
         /* open trx file for reading */
-        bHaveFirstFrame = read_first_frame(oenv, &status, in_file, &fr, flags);
+        bHaveFirstFrame = read_first_frame(oenv, &trxin, in_file, &fr, flags);
         if (fr.bPrec)
         {
             fprintf(stderr, "\nPrecision of %s is %g (nm)\n", in_file, 1/fr.prec);
@@ -1268,22 +1318,40 @@ int gmx_trjconv(int argc, char *argv[])
                 tzero = fr.time;
             }
 
-            /* open output for writing */
-            if ((bAppend) && (gmx_fexist(out_file)))
-            {
-                strcpy(filemode, "a");
-                fprintf(stderr, "APPENDING to existing file %s\n", out_file);
-            }
-            else
+            bCopy = FALSE;
+            if (bIndex)
             {
-                strcpy(filemode, "w");
+                /* check if index is meaningful */
+                for (i = 0; i < nout; i++)
+                {
+                    if (index[i] >= natoms)
+                    {
+                        gmx_fatal(FARGS,
+                                  "Index[%d] %d is larger than the number of atoms in the\n"
+                                  "trajectory file (%d). There is a mismatch in the contents\n"
+                                  "of your -f, -s and/or -n files.", i, index[i]+1, natoms);
+                    }
+                    bCopy = bCopy || (i != index[i]);
+                }
             }
+
+            /* open output for writing */
+            strcpy(filemode, "w");
             switch (ftp)
             {
+                case efTNG:
+                    trjtools_gmx_prepare_tng_writing(out_file,
+                                                     filemode[0],
+                                                     trxin,
+                                                     &trxout,
+                                                     NULL,
+                                                     nout,
+                                                     mtop,
+                                                     index,
+                                                     grpnm);
+                    break;
                 case efXTC:
-                case efG87:
                 case efTRR:
-                case efTRJ:
                     out = NULL;
                     if (!bSplit && !bSubTraj)
                     {
@@ -1295,27 +1363,13 @@ int gmx_trjconv(int argc, char *argv[])
                 case efPDB:
                     if (( !bSeparate && !bSplit ) && !bSubTraj)
                     {
-                        out = ffopen(out_file, filemode);
+                        out = gmx_ffopen(out_file, filemode);
                     }
                     break;
+                default:
+                    gmx_incons("Illegal output file format");
             }
 
-            bCopy = FALSE;
-            if (bIndex)
-            {
-                /* check if index is meaningful */
-                for (i = 0; i < nout; i++)
-                {
-                    if (index[i] >= natoms)
-                    {
-                        gmx_fatal(FARGS,
-                                  "Index[%d] %d is larger than the number of atoms in the\n"
-                                  "trajectory file (%d). There is a mismatch in the contents\n"
-                                  "of your -f, -s and/or -n files.", i, index[i]+1, natoms);
-                    }
-                    bCopy = bCopy || (i != index[i]);
-                }
-            }
             if (bCopy)
             {
                 snew(xmem, nout);
@@ -1329,13 +1383,6 @@ int gmx_trjconv(int argc, char *argv[])
                 }
             }
 
-            if (ftp == efG87)
-            {
-                fprintf(gmx_fio_getfp(trx_get_fileio(trxout)),
-                        "Generated by %s. #atoms=%d, a BOX is"
-                        " stored in this file.\n", Program(), nout);
-            }
-
             /* Start the big loop over frames */
             file_nr  =  0;
             frame    =  0;
@@ -1377,7 +1424,10 @@ int gmx_trjconv(int argc, char *argv[])
                     clear_mat(fr.box);
                     for (m = 0; m < DIM; m++)
                     {
-                        fr.box[m][m] = newbox[m];
+                        if (newbox[m] >= 0)
+                        {
+                            fr.box[m][m] = newbox[m];
+                        }
                     }
                 }
 
@@ -1450,9 +1500,7 @@ int gmx_trjconv(int argc, char *argv[])
                 }
                 else if (bCluster)
                 {
-                    rvec com;
-
-                    calc_pbc_cluster(ecenter, ifit, &top, ePBC, fr.x, ind_fit, com, fr.box, clustercenter);
+                    calc_pbc_cluster(ecenter, ifit, &top, ePBC, fr.x, ind_fit, fr.box);
                 }
 
                 if (bPFit)
@@ -1694,9 +1742,11 @@ int gmx_trjconv(int argc, char *argv[])
 
                         switch (ftp)
                         {
-                            case efTRJ:
+                            case efTNG:
+                                write_tng_frame(trxout, &frout);
+                                // TODO when trjconv behaves better: work how to read and write lambda
+                                break;
                             case efTRR:
-                            case efG87:
                             case efXTC:
                                 if (bSplitHere)
                                 {
@@ -1753,7 +1803,7 @@ int gmx_trjconv(int argc, char *argv[])
                                         top_title, fr.time);
                                 if (bSeparate || bSplitHere)
                                 {
-                                    out = ffopen(out_file2, "w");
+                                    out = gmx_ffopen(out_file2, "w");
                                 }
                                 switch (ftp)
                                 {
@@ -1802,7 +1852,7 @@ int gmx_trjconv(int argc, char *argv[])
                                 }
                                 if (bSeparate)
                                 {
-                                    ffclose(out);
+                                    gmx_ffclose(out);
                                     out = NULL;
                                 }
                                 break;
@@ -1834,7 +1884,7 @@ int gmx_trjconv(int argc, char *argv[])
                     }
                 }
                 frame++;
-                bHaveNextFrame = read_next_frame(oenv, status, &fr);
+                bHaveNextFrame = read_next_frame(oenv, trxin, &fr);
             }
             while (!(bTDump && bDumpFrame) && bHaveNextFrame);
         }
@@ -1846,7 +1896,7 @@ int gmx_trjconv(int argc, char *argv[])
         }
         fprintf(stderr, "\n");
 
-        close_trj(status);
+        close_trj(trxin);
         sfree(outf_base);
 
         if (bRmPBC)
@@ -1860,7 +1910,7 @@ int gmx_trjconv(int argc, char *argv[])
         }
         else if (out != NULL)
         {
-            ffclose(out);
+            gmx_ffclose(out);
         }
         if (bSubTraj)
         {
@@ -1874,9 +1924,9 @@ int gmx_trjconv(int argc, char *argv[])
         }
     }
 
-    do_view(oenv, out_file, NULL);
+    sfree(mtop);
 
-    thanx(stderr);
+    do_view(oenv, out_file, NULL);
 
     return 0;
 }