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
8 ;;;; Date Started: Apr 2000
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2003 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 ;;;; *************************************************************************
22 ;;; Semantic type constants
24 (defun find-tui-word (words)
25 (aif (car (find-usty-word words))
28 (memoize 'find-tui-word)
30 (defun tui-disease-or-syndrome ()
31 (find-tui-word "disease or syndrome"))
32 (defun tui-sign-or-symptom ()
33 (find-tui-word "sign or symptom"))
35 (find-tui-word "finding"))
38 ;;;; Related concepts with specific tui lookup functions
40 (defun ucon-is-tui? (ucon tui)
41 "Returns t if ucon has a semantic type of tui"
42 (find tui (s#sty ucon) :key #'tui))
44 (defun find-ucon2-tui (ucon tui cui2-func related-con-func)
45 "Returns a list of related ucons that have specific tui"
49 (aif (funcall cui2-func c)
50 (let ((ucon2 (find-ucon-cui it)))
51 (when (ucon-is-tui? ucon2 tui)
53 (funcall related-con-func ucon))
56 (defun find-ucon2-coc-tui (ucon tui)
57 "Return list of ucon's that have co-occuring concepts of semantic type tui"
58 (find-ucon2-tui ucon tui #'cui2 #'s#coc))
60 (defun find-ucon2-rel-tui (ucon tui)
61 "Return list of ucon's that have related concepts to ucon and semantic type tui"
62 (find-ucon2-tui ucon tui #'cui2 #'s#rel))
66 (defclass freq (hyperobject)
67 ((freq :value-type integer :initarg :freq :accessor freq
68 :print-formatter fmt-comma-integer))
69 (:metaclass hyperobject-class)
70 (:default-initargs :freq 0)
71 (:user-name "Frequency class" "Frequency classes")
72 (:default-print-slots freq)
73 (:description "Base class containing frequency slot, used for multi-inherited objects"))
75 (defclass ucon_freq (ucon freq)
77 (:metaclass hyperobject-class)
78 (:user-name "Concept and Count" "Concepts and Counts")
79 (:default-print-slots cui freq pfstr)
80 (:description "Composite object of ucon/freq"))
82 (defclass ustr_freq (ustr freq)
84 (:metaclass hyperobject-class)
85 (:user-name "String and Count" "Strings and Counts")
86 (:default-print-slots sui freq stt lrl str)
87 (:description "Composite object of ustr/freq"))
89 (defclass usty_freq (usty freq)
91 (:metaclass hyperobject-class)
92 (:user-name "Semantic Type and Count" "Semantic Types and Counts")
93 (:default-print-slots tui freq sty)
94 (:description "Composite object of usty/freq"))
96 (defun find-usty_freq-all ()
97 (let ((usty_freqs '()))
98 (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
99 (let* ((tui (car tuple))
100 (freq (ensure-integer
101 (caar (mutex-sql-query
102 (format nil "select count(*) from MRSTY where TUI=~a" tui)))))
103 (usty (find-usty-tui tui)))
104 (push (make-instance 'usty_freq :sty (sty usty)
105 :tui (tui usty) :freq freq) usty_freqs)))
106 (sort usty_freqs #'> :key #'freq)))
109 (defclass usrl_freq (usrl freq)
111 (:metaclass hyperobject-class)
112 (:user-name "Source and Count" "Sources and Counts")
113 (:default-print-slots sab freq srl)
114 (:description "Composite object of usrl/freq"))
116 ;; Frequency finding functions
118 (defun find-usrl_freq-all ()
120 (dolist (usrl (find-usrl-all))
121 (let ((freq (ensure-integer
122 (caar (mutex-sql-query
123 (format nil "select count(*) from MRSO where SAB='~a'"
125 (push (make-instance 'usrl_freq :sab (sab usrl) :srl (srl usrl)
128 (sort freqs #'> :key #'freq)))
130 (defun find-ucon2_freq-coc-tui (ucon tui)
131 "Return sorted list of tuples with ucon and freq that have co-occuring concepts of semantic type tui"
132 (let ((ucon_freqs '()))
133 (dolist (ucoc (s#coc ucon))
135 (let ((ucon2 (find-ucon-cui it)))
136 (when (ucon-is-tui? ucon2 tui)
137 (push (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
138 :pfstr (pfstr ucon2) :freq (cof ucoc))
140 (setq ucon_freqs (delete-duplicates ucon_freqs :key #'cui))
141 (sort ucon_freqs #'> :key #'freq)))
143 (defun find-ucon2-str&sty (str sty lookup-func)
144 "Call lookup-func for ucon and usty for given str and sty"
145 (let ((ucon (car (find-ucon-str str)))
146 (usty (car (find-usty-word sty))))
148 (funcall lookup-func ucon (tui usty))
151 (defun find-ucon2-coc-str&sty (str sty)
152 "Find all ucons that are a co-occuring concept for concept named str
153 and that have semantic type of sty"
154 (find-ucon2-str&sty str sty #'find-ucon2-coc-tui))
156 (defun find-ucon2-rel-str&sty (str sty)
157 "Find all ucons that are a relationship to concept named str
158 and that have semantic type of sty"
159 (find-ucon2-str&sty str sty #'find-ucon2-rel-tui))
161 ;;; Most common relationships, co-occurances
163 (defun find-ucon2_freq-tui-all (tui ucon2-tui-func)
164 "Return sorted list of all ucon2 that have a semantic type tui with ucon that is also has sty of tui"
165 (let ((ucon_freqs (make-array (1+ (find-cui-max)) :initial-element nil)))
166 (dolist (ucon (find-ucon-tui tui)) ;; for all disease-or-syn
167 (dolist (ucon2 (funcall ucon2-tui-func ucon tui)) ;; for each related disease
168 (aif (aref ucon_freqs (cui ucon2))
169 (setf (freq it) (1+ (freq it)))
170 (setf (aref ucon_freqs (cui ucon2))
171 (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
172 :pfstr (pfstr ucon2) :freq 1)))))
173 (let ((ucon_freq-list '()))
174 (dotimes (i (find-cui-max))
176 (awhen (aref ucon_freqs i)
177 (push it ucon_freq-list)))
178 (sort ucon_freq-list #'> :key #'freq))))
180 (defun find-ucon2_freq-rel-tui-all (tui)
181 "Sorted list of ucon_freq with semantic type tui that are rel's of ucons with semantic type tui"
182 (find-ucon2_freq-tui-all tui #'find-ucon2-rel-tui))
184 (defun find-ucon2_freq-coc-tui-all (tui)
185 (find-ucon2_freq-tui-all tui #'find-ucon2-coc-tui))
188 (dolist (c '(ucon_freq ustr_freq usty_freq usrl_freq))
189 (let ((cl #+cmu (pcl:find-class c)
190 #+scl (find-class c)))
191 #+cmu (pcl:finalize-inheritance cl)
192 #+scl (clos:finalize-inheritance cl)))