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"
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, efG87, 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, efG87, 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, efXYZ };
113 #define NSTOS asize(stos)
115 static const int stxs[] =
117 efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ,
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 { eftASC, ".g87", "gtraj", NULL, "Gromos-87 ASCII trajectory format" },
172 { eftXDR, ".edr", "ener", NULL, "Energy file"},
173 { eftGEN, ".???", "conf", "-c", "Structure file", NSTXS, stxs },
174 { eftGEN, ".???", "out", "-o", "Structure file", NSTOS, stos },
175 { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
176 { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
177 { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file"},
178 { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file"},
179 { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
180 { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
181 { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD"},
182 { eftASC, ".xyz", "conf", "-o", "Coordinate file for some other programs" },
183 { eftXDR, ".cpt", "state", "-cp", "Checkpoint file"},
184 { eftASC, ".log", "run", "-l", "Log file"},
185 { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file"},
186 { eftASC, ".out", "hello", "-o", "Generic output file"},
187 { eftASC, ".ndx", "index", "-n", "Index file", },
188 { eftASC, ".top", "topol", "-p", "Topology file"},
189 { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
190 { eftGEN, ".???", "topol", "-s", "Run input file", NTPXS, tpxs },
191 { eftGEN, ".???", "topol", "-s", "Structure+mass(db)", NTPSS, tpss },
192 { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file"},
193 { eftASC, ".tpa", "topol", "-s", "Ascii run input file"},
194 { eftBIN, ".tpb", "topol", "-s", "Binary run input file"},
195 { eftASC, ".tex", "doc", "-o", "LaTeX file"},
196 { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
197 { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
198 { eftASC, ".hdb", "polar", NULL, "Hydrogen data base"},
199 { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
200 { eftASC, ".dlg", "user", NULL, "Dialog Box data for ngmx"},
201 { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
202 { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
203 { eftASC, ".mat", "ss", NULL, "Matrix Data file"},
204 { eftASC, ".m2p", "ps", NULL, "Input file for mat2ps"},
205 { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
206 { eftASC, ".edi", "sam", NULL, "ED sampling input"},
207 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
208 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
209 { eftASC, "", "rundir", NULL, "Run directory" }
213 static const char *z_ext[NZEXT] =
216 const char *ftp2ext(int ftp)
218 if ((0 <= ftp) && (ftp < efNR))
220 return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
228 const char *ftp2ext_generic(int ftp)
230 if ((0 <= ftp) && (ftp < efNR))
256 const char *ftp2ext_with_dot(int ftp)
258 if ((0 <= ftp) && (ftp < efNR))
260 return deffile[ftp].ext;
268 int ftp2generic_count(int ftp)
270 if ((0 <= ftp) && (ftp < efNR))
272 return deffile[ftp].ntps;
280 const int *ftp2generic_list(int ftp)
282 if ((0 <= ftp) && (ftp < efNR))
284 return deffile[ftp].tps;
292 const char *ftp2desc(int ftp)
294 if ((0 <= ftp) && (ftp < efNR))
296 return deffile[ftp].descr;
300 return "unknown filetype";
304 const char *ftp2ftype(int ftp)
306 if ((ftp >= 0) && (ftp < efNR))
308 switch (deffile[ftp].ftype)
315 return "XDR portable";
321 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
328 const char *ftp2defnm(int ftp)
330 if ((0 <= ftp) && (ftp < efNR))
332 return deffile[ftp].defnm;
340 static void check_opts(int nf, t_filenm fnm[])
345 for (i = 0; (i < nf); i++)
347 df = &(deffile[fnm[i].ftp]);
348 if (fnm[i].opt == NULL)
350 if (df->defopt == NULL)
352 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
353 deffile[fnm[i].ftp].ext, fnm[i].ftp);
357 fnm[i].opt = df->defopt;
363 int fn2ftp(const char *fn)
375 if ((len >= 4) && (fn[len - 4] == '.'))
377 feptr = &(fn[len - 4]);
384 for (i = 0; (i < efNR); i++)
386 if ((eptr = deffile[i].ext) != NULL)
388 if (gmx_strcasecmp(feptr, eptr) == 0)
398 static void set_extension(char *buf, int ftp)
403 /* check if extension is already at end of filename */
404 df = &(deffile[ftp]);
406 extlen = strlen(df->ext);
407 if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
409 strcat(buf, df->ext);
413 static void add_filenm(t_filenm *fnm, const char *filenm)
415 srenew(fnm->fns, fnm->nfiles+1);
416 fnm->fns[fnm->nfiles] = strdup(filenm);
420 static void set_grpfnm(t_filenm *fnm, const char *name, const char *deffnm)
422 char buf[256], buf2[256];
428 nopts = deffile[fnm->ftp].ntps;
429 ftps = deffile[fnm->ftp].tps;
430 if ((nopts == 0) || (ftps == NULL))
432 gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
436 if (name && deffnm == NULL)
439 /* First check whether we have a valid filename already */
441 if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
443 /*if file exist don't add an extension for trajectory reading*/
444 bValidExt = gmx_fexist(name);
446 for (i = 0; (i < nopts) && !bValidExt; i++)
454 else if (deffnm != NULL)
460 /* No name given, set the default name */
461 strcpy(buf, ftp2defnm(fnm->ftp));
464 if (!bValidExt && (fnm->flag & ffREAD))
466 /* for input-files only: search for filenames in the directory */
467 for (i = 0; (i < nopts) && !bValidExt; i++)
471 set_extension(buf2, type);
472 if (gmx_fexist(buf2))
482 /* Use the first extension type */
483 set_extension(buf, ftps[0]);
486 add_filenm(fnm, buf);
489 static void set_filenm(t_filenm *fnm, const char *name, const char *deffnm,
492 /* Set the default filename, extension and option for those fields that
493 * are not already set. An extension is added if not present, if fn = NULL
494 * or empty, the default filename is given.
499 if ((fnm->flag & ffREAD) && !bReadNode)
504 if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
506 gmx_fatal(FARGS, "file type out of range (%d)", fnm->ftp);
513 if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
515 /* check if filename ends in .gz or .Z, if so remove that: */
517 for (i = 0; i < NZEXT; i++)
519 extlen = strlen(z_ext[i]);
522 if (gmx_strcasecmp(name+len-extlen, z_ext[i]) == 0)
524 buf[len-extlen] = '\0';
531 if (deffile[fnm->ftp].ntps)
533 set_grpfnm(fnm, name ? buf : NULL, deffnm);
537 if (name == NULL || deffnm != NULL)
545 strcpy(buf, ftp2defnm(fnm->ftp));
548 set_extension(buf, fnm->ftp);
550 add_filenm(fnm, buf);
554 static void set_filenms(int nf, t_filenm fnm[], const char *deffnm, gmx_bool bReadNode)
558 for (i = 0; (i < nf); i++)
562 set_filenm(&(fnm[i]), fnm[i].fn, deffnm, bReadNode);
567 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
568 const char *deffnm, gmx_bool bReadNode)
575 for (i = 0; (i < nf); i++)
582 snew(bRemove, (*argc)+1);
586 for (j = 0; (j < nf); j++)
588 if (strcmp(argv[i], fnm[j].opt) == 0)
593 /* check if we are out of arguments for this option */
594 if ((i >= *argc) || (argv[i][0] == '-'))
596 set_filenm(&fnm[j], fnm[j].fn, deffnm, bReadNode);
598 /* sweep up all file arguments for this option */
599 while ((i < *argc) && (argv[i][0] != '-'))
601 set_filenm(&fnm[j], argv[i], NULL, bReadNode);
604 /* only repeat for 'multiple' file options: */
605 if (!IS_MULT(fnm[j]))
611 break; /* jump out of 'j' loop */
614 /* No file found corresponding to option argv[i] */
622 /* Remove used entries */
623 for (i = j = 0; (i <= *argc); i++)
634 set_filenms(nf, fnm, deffnm, bReadNode);
638 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
642 for (i = 0; (i < nfile); i++)
644 if (strcmp(opt, fnm[i].opt) == 0)
646 return fnm[i].fns[0];
650 fprintf(stderr, "No option %s\n", opt);
655 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
658 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
661 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
665 for (i = 0; (i < nfile); i++)
667 if (strcmp(opt, fnm[i].opt) == 0)
670 return fnm[i].nfiles;
674 fprintf(stderr, "No option %s\n", opt);
678 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
682 for (i = 0; (i < nfile); i++)
684 if (ftp == fnm[i].ftp)
686 return fnm[i].fns[0];
690 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
694 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
698 for (i = 0; (i < nfile); i++)
700 if (ftp == fnm[i].ftp)
703 return fnm[i].nfiles;
707 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
711 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
715 for (i = 0; (i < nfile); i++)
717 if (ftp == fnm[i].ftp)
719 return (gmx_bool) IS_SET(fnm[i]);
723 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
728 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
732 for (i = 0; (i < nfile); i++)
734 if (strcmp(opt, fnm[i].opt) == 0)
736 return (gmx_bool) IS_SET(fnm[i]);
740 fprintf(stderr, "No option %s\n", opt);
745 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
749 for (i = 0; (i < nfile); i++)
751 if (strcmp(opt, fnm[i].opt) == 0)
753 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
759 return fnm[i].fns[0];
763 fprintf(stderr, "No option %s\n", opt);
767 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
771 for (i = 0; (i < nfile); i++)
773 if (ftp == fnm[i].ftp)
775 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
781 return fnm[i].fns[0];
785 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
789 gmx_bool is_optional(const t_filenm *fnm)
791 return ((fnm->flag & ffOPT) == ffOPT);
794 gmx_bool is_output(const t_filenm *fnm)
796 return ((fnm->flag & ffWRITE) == ffWRITE);
799 gmx_bool is_set(const t_filenm *fnm)
801 return ((fnm->flag & ffSET) == ffSET);
804 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
807 char buf[STRLEN], newname[STRLEN];
810 for (i = 0; i < nfile; i++)
812 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
814 /* We never use multiple _outputs_, but we might as well check
815 for it, just in case... */
816 for (j = 0; j < fnm[i].nfiles; j++)
818 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
819 extpos = strrchr(buf, '.');
821 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
823 fnm[i].fns[j] = strdup(newname);
830 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
836 for (i = 0; i < nf; i++)
838 ret[i] = tfn[i]; /* just directly copy all non-string fields */
841 ret[i].opt = strdup(tfn[i].opt);
850 ret[i].fn = strdup(tfn[i].fn);
857 if (tfn[i].nfiles > 0)
859 snew(ret[i].fns, tfn[i].nfiles);
860 for (j = 0; j < tfn[i].nfiles; j++)
862 ret[i].fns[j] = strdup(tfn[i].fns[j]);
869 void done_filenms(int nf, t_filenm fnm[])
873 for (i = 0; i < nf; ++i)
875 for (j = 0; j < fnm[i].nfiles; ++j)
877 sfree(fnm[i].fns[j]);