r9529: successful import of 2004aa
[umlisp.git] / parse-rrf.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:     parse-2002.lisp
6 ;;;; Purpose:  Parsing and SQL insertion routines for UMLisp which may
7 ;;;;           change from year to year
8 ;;;; Author:   Kevin M. Rosenberg
9 ;;;; Created:  Apr 2000
10 ;;;;
11 ;;;; $Id$
12 ;;;;
13 ;;;; This file, part of UMLisp, is
14 ;;;;    Copyright (c) 2000-2004 by Kevin M. Rosenberg, M.D.
15 ;;;;
16 ;;;; UMLisp users are granted the rights to distribute and use this software
17 ;;;; as governed by the terms of the GNU General Public License.
18 ;;;; *************************************************************************
19
20 (in-package #:umlisp)
21
22 ;;; Pre-read data for custom fields into hash tables
23 (defvar *preparse-hash-init?* nil)
24
25 (eval-when (:compile-toplevel :load-toplevel :execute)
26 (let ((pfstr-hash nil)      ;; Preferred concept strings by CUI
27       (cui-lrl-hash nil)    ;; LRL by CUI
28       (lui-lrl-hash nil)    ;; LRL by LUI
29       (sui-lrl-hash nil)    ;; LRL by SUI
30       (cuisui-lrl-hash nil) ;; LRL by CUISUI
31       (sab-srl-hash nil))   ;; SRL by SAB
32   
33   (defun make-preparse-hash-table ()
34     (if sui-lrl-hash
35         (progn
36           (clrhash pfstr-hash)
37           (clrhash cui-lrl-hash)
38           (clrhash lui-lrl-hash)
39           (clrhash sui-lrl-hash)
40           (clrhash cuisui-lrl-hash)
41           (clrhash sab-srl-hash))
42       (setf
43           pfstr-hash (make-hash-table :size 800000)
44           cui-lrl-hash (make-hash-table :size 800000)
45           lui-lrl-hash (make-hash-table :size 1500000)
46           sui-lrl-hash (make-hash-table :size 1500000)
47           cuisui-lrl-hash (make-hash-table :size 1800000)
48           sab-srl-hash (make-hash-table :size 100 :test 'equal))))
49     
50   (defun ensure-preparse (&optional (force-read nil))
51     (when (or force-read (not *preparse-hash-init?*))
52       (make-preparse-hash-table)
53       (setq *preparse-hash-init?* t))
54     (with-umls-file (line "MRCONSO.RRF")
55       (let ((cui (parse-ui (nth 0 line)))
56             (lui (parse-ui (nth 3 line)))
57             (sui (parse-ui (nth 5 line)))
58             (sab (nth 11 line))
59             (srl (parse-integer (nth 15 line))))
60         (unless (gethash cui pfstr-hash)  ;; if haven't stored pfstr for cui
61           (if (and (string-equal (nth 1 line) "ENG") ; LAT
62                    (string-equal (nth 2 line) "P") ; ts
63                    (string-equal (nth 4 line) "PF")) ; stt
64               (setf (gethash cui pfstr-hash) (nth 14 line))))
65         (set-lrl-hash cui srl cui-lrl-hash)
66         (set-lrl-hash lui srl lui-lrl-hash)
67         (set-lrl-hash sui srl sui-lrl-hash)
68         (set-lrl-hash (make-cuisui cui sui) srl cuisui-lrl-hash)
69         (multiple-value-bind (val found) (gethash sab sab-srl-hash)
70           (declare (ignore val))
71           (unless found
72             (setf (gethash sab sab-srl-hash) srl))))))
73   
74   (defun pfstr-hash (cui) (gethash cui pfstr-hash))
75   (defun cui-lrl (cui)    (gethash cui cui-lrl-hash))
76   (defun lui-lrl (lui)    (gethash lui lui-lrl-hash))
77   (defun sui-lrl (sui)    (gethash sui sui-lrl-hash))
78   (defun sab-srl (sab)    (aif (gethash sab sab-srl-hash) it 0))
79   (defun cuisui-lrl (cuisui) (gethash cuisui cuisui-lrl-hash))
80   
81 )) ;; closure
82
83 (defun set-lrl-hash (key lrl hash)
84   "Set the least restrictive level in hash table"
85   (multiple-value-bind (hash-lrl found) (gethash key hash)
86     (if (or (not found) (< lrl hash-lrl))
87         (setf (gethash key hash) lrl))))
88
89 ;; UMLS file and column structures
90 ;;; SQL datatypes symbols
91 ;;; sql-u - Unique identifier
92 ;;; sql-s - Small integer (16-bit)
93 ;;; sql-i - Integer (32-bit)
94 ;;; sql-l - Big integer (64-bit)
95 ;;; sql-f - Floating point
96 ;;; sql-c - Character data
97
98 (defparameter +col-datatypes+
99     '(("AV" sql-f) ("BTS" sql-i) ("CLS" sql-i) ("COF" sql-i) ("CUI1" sql-u)
100       ("AUI" sql-u) ("AUI1" sql-u) ("AUI2" sql-u) ("PCUI" sql-u)
101       ("PLUI" sql-u)
102       ("CUI2" sql-u) ("CUI" sql-u) ("CXN" sql-s) ("FR" sql-i) ("LRL" sql-s)
103       ("LUI" sql-u) ("MAX" sql-s) ("MIN" sql-s) ("RANK" sql-s) ("REF" sql-c)
104       ("RNK" sql-s) ("RWS" sql-i) ("SRL" sql-s) ("SUI" sql-u) ("TUI" sql-u)
105       ;;; Custom columns
106       ("KCUISUI" sql-l) ("KCUILUI" sql-l) ("KCUILRL" sql-i) ("KLUILRL" sql-i)
107       ("KSUILRL" sql-i)
108       ("KSRL" sql-i) ("KLRL" sql-i)
109       ;;; LEX columns
110       ("EUI" sql-u) ("EUI2" sql-u)
111       ;;; Semantic net columns
112       ("UI" sql-u) ("UI2" sql-u) ("UI3" sql-u)
113       ;; New fields for 2002AD
114       ("RCUI" sql-u) ("VCUI" sql-u) ("CFR" sql-i) ("TFR" sql-i)
115       ("MAPSETCUI" sql-u)
116       ) 
117     "SQL data types for each non-string column")
118
119 (defparameter +custom-tables+
120     nil
121   #+ignore
122   '(("KCON" "SELECT CUI,STR FROM MRCONSO WHERE STT='PF' AND TS='P' AND ISPREF='Y' AND LAT='ENG'"))
123   "Custom tables to create")
124
125 (defparameter +custom-cols+
126     '(("MRCONSO.RRF" "KPFSTR" "TEXT"
127                      (slot-value (find-ucol "STR" "MRCONSO.RRF") 'max)
128                      (lambda (x) (pfstr-hash (parse-ui (nth 0 x)))))
129       ("MRCONSO.RRF" "KCUISUI" "BIGINT" 0
130        (lambda (x) (write-to-string (make-cuisui (parse-ui (nth 0 x)) (parse-ui (nth 5 x))))))
131       ("MRCONSO.RRF" "KCUILUI" "BIGINT" 0
132        (lambda (x) (write-to-string (make-cuilui (parse-ui (nth 0 x)) (parse-ui (nth 3 x))))))
133       ("MRCONSO.RRF" "KCUILRL" "SMALLINT" 0
134        (lambda (x) (write-to-string (lui-lrl (parse-ui (nth 0 x))))))
135       ("MRCONSO.RRF" "KLUILRL" "SMALLINT" 0
136        (lambda (x) (write-to-string (lui-lrl (parse-ui (nth 3 x))))))
137       ("MRCONSO.RRF" "KSUILRL" "SMALLINT" 0
138        (lambda (x) (write-to-string (sui-lrl (parse-ui (nth 5 x))))))
139       ;; Deprecated, last in 2004AA -- skip index
140       #+ignore
141       ("MRLO.RRF" "KLRL" "SMALLINT" 0
142        (lambda (x) (write-to-string 
143                     (if (zerop (length (nth 4 x)))
144                         (cui-lrl (parse-ui (nth 0 x)))
145                       (cuisui-lrl (make-cuisui (parse-ui (nth 0 x)) (parse-ui (nth 4 x))))))))
146       ("MRSTY.RRF" "KLRL" "SMALLINT" 0
147        (lambda (x) (write-to-string (cui-lrl (parse-ui (nth 0 x))))))
148       ("MRCOC.RRF" "KLRL" "SMALLINT" 0
149        (lambda (x) (write-to-string 
150                     (max (cui-lrl (parse-ui (nth 0 x)))
151                          (kmrcl:aif (cui-lrl (parse-ui (nth 1 x))) kmrcl::it 0)))))
152       ("MRSAT.RRF" "KSRL" "SMALLINT" 0
153        (lambda (x) (write-to-string (sab-srl (nth 9 x)))))
154       ("MRREL.RRF" "KSRL" "SMALLINT" 0
155        (lambda (x) (write-to-string (sab-srl (nth 10 x)))))
156       ("MRRANK.RRF" "KSRL" "SMALLINT" 0
157        (lambda (x) (write-to-string (sab-srl (nth 1 x)))))
158       ("MRDEF.RRF" "KSRL" "SMALLINT" 0
159        (lambda (x) (write-to-string (sab-srl (nth 4 x)))))
160       ("MRCXT.RRF" "KSRL" "SMALLINT" 0
161        (lambda (x) (write-to-string (sab-srl (nth 2 x)))))
162       ("MRXW_ENG.RRF" "KLRL" "SMALLINT" 0
163        (lambda (x) (write-to-string (cuisui-lrl (make-cuisui 
164                                                  (parse-ui (nth 2 x))
165                                                  (parse-ui (nth 4 x)))))))
166       ("MRXW_NONENG.RRF" "KLRL" "SMALLINT" 0
167        (lambda (x) (write-to-string (cuisui-lrl (make-cuisui 
168                                                  (parse-ui (nth 2 x))
169                                                  (parse-ui (nth 4 x)))))))
170       ("MRXNW_ENG.RRF" "KLRL" "SMALLINT" 0
171        (lambda (x) (write-to-string (cuisui-lrl (make-cuisui 
172                                                  (parse-ui (nth 2 x))
173                                                  (parse-ui (nth 4 x)))))))
174       ("MRXNS_ENG.RRF" "KLRL" "SMALLINT" 0
175        (lambda (x) (write-to-string (cuisui-lrl (make-cuisui 
176                                                  (parse-ui (nth 2 x))
177                                                  (parse-ui (nth 4 x)))))))
178       ("MRREL.RRF" "KPFSTR2" "TEXT" 1024
179        (lambda (x) (pfstr-hash (parse-ui (nth 4 x)))))
180       ("MRCOC.RRF" "KPFSTR2" "TEXT" 1024
181        (lambda (x) (pfstr-hash (parse-ui (nth 2 x)))))
182       ("MRCXT.RRF" "KCUISUI" "BIGINT" 0 
183        (lambda (x) (write-to-string (make-cuisui (parse-ui (nth 0 x)) (parse-ui (nth 1 x))))))
184       ("MRSAT.RRF" "KCUILUI" "BIGINT" 0
185        (lambda (x) (write-to-string (make-cuilui (parse-ui (nth 0 x)) (parse-ui (nth 1 x))))))
186       ("MRSAT.RRF" "KCUISUI" "BIGINT" 0
187        (lambda (x) (write-to-string (make-cuisui (parse-ui (nth 0 x)) (parse-ui (nth 2 x))))))
188       ("MRXW_ENG.RRF" "KCUISUI" "BIGINT" 0
189        (lambda (x) (write-to-string (make-cuisui (parse-ui (nth 2 x)) (parse-ui (nth 4 x))))))
190       ("MRXNW_ENG.RRF" "KCUISUI" "BIGINT" 0
191        (lambda (x) (write-to-string (make-cuisui (parse-ui (nth 2 x)) (parse-ui (nth 4 x))))))
192       ("MRXNS_ENG.RRF" "KCUISUI" "BIGINT" 0
193        (lambda (x) (write-to-string (make-cuisui (parse-ui (nth 2 x)) (parse-ui (nth 4 x))))))
194       ("MRXW_NONENG.RRF" "LAT" "VARCHAR" 3 (lambda (x) (nth 0 x)))
195       ("MRXW_NONENG.RRF" "WD"  "VARCHAR" 200  (lambda (x) (nth 1 x)))
196       ("MRXW_NONENG.RRF" "CUI" "INTEGER" 0 (lambda (x) (write-to-string (parse-ui (nth 2 x)))))
197       ("MRXW_NONENG.RRF" "LUI" "INTEGER" 0 (lambda (x) (write-to-string (parse-ui (nth 3 x)))))
198       ("MRXW_NONENG.RRF" "SUI" "INTEGER" 0 (lambda (x) (write-to-string (parse-ui (nth 4 x)))))
199       ("MRXW_NONENG.RRF" "KCUISUI" "BIGINT" 0 
200        (lambda (x) (write-to-string (make-cuisui (parse-ui (nth 2 x)) (parse-ui (nth 4 x)))))))
201   "Custom columns to create.(filename, col, sqltype, value-func).")
202
203 (defparameter +index-cols+
204     '(("CUI1" "MRCOC") ("CUI" "MRCONSO") ("LUI" "MRCONSO") 
205       ("SRL" "MRCONSO") ("AUI" "MRCONSO")
206       ("SUI" "MRCONSO") ("CUI" "MRCXT") ("CUI" "MRDEF") ("CUI" "MRLO")
207       ("CUI1" "MRREL") ("CUI" "MRSAT") ("LUI" "MRSAT") ("SUI" "MRSAT")
208       ("CUI" "MRSTY")
209       ("TUI" "MRSTY") ("CUI" "MRXNS_ENG") 
210       #+ignore ("NSTR" "MRXNS_ENG" 10)
211       ("CUI" "MRXNW_ENG") ("NWD" "MRXNW_ENG") ("WD" "MRXW_ENG")
212       ("KCUISUI" "MRCONSO") ("KCUILUI" "MRCONSO") ("KCUILRL" "MRCONSO")
213       ("KLUILRL" "MRCONSO") ("KCUISUI" "MRCXT") 
214       ("KCUISUI" "MRSAT")  ("KCUILUI" "MRSAT")
215       ("KCUISUI" "MRXW_ENG") ("KCUISUI" "MRXNW_ENG") 
216       ("KCUISUI" "MRXNS_ENG") ("KCUISUI" "MRXW_NONENG")
217       ("KSRL" "MRCXT") ("KSRL" "MRDEF") ("KSRL" "MRRANK") 
218       ("KSRL" "MRREL") ("KSRL" "MRSAT") ("KLRL" "MRCOC") 
219       #+ignore ("KLRL" "MRLO")  ;; deprecated
220       ("KLRL" "MRSTY") ("KLRL" "MRXW_ENG") ("KLRL" "MRXNW_ENG")
221       ("KLRL" "MRXNS_ENG") ("KLRL" "MRXW_NONENG")
222       ;; LEX indices
223       ("EUI" "LRABR") ("EUI2" "LRABR") ("EUI" "LRAGR") ("EUI" "LRCMP") ("EUI" "LRMOD")
224       ("EUI" "LRNOM") ("EUI2" "LRNOM") ("EUI" "LRPRN") ("EUI" "LRPRP") ("EUI" "LRSPL")
225       ("EUI" "LRTRM") ("EUI" "LRTYP") ("EUI" "LRWD") ("WRD" "LRWD")
226       ("BAS" "LRABR") 
227       ;; Semantic NET indices
228       ("UI" "SRSTRE1") ("UI2" "SRSTRE1") ("UI3" "SRSTRE1") 
229       ("STY_RL" "SRDEF") ("RT" "SRDEF") ("STY_RL" "SRSTR") ("STY_RL2" "SRSTR")
230       ("RL" "SRSTR")
231       ("SRL" "MRSAB") ("RSAB" "MRSAB") ("VSAB" "MRSAB") ("RCUI" "MRSAB")
232       ("VCUI" "MRSAB") ("LAT" "MRSAB"))
233   "Columns in files to index")
234
235
236 (defparameter +custom-index-cols+
237   nil
238   #+ignore
239   '(("CUI" "KCON") ("LRL" "KCON"))
240   "Indexes to custom tables")
241
242 ;; File & Column functions
243
244 (defun gen-ucols ()
245   (add-ucols (gen-ucols-meta))
246   (add-ucols (gen-ucols-custom))
247   (add-ucols (gen-ucols-generic "LRFLD"))
248   (add-ucols (gen-ucols-generic "SRFLD")))
249
250 (defun gen-ucols-meta ()
251 "Initialize all umls columns"  
252   (let ((cols '()))
253     (with-umls-file (line "MRCOLS.RRF")
254       (destructuring-bind (col des ref min av max fil dty) line
255         (push (make-ucol col des ref (parse-integer min) (read-from-string av)
256                          (parse-integer max) fil dty)
257               cols)))
258     (nreverse cols)))
259
260 (defun gen-ucols-custom ()
261 "Initialize umls columns for custom columns"  
262   (loop for customcol in +custom-cols+
263         collect
264         (make-ucol (nth 1 customcol) "" 0 0 0 (eval (nth 3 customcol))
265                    (nth 0 customcol) nil :sqltype (canonicalize-column-type (nth 2 customcol))
266                    :custom-value-fun (nth 4 customcol))))
267
268 (defun gen-ucols-generic (col-filename)
269 "Initialize for generic (LEX/NET) columns"  
270   (let ((cols '()))
271     (with-umls-file (line col-filename)
272       (destructuring-bind (nam des ref fil) line
273         (setq nam (escape-column-name nam))
274         (dolist (file (delimited-string-to-list fil #\,))
275           (push
276            (make-ucol nam des ref nil nil nil file nil)
277            cols))))
278     (nreverse cols)))
279
280
281 (defun gen-ufiles ()
282   (add-ufiles (gen-ufiles-generic "MRFILES.RRF" "META"))
283   (add-ufiles (gen-ufiles-generic "LRFIL" "LEX"))
284   (add-ufiles (gen-ufiles-generic "SRFIL" "NET"))
285   ;; needs to come last
286   (add-ufiles (gen-ufiles-custom)))
287
288                         
289 (defun gen-ufiles-generic (files-filename dir)
290 "Initialize all LEX file structures"  
291   (let ((files '()))
292     (with-umls-file (line files-filename)
293       (destructuring-bind (fil des fmt cls rws bts) line
294         (push (make-ufile
295                dir fil des 
296                (parse-integer cls)
297                (parse-integer rws) (parse-integer bts)
298                (concatenate 'list (umls-field-string-to-list fmt)
299                             (custom-colnames-for-filename fil)))
300               files)))
301     (nreverse files)))
302
303 (defun gen-ufiles-custom ()
304   (make-ufile "META" "MRXW_NONENG.RRF" "Custom NonEnglish Index" 
305               5 0 0 (fields (find-ufile "MRXW_ENG.RRF"))))
306
307
308