-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
187 lines (171 loc) · 6.67 KB
/
index.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
/* jshint node: true */
"use strict";
var dictionary = require('./scripts/code-lookup.js'),
instructions = require('./scripts/instruction-lookup.js'),
pkg = require('./package.json'),
program = require('commander'),
main = require('./scripts/main.js'),
inquirer = require('inquirer'),
constants = require('./scripts/const.js');
program
.version(pkg.version)
.option('-c, --process-codes <file>', 'Takes <file> with a list of codes, and outputs a file with code, drug family, drug type and dose(mg)')
.option('-i, --process-instructions <file>', 'Takes <file> with a list of drug instructions (e.g. "take one a day"), and outputs a file with instruction and tablets per day')
.option('-a, --process-all <file>', 'Takes <file> with a list of drug information and outputs the data necessary for the algorithm')
.option('-x, --process-x <file>', 'Takes <file> and assumes a specific column ordering and a header')
.parse(process.argv);
var doneFn = function(err) {
if (err) {
console.log(err);
process.exit(1);
}
process.exit(0);
};
if (program.processCodes) {
dictionary.process(program.processCodes, doneFn);
} else if (program.processInstructions) {
instructions.process(program.processInstructions, doneFn);
} else if (program.processX) {
main.process(program.processX, [constants.PATIENT_ID, constants.DATE, constants.TABLETS_PRESCRIBED, constants.CLINICAL_CODE, constants.INSTRUCTION], false, function(err) {
if (err) {
console.log(err);
process.exit(1);
}
console.log("All completed successfully");
console.log();
console.log();
console.log("You should now execute: ");
console.log();
console.log("npm run -s sort " +program.processX+".done > "+program.processX+".done.sorted");
console.log();
console.log("Followed by:");
console.log();
console.log("perl parse_drug_file.pl "+program.processX+".done.sorted");
process.exit(0);
});
} else if (program.processAll) {
main.columns(program.processAll, 5, function(err, columns) {
if (err) {
console.log(err);
process.exit(1);
}
inquirer.prompt({
type: "confirm",
message: "Is this a header row: " + columns.map(function(el) {
return el[0];
}).join("|"),
name: "confirm",
default: true
}, function(val) {
var isHeader = val.confirm;
var columnTypes = [];
var hasCode = false;
var hasInstructionOrTabsPerDay = false;
var choices = [{
name: "Patient identifier",
value: constants.PATIENT_ID
}, {
name: "Date of the prescription",
value: constants.DATE
}, {
name: "Number of tablets prescribed",
value: constants.TABLETS_PRESCRIBED
}, {
name: "Clinical code",
value: constants.CLINICAL_CODE
}, {
name: "Drug family (e.g. Beta blocker, ACE Inhibitor, Loop diuretic)",
value: constants.DRUG_FAMILY
}, {
name: "Drug active ingredient (e.g. Propranolol, Captropril, Amlodipine)",
value: constants.DRUG_TYPE
}, {
name: "Dose per tablet in mg",
value: constants.DOSE
}, {
name: "Prescription instruction (e.g. take one daily, 2 twice a day, one in the morning)",
value: constants.INSTRUCTION
}, {
name: "Tablets per day",
value: constants.TABLETS_PER_DAY
}, {
name: "None of the above (this column will therefore be ignored)",
value: constants.IGNORE
}];
var q = function() {
var head;
if(isHeader) {
head = columns[0].shift();
}
inquirer.prompt({
type: "list",
choices: choices,
message: "Please identify the elements in the column " + (head ? "with header '"+ head + "' " :"") + "containing: \n" + JSON.stringify(columns[0]),
name: "col"
}, function(val) {
columns.shift();
if (val.col === constants.CLINICAL_CODE) hasCode = true;
if (val.col === constants.INSTRUCTION || val.col === constants.TABLETS_PER_DAY) hasInstructionOrTabsPerDay = true;
columnTypes.push(val.col);
choices = choices.filter(function(v) {
return v.value === constants.IGNORE || v.value !== val.col;
});
if (columns.length > 0) {
q();
} else {
var isOk = true;
//Check for essential columns
choices.forEach(function(el) {
if (el.value === constants.PATIENT_ID) {
isOk = false;
console.log("Your data does not contain a patient identifier - this is necessary for the algorithm to work correctly.");
} else if (el.value === constants.DATE) {
isOk = false;
console.log("Your data does not contain a prescription date - this is necessary for the algorithm to work correctly.");
} else if (el.value === constants.TABLETS_PRESCRIBED) {
isOk = false;
console.log("Your data does not contain the number of tablets prescribed - this is necessary for the algorithm to work correctly.");
}
});
//check for type
if (!hasCode && columnTypes.filter(function(v) {
return [constants.DRUG_TYPE, constants.DOSE].indexOf(v) > -1;
}).length !== 2) {
isOk = false;
console.log("Your must contain either a clinical code - or a drug active ingredient and dose for the algorithm to work correctly.");
}
if (!hasInstructionOrTabsPerDay) {
isOk = false;
console.log("Your must contain either an instruction or the number of tabs per day for the algorithm to work correctly.");
}
if (isOk) {
main.process(program.processAll, columnTypes, isHeader, function(err) {
if (err) {
console.log(err);
process.exit(1);
}
console.log("All completed successfully");
console.log();
console.log();
console.log("You should now execute: ");
console.log();
console.log("npm run -s sort " +program.processAll+".done > "+program.processAll+".done.sorted");
console.log();
console.log("Followed by:");
console.log();
console.log("perl parse_drug_file.pl "+program.processAll+".done.sorted");
process.exit(0);
});
} else {
process.exit(1);
}
}
});
};
q();
});
});
} else {
console.error('no command given!');
program.help();
}