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