/
create-code-system.ts
117 lines (102 loc) · 3.5 KB
/
create-code-system.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
import {Arguments, Argv} from "yargs";
import * as fs from 'fs';
export class CreateCodeSystemOptions {
type: 'rxnorm'|'snomed';
path: string;
output: string;
pretty = false;
}
interface CodeSystem {
resourceType: 'CodeSystem';
url: string;
status: string;
content: string;
concept: CodeSystemConcept[];
}
interface CodeSystemConcept {
code: string;
display: string;
}
export class CreateCodeSystem {
private options: CreateCodeSystemOptions;
private content: string;
private codeSystem: CodeSystem;
public static command = 'codesystem <type> <path> <output>';
public static description = 'Create a code system from a source file, such as RXNorm or SNOMED-CT. This command is VERY basic and does not currently account for many of the complexities of SNOMED and RXNORM code systems. This is only intended as a starting point and should not be used in production systems that require reliability.';
public static args(yargs: Argv): Argv {
return yargs
.positional('type', {
describe: 'Which code system is being created',
choices: ['rxnorm', 'snomed']
})
.positional('path', {
describe: 'The path to the source code system file to load and convert into a CodeSystem resource'
})
.positional('output', {
describe: 'The output path where the JSON CodeSystem should be stored'
})
.option('pretty', {
alias: 'p',
type: 'boolean'
});
}
public static handler(args: Arguments) {
const codeSystemCreator = new CreateCodeSystem(<CreateCodeSystemOptions><any>args);
codeSystemCreator.execute();
}
constructor(options: CreateCodeSystemOptions) {
this.options = options;
}
private rxnorm() {
const lines = this.content.replace('\r', '').split('\n');
this.codeSystem = {
resourceType: 'CodeSystem',
url: 'http://www.nlm.nih.gov/research/umls/rxnorm',
status: 'active',
content: 'complete',
concept: []
};
this.codeSystem.concept = lines.map(line => {
const parts = line.split('|');
return {
code: parts[7],
display: parts[14]
};
});
}
private snomed() {
const lines = this.content.replace('\r', '').split('\n');
this.codeSystem = {
resourceType: 'CodeSystem',
url: 'http://snomed.info/sct',
status: 'active',
content: 'complete',
concept: []
};
this.codeSystem.concept = lines.map(line => {
const parts = line.split('\t');
return {
code: parts[4],
display: parts[7]
};
});
}
public execute() {
this.content = fs.readFileSync(this.options.path).toString();
switch (this.options.type) {
case 'rxnorm':
this.rxnorm();
break;
case 'snomed':
this.snomed();
break;
default:
throw new Error(`Type ${this.options.type} not supported`);
}
if (this.options.pretty) {
fs.writeFileSync(this.options.output, JSON.stringify(this.codeSystem, null, '\t'));
} else {
fs.writeFileSync(this.options.output, JSON.stringify(this.codeSystem));
}
}
}