-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathseg_index.sql
More file actions
177 lines (177 loc) · 7.16 KB
/
Copy pathseg_index.sql
File metadata and controls
177 lines (177 loc) · 7.16 KB
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
# table-description:
# This table contains one row per DICOM Segmentation
# SeriesInstanceUID available from IDC, and captures
# key metadata about the segmentation series including
# the number of segments, segmentation type, algorithm
# type and name, and the segmented image series.
# Note: multi-valued columns (AlgorithmType, AlgorithmName,
# SegmentedPropertyCategory_CodeMeanings, etc.) are aggregated
# with DISTINCT independently, so positional correspondence
# between columns is not preserved. For example, the first
# value in AlgorithmType does not necessarily pair with the
# first value in AlgorithmName.
WITH
segmentations AS (
WITH
segmentations AS (
WITH
segs AS (
SELECT
PatientID,
StudyInstanceUID,
SeriesInstanceUID,
SOPInstanceUID,
FrameOfReferenceUID,
SegmentSequence,
SegmentationType
FROM
`bigquery-public-data.idc_v23.dicom_metadata`
WHERE
# more reliable than Modality = "SEG"
SOPClassUID = "1.2.840.10008.5.1.4.1.1.66.4"
)
SELECT
PatientID,
StudyInstanceUID,
SeriesInstanceUID,
SOPInstanceUID,
FrameOfReferenceUID,
SegmentationType,
CASE ARRAY_LENGTH(unnested.AnatomicRegionSequence)
WHEN 0 THEN NULL
ELSE
STRUCT(
unnested.AnatomicRegionSequence[OFFSET(0)].CodeValue
AS CodeValue,
unnested.AnatomicRegionSequence[OFFSET(0)].CodingSchemeDesignator
AS CodingSchemeDesignator,
unnested.AnatomicRegionSequence[OFFSET(0)].CodeMeaning
AS CodeMeaning)
END
AS AnatomicRegion,
CASE
(
ARRAY_LENGTH(unnested.AnatomicRegionSequence) > 0
AND ARRAY_LENGTH(
unnested.AnatomicRegionSequence[OFFSET(0)].AnatomicRegionModifierSequence)
> 0)
WHEN TRUE
THEN
unnested.AnatomicRegionSequence[OFFSET(0)].AnatomicRegionModifierSequence[
OFFSET(
0)] # unnested.AnatomicRegionSequence[OFFSET(0)].AnatomicRegionModifierSequence,
ELSE
NULL
END
AS AnatomicRegionModifier,
CASE ARRAY_LENGTH(unnested.SegmentedPropertyCategoryCodeSequence)
WHEN 0 THEN NULL
ELSE
unnested.SegmentedPropertyCategoryCodeSequence[
OFFSET(0)]
END
AS SegmentedPropertyCategory,
CASE ARRAY_LENGTH(unnested.SegmentedPropertyTypeCodeSequence)
WHEN 0 THEN NULL
ELSE
unnested.SegmentedPropertyTypeCodeSequence[
OFFSET(0)]
END
AS SegmentedPropertyType,
# unnested.SegmentedPropertyTypeCodeSequence,
# unnested.SegmentedPropertyTypeModifierCodeSequence,
unnested.SegmentAlgorithmType,
# for unknown reason, this attribute is REPEATED in our BigQuery schema
unnested.SegmentAlgorithmName[SAFE_OFFSET(0)] as SegmentAlgorithmName,
unnested.SegmentNumber,
unnested.TrackingUID,
unnested.TrackingID
FROM
segs
CROSS JOIN
UNNEST(SegmentSequence) AS unnested
),
sampled_sops AS (
SELECT
SOPInstanceUID AS seg_SOPInstanceUID,
ReferencedSeriesSequence[SAFE_OFFSET(0)].ReferencedInstanceSequence[SAFE_OFFSET(0)].ReferencedSOPInstanceUID
AS rss_one,
ReferencedImageSequence[SAFE_OFFSET(0)].ReferencedSOPInstanceUID
AS ris_one,
SourceImageSequence[SAFE_OFFSET(0)].ReferencedSOPInstanceUID
AS sis_one
FROM
`bigquery-public-data.idc_v23.dicom_all`
WHERE
Modality = "SEG"
AND SOPClassUID = "1.2.840.10008.5.1.4.1.1.66.4"
),
coalesced_ref AS (
SELECT
*,
COALESCE(rss_one, ris_one, sis_one) AS referenced_sop
FROM
sampled_sops
)
SELECT
segmentations.*,
dicom_all.SeriesInstanceUID AS segmented_SeriesInstanceUID,
CONCAT(
"https://viewer.imaging.datacommons.cancer.gov/viewer/",
segmentations.StudyInstanceUID,
"?seriesInstanceUID=",
segmentations.SeriesInstanceUID,
",",
dicom_all.SeriesInstanceUID) AS viewer_url,
FROM
coalesced_ref
JOIN
`bigquery-public-data.idc_v23.dicom_all` AS dicom_all
ON
coalesced_ref.referenced_sop = dicom_all.SOPInstanceUID
RIGHT JOIN
segmentations
ON
segmentations.SOPInstanceUID = coalesced_ref.seg_SOPInstanceUID
)
SELECT
# description:
# DICOM SeriesInstanceUID identifier of the segmentation series
SeriesInstanceUID,
# description:
# Type of segmentation as defined in DICOM SegmentationType attribute
any_value(SegmentationType) AS SegmentationType,
# description:
# Number of segments in the segmentation series obtained by counting distinct DICOM SegmentNumber values in the DICOM SegmentatationSequence
COUNT(DISTINCT (SegmentNumber)) total_segments,
# description:
# Segmentation algorithm type as available in DICOM SegmentAlgorithmType
string_agg(DISTINCT (SegmentAlgorithmType), ',' ORDER BY SegmentAlgorithmType) AS AlgorithmType,
# description:
# Segmentation algorithm name as available in DICOM SegmentAlgorithmName
string_agg(DISTINCT (SegmentAlgorithmName), ',' ORDER BY SegmentAlgorithmName) AS AlgorithmName,
# description:
# SeriesInstanceUID of the referenced image series that the segmentation applies to
any_value(segmentations.segmented_SeriesInstanceUID)
AS segmented_SeriesInstanceUID,
# description:
# Array of distinct CodeMeaning values from SegmentedPropertyCategoryCodeSequence across all segments
# in the series, representing the broad category of the segmented property as defined in DICOM PS3.3 C.8.20.2,
# e.g., ["Anatomical Structure"], ["Morphologically Altered Structure"]
ARRAY_AGG(DISTINCT SegmentedPropertyCategory.CodeMeaning IGNORE NULLS ORDER BY SegmentedPropertyCategory.CodeMeaning)
AS SegmentedPropertyCategory_CodeMeanings,
# description:
# Array of distinct CodeMeaning values from SegmentedPropertyTypeCodeSequence across all segments
# in the series, representing the specific type of the segmented property as defined in DICOM PS3.3 C.8.20.2,
# e.g., ["Liver", "Kidney", "Spleen"]
ARRAY_AGG(DISTINCT SegmentedPropertyType.CodeMeaning IGNORE NULLS ORDER BY SegmentedPropertyType.CodeMeaning)
AS SegmentedPropertyType_CodeMeanings,
# description:
# Array of distinct CodeMeaning values from AnatomicRegionSequence across all segments
# in the series, representing the anatomic location of the segmented structure as defined in DICOM PS3.3 C.8.20.2,
# e.g., ["Abdomen"], ["Thorax", "Head"]
ARRAY_AGG(DISTINCT AnatomicRegion.CodeMeaning IGNORE NULLS ORDER BY AnatomicRegion.CodeMeaning)
AS AnatomicRegion_CodeMeanings
FROM segmentations
GROUP BY SeriesInstanceUID
ORDER BY SegmentationType DESC, AlgorithmType, AlgorithmName