source: ccr/trunk/rxnorm/trunk/routines/C0CRXNLK.m@ 1617

Last change on this file since 1617 was 1617, checked in by Sam Habiel, 11 years ago

Improvements to bugs found in RxNorm lookups

File size: 10.4 KB
Line 
1C0CRXNLK ; VEN/SMH - RxNorm Lookup Utilities ;2013-04-10 7:01 PM
2 ;;1.0;RX NORM;;Apr 25, 2012;Build 99
3 ;(c) Sam Habiel 2013
4 ; See accompanying license. Don't use otherwise.
5 ;
6 S IO=$P
7 N DIQUIET S DIQUIET=1
8 D DT^DICRW
9 D EN^XTMUNIT($T(+0),1)
10 QUIT
11 ;
12EXIST(RXN) ; $$ Public - Does this RxNorm CUI exist?
13 ; Input: RxNorm CUI by value
14 ; Output: Extrinsic
15 Q ''$D(^C0CRXN(176.001,"B",RXN))
16 ;
17GCN2RXN(GCN) ; $$ Public - Get RxNorm CUI using GCN
18 ; Input: GCN by Value
19 ; Output: Extrinsic
20 S GCN=$$RJ^XLFSTR(GCN,6,0) ; pad to six digits by zeros (123 becomes 000123)
21 N IEN S IEN=$O(^C0CRXN(176.001,"STC","NDDF","CDC",GCN,"")) ; Get Entry
22 Q ^(IEN)
23 ;
24GCN2RXNT ; @TEST - Test Get RxNorm CUI using GCN
25 N L F L=1:1 N LN S LN=$T(GCN2RXND+L) Q:LN["<<END>>" Q:LN="" D
26 . N GCN S GCN=$P(LN,";",3)
27 . N RXN S RXN=$P(LN,";",4)
28 . D CHKEQ^XTMUNIT($$GCN2RXN(GCN),RXN,"Translation from GCN to RXCUI failed")
29 QUIT
30 ;
31GCN2RXND ; @DATA - Data for Tests ;;GCN;EXPECTED RXNCUI
32 ;;16033;991632
33 ;;8208;310429
34 ;;1275;628953
35 ;;18;197604
36 ;;346;884173
37 ;;<<END>>
38 ;
39 ;
40 ;
41RXN2VUI(RXNCUI) ; $$ Public - Get VUID(s) for given RXNCUI for Clinical Drug
42 ; Input: RXNCUI by Value
43 ; Output: Caret delimited extrinsic. Should not be more than 2 entries.
44 N C0PVUID S C0PVUID=""
45 N I S I=""
46 F S I=$O(^C0CRXN(176.001,"STX","VANDF","CD",RXNCUI,I)) Q:I="" S C0PVUID=C0PVUID_^(I)_U
47 S C0PVUID=$E(C0PVUID,1,$L(C0PVUID)-1) ; remove trailing ^
48 ; TODO: Return only the quantified form using:
49 ; I $O(^C0CRXN(176.005,"RXCUIREL",RXN,"has_quantified_form","")) N QF S QF=$O(^("")) Q $O(^C0CRXN(176.001,"RXN2VUID",QF,""))
50 Q C0PVUID
51 ;
52RXN2VUIT ; @TEST - Get VUIDs given RxNorm values
53 N L F L=1:1 N LN S LN=$T(RXN2VUID+L) Q:LN["<<END>>" Q:LN="" D
54 . N RXN S RXN=$P(LN,";",3)
55 . N VUIDS S VUIDS=$P(LN,";",4)
56 . D CHKEQ^XTMUNIT($$RXN2VUI(RXN),VUIDS,"Translation from RXNCUI to VUID failed")
57 QUIT
58 ;
59RXN2VUID ; @DATA - Data items for previous test
60 ;;991632;4006455
61 ;;310429;4002369^4013941
62 ;;628953;4000874^4000856^4013966^4015798^4015799
63 ;;197604;4003335^4015937
64 ;;884173;4002469^4013919
65 ;;<<END>>
66 ;
67 ;
68 ;
69VUI2VAP(VUID) ; $$ Public - Get VA Product IEN(s) from VUID
70 ; Input VUID by Value
71 ; Output: Extrinsic
72 D FIND^DIC(50.68,,"@","QP",VUID,,"AVUID") ; Find all in VUID index
73 N O S O="" ; Output
74 N I F I=0:0 S I=$O(^TMP("DILIST",$J,I)) Q:'I S O=O_^(I,0)_U ; Concat results together
75 S O=$E(O,1,$L(O)-1) ; remove trailing ^
76 Q O
77 ;
78VUI2VAPT ; @TEST - Get VA Product IEN from VUID
79 N L F L=1:1 N LN S LN=$T(VUI2VAPD+L) Q:LN["<<END>>" Q:LN="" D
80 . N VUID S VUID=$P(LN,";",3)
81 . N VAP S VAP=$P(LN,";",4)
82 . D CHKEQ^XTMUNIT($$VUI2VAP(VUID),VAP,"Translation from VUID to VA PRODUCT failed")
83 QUIT
84 ;
85VUI2VAPD ; @DATA - Data for above test
86 ;;4006455;5932
87 ;;4002369;1784
88 ;;4000874;252
89 ;;4003335;2756
90 ;;4002469;1884
91 ;;4009488;9046^10090
92 ;;<<END>>
93 ;
94 ;
95 ;
96VAP2MED(VAPROD) ; $$ Public - Get Drug(s) using VA Product IEN
97 ; Un-Unit-testable: Drug files differ between sites.
98 ; Input: VA Product IEN By Value
99 ; OUtput: Caret delimited extrinsic
100 ; This code inspired from PSNAPIs
101 ; WHY THE HELL WOULD I USE A TEXT INDEX?
102 ; It's my only option. Creating new xrefs on the drug file doesn't help
103 ; as they are not filled out when adding a drug (IX[ALL]^DIK isn't called).
104 N MEDS S MEDS="" ; result
105 N PN,PN1 ; Product Name, abbreviated product name.
106 S PN=$P(^PSNDF(50.68,VAPROD,0),"^"),PN1=$E(PN,1,30)
107 N P50 S P50=0 ; looper through VAPN index which is DRUG file entry
108 F S P50=$O(^PSDRUG("VAPN",PN1,P50)) Q:'P50 D ; for each text match
109 . I $P(^PSDRUG(P50,"ND"),"^",3)=VAPROD S MEDS=$G(MEDS)_P50_U ; check that the VA PRODUCT pointer is the same as ours.
110 S:MEDS MEDS=$E(MEDS,1,$L(MEDS)-1) ; remove trailing ^
111 Q MEDS
112 ;
113 ;
114RXN2MEDS(RXNCUI) ; $$ Public - Convert RxNorm value to currently existing drugs in File 50.
115 ; Input: SCD RXNCUI
116 ; Output; Caret delimited extrinsic
117 ; Un-unit testable
118 N VUIDS S VUIDS=$$RXN2VUI(RXNCUI) ; Get VUID from RXNCUI (multiple VUIDs per CUI)
119 Q:'VUIDS ""
120 N MEDS S MEDS=""
121 N DONE S DONE=0
122 N I F I=1:1:$L(VUIDS,U) D Q:DONE
123 . N VUID S VUID=$P(VUIDS,U,I)
124 . N VAPRODS S VAPRODS=$$VUI2VAP(VUID) ; Get VA Product from VUID (multiple products per VUID)
125 . I '$L(VAPRODS) S ^SAM(RXNCUI)="TELL SAM",DONE=1 QUIT ; MUST EXIST. Every VUID must have a product to go with it.
126 . N J F J=1:1:$L(VAPRODS,U) D Q:DONE
127 . . N VAPROD S VAPROD=$P(VAPRODS,U,J)
128 . . S MEDS=$$VAP2MED(VAPROD) ; Get Meds from VA Product
129 . . I $L(MEDS) S DONE=1
130 QUIT MEDS
131 ;
132FDI2RXN(BASE) ; $$ Public - Get RxNorm CUI for FDB Ingredient/Base
133 ; ^C0CRXN(176.001,"STC","NDDF","IN","014739",1000870)=1362160
134 ; Input: BASE By Value
135 ; Output: RxNorm CUI
136 S BASE=$$RJ^XLFSTR(BASE,6,0) ; pad to six digits by zeros (123 becomes 000123)
137 N IEN S IEN=$O(^C0CRXN(176.001,"STC","NDDF","IN",BASE,"")) Q ^(IEN)
138 ;
139FDI2RXNT ; @TEST - Test Get RxNorm CUI for FDB Ingredient/Base
140 D CHKEQ^XTMUNIT($$FDI2RXN(14739),1362160,"$$FDI2RXN failed")
141 QUIT
142 ;
143 ;
144 ;
145RXN2VIN(RXNCUI) ; $$ Public - Get VUID Ingredient for RxNorm CUI
146 ; ^C0CRXN(176.001,"STX","VANDF","IN",1366467,1008555)=4031768
147 ; Input: RXNCUI By Value
148 ; Output: VUID
149 N IEN S IEN=$O(^C0CRXN(176.001,"STX","VANDF","IN",RXNCUI,"")) Q ^(IEN)
150 ;
151RXN2VINT ; @TEST - Test Get VUID Ingredient for RxNorm CUI
152 D CHKEQ^XTMUNIT($$RXN2VIN(1366467),4031768,"$$RXN2VIN failed")
153 QUIT
154 ;
155 ;
156 ;
157VIN2VAG(VUID) ; $$ Public - Get VA Generic for VUID Ingredient
158 ; Input: VUID By Value
159 ; Output: IEN^VA Generic Name (i.e. .01 field value)
160 N C0PIEN S C0PIEN=$$FIND1^DIC(50.6,"","QX",VUID,"AVUID")
161 N C0P01 S C0P01=$$GET1^DIQ(50.6,C0PIEN,.01)
162 Q C0PIEN_"^"_C0P01
163 ;
164VIN2VAGT ; @TEST - Test Get VA Generic for VUID Ingredient
165 D CHKEQ^XTMUNIT(+$$VIN2VAG(4023636),2832,"$$VIN2VAG failed")
166 QUIT
167 ;
168 ;
169 ;
170FDI2VAG(BASE) ; $$ Public - Get VA Generic for FDB Ingredient/Base
171 ; TODO:Not tested...
172 ; Input: BASE By Value
173 ; Output: IEN^VA Generic Name (i.e. .01 field value)
174 Q $$VIN2VAG($$RXN2VIN($$FDI2RXN(BASE)))
175 ;
176VIN2DIN(VUID) ; $$ Public - Get Drug Ingredient for VUID Ingredient
177 ; TODO:Not tested...
178 ; Input: VUID By Value
179 ; Output: IEN^Drug Ingredient Name (i.e. .01 field value)
180 N C0PIEN S C0PIEN=$$FIND1^DIC(50.416,"","QX",VUID,"AVUID")
181 N C0P01 S C0P01=$$GET1^DIQ(50.416,C0PIEN,.01)
182 Q C0PIEN_"^"_C0P01
183 ;
184FDI2DIN(BASE) ; $$ Public - Get Drug Ingredient for FDB Ingredient/Base
185 ; TODO:Not tested...
186 ; Input: BASE By Value
187 ; Output: IEN^Drug Ingredient Name (i.e. .01 field value)
188 Q $$VIN2DIN($$RXN2VIN($$FDI2RXN(BASE)))
189 ;
190VUI2RXN(VUID) ; $$ Public - Get RXNCUI for VUID (any VUID type)
191 ; Input: VUID By Value
192 ; Output: RXNCUIs delimited by ^
193 ; Get all entries whose code is the VUID and are in the VA NDF which are clinical drugs
194 D FIND^DIC(176.001,,"@;.01","PQX",VUID,,"CODE","I $P(^(0),U,12,13)=""VANDF^CD""")
195 ; Deserialise it into a single string
196 ; ^TMP("DILIST",4844,0)="1^*^0^"
197 ; ^TMP("DILIST",4844,0,"MAP")="IEN^.01"
198 ; ^TMP("DILIST",4844,1,0)="1006351^1364462"
199 N RXNS S RXNS=""
200 N I F I=0:0 S I=$O(^TMP("DILIST",$J,I)) Q:'I S RXNS=RXNS_$P(^(I,0),U,2)_U
201 S RXNS=$E(RXNS,1,$L(RXNS)-1)
202 QUIT RXNS
203 ;
204MED2RXN(DA) ; $$ Public - Get RxNorm CUI for Drug
205 ; Input: DA - Medication IEN
206 ; Output: RXNCUIs delimited by ^
207 N ND S ND=$G(^PSDRUG(DA,"ND")) ; ND Node
208 N VAP S VAP=$P(ND,U,3) ; VA Product Pointer
209 Q:'VAP "" ; quit if empty
210 N VUID S VUID=+^PSNDF(50.68,VAP,"VUID") ; Get VUID
211 I 'VUID S $EC=",U1," ; Must exist
212 Q $$VUI2RXN(VUID)
213 ;
214MED2SCDN(DA) ; $$ Public - Medication to Semantic Clinical Drug Name
215 ; Input: DA - Medication IEN
216 ; Output: The Canonical Semantic Clinical Drug name
217 N RXNCUI S RXNCUI=$$MED2RXN(DA)
218 Q:'RXNCUI ""
219 N IEN S IEN=$O(^C0CRXN(176.001,"STC","RXNORM","SCD",RXNCUI,"")) ; Let's try generic drug
220 I 'IEN S IEN=$O(^C0CRXN(176.001,"STC","RXNORM","SBD",RXNCUI,"")) ; Let's try non-bioequivalent Brands then
221 I 'IEN S IEN=$O(^C0CRXN(176.001,"STC","RXNORM","GPCK",RXNCUI,"")) ; Let's try a Generic combination package
222 I 'IEN S IEN=$O(^C0CRXN(176.001,"STC","RXNORM","SCDF",RXNCUI,"")) ; Let's try a Clinical Drug and Form (Like Metamucil)
223 Q:'IEN "" ; Apparently not every VUID has a corresponding RXNCUI SCD.
224 Q $P(^C0CRXN(176.001,IEN,0),U,15)
225 ;
226RXN2NDI(RXNCUI) ; $$ Public - Get NDDF Ingredient for RXNCUI
227 ; Input: RXNCUI By Value
228 ; Output: NDDF Base code
229 ; TODO:Not tested...
230 N IEN S IEN=$O(^C0CRXN(176.001,"STX","NDDF","IN",RXNCUI,"")) Q ^(IEN)
231 ;
232VIN2NDI(VUID) ; $$ Public - Get NDDF Ingredient for VUID
233 ; NB: WILL ONLY WORK IF VUID IS AN INGREDIENT VUID, NOT A CLINICAL DRUG
234 ; Input: VUID By Value
235 ; Output: NDDF Base code
236 ; TODO:Not tested...
237 Q $$RXN2NDI($$VUI2RXN(VUID))
238 ;
239 ; ---
240 ;
241NDC2RXN(NDC) ; $$ Public - Get RxCUI given the NDC
242 ; NB: Will only work if passed NDC is in 5-4-2 format.
243 ; Input: NDC By Value in 5-4-2 Format
244 ; Output: RxNorm Code.
245 S NDC=$TR(NDC,"-")
246 N IEN S IEN=$O(^C0CRXN(176.002,"ASAA","RXNORM","NDC",NDC,"")) Q ^(IEN)
247 ;
248NDC2RXNT ; @TEST - Test Get RxCUI given the NDC
249 D CHKEQ^XTMUNIT($$NDC2RXN("30142-0917-71"),198439,"$$NDC2RXN failed")
250 QUIT
251 ;
252 ; ---
253 ;
254ISBRAND(RXN) ; $$ Public - Is this RxCUI for a brand drug?
255 ; Input: RxCUI
256 ; Output: 0 or 1
257 Q ''$D(^C0CRXN(176.001,"STC","RXNORM","SBD",RXN))
258ISBRANDT ; @TEST - Test Is this RxCUI for a brand drug?
259 D CHKEQ^XTMUNIT($$ISBRAND(205535),1,"$$ISBRAND failed") ; Brand Prozac
260 D CHKEQ^XTMUNIT($$ISBRAND(310384),0,"$$ISBRAND failed") ; Generic Fluoxetine
261 QUIT
262 ;
263 ; ---
264 ;
265BR2GEN(RXN) ; $$ Public - Convert Brand RxCUI to Generic RxCUI (many to 1)
266 ; Input: RxCUI of Brand
267 ; Output: RxCUI of Generic
268 Q $O(^C0CRXN(176.005,"B",RXN,"has_tradename",""))
269BR2GENT ; @TEST - Test Convert Brand RxCUI to Generic RxCUI (many to 1)
270 D CHKEQ^XTMUNIT($$BR2GEN(205535),310384,"$$BR2GEN failed")
271 QUIT
272 ;
273 ; ---
274 ;
275GEN2BR(RXN) ; $$ Public - Convert Generic RxCUI to Brand RxCUIs (1 to many).
276 N RTN S RTN="" ; Return
277 N I S I="" F S I=$O(^C0CRXN(176.005,"B",RXN,"tradename_of",I)) Q:'I S RTN=RTN_I_U
278 S RTN=$E(RTN,1,$L(RTN)-1)
279 Q RTN
280 ;
281GEN2BRT ; @TEST - Test Convert Generic RxCUI to Brand RxCUIs (1 to many).
282 D CHKTF^XTMUNIT($$GEN2BR(310384)[205535,"$$GEN2BR failed")
283 QUIT
284 ;
285 ; ---
286 ;
287RXN2BNS(RXN) ; $$ Public - Get all Brand Names associated with an RXN
288 N BNS S BNS=""
289 I $$ISBRAND(RXN) S RXN=$$BR2GEN(RXN)
290 N ALLBN S ALLBN=$$GEN2BR(RXN)
291 Q:ALLBN="" ""
292 N BNNO F BNNO=1:1:$L(ALLBN,U) D
293 . N EACHBN S EACHBN=$P(ALLBN,U,BNNO)
294 . N BNRXCUI S BNRXCUI=$O(^C0CRXN(176.005,"B",EACHBN,"ingredient_of",""))
295 . Q:BNRXCUI=""
296 . N BNIEN S BNIEN=$O(^C0CRXN(176.001,"B",BNRXCUI,""))
297 . S BNS=BNS_$P(^C0CRXN(176.001,BNIEN,0),U,15)_U
298 QUIT $E(BNS,1,$L(BNS)-1)
299RXN2BNST ; @TEST - Test Get all Brand Names associated with an RXN
300 D CHKTF^XTMUNIT($$RXN2BNS(205535)["Prozac","$$RXN2BNS failed")
301 QUIT
Note: See TracBrowser for help on using the repository browser.