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
10 ;;;; This file, part of UMLisp, is
11 ;;;; Copyright (c) 2000-2010 by Kevin M. Rosenberg, M.D.
13 ;;;; UMLisp users are granted the rights to distribute and use this software
14 ;;;; as governed by the terms of the GNU General Public License.
15 ;;;; *************************************************************************
19 ;;; Semantic type constants
21 (defun find-tui-word (words)
22 (aif (car (find-usty-word words))
25 (memoize 'find-tui-word)
27 (defun tui-disease-or-syndrome ()
28 (find-tui-word "disease or syndrome"))
29 (defun tui-sign-or-symptom ()
30 (find-tui-word "sign or symptom"))
32 (find-tui-word "finding"))
35 ;;;; Related concepts with specific tui lookup functions
37 (defun ucon-is-tui? (ucon tui)
38 "Returns t if ucon has a semantic type of tui"
39 (find tui (s#sty ucon) :key #'tui))
41 (defun find-ucon2-tui (ucon tui cui2-func related-con-func)
42 "Returns a list of related ucons that have specific tui"
46 (aif (funcall cui2-func c)
47 (let ((ucon2 (find-ucon-cui it)))
48 (when (ucon-is-tui? ucon2 tui)
50 (funcall related-con-func ucon))
53 (defun find-ucon2-coc-tui (ucon tui)
54 "Return list of ucon's that have co-occuring concepts of semantic type tui"
55 (find-ucon2-tui ucon tui #'cui2 #'s#coc))
57 (defun find-ucon2-rel-tui (ucon tui)
58 "Return list of ucon's that have related concepts to ucon and semantic type tui"
59 (find-ucon2-tui ucon tui #'cui2 #'s#rel))
63 (defclass freq (hyperobject)
64 ((freq :value-type integer :initarg :freq :accessor freq
65 :print-formatter fmt-comma-integer))
66 (:metaclass hyperobject-class)
67 (:default-initargs :freq 0)
68 (:user-name "Frequency class" "Frequency classes")
69 (:default-print-slots freq)
70 (:description "Base class containing frequency slot, used for multi-inherited objects"))
72 (defclass ucon_freq (ucon freq)
74 (:metaclass hyperobject-class)
75 (:user-name "Concept and Count" "Concepts and Counts")
76 (:default-print-slots cui freq pfstr)
77 (:description "Composite object of ucon/freq"))
79 (defclass ustr_freq (ustr freq)
81 (:metaclass hyperobject-class)
82 (:user-name "String and Count" "Strings and Counts")
83 (:default-print-slots sui freq stt lrl str)
84 (:description "Composite object of ustr/freq"))
86 (defclass usty_freq (usty freq)
88 (:metaclass hyperobject-class)
89 (:user-name "Semantic Type and Count" "Semantic Types and Counts")
90 (:default-print-slots tui freq sty)
91 (:description "Composite object of usty/freq"))
93 (defun find-usty_freq-all ()
94 (let ((usty_freqs '()))
95 (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
96 (let* ((tui (car tuple))
98 (caar (mutex-sql-query
99 (format nil "select count(*) from MRSTY where TUI=~a" tui)))))
100 (usty (find-usty-tui tui)))
101 (push (make-instance 'usty_freq :sty (sty usty)
102 :tui (tui usty) :freq freq) usty_freqs)))
103 (sort usty_freqs #'> :key #'freq)))
106 (defclass usrl_freq (usrl freq)
108 (:metaclass hyperobject-class)
109 (:user-name "Source and Count" "Sources and Counts")
110 (:default-print-slots sab freq srl)
111 (:description "Composite object of usrl/freq"))
113 ;; Frequency finding functions
115 (defun find-usrl_freq-all ()
117 (dolist (usrl (find-usrl-all))
118 (let ((freq (ensure-integer
119 (caar (mutex-sql-query
120 (format nil "select count(*) from MRSO where SAB='~a'"
122 (push (make-instance 'usrl_freq :sab (sab usrl) :srl (srl usrl)
125 (sort freqs #'> :key #'freq)))
127 (defun find-ucon2_freq-coc-tui (ucon tui)
128 "Return sorted list of tuples with ucon and freq that have co-occuring concepts of semantic type tui"
129 (let ((ucon_freqs '()))
130 (dolist (ucoc (s#coc ucon))
132 (let ((ucon2 (find-ucon-cui it)))
133 (when (ucon-is-tui? ucon2 tui)
134 (push (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
135 :pfstr (pfstr ucon2) :freq (cof ucoc))
137 (setq ucon_freqs (delete-duplicates ucon_freqs :key #'cui))
138 (sort ucon_freqs #'> :key #'freq)))
140 (defun find-ucon2-str&sty (str sty lookup-func)
141 "Call lookup-func for ucon and usty for given str and sty"
142 (let ((ucon (car (find-ucon-str str)))
143 (usty (car (find-usty-word sty))))
145 (funcall lookup-func ucon (tui usty))
148 (defun find-ucon2-coc-str&sty (str sty)
149 "Find all ucons that are a co-occuring concept for concept named str
150 and that have semantic type of sty"
151 (find-ucon2-str&sty str sty #'find-ucon2-coc-tui))
153 (defun find-ucon2-rel-str&sty (str sty)
154 "Find all ucons that are a relationship to concept named str
155 and that have semantic type of sty"
156 (find-ucon2-str&sty str sty #'find-ucon2-rel-tui))
158 ;;; Most common relationships, co-occurances
160 (defun find-ucon2_freq-tui-all (tui ucon2-tui-func)
161 "Return sorted list of all ucon2 that have a semantic type tui with ucon that is also has sty of tui"
162 (let ((ucon_freqs (make-array (1+ (find-cui-max)) :initial-element nil)))
163 (dolist (ucon (find-ucon-tui tui)) ;; for all disease-or-syn
164 (dolist (ucon2 (funcall ucon2-tui-func ucon tui)) ;; for each related disease
165 (aif (aref ucon_freqs (cui ucon2))
166 (setf (freq it) (1+ (freq it)))
167 (setf (aref ucon_freqs (cui ucon2))
168 (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
169 :pfstr (pfstr ucon2) :freq 1)))))
170 (let ((ucon_freq-list '()))
171 (dotimes (i (find-cui-max))
173 (awhen (aref ucon_freqs i)
174 (push it ucon_freq-list)))
175 (sort ucon_freq-list #'> :key #'freq))))
177 (defun find-ucon2_freq-rel-tui-all (tui)
178 "Sorted list of ucon_freq with semantic type tui that are rel's of ucons with semantic type tui"
179 (find-ucon2_freq-tui-all tui #'find-ucon2-rel-tui))
181 (defun find-ucon2_freq-coc-tui-all (tui)
182 (find-ucon2_freq-tui-all tui #'find-ucon2-coc-tui))
185 (dolist (c '(ucon_freq ustr_freq usty_freq usrl_freq))
186 (let ((cl #+cmu (pcl:find-class c)
187 #+scl (find-class c)))
188 #+cmu (pcl:finalize-inheritance cl)
189 #+scl (clos:finalize-inheritance cl)))