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 ;;;; Author: Kevin M. Rosenberg
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2006 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 ;;;; *************************************************************************
21 ;;; Semantic type constants
23 (defun find-tui-word (words)
24 (aif (car (find-usty-word words))
27 (memoize 'find-tui-word)
29 (defun tui-disease-or-syndrome ()
30 (find-tui-word "disease or syndrome"))
31 (defun tui-sign-or-symptom ()
32 (find-tui-word "sign or symptom"))
34 (find-tui-word "finding"))
37 ;;;; Related concepts with specific tui lookup functions
39 (defun ucon-is-tui? (ucon tui)
40 "Returns t if ucon has a semantic type of tui"
41 (find tui (s#sty ucon) :key #'tui))
43 (defun find-ucon2-tui (ucon tui cui2-func related-con-func)
44 "Returns a list of related ucons that have specific tui"
48 (aif (funcall cui2-func c)
49 (let ((ucon2 (find-ucon-cui it)))
50 (when (ucon-is-tui? ucon2 tui)
52 (funcall related-con-func ucon))
55 (defun find-ucon2-coc-tui (ucon tui)
56 "Return list of ucon's that have co-occuring concepts of semantic type tui"
57 (find-ucon2-tui ucon tui #'cui2 #'s#coc))
59 (defun find-ucon2-rel-tui (ucon tui)
60 "Return list of ucon's that have related concepts to ucon and semantic type tui"
61 (find-ucon2-tui ucon tui #'cui2 #'s#rel))
65 (defclass freq (hyperobject)
66 ((freq :value-type integer :initarg :freq :accessor freq
67 :print-formatter fmt-comma-integer))
68 (:metaclass hyperobject-class)
69 (:default-initargs :freq 0)
70 (:user-name "Frequency class" "Frequency classes")
71 (:default-print-slots freq)
72 (:description "Base class containing frequency slot, used for multi-inherited objects"))
74 (defclass ucon_freq (ucon freq)
76 (:metaclass hyperobject-class)
77 (:user-name "Concept and Count" "Concepts and Counts")
78 (:default-print-slots cui freq pfstr)
79 (:description "Composite object of ucon/freq"))
81 (defclass ustr_freq (ustr freq)
83 (:metaclass hyperobject-class)
84 (:user-name "String and Count" "Strings and Counts")
85 (:default-print-slots sui freq stt lrl str)
86 (:description "Composite object of ustr/freq"))
88 (defclass usty_freq (usty freq)
90 (:metaclass hyperobject-class)
91 (:user-name "Semantic Type and Count" "Semantic Types and Counts")
92 (:default-print-slots tui freq sty)
93 (:description "Composite object of usty/freq"))
95 (defun find-usty_freq-all ()
96 (let ((usty_freqs '()))
97 (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
98 (let* ((tui (car tuple))
100 (caar (mutex-sql-query
101 (format nil "select count(*) from MRSTY where TUI=~a" tui)))))
102 (usty (find-usty-tui tui)))
103 (push (make-instance 'usty_freq :sty (sty usty)
104 :tui (tui usty) :freq freq) usty_freqs)))
105 (sort usty_freqs #'> :key #'freq)))
108 (defclass usrl_freq (usrl freq)
110 (:metaclass hyperobject-class)
111 (:user-name "Source and Count" "Sources and Counts")
112 (:default-print-slots sab freq srl)
113 (:description "Composite object of usrl/freq"))
115 ;; Frequency finding functions
117 (defun find-usrl_freq-all ()
119 (dolist (usrl (find-usrl-all))
120 (let ((freq (ensure-integer
121 (caar (mutex-sql-query
122 (format nil "select count(*) from MRSO where SAB='~a'"
124 (push (make-instance 'usrl_freq :sab (sab usrl) :srl (srl usrl)
127 (sort freqs #'> :key #'freq)))
129 (defun find-ucon2_freq-coc-tui (ucon tui)
130 "Return sorted list of tuples with ucon and freq that have co-occuring concepts of semantic type tui"
131 (let ((ucon_freqs '()))
132 (dolist (ucoc (s#coc ucon))
134 (let ((ucon2 (find-ucon-cui it)))
135 (when (ucon-is-tui? ucon2 tui)
136 (push (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
137 :pfstr (pfstr ucon2) :freq (cof ucoc))
139 (setq ucon_freqs (delete-duplicates ucon_freqs :key #'cui))
140 (sort ucon_freqs #'> :key #'freq)))
142 (defun find-ucon2-str&sty (str sty lookup-func)
143 "Call lookup-func for ucon and usty for given str and sty"
144 (let ((ucon (car (find-ucon-str str)))
145 (usty (car (find-usty-word sty))))
147 (funcall lookup-func ucon (tui usty))
150 (defun find-ucon2-coc-str&sty (str sty)
151 "Find all ucons that are a co-occuring concept for concept named str
152 and that have semantic type of sty"
153 (find-ucon2-str&sty str sty #'find-ucon2-coc-tui))
155 (defun find-ucon2-rel-str&sty (str sty)
156 "Find all ucons that are a relationship to concept named str
157 and that have semantic type of sty"
158 (find-ucon2-str&sty str sty #'find-ucon2-rel-tui))
160 ;;; Most common relationships, co-occurances
162 (defun find-ucon2_freq-tui-all (tui ucon2-tui-func)
163 "Return sorted list of all ucon2 that have a semantic type tui with ucon that is also has sty of tui"
164 (let ((ucon_freqs (make-array (1+ (find-cui-max)) :initial-element nil)))
165 (dolist (ucon (find-ucon-tui tui)) ;; for all disease-or-syn
166 (dolist (ucon2 (funcall ucon2-tui-func ucon tui)) ;; for each related disease
167 (aif (aref ucon_freqs (cui ucon2))
168 (setf (freq it) (1+ (freq it)))
169 (setf (aref ucon_freqs (cui ucon2))
170 (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
171 :pfstr (pfstr ucon2) :freq 1)))))
172 (let ((ucon_freq-list '()))
173 (dotimes (i (find-cui-max))
175 (awhen (aref ucon_freqs i)
176 (push it ucon_freq-list)))
177 (sort ucon_freq-list #'> :key #'freq))))
179 (defun find-ucon2_freq-rel-tui-all (tui)
180 "Sorted list of ucon_freq with semantic type tui that are rel's of ucons with semantic type tui"
181 (find-ucon2_freq-tui-all tui #'find-ucon2-rel-tui))
183 (defun find-ucon2_freq-coc-tui-all (tui)
184 (find-ucon2_freq-tui-all tui #'find-ucon2-coc-tui))
187 (dolist (c '(ucon_freq ustr_freq usty_freq usrl_freq))
188 (let ((cl #+cmu (pcl:find-class c)
189 #+scl (find-class c)))
190 #+cmu (pcl:finalize-inheritance cl)
191 #+scl (clos:finalize-inheritance cl)))