-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
374 lines (329 loc) · 16.1 KB
/
script.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
$(document).ready(function () {
var reagentButton;
var productButton;
var finalReagent;
var finalProduct;
var addConsiderationR = null;
var addConsiderationP = null;
var finalProductNew = null;
$(".reagents button").click(function () {
if ($(this).css("background-color") !== "azure") { // not previously selected
$(this).css("background-color", "azure");
$(".reagents button").not(this).css("background-color", "rgb(180, 242, 233)");
}
reagentButton = $(this).text();
})
$(".products button").click(function () {
if ($(this).css("background-color") !== "azure") { // not previously selected
$(this).css("background-color", "azure");
$(".products button").not(this).css("background-color", "rgb(180, 242, 233)");
}
productButton = $(this).text();
})
// for structural alcohol/HX reagent settings:
$(".primarysec").click(function() { // enable buttons
$(".structural-reagent button:contains('1°')").prop("disabled", false);
$(".structural-reagent button:contains('2°')").prop("disabled", false);
$(".structural-reagent button:contains('3°')").prop("disabled", false);
})
$(".structural-reagent button").click(function() {
$(this).removeClass("inactive").addClass("active");
$(".structural-reagent button").not(this).removeClass("active").addClass("inactive");
addConsiderationR = $(this).text();
})
$(".reagents button").not(".primarysec").click(function() {
$(".structural-reagent button:contains('1°')").prop("disabled", true);
$(".structural-reagent button:contains('2°')").prop("disabled", true);
$(".structural-reagent button:contains('3°')").prop("disabled", true);
addConsiderationR = null;
})
$("#alcoholreagent").click(function() {
$(".structural-reagent button:contains('Allylic')").prop("disabled", false);
$(".structural-reagent button:contains('Benzylic')").prop("disabled", false);
$(".structural-reagent button:contains('Propargylic')").prop("disabled", false);
})
$(".reagents button").not("#alcoholreagent").click(function() {
$(".structural-reagent button:contains('Allylic')").prop("disabled", true);
$(".structural-reagent button:contains('Benzylic')").prop("disabled", true);
$(".structural-reagent button:contains('Propargylic')").prop("disabled", true);
addConsiderationR = null;
})
// for structural alcohol/HX product settings:
$(".primarysecprod").click(function() {
$(".structural-product button:contains('1°')").prop("disabled", false);
$(".structural-product button:contains('2°')").prop("disabled", false);
$(".structural-product button:contains('3°')").prop("disabled", false);
})
$(".structural-product button:contains('1°')").click(function() {
$(this).removeClass("inactive").addClass("active");
$(".structural-product button").not(this).removeClass("active").addClass("inactive");
addConsiderationP = $(this).text();
})
$(".structural-product button:contains('2°')").click(function() {
$(this).removeClass("inactive").addClass("active");
$(".structural-product button").not(this).removeClass("active").addClass("inactive");
addConsiderationP = $(this).text();
})
$(".structural-product button:contains('3°')").click(function() {
$(this).removeClass("inactive").addClass("active");
$(".structural-product button").not(this).removeClass("active").addClass("inactive");
addConsiderationP = $(this).text();
})
$(".products button").not(".primarysecprod").click(function() {
$(".structural-product button:contains('1°')").prop("disabled", true);
$(".structural-product button:contains('2°')").prop("disabled", true);
$(".structural-product button:contains('3°')").prop("disabled", true);
addConsiderationP = null;
})
// for Zaitsev or Hofmann product
$("#alkene").click(function() { // enable Zaitsev and Hofmann options
$(".regiochemistry button:contains('Zaitsev')").prop("disabled", false);
$(".regiochemistry button:contains('Hofmann')").prop("disabled", false);
})
$(".regiochemistry button:contains('Zaitsev')").click(function() {
$(this).removeClass("inactive").addClass("active");
$(".regiochemistry button").not(this).removeClass("active").addClass("inactive");
finalProductNew = "Zaitsev product";
})
$(".regiochemistry button:contains('Hofmann')").click(function() {
$(this).removeClass("inactive").addClass("active");
$(".regiochemistry button").not(this).removeClass("active").addClass("inactive");
finalProductNew = "Hofmann product";
})
$(".products button").not("#alkene").click(function() { // enable Zaitsev and Hofmann options
$(".regiochemistry button:contains('Zaitsev')").prop("disabled", true);
$(".regiochemistry button:contains('Hofmann')").prop("disabled", true);
finalProductNew = null;
})
// for Markovnikov or anti-Markovnikov
$(".primarysecprod").click(function() { // enable Markovnikov and anti-Markovnikov options
$(".regiochemistry button:contains('Markovnikov')").prop("disabled", false);
$(".regiochemistry button:contains('Anti-Markovnikov')").prop("disabled", false);
})
$(".regiochemistry button:contains('Markovnikov')").click(function() {
$(this).removeClass("inactive").addClass("active");
$(".regiochemistry button").not(this).removeClass("active").addClass("inactive");
addConsiderationP = $(this).text();
})
$(".regiochemistry button:contains('Anti-Markovnikov')").click(function() {
$(this).removeClass("inactive").addClass("active");
$(".regiochemistry button").not(this).removeClass("active").addClass("inactive");
addConsiderationP = $(this).text();;
})
$(".products button").not(".primarysecprod").click(function() { // disable Markovnikov and anti-Markovnikov options
$(".regiochemistry button:contains('Markovnikov')").prop("disabled", true);
$(".regiochemistry button:contains('Anti-Markovnikov')").prop("disabled", true);
addConsiderationP = null;
})
// for generating result with Go button
$("#goBtn").click(function () {
if (addConsiderationR !== null) {
finalReagent = addConsiderationR + ' ' + reagentButton;
} else {
finalReagent = reagentButton;
}
if (addConsiderationP !== null) {
finalProduct = addConsiderationP + " " + productButton;
} else if (finalProductNew !== null) {
finalProduct = finalProductNew;
} else {
finalProduct = productButton;
}
$("#finalResult").html(map.findPathWithDijkstra(finalReagent, finalProduct));
//$("#finalResult").toggle("slow")
});
$("#resetBtn").click(function() {
$("#finalResult").html("");
$(".reagents button").css("background-color", "rgb(180, 242, 233)");
$(".products button").css("background-color", "rgb(180, 242, 233)");
$(".considerations button").prop("disabled", true);
$(".considerations button").removeClass("active").addClass("inactive");
finalProductNew = null;
addConsiderationP = null;
addConsiderationR = null;
})
});
class Graph {
#edgeParams;
constructor() {
this.nodes = [];
this.adjacencyList = {};
}
addNodes(arrNodes) {
for (let node of arrNodes) {
this.nodes.push(node);
this.adjacencyList[node] = [];
}
}
addEdges(arrEdges) {
for (let edge of arrEdges) {
this.#edgeParams = edge.split("--");
//node1 = this.#edgeParams[0];
//node2 = this.#edgeParams[1];
//weight = this.#edgeParams[2];
this.adjacencyList[this.#edgeParams[0]].push({node:this.#edgeParams[1], weight: parseInt(this.#edgeParams[2]), other: this.#edgeParams[3]});
}
}
findPathWithDijkstra(startNode, endNode) {
if (startNode === undefined || endNode === undefined) {
return "Please enter valid inputs.";
}
let moles = {}; // distance map
let backtrace = {}; // prevVertices map
let otherMap = {}; // other reagents map
let pq = new PriorityQueue();
moles[startNode] = 0;
this.nodes.forEach(node => {
if (node !== startNode) {
moles[node] = Infinity
}
});
pq.enqueue([startNode, 0]);
while (!pq.isEmpty()) {
let shortestStep = pq.dequeue();
let currentNode = shortestStep[0];
for (let neighbor of this.adjacencyList[currentNode]) {
let mole = moles[currentNode] + neighbor.weight;
if (mole < moles[neighbor.node]) {
moles[neighbor.node] = mole;
backtrace[neighbor.node] = currentNode;
otherMap[neighbor.node] = neighbor.other;
pq.enqueue([neighbor.node, mole]);
}
}
}
let path = [endNode];
let lastStep = endNode;
while(lastStep !== startNode) {
path.unshift("------" + otherMap[lastStep] + "------>")
path.unshift(backtrace[lastStep]) // unshift adds to front of array
if (path[0] === undefined) {
return "Path not found!";
}
lastStep = backtrace[lastStep]
}
return `${path.join(" ")}`
}
}
class PriorityQueue {
constructor() {
this.collection = [];
}
enqueue(element){
if (this.isEmpty()){
this.collection.push(element);
} else {
let added = false;
for (let i = 1; i <= this.collection.length; i++){
if (element[1] < this.collection[i-1][1]){
this.collection.splice(i-1, 0, element);
added = true;
break;
}
}
if (!added){
this.collection.push(element);
}
}
};
dequeue() {
let value = this.collection.shift();
return value;
};
isEmpty() {
return (this.collection.length === 0)
};
}
let map = new Graph();
map.addNodes(["Alkyl halide", "Alkane", "Vicinal dihalide", "Geminal dihalide", "Methanol", "Alcohol", "Epoxide", "Alkene", "Alkyne", "Cyclopropane", "Chloroform", "Diiodomethane", "Alkylborane", "Conjugated diene", "Ether", "Halohydrin", "Diol", "Carboxylic acid", "Dichlorocarbene", "Carbenoid", "Cyclohexene", "Alkynide ion", "Extended alkyne", "1° Alcohol", "1° Alkyl halide", "2° Alcohol", "2° Alkyl halide", "3° Alcohol", "3° Alkyl halide", "Zaitsev product", "Hofmann product", "Syn", "Anti", "Allylic Alcohol", "Benzylic Alcohol", "Propargylic Alcohol", "Sulfonate ester", "Ketone", "Acetal", "Anti-Markovnikov Alkyl halide", "Markovnikov Alkyl halide", "Anti-Markovnikov Alcohol", "Markovnikov Alcohol", "Vinyl halide", "Aldehyde", "Acyl chloride", "Ester", "Anhydride", "Nitrile", "Amide", "Amine"]);
map.addEdges([
"Alkyl halide--Alkene--1--base or nucleophile with heat",
"Alkane--Alkyl halide--1--X-X with light",
"Alcohol--Alkyl halide--1--HX or PBr3/SOCl2", // added PBr3/SOCl2
"Epoxide--Halohydrin--1--HX",
"Epoxide--Diol--1--aqueous acid or H2O",
"Alkene--Alkyl halide--1--HBr, peroxides (anti-Markov) or HX (Markov)",
"Alkene--Epoxide--1--Peroxy acid or MCPBA or MMPP",
"Alkene--Alkane--1--H2 with Pt/C or Pd/C",
"Alkene--Vicinal dihalide--1--X-X in non-nucleophilic solvent",
"Alkene--Halohydrin--1--X-X in water",
"Alkene--Alcohol--1--H2O, catalytic HA or Hg(OAc)2 (Markov) or BH3:THF (anti-Markov)",
"Alkene--Cyclopropane--1--carbene or carbenoid",
"Alkene--Diol--1--OsO4",
"Alkyne--Alkane--1--H2 with Pt/C or Pd/C, Ra-Ni",
"Vicinal dihalide--Alkyne--3--strong base like NaNH2",
"Geminal dihalide--Alkyne--3--strong base like NaNH2",
"Methanol--Alkyl halide--1--HX",
"Alkylborane--Alkane--1--Acetic acid and heat",
"Alkyne--Carboxylic acid--1--Hot KMnO4, OH, H3O+ or O3",
"Chloroform--Dichlorocarbene--1--Strong bases like tert-butoxide or NaNH2",
"Diiodomethane--Carbenoid--1--Zn(Cu)",
"Conjugated diene--Alkene--1--H-X",
"Conjugated diene--Cyclohexene--1--Dienophile",
"Halohydrin--Epoxide--1--Strong base or sodium metal",
"Alkyne--Alkynide ion--1--Strong base like NaNH2",
"Alkynide ion--Extended alkyne--1--HX",
"Alkyne--Alkene--1--H2 with LC or Na reduction",
"1° Alcohol--1° Alkyl halide--1--SOCl2 for chloride, PBr3 for bromide, or HX",
"1° Alkyl halide--Hofmann product--1--Strong bulky base with heat",
"1° Alkyl halide--Alkene--1--Strong bulky base with heat",
"2° Alkyl halide--Alkene--1--Strong bulky base with heat",
"3° Alkyl halide--Alkene--1--Strong bulky base with heat",
"2° Alkyl halide--Zaitsev product--1--Weak nucleophile with heat",
"2° Alkyl halide--Zaitsev product--1--Strong base with heat",
"3° Alkyl halide--Zaitsev product--1--Weak or strong base with heat",
"3° Alkyl halide--Hofmann product--1--Strong bulky base with heat or base w/o antiperiplanar orientation",
"2° Alkyl halide--Hofmann product--1--Strong bulky base with heat or base w/o antiperiplanar orientation",
"Allylic Alcohol--Alkyl halide--1--HX", "Benzylic Alcohol--Alkyl halide--1--HX",
"Propargylic Alcohol--Alkyl halide--1--HX",
"1° Alcohol--Alkyl halide--1--HX and acid-promoted",
"1° Alcohol--Sulfonate ester--1--Sulfonyl chloride",
"Alcohol--Sulfonate ester--1--Sulfonyl chloride",
"1° Alcohol--Ether--1--Strong Bronsted acids at lower temperature",
"1° Alcohol--Alkene--1--Strong conc. acid and heat",
"1° Alcohol--Zaitsev product--1--Strong conc. acid and heat",
"2° Alcohol--2° Alkyl halide--1--SoCl2 for chloride or PBr3 for bromide",
"2° Alcohol--Alkene--1--Strong conc. acid and heat",
"2° Alcohol--Zaitsev product--1--Strong conc. acid and heat",
"3° Alcohol--Zaitsev product--1--Strong conc. acid and heat",
"3° Alcohol--Alkene--1--Strong conc. acid and heat",
"Ketone--Acetal--1--Alcohol and TsOH", // replaced hydronium with TsOH
"Acetal--Ketone--1--Acid",
"Ketone--3° Alcohol--1--R-MgX with hydronium",
"Ketone--2° Alcohol--1--NaBH4",
"Alkene--Anti-Markovnikov Alkyl halide--1--HBr with peroxides",
"Alkene--Markovnikov Alkyl halide--1--HX",
"Alkene--Markovnikov Alcohol--1--Aqueous acid",
"Alkene--Anti-Markovnikov Alcohol--1--B2H6 with H2O2 or BH3:THF",
"Alkyne--Geminal dihalide--2--HX",
"Alkyne--Vinyl halide--1--HBr with peroxides",
"Acyl chloride--Ketone--1--Cuprate",
"Acyl chloride--1° Alcohol--1--LAH and H2O",
"Acyl chloride--Carboxylic acid--1--H2O, py",
"Carboxylic acid--Acyl chloride--1--SOCl2 or PCl3 or PCl5",
"Acyl chloride--Amide--1--NH3 or R-NH2",
"Acyl chloride--Ester--1--ROH",
"Ester--Carboxylic acid--1--aq. acid or aq. base",
"Carboxylic acid--Ester--1--ROH with H2SO4 or RX with K2CO3",
"Acyl chloride--3° Alcohol--1--RMgBr with H2O",
"Aldehyde--Carboxylic acid--1--Ag2O",
"1° Alcohol--Carboxylic acid--1--KMnO4, -OH, heat",
"Acyl chloride--1° Alcohol--1--LAH and H2O",
"Acyl chloride--Aldehyde--1--LiAlH(tBuO)3, H3O+",
"Ester--1° Alcohol--1--LAH and H2O",
"Anhydride--Ester--1--ROH and DMAP",
"Carboxylic acid--Amide--1--NH3 and DCC",
"Amide--Carboxylic acid--1--aq. acid or aq. base",
"Carboxylic acid--Anhydride--1--Acyl chloride",
"Anhydride--Amide--1--NH3 or R-NH2",
"Nitrile--Carboxylic acid--1--H3O+, heat",
"Amide--Nitrile--1--P2O5",
"Nitrile--Amide--1--aq. acid or aq. base",
"Nitrile--Amine--1--LAH, H3O+",
"Amide--Amine--1--LAH, H3O+",
"Amine--Amide--1--Acyl chloride",
"Nitrile--Aldehyde--1--DIBAL-H, H3O+",
"Nitrile--Ketone--1--RMgBr",
"Alcohol--Acetal--1--Ketone and acid", // added this edge
"Alkyl borane--Alcohol--H2O2/NaOH"]); // added this last edge
console.log(map.findPathWithDijkstra("1° Alcohol", "1° Alkyl halide"));