-;;; $Id: sql-classes.lisp,v 1.1 2002/10/08 22:08:56 kevin Exp $
-
+;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
+;;;; *************************************************************************
+;;;; FILE IDENTIFICATION
+;;;;
+;;;; Name: sql-classes.lisp
+;;;; Purpose: Routines for reading UMLS objects from SQL database
+;;;; Programmer: Kevin M. Rosenberg
+;;;; Date Started: Apr 2000
+;;;;
+;;;; $Id: sql-classes.lisp,v 1.10 2002/11/12 18:05:00 kevin Exp $
+;;;;
+;;;; This file, part of UMLisp, is
+;;;; Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
+;;;;
+;;;; UMLisp users are granted the rights to distribute and use this software
+;;;; as governed by the terms of the GNU General Public License.
+;;;; *************************************************************************
+
(in-package :umlisp)
+(declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
-(declaim (optimize (speed 3) (safety 1)))
(defvar *current-srl* nil)
(defun current-srl ()
;;; Lookup functions for uterms,ustr in ucons
(defun find-uterm-in-ucon (ucon lui)
- (find lui (s#term ucon) :key #'uterm-lui :test 'equal))
+ (find lui (s#term ucon) :key #'lui :test 'equal))
(defun find-ustr-in-uterm (uterm sui)
- (find sui (s#str uterm) :key #'ustr-sui :test 'equal))
+ (find sui (s#str uterm) :key #'sui :test 'equal))
(defun find-ustr-in-ucon (ucon sui)
(let ((found-ustr nil))
(if srl
(string-append ls (format nil " and KCUILRL <= ~d limit 1" srl))
(string-append ls " limit 1"))
- (gu:awhen (car (mutex-sql-query ls))
- (make-instance 'ucon :cui cui :pfstr (car gu::it)
- :lrl (ensure-integer (cadr gu::it)))))
+ (kmrcl:awhen (car (mutex-sql-query ls))
+ (make-instance 'ucon :cui cui :pfstr (car kmrcl::it)
+ :lrl (ensure-integer (cadr kmrcl::it)))))
nil))
(defun find-ucon-lui (lui &key (srl *current-srl*))
(make-cuisui cui sui))))
(when srl
(string-append ls (format nil " and KCUILRL <= ~d" srl)))
- (gu:aif (car (mutex-sql-query ls))
- (make-instance 'ucon :cui (ensure-integer (nth 0 gu::it))
- :pfstr (nth 1 gu::it)
- :lrl (ensure-integer (nth 2 gu::it)))
+ (kmrcl:aif (car (mutex-sql-query ls))
+ (make-instance 'ucon :cui (ensure-integer (nth 0 kmrcl::it))
+ :pfstr (nth 1 kmrcl::it)
+ :lrl (ensure-integer (nth 2 kmrcl::it)))
nil))
nil))
ls
:database db))))
+(defun map-ucon-all (fn &key (srl *current-srl*))
+ "Return list of all ucon's"
+ (let ((ls "select distinct CUI,KPFSTR,KCUILRL from MRCON"))
+ (when srl
+ (string-append ls (format nil " where KCUILRL <= ~d" srl)))
+ (string-append ls " order by CUI asc")
+ (with-sql-connection (db)
+ (clsql:map-query
+ nil
+ #'(lambda (cui pfstr cuilrl)
+ (funcall fn
+ (make-instance 'ucon :cui (ensure-integer cui)
+ :pfstr pfstr
+ :lrl (ensure-integer cuilrl))))
+ ls
+ :database db))))
(defun find-udef-cui (cui &key (srl *current-srl*))
(defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
(mapcar
- #'(lambda (cui) (find-ucon-cui cui :key srl))
+ #'(lambda (cui) (find-ucon-cui cui :srl srl))
(remove-duplicates (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))))
(defun find-ucoc-cui (cui &key (srl *current-srl*))
(defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
"List of ucon with co-occurance cui2"
(mapcar
- #'(lambda (cui) (find-ucon-cui cui :key srl))
+ #'(lambda (cui) (find-ucon-cui cui :srl srl))
(remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
(defun find-ulo-cui (cui &key (srl *current-srl*))
ulos))
(nreverse ulos)))
+(defgeneric suistr (lo))
(defmethod suistr ((lo ulo))
"Return the string for a ulo object"
(find-string-sui (sui lo)))
(let ((ls (format nil "select LAT,TS,KLUILRL from MRCON where KCUILUI=~d limit 1" (make-cuilui cui lui))))
(when srl
(string-append ls (format nil " and KLUILRL <= ~d" srl)))
- (gu:aif (car (mutex-sql-query ls))
+ (kmrcl:aif (car (mutex-sql-query ls))
(make-instance 'uterm :cui cui
:lui lui
- :lat (nth 0 gu::it)
- :ts (nth 1 gu::it)
- :lrl (ensure-integer (nth 2 gu::it)))
+ :lat (nth 0 kmrcl::it)
+ :ts (nth 1 kmrcl::it)
+ :lrl (ensure-integer (nth 2 kmrcl::it)))
nil)))
(defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
(make-cuisui cui sui))))
(when srl
(string-append ls (format nil " and LRL <= ~d" srl)))
- (gu:aif (car (mutex-sql-query ls))
+ (kmrcl:aif (car (mutex-sql-query ls))
(make-instance 'ustr :sui sui
:cui cui
:cuisui (make-cuisui cui sui)
- :lui (ensure-integer (nth 0 gu::it))
- :stt (nth 1 gu::it)
- :str (nth 2 gu::it)
- :lrl (ensure-integer (nth 3 gu::it)))
+ :lui (ensure-integer (nth 0 kmrcl::it))
+ :stt (nth 1 kmrcl::it)
+ :str (nth 2 kmrcl::it)
+ :lrl (ensure-integer (nth 3 kmrcl::it)))
nil)))
(defun find-ustr-sui (sui &key (srl *current-srl*))
(defun find-usty-tui (tui)
"Find usty for tui"
(setq tui (parse-tui tui))
- (gu:aif (car (mutex-sql-query
+ (kmrcl:aif (car (mutex-sql-query
(format nil "select STY from MRSTY where TUI=~d limit 1" tui)))
- (make-instance 'usty :tui tui :sty (nth 0 gu::it))
+ (make-instance 'usty :tui tui :sty (nth 0 kmrcl::it))
nil))
(defun find-usty-sty (sty)
"Find usty for a sty"
- (gu:aif (car (mutex-sql-query
+ (kmrcl:aif (car (mutex-sql-query
(format nil "select TUI from MRSTY where STY='~a' limit 1" sty)))
- (make-instance 'usty :tui (ensure-integer (nth 0 gu::it)) :sty sty)
+ (make-instance 'usty :tui (ensure-integer (nth 0 kmrcl::it)) :sty sty)
nil))
(defun find-usty-all ()
(push (find-usty-tui (nth 0 tuple)) ustys))
(nreverse ustys)))
-(defun find-usty_freq-all ()
- (let ((usty_freqs '()))
- (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
- (let* ((tui (car tuple))
- (freq (ensure-integer
- (caar (mutex-sql-query
- (format nil "select count(*) from MRSTY where TUI=~a" tui))))))
- (push (make-instance 'usty_freq :usty (find-usty-tui tui) :freq freq) usty_freqs)))
- (sort usty_freqs #'> :key #'usty_freq-freq)))
-
-
-
+(defun find-usab-all ()
+ "Find usab for a key"
+ (let ((results '()))
+ (dolist (tuple (mutex-sql-query "select VCUI,RCUI,VSAB,RSAB,SON,SF,SVER,MSTART,MEND,IMETA,RMETA,SLC,SCC,SRL,TFR,CFR,CXTY,TTYL,ATNL,LAT,CENC,CURVER,SABIN from MRSAB"))
+ (destructuring-bind
+ (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) tuple
+ (push
+ (make-instance 'usab :vcui (ensure-integer vcui)
+ :rcui (ensure-integer rcui)
+ :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart
+ :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc
+ :srl (ensure-integer srl)
+ :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
+ :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
+ :curver curver :sabin sabin)
+ results)))
+ (nreverse results)))
+
+(defun find-usab-by-key (key-name key)
+ "Find usab for a key"
+ (aif (car (mutex-sql-query
+ (format nil "select VCUI,RCUI,VSAB,RSAB,SON,SF,SVER,MSTART,MEND,IMETA,RMETA,SLC,SCC,SRL,TFR,CFR,CXTY,TTYL,ATNL,LAT,CENC,CURVER,SABIN from MRSAB where ~A='~A'" key-name key)))
+ (destructuring-bind
+ (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) it
+ (make-instance 'usab :vcui (ensure-integer vcui)
+ :rcui (ensure-integer rcui)
+ :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart
+ :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc
+ :srl (ensure-integer srl)
+ :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
+ :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
+ :curver curver :sabin sabin))))
+
+(defun find-usab-rsab (rsab)
+ "Find usab for rsab"
+ (find-usab-by-key "RSAB" rsab))
+
+(defun find-usab-vsab (vsab)
+ "Find usab for vsab"
+ (find-usab-by-key "VSAB" vsab))
(defun find-cui-max ()
(let ((cui (caar (mutex-sql-query "select max(CUI) from MRCON"))))
ustrs))
ustrs))
+;; Special tables
+
+(defun find-usrl-all ()
+ (let ((usrls '())
+ (tuples (mutex-sql-query "select SAB,SRL from USRL order by SAB desc")))
+ (dolist (tuple tuples)
+ (push (make-instance 'usrl :sab (nth 0 tuple)
+ :srl (ensure-integer (nth 1 tuple))) usrls))
+ usrls))
;;; Multiword lookup and score functions
(setq ucons (append ucons (find-ucon-word word :srl srl))))
(sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
-(defun find-ucon-normalized-multiword (str &key (srl *current-srl*))
- "Return sorted list of ucon's that match a multiword string"
- (let* ((words (delimited-string-to-list str #\space))
- (ucons '())
- (nwords '()))
- (dolist (word words)
- (let ((nws (lvg:process-terms word)))
- (dolist (nword nws)
- (push nword nwords))))
- (dolist (word nwords)
- (setq ucons (append ucons (find-ucon-word word :srl srl))))
- (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
-
(defun find-ustr-multiword (str &key (srl *current-srl*))
"Return sorted list of ustr's that match a multiword string"
(let* ((words (delimited-string-to-list str #\space))
(dolist (word words)
(setq ustrs (append ustrs (find-ustr-word word :srl srl))))
(sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
-
-(defun find-ustr-normalized-multiword (str &key (srl *current-srl*))
- "Return sorted list of ustr's that match a multiword string"
- (let* ((words (delimited-string-to-list str #\space))
- (ustrs '())
- (nwords '()))
- (dolist (word words)
- (let ((nws (lvg:process-terms word)))
- (dolist (nword nws)
- (push nword nwords))))
- (dolist (word nwords)
- (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
- (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'ustr-cui))))
-
-(defun a (str)
- (find-normalized-matches-for-str str #'find-ustr-normalized-word #'ustr-sui))
-
-(defun find-normalized-matches-for-str (str lookup-func key-func)
- "Return list of objects that normalize match for words in string,
-eliminate duplicates."
- (let ((objs '())
- (nwords '()))
- (dolist (word (delimited-string-to-list str #\space))
- (dolist (nword (lvg:process-terms word))
- (unless (member nword nwords :test #'string-equal)
- (push nword nwords))))
- (dolist (nw nwords)
- (setq objs (append objs (funcall lookup-func nw))))
- (delete-duplicates objs :key key-func :test #'eql)))
(defun sort-score-ucon-str (str ucons)
"Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
(decf score (- nlong nshort)) ;; reduce score for extra words
(dotimes (iword nshort)
(declare (fixnum iword))
- (gu:aif (position (nth iword short-list) long-list :test #'string-equal)
+ (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
(progn
- (incf score (- 10 (abs (- gu::it iword))))
+ (incf score (- 10 (abs (- kmrcl::it iword))))
(decf unmatched))))
(decf score (* 2 unmatched))
score))
;;; LEX SQL functions
(defun find-lexterm-eui (eui)
- (gu:awhen (car (mutex-sql-query
+ (kmrcl:awhen (car (mutex-sql-query
(format nil "select WRD from LRWD where EUI=~d" eui)))
- (make-instance 'lexterm :eui eui :wrd (nth 0 gu:it))))
+ (make-instance 'lexterm :eui eui :wrd (nth 0 kmrcl:it))))
(defun find-lexterm-word (wrd)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select EUI from LRWD where WRD='~a'" wrd))
(let ((terms '()))
- (dolist (tuple gu:it)
+ (dolist (tuple kmrcl:it)
(let ((eui (ensure-integer (nth 0 tuple))))
(push
(make-instance 'lexterm :eui eui :wrd (copy-seq wrd))
;; LEX SQL Read functions
(defun find-labr-eui (eui)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select BAS,ABR,EUI2,BAS2 from LRABR where EUI=~d" eui))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'labr :eui eui
:bas (nth 0 tuple)
(nreverse results))))
(defun find-labr-bas (bas)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select EUI,ABR,EUI2,BAS2 from LRABR where BAS='~a'" bas))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'labr :eui (ensure-integer (nth 0 tuple))
:bas (copy-seq bas)
(nreverse results))))
(defun find-lagr-eui (eui)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select STR,SCA,AGR,CIT,BAS from LRAGR where EUI=~d" eui))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'lagr
:eui eui
(nreverse results))))
(defun find-lcmp-eui (eui)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select BAS,SCA,COM from LRCMP where EUI=~d" eui))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'lcmp
:eui eui
(nreverse results))))
(defun find-lmod-eui (eui)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select BAS,SCA,PSN_MOD,FEA from LRMOD where EUI=~d" eui))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'lmod
:eui eui
(nreverse results))))
(defun find-lnom-eui (eui)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select BAS,SCA,EUI2,BAS2,SCA2 from LRNOM where EUI=~d" eui))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'lnom
:eui eui
(nreverse results))))
(defun find-lprn-eui (eui)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select BAS,NUM,GND,CAS,POS,QNT,FEA from LRPRN where EUI=~d" eui))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'lprn
:eui eui
(nreverse results))))
(defun find-lprp-eui (eui)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select BAS,STR,SCA,FEA from LRPRP where EUI=~d" eui))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'lprp
:eui eui
(nreverse results))))
(defun find-lspl-eui (eui)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select SPV,BAS from LRSPL where EUI=~d" eui))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'lspl
:eui eui
(defun find-ltrm-eui (eui)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select BAS,GEN from LRTRM where EUI=~d" eui))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'ltrm
:eui eui
(nreverse results))))
(defun find-ltyp-eui (eui)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select BAS,SCA,TYP from LRTYP where EUI=~d" eui))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'ltyp
:eui eui
(nreverse results))))
(defun find-lwd-wrd (wrd)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select EUI from LRWD where WRD='~a'" wrd))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push (ensure-integer (nth 0 tuple)) results))
(make-instance 'lwd :wrd wrd
:euilist (nreverse results)))))
;;; Semantic Network SQL access functions
(defun find-sdef-ui (ui)
- (gu:awhen (car (mutex-sql-query
+ (kmrcl:awhen (car (mutex-sql-query
(format nil "select RT,STY_RL,STN_RTN,DEF,EX,UN,RH,ABR,RIN from SRDEF where UI=~d" ui)))
- (make-instance 'sdef :rt (nth 0 gu::it)
+ (make-instance 'sdef :rt (nth 0 kmrcl::it)
:ui ui
- :styrl (nth 1 gu::it)
- :stnrtn (nth 2 gu::it)
- :def (nth 3 gu::it)
- :ex (nth 4 gu::it)
- :un (nth 5 gu::it)
- :rh (nth 6 gu::it)
- :abr (nth 7 gu::it)
- :rin (nth 8 gu::it))))
+ :styrl (nth 1 kmrcl::it)
+ :stnrtn (nth 2 kmrcl::it)
+ :def (nth 3 kmrcl::it)
+ :ex (nth 4 kmrcl::it)
+ :un (nth 5 kmrcl::it)
+ :rh (nth 6 kmrcl::it)
+ :abr (nth 7 kmrcl::it)
+ :rin (nth 8 kmrcl::it))))
(defun find-sstre1-ui (ui)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select UI2,UI3 from SRSTRE1 where UI=~d" ui))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'sstre1 :ui ui
:ui2 (ensure-integer (nth 0 tuple))
(nreverse results))))
(defun find-sstre1-ui2 (ui2)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select UI,UI3 from SRSTRE1 where UI2=~d" ui2))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'sstre1 :ui (ensure-integer (nth 0 tuple))
:ui2 ui2
(nreverse results))))
(defun find-sstr-rl (rl)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select STY_RL,STY_RL2,LS from SRSTRE where RL='~a'" rl))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'sstr
:rl rl
(defun find-sstre2-sty (sty)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select RL,STY2 from SRSTRE2 where STY='~a'" sty))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'sstre2
:sty (copy-seq sty)
(nreverse results))))
(defun find-sstr-styrl (styrl)
- (gu:awhen (mutex-sql-query
+ (kmrcl:awhen (mutex-sql-query
(format nil "select RL,STY_RL2,LS from SRSTR where RL='~a'" styrl))
(let ((results '()))
- (dolist (tuple gu::it)
+ (dolist (tuple kmrcl::it)
(push
(make-instance 'sstr :styrl styrl
:rl (nth 0 tuple)