-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathenv_indigo.py
318 lines (268 loc) · 8.37 KB
/
env_indigo.py
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
# Setup enviroment for using Indigo both for Python, Jython and IronPython
import inspect
import os
import shutil
import sys
import threading
from math import sqrt
from util import REPO_ROOT, getPlatform, isIronPython, isJython
if isIronPython():
import clr
import System
clr.AddReference("System.IO.FileSystem")
clr.AddReference("System.IO.Compression.FileSystem")
clr.AddReference("System.Runtime.Extensions")
from System import Environment
from System.IO import Directory, DirectoryInfo, File, FileInfo
from System.IO.Compression import ZipFile
else:
import subprocess
import zipfile
if isJython():
from util import download_jna, get_indigo_java_version
frame = inspect.stack()[1]
module = inspect.getmodule(frame[0])
def dir_exists(path_):
if sys.platform == "cli" and os.name != "nt":
return Directory.Exists(path_)
else:
return os.path.isdir(path_)
def file_exists(path):
if sys.platform == "cli":
return FileInfo(path).Exists
else:
return os.path.exists(path)
def file_size(path):
if sys.platform == "cli":
return FileInfo(path).Length
else:
return os.path.getsize(path)
def makedirs(path):
if sys.platform == "cli":
if not dir_exists(path):
Directory.CreateDirectory(path)
else:
os.makedirs(path)
def unzip(path, parent):
if sys.platform == "cli":
out_dir = str(tuple(ZipFile.OpenRead(path).Entries)[0])
if dir_exists(out_dir):
Directory.Delete(out_dir, True)
ZipFile.ExtractToDirectory(path, parent)
if "latest" in path:
return out_dir
else:
with zipfile.ZipFile(item) as zf:
zf.extractall()
if "latest" in path:
return zf.filelist[0].filename
return None
def rmdir(path):
if sys.platform == "cli":
if not dir_exists(path):
return
else:
Directory.Delete(path, True)
else:
shutil.rmtree(path)
if isIronPython():
import clr
dll_full_path = lambda: os.path.normpath(
os.path.abspath(os.environ["INDIGO_PATH"])
)
clr.AddReferenceToFileAndPath(dll_full_path())
from com.epam.indigo import (
Bingo,
BingoException,
BingoObject,
Indigo,
IndigoException,
IndigoInchi,
IndigoObject,
IndigoRenderer,
ReactingCenter,
)
elif isJython():
indigo_java_version, jna_version = get_indigo_java_version()
indigo_path = os.path.normpath(
os.path.abspath(
os.getenv("INDIGO_PATH", os.path.join(REPO_ROOT, "dist"))
)
)
download_jna(jna_version, indigo_path)
jars = []
for filename in os.listdir(indigo_path):
if (
filename.endswith(".jar")
and not "javadoc" in filename
and not "sources" in filename
):
jar_path = os.path.join(indigo_path, filename)
if "jna" in filename:
jars.insert(0, jar_path)
else:
jars.append(jar_path)
for jar in sorted(jars):
sys.path.append(jar)
from com.epam.indigo import (
Bingo,
BingoException,
BingoObject,
Indigo,
IndigoException,
IndigoInchi,
IndigoObject,
IndigoRenderer,
)
dll_full_path = lambda: sys.path[-4]
else:
indigo_python_source_folder = os.path.join(REPO_ROOT, "api", "python")
sys.path.append(indigo_python_source_folder)
from indigo import Indigo, IndigoException, IndigoObject # noqa
from indigo.bingo import Bingo, BingoException, BingoObject # noqa
from indigo.inchi import IndigoInchi # noqa
from indigo.indigo.indigo_lib import IndigoLib # noqa
from indigo.renderer import IndigoRenderer # noqa
dll_full_path = lambda: IndigoLib.lib._name
def getIndigoExceptionText(e):
if isJython():
return e.message
elif isIronPython():
return e.Message.replace("\\'", "'")
else:
value = str(e)
if value[0] == "'" and value[-1] == "'":
value = value[1:-1]
if value[0] == '"' and value[-1] == '"':
value = value[1:-1]
return value.replace("\\'", "'")
inspectStackLock = threading.RLock()
def dataPath(args):
return os.path.normpath(
os.path.abspath(
os.path.join(
os.path.abspath(__file__),
"..",
"..",
"..",
"..",
"..",
"data",
args,
)
)
)
def joinPathPy(args, file_py):
return os.path.normpath(
os.path.abspath(
os.path.join(os.path.abspath(os.path.dirname(file_py)), args)
)
).replace("\\", "/")
def relativePath(args):
inspectStackLock.acquire()
frm = inspect.stack()[1][1]
inspectStackLock.release()
return os.path.normpath(
os.path.relpath(args, os.path.dirname(frm))
).replace("\\", "/")
def dist_vec(a, b):
return sqrt(sum((a - b) ** 2 for a, b in zip(a, b)))
def moleculeLayoutDiff(indigo, mol, ref, delta=0.01, ref_is_file=True):
if ref_is_file:
ref_name = getRefFilepath2(ref)
m2 = indigo.loadMoleculeFromFile(ref_name)
else:
m2 = indigo.loadMolecule(ref)
error_buf = []
for a1 in mol.iterateAtoms():
a2 = m2.getAtom(a1.index())
a = a1.xyz()
b = a2.xyz()
d = dist_vec(a, b)
if d > delta:
error_buf.append(
"atom #{} ({}) contains delta {} > {}".format(
a1.index(), a1.symbol(), d, delta
)
)
for s in mol.iterateDataSGroups():
s2 = m2.getDataSGroup(s.index())
a = s.getSGroupCoords()
b = s2.getSGroupCoords()
d = dist_vec(a, b)
if d > delta:
error_buf.append(
"data sgroup #{} contains delta {} > {}".format(
s.index(), d, delta
)
)
if len(error_buf) > 0:
return "Error: {}".format("\n ".join(error_buf))
return "Success"
def reactionLayoutDiff(indigo, rxn, ref, delta=0.001, ref_is_file=True):
if ref_is_file:
ref_name = getRefFilepath2(ref)
r2 = indigo.loadReactionFromFile(ref_name)
else:
r2 = indigo.loadReaction(ref)
error_buf = []
for m in rxn.iterateMolecules():
res = moleculeLayoutDiff(
indigo,
m,
r2.getMolecule(m.index()).molfile(),
delta,
ref_is_file=False,
)
error_buf.append("Molecule #{}: {}".format(m.index(), res))
return "\n ".join(error_buf)
def getRefFilepath(filename):
"""
Returns:
reference path for the specified filename. Platform specific if necessary
"""
with inspectStackLock:
frm = inspect.stack()[1][1]
ref_path = os.path.abspath(os.path.join(os.path.dirname(frm), "ref"))
sys_name = getPlatform()
if file_exists(os.path.join(ref_path, sys_name, filename)):
return os.path.normpath(
os.path.abspath(os.path.join(ref_path, sys_name, filename))
)
if file_exists(os.path.join(ref_path, filename)):
return os.path.normpath(
os.path.abspath(os.path.join(ref_path, filename))
)
raise RuntimeError(
'Can not find a file "%s" neither at "%s" or "%s"'
% (
filename,
ref_path,
os.path.abspath(os.path.join(ref_path, sys_name)),
)
)
def getRefFilepath2(filename):
"""
Returns:
reference path for the specified filename in stack 2. Platform specific if necessary
"""
with inspectStackLock:
frm = inspect.stack()[2][1]
ref_path = os.path.abspath(os.path.join(os.path.dirname(frm), "ref"))
if file_exists(os.path.join(ref_path, filename)):
return os.path.normpath(
os.path.abspath(os.path.join(ref_path, filename))
)
sys_name = getPlatform()
if file_exists(os.path.join(ref_path, sys_name, filename)):
return os.path.normpath(
os.path.abspath(os.path.join(ref_path, sys_name, filename))
)
raise RuntimeError(
'Can not find a file "%s" neither at "%s" or "%s"'
% (
filename,
ref_path,
os.path.abspath(os.path.join(ref_path, sys_name)),
)
)