-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurvstat_webservice.R
executable file
·371 lines (314 loc) · 17.6 KB
/
survstat_webservice.R
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#' method to recieve the xml from the WebService
#'
#' @param body_ the xml request
#' @param service_ the WebService method
#'
#' @return something XMLish
#'
getXMLFromWebService <- function(body_,service_){
# require(RCurl)
# require(XML)
# require(stringi)
# URL to the WebService
webServiceUrl <- "https://tools.rki.de/SurvStat/SurvStatWebService.svc"
bodyLength <- nchar( stri_enc_toutf8(body_, is_unknown_8bit = FALSE, validate = FALSE))
# Header Fields for the post
headerFields =
c(Accept = "text/xml",
Accept = "multipart/*",
'Content-Length' = bodyLength,
'Content-Type' = "application/soap+xml; charset=utf-8", # Extern tools.rki.de
SOAPaction = paste0("http://tools.rki.de/SurvStat/SurvStatWebService/", service_))
#Gatherer Object to recieve response xml
reader = basicTextGatherer()
# print("Header:")
# dput(headerFields)
# print("Body:")
# dput(body_)
# performe the request
result <- curlPerform(url = webServiceUrl,
httpheader = headerFields,
postfields = body_,
writefunction = reader$update,
verbose = TRUE
)
# parse the response xml
doc_x <- xmlParse(reader$value(), encoding="UTF-8")
#return xml
return(doc_x)
}
#' Utilities for accessing data
#'
#' @param cube
#' @param language
#' @param filter
#'
#' @return something XMLish
#'
getHierarchyMembers <- function(cube, language, filter){
#require(XML)
#require(dplyr)
# Select HierarchyId from the first row
hId <- getHierarchies(cube, language) %>%
filter(HierarchyCaption==filter) %>%
dplyr::select(HierarchyId) %>%
unlist()
# XML Request Body
body = paste0('<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:sur="http://tools.rki.de/SurvStat/" xmlns:rki="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:Action>http://tools.rki.de/SurvStat/SurvStatWebService/GetAllHierarchyMembers</wsa:Action>
<wsa:To>https://tools.rki.de/SurvStat/SurvStatWebService.svc</wsa:To>
</soap:Header>
<soap:Body>
<sur:GetAllHierarchyMembers>
<sur:request>
<rki:Cube>', cube,'</rki:Cube>
<rki:HierarchyId>',hId,'</rki:HierarchyId>
<rki:Language>', language,'</rki:Language>
</sur:request>
</sur:GetAllHierarchyMembers>
</soap:Body>
</soap:Envelope>')
# XML Request Body
service_ <- 'GetAllHierarchyMembers'
# WebService method
AllHierarchyMembers <- getXMLFromWebService(body,service_)
# Get NodeSet of all Hierarchy Members Nodes in the response xml
hierarchyMembers <- getNodeSet(AllHierarchyMembers, "//a:HierarchyMember", namespaces = c("a" = "http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx"))
# Parse the XML Result to DataFrame
HierarchyMemberDataFrame <-xmlToDataFrame(hierarchyMembers)
return(HierarchyMemberDataFrame)
}
#' More utilities
#'
#' @param cube
#' @param language
#'
#' @return something XMLish
getHierarchies <- function(cube, language){
#require(XML)
#require(dplyr)
#language = 'German' #'German'/'English' (Case Sensitive!)
#cube = 'SurvStat' #'SurvStat' (Case Sensitive!)
# XML Request Body -------------EXTERN
body =paste0('<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:sur="http://tools.rki.de/SurvStat/" xmlns:rki="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:Action>http://tools.rki.de/SurvStat/SurvStatWebService/GetAllDimensions</wsa:Action>
<wsa:To>https://tools.rki.de/SurvStat/SurvStatWebService.svc</wsa:To>
</soap:Header>
<soap:Body>
<sur:GetAllDimensions>
<sur:request>
<rki:Cube>', cube,'</rki:Cube>
<rki:Language>', language,'</rki:Language>
</sur:request>
</sur:GetAllDimensions>
</soap:Body>
</soap:Envelope>')
#print(body)
# WebService method
service_ <- 'GetAllDimensions'
# Call getXMLFromWebService to get response xml
AllDimensions <- getXMLFromWebService(body,service_)
# Get NodeSet of all Hierarchy Nodes in the response xml
idNodes <- getNodeSet(AllDimensions, "//b:Hierarchy", namespaces = c("b" = "http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx"))
# Build Hierarchy DataFrame
HierarchyDataFrame <- do.call(rbind.data.frame, lapply(idNodes, function(x) {
# Get the Children Nodes of the Hierarchy Node (These are its properties)
children <- xmlChildren(x)
# Get the parent of the parent Node (Parentò because every Hierarchy is in in Hierachies Node)
parent <- xmlParent(xmlParent(x))
# Initialize the result List
result=list()
# Fill the ResultList with the Hierarchy Child "Properties"
result$HierarchyId <-xmlValue(children$Id)
result$HierarchyCaption <- xmlValue(children$Caption)
result$HierarchyDescription <-xmlValue(children$Description)
result$HierarchySelectMax <-xmlValue(children$SelectMax)
result$HierarchySelectMin <-xmlValue(children$SelectMin)
result$HierarchySort <-xmlValue(children$Sort)
#If Parent is a Hierarchy --> Set den Parent Hierarchy Id
result$HierarchyParentId = ifelse(xmlName(parent)=="Hierarchy",xmlValue(xmlChildren(parent)$Id),-1)
#Loop until the Parent is the Dimension and set it as Dimension Parent
while(xmlName(parent) != "Dimension"){
parent <- xmlParent(parent)
}
#Get the Children Nodes (Its Properties) of the Dimenionnode
dimensionChilds<- xmlChildren(parent)
# Fill the ResultList with the Dimension Child "Properties"
result$DimensionId <- xmlValue(dimensionChilds$Id)
result$DimensionCaption <- xmlValue(dimensionChilds$Caption)
result$DimensionDescription <- xmlValue(dimensionChilds$Description)
result$DimensionGroup <- xmlValue(dimensionChilds$Group)
result$DimensionSort <- xmlValue(dimensionChilds$Sort)
# Return the result List
return(result)
}))
return(HierarchyDataFrame)
}
#' more utilities
#'
#' @param cube
#' @param language
#' @param hierarchy
#' @param facet
#' @param filter
#' @param filterValue
#' @param filter2
#' @param filterValue2
#'
#' @return a data.frame
getOlapData <- function(cube, language, hierarchy, facet, filter, filterValue, filter2, filterValue2){
# require(XML)
# require(data.table)
# require(dplyr)
column <- getHierarchies(cube, language) %>% filter(HierarchyCaption==hierarchy) %>% select(HierarchyId) %>% unlist() %>% as.character()
row <- getHierarchies(cube, language) %>% filter(HierarchyCaption==facet) %>% select(HierarchyId) %>% unlist() %>% as.character()
keyDim <- getHierarchies(cube, language) %>% filter(HierarchyCaption==filter) %>% select(DimensionId) %>% unlist() %>% as.character()
keyHier <- getHierarchies(cube, language) %>% filter(HierarchyCaption==filter) %>% select(HierarchyId) %>% unlist() %>% as.character()
value <- getHierarchyMembers(cube, language, filter) %>% filter(Caption==filterValue) %>% select(Id) %>% unlist() %>% as.character() %>% gsub('&', '&', .)
# catch case where due to due to German special characters things don't work:
if(length(value) == 0){ # in this case the filter() command returns no match
# and we try to construct the "value" object ourselves:
value <- paste0("[PathogenOut].[KategorieNz].[Krankheit DE].&[", filterValue, "]")
message("Maching from Caption to Id did not work, trying to guess Id...")
}
keyDim2 <- getHierarchies(cube, language) %>% filter(HierarchyCaption==filter2) %>% select(DimensionId) %>% unlist() %>% as.character()
keyHier2 <- getHierarchies(cube, language) %>% filter(HierarchyCaption==filter2) %>% select(HierarchyId) %>% unlist() %>% as.character()
value2 <- getHierarchyMembers(cube, language, filter2) %>% filter(Caption==filterValue2) %>% select(Id) %>% unlist() %>% as.character() %>% gsub('&', '&', .)
body = paste0('<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://tools.rki.de/SurvStat/" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:q1="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q2="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q3="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q4="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q5="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q6="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q7="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q8="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q9="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q10="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q11="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q12="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q13="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q14="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q15="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:q16="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx" xmlns:ser="http://schemas.microsoft.com/2003/10/Serialization/">
<soap:Header>
<wsa:To xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns="http://www.w3.org/2005/08/addressing">https://tools.rki.de/SurvStat/SurvStatWebService.svc</wsa:To>
<wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns="http://www.w3.org/2005/08/addressing">http://tools.rki.de/SurvStat/SurvStatWebService/GetOlapResultData</wsa:Action>
</soap:Header>
<soap:Body>
<GetOlapResultData xmlns="http://tools.rki.de/SurvStat/">
<request>
<q13:ColumnHierarchy xmlns:q13="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">',column,'</q13:ColumnHierarchy>
<q13:Cube xmlns:q13="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">SurvStat</q13:Cube>
<q13:HierarchyFilters xmlns:q13="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">
<q13:KeyValueOfFilterCollectionKeyFilterMemberCollectionb2rWaiIW>
<q13:Key>
<q13:DimensionId>', keyDim ,'</q13:DimensionId>
<q13:HierarchyId>', keyHier ,'</q13:HierarchyId>
</q13:Key>
<q13:Value>
<q13:string>', value ,'</q13:string>
</q13:Value>
</q13:KeyValueOfFilterCollectionKeyFilterMemberCollectionb2rWaiIW>
<q13:KeyValueOfFilterCollectionKeyFilterMemberCollectionb2rWaiIW>
<q13:Key>
<q13:DimensionId>', keyDim2 ,'</q13:DimensionId>
<q13:HierarchyId>', keyHier2 ,'</q13:HierarchyId>
</q13:Key>
<q13:Value>
<q13:string>', value2 ,'</q13:string>
</q13:Value>
</q13:KeyValueOfFilterCollectionKeyFilterMemberCollectionb2rWaiIW>
</q13:HierarchyFilters>
<q13:IncludeNullColumns xmlns:q13="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">false</q13:IncludeNullColumns>
<q13:IncludeNullRows xmlns:q13="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">false</q13:IncludeNullRows>
<q13:IncludeTotalColumn xmlns:q13="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">false</q13:IncludeTotalColumn>
<q13:IncludeTotalRow xmlns:q13="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">false</q13:IncludeTotalRow><q13:Language xmlns:q13="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">German</q13:Language>
<q13:Measure xmlns:q13="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">[Measures].[FallCount_71]</q13:Measure>
<q13:RowHierarchy xmlns:q13="http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx">',row,'</q13:RowHierarchy>
</request>
</GetOlapResultData>
</soap:Body>
</soap:Envelope>')
# WebService method
service_ <- 'GetOlapResultData'
AllData <- getXMLFromWebService(body,service_)
dataNodeColumnSet <- getNodeSet(AllData, "//b:Columns/b:QueryResultColumn", namespaces = c("b" = "http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx"))
columnCaptions <- lapply(dataNodeColumnSet, function(x) {
columnCaption <- xmlValue(getNodeSet(x, "./b:Caption", namespaces = c("b" = "http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx"))[[1]])
})
dataNodeRowSet <- getNodeSet(AllData, "//b:QueryResultRow", namespaces = c("b" = "http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx"))
rowDataSet <- do.call(rbind.data.frame, lapply(dataNodeRowSet, function(x) {
vals <- getNodeSet(x, "./b:Values/b:string", namespaces = c("b" = "http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx"))
lapply(vals, function(x) { as.numeric(gsub("\\.", "", xmlValue(x)))})
}))
rowCaptions <- getNodeSet(AllData, "//b:QueryResults/b:QueryResultRow/b:Caption", namespaces = c("b" = "http://schemas.datacontract.org/2004/07/Rki.SurvStat.WebService.Contracts.Mdx"))
rowCaptions <-lapply(rowCaptions, function(x) {xmlValue(x)})
row.names(rowDataSet) <-rowCaptions
trowSet <- as.data.frame(t(rowDataSet))
rownames(trowSet) <- 1:nrow(trowSet)
columnCaptionsT <- data.frame(t(data.frame(columnCaptions)))
names(columnCaptionsT) <- "Categories"
finalFrame <- data.frame(cbind(columnCaptionsT,trowSet))
rownames(finalFrame) <- 1:nrow(finalFrame)
setDT(finalFrame)
data.m <- data.table::melt(finalFrame, id.vars='Categories')
return(data.m)
}
#' retrieve available diseases
#'
#' @return data.frame with available diseases
#' @export
#'
get_diseases <- function(){
# require(dplyr)
cube <- "SurvStat"
language <- "English"
filter <- "Disease"
return(getHierarchyMembers(cube, language, filter) %>% dplyr::select(Caption))
}
#' Replace German special characters in a name of a disease:
handle_special_characters <- function(disease){
to_replace <- matrix(c("Ä", "Ä",
"Ö", "Ö",
"Ü", "Ü",
"ä", "ä",
"ö", "ö",
"ü", "ü",
"ß", "ß"), ncol = 2, byrow = TRUE)
for(i in 1:nrow(to_replace)){
disease <- gsub(to_replace[i, 1], to_replace[i, 2], disease)
}
return(disease)
}
#' Download and format a weekly timeseries from one year to data.frame
#'
#' @description API only allows one year downloaded at a time (max rows per query).
#' So this function helps split queries up.
#'
#' @param disease which disease to retrieve data for
#' @param year which year to retrieve data for
#' @param region_level which level to retrieve data for
#'
#' @return a tibble with data
#' @export
#'
get_weekly_timeseries_one_yr <- function(disease = "Noroviral gastroenteritis", year = "2019", region_level = "State"){
# require(ISOweek)
# require(dplyr)
cube <- "SurvStat"
language <- "English"
hierarchy <- "Week of notification"
filter <- "Disease"
filterValue <- handle_special_characters(disease)
filter2 <- "Year of notification"
filterValue2 <- year
facet <- region_level
data <- getOlapData(cube, language, hierarchy, facet, filter, filterValue, filter2, filterValue2)
string_season <- year # substr(data$Categories,1,4)
string_week <- data$Categories # substr(data$Categories,11,12)
data$week <- as.numeric(string_week)
data$year <- as.numeric(string_season)
data$Categories <- NULL
# rename columns in more meaningful way:
# colnames(data)[colnames(data) == "Categories"] <- "time_string"
colnames(data)[colnames(data) == "variable"] <- "stratum"
return(as_tibble(data))
}
#' Download and process multiple years of data
#'
#' @param disease which disease to retrieve data for
#' @param years which years to retrieve data for
#' @param region_level which level to retrieve data for
#'
#' @return a tibble with data
#' @export
get_weekly_timeseries <- function(disease = "Noroviral gastroenteritis", years, region_level="State") {
return(do.call("rbind", lapply(years, FUN=function(x) get_weekly_timeseries_one_yr(disease=disease, year=x, region_level=region_level))))
}