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
10 ;;;; $Id: composite.lisp,v 1.21 2003/05/06 02:14:59 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 ;;;; *************************************************************************
21 (eval-when (:compile-toplevel)
22 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
25 ;;; Semantic type constants
27 (defun find-tui-word (words)
28 (aif (car (find-usty-word words))
31 (memoize 'find-tui-word)
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"))
38 (find-tui-word "finding"))
41 ;;;; Related concepts with specific tui lookup functions
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))
47 (defun find-ucon2-tui (ucon tui cui2-func related-con-func)
48 "Returns a list of related ucons that have specific tui"
52 (aif (funcall cui2-func c)
53 (let ((ucon2 (find-ucon-cui it)))
54 (when (ucon-is-tui? ucon2 tui)
56 (funcall related-con-func ucon))
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))
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))
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")
74 (:default-print-slots freq)
75 (:description "Base class containing frequency slot, used for multi-inherited objects"))
77 (defclass ucon_freq (ucon freq)
79 (:metaclass hyperobject-class)
80 (:user-name "Concept and Count")
81 (:default-print-slots cui freq pfstr)
82 (:description "Composite object of ucon/freq"))
84 (defclass ustr_freq (ustr freq)
86 (:metaclass hyperobject-class)
87 (:user-name "String and Count")
88 (:default-print-slots sui freq stt lrl str)
89 (:description "Composite object of ustr/freq"))
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")
95 (:default-print-slots tui freq sty)
96 (:description "Composite object of usty/freq"))
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)))
111 (defclass usrl_freq (usrl freq)
113 (:metaclass hyperobject-class)
114 (:user-name "Source and Count")
115 (:default-print-slots sab freq srl)
116 (:description "Composite object of usrl/freq"))
118 ;; Frequency finding functions
120 (defun find-usrl_freq-all ()
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'"
127 (push (make-instance 'usrl_freq :sab (sab usrl) :srl (srl usrl)
130 (sort freqs #'> :key #'freq)))
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))
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))
142 (setq ucon_freqs (delete-duplicates ucon_freqs :key #'cui))
143 (sort ucon_freqs #'> :key #'freq)))
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))))
150 (funcall lookup-func ucon (tui usty))
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))
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))
163 ;;; Most common relationships, co-occurances
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))
178 (awhen (aref ucon_freqs i)
179 (push it ucon_freq-list)))
180 (sort ucon_freq-list #'> :key #'freq))))
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))
186 (defun find-ucon2_freq-coc-tui-all (tui)
187 (find-ucon2_freq-tui-all tui #'find-ucon2-coc-tui))
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)))