1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024 NKI/AVL, Netherlands Cancer Institute
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define CATCH_CONFIG_RUNNER
#include <catch2/catch_all.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include <stdexcept>
#include <filesystem>
#include <cif++.hpp>
#include "pdb-redo/AtomShape.hpp"
#include "pdb-redo/MapMaker.hpp"
#include "pdb-redo/Statistics.hpp"
#include "pdb-redo/DistanceMap.hpp"
#include "pdb-redo/Minimizer.hpp"
namespace fs = std::filesystem;
// --------------------------------------------------------------------
std::filesystem::path gTestDir = std::filesystem::current_path();
int main(int argc, char *argv[])
{
Catch::Session session; // There must be exactly one instance
// Build a new parser on top of Catch2's
#if CATCH22
using namespace Catch::clara;
#else
// Build a new parser on top of Catch2's
using namespace Catch::Clara;
#endif
auto cli = session.cli() // Get Catch2's command line parser
| Opt(gTestDir, "data-dir") // bind variable to a new option, with a hint string
["-D"]["--data-dir"] // the option names it will respond to
("The directory containing the data files"); // description string for the help output
// Now pass the new composite back to Catch2 so it uses that
session.cli(cli);
// Let Catch2 (using Clara) parse the command line
int returnCode = session.applyCommandLine(argc, argv);
if (returnCode != 0) // Indicates a command line error
return returnCode;
if (fs::exists(gTestDir / "minimal-components.cif"))
cif::compound_factory::instance().push_dictionary(gTestDir / "minimal-components.cif");
return session.run();
}
// --------------------------------------------------------------------
TEST_CASE("refine_0")
{
const fs::path example(gTestDir / ".." / "examples" / "1cbs.cif.gz");
cif::file file(example.string());
cif::mm::structure structure(file);
cif::crystal crystal(structure.get_datablock());
auto &chain = structure.polymers().front();
pdb_redo::MapMaker<float> mm;
float samplingRate = 0.75;
mm.loadMTZ(gTestDir / ".." / "examples" / "1cbs_map.mtz", samplingRate);
auto minimizer = pdb_redo::Minimizer::create(crystal, chain, 3, 3, mm.fb());
minimizer->printStats();
auto score = minimizer->refine(true);
std::cout << "minimizer score: " << score << '\n';
minimizer->printStats();
std::cout << std::string(cif::get_terminal_width(), '-') << '\n';
cif::file refFile(example.string());
cif::mm::structure reference(refFile);
auto &refChain = reference.polymers().front();
auto &atoms3 = chain.at(2).atoms();
auto &refAtoms3 = refChain.at(2).atoms();
CHECK(atoms3.size() == refAtoms3.size());
double d_sum = 0;
for (std::size_t i = 0; i < atoms3.size(); ++i)
{
auto a1 = atoms3.at(i);
auto a2 = refAtoms3.at(i);
auto d = distance(a1, a2);
d_sum += d * d;
std::cout << a1 << ": " << a1.get_location() << " => " << a2.get_location() << " distance: " << d << '\n';
}
auto rmsd = std::sqrt(d_sum / atoms3.size());
std::cout << "RMSd: " << rmsd << '\n';
CHECK_THAT(rmsd, Catch::Matchers::WithinAbs(0.125, 0.125));
std::cout << std::string(cif::get_terminal_width(), '=') << '\n';
}
TEST_CASE("refine_1")
{
const fs::path example(gTestDir / ".." / "examples" / "1cbs.cif.gz");
cif::file file(example.string());
cif::mm::structure structure(file);
cif::crystal crystal(structure.get_datablock());
pdb_redo::MapMaker<float> mm;
float samplingRate = 0.75;
mm.loadMTZ(gTestDir / ".." / "examples" / "1cbs_map.mtz", samplingRate);
auto &chain = structure.polymers().front();
auto &atoms3 = chain.at(2).atoms();
auto minimizer = pdb_redo::Minimizer::create(crystal, chain, 3, 3, mm.fb());
minimizer->printStats();
// nudge the atoms of the third residue
auto &res3 = chain.at(2);
for (auto a : res3.atoms())
structure.move_atom(a, cif::nudge(a.get_location(), 0.5f));
cif::file refFile(example.string());
cif::mm::structure reference(refFile);
auto &refChain = reference.polymers().front();
auto &refAtoms3 = refChain.at(2).atoms();
CHECK(atoms3.size() == refAtoms3.size());
for (std::size_t i = 0; i < atoms3.size(); ++i)
{
auto a1 = atoms3.at(i);
auto a2 = refAtoms3.at(i);
std::cout << a1 << ": " << a1.get_location() << " => " << a2.get_location() << " distance: " << distance(a1, a2) << '\n';
}
std::cout << std::string(cif::get_terminal_width(), '-') << '\n';
minimizer->printStats();
auto score = minimizer->refine(true);
std::cout << "minimizer score: " << score << '\n';
minimizer->printStats();
double d_sum = 0;
std::cout << std::string(cif::get_terminal_width(), '-') << '\n';
for (std::size_t i = 0; i < atoms3.size(); ++i)
{
auto a1 = atoms3.at(i);
auto a2 = refAtoms3.at(i);
auto d = distance(a1, a2);
d_sum += d * d;
std::cout << a1 << ": " << a1.get_location() << " => " << a2.get_location() << " distance: " << d << '\n';
}
auto rmsd = std::sqrt(d_sum / atoms3.size());
std::cout << "RMSd: " << rmsd << '\n';
CHECK(rmsd < 0.3);
std::cout << std::string(cif::get_terminal_width(), '=') << '\n';
}
TEST_CASE("refine_2")
{
const fs::path example(gTestDir / ".." / "examples" / "1cbs.cif.gz");
cif::file file(example.string());
cif::mm::structure structure(file);
const float kNearBy = 3;
pdb_redo::DistanceMap dm(structure, kNearBy);
// Move the REA residue somewhat
auto &rea = structure.get_residue("B");
std::vector<cif::mm::atom> atoms;
for (auto a : rea.atoms())
{
for (auto b : dm.near(a, kNearBy))
{
if (find(atoms.begin(), atoms.end(), b) != atoms.end())
continue;
atoms.push_back(b);
}
}
// // translate by { 0.1, 0.1, 0.1 } and then
// // rotate around 1, 0, 0 for 5 degrees
// const float angle = 5 * (cif::kPI / 180);
// cif::quaternion q(
// std::cos(angle / 2), std::sin(angle / 2), 0, 0
// );
// for (auto a : rea.atoms())
// a.translate_and_rotate({ 0.1, 0.1, 0.1 }, q);
for (auto a : rea.atoms())
{
auto l = a.get_location();
l = nudge(l, 0.5);
a.set_location(l);
}
cif::file refFile(example.string());
cif::mm::structure reference(refFile);
auto &atomsRea = rea.atoms();
auto &refAtomsRea = reference.get_residue("B").atoms();
CHECK(atomsRea.size() == refAtomsRea.size());
for (std::size_t i = 0; i < atomsRea.size(); ++i)
{
auto a1 = atomsRea.at(i);
auto a2 = refAtomsRea.at(i);
std::cout << a1 << ": " << a1.get_location() << " => " << a2.get_location() << " distance: " << distance(a1, a2) << '\n';
}
std::cout << std::string(cif::get_terminal_width(), '-') << '\n';
pdb_redo::MapMaker<float> mm;
float samplingRate = 0.75;
mm.loadMTZ(gTestDir / ".." / "examples" / "1cbs_map.mtz", samplingRate);
cif::crystal crystal(structure.get_datablock());
auto minimizer = pdb_redo::Minimizer::create(crystal, structure, atoms, mm.fb());
minimizer->printStats();
auto score = minimizer->refine(true);
std::cout << "minimizer score: " << score << '\n';
minimizer->printStats();
std::cout << std::string(cif::get_terminal_width(), '-') << '\n';
double d_sum = 0;
for (std::size_t i = 0; i < atomsRea.size(); ++i)
{
auto a1 = atomsRea.at(i);
auto a2 = refAtomsRea.at(i);
auto d = distance(a1, a2);
d_sum += d * d;
std::cout << a1 << ": " << a1.get_location() << " => " << a2.get_location() << " distance: " << d << '\n';
}
file.save("/tmp/rsr-test-3.cif");
auto rmsd = std::sqrt(d_sum / atomsRea.size());
std::cout << "RMSd: " << rmsd << '\n';
CHECK_THAT(rmsd, Catch::Matchers::WithinAbs(0.35, 0.35 / 2));
std::cout << std::string(cif::get_terminal_width(), '-') << '\n';
}
|