Merge branch release-5-1 into release-2016
[alexxy/gromacs.git] / src / gromacs / fileio / filetypes.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016, 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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #include "gmxpre.h"
38
39 #include "filetypes.h"
40
41 #include <cstring>
42
43 #include "gromacs/utility/arraysize.h"
44 #include "gromacs/utility/cstringutil.h"
45
46 enum
47 {
48     eftASC, eftXDR, eftTNG, eftGEN, eftNR
49 };
50
51 /* To support multiple file types with one general (eg TRX) we have
52  * these arrays.
53  */
54 static const int trxs[] =
55 {
56     efXTC, efTRR, efCPT,
57     efGRO, efG96, efPDB, efTNG
58 };
59 #define NTRXS asize(trxs)
60
61 static const int trcompressed[] =
62 {
63     efXTC,
64     efTNG
65 };
66 #define NTRCOMPRESSED asize(trcompressed)
67
68 static const int tros[] =
69 {
70     efXTC, efTRR,
71     efGRO, efG96, efPDB, efTNG
72 };
73 #define NTROS asize(tros)
74
75 static const int trns[] =
76 {
77     efTRR, efCPT,
78     efTNG
79 };
80 #define NTRNS asize(trns)
81
82 static const int stos[] =
83 { efGRO, efG96, efPDB, efBRK, efENT, efESP };
84 #define NSTOS asize(stos)
85
86 static const int stxs[] =
87 {
88     efGRO, efG96, efPDB, efBRK, efENT, efESP,
89     efTPR
90 };
91 #define NSTXS asize(stxs)
92
93 static const int tpss[] =
94 {
95     efTPR,
96     efGRO, efG96, efPDB, efBRK, efENT
97 };
98 #define NTPSS asize(tpss)
99
100 typedef struct
101 {
102     int         ftype;
103     const char *ext;
104     const char *defnm;
105     const char *defopt;
106     const char *descr;
107     int         ntps;
108     const int  *tps;
109 } t_deffile;
110
111 /* this array should correspond to the enum in filetypes.h */
112 static const t_deffile deffile[efNR] =
113 {
114     { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
115     { eftGEN, ".???", "traj", "-f", "Trajectory", NTRXS, trxs },
116     { eftGEN, ".???", "trajout", "-f", "Trajectory", NTROS, tros },
117     { eftGEN, ".???", "traj", NULL,
118       "Full precision trajectory", NTRNS, trns },
119     { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
120     { eftGEN, ".???", "traj_comp", NULL,
121       "Compressed trajectory (tng format or portable xdr format)", NTRCOMPRESSED, trcompressed},
122     { eftXDR, ".xtc", "traj", NULL,
123       "Compressed trajectory (portable xdr format): xtc" },
124     { eftTNG, ".tng", "traj", NULL,
125       "Trajectory file (tng format)" },
126     { eftXDR, ".edr", "ener",   NULL, "Energy file"},
127     { eftGEN, ".???", "conf", "-c", "Structure file", NSTXS, stxs },
128     { eftGEN, ".???", "out", "-o", "Structure file", NSTOS, stos },
129     { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
130     { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
131     { eftASC, ".pdb", "eiwit",  "-f", "Protein data bank file"},
132     { eftASC, ".brk", "eiwit",  "-f", "Brookhaven data bank file"},
133     { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
134     { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
135     { eftASC, ".pqr", "state",  "-o", "Coordinate file for MEAD"},
136     { eftXDR, ".cpt", "state",  "-cp", "Checkpoint file"},
137     { eftASC, ".log", "run",    "-l", "Log file"},
138     { eftASC, ".xvg", "graph",  "-o", "xvgr/xmgr file"},
139     { eftASC, ".out", "hello",  "-o", "Generic output file"},
140     { eftASC, ".ndx", "index",  "-n", "Index file", },
141     { eftASC, ".top", "topol",  "-p", "Topology file"},
142     { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
143     { eftGEN, ".???", "topol", "-s", "Structure+mass(db)", NTPSS, tpss },
144     { eftXDR, ".tpr", "topol",  "-s", "Portable xdr run input file"},
145     { eftASC, ".tex", "doc",    "-o", "LaTeX file"},
146     { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
147     { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
148     { eftASC, ".hdb", "polar",  NULL, "Hydrogen data base"},
149     { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
150     { eftASC, ".dlg", "user",   NULL, "Dialog Box data for ngmx"},
151     { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
152     { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
153     { eftASC, ".mat", "ss",     NULL, "Matrix Data file"},
154     { eftASC, ".m2p", "ps",     NULL, "Input file for mat2ps"},
155     { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
156     { eftASC, ".edi", "sam",    NULL, "ED sampling input"},
157     { eftASC, ".cub", "pot",  NULL, "Gaussian cube file" },
158     { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
159     { eftASC, "", "rundir", NULL, "Run directory" }
160 };
161
162 const char *ftp2ext(int ftp)
163 {
164     if ((0 <= ftp) && (ftp < efNR))
165     {
166         return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
167     }
168     else
169     {
170         return "unknown";
171     }
172 }
173
174 const char *ftp2ext_generic(int ftp)
175 {
176     if ((0 <= ftp) && (ftp < efNR))
177     {
178         switch (ftp)
179         {
180             case efTRX:
181                 return "trx";
182             case efTRN:
183                 return "trn";
184             case efSTO:
185                 return "sto";
186             case efSTX:
187                 return "stx";
188             case efTPS:
189                 return "tps";
190             default:
191                 return ftp2ext(ftp);
192         }
193     }
194     else
195     {
196         return "unknown";
197     }
198 }
199
200 const char *ftp2ext_with_dot(int ftp)
201 {
202     if ((0 <= ftp) && (ftp < efNR))
203     {
204         return deffile[ftp].ext;
205     }
206     else
207     {
208         return "unknown";
209     }
210 }
211
212 int ftp2generic_count(int ftp)
213 {
214     if ((0 <= ftp) && (ftp < efNR))
215     {
216         return deffile[ftp].ntps;
217     }
218     else
219     {
220         return 0;
221     }
222 }
223
224 const int *ftp2generic_list(int ftp)
225 {
226     if ((0 <= ftp) && (ftp < efNR))
227     {
228         return deffile[ftp].tps;
229     }
230     else
231     {
232         return 0;
233     }
234 }
235
236 const char *ftp2desc(int ftp)
237 {
238     if ((0 <= ftp) && (ftp < efNR))
239     {
240         return deffile[ftp].descr;
241     }
242     else
243     {
244         return "unknown filetype";
245     }
246 }
247
248 gmx_bool ftp_is_text(int ftp)
249 {
250     if ((ftp >= 0) && (ftp < efNR))
251     {
252         return deffile[ftp].ftype == eftASC;
253     }
254     return FALSE;
255 }
256
257 gmx_bool ftp_is_xdr(int ftp)
258 {
259     if ((ftp >= 0) && (ftp < efNR))
260     {
261         return deffile[ftp].ftype == eftXDR;
262     }
263     return FALSE;
264 }
265
266 const char *ftp2defnm(int ftp)
267 {
268     if ((0 <= ftp) && (ftp < efNR))
269     {
270         return deffile[ftp].defnm;
271     }
272     else
273     {
274         return NULL;
275     }
276 }
277
278 const char *ftp2defopt(int ftp)
279 {
280     if ((0 <= ftp) && (ftp < efNR))
281     {
282         return deffile[ftp].defopt;
283     }
284     else
285     {
286         return NULL;
287     }
288 }
289
290 int fn2ftp(const char *fn)
291 {
292     int         i, len;
293     const char *feptr;
294     const char *eptr;
295
296     if (!fn)
297     {
298         return efNR;
299     }
300
301     len = std::strlen(fn);
302     if ((len >= 4) && (fn[len - 4] == '.'))
303     {
304         feptr = &(fn[len - 4]);
305     }
306     else
307     {
308         return efNR;
309     }
310
311     for (i = 0; (i < efNR); i++)
312     {
313         if ((eptr = deffile[i].ext) != NULL)
314         {
315             if (gmx_strcasecmp(feptr, eptr) == 0)
316             {
317                 break;
318             }
319         }
320     }
321
322     return i;
323 }