r4942: Auto commit for Debian build
[umlisp.git] / sql-classes.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          sql-classes.lisp
6 ;;;; Purpose:       Routines for reading UMLS objects from SQL database
7 ;;;; Author:        Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: sql-classes.lisp,v 1.79 2003/05/15 05:16:44 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UMLisp, is
13 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
14 ;;;;
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 ;;;; *************************************************************************
18
19 (in-package :umlisp)
20 (declaim (optimize (compilation-speed 0) (debug 3)))
21
22
23 (defvar *current-srl* nil)
24 (defun current-srl ()
25   *current-srl*)
26 (defun current-srl! (srl)
27   (setq *current-srl* srl))
28
29 (defmacro query-string (table fields &optional srl where-name where-value
30                         &key (lrl "KCUILRL") single distinct order like)
31   (let* ((%%fields (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
32                            (if distinct "distinct " "") fields table))
33          (%%order (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}"
34                                     order)
35                       ""))
36          (%%lrl (format nil " and ~:@(~A~)<=" lrl))
37          (%%where (when where-name
38                     (format nil " where ~:@(~A~)~A" where-name
39                           (if like " like " "")))))
40     `(concatenate
41       'string
42       ,%%fields
43       ,@(when %%where (list %%where))
44       ,@(when %%where
45               `((typecase ,where-value
46                   (fixnum
47                    (prefixed-fixnum-string ,where-value #\= 10))
48                   (number
49                    (concatenate 'string "=" (write-to-string ,where-value)))
50                   (null
51                    " is null")
52                   (t
53                    (format nil ,(if like "'%~A%'" "='~A'") ,where-value)))))
54       (if ,srl (concatenate 'string ,%%lrl (write-to-string ,srl)) "")
55       ,@(when %%order (list %%order))
56       ,@(when single (list " limit 1")))))
57
58 (defun query-string-eval (table fields &optional srl where-name where-value
59                           &key (lrl "KCUILRL") single distinct order like)
60   (concatenate
61    'string
62    (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)" 
63            (if distinct "distinct " "") fields table)
64    (if where-name (format nil " where ~:@(~A~)" where-name) "")
65    (if where-name
66        (format nil
67                (typecase where-value
68                  (number "=~D")
69                  (null " is null")
70                  (t
71                   (if like " like '%~A%""='~A'")))
72                where-value)
73        "")
74    (if srl (format nil " and ~:@(~A~)<=~D" lrl srl) "")
75    (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) "")
76    (if single " limit 1" "")))
77
78
79 (defmacro umlisp-query (table fields srl where-name where-value
80                      &key (lrl "KCUILRL") single distinct order like)
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"
83   `(mutex-sql-query
84     (query-string ,table ,fields ,srl ,where-name ,where-value 
85      :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
86
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"
91   `(mutex-sql-query
92     (query-string-eval ,table ,fields ,srl ,where-name ,where-value 
93      :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
94
95 ;; only WHERE-VALUE and SRL are evaluated
96 (defmacro with-umlisp-query ((table fields srl where-name where-value
97                                     &key (lrl "KCUILRL") distinct single
98                                     order like)
99                              &body body)
100   (let ((value (gensym))
101         (r (gensym))) 
102     (if single
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
107                                           :like ,like))))
108           ,@(unless where-name `((declare (ignore ,value))))
109           (when tuple
110                 (destructuring-bind ,fields tuple
111                   ,@body)))
112         `(let ((,value ,where-value))
113            ,@(unless where-name `((declare (ignore ,value))))
114            (let ((,r '()))
115              (dolist (tuple (umlisp-query ,table ,fields ,srl ,where-name ,value
116                                           :lrl ,lrl :single ,single :distinct ,distinct
117                                           :order ,order :like ,like))
118                (push (destructuring-bind ,fields tuple ,@body) ,r))
119              (nreverse ,r))
120            #+ignore
121            (loop for tuple in
122                  (umlisp-query ,table ,fields ,srl ,where-name ,value
123                                :lrl ,lrl :single ,single :distinct ,distinct
124                                :order ,order :like ,like)
125                collect (destructuring-bind ,fields tuple ,@body))))))
126
127 (defmacro with-umlisp-query-eval ((table fields srl where-name where-value
128                                          &key (lrl "KCUILRL") distinct single
129                                          order like)
130                                   &body body)
131   (let ((value (gensym))
132         (r (gensym))
133         (eval-fields (cadr fields)))
134     (if single
135         `(let* ((,value ,where-value)
136                 (tuple (car (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
137                                                :lrl ,lrl :single ,single
138                                                :distinct ,distinct :order ,order
139                                                :like ,like))))
140           (when tuple
141             (destructuring-bind ,eval-fields tuple
142               ,@body)))
143         `(let ((,value ,where-value)
144                (,r '()))
145            (dolist (tuple (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
146                                              :lrl ,lrl :single ,single :distinct ,distinct
147                                              :order ,order :like ,like))
148              (push (destructuring-bind ,eval-fields tuple ,@body) ,r))
149            (nreverse ,r)
150            #+ignore
151            (loop for tuple in
152                  (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
153                                     :lrl ,lrl :single ,single :distinct ,distinct
154                                     :order ,order :like ,like)
155                collect (destructuring-bind ,eval-fields tuple ,@body))))))
156
157 ;;;
158 ;;; Read from SQL database
159
160 (defmacro ensure-cui-integer (cui)
161   `(if (stringp ,cui)
162     (setq ,cui (parse-cui ,cui))
163     ,cui))
164
165 (defmacro ensure-lui-integer (lui)
166   `(if (stringp ,lui)
167     (setq ,lui (parse-lui ,lui))
168     ,lui))
169
170 (defmacro ensure-sui-integer (sui)
171   `(if (stringp ,sui)
172     (setq ,sui (parse-sui ,sui))
173     ,sui))
174
175 (defmacro ensure-tui-integer (tui)
176   `(if (stringp ,tui)
177     (setq ,tui (parse-tui ,tui))
178     ,tui))
179
180 (defmacro ensure-eui-integer (eui)
181   `(if (stringp ,eui)
182     (setq ,eui (parse-eui ,eui))
183     ,eui))
184
185 (defun find-ucon-cui (cui &key (srl *current-srl*))
186   "Find ucon for a cui"
187   (ensure-cui-integer cui)
188   (with-umlisp-query (mrcon (kpfstr kcuilrl) srl cui cui :single t)
189     (make-instance 'ucon :cui cui :pfstr kpfstr
190                    :lrl (ensure-integer kcuilrl))))
191
192 (defun find-ucon-cui-sans-pfstr (cui &key (srl *current-srl*))
193   "Find ucon for a cui"
194   (ensure-cui-integer cui)
195   (with-umlisp-query (mrcon (kcuilrl) srl cui cui :single t)
196     (make-instance 'ucon :cui cui :lrl (ensure-integer kcuilrl)
197                    :pfstr nil)))
198
199 (defun find-pfstr-cui (cui &key (srl *current-srl*))
200   "Find preferred string for a cui"
201   (ensure-cui-integer cui)
202   (with-umlisp-query (mrcon (kpfstr) srl cui cui :single t)
203     kpfstr))
204
205 (defun find-ucon-lui (lui &key (srl *current-srl*))
206   "Find list of ucon for lui"
207   (ensure-lui-integer lui)
208   (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl lui lui
209                             :distinct t)
210     (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
211                    :lrl (ensure-integer kcuilrl))))
212
213 (defun find-ucon-sui (sui &key (srl *current-srl*))
214   "Find list of ucon for sui"
215   (ensure-sui-integer sui)
216   (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl sui sui :distinct t)
217     (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
218                    :lrl (ensure-integer kcuilrl))))
219
220 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
221   "Find ucon for cui/sui"
222   (ensure-cui-integer cui)
223   (ensure-sui-integer sui)
224   (when (and cui sui)
225     (with-umlisp-query (mrcon (kpfstr kcuilrl) srl kcuisui
226                               (make-cuisui cui sui))
227       (make-instance 'ucon :cui cui
228                      :pfstr kpfstr
229                      :lrl (ensure-integer kcuilrl)))))
230
231 (defun find-ucon-str (str &key (srl *current-srl*))
232   "Find ucon that are exact match for str"
233   (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl str str :distinct t)
234     (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
235                    :lrl (ensure-integer kcuilrl))))
236
237 (defun find-ucon-all (&key (srl *current-srl*))
238   "Return list of all ucon's"
239   (with-sql-connection (db)
240     (clsql:map-query 
241      'list
242      #'(lambda (cui pfstr cuilrl)
243          (make-instance 'ucon :cui (ensure-integer cui)
244                         :pfstr pfstr
245                         :lrl (ensure-integer cuilrl)))
246      (query-string mrcon (cui kpfstr kcuilrl) srl nil nil
247                    :order (cui asc) :distinct t)
248      :database db)))
249
250 (defun find-ucon-all2 (&key (srl *current-srl*))
251   "Return list of all ucon's"
252   (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
253                             :distinct t)
254     (make-instance 'ucon :cui (ensure-integer cui)
255                    :pfstr kpfstr
256                    :lrl (ensure-integer kcuilrl))))
257
258 (defun map-ucon-all (fn &key (srl *current-srl*))
259   "Map a function over all ucon's"
260   (with-sql-connection (db)
261     (clsql:map-query 
262      nil
263      #'(lambda (cui pfstr cuilrl)
264          (funcall fn
265                   (make-instance 'ucon :cui (ensure-integer cui)
266                                  :pfstr pfstr
267                                  :lrl (ensure-integer cuilrl))))
268      (query-string mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
269                    :distinct t)
270      :database db)))
271
272
273 (defun find-udef-cui (cui &key (srl *current-srl*))
274   "Return a list of udefs for cui"
275   (ensure-cui-integer cui)
276   (with-umlisp-query (mrdef (sab def) srl cui cui :lrl "KSRL")
277     (make-instance 'udef :sab sab :def def)))
278
279 (defun find-usty-cui (cui &key (srl *current-srl*))
280   "Return a list of usty for cui"
281   (ensure-cui-integer cui)
282   (with-umlisp-query (mrsty (tui sty) srl cui cui :lrl "KLRL")
283     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
284
285 (defun find-usty-word (word &key (srl *current-srl*))
286   "Return a list of usty that match word"
287   (with-umlisp-query (mrsty (tui sty) srl sty word :lrl klrl :like t
288                             :distinct t)
289     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
290
291 (defun find-urel-cui (cui &key (srl *current-srl*))
292   "Return a list of urel for cui"
293   (ensure-cui-integer cui)
294   (with-umlisp-query (mrrel (rel cui2 rela sab sl mg kpfstr2) srl cui1
295                             cui :lrl "KSRL")
296     (make-instance 'urel :cui1 cui :rel rel
297                    :cui2 (ensure-integer cui2) :rela rela :sab sab :sl sl
298                    :mg mg :pfstr2 kpfstr2)))
299
300 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
301   "Return a list of urel for cui2"
302   (ensure-cui-integer cui2)
303   (with-umlisp-query (mrrel (rel cui1 rela sab sl mg kpfstr2) srl cui2
304                             cui2 :lrl "KSRL")
305     (make-instance 'urel :cui2 cui2 :rel rel
306                    :cui1 (ensure-integer cui1) :rela rela :sab sab :sl sl
307                    :mg mg :pfstr2 kpfstr2)))
308
309 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
310   (ensure-cui-integer cui2)
311   (loop for cui in (remove-duplicates
312                     (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))
313         collect (find-ucon-cui cui :srl srl)))
314
315 (defun find-ucoc-cui (cui &key (srl *current-srl*))
316   "Return a list of ucoc for cui"
317   (ensure-cui-integer cui)
318   (with-umlisp-query (mrcoc (cui2 soc cot cof coa kpfstr2) srl cui1
319                             cui :lrl klrl :order (cof asc))
320     (setq cui2 (ensure-integer cui2))
321     (when (zerop cui2) (setq cui2 nil))
322     (make-instance 'ucoc :cui1 cui :cui2 (ensure-integer cui2)
323                    :soc soc :cot cot :cof (ensure-integer cof) :coa coa
324                    :pfstr2 kpfstr2)))
325
326 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
327   "Return a list of ucoc for cui2"
328   (ensure-cui-integer cui2)
329   (with-umlisp-query (mrcoc (cui1 soc cot cof coa kpfstr2) srl cui2
330                             cui2 :lrl klrl :order (cof asc))
331     (when (zerop cui2) (setq cui2 nil))
332     (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 cui2
333                    :soc soc :cot cot :cof (ensure-integer cof) :coa coa
334                    :pfstr2 kpfstr2)))
335
336 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
337   "List of ucon with co-occurance cui2"
338   (ensure-cui-integer cui2)
339   (mapcar 
340    #'(lambda (cui) (find-ucon-cui cui :srl srl))
341    (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
342
343 (defun find-ulo-cui (cui &key (srl *current-srl*))
344   "Return a list of ulo for cui"
345   (ensure-cui-integer cui)
346   (with-umlisp-query (mrlo (isn fr un sui sna soui) srl cui cui
347                            :lrl "KLRL")
348     (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un
349                    :sui (ensure-integer sui) :sna sna :soui soui)))
350
351 (defun find-uatx-cui (cui &key (srl *current-srl*))
352   "Return a list of uatx for cui"
353   (ensure-cui-integer cui)
354   (with-umlisp-query (mratx (sab rel atx) srl cui cui :lrl ksrl)
355     (make-instance 'uatx :sab sab :rel rel :atx atx)))
356
357
358 (defun find-uterm-cui (cui &key (srl *current-srl*))
359   "Return a list of uterm for cui"
360   (ensure-cui-integer cui)
361   (with-umlisp-query (mrcon (lui lat ts kluilrl) srl cui cui
362                             :lrl kluilrl :distinct t)
363     (make-instance 'uterm :lui (ensure-integer lui) :cui cui
364                    :lat lat :ts ts :lrl (ensure-integer kluilrl))))
365
366 (defun find-uterm-lui (lui &key (srl *current-srl*))
367   "Return a list of uterm for lui"
368   (ensure-lui-integer lui)
369   (with-umlisp-query (mrcon (cui lat ts kluilrl) srl lui lui 
370                              :lrl kluilrl :distinct t)
371     (make-instance 'uterm :cui (ensure-integer cui) :lui lui
372                    :lat lat :ts ts :lrl (ensure-integer kluilrl))))
373
374 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
375   "Return single uterm for cui/lui"
376   (ensure-cui-integer cui)
377   (ensure-lui-integer lui)
378   (with-umlisp-query (mrcon (lat ts kluilrl) srl kcuilui
379                              (make-cuilui cui lui)
380                              :lrl kluilrl :single t)
381     (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts
382                    :lrl (ensure-integer kluilrl))))
383
384 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
385   "Return a list of ustr for cui/lui"
386   (ensure-cui-integer cui)
387   (ensure-lui-integer lui)
388   (with-umlisp-query (mrcon (sui stt str lrl) srl kcuilui
389                             (make-cuilui cui lui) :lrl lrl)
390     (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui
391                    :cuisui (make-cuisui cui sui) :stt stt :str str
392                    :lrl (ensure-integer lrl))))
393
394 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
395   "Return the single ustr for cuisui"
396   (ensure-cui-integer cui)
397   (ensure-sui-integer sui)
398   (with-umlisp-query (mrcon (lui stt str lrl) srl kcuisui
399                             (make-cuisui cui sui) :lrl lrl :single t)
400     (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui)
401                    :lui (ensure-integer lui) :stt stt :str str
402                    :lrl (ensure-integer lrl))))
403
404 (defun find-ustr-sui (sui &key (srl *current-srl*))
405   "Return the list of ustr for sui"
406   (ensure-sui-integer sui)
407   (with-umlisp-query (mrcon (cui lui stt str lrl) srl sui sui
408                             :lrl lrl)
409     (make-instance 'ustr :sui sui :cui cui :stt stt :str str
410                    :cuisui (make-cuisui (ensure-integer cui) sui)
411                    :lui (ensure-integer lui) :lrl (ensure-integer lrl))))
412       
413 (defun find-ustr-sab (sab &key (srl *current-srl*))
414   "Return the list of ustr for sab"
415   (with-umlisp-query (mrso (kcuisui) srl sab sab :lrl srl)
416     (let ((cuisui (ensure-integer kcuisui)))
417       (apply #'find-ustr-cuisui 
418              (append
419               (multiple-value-list (decompose-cuisui cuisui))
420               (list :srl srl))))))
421
422 (defun find-ustr-all (&key (srl *current-srl*))
423   "Return list of all ustr's"
424     (with-sql-connection (db)
425       (clsql:map-query 
426        'list
427        #'(lambda (cui lui sui stt lrl pfstr)
428            (make-instance 'ustr :cui (ensure-integer cui)
429                           :lui (ensure-integer lui) :sui (ensure-integer sui)
430                           :stt stt :str pfstr
431                           :cuisui (make-cuisui (ensure-integer cui)
432                                                (ensure-integer sui))
433                           :lrl (ensure-integer lrl)))
434        (query-string mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl
435                      :distinct t
436                      :order (sui asc))
437        :database db)))
438
439 (defun find-string-sui (sui &key (srl *current-srl*))
440   "Return the string associated with sui"
441   (ensure-sui-integer sui)
442   (with-umlisp-query (mrcon (str) srl sui sui :lrl lrl :single t)
443     str))
444
445 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
446   (ensure-sui-integer sui)
447   (ensure-cui-integer cui)
448   (with-umlisp-query (mrso (sab code srl tty) srl kcuisui
449                            (make-cuisui cui sui) :lrl srl)
450       (make-instance 'uso :sab sab :code code :srl srl :tty tty)))
451
452 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
453   (ensure-cui-integer cui)
454   (ensure-sui-integer sui)
455   (with-umlisp-query (mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela xc)
456                             srl kcuisui (make-cuisui cui sui) :lrl ksrl)
457     (make-instance 'ucxt :sab sab :code code
458                    :cxn (ensure-integer cxn) :cxl cxl :cxs cxs :hcd hcd
459                    :rela rela :xc xc :rnk (ensure-integer rnk)
460                    :cui2 (ensure-integer cui2))))
461
462 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
463   (ensure-cui-integer cui)
464   (ensure-lui-integer lui)
465   (ensure-sui-integer sui)
466   (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
467     (cond
468       (sui (string-append ls (format nil "KCUISUI=~D" (make-cuisui cui sui))))
469       (lui (string-append ls (format nil "KCUILUI=~D and sui=0"
470                                      (make-cuilui cui lui))))
471       (t (string-append ls (format nil "cui=~D and lui=0 and sui=0" cui))))
472     (when srl
473       (string-append ls (format nil " and KSRL <= ~D" srl)))
474     (loop for tuple in (mutex-sql-query ls) collect 
475           (destructuring-bind (code atn sab atv) tuple
476             (make-instance 'usat :code code :atn atn :sab sab :atv atv)))))
477
478 (defun find-usty-tui (tui)
479   "Find usty for tui"
480   (ensure-tui-integer tui)
481   (with-umlisp-query (mrsty (sty) nil tui tui :single t)
482     (make-instance 'usty :tui tui :sty sty)))
483
484 (defun find-usty-sty (sty)
485   "Find usty for a sty"
486   (with-umlisp-query (mrsty (tui) nil sty sty :single t)
487     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
488
489 (defun find-usty-all ()
490   "Return list of usty's for all semantic types"
491   (with-umlisp-query (mrsty (tui) nil nil nil :distinct t)
492     (find-usty-tui tui)))
493
494 (defun find-usab-all ()
495   "Find usab for a key"
496   (with-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta
497                                   rmeta slc scc srl tfr cfr cxty ttyl atnl lat
498                                   cenc curver sabin) nil nil nil)
499     (make-instance 'usab :vcui (ensure-integer vcui) 
500                    :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
501                    :sf sf :sver sver :mstart mstart :mend mend :imeta imeta
502                    :rmeta rmeta :slc slc :scc scc  :srl (ensure-integer srl)
503                    :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
504                    :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
505                    :curver curver :sabin sabin)))
506
507 (defun find-usab-by-key (key-name key)
508   "Find usab for a key"
509   (with-umlisp-query-eval ('mrsab '(vcui rcui vsab rsab son sf sver mstart
510                                     mend imeta rmeta slc scc srl tfr cfr cxty
511                                     ttyl atnl lat cenc curver sabin)
512                                   nil key-name key :single t)
513     (make-instance 'usab :vcui (ensure-integer vcui) 
514                    :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
515                    :sf sf :sver sver :mstart mstart :mend mend :imeta imeta
516                    :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
517                    :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
518                    :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
519                    :curver curver :sabin sabin)))
520
521 (defun find-usab-rsab (rsab)
522   "Find usab for rsab"
523   (find-usab-by-key 'rsab rsab))
524
525 (defun find-usab-vsab (vsab)
526   "Find usab for vsab"
527   (find-usab-by-key 'vsab vsab))
528
529 (defun find-cui-max ()
530   (ensure-integer (caar (mutex-sql-query "select max(CUI) from MRCON"))))
531
532 ;;;; Cross table find functions
533
534 (defun find-ucon-tui (tui &key (srl *current-srl*))
535   "Find list of ucon for tui"
536   (ensure-tui-integer tui)
537   (with-umlisp-query (mrsty (cui) srl tui tui :lrl klrl :order (cui asc))
538     (find-ucon-cui (ensure-integer cui) :srl srl)))
539   
540 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
541   "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
542   (with-umlisp-query-eval ('mrxw_eng '(cui) srl 'wd word :like like :distinct t
543                                      :lrl 'klrl :order '(cui asc))
544     (find-ucon-cui cui :srl srl)))
545
546 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
547   "Return list of ucons that match word, optionally use SQL's LIKE syntax"
548   (with-umlisp-query-eval ('mrxnw_eng '(cui) srl 'nwd word :like like :distinct t
549                                       :lrl 'klrl :order '(cui asc))
550     (find-ucon-cui cui :srl srl)))
551
552 (defun find-ustr-word (word &key (srl *current-srl*))
553   "Return list of ustrs that match word"
554   (with-umlisp-query (mrxw_eng (cui sui) srl wd word :lrl klrl
555                                :order (cui asc sui asc))
556     (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
557
558 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
559   "Return list of ustrs that match word"
560   (with-umlisp-query (mrxnw_eng (cui sui) srl nwd word :lrl klrl
561                                  :order (cui asc sui asc))
562     (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
563
564 (defun find-ucon-noneng-word (word &key (srl *current-srl*) (like nil))
565   "Return list of ucons that match non-english word"
566   (with-umlisp-query-eval ('mrxw_noneng '(cui) srl 'wd word :like like
567                                         :distinct t :lrl 'klrl :order '(cui asc))
568     (find-ucon-cui cui :srl srl)))
569
570 (defun find-ustr-noneng-word (word &key (srl *current-srl*))
571   "Return list of ustrs that match non-english word"
572   (with-umlisp-query (mrxw_noneng (cui sui) srl wd word :lrl klrl
573                                   :order (cui asc sui asc))
574     (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
575
576 ;; Special tables
577
578 (defun find-usrl-all ()
579   (with-umlisp-query (usrl (sab srl) nil nil nil :order (sab asc))
580     (make-instance 'usrl :sab sab :srl (ensure-integer srl))))
581
582 ;;; Multiword lookup and score functions
583
584 (defun find-ucon-multiword (str &key (srl *current-srl*))
585   "Return sorted list of ucon's that match a multiword string"
586   (let* ((words (delimited-string-to-list str #\space))
587          (ucons '()))
588     (dolist (word words)
589       (setq ucons (append ucons (find-ucon-word word :srl srl))))
590     (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
591
592 (defun find-ustr-multiword (str &key (srl *current-srl*))
593   "Return sorted list of ustr's that match a multiword string"
594   (let* ((words (delimited-string-to-list str #\space))
595          (ustrs '()))
596     (dolist (word words)
597       (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
598     (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
599         
600 (defun sort-score-ucon-str (str ucons)
601   "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
602   (sort-score-umlsclass-str ucons str #'pfstr))
603
604 (defun sort-score-ustr-str (str ustrs)
605   "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
606   (sort-score-umlsclass-str ustrs str #'str))
607
608 (defun sort-score-umlsclass-str (objs str lookup-func)
609   "Sort a list of objects based on scoring to a string"
610   (let ((scored '()))
611     (dolist (obj objs)
612       (push (list obj (score-multiword-match str (funcall lookup-func obj))) 
613        scored))
614     (mapcar #'car (sort scored #'> :key #'cadr))))
615
616 (defun score-multiword-match (s1 s2)
617   "Score a match between two strings with s1 being reference string"
618   (let* ((word-list-1 (delimited-string-to-list s1 #\space))
619          (word-list-2 (delimited-string-to-list s2 #\space))
620          (n1 (length word-list-1))
621          (n2 (length word-list-2))
622          (unmatched n1)
623          (score 0)
624          (nlong 0)
625          (nshort 0)
626          short-list long-list)
627     (declare (fixnum n1 n2 nshort nlong score unmatched))
628     (if (> n1 n2)
629         (progn
630           (setq nlong n1)
631           (setq nshort n2)
632           (setq long-list word-list-1)
633           (setq short-list word-list-2))
634       (progn
635         (setq nlong n2)
636         (setq nshort n1)
637         (setq long-list word-list-2)
638         (setq short-list word-list-1)))
639     (decf score (- nlong nshort)) ;; reduce score for extra words
640     (dotimes (iword nshort)
641       (declare (fixnum iword))
642       (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
643            (progn
644              (incf score (- 10 (abs (- kmrcl::it iword))))
645              (decf unmatched))))
646     (decf score (* 2 unmatched))
647     score))
648
649
650 ;;; LEX SQL functions
651
652 (defun find-lexterm-eui (eui)
653   (ensure-eui-integer eui)
654   (with-umlisp-query (lrwd (wrd) nil eui eui :single t)
655     (make-instance 'lexterm :eui eui :wrd wrd)))
656
657 (defun find-lexterm-word (wrd)
658   (with-umlisp-query (lrwd (eui) nil wrd wrd)
659     (make-instance 'lexterm :eui (ensure-integer eui)
660                    :wrd (copy-seq wrd))))
661
662 ;; LEX SQL Read functions
663
664 (defun find-labr-eui (eui)
665   (ensure-eui-integer eui)
666   (with-umlisp-query (lrabr (bas abr eui2 bas2) nil eui eui) 
667     (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2
668                    :eui2 (ensure-integer eui2))))
669
670 (defun find-labr-bas (bas)
671   (with-umlisp-query (labr (eui abr eui2 bas2) nil bas bas)
672     (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2
673                    :bas (copy-seq bas) :eui2 (ensure-integer eui2))))
674
675 (defun find-lagr-eui (eui)
676   (ensure-eui-integer eui)
677   (with-umlisp-query (lragr (str sca agr cit bas) nil eui eui)
678     (make-instance 'lagr :eui eui :str str :sca sca :agr agr
679                    :cit cit :bas bas)))
680
681 (defun find-lcmp-eui (eui)
682   (ensure-eui-integer eui)
683   (with-umlisp-query (lrcmp (bas sca com) nil eui eui)
684     (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)))
685
686 (defun find-lmod-eui (eui)
687   (ensure-eui-integer eui)
688   (with-umlisp-query (lrmod (bas sca psn_mod fea) nil eui eui)
689     (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psn_mod :fea fea)))
690
691 (defun find-lnom-eui (eui)
692   (ensure-eui-integer eui)
693   (with-umlisp-query (lrnom (bas sca eui2 bas2 sca2) nil eui eui)
694     (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2
695                    :eui2 (ensure-integer eui2))))
696
697 (defun find-lprn-eui (eui)
698   (ensure-eui-integer eui)
699   (with-umlisp-query (lrprn (bas num gnd cas pos qnt fea) nil eui eui)
700     (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd
701                    :cas cas :pos pos :qnt qnt :fea fea)))
702
703 (defun find-lprp-eui (eui)
704   (ensure-eui-integer eui)
705   (with-umlisp-query (lrprp (bas str sca fea) nil eui eui)
706     (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)))
707
708 (defun find-lspl-eui (eui)
709   (ensure-eui-integer eui)
710   (with-umlisp-query (lrspl (spv bas) nil eui eui)
711     (make-instance 'lspl :eui eui :spv spv :bas bas)))
712
713 (defun find-ltrm-eui (eui)
714   (ensure-eui-integer eui)
715   (with-umlisp-query (lrtrm (bas gen) nil eui eui) 
716     (make-instance 'ltrm :eui eui :bas bas :gen gen)))
717
718 (defun find-ltyp-eui (eui)
719   (ensure-eui-integer eui)
720   (with-umlisp-query (lrtyp (bas sca typ) nil eui eui)
721     (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)))
722
723 (defun find-lwd-wrd (wrd)
724   (make-instance 'lwd :wrd
725                  :euilist (with-umlisp-query (lrwd (eui) nil wrd wrd)
726                             (ensure-integer eui))))
727
728 ;;; Semantic Network SQL access functions
729
730 (defun find-sdef-ui (ui)
731   (with-umlisp-query (srdef (rt sty_rl stn_rtn def ex un rh abr rin)
732                             nil ui ui :single t)
733     (make-instance 'sdef :rt rt :ui ui :styrl sty_rl :stnrtn stn_rtn
734                    :def def :ex ex :un un :rh rh :abr abr :rin rin)))
735
736 (defun find-sstre1-ui (ui)
737   (with-umlisp-query (srstre1 (ui2 ui3) nil ui ui)
738     (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2)
739                    :ui3 (ensure-integer ui3))))
740
741 (defun find-sstre1-ui2 (ui2)
742   (with-umlisp-query (srstre1 (ui ui3) nil ui2 ui2)
743     (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2
744                    :ui3 (ensure-integer ui3))))
745
746 (defun find-sstr-rl (rl)
747   (with-umlisp-query (srstre (sty_rl sty_rl2 ls) nil rl rl)
748     (make-instance 'sstr :rl rl :styrl sty_rl :styrl2 sty_rl2 :ls ls)))
749
750 (defun find-sstre2-sty (sty)
751   (with-umlisp-query (srstre2 (rl sty2) nil sty sty)
752     (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)))
753
754 (defun find-sstr-styrl (styrl)
755   (with-umlisp-query (srstr (rl sty_rl2 ls) nil styrl styrl)
756     (make-instance 'sstr :styrl styrl :rl rl :styrl2 sty_rl2 :ls ls)))