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