1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: sql-classes.lisp
6 ;;;; Purpose: Routines for reading UMLS objects from SQL database
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; $Id: sql-classes.lisp,v 1.12 2002/12/09 19:38:02 kevin Exp $
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
15 ;;;; UMLisp users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the GNU General Public License.
17 ;;;; *************************************************************************
20 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
23 (defvar *current-srl* nil)
26 (defun current-srl! (srl)
27 (setq *current-srl* srl))
31 ;;; Lookup functions for uterms,ustr in ucons
33 (defun find-uterm-in-ucon (ucon lui)
34 (find lui (s#term ucon) :key #'lui :test 'equal))
36 (defun find-ustr-in-uterm (uterm sui)
37 (find sui (s#str uterm) :key #'sui :test 'equal))
39 (defun find-ustr-in-ucon (ucon sui)
40 (let ((found-ustr nil))
41 (dolist (uterm (s#term ucon))
43 (dolist (ustr (s#str uterm))
45 (when (string-equal sui (sui ustr))
46 (setq found-ustr ustr))))))
50 (defun find-ucon-cui (cui &key (srl *current-srl*))
53 (setq cui (parse-cui cui)))
55 (let ((ls (format nil "select KPFSTR,KCUILRL from MRCON where CUI=~d"
58 (string-append ls (format nil " and KCUILRL <= ~d limit 1" srl))
59 (string-append ls " limit 1"))
60 (kmrcl:awhen (car (mutex-sql-query ls))
61 (make-instance 'ucon :cui cui :pfstr (car kmrcl::it)
62 :lrl (ensure-integer (cadr kmrcl::it)))))
65 (defun find-ucon-lui (lui &key (srl *current-srl*))
66 "Find list of ucon for lui"
67 (when (stringp lui) (setq lui (parse-lui lui)))
70 (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where LUI=~d" lui)))
72 (string-append ls (format nil " and KCUILRL <= ~d" srl)))
73 (dolist (tuple (mutex-sql-query ls))
74 (destructuring-bind (cui pfstr lrl) tuple
75 (push (make-instance 'ucon :cui (ensure-integer cui)
77 :lrl (ensure-integer lrl))
81 (defun find-ucon-sui (sui &key (srl *current-srl*))
82 "Find list of ucon for sui"
83 (when (stringp sui) (setq sui (parse-sui sui)))
86 (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where SUI=~d" sui)))
88 (string-append ls (format nil " and KCUILRL <= ~d" srl)))
89 (let ((tuples (mutex-sql-query ls)))
90 (dolist (tuple tuples)
91 (destructuring-bind (cui pfstr lrl) tuple
92 (push (make-instance 'ucon :cui (ensure-integer cui)
94 :lrl (ensure-integer lrl))
98 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
99 "Find ucon for cui/sui"
100 (when (stringp cui) (setq cui (parse-cui cui)))
101 (when (stringp sui) (setq sui (parse-sui sui)))
103 (let ((ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where KCUISUI=~d"
104 (make-cuisui cui sui))))
106 (string-append ls (format nil " and KCUILRL <= ~d" srl)))
107 (awhen (car (mutex-sql-query ls))
108 (destructuring-bind (cui pfstr lrl) it
109 (make-instance 'ucon :cui (ensure-integer cui)
111 :lrl (ensure-integer lrl)))))))
113 (defun find-ucon-str (str &key (srl *current-srl*))
114 "Find ucon that are exact match for str"
117 (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where STR='~a'" str)))
119 (string-append ls " and KCUILRL <= ~d" srl))
120 (dolist (tuple (mutex-sql-query ls))
121 (destructuring-bind (cui pfstr lrl) tuple
122 (push (make-instance 'ucon :cui (ensure-integer cui)
124 :lrl (ensure-integer lrl))
128 (defun find-ucon-all (&key (srl *current-srl*))
129 "Return list of all ucon's"
130 (let ((ls "select distinct CUI,KPFSTR,KCUILRL from MRCON"))
132 (string-append ls (format nil " where KCUILRL <= ~d" srl)))
133 (string-append ls " order by CUI asc")
134 (with-sql-connection (db)
137 #'(lambda (cui pfstr cuilrl)
138 (make-instance 'ucon :cui (ensure-integer cui)
140 :lrl (ensure-integer cuilrl)))
144 (defun map-ucon-all (fn &key (srl *current-srl*))
145 "Return list of all ucon's"
146 (let ((ls "select distinct CUI,KPFSTR,KCUILRL from MRCON"))
148 (string-append ls (format nil " where KCUILRL <= ~d" srl)))
149 (string-append ls " order by CUI asc")
150 (with-sql-connection (db)
153 #'(lambda (cui pfstr cuilrl)
155 (make-instance 'ucon :cui (ensure-integer cui)
157 :lrl (ensure-integer cuilrl))))
162 (defun find-udef-cui (cui &key (srl *current-srl*))
163 "Return a list of udefs for cui"
165 (ls (format nil "select SAB,DEF from MRDEF where CUI=~d" cui)))
167 (string-append ls (format nil " and KSRL <= ~d" srl)))
168 (dolist (tuple (mutex-sql-query ls))
169 (destructuring-bind (sab def) tuple
170 (push (make-instance 'udef :sab sab :def def) udefs)))
173 (defun find-usty-cui (cui &key (srl *current-srl*))
174 "Return a list of usty for cui"
176 (ls (format nil "select TUI,STY from MRSTY where CUI=~d" cui)))
178 (string-append ls (format nil " and KLRL <= ~d" srl)))
179 (dolist (tuple (mutex-sql-query ls))
180 (destructuring-bind (tui sty) tuple
181 (push (make-instance 'usty :tui (ensure-integer tui) :sty sty) ustys)))
184 (defun find-usty-word (word &key (srl *current-srl*))
185 "Return a list of usty that match word"
187 (ls (format nil "select distinct TUI,STY from MRSTY where STY like '%~a%'" word)))
189 (string-append ls (format nil " and KLRL <= ~d" srl)))
190 (dolist (tuple (mutex-sql-query ls))
191 (destructuring-bind (tui sty) tuple
192 (push (make-instance 'usty :tui (ensure-integer tui) :sty sty) ustys)))
195 (defun find-urel-cui (cui &key (srl *current-srl*))
196 "Return a list of urel for cui"
198 (ls (format nil "select REL,CUI2,RELA,SAB,SL,MG,KPFSTR2 from MRREL where CUI1=~d" cui)))
200 (string-append ls (format nil " and KSRL <= ~d" srl)))
201 (dolist (tuple (mutex-sql-query ls))
202 (destructuring-bind (rel cui2 rela sab sl mg pfstr2) tuple
203 (push (make-instance 'urel
206 :cui2 (ensure-integer cui2)
215 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
216 "Return a list of urel for cui2"
218 (ls (format nil "select REL,CUI1,RELA,SAB,SL,MG,KPFSTR2 from MRREL where CUI2=~d" cui2)))
220 (string-append ls (format nil " and SRL <= ~d" srl)))
221 (dolist (tuple (mutex-sql-query ls))
222 (destructuring-bind (rel cui1 rela sab sl mg pfstr2) tuple
223 (push (make-instance 'urel
226 :cui1 (ensure-integer cui1)
235 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
237 #'(lambda (cui) (find-ucon-cui cui :srl srl))
238 (remove-duplicates (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))))
240 (defun find-ucoc-cui (cui &key (srl *current-srl*))
241 "Return a list of ucoc for cui"
243 (ls (format nil "select CUI2,SOC,COT,COF,COA,KPFSTR2 from MRCOC where CUI1=~d" cui)))
245 (string-append ls (format nil " and KLRL <= ~d" srl)))
246 (string-append ls " order by COF asc")
247 (dolist (tuple (mutex-sql-query ls))
248 (destructuring-bind (cui2 soc cot cof coa pfstr2) tuple
249 (setq cui2 (ensure-integer cui2))
250 (when (zerop cui2) (setq cui2 nil))
251 (push (make-instance 'ucoc :cui1 cui
255 :cof (ensure-integer cof)
259 ucocs)) ;; akready ordered by SQL select
261 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
262 "Return a list of ucoc for cui2"
264 (ls (format nil "select CUI1,SOC,COT,COF,COA,KPFSTR2 from MRCOC where CUI2=~d" cui2)))
266 (string-append ls (format nil " and KSRL <= ~d" srl)))
267 (string-append ls " order by COF asc")
268 (dolist (tuple (mutex-sql-query ls))
269 (destructuring-bind (cui1 soc cot cof coa pfstr2) tuple
270 (push (make-instance 'ucoc :cui1 (ensure-integer cui1)
274 :cof (ensure-integer cof)
278 ucocs)) ;; already ordered by SQL select
280 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
281 "List of ucon with co-occurance cui2"
283 #'(lambda (cui) (find-ucon-cui cui :srl srl))
284 (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
286 (defun find-ulo-cui (cui &key (srl *current-srl*))
287 "Return a list of ulo for cui"
289 (ls (format nil "select ISN,FR,UN,SUI,SNA,SOUI from MRLO where CUI=~d" cui)))
291 (string-append ls (format nil " and KLRL <= ~d" srl)))
292 (dolist (tuple (mutex-sql-query ls))
293 (destructuring-bind (isn fr un sui sna soui) tuple
294 (push (make-instance 'ulo :isn isn
295 :fr (ensure-integer fr)
297 :sui (ensure-integer sui)
303 (defgeneric suistr (lo))
304 (defmethod suistr ((lo ulo))
305 "Return the string for a ulo object"
306 (find-string-sui (sui lo)))
308 (defun find-uatx-cui (cui &key (srl *current-srl*))
309 "Return a list of uatx for cui"
311 (ls (format nil "select SAB,REL,ATX from MRATX where CUI=~d" cui)))
313 (string-append ls (format nil " and KSRL <= ~d" srl)))
314 (dolist (tuple (mutex-sql-query ls))
315 (destructuring-bind (sab rel atx) tuple
316 (push (make-instance 'uatx :sab sab :rel rel :atx atx) uatxs)))
320 (defun find-uterm-cui (cui &key (srl *current-srl*))
321 "Return a list of uterm for cui"
323 (ls (format nil "select distinct LUI,LAT,TS,KLUILRL from MRCON where CUI=~d" cui)))
325 (string-append ls (format nil " and KLUILRL <= ~d" srl)))
326 (dolist (tuple (mutex-sql-query ls))
327 (destructuring-bind (lui lat ts lrl) tuple
328 (push (make-instance 'uterm :lui (ensure-integer lui) :cui cui
329 :lat lat :ts ts :lrl (ensure-integer lrl))
333 (defun find-uterm-lui (lui &key (srl *current-srl*))
334 "Return a list of uterm for lui"
336 (setq lui (parse-lui lui)))
338 (ls (format nil "select distinct CUI,LAT,TS,KLUILRL from MRCON where LUI=~d" lui)))
340 (string-append ls (format nil " and KLUILRL <= ~d" srl)))
341 (dolist (tuple (mutex-sql-query ls))
342 (destructuring-bind (cui lat ts lrl) tuple
343 (push (make-instance 'uterm :cui (ensure-integer cui) :lui lui
344 :lat lat :ts ts :lrl (ensure-integer lrl))
348 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
349 "Return single uterm for cui/lui"
350 (let ((ls (format nil "select LAT,TS,KLUILRL from MRCON where KCUILUI=~d limit 1" (make-cuilui cui lui))))
351 (when srl (string-append ls (format nil " and KLUILRL <= ~d" srl)))
352 (awhen (car (mutex-sql-query ls))
353 (destructuring-bind (lat ts lrl) it
354 (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts
355 :lrl (ensure-integer lrl))))))
357 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
358 "Return a list of ustr for cui/lui"
359 (declare (fixnum cui lui))
361 (ls (format nil "select SUI,STT,STR,LRL from MRCON where KCUILUI=~d" (make-cuilui cui lui))))
362 (when srl (string-append ls (format nil " and LRL <= ~d" srl)))
363 (dolist (tuple (mutex-sql-query ls))
364 (destructuring-bind (sui stt str lrl) tuple
366 (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui
367 :cuisui (make-cuisui cui sui) :stt stt :str str
368 :lrl (ensure-integer lrl))
372 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
373 "Return the single ustr for cuisui"
374 (let ((ls (format nil "select LUI,STT,STR,LRL from MRCON where KCUISUI=~d"
375 (make-cuisui cui sui))))
376 (when srl (string-append ls (format nil " and LRL <= ~d" srl)))
377 (awhen (car (mutex-sql-query ls))
378 (destructuring-bind (lui stt str lrl) it
379 (make-instance 'ustr :sui sui :cui cui
380 :cuisui (make-cuisui cui sui)
381 :lui (ensure-integer lui) :stt stt :str str
382 :lrl (ensure-integer lrl))))))
384 (defun find-ustr-sui (sui &key (srl *current-srl*))
385 "Return the list of ustr for sui"
387 (setq sui (parse-sui sui)))
389 (ls (format nil "select CUI,LUI,STT,STR,LRL from MRCON where SUI=~d" sui)))
390 (when srl (string-append ls (format nil " and LRL <= ~d" srl)))
391 (dolist (tuple (mutex-sql-query ls))
392 (destructuring-bind (cui lui stt str lrl) tuple
393 (setq cui (ensure-integer cui))
394 (push (make-instance 'ustr :sui sui :cui cui :stt stt :str str
395 :cuisui (make-cuisui cui sui)
396 :lui (ensure-integer lui)
397 :lrl (ensure-integer lrl))
401 (defun find-ustr-sab (sab &key (srl *current-srl*))
402 "Return the list of ustr for sab"
404 (ls (format nil "select KCUISUI from MRSO where SAB='~a'" sab)))
406 (string-append ls (format nil " and SRL <= ~d" srl)))
407 (dolist (tuple (mutex-sql-query ls))
408 (let ((cuisui (ensure-integer (car tuple))))
409 (push (apply #'find-ustr-cuisui
411 (multiple-value-list (decompose-cuisui cuisui))
416 (defun find-ustr-all (&key (srl *current-srl*))
417 "Return list of all ustr's"
418 (let ((ls "select distinct CUI,LUI,SUI,STT,LRL,KPFSTR from MRCON"))
420 (string-append ls (format nil " where LRL <= ~d" srl)))
421 (string-append ls " order by SUI asc")
422 (with-sql-connection (db)
425 #'(lambda (cui lui sui stt lrl pfstr)
426 (setq cui (ensure-integer cui))
427 (setq lui (ensure-integer lui))
428 (setq sui (ensure-integer sui))
429 (setq lrl (ensure-integer lrl))
430 (make-instance 'ustr :cui cui
433 :cuisui (make-cuisui cui sui)
440 (defun find-string-sui (sui &key (srl *current-srl*))
441 "Return the string associated with sui"
442 (let ((ls (format nil "select STR from MRCON where SUI=~d" sui)))
444 (string-append ls (format nil " and LRL <= ~d" srl)))
445 (string-append ls " limit 1")
446 (caar (mutex-sql-query ls))))
448 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
449 (declare (fixnum cui sui))
451 (ls (format nil "select SAB,CODE,SRL,TTY from MRSO where KCUISUI=~d"
452 (make-cuisui cui sui))))
454 (string-append ls (format nil " and SRL <= ~d" srl)))
455 (dolist (tuple (mutex-sql-query ls))
456 (destructuring-bind (sab code srl tty) tuple
457 (push (make-instance 'uso :sab sab :code code :srl srl :tty tty)
461 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
462 (declare (fixnum cui sui))
464 (ls (format nil "select SAB,CODE,CXN,CXL,RNK,CXS,CUI2,HCD,RELA,XC from MRCXT where KCUISUI=~d"
465 (make-cuisui cui sui))))
467 (string-append ls (format nil " and KSRL <= ~d" srl)))
468 (dolist (tuple (mutex-sql-query ls))
469 (destructuring-bind (sab code cxn cxl rnk cxs cui2 hcd rela xc) tuple
470 (push (make-instance 'ucxt :sab sab :code code
471 :cxn (ensure-integer cxn)
472 :cxl cxl :cxs cxs :hcd hcd :rela rela :xc xc
473 :rnk (ensure-integer rnk)
474 :cui2 (ensure-integer cui2))
478 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
479 (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
481 (sui (string-append ls (format nil "KCUISUI=~d" (make-cuisui cui sui))))
482 (lui (string-append ls (format nil "KCUILUI=~d and sui=0" (make-cuilui cui lui))))
483 (t (string-append ls (format nil "cui=~d and lui=0 and sui=0" cui))))
485 (string-append ls (format nil " and KSRL <= ~d" srl)))
487 (dolist (tuple (mutex-sql-query ls))
488 (destructuring-bind (code atn sab atv) tuple
489 (push (make-instance 'usat :code code :atn atn :sab sab :atv atv)
494 (defun find-pfstr-cui (cui)
495 (caar (mutex-sql-query (format nil "select KPFSTR from MRCON where CUI=~d limit 1" cui))))
497 (defun find-usty-tui (tui)
499 (setq tui (parse-tui tui))
500 (awhen (car (mutex-sql-query
501 (format nil "select STY from MRSTY where TUI=~d limit 1" tui)))
502 (make-instance 'usty :tui tui :sty (car it))))
504 (defun find-usty-sty (sty)
505 "Find usty for a sty"
506 (awhen (car (mutex-sql-query
507 (format nil "select TUI from MRSTY where STY='~a' limit 1" sty)))
508 (make-instance 'usty :tui (ensure-integer (car it)) :sty sty)))
510 (defun find-usty-all ()
511 "Return list of usty's for all semantic types"
513 (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
514 (push (find-usty-tui (nth 0 tuple)) ustys))
517 (defun find-usab-all ()
518 "Find usab for a key"
520 (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"))
522 (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) tuple
524 (make-instance 'usab :vcui (ensure-integer vcui)
525 :rcui (ensure-integer rcui)
526 :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart
527 :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc
528 :srl (ensure-integer srl)
529 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
530 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
531 :curver curver :sabin sabin)
535 (defun find-usab-by-key (key-name key)
536 "Find usab for a key"
537 (aif (car (mutex-sql-query
538 (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)))
540 (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) it
541 (make-instance 'usab :vcui (ensure-integer vcui)
542 :rcui (ensure-integer rcui)
543 :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart
544 :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc
545 :srl (ensure-integer srl)
546 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
547 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
548 :curver curver :sabin sabin))))
550 (defun find-usab-rsab (rsab)
552 (find-usab-by-key "RSAB" rsab))
554 (defun find-usab-vsab (vsab)
556 (find-usab-by-key "VSAB" vsab))
558 (defun find-cui-max ()
559 (let ((cui (caar (mutex-sql-query "select max(CUI) from MRCON"))))
560 (ensure-integer cui)))
562 ;;;; Cross table find functions
564 (defun find-ucon-tui (tui &key (srl *current-srl*))
565 "Find list of ucon for tui"
566 (when (stringp tui) (setq tui (parse-tui tui)))
568 (ls (format nil "select CUI from MRSTY where TUI=~d" tui)))
569 (when srl (string-append ls (format nil " and KLRL <= ~d" srl)))
570 (string-append ls " order by cui desc")
571 (dolist (tuple (mutex-sql-query ls))
572 (push (find-ucon-cui (ensure-integer (car tuple)) :srl srl) ucons))
575 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
576 "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
578 (ls (format nil "select distinct cui from MRXW_ENG where wd~A'~A'"
579 (if like " LIKE " "=")
581 (when srl (string-append ls (format nil " and KLRL <= ~d" srl)))
582 (string-append ls " order by cui desc")
583 (dolist (tuple (mutex-sql-query ls))
584 (push (find-ucon-cui (car tuple) :srl srl) ucons))
587 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
588 "Return list of ucons that match word, optionally use SQL's LIKE syntax"
590 (ls (format nil "select distinct cui from MRXNW_ENG where nwd~A'~A'"
591 (if like " LIKE " "=")
593 (when srl (string-append ls (format nil " and KLRL <= ~d" srl)))
594 (string-append ls " order by cui desc")
595 (dolist (tuple (mutex-sql-query ls))
596 (push (find-ucon-cui (car tuple) :srl srl) ucons))
599 (defun find-ustr-word (word &key (srl *current-srl*))
600 "Return list of ustrs that match word"
602 (ls (format nil "select cui,sui from MRXW_ENG where wd='~a'" word)))
603 (when srl (string-append ls (format nil " and KLRL <= ~d" srl)))
604 (string-append ls " order by cui desc,sui desc")
605 (dolist (tuple (mutex-sql-query ls))
606 (destructuring-bind (cui sui) tuple
607 (push (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui)
612 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
613 "Return list of ustrs that match word"
615 (ls (format nil "select cui,sui from MRXNW_ENG where nwd='~a'" word)))
617 (string-append ls (format nil " and KLRL <= ~d" srl)))
618 (string-append ls " order by cui desc,sui desc")
619 (dolist (tuple (mutex-sql-query ls))
620 (destructuring-bind (cui sui) tuple
621 (push (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui)
628 (defun find-usrl-all ()
630 (tuples (mutex-sql-query "select SAB,SRL from USRL order by SAB desc")))
631 (dolist (tuple tuples)
632 (destructuring-bind (sab srl) tuple
633 (push (make-instance 'usrl :sab sab :srl (ensure-integer srl)) usrls)))
636 ;;; Multiword lookup and score functions
638 (defun find-ucon-multiword (str &key (srl *current-srl*))
639 "Return sorted list of ucon's that match a multiword string"
640 (let* ((words (delimited-string-to-list str #\space))
643 (setq ucons (append ucons (find-ucon-word word :srl srl))))
644 (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
646 (defun find-ustr-multiword (str &key (srl *current-srl*))
647 "Return sorted list of ustr's that match a multiword string"
648 (let* ((words (delimited-string-to-list str #\space))
651 (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
652 (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
654 (defun sort-score-ucon-str (str ucons)
655 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
656 (sort-score-umlsclass-str ucons str #'pfstr))
658 (defun sort-score-ustr-str (str ustrs)
659 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
660 (sort-score-umlsclass-str ustrs str #'str))
662 (defun sort-score-umlsclass-str (objs str lookup-func)
663 "Sort a list of objects based on scoring to a string"
668 (score-multiword-match str (funcall lookup-func obj)))
670 (mapcar #'car (sort scored #'> :key #'cadr))))
672 (defun score-multiword-match (s1 s2)
673 "Score a match between two strings with s1 being reference string"
674 (let* ((word-list-1 (delimited-string-to-list s1 #\space))
675 (word-list-2 (delimited-string-to-list s2 #\space))
676 (n1 (length word-list-1))
677 (n2 (length word-list-2))
682 short-list long-list)
683 (declare (fixnum n1 n2 nshort nlong score unmatched))
688 (setq long-list word-list-1)
689 (setq short-list word-list-2))
693 (setq long-list word-list-2)
694 (setq short-list word-list-1)))
695 (decf score (- nlong nshort)) ;; reduce score for extra words
696 (dotimes (iword nshort)
697 (declare (fixnum iword))
698 (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
700 (incf score (- 10 (abs (- kmrcl::it iword))))
702 (decf score (* 2 unmatched))
706 ;;; LEX SQL functions
708 (defun find-lexterm-eui (eui)
709 (awhen (car (mutex-sql-query
710 (format nil "select WRD from LRWD where EUI=~d" eui)))
711 (make-instance 'lexterm :eui eui :wrd (car it))))
713 (defun find-lexterm-word (wrd)
714 (let ((tuples (mutex-sql-query
715 (format nil "select EUI from LRWD where WRD='~a'" wrd)))
717 (dolist (tuple tuples)
718 (push (make-instance 'lexterm :eui (ensure-integer (car tuple))
723 ;; LEX SQL Read functions
725 (defun find-labr-eui (eui)
728 (format nil "select BAS,ABR,EUI2,BAS2 from LRABR where EUI=~d" eui)))
730 (dolist (tuple tuples)
731 (destructuring-bind (bas abr eui2 bas2) tuple
732 (push (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2
733 :eui2 (ensure-integer eui2))
737 (defun find-labr-bas (bas)
740 (format nil "select EUI,ABR,EUI2,BAS2 from LRABR where BAS='~a'"
743 (dolist (tuple tuples)
744 (destructuring-bind (eui abr eui2 bas2) tuple
746 (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2
747 :bas (copy-seq bas) :eui2 (ensure-integer eui2))
751 (defun find-lagr-eui (eui)
754 (format nil "select STR,SCA,AGR,CIT,BAS from LRAGR where EUI=~d" eui)))
756 (dolist (tuple tuples)
757 (destructuring-bind (str sca agr cit bas) tuple
758 (push (make-instance 'lagr :eui eui :str str :sca sca :agr agr :
763 (defun find-lcmp-eui (eui)
764 (let ((tuples (mutex-sql-query
765 (format nil "select BAS,SCA,COM from LRCMP where EUI=~d" eui)))
767 (dolist (tuple tuples)
768 (destructuring-bind (bas sca com) tuple
769 (push (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)
773 (defun find-lmod-eui (eui)
777 "select BAS,SCA,PSN_MOD,FEA from LRMOD where EUI=~d" eui)))
779 (dolist (tuple tuples)
780 (destructuring-bind (bas sca psnmod fea) tuple
781 (push (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psnmod
786 (defun find-lnom-eui (eui)
790 nil "select BAS,SCA,EUI2,BAS2,SCA2 from LRNOM where EUI=~d" eui)))
792 (dolist (tuple tuples)
793 (destructuring-bind (bas sca eui2 bas2 sca2) tuple
795 (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2
796 :eui2 (ensure-integer eui2))
800 (defun find-lprn-eui (eui)
805 "select BAS,NUM,GND,CAS,POS,QNT,FEA from LRPRN where EUI=~d" eui)))
807 (dolist (tuple tuples)
808 (destructuring-bind (bas num gnd cas pos qnt fea) tuple
809 (push (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd
810 :cas cas :pos pos :qnt qnt :fea fea)
814 (defun find-lprp-eui (eui)
817 (format nil "select BAS,STR,SCA,FEA from LRPRP where EUI=~d" eui)))
819 (dolist (tuple tuples)
820 (destructuring-bind (bas str sca fea) tuple
822 (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)
826 (defun find-lspl-eui (eui)
827 (let ((tuples (mutex-sql-query
828 (format nil "select SPV,BAS from LRSPL where EUI=~d" eui)))
830 (dolist (tuple tuples)
831 (destructuring-bind (spv bas) tuple
832 (push (make-instance 'lspl :eui eui :spv spv :bas bas) results)))
835 (defun find-ltrm-eui (eui)
836 (let ((tuples (mutex-sql-query
837 (format nil "select BAS,GEN from LRTRM where EUI=~d" eui)))
839 (dolist (tuple tuples)
840 (destructuring-bind (bas gen) tuple
841 (push (make-instance 'ltrm :eui eui :bas bas :gen gen) results)))
844 (defun find-ltyp-eui (eui)
845 (let ((tuples (mutex-sql-query
846 (format nil "select BAS,SCA,TYP from LRTYP where EUI=~d" eui)))
848 (dolist (tuple tuples)
849 (destructuring-bind (bas sca typ) tuple
850 (push (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)
854 (defun find-lwd-wrd (wrd)
855 (let ((tuples (mutex-sql-query
856 (format nil "select EUI from LRWD where WRD='~a'" wrd)))
858 (dolist (tuple tuples)
859 (push (ensure-integer (car tuple)) results))
860 (make-instance 'lwd :wrd wrd :euilist (nreverse results))))
862 ;;; Semantic Network SQL access functions
864 (defun find-sdef-ui (ui)
865 (awhen (car (mutex-sql-query
866 (format nil "select RT,STY_RL,STN_RTN,DEF,EX,UN,RH,ABR,RIN from SRDEF where UI=~d" ui)))
867 (destructuring-bind (rt styrl stnrtn def ex un rh abr rin) it
868 (make-instance 'sdef :rt rt :ui ui :styrl styrl :stnrtn stnrtn
869 :def def :ex ex :un un :rh rh :abr abr :rin rin))))
871 (defun find-sstre1-ui (ui)
872 (let ((tuples (mutex-sql-query
873 (format nil "select UI2,UI3 from SRSTRE1 where UI=~d" ui)))
875 (dolist (tuple tuples)
876 (destructuring-bind (ui2 ui3) tuple
877 (push (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2)
878 :ui3 (ensure-integer ui3))
882 (defun find-sstre1-ui2 (ui2)
883 (let ((tuples (mutex-sql-query
884 (format nil "select UI,UI3 from SRSTRE1 where UI2=~d" ui2)))
886 (dolist (tuple tuples)
887 (destructuring-bind (ui ui3) tuple
888 (push (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2
889 :ui3 (ensure-integer ui3))
893 (defun find-sstr-rl (rl)
896 (format nil "select STY_RL,STY_RL2,LS from SRSTRE where RL='~a'" rl)))
898 (dolist (tuple tuples)
899 (destructuring-bind (styrl styrl2 ls) tuple
900 (push (make-instance 'sstr :rl rl :styrl styrl :styrl2 styrl2 :ls ls)
905 (defun find-sstre2-sty (sty)
906 (let ((tuples (mutex-sql-query
907 (format nil "select RL,STY2 from SRSTRE2 where STY='~a'" sty)))
909 (dolist (tuple tuples)
910 (destructuring-bind (rl sty2) tuple
911 (push (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)
915 (defun find-sstr-styrl (styrl)
918 (format nil "select RL,STY_RL2,LS from SRSTR where RL='~a'" styrl)))
920 (dolist (tuple tuples)
921 (destructuring-bind (rl styrl2 ls) tuple
922 (push (make-instance 'sstr :styrl styrl :rl rl :styrl2 styrl2 :ls ls)