forked from ggasoftware/ketcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathketcher.js
343 lines (311 loc) · 14.5 KB
/
ketcher.js
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
332
333
334
335
336
337
338
339
340
341
342
343
/****************************************************************************
* Copyright (C) 2009-2010 GGA Software Services LLC
*
* This file may be distributed and/or modified under the terms of the
* GNU Affero General Public License version 3 as published by the Free
* Software Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
***************************************************************************/
ketcher = function () {
this.render = null;
};
ketcher.version = "1.0b5";
ketcher.init = function ()
{
document.title += ' v' + ketcher.version;
ketcher.templates = {};
ketcher.button_areas = {};
var elemLabelOpts = {'fontSize':25};
ketcher.button_areas.atom_h = new rnd.ElementTable('atom_h', elemLabelOpts).renderSingle('H');
ketcher.button_areas.atom_c = new rnd.ElementTable('atom_c', elemLabelOpts).renderSingle('C');
ketcher.button_areas.atom_n = new rnd.ElementTable('atom_n', elemLabelOpts).renderSingle('N');
ketcher.button_areas.atom_o = new rnd.ElementTable('atom_o', elemLabelOpts).renderSingle('O');
ketcher.button_areas.atom_s = new rnd.ElementTable('atom_s', elemLabelOpts).renderSingle('S');
ketcher.button_areas.atom_p = new rnd.ElementTable('atom_p', elemLabelOpts).renderSingle('P');
ketcher.button_areas.atom_f = new rnd.ElementTable('atom_f', elemLabelOpts).renderSingle('F');
ketcher.button_areas.atom_cl = new rnd.ElementTable('atom_cl', elemLabelOpts).renderSingle('Cl');
ketcher.button_areas.atom_br = new rnd.ElementTable('atom_br', elemLabelOpts).renderSingle('Br');
ketcher.button_areas.atom_i = new rnd.ElementTable('atom_i', elemLabelOpts).renderSingle('I');
ketcher.button_areas.atom_table = new rnd.ElementTable('atom_table', elemLabelOpts).renderSingle('...');
ketcher.button_areas.atom_any = new rnd.ElementTable('atom_reagenerics', {'fontSize':9}).renderSingle('Generic\nGroups');
var charge_head = ['', ' fun stuff 0123456789AB', '',
' 1 0 0 0 0 0 999 V2000',
' 0.4714 1.8562 0.0000 A 0 3 0 0 0 0 0 0 0 0 0 0'];
var charge_tail = ['M END'];
var tmpl = ketcher.templates;
tmpl.charge_plus = charge_head.concat(['M CHG 1 1 1'], charge_tail);
tmpl.charge_minus = charge_head.concat(['M CHG 1 1 -1'], charge_tail);
var renderOpts = {
'autoScale':true,
'autoScaleMargin':2,
'hideImplicitHydrogen':true,
'hideTerminalLabels':true,
'ignoreMouseEvents':true};
var renderOptsBond = {
'autoScale':true,
'autoScaleMargin':4,
'hideImplicitHydrogen':true,
'hideTerminalLabels':true,
'ignoreMouseEvents':true};
ketcher.button_areas.charge_plus = ketcher.showMolfileOpts('charge_plus', tmpl.charge_plus, 75, renderOpts);
ketcher.button_areas.charge_minus = ketcher.showMolfileOpts('charge_minus', tmpl.charge_minus, 75, renderOpts);
var bond_head = ['', ' Ketcher 08191119302D 1 1.00000 0.00000 0', '',
' 2 1 0 0 0 999 V2000',
' -2.5000 -0.3000 0.0000 C 0 0 0 0 0 0 0 0 0 0',
' -1.0000 0.3000 0.0000 C 0 0 0 0 0 0 0 0 0 0'];
var bond_tail = ['M END'];
// will use this templates in dropdown list
tmpl.bond_any = bond_head.concat([' 1 2 8 0 0 0'], bond_tail);
tmpl.bond_single = bond_head.concat([' 1 2 1 0 0 0'], bond_tail);
tmpl.bond_up = bond_head.concat([' 1 2 1 1 0 0'], bond_tail);
tmpl.bond_down = bond_head.concat([' 1 2 1 6 0 0'], bond_tail);
tmpl.bond_updown = bond_head.concat([' 1 2 1 4 0 0'], bond_tail);
tmpl.bond_double = bond_head.concat([' 1 2 2 0 0 0'], bond_tail);
tmpl.bond_crossed = bond_head.concat([' 1 2 2 3 0 0'], bond_tail);
tmpl.bond_triple = bond_head.concat([' 1 2 3 0 0 0'], bond_tail);
tmpl.bond_aromatic = bond_head.concat([' 1 2 4 0 0 0'], bond_tail);
tmpl.bond_singledouble = bond_head.concat([' 1 2 5 0 0 0'], bond_tail);
tmpl.bond_singlearomatic = bond_head.concat([' 1 2 6 0 0 0'], bond_tail);
tmpl.bond_doublearomatic = bond_head.concat([' 1 2 7 0 0 0'], bond_tail);
ketcher.button_areas.bond_single = ketcher.showMolfileOpts('bond', tmpl.bond_single, 20, renderOptsBond);
var renderOptsPattern = {
'autoScale':true,
'autoScaleMargin':2,
'hideImplicitHydrogen':true,
'hideTerminalLabels':true,
'ignoreMouseEvents':true};
tmpl.chain = ['', ' Ketcher 10181123552D 1 1.00000 0.00000 0', '',
' 4 3 0 0 0 999 V2000',
' -5.8000 -0.6500 0.0000 C 0 0 0 0 0 0 0 0 0',
' -4.9340 -1.1500 0.0000 C 0 0 0 0 0 0 0 0 0',
' -4.0679 -0.6500 0.0000 C 0 0 0 0 0 0 0 0 0',
' -3.2019 -1.1500 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1 2 1 0 0 0',
' 2 3 1 0 0 0',
' 3 4 1 0 0 0',
'M END'
];
tmpl.template_0 = ['', ' Ketcher 12101120452D 1 1.00000 0.00000 0', '',
' 6 6 0 0 0 999 V2000',
' 0.8660 2.0000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1.7320 1.5000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1.7320 0.5000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.8660 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.0000 0.5000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.0000 1.5000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1 2 1 0 0 0',
' 2 3 2 0 0 0',
' 3 4 1 0 0 0',
' 4 5 2 0 0 0',
' 5 6 1 0 0 0',
' 6 1 2 0 0 0',
'M END'
];
tmpl.template_1 = ['', ' Ketcher 12101117232D 1 1.00000 0.00000 0', '',
' 5 5 0 0 0 999 V2000',
' 0.0000 1.4257 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.8090 0.8379 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.5000 -0.1132 0.0000 C 0 0 0 0 0 0 0 0 0',
' -0.5000 -0.1132 0.0000 C 0 0 0 0 0 0 0 0 0',
' -0.8090 0.8379 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1 2 1 0 0 0',
' 2 3 2 0 0 0',
' 3 4 1 0 0 0',
' 4 5 2 0 0 0',
' 5 1 1 0 0 0',
'M END'
];
tmpl.template_2 = ['', ' Ketcher 12101120472D 1 1.00000 0.00000 0', '',
' 6 6 0 0 0 999 V2000',
' 0.8660 2.0000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1.7320 1.5000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1.7320 0.5000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.8660 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.0000 0.5000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.0000 1.5000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1 2 1 0 0 0',
' 2 3 1 0 0 0',
' 3 4 1 0 0 0',
' 4 5 1 0 0 0',
' 5 6 1 0 0 0',
' 6 1 1 0 0 0',
'M END'
];
tmpl.template_3 = ['', ' Ketcher 12101120492D 1 1.00000 0.00000 0', '',
' 5 5 0 0 0 999 V2000',
' 0.8090 1.5389 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1.6180 0.9511 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1.3090 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.3090 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.0000 0.9511 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1 2 1 0 0 0',
' 2 3 1 0 0 0',
' 3 4 1 0 0 0',
' 4 5 1 0 0 0',
' 5 1 1 0 0 0',
'M END'
];
tmpl.template_4 = ['', ' Ketcher 12101118272D 1 1.00000 0.00000 0', '',
' 3 3 0 0 0 999 V2000',
' -3.2250 -0.2750 0.0000 C 0 0 0 0 0 0 0 0 0',
' -2.2250 -0.2750 0.0000 C 0 0 0 0 0 0 0 0 0',
' -2.7250 0.5910 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1 2 1 0 0 0',
' 2 3 1 0 0 0',
' 1 3 1 0 0 0',
'M END'
];
tmpl.template_5 = ['', ' Ketcher 12101118312D 1 1.00000 0.00000 0', '',
' 4 4 0 0 0 999 V2000',
' -3.8250 1.5500 0.0000 C 0 0 0 0 0 0 0 0 0',
' -3.8250 0.5500 0.0000 C 0 0 0 0 0 0 0 0 0',
' -2.8250 1.5500 0.0000 C 0 0 0 0 0 0 0 0 0',
' -2.8250 0.5500 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1 2 1 0 0 0',
' 1 3 1 0 0 0',
' 3 4 1 0 0 0',
' 4 2 1 0 0 0',
'M END'
];
tmpl.template_6 = ['', ' Ketcher 12101118372D 1 1.00000 0.00000 0', '',
' 7 7 0 0 0 999 V2000',
' 0.0000 1.6293 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.7835 2.2465 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1.7559 2.0242 0.0000 C 0 0 0 0 0 0 0 0 0',
' 2.1897 1.1289 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.0000 0.6228 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1.7566 0.2224 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.7835 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 6 7 1 0 0 0',
' 5 7 1 0 0 0',
' 1 5 1 0 0 0',
' 4 6 1 0 0 0',
' 3 4 1 0 0 0',
' 2 3 1 0 0 0',
' 1 2 1 0 0 0',
'M END'
];
tmpl.template_7 = ['', ' Ketcher 12101118392D 1 1.00000 0.00000 0', '',
' 8 8 0 0 0 999 V2000',
' 0.0000 0.7053 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.0000 1.7078 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.7053 2.4131 0.0000 C 0 0 0 0 0 0 0 0 0',
' 0.7056 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1.7079 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0',
' 2.4133 0.7053 0.0000 C 0 0 0 0 0 0 0 0 0',
' 2.4133 1.7078 0.0000 C 0 0 0 0 0 0 0 0 0',
' 1.7079 2.4131 0.0000 C 0 0 0 0 0 0 0 0 0',
' 8 3 1 0 0 0',
' 7 8 1 0 0 0',
' 6 7 1 0 0 0',
' 5 6 1 0 0 0',
' 4 5 1 0 0 0',
' 1 4 1 0 0 0',
' 2 3 1 0 0 0',
' 1 2 1 0 0 0',
'M END'
];
ketcher.button_areas.chain = ketcher.showMolfileOpts('chain', tmpl.chain, 20, renderOptsPattern);
ketcher.button_areas.template_0 = ketcher.showMolfileOpts('template', tmpl.template_0, 20, renderOptsPattern);
// TODO code cleanup
/*
tmpl.rgroup_label = ['', ' Ketcher 12131120282D 1 1.00000 0.00000 0', '',
' 1 0 0 0 0 999 V2000',
' 0.0000 0.0000 0.0000 R# 0 0 0 0 0 0 0 0 0',
'M END'
];
tmpl.rgroup_attpoints = ['', ' Ketcher 12131120282D 1 1.00000 0.00000 0', '',
' 1 0 0 0 0 0 0 0 0 0999 V2000',
' 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0',
'M APO 1 1 1',
'M END'
];
ketcher.button_areas.rgroup_label = ketcher.showMolfileOpts('rgroup', tmpl.rgroup_label, 75, renderOpts);
*/
ui.init();
};
ketcher.getSmiles = function ()
{
var saver = new chem.SmilesSaver();
return saver.saveMolecule(ui.ctab, true);
};
ketcher.getMolfile = function ()
{
var saver = new chem.MolfileSaver();
return saver.saveMolecule(ui.ctab, true);
};
ketcher.setMolecule = function (mol_string)
{
if (!Object.isString(mol_string))
return;
ui.loadMolecule(mol_string);
};
ketcher.addFragment = function (mol_string)
{
if (!Object.isString(mol_string))
return;
ui.loadMolecule(mol_string, undefined, undefined, true);
};
ketcher.showMolfile = function (clientArea, molfileText, autoScale, hideImplicitHydrogen)
{
return ketcher.showMolfileOpts(clientArea, molfileText, 75, {
'showSelectionRegions':false,
'showBondIds':false,
'showHalfBondIds':false,
'showLoopIds':false,
'showAtomIds':false,
'autoScale':autoScale||false,
'autoScaleMargin':20,
'hideImplicitHydrogen':hideImplicitHydrogen||false
});
};
ketcher.showMolfileOpts = function (clientArea, molfileText, bondLength, opts)
{
this.render = new rnd.Render(clientArea, bondLength, opts);
if (molfileText)
this.render.setMolecule(chem.Molfile.parseCTFile(typeof(molfileText)=='string' ? molfileText.split('\n') : molfileText));
this.render.update();
return this.render;
};
/*
ketcher.testShiftRayBox = function (clientArea)
{
var bx = 50, by = 180, bw = 220, bh = 40;
var b = new util.Box2Abs(bx, by, bx + bw, by + bh);
var c = new Raphael(clientArea);
c.rect(0, 0, 300, 300).attr({stroke:'#0f0'});
c.rect(bx, by, bw, bh).attr({stroke:'#00f'});
for (var i = 0; i < 3; ++i)
{
var p = new util.Vec2(Math.random() * 300, Math.random() * 300);
var d = new util.Vec2(Math.random() * 300, Math.random() * 300);
var p1 = p.add(d);
c.circle(p.x, p.y, 4).attr({fill:'#0f0'});
c.path("M{0},{1}L{2},{3}", p.x, p.y, p1.x, p1.y).attr({'stroke-width':'3','stroke':'#f00'});
var t = Math.max(0, util.Vec2.shiftRayBox(p, d, b));
var p0 = p.addScaled(d, t / d.length());
c.path("M{0},{1}L{2},{3}", p0.x, p0.y, p1.x, p1.y).attr({'stroke-width':'1','stroke':'#000'});
}
}
ketcher.runTest = function(test, context) {
var runs = [], nr = 5, diff;
for (var r = 0; r < nr; ++r) {
var start = (new Date).getTime();
for ( var n = 0; (diff = (new Date).getTime() - start) < 1000; n++ )
test.call(context);
runs.push( diff / n );
}
var avg = 0, std = 0, t;
for (r = 0; r < nr; ++r) {
t = runs[r];
avg += t;
std += t * t;
}
avg /= nr;
std = Math.sqrt(std - avg * avg) / nr;
console.log(avg + ' ' + std);
}
*/