Skip to content

Commit b3f9310

Browse files
authored
Merge pull request #65 from mcode/generic-valueset-lookup
Generic valueSet lookup
2 parents cca8410 + a6b54c6 commit b3f9310

23 files changed

+1539
-1388
lines changed

src/helpers/cancerStagingUtils.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
const cancerStagingSystemVS = require('../valueSets/ValueSet-mcode-cancer-staging-system-vs.json');
2-
3-
function checkCodeInVS(code, valueSet) {
4-
return valueSet.compose.include[0].concept.map((c) => c.code).includes(code);
5-
}
1+
const path = require('path');
2+
const { checkCodeInVs } = require('./valueSetUtils');
63

74
function isCancerStagingSystem(code) {
8-
return checkCodeInVS(code, cancerStagingSystemVS);
5+
const cancerStagingSystemVSPath = path.resolve(__dirname, 'valueSets', 'ValueSet-mcode-cancer-staging-system-vs.json');
6+
return checkCodeInVs(code, cancerStagingSystemVSPath);
97
}
108

119
module.exports = {

src/helpers/conditionUtils.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
const primaryCancerConditionVS = require('../valueSets/ValueSet-onco-core-PrimaryOrUncertainBehaviorCancerDisorderVS.json');
2-
const secondaryCancerConditionVS = require('../valueSets/ValueSet-onco-core-SecondaryCancerDisorderVS.json');
1+
const path = require('path');
2+
const { checkCodeInVs } = require('./valueSetUtils');
33

4-
function checkCodeInVS(code, valueSet) {
5-
// strips the period in the code since the provided value set has the periods removed
6-
return valueSet.compose.include[2].concept.map((c) => c.code).includes(code.replace('.', ''));
7-
}
84

95
/**
106
* Checks for ICD-10 code
@@ -28,7 +24,8 @@ function getICD10Code(condition) {
2824
* @return {boolean} if primary cancer condition
2925
*/
3026
function isConditionCodePrimary(code) {
31-
return checkCodeInVS(code, primaryCancerConditionVS);
27+
const primaryCancerConditionVSFilepath = path.resolve(__dirname, 'valueSets', 'ValueSet-mcode-primary-or-uncertain-behavior-cancer-disorder-vs.json');
28+
return checkCodeInVs(code, primaryCancerConditionVSFilepath);
3229
}
3330

3431
/**
@@ -37,7 +34,8 @@ function isConditionCodePrimary(code) {
3734
* @return {boolean} if secondary cancer condition
3835
*/
3936
function isConditionCodeSecondary(code) {
40-
return checkCodeInVS(code, secondaryCancerConditionVS);
37+
const secondaryCancerConditionVSFilepath = path.resolve(__dirname, 'valueSets', 'ValueSet-mcode-secondary-cancer-disorder-vs.json');
38+
return checkCodeInVs(code, secondaryCancerConditionVSFilepath);
4139
}
4240

4341
/**

src/helpers/observationUtils.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const tumorMarkerTestVS = require('../valueSets/ValueSet-mcode-tumor-marker-test-vs.json');
1+
const path = require('path');
2+
const { checkCodeInVs } = require('./valueSetUtils');
23

34
// Codes and display values for Vital Signs resources
45
// Code mapping is based on http://hl7.org/fhir/R4/observation-vitalsigns.html
@@ -17,12 +18,10 @@ const vitalSignsCodeToTextLookup = {
1718
'8462-4': 'Diastolic blood pressure',
1819
};
1920

20-
function checkCodeInVS(code, valueSet) {
21-
return valueSet.compose.include[0].concept.map((c) => c.code).includes(code);
22-
}
2321

2422
function isTumorMarker(code) {
25-
return checkCodeInVS(code, tumorMarkerTestVS);
23+
const tumorMarkerTestVSPath = path.resolve(__dirname, 'valueSets', 'ValueSet-mcode-tumor-marker-test-vs.json');
24+
return checkCodeInVs(code, tumorMarkerTestVSPath);
2625
}
2726

2827
function isVitalSign(code) {

src/helpers/valueSetUtils.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const fs = require('fs');
2+
const logger = require('./logger');
3+
4+
const vsTypes = {
5+
json: 1,
6+
xml: 2,
7+
turtle: 3,
8+
};
9+
10+
function loadJsonVs(absoluteFilepath) {
11+
try {
12+
const vsData = fs.readFileSync(absoluteFilepath);
13+
const vsJson = JSON.parse(vsData);
14+
return vsJson;
15+
} catch (error) {
16+
logger.error(`Could not load valueSet from path ${absoluteFilepath}`);
17+
throw error;
18+
}
19+
}
20+
21+
function loadVs(absoluteFilepath, typeOfVS) {
22+
switch (typeOfVS) {
23+
case vsTypes.json:
24+
logger.debug(`loading JSON valueset from ${absoluteFilepath}`);
25+
return loadJsonVs(absoluteFilepath);
26+
27+
case vsTypes.xml:
28+
throw Error('No defined valueset loader for `xml` type valuesets');
29+
30+
case vsTypes.turtle:
31+
throw Error('No defined valueset loader for `turtle` type valuesets');
32+
33+
default:
34+
throw Error(`'${typeOfVS}' is not a recognized valueset type`);
35+
}
36+
}
37+
38+
/**
39+
* Check if code is in value set
40+
* @param {string} code value to look for in a valueset
41+
* @param {object} valueSet contains list of codes included in value set
42+
* @return {boolean} true if condition is in valueSet's compose block or expansion block
43+
*/
44+
const checkCodeInVs = (code, valueSetFilePath, typeOfVS = vsTypes.json) => {
45+
const valueSet = loadVs(valueSetFilePath, typeOfVS);
46+
let inVSExpansion = false;
47+
let inVSCompose = false;
48+
if (valueSet.expansion) {
49+
// If valueSet has expansion, we only need to check these codes
50+
inVSExpansion = valueSet.expansion.contains.some((containsItem) => {
51+
if (!code || !containsItem) return false;
52+
// NOTE: This is a technically incorrect interpretation of ValueSets;
53+
// this matching ought to check both code and system
54+
return code === containsItem.code;
55+
});
56+
} else {
57+
// Checks if code is in any of the valueSet.compose.include arrays
58+
inVSCompose = valueSet.compose.include.some((includeItem) => {
59+
if (!code || !includeItem || !includeItem.concept) return false;
60+
// NOTE: This is a technically incorrect interpretation of ValueSets;
61+
// this matching ought to check both code and system
62+
return includeItem.concept.map((concept) => concept.code).includes(code);
63+
});
64+
}
65+
return inVSCompose || inVSExpansion;
66+
};
67+
68+
module.exports = {
69+
vsTypes,
70+
loadJsonVs,
71+
loadVs,
72+
checkCodeInVs,
73+
};

0 commit comments

Comments
 (0)