-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmrSim v1.0.js
303 lines (268 loc) · 11.3 KB
/
nmrSim v1.0.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
///NMR SIM v1.0
var log = console.log;
var _ = require('lodash');
var inspect = require('util').inspect;
var prompt = require('prompt');
var fs = require('fs');
var config = JSON.parse(fs.readFileSync('./nmrSim.cfg.txt'));
var machines = {
'300WB': {
topshim: false,
supportedExperiments: [],
},
'300NB': {
topshim: true,
supportedExperiments: []
},
'400': {
topshim: true,
supportedExperiments: []
},
'400SL': {
topshim: true,
supportedExperiments: []
},
'500SL': {
topshim: true,
supportedExperiments: []
}
};
function forceQuit() {
log('\tterminating application...');
process.exit();
}
function confirm(command, ask, require, key, cb) {
log(ask);
prompt.get("(Y/N)", (err, res) => {
switch (res["(Y/N)"]) {
case "Y":
case "y":
case "yes":
case "YES":
confirmations[key] = true;
if (cb) cb();
else progress(command);
break;
default:
log(require);
confirm(command, ask, require, key, cb);
}
});
}
var confirmations = {
'cleaned sample': (JSON.parse(config['use confirmations'])) ? false : true,
'selected directory': (JSON.parse(config['use confirmations'])) ? false : true,
'lock': false,
'new': false,
'details': (JSON.parse(config['use confirmations'])) ? false : true,
'rsh today': false,
'atma': false,
'rpar': false,
'shim': false,
'bsmsdisp': (JSON.parse(config['use confirmations'])) ? false : true
};
var hints = {
'New/Lock': {
'lock': () => (confirmations['lock']) ? false : "hint: you must lock in your solvent signal",
'new': () => (confirmations['new']) ? false : 'hint: you are ready to create a new file',
'rsh today': () => (confirmations['rsh today']) ? false : 'hint: you should reset the shims to today\'s standard best values',
},
'rpar/shim': {
'shim': () => (confirmations['shim']) ? false : "hint: you must shim your sample",
'rpar': () => (confirmations['rpar']) ? false : "hint: you must load the parameters for your experiment",
},
ej: 'hint: the blank must be ejected and removed from the machine...',
ij: 'hint: you are ready to inject your sample into the NMR machine',
atma: 'hint: you are ready to tune your probe',
start: 'hint: you are ready to start the NMR experiment',
zg: 'hint: you are ready to start the heteronucleus NMR experiment (use "zg", not "start")',
tr: 'hint: you are ready to transfer your data',
efp: 'hint: you are ready to efp'
};
var solvents = [
'c6d6',
'cdcl3',
'd2o'
];
log('\n----- NMR Sim 1.0 ----- \n');
log(`Supported solvents: ${inspect(solvents)}\n`);
_.each(solvents, (solvent, i) => {
solvents[i] = `lock ${solvent}`;
});
var parameterFile = ['protonstd', 'carbon.top', 'fluorine.top', 'phosphorus.top'];
log(`Supported Parameters: ${inspect(parameterFile)}\n`);
_.each(parameterFile, (file, i) => {
parameterFile[i] = `rpar ${file}`;
});
function progress(command) {
switch (command) {
case 'init':
getNextCommand('ej', "command", hints.ej)
break;
case 'ej':
if (!confirmations['cleaned sample']) confirm(command,
'Did you clean your sample with ethanol and use the sizer to ensure proper fit?',
'You must clean your sample so that no particles get lodged in the machine, as these can interfere with everyone\'s experiments. \nYou must use the sizer to ensure proper fit',
'cleaned sample');
else getNextCommand('ij', "command", hints.ij);
break;
case 'ij':
unorderedCommand(['new', 'lock', 'rsh today'], 'New/Lock', hints['New/Lock'], 'command', {
'lock': solvents
}, (input, commandArray) => {
if (input == 'new') if (!confirmations['details']) {
confirm(undefined,
'Did you type in\n -Title \n -Experiment # \n -Procedure # \n -etc?',
'You must type in the descriptive information for this experiment',
'details', () => {
confirmations['new'] = true;
var commandsRemaining = _.without(commandArray, 'new');
if (commandsRemaining.length == 0) { progress('New/Lock') }
else unorderedCommand(commandsRemaining, 'New/Lock', hints['New/Lock'], 'command', {
'lock': solvents
});
});
return true; //interrupts usual code to only run above code instead
}
});
break;
case 'New/Lock':
unorderedCommand(['rpar', 'shim'], 'rpar/shim', hints['rpar/shim'], 'command', {
'rpar': parameterFile,
'shim': ((config.machine == "300WB") || (config.machine == "300wb")) ? ['bsmsdisp'] : ['bsmsdisp', 'topshim']
},
(input, commandArray) => {
if (input.lastIndexOf('rpar') != -1) {
if (input.lastIndexOf('protonstd') != -1) {
confirmations.needATMA = false;
} else {
confirmations.needATMA = true;
}
}
if (input == 'bsmsdisp') if (!(confirmations['bsmsdisp'])) {
confirm(undefined,
'Did you manually shim Z, then Z^2, and alternate between the two until the lock signal was maximized (finishing with Z)?',
'You must manually shim Z, then Z^2, and alternate between the two until lock signal is maximized (finishing with Z)',
'bsmsdisp', () => {
confirmations['shim'] = true;
var commandsRemaining = _.without(commandArray, 'shim');
if (commandsRemaining.length == 0) { progress('rpar/shim'); }
else unorderedCommand(commandsRemaining, 'rpar/shim', hints['rpar/shim'], 'command', {
'rpar': parameterFile,
'shim': (String(config.machine).toUpperCase() == "300WB") ? ['bsmsdisp'] : ['bsmsdisp', 'topshim']
}, input => {
if (input.lastIndexOf('rpar') != -1) {
if (input.lastIndexOf('protonstd') != -1) {
confirmations.needATMA = false;
} else {
confirmations.needATMA = true;
}
}
});
});
return true;
}
});
break;
case 'rpar/shim':
if (confirmations.needATMA) getNextCommand('atma', 'command', hints.atma);
else getNextCommand('start', 'command', hints.start);
break;
case 'atma':
getNextCommand('zg', 'command', hints.zg);
break;
case 'start':
case 'zg':
log('scanning...', '(please wait)');
setTimeout(() => {
log('scans complete');
setTimeout(() => { getNextCommand('tr', 'command', hints.tr); }, 500);
}, 500);
break;
case 'tr':
//log('you have now transferred the data from the scans');
getNextCommand('efp', 'command', hints.efp);
break;
case 'efp':
log('Congratulations, your NMR data is now displayed on screen!')
log('\nDon\'t forget to open the terminal (under applications),\ntype "update", \nhit enter, \ntype your password (it will be invisible, just type anyways),\nand hit enter again to save your data to the NMR server!\n\n\t----- simulation complete -----');
log('\nHit Enter to terminate application');
prompt.get('(press Enter)',(err,res)=>{
forceQuit();
});
break;
default:
log('\n \t critical error: progress(undefined)')
}
}
var getNextCommand = function (nextCommand, type, hint) {
log('\nReady for command');
prompt.get(type, (err, res) => {
if (res[type] == 'exit') forceQuit();
if (res[type] == nextCommand) {
log(nextCommand, 'done \n');
progress(nextCommand);
}
else {
log('command INCORRECT \n');
if (hint) log(hint);
getNextCommand(nextCommand, type, hint);
}
});
}
var optionGetNextCommand = function (command, nextCommand, hint, type) {
log('\nReady for command');
prompt.get(type, (err, res) => {
if (res[type] == 'exit') forceQuit();
if (res[type].lastIndexOf(command) != -1) {
log(res[type], 'done \n');
progress(nextCommand);
}
else {
log('command INCORRECT \n');
if (hint) log(hint);
optionGetNextCommand(command, nextCommand, hint, type);
}
});
};
var unorderedCommand = function (commandArray, nextCommand, hintOBJ, type, options, immediate) {
log('\nReady for command');
prompt.get(type, (err, res) => {
if (res[type] == 'exit') forceQuit();
if (immediate) if (immediate(res[type], commandArray)) { return true; }
var validCommand;
_.each(commandArray, (com) => {
if (options[com]) {
_.each(options[com], (option, i) => {
if (res[type] == option) {
validCommand = com;
return false;
}
});
} else if (res[type] == com) {
validCommand = com;
return false;
}
});
if (validCommand) {
log(res[type], 'done \n');
var commandsRemaining = _.without(commandArray, validCommand);
confirmations[validCommand] = true;
if (commandsRemaining.length == 0) {
progress(nextCommand);
}
else {
unorderedCommand(commandsRemaining, nextCommand, hintOBJ, type, options, immediate);
}
}
else {
log('command INCORRECT \n');
if (hintOBJ) _.each(hintOBJ, hint => { if (hint()) log(hint()) });
unorderedCommand(commandArray, nextCommand, hintOBJ, type, options, immediate);
}
});
};
prompt.start();
log(`You are using the "Bruker ${String(config.machine).toUpperCase()}"\n`);
log('A blank sample is currently in the machine \n');
progress('init');