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.23 2003/05/27 21:43:41 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
71 :print-formatter fmt-comma-integer))
72 (:metaclass hyperobject-class)
73 (:default-initargs :freq 0)
74 (:user-name "Frequency class" "Frequency classes")
75 (:default-print-slots freq)
76 (:description "Base class containing frequency slot, used for multi-inherited objects"))
78 (defclass ucon_freq (ucon freq)
80 (:metaclass hyperobject-class)
81 (:user-name "Concept and Count" "Concepts and Counts")
82 (:default-print-slots cui freq pfstr)
83 (:description "Composite object of ucon/freq"))
85 (defclass ustr_freq (ustr freq)
87 (:metaclass hyperobject-class)
88 (:user-name "String and Count" "Strings and Counts")
89 (:default-print-slots sui freq stt lrl str)
90 (:description "Composite object of ustr/freq"))
92 (defclass usty_freq (usty freq)
94 (:metaclass hyperobject-class)
95 (:user-name "Semantic Type and Count" "Semantic Types and Counts")
96 (:default-print-slots tui freq sty)
97 (:description "Composite object of usty/freq"))
99 (defun find-usty_freq-all ()
100 (let ((usty_freqs '()))
101 (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
102 (let* ((tui (car tuple))
103 (freq (ensure-integer
104 (caar (mutex-sql-query
105 (format nil "select count(*) from MRSTY where TUI=~a" tui)))))
106 (usty (find-usty-tui tui)))
107 (push (make-instance 'usty_freq :sty (sty usty)
108 :tui (tui usty) :freq freq) usty_freqs)))
109 (sort usty_freqs #'> :key #'freq)))
112 (defclass usrl_freq (usrl freq)
114 (:metaclass hyperobject-class)
115 (:user-name "Source and Count" "Sources and Counts")
116 (:default-print-slots sab freq srl)
117 (:description "Composite object of usrl/freq"))
119 ;; Frequency finding functions
121 (defun find-usrl_freq-all ()
123 (dolist (usrl (find-usrl-all))
124 (let ((freq (ensure-integer
125 (caar (mutex-sql-query
126 (format nil "select count(*) from MRSO where SAB='~a'"
128 (push (make-instance 'usrl_freq :sab (sab usrl) :srl (srl usrl)
131 (sort freqs #'> :key #'freq)))
133 (defun find-ucon2_freq-coc-tui (ucon tui)
134 "Return sorted list of tuples with ucon and freq that have co-occuring concepts of semantic type tui"
135 (let ((ucon_freqs '()))
136 (dolist (ucoc (s#coc ucon))
138 (let ((ucon2 (find-ucon-cui it)))
139 (when (ucon-is-tui? ucon2 tui)
140 (push (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
141 :pfstr (pfstr ucon2) :freq (cof ucoc))
143 (setq ucon_freqs (delete-duplicates ucon_freqs :key #'cui))
144 (sort ucon_freqs #'> :key #'freq)))
146 (defun find-ucon2-str&sty (str sty lookup-func)
147 "Call lookup-func for ucon and usty for given str and sty"
148 (let ((ucon (car (find-ucon-str str)))
149 (usty (car (find-usty-word sty))))
151 (funcall lookup-func ucon (tui usty))
154 (defun find-ucon2-coc-str&sty (str sty)
155 "Find all ucons that are a co-occuring concept for concept named str
156 and that have semantic type of sty"
157 (find-ucon2-str&sty str sty #'find-ucon2-coc-tui))
159 (defun find-ucon2-rel-str&sty (str sty)
160 "Find all ucons that are a relationship to concept named str
161 and that have semantic type of sty"
162 (find-ucon2-str&sty str sty #'find-ucon2-rel-tui))
164 ;;; Most common relationships, co-occurances
166 (defun find-ucon2_freq-tui-all (tui ucon2-tui-func)
167 "Return sorted list of all ucon2 that have a semantic type tui with ucon that is also has sty of tui"
168 (let ((ucon_freqs (make-array (1+ (find-cui-max)) :initial-element nil)))
169 (dolist (ucon (find-ucon-tui tui)) ;; for all disease-or-syn
170 (dolist (ucon2 (funcall ucon2-tui-func ucon tui)) ;; for each related disease
171 (aif (aref ucon_freqs (cui ucon2))
172 (setf (freq it) (1+ (freq it)))
173 (setf (aref ucon_freqs (cui ucon2))
174 (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
175 :pfstr (pfstr ucon2) :freq 1)))))
176 (let ((ucon_freq-list '()))
177 (dotimes (i (find-cui-max))
179 (awhen (aref ucon_freqs i)
180 (push it ucon_freq-list)))
181 (sort ucon_freq-list #'> :key #'freq))))
183 (defun find-ucon2_freq-rel-tui-all (tui)
184 "Sorted list of ucon_freq with semantic type tui that are rel's of ucons with semantic type tui"
185 (find-ucon2_freq-tui-all tui #'find-ucon2-rel-tui))
187 (defun find-ucon2_freq-coc-tui-all (tui)
188 (find-ucon2_freq-tui-all tui #'find-ucon2-coc-tui))
191 (dolist (c '(ucon_freq ustr_freq usty_freq usrl_freq))
192 (let ((cl #+cmu (pcl:find-class c)
193 #+scl (find-class c)))
194 #+cmu (pcl:finalize-inheritance cl)
195 #+scl (clos:finalize-inheritance cl)))