-
Notifications
You must be signed in to change notification settings - Fork 0
/
kabsch.go
207 lines (161 loc) · 4.55 KB
/
kabsch.go
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
package main
import (
"fmt"
"math"
"gonum.org/v1/gonum/mat"
)
// RunKabsch takes as input two slices of atoms pointers and returns the rotated versions of these atoms slices
// such that the RMSD is minimized
func RunKabsch(samp1, samp2 []*Atom) ([]*Atom, []*Atom, float64) {
var s1 *mat.Dense
var s2 *mat.Dense
s1 = GenerateMatrix(samp1)
s2 = GenerateMatrix(samp2)
result1, result2, rmsd := kabsch(s1, s2)
r1 := GenerateAtomSlice(result1, samp1)
r2 := GenerateAtomSlice(result2, samp2)
return r1, r2, rmsd
}
// Kabsch takes as input two dense matrices and performs the linear algebra calcuations and returns the matrices
// of the rotated points and the RMSD
func kabsch(p, q *mat.Dense) (*mat.Dense, *mat.Dense, float64) {
a := CopyMatrix(p)
b := CopyMatrix(q)
bColAvgs := AvgColumns(b)
aColAvgs := AvgColumns(a)
a = CenterAtOrigin(a)
b = CenterAtOrigin(b)
var E0 float64
for i := 0; i < a.RawMatrix().Rows; i++ {
for j := 0; j < a.RawMatrix().Cols; j++ {
E0 += a.At(i, j) * a.At(i, j)
E0 += b.At(i, j) * b.At(i, j)
}
}
var h mat.Dense
h.Mul(b.T(), a)
fmt.Println("Covariance matrix h: ")
matPrint(&h)
var svd mat.SVD
if ok := svd.Factorize(&h, mat.SVDFull); !ok {
fmt.Println("SVD failed")
return nil, nil, 0.0
}
S := svd.Values(nil)
var U mat.Dense
svd.UTo(&U)
fmt.Println("SVD U matrix: ")
matPrint(&U)
var VT mat.Dense
svd.VTo(&VT)
fmt.Println("SVD VT: ")
matPrint(VT.T())
reflect := mat.Det(&U) * mat.Det(VT.T())
if reflect < 0 {
S[len(S)-1] = -S[len(S)-1]
for i := range U.RawMatrix().Data {
if i%U.RawMatrix().Cols == U.RawMatrix().Cols-1 {
U.RawMatrix().Data[i] = -U.RawMatrix().Data[i]
}
}
}
fmt.Println("Reflect value: ", reflect)
RMSD := E0 - 2*mat.Sum(mat.NewVecDense(len(S), S))
RMSD = math.Sqrt(math.Abs(RMSD / float64(a.RawMatrix().Rows)))
var r mat.Dense
r.Mul(&U, VT.T())
for i := 0; i < b.RawMatrix().Rows; i++ {
for j := 0; j < b.RawMatrix().Cols; j++ {
b.Set(i, j, b.At(i, j)+bColAvgs[j])
}
}
var bRotated mat.Dense
bRotated.Mul(b, &r)
for i := 0; i < a.RawMatrix().Rows; i++ {
for j := 0; j < a.RawMatrix().Cols; j++ {
a.Set(i, j, a.At(i, j)+aColAvgs[j])
}
}
return a, &bRotated, RMSD
}
// GenerateMatrix takes as input a slice of atom pointers and returns a dense matrix of the coordinates
func GenerateMatrix(atoms []*Atom) *mat.Dense {
n := len(atoms)
data := make([]float64, 3*n)
for i, atom := range atoms {
data[3*i] = atom.x
data[3*i+1] = atom.y
data[3*i+2] = atom.z
}
matrix := mat.NewDense(n, 3, data)
return matrix
}
// GenerateAtomSlice takes as input a dense matrix and atom slice and returns an atom slice with
// with the correct pdb info incorporated
func GenerateAtomSlice(matrix *mat.Dense, pdbInfo []*Atom) []*Atom {
rows, _ := matrix.Dims()
atoms := make([]*Atom, rows)
for i := 0; i < rows; i++ {
atoms[i] = &Atom{
number: pdbInfo[i].number,
element: pdbInfo[i].element,
amino: pdbInfo[i].amino,
chain: pdbInfo[i].chain,
seqIndex: pdbInfo[i].seqIndex,
x: matrix.At(i, 0),
y: matrix.At(i, 1),
z: matrix.At(i, 2),
radius: pdbInfo[i].radius,
}
}
return atoms
}
// CenterAtOrigin takes as input a dense matrix and returns another dense matrix with the coordinates
// centered at the origin
func CenterAtOrigin(a *mat.Dense) *mat.Dense {
numRows, numCols := a.Dims()
colAvgs := AvgColumns(a)
for j := 0; j < numCols; j++ {
for i := 0; i < numRows; i++ {
a.Set(i, j, a.At(i, j)-colAvgs[j])
}
}
return a
}
// SumColumns takes an input a dense matrix and returns a slice of floats with the sum of the columns
func SumColumns(a *mat.Dense) []float64 {
numRows, numCols := a.Dims()
sums := make([]float64, numCols)
for j := 0; j < numCols; j++ {
for i := 0; i < numRows; i++ {
sums[j] += a.At(i, j)
}
}
return sums
}
// AvgColumns takes as input a dense matrix and returns a slice of floats that correspond to the column averages
func AvgColumns(a *mat.Dense) []float64 {
numRows, _ := a.Dims()
avgs := SumColumns(a)
for i := range avgs {
avgs[i] = avgs[i] / float64(numRows)
}
return avgs
}
// CopyMatrix takes as input a dense matrix and returns a exact copy as a dense matrix
func CopyMatrix(a *mat.Dense) *mat.Dense {
r, c := a.Dims()
data := make([]float64, r*c)
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
data[i*c+j] = a.At(i, j)
}
}
newMat := mat.NewDense(r, c, data)
return newMat
}
// matPrint takes as input a matrix and prints it
func matPrint(X mat.Matrix) {
fa := mat.Formatted(X, mat.Prefix(""), mat.Squeeze())
fmt.Printf("%v\n", fa)
}