2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * Implement mrc/ccp4-file metadata.
39 * \author Christian Blau <blau@kth.se>
41 * \ingroup module_fileio
45 #include "mrcdensitymapheader.h"
49 #include "gromacs/utility/exceptions.h"
57 //! Returns true if any of the argument values is smaller than zero.
58 template<typename Container>
59 bool anySmallerZero(Container values)
61 return std::any_of(std::begin(values), std::end(values), [](auto v) { return v < 0; });
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)
68 return std::any_of(std::begin(values), std::end(values),
69 [boundaryValue](auto v) { return v > boundaryValue; });
74 size_t numberOfExpectedDataItems(const MrcDensityMapHeader& header)
76 if (anySmallerZero(header.numColumnRowSection_))
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)."));
84 return header.numColumnRowSection_[XX] * header.numColumnRowSection_[YY]
85 * header.numColumnRowSection_[ZZ];
88 TranslateAndScale getCoordinateTransformationToLattice(const MrcDensityMapHeader& header)
90 constexpr real c_AAtoNmConversion = 0.1;
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] };
98 if (emdbOrigin[XX] == 0. && emdbOrigin[YY] == 0. && emdbOrigin[ZZ] == 0.)
100 translation = RVec{ -header.columnRowSectionStart_[XX] / scale[XX],
101 -header.columnRowSectionStart_[YY] / scale[YY],
102 -header.columnRowSectionStart_[ZZ] / scale[ZZ] };
106 translation = { -emdbOrigin[XX] * c_AAtoNmConversion, -emdbOrigin[YY] * c_AAtoNmConversion,
107 -emdbOrigin[ZZ] * c_AAtoNmConversion };
109 return { scale, translation };
112 dynamicExtents3D getDynamicExtents3D(const MrcDensityMapHeader& header)
114 return { header.numColumnRowSection_[ZZ], header.numColumnRowSection_[YY],
115 header.numColumnRowSection_[XX] };
118 bool mrcHeaderIsSane(const MrcDensityMapHeader& header)
120 // Make sure all numbers of columns, row sections, extents and cell angles
122 if (anySmallerZero(header.numColumnRowSection_) || anySmallerZero(header.cellAngles_)
123 || anySmallerZero(header.extent_))
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))
136 constexpr std::int32_t c_maxCellAngle = 360;
137 if (anyLargerThanValue(header.cellAngles_, c_maxCellAngle))
139 return false; //NOLINT(readability-simplify-boolean-expr)