Moved old stuff into tags for 0.02

This commit is contained in:
sam 2013-04-09 17:31:32 +00:00
parent 11e1f82663
commit 2af1ab957f
5 changed files with 450 additions and 0 deletions

282
rxnorm/trunk/C0CRXNLK.m Normal file
View File

@ -0,0 +1,282 @@
C0CRXNLK ; VEN/SMH - RxNorm Lookup Utilities ;2013-04-08 5:00 PM
;;1.0;RX NORM;;Apr 25, 2012;Build 99
;(c) Sam Habiel 2013
; See accompanying license. Don't use otherwise.
;
S IO=$P
N DIQUIET S DIQUIET=1
D DT^DICRW
D EN^XTMUNIT($T(+0),1)
QUIT
;
GCN2RXN(GCN) ; $$ Public - Get RxNorm CUI using GCN
; Input: GCN by Value
; Output: Extrinsic
S GCN=$$RJ^XLFSTR(GCN,6,0) ; pad to six digits by zeros (123 becomes 000123)
N IEN S IEN=$O(^C0CRXN(176.001,"STC","NDDF","CDC",GCN,"")) ; Get Entry
Q ^(IEN)
;
GCN2RXNT ; @TEST - Test Get RxNorm CUI using GCN
N L F L=1:1 N LN S LN=$T(GCN2RXND+L) Q:LN["<<END>>" Q:LN="" D
. N GCN S GCN=$P(LN,";",3)
. N RXN S RXN=$P(LN,";",4)
. D CHKEQ^XTMUNIT($$GCN2RXN(GCN),RXN,"Translation from GCN to RXCUI failed")
QUIT
;
GCN2RXND ; @DATA - Data for Tests ;;GCN;EXPECTED RXNCUI
;;16033;991632
;;8208;310429
;;1275;628953
;;18;197604
;;346;884173
;;<<END>>
;
;
;
RXN2VUI(RXNCUI) ; $$ Public - Get VUID(s) for given RXNCUI for Clinical Drug
; Input: RXNCUI by Value
; Output: Caret delimited extrinsic. Should not be more than 2 entries.
N C0PVUID S C0PVUID=""
N I S I=""
F S I=$O(^C0CRXN(176.001,"STX","VANDF","CD",RXNCUI,I)) Q:I="" S C0PVUID=C0PVUID_^(I)_U
S C0PVUID=$E(C0PVUID,1,$L(C0PVUID)-1) ; remove trailing ^
; TODO: Return only the quantified form using:
; I $O(^C0CRXN(176.005,"RXCUIREL",RXN,"has_quantified_form","")) N QF S QF=$O(^("")) Q $O(^C0CRXN(176.001,"RXN2VUID",QF,""))
Q C0PVUID
;
RXN2VUIT ; @TEST - Get VUIDs given RxNorm values
N L F L=1:1 N LN S LN=$T(RXN2VUID+L) Q:LN["<<END>>" Q:LN="" D
. N RXN S RXN=$P(LN,";",3)
. N VUIDS S VUIDS=$P(LN,";",4)
. D CHKEQ^XTMUNIT($$RXN2VUI(RXN),VUIDS,"Translation from RXNCUI to VUID failed")
QUIT
;
RXN2VUID ; @DATA - Data items for previous test
;;991632;4006455
;;310429;4002369^4013941
;;628953;4000874^4000856^4013966^4015798^4015799
;;197604;4003335^4015937
;;884173;4002469^4013919
;;<<END>>
;
;
;
VUI2VAP(VUID) ; $$ Public - Get VA Product IEN from VUID
; Input VUID by Value
; Output: Extrinsic
Q $$FIND1^DIC(50.68,"","QX",VUID,"AVUID")
;
VUI2VAPT ; @TEST - Get VA Product IEN from VUID
N L F L=1:1 N LN S LN=$T(VUI2VAPD+L) Q:LN["<<END>>" Q:LN="" D
. N VUID S VUID=$P(LN,";",3)
. N VAP S VAP=$P(LN,";",4)
. D CHKEQ^XTMUNIT($$VUI2VAP(VUID),VAP,"Translation from VUID to VA PRODUCT failed")
QUIT
;
VUI2VAPD ; @DATA - Data for above test
;;4006455;5932
;;4002369;1784
;;4000874;252
;;4003335;2756
;;4002469;1884
;;<<END>>
;
;
;
VAP2MED(VAPROD) ; $$ Public - Get Drug(s) using VA Product IEN
; Un-Unit-testable: Drug files differ between sites.
; Input: VA Product IEN By Value
; OUtput: Caret delimited extrinsic
; This code inspired from PSNAPIs
; WHY THE HELL WOULD I USE A TEXT INDEX?
; It's my only option. Creating new xrefs on the drug file doesn't help
; as they are not filled out when adding a drug (IX[ALL]^DIK isn't called).
N MEDS S MEDS="" ; result
N PN,PN1 ; Product Name, abbreviated product name.
S PN=$P(^PSNDF(50.68,VAPROD,0),"^"),PN1=$E(PN,1,30)
N P50 S P50=0 ; looper through VAPN index which is DRUG file entry
F S P50=$O(^PSDRUG("VAPN",PN1,P50)) Q:'P50 D ; for each text match
. I $P(^PSDRUG(P50,"ND"),"^",3)=VAPROD S MEDS=$G(MEDS)_P50_U ; check that the VA PRODUCT pointer is the same as ours.
S:MEDS MEDS=$E(MEDS,1,$L(MEDS)-1) ; remove trailing ^
Q MEDS
;
;
RXN2MEDS(RXNCUI) ; $$ Public - Convert RxNorm value to currently existing drugs in File 50.
; Input: SCD RXNCUI
; Output; Caret delimited extrinsic
; Un-unit testable
N VUIDS S VUIDS=$$RXN2VUI(RXNCUI) ; Get VUID from RXNCUI
; TODO: Decide which VUID to use!
N VAPROD S VAPROD=$$VUI2VAP(VUIDS) ; Get VA Product from VUID
N MEDS S MEDS=$$VAP2MED(VAPROD) ; Get Meds from VA Product
QUIT MEDS
;
FDI2RXN(BASE) ; $$ Public - Get RxNorm CUI for FDB Ingredient/Base
; ^C0CRXN(176.001,"STC","NDDF","IN","014739",1000870)=1362160
; Input: BASE By Value
; Output: RxNorm CUI
S BASE=$$RJ^XLFSTR(BASE,6,0) ; pad to six digits by zeros (123 becomes 000123)
N IEN S IEN=$O(^C0CRXN(176.001,"STC","NDDF","IN",BASE,"")) Q ^(IEN)
;
FDI2RXNT ; @TEST - Test Get RxNorm CUI for FDB Ingredient/Base
D CHKEQ^XTMUNIT($$FDI2RXN(14739),1362160,"$$FDI2RXN failed")
QUIT
;
;
;
RXN2VIN(RXNCUI) ; $$ Public - Get VUID Ingredient for RxNorm CUI
; ^C0CRXN(176.001,"STX","VANDF","IN",1366467,1008555)=4031768
; Input: RXNCUI By Value
; Output: VUID
N IEN S IEN=$O(^C0CRXN(176.001,"STX","VANDF","IN",RXNCUI,"")) Q ^(IEN)
;
RXN2VINT ; @TEST - Test Get VUID Ingredient for RxNorm CUI
D CHKEQ^XTMUNIT($$RXN2VIN(1366467),4031768,"$$RXN2VIN failed")
QUIT
;
;
;
VIN2VAG(VUID) ; $$ Public - Get VA Generic for VUID Ingredient
; Input: VUID By Value
; Output: IEN^VA Generic Name (i.e. .01 field value)
N C0PIEN S C0PIEN=$$FIND1^DIC(50.6,"","QX",VUID,"AVUID")
N C0P01 S C0P01=$$GET1^DIQ(50.6,C0PIEN,.01)
Q C0PIEN_"^"_C0P01
;
VIN2VAGT ; @TEST - Test Get VA Generic for VUID Ingredient
D CHKEQ^XTMUNIT(+$$VIN2VAG(4023636),2832,"$$VIN2VAG failed")
QUIT
;
;
;
FDI2VAG(BASE) ; $$ Public - Get VA Generic for FDB Ingredient/Base
; TODO:Not tested...
; Input: BASE By Value
; Output: IEN^VA Generic Name (i.e. .01 field value)
Q $$VIN2VAG($$RXN2VIN($$FDI2RXN(BASE)))
;
VIN2DIN(VUID) ; $$ Public - Get Drug Ingredient for VUID Ingredient
; TODO:Not tested...
; Input: VUID By Value
; Output: IEN^Drug Ingredient Name (i.e. .01 field value)
N C0PIEN S C0PIEN=$$FIND1^DIC(50.416,"","QX",VUID,"AVUID")
N C0P01 S C0P01=$$GET1^DIQ(50.416,C0PIEN,.01)
Q C0PIEN_"^"_C0P01
;
FDI2DIN(BASE) ; $$ Public - Get Drug Ingredient for FDB Ingredient/Base
; TODO:Not tested...
; Input: BASE By Value
; Output: IEN^Drug Ingredient Name (i.e. .01 field value)
Q $$VIN2DIN($$RXN2VIN($$FDI2RXN(BASE)))
;
VUI2RXN(VUID) ; $$ Public - Get RXNCUI for VUID (any VUID type)
; Input: VUID By Value
; Output: RXNCUIs delimited by ^
; Get all entries whose code is the VUID and are in the VA NDF which are clinical drugs
D FIND^DIC(176.001,,"@;.01","PQX",VUID,,"CODE","I $P(^(0),U,12,13)=""VANDF^CD""")
; Deserialise it into a single string
; ^TMP("DILIST",4844,0)="1^*^0^"
; ^TMP("DILIST",4844,0,"MAP")="IEN^.01"
; ^TMP("DILIST",4844,1,0)="1006351^1364462"
N RXNS S RXNS=""
N I F I=0:0 S I=$O(^TMP("DILIST",$J,I)) Q:'I S RXNS=RXNS_$P(^(I,0),U,2)_U
S RXNS=$E(RXNS,1,$L(RXNS)-1)
QUIT RXNS
;
MED2RXN(DA) ; $$ Public - Get RxNorm CUI for Drug
; Input: DA - Medication IEN
; Output: RXNCUIs delimited by ^
N ND S ND=$G(^PSDRUG(DA,"ND")) ; ND Node
N VAP S VAP=$P(ND,U,3) ; VA Product Pointer
Q:'VAP "" ; quit if empty
N VUID S VUID=+^PSNDF(50.68,VAP,"VUID") ; Get VUID
I 'VUID S $EC=",U1," ; Must exist
Q $$VUI2RXN(VUID)
;
MED2SCDN(DA) ; $$ Public - Medication to Semantic Clinical Drug Name
; Input: DA - Medication IEN
; Output: The Canonical Semantic Clinical Drug name
N RXNCUI S RXNCUI=$$MED2RXN(DA)
Q:'RXNCUI ""
N IEN S IEN=$O(^C0CRXN(176.001,"STC","RXNORM","SCD",RXNCUI,"")) ; Let's try generic drug
I 'IEN S IEN=$O(^C0CRXN(176.001,"STC","RXNORM","SBD",RXNCUI,"")) ; Let's try non-bioequivalent Brands then
I 'IEN S IEN=$O(^C0CRXN(176.001,"STC","RXNORM","GPCK",RXNCUI,"")) ; Let's try a Generic combination package
I 'IEN S IEN=$O(^C0CRXN(176.001,"STC","RXNORM","SCDF",RXNCUI,"")) ; Let's try a Clinical Drug and Form (Like Metamucil)
Q:'IEN "" ; Apparently not every VUID has a corresponding RXNCUI SCD.
Q $P(^C0CRXN(176.001,IEN,0),U,15)
;
RXN2NDI(RXNCUI) ; $$ Public - Get NDDF Ingredient for RXNCUI
; Input: RXNCUI By Value
; Output: NDDF Base code
; TODO:Not tested...
N IEN S IEN=$O(^C0CRXN(176.001,"STX","NDDF","IN",RXNCUI,"")) Q ^(IEN)
;
VIN2NDI(VUID) ; $$ Public - Get NDDF Ingredient for VUID
; NB: WILL ONLY WORK IF VUID IS AN INGREDIENT VUID, NOT A CLINICAL DRUG
; Input: VUID By Value
; Output: NDDF Base code
; TODO:Not tested...
Q $$RXN2NDI($$VUI2RXN(VUID))
;
; ---
;
NDC2RXN(NDC) ; $$ Public - Get RxCUI given the NDC
; NB: Will only work if passed NDC is in 5-4-2 format.
; Input: NDC By Value in 5-4-2 Format
; Output: RxNorm Code.
S NDC=$TR(NDC,"-")
N IEN S IEN=$O(^C0CRXN(176.002,"ASAA","RXNORM","NDC",NDC,"")) Q ^(IEN)
;
NDC2RXNT ; @TEST - Test Get RxCUI given the NDC
D CHKEQ^XTMUNIT($$NDC2RXN("30142-0917-71"),198439,"$$NDC2RXN failed")
QUIT
;
; ---
;
ISBRAND(RXN) ; $$ Public - Is this RxCUI for a brand drug?
; Input: RxCUI
; Output: 0 or 1
Q ''$D(^C0CRXN(176.001,"STC","RXNORM","SBD",RXN))
ISBRANDT ; @TEST - Test Is this RxCUI for a brand drug?
D CHKEQ^XTMUNIT($$ISBRAND(205535),1,"$$ISBRAND failed") ; Brand Prozac
D CHKEQ^XTMUNIT($$ISBRAND(310384),0,"$$ISBRAND failed") ; Generic Fluoxetine
QUIT
;
; ---
;
BR2GEN(RXN) ; $$ Public - Convert Brand RxCUI to Generic RxCUI (many to 1)
; Input: RxCUI of Brand
; Output: RxCUI of Generic
Q $O(^C0CRXN(176.005,"B",RXN,"has_tradename",""))
BR2GENT ; @TEST - Test Convert Brand RxCUI to Generic RxCUI (many to 1)
D CHKEQ^XTMUNIT($$BR2GEN(205535),310384,"$$BR2GEN failed")
QUIT
;
; ---
;
GEN2BR(RXN) ; $$ Public - Convert Generic RxCUI to Brand RxCUIs (1 to many).
N RTN S RTN="" ; Return
N I S I="" F S I=$O(^C0CRXN(176.005,"B",RXN,"tradename_of",I)) Q:'I S RTN=RTN_I_U
S RTN=$E(RTN,1,$L(RTN)-1)
Q RTN
;
GEN2BRT ; @TEST - Test Convert Generic RxCUI to Brand RxCUIs (1 to many).
D CHKTF^XTMUNIT($$GEN2BR(310384)[205535,"$$GEN2BR failed")
QUIT
;
; ---
;
RXN2BNS(RXN) ; $$ Public - Get all Brand Names associated with an RXN
N BNS S BNS=""
I $$ISBRAND(RXN) S RXN=$$BR2GEN(RXN)
N ALLBN S ALLBN=$$GEN2BR(RXN)
N BNNO F BNNO=1:1:$L(ALLBN,U) D
. N EACHBN S EACHBN=$P(ALLBN,U,BNNO)
. N BNRXCUI S BNRXCUI=$O(^C0CRXN(176.005,"B",EACHBN,"ingredient_of",""))
. Q:BNRXCUI=""
. N BNIEN S BNIEN=$O(^C0CRXN(176.001,"B",BNRXCUI,""))
. S BNS=BNS_$P(^C0CRXN(176.001,BNIEN,0),U,15)_U
QUIT $E(BNS,1,$L(BNS)-1)
RXN2BNST ; @TEST - Test Get all Brand Names associated with an RXN
D CHKTF^XTMUNIT($$RXN2BNS(205535)["Prozac","$$RXN2BNS failed")
QUIT

168
rxnorm/trunk/C0CRXNRD.m Normal file
View File

@ -0,0 +1,168 @@
C0CRXNRD ; VEN/SMH - RxNorm Utilities: Routine to Read RxNorm files;2013-03-06 4:32 PM
;;2.0;RX NORM;;May 11, 2012;Build 50
; (C) Sam Habiel 2013
; See license for terms of use.
;
W "No entry from top" Q
IMPORT(PATH) ; PUBLIC ENTRY POINT. Rest are private
I PATH="" QUIT
S U="^"
N STARTTIME S STARTTIME=$P($H,",")*24*60*60+$P($H,",",2)
D SAB(PATH),CONSO(PATH),SAT(PATH),STY(PATH),REL(PATH),DOC(PATH)
N ENDTIME S ENDTIME=$P($H,",")*24*60*60+$P($H,",",2)
W !,(ENDTIME-STARTTIME)/60_" minutes elapsed"
QUIT
;
; Everything is private from down on...
DELFILED(FN) ; Delete file data; PEP procedure; only for RxNorm files
; FN is Filenumber passed by Value
QUIT:$E(FN,1,3)'=176 ; Quit if not RxNorm files
N ROOT S ROOT=$$ROOT^DILFD(FN,"",1) ; global root
N ZERO S ZERO=@ROOT@(0) ; Save zero node
S $P(ZERO,U,3,9999)="" ; Remove entry # and last edited
K @ROOT ; Kill the file -- so sad!
S @ROOT@(0)=ZERO ; It riseth again!
QUIT
GETLINES(PATH,FILENAME) ; Get number of lines in a file
D OPEN^%ZISH("FILE",PATH,FILENAME,"R")
U IO
N I,LINE
F I=1:1 R LINE:0 Q:$$STATUS^%ZISH
D CLOSE^%ZISH("FILE")
Q I-1
CONSO(PATH,INCRES) ; Open and read concepts file: RXNCONSO.RRF
; PATH ByVal, path of RxNorm files
; INCRES ByVal, include restricted sources. 1 for yes, 0 for no
I PATH="" QUIT
S INCRES=+$G(INCRES) ; if not passed, becomes zero.
N FILENAME S FILENAME="RXNCONSO.RRF"
D DELFILED(176.001) ; delete data
N LINES S LINES=$$GETLINES(PATH,FILENAME)
N POP
D OPEN^%ZISH("FILE",PATH,FILENAME,"R")
IF POP D EN^DDIOL("Error reading file..., Please check...") G EX
N C0CCOUNT
F C0CCOUNT=1:1 D Q:$$STATUS^%ZISH
. U IO
. N LINE R LINE:0
. IF $$STATUS^%ZISH QUIT
. I '(C0CCOUNT#1000) U $P W C0CCOUNT," of ",LINES," read ",! U IO ; update every 1000
. S ^C0CRXN(176.001,C0CCOUNT,0)=$TR(LINE,"|^","^|")
. ; TODO Implement Filtering:
. ; If the source is a restricted source, decide what to do based on what's asked.
. ; N SRCIEN S SRCIEN=$$FIND1^DIC(176.003,"","QX",SAB,"B") ; SrcIEN in RXNORM SOURCES file
. ; N RESTRIC S RESTRIC=$$GET1^DIQ(176.003,SRCIEN,14,"I") ; 14 is restriction field; values 0-4
. ; If RESTRIC is zero, then it's unrestricted. Everything else is restricted.
. ; If user didn't ask to include restricted sources, and the source is restricted, then quit
. ; I 'INCRES,RESTRIC QUIT
EX D CLOSE^%ZISH("FILE")
N DIK S DIK="^C0CRXN(176.001," D IXALL^DIK
QUIT
;
;
SAT(PATH) ; Open and read Concept and Atom attributes: RXNSAT.RRF
I PATH="" QUIT
N FILENAME S FILENAME="RXNSAT.RRF"
D DELFILED(176.002) ; delete data
N LINES S LINES=$$GETLINES(PATH,FILENAME)
N POP
D OPEN^%ZISH("FILE",PATH,FILENAME,"R")
IF POP W "Error reading file..., Please check...",! G EX2
N C0CCOUNT F C0CCOUNT=1:1 Q:$$STATUS^%ZISH D
. U IO
. N LINE R LINE:0
. IF $$STATUS^%ZISH QUIT
. I '(C0CCOUNT#1000) U $P W C0CCOUNT," of ",LINES," read ",! U IO ; update every 1000
. ;
. ; We switch around the fields .01 and .09 because the .01 isn't always present; where as .09 is required
. N RXCUI1,ATN9
. S RXCUI1=$P(LINE,"|",1)
. S ATN9=$P(LINE,"|",9)
. S $P(LINE,"|",1)=ATN9
. S $P(LINE,"|",9)=RXCUI1
. ;
. ; Save off
. S ^C0CRXN(176.002,C0CCOUNT,0)=$TR(LINE,"|^","^|")
EX2 D CLOSE^%ZISH("FILE")
N DIK S DIK="^C0CRXN(176.002," D IXALL^DIK
QUIT
;
;
SAB(PATH) ; Open the read RxNorm Sources file: RXNSAB.RRF
I PATH="" QUIT
N FILENAME S FILENAME="RXNSAB.RRF"
D DELFILED(176.003) ; delete data
N POP
D OPEN^%ZISH("FILE",PATH,FILENAME,"R")
IF POP W "Error reading file..., Please check...",! G EX3
N I F I=1:1 Q:$$STATUS^%ZISH D
. U IO
. N LINE R LINE:0
. IF $$STATUS^%ZISH QUIT
. U $P W I,! U IO ; Write I to the screen, then go back to reading the file
. ; Switch pieces 1 and 4 because 4 is always defined. Goes into .01 field.
. N VCUI S VCUI=$P(LINE,"|",1)
. N RSAB S RSAB=$P(LINE,"|",4)
. S $P(LINE,"|",1)=RSAB
. S $P(LINE,"|",4)=VCUI
. S ^C0CRXN(176.003,I,0)=$TR(LINE,"^|","|^")
EX3 D CLOSE^%ZISH("FILE")
N DIK S DIK="^C0CRXN(176.003," D IXALL^DIK
QUIT
STY(PATH) ; Open and read RxNorm Semantic types file: RXNSTY.RRF
I PATH="" QUIT
N FILENAME S FILENAME="RXNSTY.RRF"
D DELFILED(176.004) ; delete data
N LINES S LINES=$$GETLINES(PATH,FILENAME) ; Get # of lines
N POP
D OPEN^%ZISH("FILE",PATH,FILENAME,"R")
IF POP W "Error reading file..., Please check...",! G EX4
N I F I=1:1 Q:$$STATUS^%ZISH D
. U IO
. N LINE R LINE:0
. IF $$STATUS^%ZISH QUIT
. I '(I#1000) U $P W I," of ",LINES," read ",! U IO ; update every 1000
. S ^C0CRXN(176.004,I,0)=$TR(LINE,"^|","|^")
EX4 D CLOSE^%ZISH("FILE")
N DIK S DIK="^C0CRXN(176.004," D IXALL^DIK
QUIT
;
REL(PATH) ; Open and read RxNorm Relationships file: RXNREL.RRF
I PATH="" QUIT
N FILENAME S FILENAME="RXNREL.RRF"
D DELFILED(176.005) ; delete data
N LINES S LINES=$$GETLINES(PATH,FILENAME) ; Get # of lines
N POP
D OPEN^%ZISH("FILE",PATH,FILENAME,"R")
IF POP W "Error reading file..., Please check...",! G EX5
N I F I=1:1 Q:$$STATUS^%ZISH D
. U IO
. N LINE R LINE:0
. IF $$STATUS^%ZISH QUIT
. I '(I#1000) U $P W I," of ",LINES," read ",! U IO ; update every 1000
. ; swap RXCUI1 location with SAB b/c SAB is required so can be .01 field
. N RXCUI1 S RXCUI1=$P(LINE,"|",1)
. N SAB S SAB=$P(LINE,"|",11)
. S $P(LINE,"|",1)=SAB
. S $P(LINE,"|",11)=RXCUI1
. S ^C0CRXN(176.005,I,0)=$TR(LINE,"^|","|^")
EX5 D CLOSE^%ZISH("FILE")
N DIK S DIK="^C0CRXN(176.005," D IXALL^DIK
QUIT
DOC(PATH) ; Open the read RxNorm Abbreviation Documentation file: RXNDOC.RRF
I PATH="" QUIT
N FILENAME S FILENAME="RXNDOC.RRF"
D DELFILED(176.006) ; delete data
N LINES S LINES=$$GETLINES(PATH,FILENAME) ; Get # of lines
N POP
D OPEN^%ZISH("FILE",PATH,FILENAME,"R")
IF POP W "Error reading file..., Please check...",! G EX6
N I F I=1:1 Q:$$STATUS^%ZISH D
. U IO
. N LINE R LINE:0
. IF $$STATUS^%ZISH QUIT
. I '(I#1000) U $P W I," of ",LINES," read ",! U IO ; update every 1000
. S ^C0CRXN(176.006,I,0)=$TR(LINE,"^|","|^")
EX6 D CLOSE^%ZISH("FILE")
N DIK S DIK="^C0CRXN(176.006," D IXALL^DIK
QUIT