-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsp_GetOntologyFromUMLS.sql
282 lines (235 loc) · 8.87 KB
/
sp_GetOntologyFromUMLS.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Nic Dobbins
-- Create date: 2015/12/30
-- Description: Returns a heirarchical SQL table of
-- Ontology items which can be inserted into the
-- Leaf app.Concept table.
-- Comments: This script assumes
-- 1) You have a valid UMLS license.
-- 2) There is a UMLS database named [UMLS].
-- =============================================
CREATE PROCEDURE [dbo].[sp_GetConceptOntologyFromUMLS]
@OntologyType NVARCHAR (20)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY DROP TABLE #codeHeirarchy END TRY BEGIN CATCH END CATCH
BEGIN TRY DROP TABLE #distinctCodes END TRY BEGIN CATCH END CATCH
BEGIN TRY DROP TABLE #allParentChildCodes END TRY BEGIN CATCH END CATCH
BEGIN TRY DROP TABLE #allCodes END TRY BEGIN CATCH END CATCH
BEGIN TRY DROP TABLE #codeHeirarchyVertical END TRY BEGIN CATCH END CATCH
BEGIN TRY DROP TABLE #minmax END TRY BEGIN CATCH END CATCH
BEGIN TRY DROP TABLE #MRCONSO END TRY BEGIN CATCH END CATCH
BEGIN TRY DROP TABLE #MRHIER END TRY BEGIN CATCH END CATCH
-- Copy MRCONSO
SELECT m.AUI
, m.SAB
, m.CODE
, m.TTY
, m.STR
INTO #MRCONSO
FROM UMLS.dbo.MRCONSO m
WHERE SAB = @ontologyType
-- Copy MRHEIR
SELECT mh.AUI
, mh.PTR
INTO #MRHIER
FROM UMLS.dbo.MRHIER mh
WHERE mh.AUI IN (SELECT m.AUI FROM #MRCONSO m)
SELECT SourceType = @ontologyType
, mh.AUI
, PTR = CONVERT(NVARCHAR(400), mh.PTR)
, PreviousPTR = CONVERT(NVARCHAR(20),'')
INTO #codeHeirarchy
FROM #MRHIER mh
CREATE NONCLUSTERED INDEX IDX_CPT ON #codeHeirarchy (PTR ASC, AUI ASC) INCLUDE (SourceType, PreviousPTR)
CREATE NONCLUSTERED INDEX IDX_M ON #MRCONSO (AUI ASC, SAB ASC)
/*******************************************************************************************************
* Loop through AUI codes and separate each into new row
*******************************************************************************************************/
BEGIN TRY DROP TABLE #Codes END TRY BEGIN CATCH END CATCH
DECLARE @loopCount INT = 0,
@delimeter NVARCHAR(2) = '.',
@loopLimit INT = 20,
@updatedRows INT = 1;
CREATE TABLE #Codes
(
Code NVARCHAR(20) NULL,
PreviousCode NVARCHAR(20) NULL,
CodeOrder INT NULL,
AUI NVARCHAR(20) NULL,
CodeType NVARCHAR(20) NULL
)
WHILE @loopCount < @loopLimit AND @updatedRows > 0
BEGIN
BEGIN TRY DROP TABLE #CurrentCodes END TRY BEGIN CATCH END CATCH
-- Get the current left-most AUI (i.e. everything up to the first period ".")
INSERT INTO #Codes
SELECT Code = CASE CHARINDEX(@delimeter, PTR) WHEN 0 THEN LTRIM(RTRIM(PTR))
ELSE LTRIM(RTRIM(LEFT(PTR, CHARINDEX(@delimeter, PTR)))) END
,PreviousCode = PreviousPTR
,CodeOrder = @loopCount
,AUI
,CodeType = SourceType
FROM #codeHeirarchy c
WHERE PTR IS NOT NULL
SET @updatedRows = @@ROWCOUNT
-- Save the previous PTR
UPDATE #codeHeirarchy
SET PreviousPTR =
CASE CHARINDEX(@delimeter, PTR)
WHEN 0 THEN LTRIM(RTRIM(PTR))
ELSE LTRIM(RTRIM(LEFT(PTR, CHARINDEX(@delimeter, PTR))))
END
-- Chop off everything to the left of the first period "."
UPDATE #codeHeirarchy
SET PTR = NULLIF(RIGHT(PTR, LEN(PTR) - CHARINDEX(@delimeter, PTR)),'')
DELETE FROM #codeHeirarchy
WHERE PTR IS NULL
-- Increment the @loopCount
SET @loopCount += 1
END
UPDATE #Codes
SET Code = REPLACE(Code,@delimeter,'')
, PreviousCode = REPLACE(PreviousCode,@delimeter,'')
/*******************************************************************************************************
* Get distinct list of each code
*******************************************************************************************************/
SELECT c.Code
,c.PreviousCode
,CodeOrder = MIN(CodeOrder)
,c.AUI
,c.CodeType
INTO #distinctCodes
FROM #Codes c
GROUP BY c.Code, c.PreviousCode, c.AUI, c.CodeType
SELECT DISTINCT ParentAUI = d.PreviousCode, AUI = d.Code
INTO #allParentChildCodes
FROM #distinctCodes d
WHERE d.Code != d.PreviousCode
UNION
SELECT ParentAUI = d.Code, d.AUI
FROM #distinctCodes d
WHERE d.CodeOrder = (SELECT MAX(CodeOrder)
FROM #distinctCodes d2
WHERE d.AUI = d2.AUI)
/*******************************************************************************************************
* Get distinct list of each code with future display name
*******************************************************************************************************/
SELECT DISTINCT a.AUI, m.Code, is_Parent = 0, ui_Display_Name = CONVERT(NVARCHAR(400),'')
INTO #allCodes
FROM #allParentChildCodes a
INNER JOIN #MRCONSO m
ON a.AUI = m.AUI
UNION
SELECT DISTINCT ParentAUI, m.Code, is_Parent = 0, ui_Display_Name = CONVERT(NVARCHAR(400),'')
FROM #allParentChildCodes a
INNER JOIN #MRCONSO m
ON a.ParentAUI = m.AUI
/*******************************************************************************************************
* Figure out of it is a parent
*******************************************************************************************************/
UPDATE #allCodes
SET is_Parent = 1
FROM #allCodes a
WHERE a.AUI IN (SELECT DISTINCT a2.ParentAUI FROM #allParentChildCodes a2)
UPDATE #allCodes
SET Code = NULL
WHERE is_Parent = 1
/*******************************************************************************************************
* Update display name
* If it is a parent use shorter name, if base child use verbose descriptive name
*******************************************************************************************************/
UPDATE #allCodes
SET ui_Display_Name = LEFT(m.STR,400)
FROM #allCodes a
INNER JOIN #MRCONSO m
ON a.CODE = m.CODE
WHERE a.is_Parent = 0
AND m.SAB = @OntologyType
AND m.TTY = 'PT'
UPDATE #allCodes
SET ui_Display_Name = LEFT(m.STR,400)
FROM #allCodes a
INNER JOIN #MRCONSO m
ON a.AUI = m.AUI
WHERE a.ui_Display_Name = ''
CREATE NONCLUSTERED INDEX IDX_CPT_AC ON #allCodes (AUI ASC, Code ASC)
CREATE NONCLUSTERED INDEX IDX_CPT_APC ON #allParentChildCodes (AUI ASC, ParentAUI ASC)
/*******************************************************************************************************
* Create a vertical table for storing all children of a given AUI
*******************************************************************************************************/
CREATE TABLE #codeHeirarchyVertical
(
AUI NVARCHAR(20) NULL,
ChildAUI NVARCHAR(20) NULL
)
INSERT INTO #codeHeirarchyVertical
SELECT a.AUI
,a.AUI
FROM #allCodes a
SET @loopCount = 0
SET @loopLimit = 10
SET @updatedRows = 1
/*******************************************************************************************************
* Loop through 10 times, each time going one level deeper in adding descendants
*******************************************************************************************************/
WHILE @loopCount < @loopLimit
BEGIN
BEGIN TRY DROP TABLE #currentBatch END TRY BEGIN CATCH END CATCH
SELECT a.AUI
,ChildAUI = ap.AUI
INTO #currentBatch
FROM #codeHeirarchyVertical a
INNER JOIN #allParentChildCodes ap
ON a.ChildAUI = ap.ParentAUI
-- Delete if row already exists
DELETE #currentBatch
FROM #currentBatch c
INNER JOIN #codeHeirarchyVertical cv
ON c.AUI = cv.AUI
AND c.ChildAUI = cv.ChildAUI
-- Insert if it is new
INSERT INTO #codeHeirarchyVertical
SELECT c.AUI
,c.ChildAUI
FROM #currentBatch c
-- Increment the @loopCount
SET @loopCount += 1
END
/*******************************************************************************************************
* Get the highest and lowest associated codes for a given AUI
*******************************************************************************************************/
SELECT a.AUI
,MinCode = MIN(ac.Code)
,MaxCode = MAX(ac.Code)
,CodeCount = COUNT(DISTINCT ac.Code)
INTO #minmax
FROM #codeHeirarchyVertical a
INNER JOIN #allCodes ac
ON a.ChildAUI = ac.AUI
WHERE ac.CODE IS NOT NULL
GROUP BY a.AUI
SELECT a.AUI
,ParentAUI = NULLIF(ap.ParentAUI,'')
,a.MinCode
,a.MaxCode
,a.CodeCount
,OntologyType = @OntologyType
,SqlSetWhere = CASE a.CodeCount WHEN 1 THEN '= ''' + MinCode + ''''
WHEN 2 THEN 'IN (''' + MinCode + ''',''' + MaxCode + ''')'
ELSE 'BETWEEN ''' + MinCode + ''' AND ''' + MaxCode + '''' END
,uiDisplayName = LEFT(ac.ui_Display_Name, 400) + ' (' +
CASE a.CodeCount WHEN 1 THEN @OntologyType + ':' + MinCode + ')'
ELSE @OntologyType + ':' + MinCode + '-' + MaxCode + ')'
END
FROM #minmax a
INNER JOIN #allCodes ac
ON a.AUI = ac.AUI
LEFT JOIN #allParentChildCodes ap
ON a.AUI = ap.AUI
END