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
mod qcprot;
use gchemol_gut::prelude::*;
use vecfx::Matrix3f;
use vecfx::Vector3f;
#[derive(Clone, Copy, Debug)]
pub enum SuperpositionAlgo {
QCP,
Quaternion,
}
impl Default for SuperpositionAlgo {
fn default() -> Self {
SuperpositionAlgo::QCP
}
}
#[derive(Clone, Debug)]
pub struct Superposition {
pub rmsd: f64,
pub translation: Vector3f,
pub rotation_matrix: Matrix3f,
}
impl Superposition {
pub fn apply(&self, conf: &[[f64; 3]]) -> Vec<[f64; 3]> {
let mut res = Vec::with_capacity(conf.len());
for &v in conf {
let v = Vector3f::from(v);
let v = self.rotation_matrix * v + self.translation;
res.push(v.into());
}
res
}
}
#[derive(Clone, Debug)]
pub struct Alignment<'a> {
positions: &'a [[f64; 3]],
pub algorithm: SuperpositionAlgo,
}
impl<'a> Alignment<'a> {
pub fn new(positions: &'a [[f64; 3]]) -> Self {
Alignment {
positions,
algorithm: SuperpositionAlgo::default(),
}
}
pub fn rmsd(&self, reference: &[[f64; 3]], weights: Option<&[f64]>) -> Result<f64> {
let npts = self.positions.len();
if reference.len() != npts {
bail!("points size mismatch!");
}
if weights.is_some() && weights.unwrap().len() != npts {
bail!("weights size mismatch!");
}
let mut ws = 0.0f64;
for i in 0..npts {
let wi = weights.map_or_else(|| 1.0, |w| w[i]);
let dx = wi * (self.positions[i][0] - reference[i][0]);
let dy = wi * (self.positions[i][1] - reference[i][1]);
let dz = wi * (self.positions[i][2] - reference[i][2]);
ws += dx.powi(2) + dy.powi(2) + dz.powi(2);
}
let ws = ws.sqrt();
Ok(ws)
}
pub fn superpose(&mut self, reference: &[[f64; 3]], weights: Option<&[f64]>) -> Result<Superposition> {
let (rmsd, trans, rot) = match self.algorithm {
SuperpositionAlgo::QCP => self::qcprot::calc_rmsd_rotational_matrix(&reference, &self.positions, weights),
SuperpositionAlgo::Quaternion => {
todo!()
}
};
let rotation_matrix = if let Some(rot) = rot {
Matrix3f::from_row_slice(&rot)
} else {
Matrix3f::identity()
};
let sp = Superposition {
rmsd,
translation: trans.into(),
rotation_matrix,
};
Ok(sp)
}
}
#[test]
fn test_alignment() {
use approx::*;
let (reference, candidate, weights) = qcprot::prepare_test_data();
let mut align = Alignment::new(&candidate);
let sp = align.superpose(&reference, Some(&weights)).unwrap();
let rot = sp.rotation_matrix;
let rot_expected = Matrix3f::from_row_slice(&[
0.77227551,
0.63510272,
-0.01533190,
-0.44544846,
0.52413614,
-0.72584914,
-0.45295276,
0.56738509,
0.68768304,
]);
assert_relative_eq!(rot_expected, rot, epsilon = 1e-4);
}