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, 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",
160 "Trajectory: tng xtc trr trj gro g96 pdb cpt", NTRXS, trxs },
161 { eftGEN, ".???", "trajout", "-f",
162 "Trajectory: tng xtc trr trj gro g96 pdb", NTROS, tros },
163 { eftGEN, ".???", "traj", NULL,
164 "Full precision trajectory: tng trr trj cpt", NTRNS, trns },
165 { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
166 { eftBIN, ".trj", "traj", NULL, "Trajectory file (architecture specific)" },
167 { eftGEN, ".???", "traj_comp", NULL,
168 "Compressed trajectory (tng format or portable xdr format): tng xtc", NTRCOMPRESSED, trcompressed},
169 { eftXDR, ".xtc", "traj", NULL,
170 "Compressed trajectory (portable xdr format): xtc" },
171 { eftTNG, ".tng", "traj", NULL,
172 "Trajectory file (tng format)" },
173 { eftASC, ".g87", "gtraj", NULL, "Gromos-87 ASCII trajectory format" },
174 { eftXDR, ".edr", "ener", NULL, "Energy file"},
175 { eftGEN, ".???", "conf", "-c", "Structure file: gro g96 pdb tpr etc.",
177 { eftGEN, ".???", "out", "-o", "Structure file: gro g96 pdb etc.",
179 { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
180 { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
181 { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file"},
182 { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file"},
183 { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
184 { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
185 { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD"},
186 { eftASC, ".xyz", "conf", "-o", "Coordinate file for some other programs" },
187 { eftXDR, ".cpt", "state", "-cp", "Checkpoint file"},
188 { eftASC, ".log", "run", "-l", "Log file"},
189 { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file"},
190 { eftASC, ".out", "hello", "-o", "Generic output file"},
191 { eftASC, ".ndx", "index", "-n", "Index file", },
192 { eftASC, ".top", "topol", "-p", "Topology file"},
193 { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
194 { eftGEN, ".???", "topol", "-s", "Run input file: tpr tpb tpa",
196 { eftGEN, ".???", "topol", "-s",
197 "Structure+mass(db): tpr tpb tpa gro g96 pdb", NTPSS, tpss },
198 { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file"},
199 { eftASC, ".tpa", "topol", "-s", "Ascii run input file"},
200 { eftBIN, ".tpb", "topol", "-s", "Binary run input file"},
201 { eftASC, ".tex", "doc", "-o", "LaTeX file"},
202 { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
203 { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
204 { eftASC, ".hdb", "polar", NULL, "Hydrogen data base"},
205 { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
206 { eftASC, ".dlg", "user", NULL, "Dialog Box data for ngmx"},
207 { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
208 { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
209 { eftASC, ".mat", "ss", NULL, "Matrix Data file"},
210 { eftASC, ".m2p", "ps", NULL, "Input file for mat2ps"},
211 { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
212 { eftASC, ".edi", "sam", NULL, "ED sampling input"},
213 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
214 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
215 { eftASC, "", "rundir", NULL, "Run directory" }
219 static const char *z_ext[NZEXT] =
222 const char *ftp2ext(int ftp)
224 if ((0 <= ftp) && (ftp < efNR))
226 return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
234 const char *ftp2ext_generic(int ftp)
236 if ((0 <= ftp) && (ftp < efNR))
262 const char *ftp2ext_with_dot(int ftp)
264 if ((0 <= ftp) && (ftp < efNR))
266 return deffile[ftp].ext;
274 int ftp2generic_count(int ftp)
276 if ((0 <= ftp) && (ftp < efNR))
278 return deffile[ftp].ntps;
286 const int *ftp2generic_list(int ftp)
288 if ((0 <= ftp) && (ftp < efNR))
290 return deffile[ftp].tps;
298 const char *ftp2desc(int ftp)
300 if ((0 <= ftp) && (ftp < efNR))
302 return deffile[ftp].descr;
306 return "unknown filetype";
310 const char *ftp2ftype(int ftp)
312 if ((ftp >= 0) && (ftp < efNR))
314 switch (deffile[ftp].ftype)
321 return "XDR portable";
327 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
334 const char *ftp2defnm(int ftp)
336 if ((0 <= ftp) && (ftp < efNR))
338 return deffile[ftp].defnm;
346 static void check_opts(int nf, t_filenm fnm[])
351 for (i = 0; (i < nf); i++)
353 df = &(deffile[fnm[i].ftp]);
354 if (fnm[i].opt == NULL)
356 if (df->defopt == NULL)
358 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
359 deffile[fnm[i].ftp].ext, fnm[i].ftp);
363 fnm[i].opt = df->defopt;
369 int fn2ftp(const char *fn)
381 if ((len >= 4) && (fn[len - 4] == '.'))
383 feptr = &(fn[len - 4]);
390 for (i = 0; (i < efNR); i++)
392 if ((eptr = deffile[i].ext) != NULL)
394 if (gmx_strcasecmp(feptr, eptr) == 0)
404 static void set_extension(char *buf, int ftp)
409 /* check if extension is already at end of filename */
410 df = &(deffile[ftp]);
412 extlen = strlen(df->ext);
413 if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
415 strcat(buf, df->ext);
419 static void add_filenm(t_filenm *fnm, const char *filenm)
421 srenew(fnm->fns, fnm->nfiles+1);
422 fnm->fns[fnm->nfiles] = strdup(filenm);
426 static void set_grpfnm(t_filenm *fnm, const char *name, const char *deffnm)
428 char buf[256], buf2[256];
434 nopts = deffile[fnm->ftp].ntps;
435 ftps = deffile[fnm->ftp].tps;
436 if ((nopts == 0) || (ftps == NULL))
438 gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
442 if (name && deffnm == NULL)
445 /* First check whether we have a valid filename already */
447 if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
449 /*if file exist don't add an extension for trajectory reading*/
450 bValidExt = gmx_fexist(name);
452 for (i = 0; (i < nopts) && !bValidExt; i++)
460 else if (deffnm != NULL)
466 /* No name given, set the default name */
467 strcpy(buf, ftp2defnm(fnm->ftp));
470 if (!bValidExt && (fnm->flag & ffREAD))
472 /* for input-files only: search for filenames in the directory */
473 for (i = 0; (i < nopts) && !bValidExt; i++)
477 set_extension(buf2, type);
478 if (gmx_fexist(buf2))
488 /* Use the first extension type */
489 set_extension(buf, ftps[0]);
492 add_filenm(fnm, buf);
495 static void set_filenm(t_filenm *fnm, const char *name, const char *deffnm,
498 /* Set the default filename, extension and option for those fields that
499 * are not already set. An extension is added if not present, if fn = NULL
500 * or empty, the default filename is given.
505 if ((fnm->flag & ffREAD) && !bReadNode)
510 if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
512 gmx_fatal(FARGS, "file type out of range (%d)", fnm->ftp);
519 if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
521 /* check if filename ends in .gz or .Z, if so remove that: */
523 for (i = 0; i < NZEXT; i++)
525 extlen = strlen(z_ext[i]);
528 if (gmx_strcasecmp(name+len-extlen, z_ext[i]) == 0)
530 buf[len-extlen] = '\0';
537 if (deffile[fnm->ftp].ntps)
539 set_grpfnm(fnm, name ? buf : NULL, deffnm);
543 if (name == NULL || deffnm != NULL)
551 strcpy(buf, ftp2defnm(fnm->ftp));
554 set_extension(buf, fnm->ftp);
556 add_filenm(fnm, buf);
560 static void set_filenms(int nf, t_filenm fnm[], const char *deffnm, gmx_bool bReadNode)
564 for (i = 0; (i < nf); i++)
568 set_filenm(&(fnm[i]), fnm[i].fn, deffnm, bReadNode);
573 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
574 const char *deffnm, gmx_bool bReadNode)
581 for (i = 0; (i < nf); i++)
588 snew(bRemove, (*argc)+1);
592 for (j = 0; (j < nf); j++)
594 if (strcmp(argv[i], fnm[j].opt) == 0)
599 /* check if we are out of arguments for this option */
600 if ((i >= *argc) || (argv[i][0] == '-'))
602 set_filenm(&fnm[j], fnm[j].fn, deffnm, bReadNode);
604 /* sweep up all file arguments for this option */
605 while ((i < *argc) && (argv[i][0] != '-'))
607 set_filenm(&fnm[j], argv[i], NULL, bReadNode);
610 /* only repeat for 'multiple' file options: */
611 if (!IS_MULT(fnm[j]))
617 break; /* jump out of 'j' loop */
620 /* No file found corresponding to option argv[i] */
628 /* Remove used entries */
629 for (i = j = 0; (i <= *argc); i++)
640 set_filenms(nf, fnm, deffnm, bReadNode);
644 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
648 for (i = 0; (i < nfile); i++)
650 if (strcmp(opt, fnm[i].opt) == 0)
652 return fnm[i].fns[0];
656 fprintf(stderr, "No option %s\n", opt);
661 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
664 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
667 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
671 for (i = 0; (i < nfile); i++)
673 if (strcmp(opt, fnm[i].opt) == 0)
676 return fnm[i].nfiles;
680 fprintf(stderr, "No option %s\n", opt);
684 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
688 for (i = 0; (i < nfile); i++)
690 if (ftp == fnm[i].ftp)
692 return fnm[i].fns[0];
696 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
700 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
704 for (i = 0; (i < nfile); i++)
706 if (ftp == fnm[i].ftp)
709 return fnm[i].nfiles;
713 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
717 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
721 for (i = 0; (i < nfile); i++)
723 if (ftp == fnm[i].ftp)
725 return (gmx_bool) IS_SET(fnm[i]);
729 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
734 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
738 for (i = 0; (i < nfile); i++)
740 if (strcmp(opt, fnm[i].opt) == 0)
742 return (gmx_bool) IS_SET(fnm[i]);
746 fprintf(stderr, "No option %s\n", opt);
751 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
755 for (i = 0; (i < nfile); i++)
757 if (strcmp(opt, fnm[i].opt) == 0)
759 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
765 return fnm[i].fns[0];
769 fprintf(stderr, "No option %s\n", opt);
773 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
777 for (i = 0; (i < nfile); i++)
779 if (ftp == fnm[i].ftp)
781 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
787 return fnm[i].fns[0];
791 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
795 gmx_bool is_optional(const t_filenm *fnm)
797 return ((fnm->flag & ffOPT) == ffOPT);
800 gmx_bool is_output(const t_filenm *fnm)
802 return ((fnm->flag & ffWRITE) == ffWRITE);
805 gmx_bool is_set(const t_filenm *fnm)
807 return ((fnm->flag & ffSET) == ffSET);
810 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
813 char buf[STRLEN], newname[STRLEN];
816 for (i = 0; i < nfile; i++)
818 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
820 /* We never use multiple _outputs_, but we might as well check
821 for it, just in case... */
822 for (j = 0; j < fnm[i].nfiles; j++)
824 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
825 extpos = strrchr(buf, '.');
827 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
829 fnm[i].fns[j] = strdup(newname);
836 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
842 for (i = 0; i < nf; i++)
844 ret[i] = tfn[i]; /* just directly copy all non-string fields */
847 ret[i].opt = strdup(tfn[i].opt);
856 ret[i].fn = strdup(tfn[i].fn);
863 if (tfn[i].nfiles > 0)
865 snew(ret[i].fns, tfn[i].nfiles);
866 for (j = 0; j < tfn[i].nfiles; j++)
868 ret[i].fns[j] = strdup(tfn[i].fns[j]);
875 void done_filenms(int nf, t_filenm fnm[])
879 for (i = 0; i < nf; ++i)
881 for (j = 0; j < fnm[i].nfiles; ++j)
883 sfree(fnm[i].fns[j]);