-
Notifications
You must be signed in to change notification settings - Fork 39
/
snomed-dedup-and-filter.ts
302 lines (278 loc) · 10.2 KB
/
snomed-dedup-and-filter.ts
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
import fs from "fs";
import path from "path";
import { getCodeDisplay, getCodeDetailsFull } from "./term-server-api";
import { populateHashTableFromCodeDetails, SnomedHierarchyTableEntry } from "./snomed-heirarchies";
import { RemovalStats, createInitialRemovalStats, prettyPrintRemovalStats } from "./stats";
async function processDirectoryOrFile(
directoryOrFile: string,
processFile: (filePath: string) => Promise<void>
) {
const stat = fs.statSync(directoryOrFile);
if (stat.isFile()) {
if (directoryOrFile.endsWith(".json")) {
await processFile(directoryOrFile);
}
return;
}
const items = fs.readdirSync(directoryOrFile, { withFileTypes: true });
for (const item of items) {
const sourcePath = path.join(directoryOrFile, item.name);
if (item.isDirectory()) {
await processDirectoryOrFile(sourcePath, processFile);
} else if (item.isFile() && item.name.endsWith(".json")) {
await processFile(sourcePath);
}
}
}
async function computeHashTable(
filePath: string,
hashTable: Record<string, SnomedHierarchyTableEntry>
) {
const data = JSON.parse(fs.readFileSync(filePath, "utf8"));
const entries = data.bundle ? data.bundle.entry : data.entry;
for (const entry of entries) {
const resource = entry.resource;
if (resource && resource.resourceType === "Condition") {
const codings = resource.code?.coding || [];
for (const coding of codings) {
if (coding.system === "http:https://snomed.info/sct") {
const codeDetails = await getCodeDetailsFull(coding.code, "SNOMEDCT_US");
if (codeDetails) {
await populateHashTableFromCodeDetails(hashTable, coding.code, resource.id);
}
}
}
}
}
}
async function removeNonRootSnomedCodes(
filePath: string,
hashTable: Record<string, SnomedHierarchyTableEntry>,
conditionIdsDictionary: Set<string>,
allRemainingEnries: Set<string>,
removalStats: RemovalStats
) {
const data = JSON.parse(fs.readFileSync(filePath, "utf8"));
const entries = data.bundle ? data.bundle.entry : data.entry;
for (let i = entries.length - 1; i >= 0; i--) {
const entry = entries[i];
const resource = entry.resource;
if (resource && resource.resourceType === "Condition") {
conditionIdsDictionary.add(resource.id);
const codings = resource.code?.coding || [];
for (const coding of codings) {
if (coding.system === "http:https://snomed.info/sct") {
if (hashTable[coding.code] && !hashTable[coding.code].root) {
removalStats.nonSnomedRootCodes.count += 1;
removalStats.nonSnomedRootCodes.codes.add(coding.code);
console.log("Removing non-root SNOMED code:", coding.code, "Resource.id:", resource.id);
entries.splice(i, 1);
break;
} else {
hashTable[coding.code].inserted = true;
allRemainingEnries.add(resource.id);
break;
}
}
}
}
}
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf8");
}
async function removeConditionsProceduresMedAdmins(
filePath: string,
conditionSet: Set<string>,
cptSet: Set<string>,
medicationConditionDict: Record<string, string>,
allRemainingEnries: Set<string>,
removalStats: RemovalStats
) {
const data = JSON.parse(fs.readFileSync(filePath, "utf8"));
const entries = data.bundle ? data.bundle.entry : data.entry;
for (let i = entries.length - 1; i >= 0; i--) {
const entry = entries[i];
const resource = entry.resource;
if (resource && resource.resourceType === "Condition") {
const codings = resource.code?.coding || [];
let hasSnomedCode = false;
for (const coding of codings) {
if (coding.system === "http:https://snomed.info/sct") {
if (conditionSet.has(coding.code)) {
removalStats.duplicateCodes.count += 1;
removalStats.duplicateCodes.codes.add(coding.code);
break;
} else {
conditionSet.add(coding.code);
const codeDetails = await getCodeDisplay(coding.code, "SNOMEDCT_US");
if (codeDetails && codeDetails.category === "disorder") {
hasSnomedCode = true;
const updatedText = `${codeDetails.display} (${codeDetails.category})`;
resource.code.text = updatedText;
coding.text = updatedText;
break;
} else {
removalStats.nonDisorderCodes.count += 1;
removalStats.nonDisorderCodes.codes.add(coding.code);
break;
}
}
}
}
if (!hasSnomedCode) {
removalStats.entriesWithoutSnomedCodes.count += 1;
entries.splice(i, 1);
}
} else if (resource && resource.resourceType === "Procedure") {
const codings = resource.code?.coding || [];
let hasValidCptCode = false;
for (const coding of codings) {
if (coding.system === "http:https://www.ama-assn.org/go/cpt") {
const cptCode = parseInt(coding.code, 10);
if (cptCode >= 10004 && cptCode <= 69990) {
if (cptSet.has(coding.code)) {
removalStats.duplicateCptCodes.count += 1;
removalStats.duplicateCptCodes.codes.add(coding.code);
break;
} else {
hasValidCptCode = true;
console.log(
"Keeping CPT code:",
coding.code,
"Resource.id:",
resource.id,
"hasValidCptCode:",
hasValidCptCode
);
cptSet.add(coding.code);
allRemainingEnries.add(resource.id);
break;
}
} else {
removalStats.invalidCptCodes.count += 1;
removalStats.invalidCptCodes.codes.add(coding.code);
break;
}
}
}
if (!hasValidCptCode) {
removalStats.entriesWithoutCptCodes.count += 1;
entries.splice(i, 1);
}
} else if (resource && resource.resourceType === "MedicationAdministration") {
if (resource.reasonReference) {
const medicationRef = resource.medicationReference?.reference.split("/")[1];
const conditionRef = resource.reasonReference[0]?.reference.split("/")[1];
if (medicationRef && conditionRef) {
medicationConditionDict[medicationRef] = conditionRef;
allRemainingEnries.add(resource.id);
}
} else {
console.log("Removing MedicationAdministration entry without condition reference");
entries.splice(i, 1);
}
}
}
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf8");
}
async function filterMedicationsEntries(
filePath: string,
conditionIdsDictionary: Set<string>,
medicationConditionDict: Record<string, string>,
rxNormSet: Set<string>,
removalStats: RemovalStats
) {
const data = JSON.parse(fs.readFileSync(filePath, "utf8"));
const entries = data.bundle ? data.bundle.entry : data.entry;
for (let i = entries.length - 1; i >= 0; i--) {
const entry = entries[i];
const resource = entry.resource;
if (resource && resource.resourceType === "Medication") {
const conditionRef = medicationConditionDict[resource.id];
// if the medication is linked to a conditon that is a non-duplicate disorder
if (conditionIdsDictionary.has(conditionRef)) {
const codings = resource.code?.coding || [];
for (const coding of codings) {
if (coding.system === "http:https://www.nlm.nih.gov/research/umls/rxnorm") {
if (rxNormSet.has(coding.code)) {
removalStats.rxnormDuplicates.count += 1;
removalStats.rxnormDuplicates.codes.add(coding.code);
entries.splice(i, 1);
} else {
rxNormSet.add(coding.code);
console.log(
"Keeping Medication. RXNORM code:",
coding.code,
"linked to condition:",
conditionRef,
"Resource.id:",
resource.id
);
}
}
}
} else {
removalStats.nonConditionLinkedMedications.count += 1;
entries.splice(i, 1);
}
}
}
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf8");
}
export async function fullProcessing(directoryPath: string) {
const removalStats = createInitialRemovalStats();
// Removal of non-disorder SNOMED codes, invalid CPT codes, and duplicate CPT codes
const cptSet = new Set<string>();
const conditionsSet = new Set<string>();
const allRemainingEnries = new Set<string>();
const medicationConditionDict: Record<string, string> = {}; // a mapping of all medications to conditions
await processDirectoryOrFile(directoryPath, async filePath => {
await removeConditionsProceduresMedAdmins(
filePath,
conditionsSet,
cptSet,
medicationConditionDict,
allRemainingEnries,
removalStats
);
});
prettyPrintRemovalStats(removalStats);
// Create hierarchy of SNOMED codes
const hashTable: Record<string, SnomedHierarchyTableEntry> = {};
await processDirectoryOrFile(directoryPath, async filePath => {
await computeHashTable(filePath, hashTable);
});
// Remove non-root SNOMED codes
const conditionIdsDictionary = new Set<string>();
await processDirectoryOrFile(directoryPath, async filePath => {
await removeNonRootSnomedCodes(
filePath,
hashTable,
allRemainingEnries,
conditionIdsDictionary,
removalStats
);
});
console.log("conditionIdsDictionary", conditionIdsDictionary);
console.log("medicationConditionDict", medicationConditionDict);
// Removal of duplicate RXNORM codes and medications without condition reference
const rxNormSet = new Set<string>();
await processDirectoryOrFile(directoryPath, async filePath => {
await filterMedicationsEntries(
filePath,
conditionIdsDictionary,
medicationConditionDict,
rxNormSet,
removalStats
);
});
prettyPrintRemovalStats(removalStats);
}
async function main() {
const [directoryPath] = process.argv.slice(2);
if (!directoryPath) {
console.error("Please provide a directory path as an argument.");
process.exit(1);
}
await fullProcessing(directoryPath);
}
main();