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, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
48 #include "gmx_fatal.h"
50 #include "gromacs/utility/smalloc.h"
51 #include "gromacs/utility/cstringutil.h"
52 #include "types/simple.h"
53 #include "types/commrec.h"
55 /* XDR should be available on all platforms now,
56 * but we keep the possibility of turning it off...
61 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
62 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
63 #define IS_MULT(fn) ((fn.flag & ffMULT) != 0)
64 #define UN_SET(fn) (fn.flag = (fn.flag & ~ffSET))
65 #define DO_SET(fn) (fn.flag = (fn.flag | ffSET))
69 eftASC, eftBIN, eftXDR, eftTNG, eftGEN, eftNR
72 /* To support multiple file types with one general (eg TRX) we have
75 static const int trxs[] =
80 efTRJ, efGRO, efG96, efPDB, efTNG
82 #define NTRXS asize(trxs)
84 static const int trcompressed[] =
91 #define NTRCOMPRESSED asize(trcompressed)
93 static const int tros[] =
98 efTRJ, efGRO, efG96, efPDB, efTNG
100 #define NTROS asize(tros)
102 static const int trns[] =
109 #define NTRNS asize(trns)
111 static const int stos[] =
112 { efGRO, efG96, efPDB, efBRK, efENT, efESP };
113 #define NSTOS asize(stos)
115 static const int stxs[] =
117 efGRO, efG96, efPDB, efBRK, efENT, efESP,
123 #define NSTXS asize(stxs)
125 static const int tpxs[] =
132 #define NTPXS asize(tpxs)
134 static const int tpss[] =
139 efTPB, efTPA, efGRO, efG96, efPDB, efBRK, efENT
141 #define NTPSS asize(tpss)
154 /* this array should correspond to the enum in filenm.h */
155 static const t_deffile
158 { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
159 { eftGEN, ".???", "traj", "-f", "Trajectory", NTRXS, trxs },
160 { eftGEN, ".???", "trajout", "-f", "Trajectory", NTROS, tros },
161 { eftGEN, ".???", "traj", NULL,
162 "Full precision trajectory", NTRNS, trns },
163 { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
164 { eftBIN, ".trj", "traj", NULL, "Trajectory file (architecture specific)" },
165 { eftGEN, ".???", "traj_comp", NULL,
166 "Compressed trajectory (tng format or portable xdr format)", NTRCOMPRESSED, trcompressed},
167 { eftXDR, ".xtc", "traj", NULL,
168 "Compressed trajectory (portable xdr format): xtc" },
169 { eftTNG, ".tng", "traj", NULL,
170 "Trajectory file (tng format)" },
171 { eftXDR, ".edr", "ener", NULL, "Energy file"},
172 { eftGEN, ".???", "conf", "-c", "Structure file", NSTXS, stxs },
173 { eftGEN, ".???", "out", "-o", "Structure file", NSTOS, stos },
174 { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
175 { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
176 { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file"},
177 { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file"},
178 { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
179 { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
180 { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD"},
181 { eftXDR, ".cpt", "state", "-cp", "Checkpoint file"},
182 { eftASC, ".log", "run", "-l", "Log file"},
183 { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file"},
184 { eftASC, ".out", "hello", "-o", "Generic output file"},
185 { eftASC, ".ndx", "index", "-n", "Index file", },
186 { eftASC, ".top", "topol", "-p", "Topology file"},
187 { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
188 { eftGEN, ".???", "topol", "-s", "Run input file", NTPXS, tpxs },
189 { eftGEN, ".???", "topol", "-s", "Structure+mass(db)", NTPSS, tpss },
190 { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file"},
191 { eftASC, ".tpa", "topol", "-s", "Ascii run input file"},
192 { eftBIN, ".tpb", "topol", "-s", "Binary run input file"},
193 { eftASC, ".tex", "doc", "-o", "LaTeX file"},
194 { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
195 { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
196 { eftASC, ".hdb", "polar", NULL, "Hydrogen data base"},
197 { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
198 { eftASC, ".dlg", "user", NULL, "Dialog Box data for ngmx"},
199 { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
200 { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
201 { eftASC, ".mat", "ss", NULL, "Matrix Data file"},
202 { eftASC, ".m2p", "ps", NULL, "Input file for mat2ps"},
203 { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
204 { eftASC, ".edi", "sam", NULL, "ED sampling input"},
205 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
206 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
207 { eftASC, "", "rundir", NULL, "Run directory" }
211 static const char *z_ext[NZEXT] =
214 const char *ftp2ext(int ftp)
216 if ((0 <= ftp) && (ftp < efNR))
218 return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
226 const char *ftp2ext_generic(int ftp)
228 if ((0 <= ftp) && (ftp < efNR))
254 const char *ftp2ext_with_dot(int ftp)
256 if ((0 <= ftp) && (ftp < efNR))
258 return deffile[ftp].ext;
266 int ftp2generic_count(int ftp)
268 if ((0 <= ftp) && (ftp < efNR))
270 return deffile[ftp].ntps;
278 const int *ftp2generic_list(int ftp)
280 if ((0 <= ftp) && (ftp < efNR))
282 return deffile[ftp].tps;
290 const char *ftp2desc(int ftp)
292 if ((0 <= ftp) && (ftp < efNR))
294 return deffile[ftp].descr;
298 return "unknown filetype";
302 const char *ftp2ftype(int ftp)
304 if ((ftp >= 0) && (ftp < efNR))
306 switch (deffile[ftp].ftype)
313 return "XDR portable";
319 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
326 const char *ftp2defnm(int ftp)
328 if ((0 <= ftp) && (ftp < efNR))
330 return deffile[ftp].defnm;
338 static void check_opts(int nf, t_filenm fnm[])
343 for (i = 0; (i < nf); i++)
345 df = &(deffile[fnm[i].ftp]);
346 if (fnm[i].opt == NULL)
348 if (df->defopt == NULL)
350 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
351 deffile[fnm[i].ftp].ext, fnm[i].ftp);
355 fnm[i].opt = df->defopt;
361 int fn2ftp(const char *fn)
373 if ((len >= 4) && (fn[len - 4] == '.'))
375 feptr = &(fn[len - 4]);
382 for (i = 0; (i < efNR); i++)
384 if ((eptr = deffile[i].ext) != NULL)
386 if (gmx_strcasecmp(feptr, eptr) == 0)
396 static void set_extension(char *buf, int ftp)
401 /* check if extension is already at end of filename */
402 df = &(deffile[ftp]);
404 extlen = strlen(df->ext);
405 if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
407 strcat(buf, df->ext);
411 static void add_filenm(t_filenm *fnm, const char *filenm)
413 srenew(fnm->fns, fnm->nfiles+1);
414 fnm->fns[fnm->nfiles] = strdup(filenm);
418 static void set_grpfnm(t_filenm *fnm, const char *name, const char *deffnm)
420 char buf[256], buf2[256];
426 nopts = deffile[fnm->ftp].ntps;
427 ftps = deffile[fnm->ftp].tps;
428 if ((nopts == 0) || (ftps == NULL))
430 gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
434 if (name && deffnm == NULL)
437 /* First check whether we have a valid filename already */
439 if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
441 /*if file exist don't add an extension for trajectory reading*/
442 bValidExt = gmx_fexist(name);
444 for (i = 0; (i < nopts) && !bValidExt; i++)
452 else if (deffnm != NULL)
458 /* No name given, set the default name */
459 strcpy(buf, ftp2defnm(fnm->ftp));
462 if (!bValidExt && (fnm->flag & ffREAD))
464 /* for input-files only: search for filenames in the directory */
465 for (i = 0; (i < nopts) && !bValidExt; i++)
469 set_extension(buf2, type);
470 if (gmx_fexist(buf2))
480 /* Use the first extension type */
481 set_extension(buf, ftps[0]);
484 add_filenm(fnm, buf);
487 static void set_filenm(t_filenm *fnm, const char *name, const char *deffnm,
490 /* Set the default filename, extension and option for those fields that
491 * are not already set. An extension is added if not present, if fn = NULL
492 * or empty, the default filename is given.
497 if ((fnm->flag & ffREAD) && !bReadNode)
502 if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
504 gmx_fatal(FARGS, "file type out of range (%d)", fnm->ftp);
511 if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
513 /* check if filename ends in .gz or .Z, if so remove that: */
515 for (i = 0; i < NZEXT; i++)
517 extlen = strlen(z_ext[i]);
520 if (gmx_strcasecmp(name+len-extlen, z_ext[i]) == 0)
522 buf[len-extlen] = '\0';
529 if (deffile[fnm->ftp].ntps)
531 set_grpfnm(fnm, name ? buf : NULL, deffnm);
535 if (name == NULL || deffnm != NULL)
543 strcpy(buf, ftp2defnm(fnm->ftp));
546 set_extension(buf, fnm->ftp);
548 add_filenm(fnm, buf);
552 static void set_filenms(int nf, t_filenm fnm[], const char *deffnm, gmx_bool bReadNode)
556 for (i = 0; (i < nf); i++)
560 set_filenm(&(fnm[i]), fnm[i].fn, deffnm, bReadNode);
565 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
566 const char *deffnm, gmx_bool bReadNode)
573 for (i = 0; (i < nf); i++)
580 snew(bRemove, (*argc)+1);
584 for (j = 0; (j < nf); j++)
586 if (strcmp(argv[i], fnm[j].opt) == 0)
591 /* check if we are out of arguments for this option */
592 if ((i >= *argc) || (argv[i][0] == '-'))
594 set_filenm(&fnm[j], fnm[j].fn, deffnm, bReadNode);
596 /* sweep up all file arguments for this option */
597 while ((i < *argc) && (argv[i][0] != '-'))
599 set_filenm(&fnm[j], argv[i], NULL, bReadNode);
602 /* only repeat for 'multiple' file options: */
603 if (!IS_MULT(fnm[j]))
609 break; /* jump out of 'j' loop */
612 /* No file found corresponding to option argv[i] */
620 /* Remove used entries */
621 for (i = j = 0; (i <= *argc); i++)
632 set_filenms(nf, fnm, deffnm, bReadNode);
636 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
640 for (i = 0; (i < nfile); i++)
642 if (strcmp(opt, fnm[i].opt) == 0)
644 return fnm[i].fns[0];
648 fprintf(stderr, "No option %s\n", opt);
653 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
656 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
659 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
663 for (i = 0; (i < nfile); i++)
665 if (strcmp(opt, fnm[i].opt) == 0)
668 return fnm[i].nfiles;
672 fprintf(stderr, "No option %s\n", opt);
676 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
680 for (i = 0; (i < nfile); i++)
682 if (ftp == fnm[i].ftp)
684 return fnm[i].fns[0];
688 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
692 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
696 for (i = 0; (i < nfile); i++)
698 if (ftp == fnm[i].ftp)
701 return fnm[i].nfiles;
705 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
709 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
713 for (i = 0; (i < nfile); i++)
715 if (ftp == fnm[i].ftp)
717 return (gmx_bool) IS_SET(fnm[i]);
721 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
726 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
730 for (i = 0; (i < nfile); i++)
732 if (strcmp(opt, fnm[i].opt) == 0)
734 return (gmx_bool) IS_SET(fnm[i]);
738 fprintf(stderr, "No option %s\n", opt);
743 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
747 for (i = 0; (i < nfile); i++)
749 if (strcmp(opt, fnm[i].opt) == 0)
751 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
757 return fnm[i].fns[0];
761 fprintf(stderr, "No option %s\n", opt);
765 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
769 for (i = 0; (i < nfile); i++)
771 if (ftp == fnm[i].ftp)
773 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
779 return fnm[i].fns[0];
783 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
787 gmx_bool is_optional(const t_filenm *fnm)
789 return ((fnm->flag & ffOPT) == ffOPT);
792 gmx_bool is_output(const t_filenm *fnm)
794 return ((fnm->flag & ffWRITE) == ffWRITE);
797 gmx_bool is_set(const t_filenm *fnm)
799 return ((fnm->flag & ffSET) == ffSET);
802 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
805 char buf[STRLEN], newname[STRLEN];
808 for (i = 0; i < nfile; i++)
810 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
812 /* We never use multiple _outputs_, but we might as well check
813 for it, just in case... */
814 for (j = 0; j < fnm[i].nfiles; j++)
816 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
817 extpos = strrchr(buf, '.');
819 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
821 fnm[i].fns[j] = strdup(newname);
828 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
834 for (i = 0; i < nf; i++)
836 ret[i] = tfn[i]; /* just directly copy all non-string fields */
839 ret[i].opt = strdup(tfn[i].opt);
848 ret[i].fn = strdup(tfn[i].fn);
855 if (tfn[i].nfiles > 0)
857 snew(ret[i].fns, tfn[i].nfiles);
858 for (j = 0; j < tfn[i].nfiles; j++)
860 ret[i].fns[j] = strdup(tfn[i].fns[j]);
867 void done_filenms(int nf, t_filenm fnm[])
871 for (i = 0; i < nf; ++i)
873 for (j = 0; j < fnm[i].nfiles; ++j)
875 sfree(fnm[i].fns[j]);