NuriKit v0.1.0b2
Loading...
Searching...
No Matches
tm.h
Go to the documentation of this file.
1//
2// Project NuriKit - Copyright 2024 SNU Compbio Lab.
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef NURI_TOOLS_TM_H_
7#define NURI_TOOLS_TM_H_
8
10#include <cstdint>
11#include <utility>
12
13#include <absl/base/attributes.h>
14#include <Eigen/Dense>
16
17#include "nuri/eigen_config.h"
18
19namespace nuri {
20namespace internal {
21 class AlignedXY;
22
23 template <class Pred>
24 void remap_helper(AlignedXY &xy, const Pred &pred) noexcept;
25
26 class AlignedXY {
27 public:
28 AlignedXY(ConstRef<Matrix3Xd> x, ConstRef<Matrix3Xd> y, const int l_min)
29 : x_(x), y_(y), xtm_(3, l_min), ytm_(3, l_min), y2x_(y.cols()),
30 l_ali_(0) { }
31
32 void remap(ConstRef<ArrayXi> y2x) noexcept;
33
34 void remap(ArrayXi &&y2x) noexcept;
35
36 void remap_final(ConstRef<Matrix3Xd> x_aln, double score_d8sq) noexcept;
37
38 void swap_remap(ArrayXi &y2x) noexcept;
39
40 void swap_align_with(ArrayXi &y2x) noexcept;
41
42 void reset() noexcept {
43 y2x_.setConstant(-1);
44 l_ali_ = 0;
45 }
46
47 ConstRef<Matrix3Xd> x() const { return x_; }
48
49 auto xtm() { return xtm_.leftCols(l_ali_); }
50 auto xtm() const { return xtm_.leftCols(l_ali_); }
51
52 ConstRef<Matrix3Xd> y() const { return y_; }
53
54 auto ytm() { return ytm_.leftCols(l_ali_); }
55 auto ytm() const { return ytm_.leftCols(l_ali_); }
56
57 int l_ali() const { return l_ali_; }
58
59 int l_min() const { return static_cast<int>(xtm_.cols()); }
60
61 ArrayXi &y2x() { return y2x_; }
62 const ArrayXi &y2x() const { return y2x_; }
63
64 private:
65 template <class Pred>
66 friend void remap_helper(AlignedXY &xy, const Pred &pred) noexcept;
67
68 Eigen::Ref<const Matrix3Xd> x_;
69 Eigen::Ref<const Matrix3Xd> y_;
70
71 Matrix3Xd xtm_;
72 Matrix3Xd ytm_;
73 ArrayXi y2x_;
74
75 int l_ali_;
76 };
77
78 enum class SecStr : char {
79 kCoil = 'C',
80 kHelix = 'H',
81 kStrand = 'E',
82 kTurn = 'T',
83 };
84
93 extern SecStr assign_secstr_approx(ConstRef<Array33d> dists);
94
105 extern ArrayXc assign_secstr_approx_full(ConstRef<Matrix3Xd> pts,
106 Matrix3Xd &buf);
107
108 ABSL_MUST_USE_RESULT
109 extern double tm_initial_gt(Matrix3Xd &rx, Matrix3Xd &ry, ArrayXd &dsqs,
110 ConstRef<Matrix3Xd> x, ConstRef<Matrix3Xd> y,
111 ArrayXi &y2x, double d0sq_inv,
112 double d0sq_search);
113
114 extern void tm_initial_ss(ArrayXi &y2x, ArrayXXc &path, ArrayXXd &val,
115 ConstRef<ArrayXc> secx, ConstRef<ArrayXc> secy);
116
117 ABSL_MUST_USE_RESULT
118 extern double tm_initial_local(Matrix3Xd &rx, Matrix3Xd &ry, ArrayXd &dsqs,
119 ArrayXXc &path, ArrayXXd &val, AlignedXY &xy,
120 ArrayXi &y2x, ArrayXi &buf, double d0sq_inv,
121 double d01sq_inv, double d0sq_search);
122
123 ABSL_MUST_USE_RESULT
124 extern bool tm_initial_ssplus(Matrix3Xd &rx, Matrix3Xd &ry, ArrayXXc &path,
125 ArrayXXd &val, const AlignedXY &xy,
126 ArrayXi &y2x, ConstRef<ArrayXc> secx,
127 ConstRef<ArrayXc> secy, double d01sq_inv);
128
129 ABSL_MUST_USE_RESULT
130 extern double tm_initial_fgt(Matrix3Xd &rx, Matrix3Xd &ry, ArrayXd &dsqs,
131 ConstRef<Matrix3Xd> x, ConstRef<Matrix3Xd> y,
132 ArrayXi &y2x, double dcu0_sq, double d0sq_inv,
133 double d0sq_search);
134
135 extern double tm_realign_calculate_msd(AlignedXY &xy, Matrix3Xd &rx,
136 Matrix3Xd &ry, const Isometry3d &xform,
137 double score_d8sq,
138 bool keep_alignment);
139} // namespace internal
140
173class TMAlign {
174public:
178 enum class InitFlags : std::uint32_t {
179 kNone = 0x0,
180
183
186 kSecStr = 0x2,
187
191 kLocal = 0x4,
192
196
199
203 };
204
213 TMAlign(ConstRef<Matrix3Xd> query, ConstRef<Matrix3Xd> templ);
214
225 ABSL_MUST_USE_RESULT
227
239 ABSL_MUST_USE_RESULT
240 bool initialize(InitFlags flags, ConstRef<ArrayXc> secx,
241 ConstRef<ArrayXc> secy);
242
257 ABSL_MUST_USE_RESULT
258 bool initialize(ConstRef<ArrayXi> y2x, bool keep_alignment = true);
259
260 bool initialized() const { return xy_.l_ali() > 0; }
261
273 std::pair<Isometry3d, double> tm_score(int l_norm = -1, double d0 = -1);
274
281 const ArrayXi &templ_to_query() const & { return xy_.y2x(); }
282
290 ArrayXi &&templ_to_query() && { return std::move(xy_.y2x()); }
291
296 int l_ali() const { return xy_.l_ali(); }
297
302 double aligned_msd() const { return aligned_msd_; }
303
304private:
305 ConstRef<Matrix3Xd> query() const { return xy_.x(); }
306 ConstRef<Matrix3Xd> templ() const { return xy_.y(); }
307
308 int l_min() const { return l_minmax_.first; }
309 int l_max() const { return l_minmax_.second; }
310
311 ArrayXi &y2x_local() { return y2x_buf1_; }
312 ArrayXi &y2x_buf() { return y2x_buf2_; }
313
314 ArrayXi &i_ali() { return y2x_buf1_; }
315 ArrayXi &j_ali() { return y2x_buf2_; }
316
317 std::pair<int, int> l_minmax_;
318 internal::AlignedXY xy_;
319
320 Isometry3d best_xform_;
321 double aligned_msd_;
322
323 Matrix3Xd rx_, ry_;
324 ArrayXd dsqs_;
325 ArrayXi y2x_buf1_, y2x_buf2_;
326};
327
336 Isometry3d xform;
337
341
343 double msd;
344
346 double tm_score = -1;
347};
348
366extern TMAlignResult
367tm_align(ConstRef<Matrix3Xd> query, ConstRef<Matrix3Xd> templ,
369 int l_norm = -1, double d0 = -1);
370
388extern TMAlignResult
389tm_align(ConstRef<Matrix3Xd> query, ConstRef<Matrix3Xd> templ,
390 ConstRef<ArrayXc> secx, ConstRef<ArrayXc> secy,
392 int l_norm = -1, double d0 = -1);
393
417extern TMAlignResult tm_align(ConstRef<Matrix3Xd> query,
418 ConstRef<Matrix3Xd> templ, ConstRef<ArrayXi> y2x,
419 int l_norm = -1, double d0 = -1,
420 bool keep_alignment = true);
421
422// test utils
423namespace internal {
424 extern std::pair<int, double>
425 tmalign_score_fun8(const Matrix3Xd &x, const Matrix3Xd &y, ArrayXi &aligned,
426 double d_cutoff, double d0sq_inv,
427 double score_d8sq_cutoff);
428
429 extern std::pair<int, double>
430 tmalign_score_fun8(const Matrix3Xd &x, const Matrix3Xd &y, ArrayXi &aligned,
431 double d_cutoff, double d0sq_inv);
432
433 extern std::pair<Isometry3d, double>
434 tmalign_tmscore8_search(const AlignedXY &xy, int simplify_step,
435 double local_d0_search, double score_d8sq_cutoff,
436 double d0sq_inv);
437
438 extern void tmalign_dp_iter(Isometry3d &xform_best, double &tmscore_max,
439 AlignedXY &xy, ArrayXi &y2x_best, int g1, int g2,
440 int max_iter, int simplify_step,
441 double local_d0_search, double score_d8sq_cutoff,
442 double d0sq_inv);
443
444 extern double tmalign_get_score_fast(const Matrix3Xd &x, const Matrix3Xd &y,
445 double d0sq_inv, double d0_search);
446} // namespace internal
447} // namespace nuri
448
449#endif /* NURI_TOOLS_TM_H_ */
int l_ali() const
Get the length of the aligned region.
Definition tm.h:296
TMAlign(ConstRef< Matrix3Xd > query, ConstRef< Matrix3Xd > templ)
Prepare TM-align algorithm with the given structures.
InitFlags
Initialization flags for TM-align algorithm.
Definition tm.h:178
@ kNone
Definition tm.h:179
@ kDefault
Default initialization flags, combination of all initialization methods.
Definition tm.h:201
@ kGaplessThreading
Enable gapless threading.
Definition tm.h:182
@ kLocalPlusSecStr
Definition tm.h:195
@ kFragmentGaplessThreading
Enable fragment gapless threading.
Definition tm.h:198
@ kSecStr
Definition tm.h:186
@ kLocal
Definition tm.h:191
ABSL_MUST_USE_RESULT bool initialize(ConstRef< ArrayXi > y2x, bool keep_alignment=true)
Initialize the TM-align algorithm with user-provided alignment.
double aligned_msd() const
Get the mean square deviation of the aligned region.
Definition tm.h:302
bool initialized() const
Definition tm.h:260
ArrayXi && templ_to_query() &&
Final alignment of the structures (move version).
Definition tm.h:290
const ArrayXi & templ_to_query() const &
Final alignment of the structures.
Definition tm.h:281
std::pair< Isometry3d, double > tm_score(int l_norm=-1, double d0=-1)
Calculate TM-score using the current alignment.
ABSL_MUST_USE_RESULT bool initialize(InitFlags flags, ConstRef< ArrayXc > secx, ConstRef< ArrayXc > secy)
Initialize the TM-align algorithm with user-provided secondary structures.
ABSL_MUST_USE_RESULT bool initialize(InitFlags flags=InitFlags::kDefault)
Initialize the TM-align algorithm.
Definition crdgen.h:16
TMAlignResult tm_align(ConstRef< Matrix3Xd > query, ConstRef< Matrix3Xd > templ, TMAlign::InitFlags flags=TMAlign::InitFlags::kDefault, int l_norm=-1, double d0=-1)
Align two structures using TM-align algorithm.
Result of TM-align algorithm.
Definition tm.h:334
Isometry3d xform
The transformation matrix.
Definition tm.h:336
double msd
The mean square deviation of the aligned region.
Definition tm.h:343
ArrayXi templ_to_query
Definition tm.h:340
double tm_score
The TM-score of the alignment.
Definition tm.h:346