2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019,2020, 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), [boundaryValue](auto v) {
69 return v > boundaryValue;
75 size_t numberOfExpectedDataItems(const MrcDensityMapHeader& header)
77 if (anySmallerZero(header.numColumnRowSection_))
80 InternalError("Cannot determine data size, because the mrc "
81 "density map header is invalid (Negative number "
82 "describing data extent in at least one dimension)."));
85 return header.numColumnRowSection_[XX] * header.numColumnRowSection_[YY]
86 * header.numColumnRowSection_[ZZ];
89 TranslateAndScale getCoordinateTransformationToLattice(const MrcDensityMapHeader& header)
91 constexpr real c_AAtoNmConversion = 0.1;
93 RVec scale = { header.extent_[XX] / (header.cellLength_[XX] * c_AAtoNmConversion),
94 header.extent_[YY] / (header.cellLength_[YY] * c_AAtoNmConversion),
95 header.extent_[ZZ] / (header.cellLength_[ZZ] * c_AAtoNmConversion) };
96 const RVec emdbOrigin{ header.userDefinedFloat_[12],
97 header.userDefinedFloat_[13],
98 header.userDefinedFloat_[14] };
100 if (emdbOrigin[XX] == 0. && emdbOrigin[YY] == 0. && emdbOrigin[ZZ] == 0.)
102 translation = RVec{ -header.columnRowSectionStart_[XX] / scale[XX],
103 -header.columnRowSectionStart_[YY] / scale[YY],
104 -header.columnRowSectionStart_[ZZ] / scale[ZZ] };
108 translation = { -emdbOrigin[XX] * c_AAtoNmConversion,
109 -emdbOrigin[YY] * c_AAtoNmConversion,
110 -emdbOrigin[ZZ] * c_AAtoNmConversion };
112 return { scale, translation };
115 dynamicExtents3D getDynamicExtents3D(const MrcDensityMapHeader& header)
117 return { header.numColumnRowSection_[ZZ],
118 header.numColumnRowSection_[YY],
119 header.numColumnRowSection_[XX] };
122 bool mrcHeaderIsSane(const MrcDensityMapHeader& header)
124 // Make sure all numbers of columns, row sections, extents and cell angles
126 if (anySmallerZero(header.numColumnRowSection_) || anySmallerZero(header.cellAngles_)
127 || anySmallerZero(header.extent_))
132 // The maximum integer number in an mrc header to be considered sane
133 constexpr std::int32_t c_maxIntegerNumber = 100'000;
134 if (anyLargerThanValue(header.numColumnRowSection_, c_maxIntegerNumber)
135 || anyLargerThanValue(header.extent_, c_maxIntegerNumber))
140 constexpr std::int32_t c_maxCellAngle = 360;
141 if (anyLargerThanValue(header.cellAngles_, c_maxCellAngle))
143 return false; //NOLINT(readability-simplify-boolean-expr)