6729446ecd33dc47f4bdba90956301823a41bda5
[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,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include "gmxpre.h"
39
40 #include "filetypes.h"
41
42 #include <cstring>
43
44 #include "gromacs/utility/arraysize.h"
45 #include "gromacs/utility/cstringutil.h"
46
47 enum
48 {
49     eftASC,
50     eftXDR,
51     eftTNG,
52     eftGEN,
53     eftNR
54 };
55
56 /* To support multiple file types with one general (eg TRX) we have
57  * these arrays.
58  */
59 static const int trxs[] = { efXTC, efTRR, efCPT, efGRO, efG96, efPDB, efTNG };
60 #define NTRXS asize(trxs)
61
62 static const int trcompressed[] = { efXTC, efTNG };
63 #define NTRCOMPRESSED asize(trcompressed)
64
65 static const int tros[] = { efXTC, efTRR, efGRO, efG96, efPDB, efTNG };
66 #define NTROS asize(tros)
67
68 static const int trns[] = { efTRR, efCPT, efTNG };
69 #define NTRNS asize(trns)
70
71 static const int stos[] = { efGRO, efG96, efPDB, efBRK, efENT, efESP };
72 #define NSTOS asize(stos)
73
74 static const int stxs[] = { efGRO, efG96, efPDB, efBRK, efENT, efESP, efTPR };
75 #define NSTXS asize(stxs)
76
77 static const int tpss[] = { efTPR, efGRO, efG96, efPDB, efBRK, efENT };
78 #define NTPSS asize(tpss)
79
80 typedef struct // NOLINT(clang-analyzer-optin.performance.Padding)
81 {
82     int         ftype;
83     const char* ext;
84     const char* defnm;
85     const char* defopt;
86     const char* descr;
87     int         ntps;
88     const int*  tps;
89 } t_deffile;
90
91 /* this array should correspond to the enum in filetypes.h */
92 static const t_deffile deffile[efNR] = {
93     { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
94     { eftGEN, ".???", "traj", "-f", "Trajectory", NTRXS, trxs },
95     { eftGEN, ".???", "trajout", "-f", "Trajectory", NTROS, tros },
96     { eftGEN, ".???", "traj", nullptr, "Full precision trajectory", NTRNS, trns },
97     { eftXDR, ".trr", "traj", nullptr, "Trajectory in portable xdr format" },
98     { eftGEN, ".???", "traj_comp", nullptr,
99       "Compressed trajectory (tng format or portable xdr format)", NTRCOMPRESSED, trcompressed },
100     { eftXDR, ".xtc", "traj", nullptr, "Compressed trajectory (portable xdr format): xtc" },
101     { eftTNG, ".tng", "traj", nullptr, "Trajectory file (tng format)" },
102     { eftXDR, ".edr", "ener", nullptr, "Energy file" },
103     { eftGEN, ".???", "conf", "-c", "Structure file", NSTXS, stxs },
104     { eftGEN, ".???", "out", "-o", "Structure file", NSTOS, stos },
105     { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
106     { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
107     { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file" },
108     { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file" },
109     { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
110     { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
111     { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD" },
112     { eftXDR, ".cpt", "state", "-cp", "Checkpoint file" },
113     { eftASC, ".log", "run", "-l", "Log file" },
114     { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file" },
115     { eftASC, ".out", "hello", "-o", "Generic output file" },
116     {
117             eftASC,
118             ".ndx",
119             "index",
120             "-n",
121             "Index file",
122     },
123     { eftASC, ".top", "topol", "-p", "Topology file" },
124     { eftASC, ".itp", "topinc", nullptr, "Include file for topology" },
125     { eftGEN, ".???", "topol", "-s", "Structure+mass(db)", NTPSS, tpss },
126     { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file" },
127     { eftASC, ".tex", "doc", "-o", "LaTeX file" },
128     { eftASC, ".rtp", "residue", nullptr, "Residue Type file used by pdb2gmx" },
129     { eftASC, ".atp", "atomtp", nullptr, "Atomtype file used by pdb2gmx" },
130     { eftASC, ".hdb", "polar", nullptr, "Hydrogen data base" },
131     { eftASC, ".dat", "nnnice", nullptr, "Generic data file" },
132     { eftASC, ".dlg", "user", nullptr, "Dialog Box data for ngmx" },
133     { eftASC, ".map", "ss", nullptr, "File that maps matrix data to colors" },
134     { eftASC, ".eps", "plot", nullptr, "Encapsulated PostScript (tm) file" },
135     { eftASC, ".mat", "ss", nullptr, "Matrix Data file" },
136     { eftASC, ".m2p", "ps", nullptr, "Input file for mat2ps" },
137     { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix" },
138     { eftASC, ".edi", "sam", nullptr, "ED sampling input" },
139     { eftASC, ".cub", "pot", nullptr, "Gaussian cube file" },
140     { eftASC, ".xpm", "root", nullptr, "X PixMap compatible matrix file" },
141     { eftASC, "", "rundir", nullptr, "Run directory" }
142 };
143
144 const char* ftp2ext(int ftp)
145 {
146     if ((0 <= ftp) && (ftp < efNR))
147     {
148         return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
149     }
150     else
151     {
152         return "unknown";
153     }
154 }
155
156 const char* ftp2ext_generic(int ftp)
157 {
158     if ((0 <= ftp) && (ftp < efNR))
159     {
160         switch (ftp)
161         {
162             case efTRX: return "trx";
163             case efTRN: return "trn";
164             case efSTO: return "sto";
165             case efSTX: return "stx";
166             case efTPS: return "tps";
167             default: return ftp2ext(ftp);
168         }
169     }
170     else
171     {
172         return "unknown";
173     }
174 }
175
176 const char* ftp2ext_with_dot(int ftp)
177 {
178     if ((0 <= ftp) && (ftp < efNR))
179     {
180         return deffile[ftp].ext;
181     }
182     else
183     {
184         return "unknown";
185     }
186 }
187
188 int ftp2generic_count(int ftp)
189 {
190     if ((0 <= ftp) && (ftp < efNR))
191     {
192         return deffile[ftp].ntps;
193     }
194     else
195     {
196         return 0;
197     }
198 }
199
200 const int* ftp2generic_list(int ftp)
201 {
202     if ((0 <= ftp) && (ftp < efNR))
203     {
204         return deffile[ftp].tps;
205     }
206     else
207     {
208         return nullptr;
209     }
210 }
211
212 const char* ftp2desc(int ftp)
213 {
214     if ((0 <= ftp) && (ftp < efNR))
215     {
216         return deffile[ftp].descr;
217     }
218     else
219     {
220         return "unknown filetype";
221     }
222 }
223
224 gmx_bool ftp_is_text(int ftp)
225 {
226     if ((ftp >= 0) && (ftp < efNR))
227     {
228         return deffile[ftp].ftype == eftASC;
229     }
230     return FALSE;
231 }
232
233 gmx_bool ftp_is_xdr(int ftp)
234 {
235     if ((ftp >= 0) && (ftp < efNR))
236     {
237         return deffile[ftp].ftype == eftXDR;
238     }
239     return FALSE;
240 }
241
242 const char* ftp2defnm(int ftp)
243 {
244     if ((0 <= ftp) && (ftp < efNR))
245     {
246         return deffile[ftp].defnm;
247     }
248     else
249     {
250         return nullptr;
251     }
252 }
253
254 const char* ftp2defopt(int ftp)
255 {
256     if ((0 <= ftp) && (ftp < efNR))
257     {
258         return deffile[ftp].defopt;
259     }
260     else
261     {
262         return nullptr;
263     }
264 }
265
266 int fn2ftp(const char* fn)
267 {
268     int         i, len;
269     const char* feptr;
270     const char* eptr;
271
272     if (!fn)
273     {
274         return efNR;
275     }
276
277     len = std::strlen(fn);
278     if ((len >= 4) && (fn[len - 4] == '.'))
279     {
280         feptr = &(fn[len - 4]);
281     }
282     else
283     {
284         return efNR;
285     }
286
287     for (i = 0; (i < efNR); i++)
288     {
289         if ((eptr = deffile[i].ext) != nullptr)
290         {
291             if (gmx_strcasecmp(feptr, eptr) == 0)
292             {
293                 break;
294             }
295         }
296     }
297
298     return i;
299 }