File size: 12,418 Bytes
9f39a5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""
.. codeauthor:: Zander Harteveld <zandermilanh@gmail.com>
.. codeauthor:: Jaume Bonet      <jaume.bonet@gmail.com>

.. affiliation::
    Laboratory of Protein Design and Immunoengineering <lpdi.epfl.ch>
    Bruno Correia <bruno.correia@epfl.ch>

.. class:: VirtualStructure
"""

# Standard Libraries
import copy

# External Libraries
from collections.abc import Iterable
# from collections import Iterable
from random import random
from bisect import bisect
import numpy as np
import pandas as pd
import scipy.spatial
from transforms3d.euler import euler2mat, mat2euler
from pdb import set_trace
# This Library
def quaternion_rmsd(P, Q):
    P = P - P.mean(axis=0)
    Q = Q - Q.mean(axis=0)
    rot = quaternion_rotate(P, Q)
    P = np.dot(P, rot)
    return rmsd(P, Q)

def quaternion_transform(r):
    Wt_r = makeW(*r).T
    Q_r = makeQ(*r)
    rot = Wt_r.dot(Q_r)[:3, :3]
    return rot

def makeW(r1, r2, r3, r4=0):
    W = np.asarray([
        [r4, r3, -r2, r1],
        [-r3, r4, r1, r2],
        [r2, -r1, r4, r3],
        [-r1, -r2, -r3, r4]])
    return W


def makeQ(r1, r2, r3, r4=0):
    Q = np.asarray([
        [r4, -r3, r2, r1],
        [r3, r4, -r1, r2],
        [-r2, r1, r4, r3],
        [-r1, -r2, -r3, r4]])
    return Q


def quaternion_rotate(X, Y):
    N = X.shape[0]
    W = np.asarray([makeW(*Y[k]) for k in range(N)])
    Q = np.asarray([makeQ(*X[k]) for k in range(N)])
    Qt_dot_W = np.asarray([np.dot(Q[k].T, W[k]) for k in range(N)])
    W_minus_Q = np.asarray([W[k] - Q[k] for k in range(N)])
    A = np.sum(Qt_dot_W, axis=0)
    eigen = np.linalg.eigh(A)
    r = eigen[1][:, eigen[0].argmax()]
    rot = quaternion_transform(r)
    return rot
def rmsd(V, W):
    D = len(V[0])
    N = len(V)
    result = 0.0
    for v, w in zip(V, W):
        result += sum([(v[i] - w[i])**2.0 for i in range(D)])
    return np.sqrt(result/N)

class VirtualStructure( object ):
    """
    """
    _MAX_AA_DIST = 3.2
    _ATOMTYPE     = ("N", "CA", "C", "O")
    _STRING_X    = "HETATM{0:>5d}  X     X {2}{0:>4d} {1[0]:>11.3f}{1[1]:>8.3f}{1[2]:>8.3f}  1.00"

    _STRING_ATOMS   = {
                        "N": "ATOM  {4:>5d}  N   {3:>3} {2}{0:>4d} {1[0]:>11.3f}{1[1]:>8.3f}{1[2]:>8.3f}  1.00  0.00",
                        "CA": "ATOM  {4:>5d}  CA  {3:>3} {2}{0:>4d} {1[0]:>11.3f}{1[1]:>8.3f}{1[2]:>8.3f}  1.00  0.00",
                        "C": "ATOM  {4:>5d}  C   {3:>3} {2}{0:>4d} {1[0]:>11.3f}{1[1]:>8.3f}{1[2]:>8.3f}  1.00  0.00",
                        "O": "ATOM  {4:>5d}  O   {3:>3} {2}{0:>4d} {1[0]:>11.3f}{1[1]:>8.3f}{1[2]:>8.3f}  1.00  0.00",
                        "H": "ATOM  {4:>5d}  H   {3:>3} {2}{0:>4d} {1[0]:>11.3f}{1[1]:>8.3f}{1[2]:>8.3f}  1.00  0.00",
                        }

    _A123 = {"C": "CYS", "D": "ASP", "S": "SER", "Q": "GLN", "K": "LYS",
             "I": "ILE", "P": "PRO", "T": "THR", "F": "PHE", "N": "ASN",
             "G": "GLY", "H": "HIS", "L": "LEU", "R": "ARG", "W": "TRP",
             "A": "ALA", "V": "VAL", "E": "GLU", "Y": "TYR", "M": "MET"}

    def __init__(self, residues, centre = [0., 0., 0.], start_atomcount = 0, start_residuecount = 0, chain = "A"):
        self.residues = int(residues)
        self.chain    = chain
        self.centre   = np.array(centre, dtype="float64")
        self.start_atomcount = start_atomcount
        self.start_residuecount = start_residuecount

        self.max_dist = float(self._MAX_AA_DIST * self.residues)
        self.edges    = [np.copy(self.centre) + np.array([0, self.max_dist / 2, 0]),
                         np.copy(self.centre) - np.array([0, self.max_dist / 2, 0])]

        self.points   = []
        for x in range(self.residues):
            self.points.append(np.copy(self.edges[0]) - np.array([0, self._MAX_AA_DIST * x, 0]) )
        self.atoms    = []
        self.atomtypes = []
        self.ca_atoms = []
        self.atom = None
        self.Rapplied = np.eye(3)
        self.is_inverted = False
        self.spinned     = 0
        self.sequence    = None
        self.ref         = None
        self.name        = None

    # BOOLEANS
    def in_origin(self):
        return np.allclose(self.centre, [0., 0., 0.])

    def goes_up(self):
        if len(self.atoms) > 0:
            return self.atoms[0][1] < self.atoms[-1][1]
        elif len(self.points) > 0:
            return self.points[0][1] < self.points[-1][1]
    
    def display_atoms(self):
        if len(self.atoms) > 0:
            print('atoms', self.atoms)
        elif len(self.points) > 0:
            print('points', self.points)

    #def get_direction(self):
    #    self.atoms[-1] - self.atoms[-1][1]

    def goes_down(self):
        return not self.goes_up()

    # GETTERS
    def get_type(self):
        return self._TYPE

    def up_is_1(self):
        return 1 if self.goes_up() else 0

    # TILT
    def tilt_x_degrees(self, angle): self.tilt_degrees(x_angle = angle)
    def tilt_y_degrees(self, angle): self.tilt_degrees(y_angle = angle)
    def tilt_z_degrees(self, angle): self.tilt_degrees(z_angle = angle)

    def tilt_degrees(self, x_angle = 0, y_angle = 0, z_angle = 0, store = True):
        if x_angle == 0 and y_angle == 0 and z_angle == 0: return
        self.tilt_radiants(x_angle = np.radians(x_angle),
                           y_angle = np.radians(y_angle),
                           z_angle = np.radians(z_angle), store = store)

    def tilt_x_radiants(self, angle): self.tilt_radiants(x_angle = angle)
    def tilt_y_radiants(self, angle): self.tilt_radiants(y_angle = angle)
    def tilt_z_radiants(self, angle): self.tilt_radiants(z_angle = angle)

    def tilt_radiants(self, x_angle = 0, y_angle = 0, z_angle = 0, store = True):
        Rx = euler2mat(x_angle, 0, 0, "sxyz")
        Ry = euler2mat(0, y_angle, 0, "sxyz")
        Rz = euler2mat(0, 0, z_angle, "sxyz")
        R  = np.dot(Rz, np.dot(Rx, Ry))

        tmpctr = np.array([0., 0., 0.])
        fixpos = not np.allclose(self.centre, tmpctr)
        tmpctr = np.copy(self.centre)
        if fixpos: self.shift(x = -tmpctr[0], y = -tmpctr[1], z = -tmpctr[2])
        self.apply_matrix(R)
        if fixpos: self.shift(x = tmpctr[0], y = tmpctr[1], z = tmpctr[2])

        if store: self.Rapplied = np.dot(self.Rapplied, R)
    def rotate(self,A,B):
        #B is the ref
        rot = quaternion_rotate(A, B)
        self.apply_matrix(rot)
    def apply_matrix(self, R):
        if len(self.edges):  self.edges  = np.dot(self.edges,  R)
        if len(self.points): self.points = np.dot(self.points, R)
        if len(self.atoms):  self.atoms  = np.dot(self.atoms,  R)

    # ROTATE ON AXIS
    def spin_radians(self, angle): self.spin_degrees(np.degrees(angle))

    def spin_degrees(self, angle):
        if np.allclose(self.Rapplied, np.eye(3)):
            self.tilt_degrees(y_angle = angle, store = False)
        else:
            euler1 = mat2euler(self.Rapplied)
            euler2 = mat2euler(self.Rapplied.transpose())
            self.tilt_radiants(euler2[0], euler2[1], euler2[2])
            self.tilt_degrees(y_angle = angle, store = False)
            self.tilt_radiants(euler1[0], euler1[1], euler1[2])
        self.spinned += angle

    def remove_movement_memory(self):
        self.Rapplied = np.eye(3)

    # SHIFT
    def shift_x(self, x): self.shift(x, 0., 0.)
    def shift_y(self, y): self.shift(0., y, 0.)
    def shift_z(self, z): self.shift(0., 0., z)

    def shift(self, x = 0., y = 0., z = 0.):
        t = np.array(x) if isinstance(x, Iterable) else np.array([x, y, z])
        self.centre += t
        self.edges  += t
        self.points += t
        if len(self.atoms): self.atoms += t

    def shift_to_origin(self):
        anti = np.copy(self.centre) if not self.in_origin() else np.array([0., 0., 0.])
        self.shift(-anti)
        return anti

    def get_center(self):
        return np.array(self.points).mean(axis=0)

    # FUNC
    def invert_direction(self):
        if np.allclose(self.Rapplied, np.eye(3)):
            self.tilt_degrees(x_angle = 180, y_angle = 180, store = False)
        else:
            euler1 = mat2euler(self.Rapplied)
            euler2 = mat2euler(self.Rapplied.transpose())
            self.tilt_radiants(euler2[0], euler2[1], euler2[2])
            self.tilt_degrees(x_angle = 180, y_angle = 180, store = False)
            self.tilt_radiants(euler1[0], euler1[1], euler1[2])
        self.is_inverted = not self.is_inverted

    def atom_points(self, atom = 1, seq = None):
        count = 0
        data  = []
        d = {"C": "CYS", "D": "ASP", "S": "SER", "Q": "GLN", "K": "LYS",
             "I": "ILE", "P": "PRO", "T": "THR", "F": "PHE", "N": "ASN",
             "G": "GLY", "H": "HIS", "L": "LEU", "R": "ARG", "W": "TRP",
             "A": "ALA", "V": "VAL", "E": "GLU", "Y": "TYR", "M": "MET"}
        if seq is None and self.sequence is not None: seq = self.sequence
        if seq is None: seq = "G"
        else:           seq = seq.upper()

        for x, (points, atomtype) in enumerate(zip(self.atoms, self.atomtypes)):
            data.append(self._STRING_ATOMS[atomtype].format(atom + count, points, self.chain, d[seq[count]], atom + x))
            if (1 + x)%len(self._ATOMTYPE)==0:
                count += 1
        return "\n".join(data)

    def atom_data(self, atom = 1, seq = None):
        count = 0
        data = {
            'x': [],
            'y': [],
            'z': [],
            'id': [],
            'chain': [],
            'atomtype': [],
            'atomnum': [],
            'res1aa': [],
            'res3aa': []
        }

        d = {"C": "CYS", "D": "ASP", "S": "SER", "Q": "GLN", "K": "LYS",
             "I": "ILE", "P": "PRO", "T": "THR", "F": "PHE", "N": "ASN",
             "G": "GLY", "H": "HIS", "L": "LEU", "R": "ARG", "W": "TRP",
             "A": "ALA", "V": "VAL", "E": "GLU", "Y": "TYR", "M": "MET"}
        if seq is None and self.sequence is not None: seq = self.sequence
        if seq is None: seq = "G"
        else:           seq = seq.upper()
        for x, (points, atomtype) in enumerate(zip(self.atoms, self.atomtypes)):
            #data.append(self._STRING_ATOMS[atomtype].format(atom + count, points, self.chain, d[seq[count]], atom + x))
            data['x'].append( float(points[0]) )
            data['y'].append( float(points[1]) )
            data['z'].append( float(points[2]) )
            data['id'].append( atom + count )
            data['atomnum'].append( atom + x )
            data['atomtype'].append( atomtype )
            data['res1aa'].append( seq[count] )
            data['res3aa'].append( d[seq[count]] )
            data['chain'].append( self.chain )

            if (1 + x)%len(self._ATOMTYPE)==0:
                count += 1
        return pd.DataFrame(data)

    def eigenvectors(self, modules=[2.0, 2.0, 2.0]):
        def point_distance( point1, point2 ):
            return np.linalg.norm(point1 - point2)

        coordinates = np.array(self.points)
        center = coordinates.mean(axis=0)
        A  = np.asmatrix(np.zeros((3, 3)))
        P  = coordinates - center
        for p in P:
            r = np.asmatrix(p)
            A += r.transpose() * r
        val, EigVc = np.linalg.eigh(A)
        vectors = []
        for axis, module in enumerate(modules):
            t = np.asarray(EigVc[:, axis]).reshape(3)
            vectors.append([np.around(np.asarray(center + (module / 2.0) * t, dtype=np.float32), decimals=3),
                            np.around(center, decimals=3),
                            np.around(np.asarray(center - (module / 2.0) * t, dtype=np.float32), decimals=3)])

        ## Correct direction Major Axis
        if point_distance(vectors[2][0], coordinates[-1]) < point_distance(vectors[2][0], coordinates[0]):
            vectors[2] = np.flip(vectors[2], axis=0)

        return np.asarray(vectors)

    def change_basis(self, eigenvectors):
        # centering points
        evecs_cen = [eigenvectors - eigenvectors[1] for eigenvectors in eigenvectors]
        evecs_sys = np.array([eigenvectors[2] - eigenvectors[0] for eigenvectors in evecs_cen])

        points_new = []
        for i in range(len(self.points)):
            points_new.append(evecs_sys.dot(self.points[i]))
        self.points = points_new

    def create_val_sequence(self):
        if self.sequence is None:
            self.sequence = ""
            for x in range(self.residues):
                self.sequence += "V"