-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusdm_bc.js
More file actions
84 lines (76 loc) · 3 KB
/
usdm_bc.js
File metadata and controls
84 lines (76 loc) · 3 KB
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
const fs = require('fs');
const jsonata = require("jsonata");
// Parse command-line arguments
const args = process.argv.slice(2);
const fileIndex = args.indexOf('-f');
if (fileIndex === -1 || !args[fileIndex + 1]) {
console.error('Usage: node usdm_bc.js -f <filepath to USDM JSON file>');
process.exit(1);
}
const filePath = args[fileIndex + 1];
// Read and parse the JSON file
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
/**
* Function to evaluate a JSONata expression and output the result to a JSON file
* @param {string} expression - The JSONata expression
* @param {string} fileName - The name of the output file
* @param {object} inputUsdm - The input USDM JSON data
*/
async function generateOutput(expression, fileName, inputUsdm) {
try {
const jsonataExpr = jsonata(expression);
jsonataExpr.assign("studyVersionId", "2"); // Assign studyVersionId dynamically if needed
const result = await jsonataExpr.evaluate(inputUsdm);
console.log(result);
// Write JSON to file
const outputFileName = `${fileName}.json`;
fs.writeFileSync(outputFileName, JSON.stringify(result, null, 2), 'utf-8');
console.log(`Output written to ${outputFileName}`);
} catch (error) {
console.error(`Error generating ${fileName}:`, error);
}
}
// Define extraction expressions for biomedical concepts within each study design activity
const extractSoaBc = `
(
$forceArray := function($v) {
$type($v) = "array" ? $v : [$v]
};
$a := study.versions[].studyDesigns[].activities[].{
"id": id,
"name": name,
"biomedicalConceptIds": biomedicalConceptIds
};
$b := study.versions[].**.biomedicalConcepts[].{
"id": id,
"concept": name,
"type": reference ? (
$contains(reference, "datasetspecializations") ? "Dataset Specialization" :
$contains(reference, "biomedicalconcepts") ? "Biomedical Concept"
) : "Undefined",
"code": code.standardCode.code,
"decode": code.standardCode.decode,
"dataElements": properties.name~>$join(", "),
"dataTypes": properties.datatype.( $ = "" or $ = null ? "null" : $ )~>$join(", ")
};
$a.{
"activityId": id,
"activityName": name,
"bc": $forceArray(
$map(biomedicalConceptIds, function($id){
$b[id = $id].{
"usdmBcId": id,
"concept": concept,
"bcId": code,
"bcShortname": decode,
"bcType": type,
"dataElements": dataElements,
"dataTypes": dataTypes
}
})
)
}
)
`;
// Generate output
generateOutput(extractSoaBc, "usdm_bc", data);