|
a |
|
b/archives/RadETL/R/DicomRDS.R |
|
|
1 |
#################################### DicomRDS Class ############################################# |
|
|
2 |
#' DicomRDS Class |
|
|
3 |
#' |
|
|
4 |
#' This class is a class that imports various metadata by reading an RDS file containing DICOM information. |
|
|
5 |
#' There are basically functions that import data related to Radiology CDM. |
|
|
6 |
#' |
|
|
7 |
#' @param data Data frame imported from DICOM RDS file |
|
|
8 |
#' @seealso https://github.com/OHDSI/Radiology-CDM/wiki |
|
|
9 |
#' @author Neon K.I.D |
|
|
10 |
#' @example Examples/DicomRDS_Ex.R |
|
|
11 |
#' @export |
|
|
12 |
DicomRDS <- R6::R6Class(classname = "DicomRDS", |
|
|
13 |
private = list( |
|
|
14 |
# Parameter name is TagName |
|
|
15 |
getTagValue = function(name) { |
|
|
16 |
tryCatch({ |
|
|
17 |
res <- self$data$value[which(ifelse(self$data$name %in% name, TRUE, FALSE))] |
|
|
18 |
if(is.empty(x = res)) |
|
|
19 |
res <- NA |
|
|
20 |
}, error = function(e) { |
|
|
21 |
res <- NA |
|
|
22 |
assign("res", res, envir = .GlobalEnv) |
|
|
23 |
}) |
|
|
24 |
return(res) |
|
|
25 |
}, |
|
|
26 |
isValidTag = function(name) if(is.boolean(which(ifelse(self$data$name %in% name, TRUE, FALSE)) == 0)) TRUE else FALSE, |
|
|
27 |
getTagLength = function(name) { |
|
|
28 |
tryCatch({ |
|
|
29 |
len <- as.numeric(self$data$length[which(ifelse(self$data$name %in% name, TRUE, FALSE))]) |
|
|
30 |
if(is.empty(x = len)) |
|
|
31 |
len <- NA |
|
|
32 |
}, error = function(e) { |
|
|
33 |
len <- NA |
|
|
34 |
assign("len", len, envir = .GlobalEnv) |
|
|
35 |
}) |
|
|
36 |
return(len) |
|
|
37 |
} |
|
|
38 |
), |
|
|
39 |
|
|
|
40 |
public = list( |
|
|
41 |
data = NULL, |
|
|
42 |
idp = NULL, |
|
|
43 |
initialize = function(data, idp = 2) { |
|
|
44 |
library(stringr) |
|
|
45 |
self$data = data |
|
|
46 |
self$idp = idp |
|
|
47 |
}, |
|
|
48 |
|
|
|
49 |
# isContrast for Brain-CT |
|
|
50 |
isPost4BrainCT = function() { |
|
|
51 |
tryCatch({ |
|
|
52 |
if(private$isValidTag(name = "ContrastBolusAgent")) |
|
|
53 |
return(TRUE) |
|
|
54 |
else if(private$isValidTag(name = "PhotometricInterpretation")) { |
|
|
55 |
colorVal <- private$getTagValue(name = "PhotometricInterpretation") |
|
|
56 |
if(pmatch(x = colorVal, "RGB", nomatch = FALSE) == 1) |
|
|
57 |
return(TRUE) |
|
|
58 |
} |
|
|
59 |
if(private$isValidTag(name = "ContrastBolusRoute")) |
|
|
60 |
return(TRUE) |
|
|
61 |
else |
|
|
62 |
return(FALSE) |
|
|
63 |
}, error = function(e) { |
|
|
64 |
return(FALSE) |
|
|
65 |
}) |
|
|
66 |
}, |
|
|
67 |
|
|
|
68 |
# Creation Radiology ID |
|
|
69 |
# createOccurrenceID = function() { |
|
|
70 |
# seriesID <- unlist(strsplit(x = private$getTagValue("SeriesInstanceUID"), split = '[.]')) |
|
|
71 |
# studyID <- unlist(strsplit(x = private$getTagValue("StudyInstanceUID"), split = '[.]')) |
|
|
72 |
# |
|
|
73 |
# x <- seriesID[length(seriesID)] |
|
|
74 |
# y <- as.numeric( |
|
|
75 |
# substr(x = studyID[length(studyID)], start = nchar(studyID[length(studyID)]) - 4, stop = nchar(studyID[length(studyID)])) |
|
|
76 |
# ) |
|
|
77 |
# |
|
|
78 |
# directoryID <- self$getDirectoryID() |
|
|
79 |
# i <- directoryID |
|
|
80 |
# z <- as.numeric(substr(x = directoryID, start = nchar(directoryID) - 2, stop = nchar(directoryID))) |
|
|
81 |
# |
|
|
82 |
# # Numbering... |
|
|
83 |
# if(nchar(x) > 7) |
|
|
84 |
# x <- as.numeric(substr(x = x, start = nchar(x) - 5, stop = nchar(x))) |
|
|
85 |
# else |
|
|
86 |
# x <- as.numeric(x) |
|
|
87 |
# |
|
|
88 |
# max.val <- i + abs(y - nchar(trunc(x)) - x) |
|
|
89 |
# count <- nchar(trunc(max.val)) |
|
|
90 |
# |
|
|
91 |
# size <- paste("%0", count, "d", sep = "") |
|
|
92 |
# set.seed(i) |
|
|
93 |
# # lets <- toupper(sample(letters,x, replace = TRUE)) |
|
|
94 |
# nums <- sprintf(ifelse(length(size) > 1, size[1], size), sample(1:max.val)[1:nchar(trunc(z))]) |
|
|
95 |
# res <- paste(nums, sep = "") |
|
|
96 |
# return(sum(as.numeric(res))) |
|
|
97 |
# }, |
|
|
98 |
|
|
|
99 |
getStudyDate = function() return(private$getTagValue("StudyDate")), |
|
|
100 |
getStudyTime = function() return(private$getTagValue("StudyTime")), |
|
|
101 |
|
|
|
102 |
# Get Image shot dateTime |
|
|
103 |
getStudyDateTime = function() { |
|
|
104 |
studyDate <- private$getTagValue("StudyDate") |
|
|
105 |
studyTime <- private$getTagValue("StudyTime") |
|
|
106 |
|
|
|
107 |
return(getDateTime(studyDate, studyTime)) |
|
|
108 |
}, |
|
|
109 |
|
|
|
110 |
getDuringTime = function(studyDateTime) { |
|
|
111 |
tryCatch({ |
|
|
112 |
if(private$isValidTag("AcquisitionDate")) { |
|
|
113 |
acqDate <- private$getTagValue("AcquisitionDate") |
|
|
114 |
acqTime <- private$getTagValue("AcquisitionTime") |
|
|
115 |
acqDatetime <- getDateTime(acqDate, acqTime) |
|
|
116 |
duringTime <- getDiffTime(studyDateTime, acqDatetime) |
|
|
117 |
return(duringTime) |
|
|
118 |
} |
|
|
119 |
}, error = function(e) { |
|
|
120 |
return(NA) |
|
|
121 |
}) |
|
|
122 |
}, |
|
|
123 |
|
|
|
124 |
getPatientID = function() return(private$getTagValue("PatientID")), |
|
|
125 |
getDeviceID = function() return(private$getTagValue("DeviceSerialNumber")), |
|
|
126 |
getModality = function() { |
|
|
127 |
modal <- private$getTagValue("Modality") |
|
|
128 |
if(!is.na(modal) && !is.null(modal)) switch(modal, CT = 10321, MR = 10312, OT = 49585) |
|
|
129 |
else NA |
|
|
130 |
}, |
|
|
131 |
getOrientation = function() return(private$getTagValue("PatientOrientation")), |
|
|
132 |
getPosition = function() { |
|
|
133 |
pos <- private$getTagValue("PatientPosition") |
|
|
134 |
if(!is.na(pos) && !is.null(pos)) switch(pos, HFS = 10421, pos) |
|
|
135 |
else NA |
|
|
136 |
}, |
|
|
137 |
|
|
|
138 |
getComment = function() return(private$getTagValue("ImageComments")), |
|
|
139 |
getDosageunit = function(modality) if(modality == 10312) return("Tesla") else return("kVp"), |
|
|
140 |
getDosage = function(dosageUnit) { |
|
|
141 |
sha = private$getTagValue(dosageUnit) |
|
|
142 |
if(is.empty(sha)) return(NA) else return(sha) |
|
|
143 |
}, |
|
|
144 |
|
|
|
145 |
getSourceID = function() return(private$getTagValue("SOPInstanceUID")), |
|
|
146 |
getPersonID = function() return(private$getTagValue("PatientID")), |
|
|
147 |
getStudyID = function() return(private$getTagValue("StudyID")), |
|
|
148 |
getDirectoryID = function() { |
|
|
149 |
sp <- strsplit(as.character(self$data$path[length(self$data$path)]), '/') |
|
|
150 |
shortPath <- tail(x = unlist(sp), -1) |
|
|
151 |
nVec <- unlist(stringr::str_extract_all(string = shortPath[self$idp], pattern = "\\-*\\d+\\.*\\d*")) |
|
|
152 |
num <- ifelse(length(nVec) > 1, Reduce(pasteNormal, c(abs(as.numeric(nVec[1])), abs(as.numeric(nVec[2])))), nVec) |
|
|
153 |
return(as.numeric(num)) |
|
|
154 |
}, |
|
|
155 |
getImageType = function() { |
|
|
156 |
exType <- private$getTagValue("ImageType") |
|
|
157 |
if(!is.na(exType)) { |
|
|
158 |
if(stringr::str_detect(string = exType, pattern = "ORIGINAL")) |
|
|
159 |
imType <- "PRIMARY" |
|
|
160 |
else if(is.boolean(str_detect(exType, "DERIVED"))) |
|
|
161 |
imType <- "SECONDARY" |
|
|
162 |
} else { |
|
|
163 |
imType <- exType |
|
|
164 |
} |
|
|
165 |
return(imType) |
|
|
166 |
}, |
|
|
167 |
|
|
|
168 |
getThickness = function() return(private$getTagValue(name = "SliceThickness")), |
|
|
169 |
getImgRows = function() return(private$getTagValue(name = "Rows")), |
|
|
170 |
getImgCols = function() return(private$getTagValue(name = "Columns")), |
|
|
171 |
getWindowCenter = function() return(private$getTagValue(name = "WindowCenter")), |
|
|
172 |
getWindowWidth = function() return(private$getTagValue(name = "WindowWidth")), |
|
|
173 |
|
|
|
174 |
isPixelData = function() return(private$getTagLength("PixelData") != -1), |
|
|
175 |
|
|
|
176 |
# free func in C... |
|
|
177 |
# finalize method in Java... |
|
|
178 |
finalize = function() {} |
|
|
179 |
) |
|
|
180 |
) |