faaefc5773a77b12c7289d99fb48c91513739acc
[alexxy/gromacs.git] / src / gromacs / fileio / mrcdensitymapheader.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019, 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  * Implement mrc/ccp4-file metadata.
38  *
39  * \author Christian Blau <blau@kth.se>
40  *
41  * \ingroup module_fileio
42  */
43 #include "gmxpre.h"
44
45 #include "mrcdensitymapheader.h"
46
47 #include <algorithm>
48
49 #include "gromacs/utility/exceptions.h"
50
51 namespace gmx
52 {
53
54 namespace
55 {
56
57 //! Returns true if any of the argument values is smaller than zero.
58 template<typename Container>
59 bool anySmallerZero(Container values)
60 {
61     return std::any_of(std::begin(values), std::end(values), [](auto v) { return v < 0; });
62 }
63
64 //! Returns true if any of the argument values is larger than a given boundary value.
65 template<typename Container>
66 bool anyLargerThanValue(Container values, typename Container::value_type boundaryValue)
67 {
68     return std::any_of(std::begin(values), std::end(values),
69                        [boundaryValue](auto v) { return v > boundaryValue; });
70 }
71
72 } // namespace
73
74 size_t numberOfExpectedDataItems(const MrcDensityMapHeader& header)
75 {
76     if (anySmallerZero(header.numColumnRowSection_))
77     {
78         GMX_THROW(
79                 InternalError("Cannot determine data size, because the mrc "
80                               "density map header is invalid (Negative number "
81                               "describing data extent in at least one dimension)."));
82     }
83
84     return header.numColumnRowSection_[XX] * header.numColumnRowSection_[YY]
85            * header.numColumnRowSection_[ZZ];
86 }
87
88 TranslateAndScale getCoordinateTransformationToLattice(const MrcDensityMapHeader& header)
89 {
90     constexpr real c_AAtoNmConversion = 0.1;
91
92     RVec       scale = { header.extent_[XX] / (header.cellLength_[XX] * c_AAtoNmConversion),
93                    header.extent_[YY] / (header.cellLength_[YY] * c_AAtoNmConversion),
94                    header.extent_[ZZ] / (header.cellLength_[ZZ] * c_AAtoNmConversion) };
95     const RVec emdbOrigin{ header.userDefinedFloat_[12], header.userDefinedFloat_[13],
96                            header.userDefinedFloat_[14] };
97     RVec       translation;
98     if (emdbOrigin[XX] == 0. && emdbOrigin[YY] == 0. && emdbOrigin[ZZ] == 0.)
99     {
100         translation = RVec{ -header.columnRowSectionStart_[XX] / scale[XX],
101                             -header.columnRowSectionStart_[YY] / scale[YY],
102                             -header.columnRowSectionStart_[ZZ] / scale[ZZ] };
103     }
104     else
105     {
106         translation = { -emdbOrigin[XX] * c_AAtoNmConversion, -emdbOrigin[YY] * c_AAtoNmConversion,
107                         -emdbOrigin[ZZ] * c_AAtoNmConversion };
108     }
109     return { scale, translation };
110 }
111
112 dynamicExtents3D getDynamicExtents3D(const MrcDensityMapHeader& header)
113 {
114     return { header.numColumnRowSection_[ZZ], header.numColumnRowSection_[YY],
115              header.numColumnRowSection_[XX] };
116 };
117
118 bool mrcHeaderIsSane(const MrcDensityMapHeader& header)
119 {
120     // Make sure all numbers of columns, row sections, extents and cell angles
121     // are positive
122     if (anySmallerZero(header.numColumnRowSection_) || anySmallerZero(header.cellAngles_)
123         || anySmallerZero(header.extent_))
124     {
125         return false;
126     }
127
128     // The maximum integer number in an mrc header to be considered sane
129     constexpr std::int32_t c_maxIntegerNumber = 100'000;
130     if (anyLargerThanValue(header.numColumnRowSection_, c_maxIntegerNumber)
131         || anyLargerThanValue(header.extent_, c_maxIntegerNumber))
132     {
133         return false;
134     }
135
136     constexpr std::int32_t c_maxCellAngle = 360;
137     if (anyLargerThanValue(header.cellAngles_, c_maxCellAngle))
138     {
139         return false; //NOLINT(readability-simplify-boolean-expr)
140     }
141
142     return true;
143 }
144
145 } // namespace gmx