-
Notifications
You must be signed in to change notification settings - Fork 1
/
patient-to-csv.js
172 lines (148 loc) · 5.34 KB
/
patient-to-csv.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
const fs = require('fs');
const { parse } = require('json2csv');
const path = require('path');
const program = require('commander');
const _ = require('lodash');
program
.option ('-f, --file <file>', 'Path to the json file to parse')
.usage(' -f /path/to/file.json')
.parse(process.argv);
if (!(program.file && fs.existsSync(program.file))) {
console.error('Error: Specified file does not exist');
program.help();
process.exit(1);
}
if (!fs.existsSync('./output')){
fs.mkdirSync('./output');
}
function safe(obj) {
return new Proxy(obj, {
get(target, name) {
const result = target[name];
if (!!result) {
return (result instanceof Object)? safe(result) : result;
}
return safe({});
}
});
}
const getNamespace = (url) => url.substring(url.lastIndexOf('/')+1);
const getCodeValue = o => o.Value.Coding[0].CodeValue.Value;
const getCodeableConceptDisplayText = o => o.Value.Coding[0].DisplayText.Value;
const getCodingTuple = o => { return { displayText: getCodeableConceptDisplayText(o), codeValue: getCodeValue(o) } };
const getVitalValue = (o, useUnits = true) => `${o.Value.Number.Value}${useUnits ? ' ' + o.Value.Units.Value.CodeValue.Value : ''}`;
const output = [];
const seenDataElements = [];
const entries = JSON.parse(fs.readFileSync(program.file, 'utf8'));
entries.forEach(entry => {
const safeEntry = safe(entry);
const entryType = getNamespace(safeEntry.EntryType.Value);
switch(entryType) {
case 'Patient':
processPatientInfo(safeEntry, entryType);
break;
case 'CancerCondition':
processCancerCondition(safeEntry, entryType);
break;
case 'TNMClinicalDistantMetastasesCategory':
case 'TNMClinicalRegionalNodesCategory':
case 'TNMClinicalPrimaryTumorCategory':
case 'TNMClinicalStageGroup':
processClinicalStageGroup(safeEntry, entryType);
break;
case 'GeneticMutationTestResult':
processGeneticTest(safeEntry, entryType);
break;
case 'BodyWeight':
addRow({
Weight: getVitalValue(entry.DataValue)
}, 'Vitals');
break;
case 'BloodPressure':
const systolicComponent = entry.Components.find(c => getNamespace(c.EntryType.Value) === 'SystolicPressure');
const diastolicComponent = entry.Components.find(c => getNamespace(c.EntryType.Value) === 'DiastolicPressure');
addRow({
'Systolic Blood Pressure': getVitalValue(systolicComponent.DataValue, false)
}, 'Vitals');
addRow({
'Diastolic Blood Pressure': getVitalValue(diastolicComponent.DataValue, false)
}, 'Vitals');
break;
case 'MedicationStatement':
processMedicationStatement(safeEntry, entryType);
break;
case 'CancerDiseaseStatus':
addRow({
'Disease Status': getCodingTuple(entry.DataValue)
}, entryType);
break;
default:
break
}
});
function addRow(data, entryType) {
for(let key in data) {
const dataElementId = `${entryType}-${key}`;
if (!_.isEmpty(data[key]) && !_.includes(seenDataElements, dataElementId)) {
const { displayText, codeValue } = _.isObject(data[key]) ? data[key] : { displayText: data[key], codeValue: '' };
if (_.isEmpty(displayText) && _.isEmpty(codeValue)) continue;
output.push({
domain: entryType,
element: key,
value: !_.isEmpty(displayText) ? displayText : '',
code: !_.isEmpty(codeValue) ? codeValue : ''
});
seenDataElements.push(dataElementId);
}
}
}
function processPatientInfo(entry, entryType) {
const data = {
'Date of Birth': entry.Person.DateOfBirth.Value,
'Administrative Gender': getCodingTuple(entry.Person.AdministrativeGender),
'Race': getCodingTuple(entry.Person.Race.RaceCode),
'Ethnicity': getCodingTuple(entry.Person.Ethnicity.EthnicityCode)
};
addRow(data, entryType);
}
function processCancerCondition(entry, entryType) {
const data = {
'Body Location': getCodingTuple(entry.BodyLocation[0].LocationCode),
'Clinical Status': getCodingTuple(entry.ClinicalStatus),
'Condition': getCodingTuple(entry.Code),
'Date of Diagnosis': entry.Onset.Value
};
addRow(data, entryType);
}
function processClinicalStageGroup(entry, entryType) {
const data = {
'Stage Group': getCodingTuple(entry.DataValue),
'Staging System': getCodingTuple(entry.Method)
};
addRow(data, entryType);
}
function processGeneticTest(entry, entryType) {
const data = {
'Test Result': getCodingTuple(entry.DataValue),
'Mutation Tested Variant Identifier': getCodingTuple(entry.MutationTested.VariantIdentifier)
};
addRow(data, entryType);
}
function processMedicationStatement(entry, entryType) {
const medCodeOrRef = getCodeableConceptDisplayText(entry.MedicationCodeOrReference);
let med;
if (_.isEmpty(medCodeOrRef)) {
med = getCodingTuple(entries.find(e => entry.MedicationCodeOrReference.Value._EntryId === e.EntryId.Value).Type);
} else {
med = medCodeOrRef;
}
const data = {
'Medication': med,
'Termination Reason': getCodingTuple(entry.ReasonCode[0]),
'Treatment Intent': getCodingTuple(entry.TreatmentIntent),
'Start Date': entry.OccurrenceTimeOrPeriod.Value.BeginDateTime.Value,
'End Date': entry.OccurrenceTimeOrPeriod.Value.EndDateTime.Value
};
addRow(data, entryType);
}
fs.writeFileSync(`./output/${path.parse(path.basename(program.file)).name}.csv`, parse(output), 'utf8');