-
Notifications
You must be signed in to change notification settings - Fork 1
/
ncpdp.go
185 lines (154 loc) · 4.69 KB
/
ncpdp.go
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
package ncpdp
import (
"bytes"
_ "embed"
"encoding/csv"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"github.com/gocarina/gocsv"
)
var (
//go:embed NCPDPTerminology.txt
NCPDPTerminologyDataFile []byte
//go:embed Loinc.csv
LoincDataFile []byte
)
type Decoder struct {
msg *Message
r io.Reader
buf []byte
}
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{r: r}
}
func (d *Decoder) Decode() (*Message, error) {
return d.msg, d.decode()
}
func (d *Decoder) decode() error {
if d.buf == nil && d.r != nil {
buf := new(bytes.Buffer)
buf.ReadFrom(d.r)
d.buf = buf.Bytes()
}
return xml.Unmarshal(d.buf, &d.msg)
}
func (d *Decoder) ToJson() ([]byte, error) {
err := d.decode()
if err != nil {
return nil, err
}
return json.Marshal(d.msg)
}
type Terminologies []*Terminology
type Terminology struct {
NCItSubsetCode string
SubsetPreferredTerm string
NCItCode string
PreferredTerm string
Synonym string
NCItPreferredTerm string
NCItDefinition string
}
func LoadTerminology(r io.Reader) (*Terminologies, error) {
if r == nil {
r = bytes.NewReader(NCPDPTerminologyDataFile)
}
gocsv.SetCSVReader(func(in io.Reader) gocsv.CSVReader {
r := csv.NewReader(in)
r.Comma = '\t'
r.FieldsPerRecord = 7
return r
})
var data Terminologies
if err := gocsv.UnmarshalWithoutHeaders(r, &data); err != nil {
return nil, err
}
return &data, nil
}
func (t *Terminologies) Len() int {
if t == nil {
return 0
}
return len(*t)
}
func (t Terminologies) FindTermByQuantityUnitOfMeasureCode(s string) string {
if len(t) == 0 {
fmt.Println("no terminology data loaded")
return ""
}
for i := range t {
if t[i].NCItCode == s {
return t[i].PreferredTerm
}
}
return ""
}
type LoincData []*Loinc
type Loinc struct {
LoincNum string `csv:"LOINC_NUM"`
Component string `csv:"COMPONENT"`
Property string `csv:"PROPERTY"`
TimeAspect string `csv:"TIME_ASPCT"`
System string `csv:"SYSTEM"`
ScaleTyp string `csv:"SCALE_TYP"`
MethodTyp string `csv:"METHOD_TYP"`
Class string `csv:"CLASS"`
VersionLastChanged string `csv:"VersionLastChanged"`
ChngType string `csv:"CHNG_TYPE"`
DefinitionDescription string `csv:"DefinitionDescription"`
Status string `csv:"STATUS"`
ConsumerName string `csv:"CONSUMER_NAME"`
ClassType string `csv:"CLASSTYPE"`
Formula string `csv:"FORMULA"`
ExmplAnswers string `csv:"EXMPL_ANSWERS"`
SurveryQuestText string `csv:"SURVEY_QUEST_TEXT"`
SurveyQuestSrc string `csv:"SURVEY_QUEST_SRC"`
UnitsRequired string `csv:"UNITSREQUIRED"`
SubmittedUnits string `csv:"SUBMITTED_UNITS"`
RelatedNames2 string `csv:"RELATEDNAMES2"`
ShortName string `csv:"SHORTNAME"`
OrderObs string `csv:"ORDER_OBS"`
CDISCCommonTests string `csv:"CDISC_COMMON_TESTS"`
HL7FieldSubfieldID string `csv:"HL7_FIELD_SUBFIELD_ID"`
ExternalCopyrightNotice string `csv:"EXTERNAL_COPYRIGHT_NOTICE"`
ExampleUnits string `csv:"EXAMPLE_UNITS"`
LongCommonName string `csv:"LONG_COMMON_NAME"`
UnitsAndRange string `csv:"UnitsAndRange"`
ExampleUCUMUnits string `csv:"EXAMPLE_UCUM_UNITS"`
ExampleSiUCUMUnits string `csv:"EXAMPLE_SI_UCUM_UNITS"`
StatusReason string `csv:"STATUS_REASON"`
StatusText string `csv:"STATUS_TEXT"`
ChangeReasonPublic string `csv:"CHANGE_REASON_PUBLIC"`
CommonTestRank string `csv:"COMMON_TEST_RANK"`
CommonOrderRank string `csv:"COMMON_ORDER_RANK"`
CommonSiTestRank string `csv:"COMMON_SI_TEST_RANK"`
HL7AttachmentStructure string `csv:"HL7_ATTACHMENT_STRUCTURE"`
ExternalCopyrightLink string `csv:"EXTERNAL_COPYRIGHT_LINK"`
PanelType string `csv:"PanelType"`
AskAtOrderEntry string `csv:"AskAtOrderEntry"`
AssociatedObservations string `csv:"AssociatedObservations"`
VersionFirstReleased string `csv:"VersionFirstReleased"`
ValidHL7AttachmentRequest string `csv:"ValidHL7AttachmentRequest"`
DisplayName string `csv:"DisplayName"`
}
func LoadLoinc(r io.Reader) (*LoincData, error) {
if r == nil {
r = bytes.NewReader(LoincDataFile)
}
gocsv.SetCSVReader(func(in io.Reader) gocsv.CSVReader {
return gocsv.LazyCSVReader(in)
})
var data LoincData
if err := gocsv.Unmarshal(r, &data); err != nil {
return nil, err
}
return &data, nil
}
func (l *LoincData) Len() int {
if l == nil {
return 0
}
return len(*l)
}