r4924: *** 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 ;;;; Author:        Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: composite.lisp,v 1.22 2003/05/14 05:37:48 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
21 (eval-when (:compile-toplevel)
22   (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
23
24
25 ;;; Semantic type constants
26
27 (defun find-tui-word (words)
28   (aif (car (find-usty-word words))
29        (tui it)
30        nil))
31 (memoize 'find-tui-word)
32
33 (defun tui-disease-or-syndrome ()
34   (find-tui-word "disease or syndrome"))
35 (defun tui-sign-or-symptom () 
36   (find-tui-word "sign or symptom"))
37 (defun tui-finding ()
38   (find-tui-word "finding"))
39
40
41 ;;;; Related concepts with specific tui lookup functions
42
43 (defun ucon-is-tui? (ucon tui)
44   "Returns t if ucon has a semantic type of tui"
45   (find tui (s#sty ucon) :key #'tui))
46
47 (defun find-ucon2-tui (ucon tui cui2-func related-con-func)
48   "Returns a list of related ucons that have specific tui"
49   (remove-duplicates 
50    (filter
51     #'(lambda (c) 
52         (aif (funcall cui2-func c)
53              (let ((ucon2 (find-ucon-cui it)))
54                (when (ucon-is-tui? ucon2 tui)
55                  ucon2)) nil))
56     (funcall related-con-func ucon))
57    :key #'cui))
58
59 (defun find-ucon2-coc-tui (ucon tui)
60   "Return list of ucon's that have co-occuring concepts of semantic type tui"
61   (find-ucon2-tui ucon tui #'cui2 #'s#coc))
62   
63 (defun find-ucon2-rel-tui (ucon tui)
64   "Return list of ucon's that have related concepts to ucon and semantic type tui"
65   (find-ucon2-tui ucon tui #'cui2 #'s#rel))
66
67 ;;; Composite Objects
68
69 (defclass freq (hyperobject)
70   ((freq :value-type integer :initarg :freq :accessor freq :print-formatter fmt-comma-integer))
71   (:metaclass hyperobject-class)
72   (:default-initargs :freq 0)
73   (:user-name "Frequency class" "Frequency classes")
74   (:default-print-slots freq)
75   (:description "Base class containing frequency slot, used for multi-inherited objects"))
76
77 (defclass ucon_freq (ucon freq)
78   ()
79   (:metaclass hyperobject-class)
80   (:user-name "Concept and Count" "Concepts and Counts")
81   (:default-print-slots cui freq pfstr)
82   (:description "Composite object of ucon/freq"))
83
84 (defclass ustr_freq (ustr freq)
85   ()
86   (:metaclass hyperobject-class)
87   (:user-name "String and Count" "Strings and Counts")
88   (:default-print-slots sui freq stt lrl str)
89   (:description "Composite object of ustr/freq"))
90
91 (defclass usty_freq (usty freq)
92   ((freq :value-type fixnum :initarg :freq :accessor freq))
93   (:metaclass hyperobject-class)
94   (:user-name "Semantic Type and Count" "Semantic Types and Counts")
95   (:default-print-slots tui freq sty)
96   (:description "Composite object of usty/freq"))
97
98 (defun find-usty_freq-all ()
99   (let ((usty_freqs '()))
100     (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
101       (let* ((tui (car tuple))
102              (freq (ensure-integer 
103                      (caar (mutex-sql-query 
104                             (format nil "select count(*) from MRSTY where TUI=~a" tui)))))
105              (usty (find-usty-tui tui)))
106         (push (make-instance 'usty_freq :sty (sty usty)
107                              :tui (tui usty) :freq freq) usty_freqs)))
108     (sort usty_freqs #'> :key #'freq)))
109
110
111 (defclass usrl_freq (usrl freq)
112   ()
113   (:metaclass hyperobject-class)
114   (:user-name "Source and Count" "Sources and Counts")
115   (:default-print-slots sab freq srl)
116   (:description "Composite object of usrl/freq"))
117
118 ;; Frequency finding functions
119
120 (defun find-usrl_freq-all ()
121   (let ((freqs '()))
122     (dolist (usrl (find-usrl-all))
123       (let ((freq (ensure-integer 
124                    (caar (mutex-sql-query 
125                           (format nil "select count(*) from MRSO where SAB='~a'" 
126                                   (sab usrl)))))))
127         (push (make-instance 'usrl_freq :sab (sab usrl) :srl (srl usrl) 
128                              :freq freq) 
129               freqs)))
130     (sort freqs #'> :key #'freq)))
131
132 (defun find-ucon2_freq-coc-tui (ucon tui)
133 "Return sorted list of tuples with ucon and freq that have co-occuring concepts of semantic type tui" 
134   (let ((ucon_freqs '())) 
135     (dolist (ucoc (s#coc ucon)) 
136       (aif (cui2 ucoc) 
137            (let ((ucon2 (find-ucon-cui it))) 
138              (when (ucon-is-tui? ucon2 tui)
139                (push (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
140                                     :pfstr (pfstr ucon2) :freq (cof ucoc)) 
141                      ucon_freqs)))))
142     (setq ucon_freqs (delete-duplicates ucon_freqs :key #'cui))
143     (sort ucon_freqs #'> :key #'freq)))
144  
145 (defun find-ucon2-str&sty (str sty lookup-func)
146   "Call lookup-func for ucon and usty for given str and sty"
147   (let ((ucon (car (find-ucon-str str)))
148         (usty (car (find-usty-word sty))))
149     (if (and ucon usty)
150         (funcall lookup-func ucon (tui usty))
151       nil)))
152   
153 (defun find-ucon2-coc-str&sty (str sty)
154   "Find all ucons that are a co-occuring concept for concept named str
155    and that have semantic type of sty"
156   (find-ucon2-str&sty str sty #'find-ucon2-coc-tui))
157
158 (defun find-ucon2-rel-str&sty (str sty)
159   "Find all ucons that are a relationship to concept named str
160    and that have semantic type of sty"
161   (find-ucon2-str&sty str sty #'find-ucon2-rel-tui))
162
163 ;;; Most common relationships, co-occurances
164
165 (defun find-ucon2_freq-tui-all (tui ucon2-tui-func)
166   "Return sorted list of all ucon2 that have a semantic type tui with ucon that is also has sty of tui"
167   (let ((ucon_freqs (make-array (1+ (find-cui-max)) :initial-element nil)))
168     (dolist (ucon (find-ucon-tui tui)) ;; for all disease-or-syn
169       (dolist (ucon2 (funcall ucon2-tui-func ucon tui)) ;; for each related disease
170         (aif (aref ucon_freqs (cui ucon2))
171              (setf (freq it) (1+ (freq it)))
172              (setf (aref ucon_freqs (cui ucon2)) 
173                (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
174                               :pfstr (pfstr ucon2) :freq 1)))))
175     (let ((ucon_freq-list '()))
176       (dotimes (i (find-cui-max))
177         (declare (fixnum i))
178         (awhen (aref ucon_freqs i)
179              (push it ucon_freq-list)))
180       (sort ucon_freq-list #'> :key #'freq))))
181
182 (defun find-ucon2_freq-rel-tui-all (tui)
183   "Sorted list of ucon_freq with semantic type tui that are rel's of ucons with semantic type tui"
184   (find-ucon2_freq-tui-all tui #'find-ucon2-rel-tui))
185
186 (defun find-ucon2_freq-coc-tui-all (tui)
187   (find-ucon2_freq-tui-all tui #'find-ucon2-coc-tui))
188
189 #+(or cmu scl)
190 (dolist (c '(ucon_freq ustr_freq usty_freq usrl_freq))
191   (let ((cl #+cmu (pcl:find-class c)
192             #+scl (find-class c)))
193     #+cmu (pcl:finalize-inheritance cl)
194     #+scl (clos:finalize-inheritance cl)))