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 ;;;; Author: Kevin M. Rosenberg
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2003 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 ;;;; *************************************************************************
19 (in-package #:umlisp-orf)
22 (defvar *current-srl* nil)
25 (defun current-srl! (srl)
26 (setq *current-srl* srl))
28 (defmacro query-string (table fields &optional srl where-name where-value
29 &key (lrl "KCUILRL") single distinct order like)
30 (let* ((%%fields (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
31 (if distinct "distinct " "") fields table))
32 (%%order (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}"
35 (%%lrl (format nil " and ~:@(~A~)<=" lrl))
36 (%%where (when where-name
37 (format nil " where ~:@(~A~)~A" where-name
38 (if like " like " "")))))
42 ,@(when %%where (list %%where))
44 `((typecase ,where-value
46 (concatenate 'string "='" (prefixed-fixnum-string ,where-value #\0 10) "'"))
48 (concatenate 'string "='" (write-to-string ,where-value) "'"))
52 (format nil ,(if like "'%~A%'" "='~A'") ,where-value)))))
53 (if ,srl (concatenate 'string ,%%lrl (write-to-string ,srl)) "")
54 ,@(when %%order (list %%order))
55 ,@(when single (list " limit 1")))))
57 (defun query-string-eval (table fields &optional srl where-name where-value
58 &key (lrl "KCUILRL") single distinct order like)
61 (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
62 (if distinct "distinct " "") fields table)
63 (if where-name (format nil " where ~:@(~A~)" where-name) "")
70 (if like " like '%~A%""='~A'")))
73 (if srl (format nil " and ~:@(~A~)<=~D" lrl srl) "")
74 (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) "")
75 (if single " limit 1" "")))
78 (defmacro umlisp-query (table fields srl where-name where-value
79 &key (lrl "KCUILRL") single distinct order like
80 (query-cmd 'mutex-sql-query))
81 "Query the UMLisp database. Return a list of umlisp objects whose name
82 is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
84 (query-string ,table ,fields ,srl ,where-name ,where-value
85 :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
87 (defmacro umlisp-query-eval (table fields srl where-name where-value
88 &key (lrl "KCUILRL") single distinct order like)
89 "Query the UMLisp database. Return a list of umlisp objects whose name
90 is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
92 (query-string-eval ,table ,fields ,srl ,where-name ,where-value
93 :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
95 ;; only WHERE-VALUE and SRL are evaluated
96 (defmacro collect-umlisp-query ((table fields srl where-name where-value
97 &key (lrl "KCUILRL") distinct single
98 order like (query-cmd 'mutex-sql-query))
100 (let ((value (gensym))
103 `(let* ((,value ,where-value)
104 (tuple (car (umlisp-query ,table ,fields ,srl ,where-name ,value
105 :lrl ,lrl :single ,single
106 :distinct ,distinct :order ,order
108 :query-cmd ,query-cmd))))
109 ,@(unless where-name `((declare (ignore ,value))))
111 (destructuring-bind ,fields tuple
113 `(let ((,value ,where-value))
114 ,@(unless where-name `((declare (ignore ,value))))
116 (dolist (tuple (umlisp-query ,table ,fields ,srl ,where-name ,value
117 :lrl ,lrl :single ,single :distinct ,distinct
118 :order ,order :like ,like))
119 (push (destructuring-bind ,fields tuple ,@body) ,r))
123 (umlisp-query ,table ,fields ,srl ,where-name ,value
124 :lrl ,lrl :single ,single :distinct ,distinct
125 :order ,order :like ,like)
126 collect (destructuring-bind ,fields tuple ,@body))))))
128 (defmacro collect-umlisp-query-eval ((table fields srl where-name where-value
129 &key (lrl "KCUILRL") distinct single
132 (let ((value (gensym))
134 (eval-fields (cadr fields)))
136 `(let* ((,value ,where-value)
137 (tuple (car (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
138 :lrl ,lrl :single ,single
139 :distinct ,distinct :order ,order
142 (destructuring-bind ,eval-fields tuple
144 `(let ((,value ,where-value)
146 (dolist (tuple (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
147 :lrl ,lrl :single ,single :distinct ,distinct
148 :order ,order :like ,like))
149 (push (destructuring-bind ,eval-fields tuple ,@body) ,r))
153 (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
154 :lrl ,lrl :single ,single :distinct ,distinct
155 :order ,order :like ,like)
156 collect (destructuring-bind ,eval-fields tuple ,@body))))))
159 ;;; Read from SQL database
161 (defmacro ensure-cui-integer (cui)
163 (setq ,cui (parse-cui ,cui))
166 (defmacro ensure-lui-integer (lui)
168 (setq ,lui (parse-lui ,lui))
171 (defmacro ensure-sui-integer (sui)
173 (setq ,sui (parse-sui ,sui))
176 (defmacro ensure-tui-integer (tui)
178 (setq ,tui (parse-tui ,tui))
181 (defmacro ensure-eui-integer (eui)
183 (setq ,eui (parse-eui ,eui))
186 (defun find-ucon-cui (cui &key (srl *current-srl*))
187 "Find ucon for a cui"
188 (ensure-cui-integer cui)
189 (collect-umlisp-query (mrcon (kpfstr kcuilrl) srl cui cui :single t)
190 (make-instance 'ucon :cui cui :pfstr kpfstr
191 :lrl (ensure-integer kcuilrl))))
193 (defun find-ucon-cui-sans-pfstr (cui &key (srl *current-srl*))
194 "Find ucon for a cui"
195 (ensure-cui-integer cui)
196 (collect-umlisp-query (mrcon (kcuilrl) srl cui cui :single t)
197 (make-instance 'ucon :cui cui :lrl (ensure-integer kcuilrl)
200 (defun find-pfstr-cui (cui &key (srl *current-srl*))
201 "Find preferred string for a cui"
202 (ensure-cui-integer cui)
203 (collect-umlisp-query (mrcon (kpfstr) srl cui cui :single t)
206 (defun find-ucon-lui (lui &key (srl *current-srl*))
207 "Find list of ucon for lui"
208 (ensure-lui-integer lui)
209 (collect-umlisp-query (mrcon (cui kpfstr kcuilrl) srl lui lui
211 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
212 :lrl (ensure-integer kcuilrl))))
214 (defun find-ucon-sui (sui &key (srl *current-srl*))
215 "Find list of ucon for sui"
216 (ensure-sui-integer sui)
217 (collect-umlisp-query (mrcon (cui kpfstr kcuilrl) srl sui sui :distinct t)
218 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
219 :lrl (ensure-integer kcuilrl))))
221 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
222 "Find ucon for cui/sui"
223 (ensure-cui-integer cui)
224 (ensure-sui-integer sui)
226 (collect-umlisp-query (mrcon (kpfstr kcuilrl) srl kcuisui
227 (make-cuisui cui sui))
228 (make-instance 'ucon :cui cui
230 :lrl (ensure-integer kcuilrl)))))
232 (defun find-ucon-str (str &key (srl *current-srl*))
233 "Find ucon that are exact match for str"
234 (collect-umlisp-query (mrcon (cui kpfstr kcuilrl) srl str str :distinct t)
235 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
236 :lrl (ensure-integer kcuilrl))))
238 (defun find-ucon-all (&key (srl *current-srl*))
239 "Return list of all ucon's"
240 (with-sql-connection (db)
244 (destructuring-bind (cui pfstr cuilrl) tuple
245 (make-instance 'ucon :cui (ensure-integer cui)
247 :lrl (ensure-integer cuilrl))))
248 (query-string mrcon (cui kpfstr kcuilrl) srl nil nil
249 :order (cui asc) :distinct t)
252 (defun find-ucon-all2 (&key (srl *current-srl*))
253 "Return list of all ucon's"
254 (collect-umlisp-query (mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
256 (make-instance 'ucon :cui (ensure-integer cui)
258 :lrl (ensure-integer kcuilrl))))
260 (defun find-cui-ucon-all (&key (srl *current-srl*))
261 "Return list of CUIs for all ucons"
262 (collect-umlisp-query (mrcon (cui) srl nil nil :order (cui asc)
266 (defun map-ucon-all (fn &key (srl *current-srl*))
267 "Map a function over all ucon's"
268 (with-sql-connection (db)
272 (destructuring-bind (cui pfstr cuilrl) tuple
274 (make-instance 'ucon :cui (ensure-integer cui)
276 :lrl (ensure-integer cuilrl)))))
277 (query-string mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
282 (defun find-udef-cui (cui &key (srl *current-srl*))
283 "Return a list of udefs for cui"
284 (ensure-cui-integer cui)
285 (collect-umlisp-query (mrdef (sab def) srl cui cui :lrl "KSRL")
286 (make-instance 'udef :sab sab :def def)))
288 (defun find-usty-cui (cui &key (srl *current-srl*))
289 "Return a list of usty for cui"
290 (ensure-cui-integer cui)
291 (collect-umlisp-query (mrsty (tui sty) srl cui cui :lrl "KLRL")
292 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
294 (defun find-usty-word (word &key (srl *current-srl*))
295 "Return a list of usty that match word"
296 (collect-umlisp-query (mrsty (tui sty) srl sty word :lrl klrl :like t
298 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
300 (defun find-urel-cui (cui &key (srl *current-srl*))
301 "Return a list of urel for cui"
302 (ensure-cui-integer cui)
303 (collect-umlisp-query (mrrel (rel cui2 rela sab sl mg kpfstr2) srl cui1
305 (make-instance 'urel :cui1 cui :rel rel
306 :cui2 (ensure-integer cui2) :rela rela :sab sab :sl sl
307 :mg mg :pfstr2 kpfstr2)))
309 (defun find-cui2-urel-cui (cui &key (srl *current-srl*))
310 "Return a list of urel for cui"
311 (ensure-cui-integer cui)
312 (collect-umlisp-query (mrrel (cui2) srl cui1
316 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
317 "Return a list of urel for cui2"
318 (ensure-cui-integer cui2)
319 (collect-umlisp-query (mrrel (rel cui1 rela sab sl mg kpfstr2) srl cui2
321 (make-instance 'urel :cui2 cui2 :rel rel
322 :cui1 (ensure-integer cui1) :rela rela :sab sab :sl sl
323 :mg mg :pfstr2 kpfstr2)))
325 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
326 (ensure-cui-integer cui2)
327 (loop for cui in (remove-duplicates
328 (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))
329 collect (find-ucon-cui cui :srl srl)))
331 (defun find-ucoc-cui (cui &key (srl *current-srl*))
332 "Return a list of ucoc for cui"
333 (ensure-cui-integer cui)
334 (collect-umlisp-query (mrcoc (cui2 soc cot cof coa kpfstr2) srl cui1
335 cui :lrl klrl :order (cof asc))
336 (setq cui2 (ensure-integer cui2))
337 (when (eql 0 cui2) (setq cui2 nil))
338 (make-instance 'ucoc :cui1 cui :cui2 (ensure-integer cui2)
339 :soc soc :cot cot :cof (ensure-integer cof) :coa coa
342 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
343 "Return a list of ucoc for cui2"
344 (ensure-cui-integer cui2)
345 (collect-umlisp-query (mrcoc (cui1 soc cot cof coa kpfstr2) srl cui2
346 cui2 :lrl klrl :order (cof asc))
347 (when (zerop cui2) (setq cui2 nil))
348 (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 cui2
349 :soc soc :cot cot :cof (ensure-integer cof) :coa coa
352 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
353 "List of ucon with co-occurance cui2"
354 (ensure-cui-integer cui2)
356 #'(lambda (cui) (find-ucon-cui cui :srl srl))
357 (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
359 (defun find-ulo-cui (cui &key (srl *current-srl*))
360 "Return a list of ulo for cui"
361 (ensure-cui-integer cui)
362 (collect-umlisp-query (mrlo (isn fr un sui sna soui) srl cui cui
364 (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un
365 :sui (ensure-integer sui) :sna sna :soui soui)))
367 (defun find-uatx-cui (cui &key (srl *current-srl*))
368 "Return a list of uatx for cui"
369 (ensure-cui-integer cui)
370 (collect-umlisp-query (mratx (sab rel atx) srl cui cui :lrl ksrl)
371 (make-instance 'uatx :sab sab :rel rel :atx atx)))
374 (defun find-uterm-cui (cui &key (srl *current-srl*))
375 "Return a list of uterm for cui"
376 (ensure-cui-integer cui)
377 (collect-umlisp-query (mrcon (lui lat ts kluilrl) srl cui cui
378 :lrl kluilrl :distinct t)
379 (make-instance 'uterm :lui (ensure-integer lui) :cui cui
380 :lat lat :ts ts :lrl (ensure-integer kluilrl))))
382 (defun find-uterm-lui (lui &key (srl *current-srl*))
383 "Return a list of uterm for lui"
384 (ensure-lui-integer lui)
385 (collect-umlisp-query (mrcon (cui lat ts kluilrl) srl lui lui
386 :lrl kluilrl :distinct t)
387 (make-instance 'uterm :cui (ensure-integer cui) :lui lui
388 :lat lat :ts ts :lrl (ensure-integer kluilrl))))
390 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
391 "Return single uterm for cui/lui"
392 (ensure-cui-integer cui)
393 (ensure-lui-integer lui)
394 (collect-umlisp-query (mrcon (lat ts kluilrl) srl kcuilui
395 (make-cuilui cui lui)
396 :lrl kluilrl :single t)
397 (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts
398 :lrl (ensure-integer kluilrl))))
400 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
401 "Return a list of ustr for cui/lui"
402 (ensure-cui-integer cui)
403 (ensure-lui-integer lui)
404 (collect-umlisp-query (mrcon (sui stt str lrl) srl kcuilui
405 (make-cuilui cui lui) :lrl lrl)
406 (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui
407 :cuisui (make-cuisui cui sui) :stt stt :str str
408 :lrl (ensure-integer lrl))))
410 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
411 "Return the single ustr for cuisui"
412 (ensure-cui-integer cui)
413 (ensure-sui-integer sui)
414 (collect-umlisp-query (mrcon (lui stt str lrl) srl kcuisui
415 (make-cuisui cui sui) :lrl lrl :single t)
416 (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui)
417 :lui (ensure-integer lui) :stt stt :str str
418 :lrl (ensure-integer lrl))))
420 (defun find-ustr-sui (sui &key (srl *current-srl*))
421 "Return the list of ustr for sui"
422 (ensure-sui-integer sui)
423 (collect-umlisp-query (mrcon (cui lui stt str lrl) srl sui sui
425 (make-instance 'ustr :sui sui :cui cui :stt stt :str str
426 :cuisui (make-cuisui (ensure-integer cui) sui)
427 :lui (ensure-integer lui) :lrl (ensure-integer lrl))))
429 (defun find-ustr-sab (sab &key (srl *current-srl*))
430 "Return the list of ustr for sab"
431 (collect-umlisp-query (mrso (kcuisui) srl sab sab :lrl srl)
432 (let ((cuisui (ensure-integer kcuisui)))
433 (apply #'find-ustr-cuisui
435 (multiple-value-list (decompose-cuisui cuisui))
438 (defun find-ustr-all (&key (srl *current-srl*))
439 "Return list of all ustr's"
440 (with-sql-connection (db)
444 (destructuring-bind (cui lui sui stt lrl pfstr) tuple
445 (make-instance 'ustr :cui (ensure-integer cui)
446 :lui (ensure-integer lui) :sui (ensure-integer sui)
448 :cuisui (make-cuisui (ensure-integer cui)
449 (ensure-integer sui))
450 :lrl (ensure-integer lrl))))
451 (query-string mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl
456 (defun find-string-sui (sui &key (srl *current-srl*))
457 "Return the string associated with sui"
458 (ensure-sui-integer sui)
459 (collect-umlisp-query (mrcon (str) srl sui sui :lrl lrl :single t)
462 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
463 (ensure-sui-integer sui)
464 (ensure-cui-integer cui)
465 (collect-umlisp-query (mrso (sab code srl tty) srl kcuisui
466 (make-cuisui cui sui) :lrl srl)
467 (make-instance 'uso :sab sab :code code :srl srl :tty tty)))
469 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
470 (ensure-cui-integer cui)
471 (ensure-sui-integer sui)
472 (collect-umlisp-query (mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela xc)
473 srl kcuisui (make-cuisui cui sui) :lrl ksrl)
474 (make-instance 'ucxt :sab sab :code code
475 :cxn (ensure-integer cxn) :cxl cxl :cxs cxs :hcd hcd
476 :rela rela :xc xc :rnk (ensure-integer rnk)
477 :cui2 (ensure-integer cui2))))
479 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
480 (ensure-cui-integer cui)
481 (ensure-lui-integer lui)
482 (ensure-sui-integer sui)
483 (let ((ls "select CODE,ATN,SAB,ATV from MRSAT where "))
485 (sui (string-append ls "KCUISUI='"
486 (integer-string (make-cuisui cui sui) 14)
488 (lui (string-append ls "KCUILUI='"
489 (integer-string (make-cuilui cui lui) 14)
491 (t (string-append ls "cui='" (prefixed-fixnum-string cui nil 7)
492 "' and lui='0' and sui='0'")))
494 (string-append ls " and KSRL<=" (prefixed-fixnum-string srl nil 3)))
495 (loop for tuple in (mutex-sql-query ls) collect
496 (destructuring-bind (code atn sab atv) tuple
497 (make-instance 'usat :code code :atn atn :sab sab :atv atv)))))
499 (defun find-usty-tui (tui)
501 (ensure-tui-integer tui)
502 (collect-umlisp-query (mrsty (sty) nil tui tui :single t)
503 (make-instance 'usty :tui tui :sty sty)))
505 (defun find-usty-sty (sty)
506 "Find usty for a sty"
507 (collect-umlisp-query (mrsty (tui) nil sty sty :single t)
508 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
510 (defun find-usty-all ()
511 "Return list of usty's for all semantic types"
512 (collect-umlisp-query (mrsty (tui) nil nil nil :distinct t)
513 (find-usty-tui tui)))
515 (defun find-usab-all ()
516 "Find usab for a key"
517 (collect-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver vstart vend imeta
518 rmeta slc scc srl tfr cfr cxty ttyl atnl lat
519 cenc curver sabin) nil nil nil)
520 (make-instance 'usab :vcui (ensure-integer vcui)
521 :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
522 :sf sf :sver sver :vstart vstart :vend vend :imeta imeta
523 :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
524 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
525 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
526 :curver curver :sabin sabin)))
528 (defun find-usab-by-key (key-name key)
529 "Find usab for a key"
530 (collect-umlisp-query-eval ('mrsab '(vcui rcui vsab rsab son sf sver vstart
531 vend imeta rmeta slc scc srl tfr cfr cxty
532 ttyl atnl lat cenc curver sabin)
533 nil key-name key :single t)
534 (make-instance 'usab :vcui (ensure-integer vcui)
535 :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
536 :sf sf :sver sver :vstart vstart :vend vend :imeta imeta
537 :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
538 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
539 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
540 :curver curver :sabin sabin)))
542 (defun find-usab-rsab (rsab)
544 (find-usab-by-key 'rsab rsab))
546 (defun find-usab-vsab (vsab)
548 (find-usab-by-key 'vsab vsab))
550 (defun find-cui-max ()
551 (ensure-integer (caar (mutex-sql-query "select max(CUI) from MRCON"))))
553 ;;;; Cross table find functions
555 (defun find-ucon-tui (tui &key (srl *current-srl*))
556 "Find list of ucon for tui"
557 (ensure-tui-integer tui)
558 (collect-umlisp-query (mrsty (cui) srl tui tui :lrl klrl :order (cui asc))
559 (find-ucon-cui (ensure-integer cui) :srl srl)))
561 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
562 "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
563 (collect-umlisp-query-eval ('mrxw_eng '(cui) srl 'wd word :like like :distinct t
564 :lrl 'klrl :order '(cui asc))
565 (find-ucon-cui cui :srl srl)))
567 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
568 "Return list of ucons that match word, optionally use SQL's LIKE syntax"
569 (collect-umlisp-query-eval ('mrxnw_eng '(cui) srl 'nwd word :like like :distinct t
570 :lrl 'klrl :order '(cui asc))
571 (find-ucon-cui cui :srl srl)))
573 (defun find-cui-normalized-word (word &key (srl *current-srl*) (like nil))
574 "Return list of cui that match word, optionally use SQL's LIKE syntax"
575 (collect-umlisp-query-eval ('mrxnw_eng '(cui) srl 'nwd word :like like :distinct t
576 :lrl 'klrl :order '(cui asc))
579 (defun find-lui-normalized-word (word &key (srl *current-srl*) (like nil))
580 "Return list of cui that match word, optionally use SQL's LIKE syntax"
581 (collect-umlisp-query-eval ('mrxnw_eng '(lui) srl 'nwd word :like like :distinct t
582 :lrl 'klrl :order '(cui asc))
585 (defun find-sui-normalized-word (word &key (srl *current-srl*) (like nil))
586 "Return list of cui that match word, optionally use SQL's LIKE syntax"
587 (collect-umlisp-query-eval ('mrxnw_eng '(sui) srl 'nwd word :like like :distinct t
588 :lrl 'klrl :order '(cui asc))
591 (defun find-ustr-word (word &key (srl *current-srl*))
592 "Return list of ustrs that match word"
593 (collect-umlisp-query (mrxw_eng (cui sui) srl wd word :lrl klrl
594 :order (cui asc sui asc))
595 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
597 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
598 "Return list of ustrs that match word"
599 (collect-umlisp-query (mrxnw_eng (cui sui) srl nwd word :lrl klrl
600 :order (cui asc sui asc))
601 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
603 (defun find-uterm-word (word &key (srl *current-srl*))
604 "Return list of uterms that match word"
605 (collect-umlisp-query (mrxw_eng (cui lui) srl wd word :lrl klrl
606 :order (cui asc lui asc))
607 (find-uterm-cuilui (ensure-integer cui) (ensure-integer lui) :srl srl)))
609 (defun find-uterm-normalized-word (word &key (srl *current-srl*))
610 "Return list of uterms that match word"
611 (collect-umlisp-query (mrxnw_eng (cui lui) srl nwd word :lrl klrl
612 :order (cui asc lui asc))
613 (find-uterm-cuilui (ensure-integer cui) (ensure-integer lui) :srl srl)))
615 (defun find-ucon-noneng-word (word &key (srl *current-srl*) (like nil))
616 "Return list of ucons that match non-english word"
617 (collect-umlisp-query-eval ('mrxw_noneng '(cui) srl 'wd word :like like
618 :distinct t :lrl 'klrl :order '(cui asc))
619 (find-ucon-cui cui :srl srl)))
621 (defun find-ustr-noneng-word (word &key (srl *current-srl*))
622 "Return list of ustrs that match non-english word"
623 (collect-umlisp-query (mrxw_noneng (cui sui) srl wd word :lrl klrl
624 :order (cui asc sui asc))
625 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
629 (defun find-usrl-all ()
630 (collect-umlisp-query (usrl (sab srl) nil nil nil :order (sab asc))
631 (make-instance 'usrl :sab sab :srl (ensure-integer srl))))
633 ;;; Multiword lookup and score functions
635 (defun find-uobj-multiword (str obj-lookup-fun sort-fun key srl
638 (dolist (word (delimited-string-to-list str #\space))
639 (setq uobjs (append uobjs (funcall obj-lookup-fun word :srl srl))))
641 (funcall sort-fun str
642 (delete-duplicates uobjs :test #'= :key key))))
643 (if (and (plusp (length sorted))
645 (multiword-match str (pfstr (first sorted))))
649 (defun find-ucon-multiword (str &key (srl *current-srl*)
650 (only-exact-if-match t))
651 (find-uobj-multiword str #'find-ucon-word #'sort-score-pfstr-str
652 #'cui srl only-exact-if-match))
654 (defun find-uterm-multiword (str &key (srl *current-srl*)
655 (only-exact-if-match t))
656 (find-uobj-multiword str #'find-uterm-word #'sort-score-pfstr-str
657 #'lui srl only-exact-if-match))
659 (defun find-ustr-multiword (str &key (srl *current-srl*)
660 (only-exact-if-match t))
661 (find-uobj-multiword str #'find-ustr-word #'sort-score-ustr-str
662 #'sui srl only-exact-if-match))
664 (defun sort-score-pfstr-str (str uobjs)
665 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
666 (sort-score-umlsclass-str uobjs str #'pfstr))
668 (defun sort-score-ustr-str (str ustrs)
669 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
670 (sort-score-umlsclass-str ustrs str #'str))
672 (defun sort-score-umlsclass-str (objs str lookup-func)
673 "Sort a list of objects based on scoring to a string"
676 (push (list obj (score-multiword-match str (funcall lookup-func obj)))
678 (mapcar #'car (sort scored #'> :key #'cadr))))
681 ;;; LEX SQL functions
683 (defun find-lexterm-eui (eui)
684 (ensure-eui-integer eui)
685 (collect-umlisp-query (lrwd (wrd) nil eui eui :single t)
686 (make-instance 'lexterm :eui eui :wrd wrd)))
688 (defun find-lexterm-word (wrd)
689 (collect-umlisp-query (lrwd (eui) nil wrd wrd)
690 (make-instance 'lexterm :eui (ensure-integer eui)
691 :wrd (copy-seq wrd))))
693 ;; LEX SQL Read functions
695 (defun find-labr-eui (eui)
696 (ensure-eui-integer eui)
697 (collect-umlisp-query (lrabr (bas abr eui2 bas2) nil eui eui)
698 (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2
699 :eui2 (ensure-integer eui2))))
701 (defun find-labr-bas (bas)
702 (collect-umlisp-query (labr (eui abr eui2 bas2) nil bas bas)
703 (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2
704 :bas (copy-seq bas) :eui2 (ensure-integer eui2))))
706 (defun find-lagr-eui (eui)
707 (ensure-eui-integer eui)
708 (collect-umlisp-query (lragr (str sca agr cit bas) nil eui eui)
709 (make-instance 'lagr :eui eui :str str :sca sca :agr agr
712 (defun find-lcmp-eui (eui)
713 (ensure-eui-integer eui)
714 (collect-umlisp-query (lrcmp (bas sca com) nil eui eui)
715 (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)))
717 (defun find-lmod-eui (eui)
718 (ensure-eui-integer eui)
719 (collect-umlisp-query (lrmod (bas sca psn_mod fea) nil eui eui)
720 (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psn_mod :fea fea)))
722 (defun find-lnom-eui (eui)
723 (ensure-eui-integer eui)
724 (collect-umlisp-query (lrnom (bas sca eui2 bas2 sca2) nil eui eui)
725 (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2
726 :eui2 (ensure-integer eui2))))
728 (defun find-lprn-eui (eui)
729 (ensure-eui-integer eui)
730 (collect-umlisp-query (lrprn (bas num gnd cas pos qnt fea) nil eui eui)
731 (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd
732 :cas cas :pos pos :qnt qnt :fea fea)))
734 (defun find-lprp-eui (eui)
735 (ensure-eui-integer eui)
736 (collect-umlisp-query (lrprp (bas str sca fea) nil eui eui)
737 (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)))
739 (defun find-lspl-eui (eui)
740 (ensure-eui-integer eui)
741 (collect-umlisp-query (lrspl (spv bas) nil eui eui)
742 (make-instance 'lspl :eui eui :spv spv :bas bas)))
744 (defun find-ltrm-eui (eui)
745 (ensure-eui-integer eui)
746 (collect-umlisp-query (lrtrm (bas gen) nil eui eui)
747 (make-instance 'ltrm :eui eui :bas bas :gen gen)))
749 (defun find-ltyp-eui (eui)
750 (ensure-eui-integer eui)
751 (collect-umlisp-query (lrtyp (bas sca typ) nil eui eui)
752 (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)))
754 (defun find-lwd-wrd (wrd)
755 (make-instance 'lwd :wrd wrd
756 :euilist (collect-umlisp-query (lrwd (eui) nil wrd wrd)
757 (ensure-integer eui))))
759 ;;; Semantic Network SQL access functions
761 (defun find-sdef-ui (ui)
762 (collect-umlisp-query (srdef (rt sty_rl stn_rtn def ex un rh abr rin)
764 (make-instance 'sdef :rt rt :ui ui :styrl sty_rl :stnrtn stn_rtn
765 :def def :ex ex :un un :rh rh :abr abr :rin rin)))
767 (defun find-sstre1-ui (ui)
768 (collect-umlisp-query (srstre1 (ui2 ui3) nil ui ui)
769 (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2)
770 :ui3 (ensure-integer ui3))))
772 (defun find-sstre1-ui2 (ui2)
773 (collect-umlisp-query (srstre1 (ui ui3) nil ui2 ui2)
774 (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2
775 :ui3 (ensure-integer ui3))))
777 (defun find-sstr-rl (rl)
778 (collect-umlisp-query (srstre (sty_rl sty_rl2 ls) nil rl rl)
779 (make-instance 'sstr :rl rl :styrl sty_rl :styrl2 sty_rl2 :ls ls)))
781 (defun find-sstre2-sty (sty)
782 (collect-umlisp-query (srstre2 (rl sty2) nil sty sty)
783 (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)))
785 (defun find-sstr-styrl (styrl)
786 (collect-umlisp-query (srstr (rl sty_rl2 ls) nil styrl styrl)
787 (make-instance 'sstr :styrl styrl :rl rl :styrl2 sty_rl2 :ls ls)))
790 ;;; **************************
792 ;;; **************************
795 (defun make-ustats ()
796 (with-sql-connection (conn)
797 (ignore-errors (sql-execute "drop table USTATS" conn))
798 (sql-execute "create table USTATS (NAME varchar(160), COUNT bigint, SRL integer)" conn)
801 (insert-ustats-count conn "Concept Count" "MRCON" "distinct CUI" "KCUILRL" srl)
802 (insert-ustats-count conn "Term Count" "MRCON" "distinct KCUILUI" "KCUILRL" srl)
803 (insert-ustats-count conn "Distinct Term Count" "MRCON" "distinct LUI" "KLUILRL" srl)
804 (insert-ustats-count conn "String Count" "MRCON" "*" "LRL" srl)
805 (insert-ustats-count conn "Distinct String Count" "MRCON" "distinct SUI" "LRL" srl)
806 (insert-ustats-count conn "Associated Expression Count" "MRATX" "*" "KSRL" srl)
807 (insert-ustats-count conn "Context Count" "MRCXT" "*" "KSRL" srl)
808 (insert-ustats-count conn "Co-occuring Concept Count" "MRCOC" "*" "KLRL" srl)
809 (insert-ustats-count conn "Definition Count" "MRDEF" "*" "KSRL" srl)
810 (insert-ustats-count conn "Locator Count" "MRLO" "*" "KLRL" srl)
811 (insert-ustats-count conn "Rank Count" "MRRANK" "*" "KSRL" srl)
812 (insert-ustats-count conn "Relationship Count" "MRREL" "*" "KSRL" srl)
813 (insert-ustats-count conn "Semantic Type Count" "MRSTY" "*" "KLRL" srl)
814 (insert-ustats-count conn "Simple Attribute Count" "MRSAT" "*" "KSRL" srl)
815 (insert-ustats-count conn "Source Count" "MRSO" "*" "SRL" srl)
816 (insert-ustats-count conn "Word Index Count" "MRXW_ENG" "*" "KLRL" srl)
817 (insert-ustats-count conn "Normalized Word Index Count" "MRXNW_ENG" "*" "KLRL" srl)
818 (insert-ustats-count conn "Normalized String Index Count" "MRXNS_ENG" "*" "KLRL" srl)
819 (insert-ustats-count conn "Bonus Attribute Name Count" "BONUS_ATN" "*" nil srl)
820 (insert-ustats-count conn "Bonus Relationship Count" "BONUS_REL" "*" nil srl)
821 (insert-ustats-count conn "Bonus Source Abbreviation Count" "BONUS_SAB" "*" nil srl)
822 (insert-ustats-count conn "Bonus Term Type Count" "BONUS_TTY" "*" nil srl))
823 (sql-execute "create index USTATS_SRL on USTATS (SRL)" conn))
826 (defun insert-ustats-count (conn name table count-variable srl-control srl)
827 (insert-ustats conn name (find-count-table conn table srl count-variable srl-control) srl))
829 (defun find-count-table (conn table srl count-variable srl-control)
831 ((stringp srl-control)
833 (caar (sql-query (format nil "select count(~a) from ~a where ~a <= ~d"
834 count-variable table srl-control srl)
838 (caar (sql-query (format nil "select count(~a) from ~a"
839 count-variable table )
842 (error "Unknown srl-control")
845 (defun insert-ustats (conn name count srl)
846 (sql-execute (format nil "insert into USTATS (name,count,srl) values ('~a',~d,~d)"
847 name count (if srl srl 3))
850 (defun find-ustats-all (&key (srl *current-srl*))
852 (collect-umlisp-query (ustats (name count srl) nil srl srl
854 (make-instance 'ustats :name name
855 :hits (ensure-integer count)
856 :srl (ensure-integer srl)))
857 (collect-umlisp-query (ustats (name count srl) nil nil nil
859 (make-instance 'ustats :name name
860 :hits (ensure-integer count)
861 :srl (ensure-integer srl)))))
863 (defun find-ustats-srl (srl)
864 (collect-umlisp-query (ustats (name count) nil srl srl :order (name asc))
865 (make-instance 'ustats :name name :hits (ensure-integer count))))
869 (defun find-bsab-sab (sab)
870 (collect-umlisp-query (bonus_sab (name count) nil sab sab :single t)
871 (make-instance 'bsab :sab sab :name name :hits (ensure-integer count))))
873 (defun find-bsab-all ()
874 (collect-umlisp-query (bonus_sab (sab name count) nil nil nil :order (sab asc))
875 (make-instance 'bsab :sab sab :name name :hits (ensure-integer count))))
877 (defun find-btty-tty (tty)
878 (collect-umlisp-query (bonus_tty (name count) nil tty tty :single t)
879 (make-instance 'btty :tty tty :name name :hits (ensure-integer count))))
881 (defun find-btty-all ()
882 (collect-umlisp-query (bonus_tty (tty name count) nil nil nil :order (tty asc))
883 (make-instance 'btty :tty tty :name name :hits (ensure-integer count))))
885 (defun find-brel-rel (rel)
886 (collect-umlisp-query (bonus_rel (sab sl rel rela count) nil rel rel)
887 (make-instance 'brel :sab sab :sl sl :rel rel :rela rela
888 :hits (ensure-integer count))))