1
|
|
|
#! /usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# Copyright (C) 2016 Rich Lewis <[email protected]> |
4
|
|
|
# License: 3-clause BSD |
5
|
|
|
|
6
|
1 |
|
""" |
7
|
|
|
## skchem.vis.mol |
8
|
|
|
|
9
|
|
|
Module for drawing molecules. |
10
|
|
|
""" |
11
|
|
|
|
12
|
1 |
|
from matplotlib import pyplot as plt |
|
|
|
|
13
|
1 |
|
from rdkit.Chem.Draw import DrawingOptions, MolToImage |
|
|
|
|
14
|
1 |
|
from matplotlib.widgets import CheckButtons |
|
|
|
|
15
|
1 |
|
from mpl_toolkits.mplot3d import Axes3D # needed to get 3D |
|
|
|
|
16
|
1 |
|
import numpy as np |
|
|
|
|
17
|
|
|
|
18
|
1 |
|
def draw(mol, quality=1, ax=None): |
|
|
|
|
19
|
|
|
|
20
|
|
|
"""Draw a molecule on a matplotlib axis. |
21
|
|
|
|
22
|
|
|
Args: |
23
|
|
|
mol (skchem.Mol): |
24
|
|
|
The molecule to be drawn. |
25
|
|
|
quality (int): |
26
|
|
|
The level of quality. Higher quality takes more time, but will |
27
|
|
|
look better (so long as matplotlib's savefig.dpi is high enough). |
28
|
|
|
ax (plt.Axes or None): |
29
|
|
|
An existing axis on which to draw the molecule. |
30
|
|
|
|
31
|
|
|
Returns: |
32
|
|
|
plt.AxesImage: |
33
|
|
|
A matplotlib AxesImage object with the molecule drawn. |
34
|
|
|
""" |
35
|
|
|
|
36
|
|
|
if not ax: |
37
|
|
|
ax = plt.gca() |
38
|
|
|
ax.grid('off') |
39
|
|
|
ax.axis('off') |
40
|
|
|
|
41
|
|
|
opts = DrawingOptions() |
42
|
|
|
opts.dotsPerAngstrom *= quality |
43
|
|
|
opts.atomLabelFontSize *= quality |
44
|
|
|
opts.bondLineWidth *= quality |
45
|
|
|
|
46
|
|
|
size = 300 * quality |
47
|
|
|
|
48
|
|
|
img, canvas, drawer = MolToImage(mol, size=(size, size), options=opts, |
|
|
|
|
49
|
|
|
returnCanvas=True) |
50
|
|
|
canvas.flush() |
51
|
|
|
return ax.imshow(img, extent=(0, 1, 0, 1)) |
52
|
|
|
|
53
|
|
|
|
54
|
1 |
|
def _plot_sphere(pos=(0, 0, 0), radius=1., ax=None, **kwargs): |
|
|
|
|
55
|
|
|
|
56
|
|
|
""" Plot a sphere. |
57
|
|
|
|
58
|
|
|
Args: |
59
|
|
|
pos tuple[float]: |
60
|
|
|
the centre of the sphere. |
61
|
|
|
radius (bool): |
62
|
|
|
The radius of the sphere. |
63
|
|
|
ax (plt.axes): |
64
|
|
|
The axes to draw the sphere on (defaults to current axis). |
65
|
|
|
|
66
|
|
|
Note: |
67
|
|
|
Keyword args are passed to Axes3D.plot_surface. |
68
|
|
|
""" |
69
|
|
|
|
70
|
|
|
if ax is None: |
71
|
|
|
ax = plt.gca() |
72
|
|
|
|
73
|
|
|
u, v = np.mgrid[0:2 * np.pi:100j], np.mgrid[0:np.pi:100j] |
|
|
|
|
74
|
|
|
|
75
|
|
|
x = 2 * radius * np.outer(np.cos(u), np.sin(v)) + pos[0] |
|
|
|
|
76
|
|
|
y = 2 * radius * np.outer(np.sin(u), np.sin(v)) + pos[1] |
|
|
|
|
77
|
|
|
z = 2 * radius * np.outer(np.ones(np.size(u)), np.cos(v)) + pos[2] |
|
|
|
|
78
|
|
|
|
79
|
|
|
return ax.plot_surface(x, y, z, **kwargs) |
80
|
|
|
|
81
|
|
|
|
82
|
1 |
|
_skchem_mpl3d_check = None |
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
1 |
|
def draw_3d(m, conformer_id=-1, label_atoms=None): |
|
|
|
|
86
|
|
|
|
87
|
|
|
""" Draw a molecule in three dimensions. |
88
|
|
|
|
89
|
|
|
Args: |
90
|
|
|
conformer_id (int): |
91
|
|
|
The id of the conformer to draw. |
92
|
|
|
label_atoms (bool): |
93
|
|
|
Whether to label the atoms (this can be toggled in interactive |
94
|
|
|
mode): |
95
|
|
|
|
96
|
|
|
Returns: |
97
|
|
|
plt.figure |
98
|
|
|
|
99
|
|
|
Note: |
100
|
|
|
This works great in the notebook with `%matplotlib notebook`. |
101
|
|
|
""" |
102
|
|
|
|
103
|
|
|
# required to stop widgets getting garbage collected |
104
|
|
|
global _skchem_mpl3d_check |
|
|
|
|
105
|
|
|
|
106
|
|
|
fig = plt.figure() |
107
|
|
|
ax = fig.add_subplot(111, projection='3d') |
|
|
|
|
108
|
|
|
|
109
|
|
|
pos = m.conformers[conformer_id].positions |
110
|
|
|
|
111
|
|
|
for i, j in m.bonds.atom_idxs: |
112
|
|
|
ax.plot(*[(pos[i, d], pos[j, d]) for d in range(3)], c='black', |
113
|
|
|
zorder=1) |
114
|
|
|
|
115
|
|
|
for atom, p in zip(m.atoms, pos): |
|
|
|
|
116
|
|
|
_plot_sphere(p, radius=0.25 * atom.covalent_radius, ax=ax, |
|
|
|
|
117
|
|
|
color=atom.hexcode, linewidth=0, zorder=10) |
118
|
|
|
x, y, z = p + 0.2 |
|
|
|
|
119
|
|
|
labels = [ax.text(x, y, z, i, visible=False) for i, p in enumerate(pos)] |
120
|
|
|
|
121
|
|
|
# set limits |
122
|
|
|
lo, hi = min(pos.flatten()) - 1, max( |
|
|
|
|
123
|
|
|
pos.flatten()) + 1.5 # 1.5 angstrom outside bording box |
124
|
|
|
ax.set_xlim(lo, hi) |
125
|
|
|
ax.set_ylim(lo, hi) |
126
|
|
|
ax.set_zlim(lo, hi) |
127
|
|
|
ax.set_aspect('equal') |
128
|
|
|
|
129
|
|
|
ax.set_xlabel('x') |
130
|
|
|
ax.set_ylabel('y') |
131
|
|
|
ax.set_zlabel('z') |
132
|
|
|
|
133
|
|
|
rax = plt.axes([0.15, 0.1, 0.1, 0.15]) |
134
|
|
|
|
135
|
|
|
_skchem_mpl3d_check = CheckButtons(rax, ('show labels', 'show axes'), |
136
|
|
|
(False, True)) |
137
|
|
|
|
138
|
|
|
def toggle(label): |
|
|
|
|
139
|
|
|
if label == 'show labels': |
140
|
|
|
for l in labels: |
|
|
|
|
141
|
|
|
l.set_visible(not l.get_visible()) |
142
|
|
|
|
143
|
|
|
if label == 'show axes': |
144
|
|
|
ax._axis3don = not ax._axis3don # easiest way |
|
|
|
|
145
|
|
|
|
146
|
|
|
plt.draw() |
147
|
|
|
|
148
|
|
|
_skchem_mpl3d_check.on_clicked(toggle) |
149
|
|
|
|
150
|
|
|
return ax |
151
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.