r3016: *** empty log message ***
[umlisp.git] / composite.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          composite.lisp
6 ;;;; Purpose:       Composite Classes for UMLisp
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: composite.lisp,v 1.4 2002/10/14 09:25:20 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 (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
21
22
23 ;;; Semantic type constants
24
25 (defun find-tui-word (words)
26   (kmrcl:aif (car (find-usty-word words))
27        (tui kmrcl::it)
28        nil))
29 (kmrcl:memoize 'find-tui-word)
30
31 (defun tui-disease-or-syndrome ()
32   (find-tui-word "disease or syndrome"))
33 (defun tui-sign-or-symptom () 
34   (find-tui-word "sign or symptom"))
35 (defun tui-finding ()
36   (find-tui-word "finding"))
37
38
39 ;;;; Related concepts with specific tui lookup functions
40
41 (defun ucon-is-tui? (ucon tui)
42   "Returns t if ucon has a semantic type of tui"
43   (find tui (s#sty ucon) :key #'tui))
44
45 (defun find-ucon2-tui (ucon tui cui2-func related-con-func)
46   "Returns a list of related ucons that have specific tui"
47   (remove-duplicates 
48    (filter
49     #'(lambda (c) 
50         (kmrcl:aif (funcall cui2-func c)
51              (let ((ucon2 (find-ucon-cui kmrcl::it)))
52                (when (ucon-is-tui? ucon2 tui)
53                  ucon2))
54              nil))
55     (funcall related-con-func ucon))
56    :key #'cui))
57
58 (defun find-ucon2-coc-tui (ucon tui)
59   "Return list of ucon's that have co-occuring concepts of semantic type tui"
60   (find-ucon2-tui ucon tui #'cui2 #'s#coc))
61   
62 (defun find-ucon2-rel-tui (ucon tui)
63   "Return list of ucon's that have related concepts to ucon and semantic type tui"
64   (find-ucon2-tui ucon tui #'cui2 #'s#rel))
65
66 ;;; Composite Objects
67
68 (defclass ucon_freq (umlsclass)
69   ((ucon :type ucon :initarg :ucon :reader ucon)
70    (freq :type fixnum :initarg :freq :accessor freq))
71   (:metaclass kmrcl:ml-class)
72   (:default-initargs :cui nil :pfstr nil :freq nil)
73   (:title "Concept and Count")
74   (:fields (cui :string fmt-cui) (freq :fixnum) (pfstr :cdata))
75   (:ref-fields (cui find-ucon-cui))
76   (:documentation "Composite object of ucon/freq"))
77
78 (defun ucon_freq-cui (c)
79   (cui (ucon c)))
80
81 (defun ucon_freq-pfstr (c)
82   (pfstr (ucon c)))
83
84 (defclass ustr_freq (umlsclass)
85   ((ustr :type ustr :initarg :ustr :reader ustr)
86    (freq :type fixnum :initarg :freq :accessor freq))
87   (:metaclass kmrcl:ml-class)
88   (:default-initargs :cui nil :pfstr nil :freq nil)
89   (:title "String and Count")
90   (:fields (sui :string fmt-sui) (freq :fixnum) (stt :string) (lrl :fixnum) (str :cdata))
91   (:ref-fields (sui find-ustr-sui))
92   (:documentation "Composite object of ustr/freq"))
93
94 (defun ustr_freq-sui (s)
95   (sui (ustr s)))
96
97 (defun ustr_freq-str (s)
98   (str (ustr s)))
99
100 (defun ustr_freq-lrl (s)
101   (lrl (ustr s)))
102
103 (defun ustr_freq-stt (s)
104   (stt (ustr s)))
105
106 (defclass usty_freq (umlsclass)
107   ((usty :type usty :initarg :usty :reader usty)
108    (freq :type fixnum :initarg :freq :accessor freq))
109   (:metaclass kmrcl:ml-class)
110   (:default-initargs :usty nil :freq nil)
111   (:title "Semantic Type and Count")
112   (:ref-fields (tui find-ucon-tui "subobjects=no"))
113   (:fields (tui :string fmt-tui) (freq :fixnum) (sty :string))
114   (:documentation "Composite object of usty/freq"))
115
116 (defun find-usty_freq-all ()
117   (let ((usty_freqs '()))
118     (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
119       (let* ((tui (car tuple))
120              (freq (ensure-integer 
121                      (caar (mutex-sql-query 
122                             (format nil "select count(*) from MRSTY where TUI=~a" tui))))))
123         (push (make-instance 'usty_freq :usty (find-usty-tui tui) :freq freq) usty_freqs)))
124     (sort usty_freqs #'> :key #'freq)))
125
126 (defun usty_freq-tui (s)
127   (tui (usty s)))
128  
129 (defun usty_freq-sty (s)
130   (sty (usty s)))
131
132 (defclass usrl_freq (umlsclass)
133   ((usrl :type usrl :initarg :usrl :reader usrl)
134    (freq :type fixnum :initarg :freq :accessor freq))
135   (:metaclass kmrcl:ml-class)
136   (:default-initargs :usrl nil :freq nil)
137   (:title "Source and Count")
138   (:ref-fields (sab find-ustr-sab))
139   (:fields (sab :string) (freq :commainteger) (srl :fixnum))
140   (:documentation "Composite object of usrl/freq"))
141
142 (defun usrl_freq-sab (s)
143   (sab (usrl s)))
144  
145 (defun usrl_freq-srl (s)
146   (srl (usrl s)))
147
148
149 ;; Frequency finding functions
150 (defun find-ucon2_freq-coc-tui (ucon tui)
151 "Return sorted list of tuples with ucon and freq that have co-occuring concepts of semantic type tui" 
152   (let ((ucon_freqs '())) 
153     (dolist (ucoc (s#coc ucon)) 
154       (kmrcl:aif (cui2 ucoc) 
155            (let ((ucon2 (find-ucon-cui kmrcl::it))) 
156              (when (ucon-is-tui? ucon2 tui)
157                (push (make-instance 'ucon_freq :ucon ucon2 :freq (cof ucoc)) 
158                      ucon_freqs)))))
159     (setq ucon_freqs (delete-duplicates ucon_freqs :key #'cui))
160     (sort ucon_freqs #'> :key #'freq)))
161  
162 (defun find-ucon2-str&sty (str sty lookup-func)
163   "Call lookup-func for ucon and usty for given str and sty"
164   (let ((ucon (car (find-ucon-str str)))
165         (usty (car (find-usty-word sty))))
166     (if (and ucon usty)
167         (funcall lookup-func ucon (tui usty))
168       nil)))
169   
170 (defun find-ucon2-coc-str&sty (str sty)
171   "Find all ucons that are a co-occuring concept for concept named str
172    and that have semantic type of sty"
173   (find-ucon2-str&sty str sty #'find-ucon2-coc-tui))
174
175 (defun find-ucon2-rel-str&sty (str sty)
176   "Find all ucons that are a relationship to concept named str
177    and that have semantic type of sty"
178   (find-ucon2-str&sty str sty #'find-ucon2-rel-tui))
179
180 ;;; Most common relationships, co-occurances
181
182 (defun find-ucon2_freq-tui-all (tui ucon2-tui-func)
183   "Return sorted list of all ucon2 that have a semantic type tui with ucon that is also has sty of tui"
184   (let ((ucon_freqs (make-array (1+ (find-cui-max)) :initial-element nil)))
185     (dolist (ucon (find-ucon-tui tui)) ;; for all disease-or-syn
186       (dolist (ucon2 (funcall ucon2-tui-func ucon tui)) ;; for each related disease
187         (kmrcl:aif (aref ucon_freqs (cui ucon2))
188              (setf (freq kmrcl::it) (1+ (freq kmrcl::it)))
189              (setf (aref ucon_freqs (cui ucon2)) 
190                (make-instance 'ucon_freq :ucon ucon2 :freq 1)))))
191     (let ((ucon_freq-list '()))
192       (dotimes (i (find-cui-max))
193         (declare (fixnum i))
194         (kmrcl:awhen (aref ucon_freqs i)
195              (push kmrcl::it ucon_freq-list)))
196       (sort ucon_freq-list #'> :key #'freq))))
197
198 (defun find-ucon2_freq-rel-tui-all (tui)
199   "Sorted list of ucon_freq with semantic type tui that are rel's of ucons with semantic type tui"
200   (find-ucon2_freq-tui-all tui #'find-ucon2-rel-tui))
201
202 (defun find-ucon2_freq-coc-tui-all (tui)
203   (find-ucon2_freq-tui-all tui #'find-ucon2-coc-tui))
204
205 (dolist (c '(ucon_freq ustr_freq usty_freq usrl_freq))
206   (let ((cl #+cmu (pcl:find-class c)
207             #+sbcl (sb-pcl:find-class c)))
208     #+cmu (pcl:finalize-inheritance cl)
209     #+sbcl (sb-pcl:finalize-inheritance cl)))