| 1 |
from __future__ import division |
| 2 |
|
| 3 |
import sys |
| 4 |
import time |
| 5 |
from math import sin, cos, pi, sqrt |
| 6 |
|
| 7 |
from shelxfile import Shelxfile |
| 8 |
from shelxfile.atoms.atoms import Atoms |
| 9 |
|
| 10 |
""" |
| 11 |
This module is a fork from https://github.com/des4maisons/molecule-viewer |
| 12 |
""" |
| 13 |
|
| 14 |
|
| 15 |
class Coordinate2D(object): |
| 16 |
def __init__(self, x, y): |
| 17 |
self.x = x |
| 18 |
self.y = y |
| 19 |
|
| 20 |
def __mul__(self, const): |
| 21 |
return Coordinate2D(self.x * const, self.y * const) |
| 22 |
|
| 23 |
def __add__(self, coor): |
| 24 |
return Coordinate2D(self.x + coor.x, self.y + coor.y) |
| 25 |
|
| 26 |
def __sub__(self, coor): |
| 27 |
return self + coor * (-1) |
| 28 |
|
| 29 |
def __truediv__(self, const): |
| 30 |
return self * (1 / const) |
| 31 |
|
| 32 |
def __repr__(self): |
| 33 |
return "(%.02f, %.02f)" % (self.x, self.y) |
| 34 |
|
| 35 |
def __str__(self): |
| 36 |
return self.__repr__() |
| 37 |
|
| 38 |
|
| 39 |
class Atom(object): |
| 40 |
def __init__(self, x, y, z, symbol, id): |
| 41 |
self.coordinate = Coordinate(x, y, z) |
| 42 |
self.symbol = symbol |
| 43 |
self.id = int(id) |
| 44 |
|
| 45 |
def __repr__(self): |
| 46 |
return str((self.symbol, self.id, self.coordinate)) |
| 47 |
|
| 48 |
def __str__(self): |
| 49 |
return self.__repr__() |
| 50 |
|
| 51 |
def flatten(self, plane=None): |
| 52 |
return self.coordinate.flatten(plane) |
| 53 |
|
| 54 |
|
| 55 |
def coloured_symbol(self): |
| 56 |
return "\x1b[" + str(31 + (self.id % 7)) + "m" + self.symbol |
| 57 |
|
| 58 |
|
| 59 |
class Coordinate(object): |
| 60 |
def __init__(self, x, y, z): |
| 61 |
self.x, self.y, self.z = [x, y, z] |
| 62 |
|
| 63 |
def __repr__(self): |
| 64 |
return "(%.02f, %.02f, %.02f)" % (self.x, self.y, self.z) |
| 65 |
|
| 66 |
def __str__(self): |
| 67 |
return self.__repr__() |
| 68 |
|
| 69 |
def __truediv__(self, const): |
| 70 |
return self * (1 / const) |
| 71 |
|
| 72 |
def __mul__(self, const): |
| 73 |
return Coordinate(self.x * const, self.y * const, self.z * const) |
| 74 |
|
| 75 |
def __add__(self, coor): |
| 76 |
return Coordinate(self.x + coor.x, self.y + coor.y, self.z + coor.z) |
| 77 |
|
| 78 |
|
| 79 |
def dot(self, vector): |
| 80 |
return self.x * vector.x + self.y * vector.y + self.z * vector.z |
| 81 |
|
| 82 |
def length(self): |
| 83 |
return sqrt((self.x ** 2) + (self.y ** 2) + (self.z ** 2)) |
| 84 |
|
| 85 |
|
| 86 |
def project(self, onto): |
| 87 |
length = onto.length() |
| 88 |
scale_factor = self.dot(onto) / (length ** 2) |
| 89 |
return onto * scale_factor |
| 90 |
|
| 91 |
|
| 92 |
def cross(self, vector): |
| 93 |
a1, a2, a3 = [self.x, self.y, self.z] |
| 94 |
b1, b2, b3 = [vector.x, vector.y, vector.z] |
| 95 |
return Coordinate(a2 * b3 - a3 * b2, |
| 96 |
a3 * b1 - a1 * b3, |
| 97 |
a1 * b2 - a2 * b1) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
def flatten(self, plane=None): |
| 102 |
if plane is None: |
| 103 |
plane = [Coordinate(1, 0, 0), Coordinate(0, 1, 0)] |
| 104 |
|
| 105 |
plane = list(plane) |
| 106 |
|
| 107 |
perp = plane[0].cross(plane[1]) |
| 108 |
plane[0] = perp.cross(plane[1]) |
| 109 |
|
| 110 |
plane[0] = plane[0] / (plane[0].length()) |
| 111 |
plane[1] = plane[1] / (plane[1].length()) |
| 112 |
|
| 113 |
proj0 = self.project(plane[0]) |
| 114 |
proj1 = self.project(plane[1]) |
| 115 |
ratio0 = None |
| 116 |
ratio1 = None |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
for component in ["x", "y", "z"]: |
| 121 |
val = getattr(plane[0], component) |
| 122 |
if val != 0: |
| 123 |
ratio0 = getattr(proj0, component) / val |
| 124 |
val = getattr(plane[1], component) |
| 125 |
if val != 0: |
| 126 |
ratio1 = getattr(proj1, component) / val |
| 127 |
if ratio0 is None or ratio1 is None: |
| 128 |
raise ValueError("plane defined with zero vector") |
| 129 |
return Coordinate2D(ratio0, ratio1) |
| 130 |
|
| 131 |
|
| 132 |
class Molecule(object): |
| 133 |
def __init__(self, shx_atoms: Atoms): |
| 134 |
self.atoms = [] |
| 135 |
for at in shx_atoms: |
| 136 |
self.atoms.append(Atom(at.x, at.y, at.z, at.name, at.resinum)) |
| 137 |
|
| 138 |
def parse_atom(self, str): |
| 139 |
atype, seqnum, elt, one, x, y, z, who, cares = str.split() |
| 140 |
self.atoms.append(Atom(float(x), float(y), float(z), elt, int(seqnum))) |
| 141 |
|
| 142 |
|
| 143 |
def draw(self, plane=None, dimensions=None): |
| 144 |
if dimensions is None: |
| 145 |
dimensions = (80, 29) |
| 146 |
screen = Screen(dimensions) |
| 147 |
flattened = [a.flatten(plane) for a in self.atoms] |
| 148 |
max_extreme = Coordinate2D(max([coor.x for coor in flattened]), |
| 149 |
max([coor.y for coor in flattened])) |
| 150 |
min_extreme = Coordinate2D(min([coor.x for coor in flattened]), |
| 151 |
min([coor.y for coor in flattened])) |
| 152 |
span = max_extreme - min_extreme |
| 153 |
|
| 154 |
scale_factor = min((screen.width - 1) / span.x, (screen.height - 1) / span.y) |
| 155 |
extra_space = Coordinate2D(screen.width - 1, screen.height - 1) - span * scale_factor |
| 156 |
offset = extra_space / 2 |
| 157 |
for atom in self.atoms: |
| 158 |
|
| 159 |
screen_index = (atom.flatten(plane) - min_extreme) * scale_factor |
| 160 |
screen_index = screen_index + offset |
| 161 |
screen_index = (int(round(screen_index.x)), int(round(screen_index.y))) |
| 162 |
screen.set(screen_index, atom.coloured_symbol()) |
| 163 |
screen.show() |
| 164 |
|
| 165 |
|
| 166 |
class Screen(list): |
| 167 |
def __init__(self, widthheight): |
| 168 |
list.__init__(self) |
| 169 |
self.width = widthheight[1] |
| 170 |
self.height = widthheight[0] |
| 171 |
for i in range(self.width): |
| 172 |
self.append([]) |
| 173 |
for _ in range(self.height): |
| 174 |
self[i].append(" ") |
| 175 |
|
| 176 |
def set(self, index, val): |
| 177 |
x = index[0] |
| 178 |
y = index[1] |
| 179 |
self[x][y] = val |
| 180 |
|
| 181 |
def show(self): |
| 182 |
for row in self: |
| 183 |
print("".join(row)) |
| 184 |
|
| 185 |
|
| 186 |
if __name__ == "__main__": |
| 187 |
shx = Shelxfile() |
| 188 |
shx.read_file('./tests/resources/p21c.res') |
| 189 |
try: |
| 190 |
width = sys.argv[2] |
| 191 |
height = sys.argv[3] |
| 192 |
dims = (int(width), int(height) - 1) |
| 193 |
except IndexError: |
| 194 |
dims = None |
| 195 |
angle = 0 |
| 196 |
angle_incr = (2 * pi) * (1 / 40) |
| 197 |
while True: |
| 198 |
Molecule(shx.atoms).draw(plane=[Coordinate(0, 1, 0), Coordinate(sin(angle), 0, cos(angle))], dimensions=dims) |
| 199 |
angle = angle + angle_incr |
| 200 |
time.sleep(0.5) |
| 201 |
print("\33[H") |