1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: composite.lisp
6 ;;;; Purpose: Composite Classes for UMLisp
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; $Id: composite.lisp,v 1.9 2002/11/04 18:02:13 kevin Exp $
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2002 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 ;;;; *************************************************************************
20 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
23 ;;; Semantic type constants
25 (defun find-tui-word (words)
26 (kmrcl:aif (car (find-usty-word words))
29 (kmrcl:memoize 'find-tui-word)
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"))
36 (find-tui-word "finding"))
39 ;;;; Related concepts with specific tui lookup functions
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))
45 (defun find-ucon2-tui (ucon tui cui2-func related-con-func)
46 "Returns a list of related ucons that have specific tui"
50 (kmrcl:aif (funcall cui2-func c)
51 (let ((ucon2 (find-ucon-cui kmrcl::it)))
52 (when (ucon-is-tui? ucon2 tui)
55 (funcall related-con-func ucon))
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))
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))
68 (defclass ucon_freq (umlsclass)
69 ((ucon :type ucon :initarg :ucon :reader ucon)
70 (freq :type fixnum :initarg :freq :accessor freq))
71 (:metaclass hyperobject-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"))
78 (defun ucon_freq-cui (c)
81 (defun ucon_freq-pfstr (c)
84 (defclass ustr_freq (umlsclass)
85 ((ustr :type ustr :initarg :ustr :reader ustr)
86 (freq :type fixnum :initarg :freq :accessor freq))
87 (:metaclass hyperobject-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"))
94 (defun ustr_freq-sui (s)
97 (defun ustr_freq-str (s)
100 (defun ustr_freq-lrl (s)
103 (defun ustr_freq-stt (s)
106 (defclass usty_freq (umlsclass)
107 ((usty :type usty :initarg :usty :reader usty)
108 (freq :type fixnum :initarg :freq :accessor freq))
109 (:metaclass hyperobject-class)
110 (:default-initargs :usty nil :freq nil)
111 (:title "Semantic Type and Count")
112 ;; (:ref-fields (tui find-ucon-tui "subobjects=no"))
113 (:ref-fields (tui find-ucon-tui))
114 (:fields (tui :string fmt-tui) (freq :fixnum) (sty :string))
115 (:documentation "Composite object of usty/freq"))
117 (defmethod tui ((s usty_freq))
120 (defmethod sty ((s usty_freq))
124 (defun find-usty_freq-all ()
125 (let ((usty_freqs '()))
126 (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
127 (let* ((tui (car tuple))
128 (freq (ensure-integer
129 (caar (mutex-sql-query
130 (format nil "select count(*) from MRSTY where TUI=~a" tui))))))
131 (push (make-instance 'usty_freq :usty (find-usty-tui tui) :freq freq) usty_freqs)))
132 (sort usty_freqs #'> :key #'freq)))
135 (defclass usrl_freq (umlsclass)
136 ((usrl :type usrl :initarg :usrl :reader usrl)
137 (freq :type fixnum :initarg :freq :accessor freq))
138 (:metaclass hyperobject-class)
139 (:default-initargs :usrl nil :freq nil)
140 (:title "Source and Count")
141 (:ref-fields (sab find-ustr-sab))
142 (:fields (sab :string) (freq :commainteger) (srl :fixnum))
143 (:documentation "Composite object of usrl/freq"))
145 (defmethod sab ((s usrl_freq))
148 (defmethod srl ((s usrl_freq))
152 ;; Frequency finding functions
154 (defun find-usrl_freq-all ()
156 (dolist (usrl (find-usrl-all))
157 (let ((freq (ensure-integer
158 (caar (mutex-sql-query
159 (format nil "select count(*) from MRSO where SAB='~a'"
161 (push (make-instance 'usrl_freq :usrl usrl :freq freq) freqs)))
162 (sort freqs #'> :key #'usrl_freq-freq)))
164 (defun find-ucon2_freq-coc-tui (ucon tui)
165 "Return sorted list of tuples with ucon and freq that have co-occuring concepts of semantic type tui"
166 (let ((ucon_freqs '()))
167 (dolist (ucoc (s#coc ucon))
168 (kmrcl:aif (cui2 ucoc)
169 (let ((ucon2 (find-ucon-cui kmrcl::it)))
170 (when (ucon-is-tui? ucon2 tui)
171 (push (make-instance 'ucon_freq :ucon ucon2 :freq (cof ucoc))
173 (setq ucon_freqs (delete-duplicates ucon_freqs :key #'cui))
174 (sort ucon_freqs #'> :key #'freq)))
176 (defun find-ucon2-str&sty (str sty lookup-func)
177 "Call lookup-func for ucon and usty for given str and sty"
178 (let ((ucon (car (find-ucon-str str)))
179 (usty (car (find-usty-word sty))))
181 (funcall lookup-func ucon (tui usty))
184 (defun find-ucon2-coc-str&sty (str sty)
185 "Find all ucons that are a co-occuring concept for concept named str
186 and that have semantic type of sty"
187 (find-ucon2-str&sty str sty #'find-ucon2-coc-tui))
189 (defun find-ucon2-rel-str&sty (str sty)
190 "Find all ucons that are a relationship to concept named str
191 and that have semantic type of sty"
192 (find-ucon2-str&sty str sty #'find-ucon2-rel-tui))
194 ;;; Most common relationships, co-occurances
196 (defun find-ucon2_freq-tui-all (tui ucon2-tui-func)
197 "Return sorted list of all ucon2 that have a semantic type tui with ucon that is also has sty of tui"
198 (let ((ucon_freqs (make-array (1+ (find-cui-max)) :initial-element nil)))
199 (dolist (ucon (find-ucon-tui tui)) ;; for all disease-or-syn
200 (dolist (ucon2 (funcall ucon2-tui-func ucon tui)) ;; for each related disease
201 (kmrcl:aif (aref ucon_freqs (cui ucon2))
202 (setf (freq kmrcl::it) (1+ (freq kmrcl::it)))
203 (setf (aref ucon_freqs (cui ucon2))
204 (make-instance 'ucon_freq :ucon ucon2 :freq 1)))))
205 (let ((ucon_freq-list '()))
206 (dotimes (i (find-cui-max))
208 (kmrcl:awhen (aref ucon_freqs i)
209 (push kmrcl::it ucon_freq-list)))
210 (sort ucon_freq-list #'> :key #'freq))))
212 (defun find-ucon2_freq-rel-tui-all (tui)
213 "Sorted list of ucon_freq with semantic type tui that are rel's of ucons with semantic type tui"
214 (find-ucon2_freq-tui-all tui #'find-ucon2-rel-tui))
216 (defun find-ucon2_freq-coc-tui-all (tui)
217 (find-ucon2_freq-tui-all tui #'find-ucon2-coc-tui))
220 (dolist (c '(ucon_freq ustr_freq usty_freq usrl_freq))
221 (let ((cl #+cmu (pcl:find-class c)
222 #+scl (find-class c)))
223 #+cmu (pcl:finalize-inheritance cl)
224 #+scl (clos:finalize-inheritance cl)))