a5b6f24b8796882b38bdc104590315807d463f91
[umlisp.git] / composite.lisp
1 ;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          composite.lisp
6 ;;;; Purpose:       Composite Classes for UMLisp
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: composite.lisp,v 1.19 2002/12/14 02:35:28 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UMLisp, is
13 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
14 ;;;;
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 ;;;; *************************************************************************
18
19 (in-package :umlisp)
20 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
21
22
23 ;;; Semantic type constants
24
25 (defun find-tui-word (words)
26   (aif (car (find-usty-word words))
27        (tui it)
28        nil))
29 (memoize 'find-tui-word)
30
31 (defun tui-disease-or-syndrome ()
32   (find-tui-word "disease or syndrome"))
33 (defun tui-sign-or-symptom () 
34   (find-tui-word "sign or symptom"))
35 (defun tui-finding ()
36   (find-tui-word "finding"))
37
38
39 ;;;; Related concepts with specific tui lookup functions
40
41 (defun ucon-is-tui? (ucon tui)
42   "Returns t if ucon has a semantic type of tui"
43   (find tui (s#sty ucon) :key #'tui))
44
45 (defun find-ucon2-tui (ucon tui cui2-func related-con-func)
46   "Returns a list of related ucons that have specific tui"
47   (remove-duplicates 
48    (filter
49     #'(lambda (c) 
50         (aif (funcall cui2-func c)
51              (let ((ucon2 (find-ucon-cui it)))
52                (when (ucon-is-tui? ucon2 tui)
53                  ucon2)) nil))
54     (funcall related-con-func ucon))
55    :key #'cui))
56
57 (defun find-ucon2-coc-tui (ucon tui)
58   "Return list of ucon's that have co-occuring concepts of semantic type tui"
59   (find-ucon2-tui ucon tui #'cui2 #'s#coc))
60   
61 (defun find-ucon2-rel-tui (ucon tui)
62   "Return list of ucon's that have related concepts to ucon and semantic type tui"
63   (find-ucon2-tui ucon tui #'cui2 #'s#rel))
64
65 ;;; Composite Objects
66
67 (defclass freq (hyperobject)
68   ((freq :value-type integer :initarg :freq :accessor freq :print-formatter fmt-comma-integer))
69   (:metaclass hyperobject-class)
70   (:default-initargs :freq 0)
71   (:user-name "Frequency class")
72   (:default-print-slots freq)
73   (:description "Base class containing frequency slot, used for multi-inherited objects"))
74
75 (defclass ucon_freq (ucon freq)
76   ()
77   (:metaclass hyperobject-class)
78   (:user-name "Concept and Count")
79   (:default-print-slots cui freq pfstr)
80   (:description "Composite object of ucon/freq"))
81
82 (defclass ustr_freq (ustr freq)
83   ()
84   (:metaclass hyperobject-class)
85   (:user-name "String and Count")
86   (:default-print-slots sui freq stt lrl str)
87   (:description "Composite object of ustr/freq"))
88
89 (defclass usty_freq (usty freq)
90   ((freq :value-type fixnum :initarg :freq :accessor freq))
91   (:metaclass hyperobject-class)
92   (:user-name "Semantic Type and Count")
93   (:default-print-slots tui freq sty)
94   (:description "Composite object of usty/freq"))
95
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         (push (make-instance 'usty_freq :usty (find-usty-tui tui) :freq freq) usty_freqs)))
104     (sort usty_freqs #'> :key #'freq)))
105
106
107 (defclass usrl_freq (usrl freq)
108   ()
109   (:metaclass hyperobject-class)
110   (:user-name "Source and Count")
111   (:default-print-slots sab freq srl)
112   (:description "Composite object of usrl/freq"))
113
114 ;; Frequency finding functions
115
116 (defun find-usrl_freq-all ()
117   (let ((freqs '()))
118     (dolist (usrl (find-usrl-all))
119       (let ((freq (ensure-integer 
120                    (caar (mutex-sql-query 
121                           (format nil "select count(*) from MRSO where SAB='~a'" 
122                                   (sab usrl)))))))
123         (push (make-instance 'usrl_freq :usrl usrl :freq freq) freqs)))
124     (sort freqs #'> :key #'freq)))
125
126 (defun find-ucon2_freq-coc-tui (ucon tui)
127 "Return sorted list of tuples with ucon and freq that have co-occuring concepts of semantic type tui" 
128   (let ((ucon_freqs '())) 
129     (dolist (ucoc (s#coc ucon)) 
130       (aif (cui2 ucoc) 
131            (let ((ucon2 (find-ucon-cui it))) 
132              (when (ucon-is-tui? ucon2 tui)
133                (push (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
134                                     :pfstr (pfstr ucon2) :freq (cof ucoc)) 
135                      ucon_freqs)))))
136     (setq ucon_freqs (delete-duplicates ucon_freqs :key #'cui))
137     (sort ucon_freqs #'> :key #'freq)))
138  
139 (defun find-ucon2-str&sty (str sty lookup-func)
140   "Call lookup-func for ucon and usty for given str and sty"
141   (let ((ucon (car (find-ucon-str str)))
142         (usty (car (find-usty-word sty))))
143     (if (and ucon usty)
144         (funcall lookup-func ucon (tui usty))
145       nil)))
146   
147 (defun find-ucon2-coc-str&sty (str sty)
148   "Find all ucons that are a co-occuring concept for concept named str
149    and that have semantic type of sty"
150   (find-ucon2-str&sty str sty #'find-ucon2-coc-tui))
151
152 (defun find-ucon2-rel-str&sty (str sty)
153   "Find all ucons that are a relationship to concept named str
154    and that have semantic type of sty"
155   (find-ucon2-str&sty str sty #'find-ucon2-rel-tui))
156
157 ;;; Most common relationships, co-occurances
158
159 (defun find-ucon2_freq-tui-all (tui ucon2-tui-func)
160   "Return sorted list of all ucon2 that have a semantic type tui with ucon that is also has sty of tui"
161   (let ((ucon_freqs (make-array (1+ (find-cui-max)) :initial-element nil)))
162     (dolist (ucon (find-ucon-tui tui)) ;; for all disease-or-syn
163       (dolist (ucon2 (funcall ucon2-tui-func ucon tui)) ;; for each related disease
164         (aif (aref ucon_freqs (cui ucon2))
165              (setf (freq it) (1+ (freq it)))
166              (setf (aref ucon_freqs (cui ucon2)) 
167                (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
168                               :pfstr (pfstr ucon2) :freq 1)))))
169     (let ((ucon_freq-list '()))
170       (dotimes (i (find-cui-max))
171         (declare (fixnum i))
172         (awhen (aref ucon_freqs i)
173              (push it ucon_freq-list)))
174       (sort ucon_freq-list #'> :key #'freq))))
175
176 (defun find-ucon2_freq-rel-tui-all (tui)
177   "Sorted list of ucon_freq with semantic type tui that are rel's of ucons with semantic type tui"
178   (find-ucon2_freq-tui-all tui #'find-ucon2-rel-tui))
179
180 (defun find-ucon2_freq-coc-tui-all (tui)
181   (find-ucon2_freq-tui-all tui #'find-ucon2-coc-tui))
182
183 #+(or cmu scl)
184 (dolist (c '(ucon_freq ustr_freq usty_freq usrl_freq))
185   (let ((cl #+cmu (pcl:find-class c)
186             #+scl (find-class c)))
187     #+cmu (pcl:finalize-inheritance cl)
188     #+scl (clos:finalize-inheritance cl)))