2351c67956da4f35fd443851b13ab270cb99fccb
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / tests / pdb2gmx.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Tests for pdb2gmx
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  */
41
42 #include "gmxpre.h"
43
44 #include "gromacs/gmxpreprocess/pdb2gmx.h"
45
46 #include <tuple>
47
48 #include "gromacs/fileio/filetypes.h"
49 #include "gromacs/utility/futil.h"
50 #include "gromacs/utility/stringutil.h"
51 #include "gromacs/utility/textreader.h"
52
53 #include "testutils/cmdlinetest.h"
54 #include "testutils/conftest.h"
55 #include "testutils/filematchers.h"
56 #include "testutils/refdata.h"
57 #include "testutils/testfilemanager.h"
58 #include "testutils/textblockmatchers.h"
59
60 namespace gmx
61 {
62 namespace test
63 {
64 namespace
65 {
66
67 using test::CommandLine;
68
69 //! Test parameter struct.
70 using CommandLineOptionParams =
71         std::tuple<std::string, std::string, std::string, std::string, std::string, std::string, int, bool>;
72
73 /*! \brief Strings containing regular expressions for lines to skip
74  * when matching.
75  *
76  * \todo It would be preferable to just scrub the content that actually
77  * varies, but we don't use enough regular expression support for that
78  * yet.
79  *
80  * Note that the "\n" are needed so these regular expressions match
81  * Windows line endings. */
82 std::vector<std::string> c_regexStringsToSkip = { "^;[[:blank:]] *File '.*' was generated.*\n",
83                                                   "^;[[:blank:]]*By user:.*\n",
84                                                   "^;[[:blank:]]*On host:.*\n",
85                                                   "^;[[:blank:]]*At date:.*\n",
86                                                   "^;[[:blank:]]*:-\\).*\\(-:.*\n",
87                                                   "^;[[:blank:]]*Executable:.*\n",
88                                                   "^;[[:blank:]]*Data prefix:.*\n",
89                                                   "^;[[:blank:]]*Working dir:.*\n",
90                                                   "^;[[:blank:]]*pdb2gmx.*-test.*\n" };
91 //! Compiled regular expressions for lines to skip when matching.
92 FilteringExactTextMatch c_textMatcher(c_regexStringsToSkip);
93
94 class Pdb2gmxTest : public test::CommandLineTestBase, public ::testing::WithParamInterface<CommandLineOptionParams>
95 {
96 public:
97     Pdb2gmxTest()
98     {
99         int outputFileType = std::get<6>(GetParam());
100         // If the file type of the output configuration is one
101         // commonly used (ie. pdb, gro), then check its content.
102         if (outputFileType == efPDB)
103         {
104             // If we're writing PDB output, we are interested in
105             // testing things like TER records and chain IDs.
106             std::string outputfile = "conf.";
107             outputfile += ftp2ext(outputFileType);
108             ExactTextMatch settings;
109             setOutputFile("-o", outputfile.c_str(), TextFileMatch(settings));
110         }
111         else if (outputFileType == efGRO)
112         {
113             setOutputFile("-o", "conf.gro", ConfMatch().matchFullConfiguration(std::get<7>(GetParam())));
114         }
115         setOutputFile("-p", "topol.top", TextFileMatch(c_textMatcher));
116     }
117
118     void runTest(const CommandLine& args)
119     {
120         CommandLine& cmdline = commandLine();
121         cmdline.merge(args);
122
123         TestReferenceChecker rootChecker(this->rootChecker());
124
125         ASSERT_EQ(0, CommandLineTestHelper::runModuleFactory(&pdb2gmxInfo::create, &cmdline));
126
127         checkOutputFiles();
128     }
129 };
130
131 TEST_P(Pdb2gmxTest, ProducesMatchingTopology)
132 {
133     const auto& params    = GetParam();
134     std::string cmdline[] = { "pdb2gmx",   "-ignh",
135                               "-ff",       std::get<0>(params),
136                               "-water",    std::get<1>(params),
137                               "-vsite",    std::get<2>(params),
138                               "-chainsep", std::get<3>(params),
139                               "-merge",    std::get<4>(params) };
140     setInputFile("-f", std::get<5>(params));
141     runTest(CommandLine(cmdline));
142 }
143
144 //! Help GoogleTest name our test cases
145 std::string namesOfTests(const testing::TestParamInfo<Pdb2gmxTest::ParamType>& info)
146 {
147     const auto& param = info.param;
148
149     std::string testName = formatString(
150             "ff_%s_"
151             "water_%s_"
152             "vsite_%s_"
153             "chainsep_%s_"
154             "merge_%s_"
155             "input_%s_"
156             "format_%s_"
157             "matchfullconfiguration_%s_",
158             std::get<0>(param).c_str(),
159             std::get<1>(param).c_str(),
160             std::get<2>(param).c_str(),
161             std::get<3>(param).c_str(),
162             std::get<4>(param).c_str(),
163             std::get<5>(param).c_str(),
164             ftp2ext(std::get<6>(param)),
165             std::get<7>(param) ? "true" : "false");
166
167     // Note that the returned names must be unique and may use only
168     // alphanumeric ASCII characters. It's not supposed to contain
169     // underscores (see the GoogleTest FAQ
170     // why-should-test-suite-names-and-test-names-not-contain-underscore),
171     // but doing so works for now, is likely to remain so, and makes
172     // such test names much more readable.
173     testName = replaceAll(testName, "-", "");
174     testName = replaceAll(testName, ".", "");
175     return testName;
176 }
177
178 // These tests are still rather slow when run with TSAN, so in the
179 // CMakeLists.txt file we split them into separtae test binaries.
180
181 #if OPLSAA
182 INSTANTIATE_TEST_CASE_P(ForOplsaa,
183                         Pdb2gmxTest,
184                         ::testing::Combine(::testing::Values("oplsaa"),
185                                            ::testing::Values("tip3p", "tip4p", "tip5p"),
186                                            ::testing::Values("none", "h"),
187                                            ::testing::Values("id_or_ter"),
188                                            ::testing::Values("no"),
189                                            ::testing::Values("fragment1.pdb",
190                                                              "fragment2.pdb",
191                                                              "fragment3.pdb",
192                                                              "fragment4.pdb"),
193                                            ::testing::Values(efGRO),
194                                            ::testing::Values(false)),
195                         namesOfTests);
196 #endif
197
198 #if GROMOS
199 INSTANTIATE_TEST_CASE_P(ForGromos43a1,
200                         Pdb2gmxTest,
201                         ::testing::Combine(::testing::Values("gromos43a1"),
202                                            ::testing::Values("spc", "spce"),
203                                            ::testing::Values("none", "h"),
204                                            ::testing::Values("id_or_ter"),
205                                            ::testing::Values("no"),
206                                            ::testing::Values("fragment1.pdb",
207                                                              "fragment2.pdb",
208                                                              "fragment3.pdb",
209                                                              "fragment4.pdb"),
210                                            ::testing::Values(efGRO),
211                                            ::testing::Values(false)),
212                         namesOfTests);
213
214 INSTANTIATE_TEST_CASE_P(ForGromos53a6,
215                         Pdb2gmxTest,
216                         ::testing::Combine(::testing::Values("gromos53a6"),
217                                            ::testing::Values("spc", "spce"),
218                                            ::testing::Values("none", "h"),
219                                            ::testing::Values("id_or_ter"),
220                                            ::testing::Values("no"),
221                                            ::testing::Values("fragment1.pdb",
222                                                              "fragment2.pdb",
223                                                              "fragment3.pdb",
224                                                              "fragment4.pdb"),
225                                            ::testing::Values(efGRO),
226                                            ::testing::Values(false)),
227                         namesOfTests);
228 #endif
229
230 #if AMBER
231 INSTANTIATE_TEST_CASE_P(ForAmber99sb_ildn,
232                         Pdb2gmxTest,
233                         ::testing::Combine(::testing::Values("amber99sb-ildn"),
234                                            ::testing::Values("tip3p"),
235                                            ::testing::Values("none", "h"),
236                                            ::testing::Values("id_or_ter"),
237                                            ::testing::Values("no"),
238                                            ::testing::Values("fragment1.pdb",
239                                                              "fragment2.pdb",
240                                                              "fragment3.pdb",
241                                                              "fragment4.pdb"),
242                                            ::testing::Values(efGRO),
243                                            ::testing::Values(false)),
244                         namesOfTests);
245 INSTANTIATE_TEST_CASE_P(ForAmber99sb_ildnWithTip4p,
246                         Pdb2gmxTest,
247                         ::testing::Combine(::testing::Values("amber99sb-ildn"),
248                                            ::testing::Values("tip4p"),
249                                            ::testing::Values("none"),
250                                            ::testing::Values("id_or_ter"),
251                                            ::testing::Values("no"),
252                                            ::testing::Values("tip4p.pdb"),
253                                            ::testing::Values(efGRO),
254                                            ::testing::Values(true)),
255                         namesOfTests);
256 #endif
257
258 #if CHARMM
259 INSTANTIATE_TEST_CASE_P(ForCharmm27,
260                         Pdb2gmxTest,
261                         ::testing::Combine(::testing::Values("charmm27"),
262                                            ::testing::Values("tip3p"),
263                                            ::testing::Values("none", "h"),
264                                            ::testing::Values("id_or_ter"),
265                                            ::testing::Values("no"),
266                                            ::testing::Values("fragment1.pdb",
267                                                              "fragment2.pdb",
268                                                              "fragment3.pdb",
269                                                              "fragment4.pdb",
270                                                              "single-residue.pdb"),
271                                            ::testing::Values(efGRO),
272                                            ::testing::Values(false)),
273                         namesOfTests);
274
275
276 INSTANTIATE_TEST_CASE_P(ChainSep,
277                         Pdb2gmxTest,
278                         ::testing::Combine(::testing::Values("charmm27"),
279                                            ::testing::Values("tip3p"),
280                                            ::testing::Values("none"),
281                                            ::testing::Values("id", "ter", "id_or_ter", "id_and_ter"),
282                                            ::testing::Values("all", "no"),
283                                            ::testing::Values("chainTer.pdb"),
284                                            ::testing::Values(efGRO),
285                                            ::testing::Values(false)),
286                         namesOfTests);
287
288 INSTANTIATE_TEST_CASE_P(ChainChanges,
289                         Pdb2gmxTest,
290                         ::testing::Combine(::testing::Values("charmm27"),
291                                            ::testing::Values("tip3p"),
292                                            ::testing::Values("none"),
293                                            ::testing::Values("id", "ter", "id_or_ter", "id_and_ter"),
294                                            ::testing::Values("no"),
295                                            ::testing::Values("two-fragments.pdb"),
296                                            ::testing::Values(efPDB),
297                                            ::testing::Values(false)),
298                         namesOfTests);
299
300 INSTANTIATE_TEST_CASE_P(ForCharmm27CyclicSystem,
301                         Pdb2gmxTest,
302                         ::testing::Combine(::testing::Values("charmm27"),
303                                            ::testing::Values("tip3p"),
304                                            ::testing::Values("none"),
305                                            ::testing::Values("id_or_ter"),
306                                            ::testing::Values("no", "all"),
307                                            ::testing::Values("cyclic-rna.pdb",
308                                                              "cyclic-protein-small.pdb"),
309                                            ::testing::Values(efGRO),
310                                            ::testing::Values(false)),
311                         namesOfTests);
312 #endif
313
314 } // namespace
315 } // namespace test
316 } // namespace gmx