-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSVASSS.r
449 lines (378 loc) · 14.2 KB
/
SVASSS.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
list.of.packages <- c("gdata", "timeDate","surveillance","abind","qcc","caTools","tseries","np",
"fitdistrplus","pscl","sendmailR","rgdal","ggmap","mapproj","maps","stringr","ISOweek", "devtools",
"lubridate","dplyr")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
require(gdata) #install.packages("gdata")
require(timeDate) #install.packages("timeDate")
require(surveillance) #install.packages("surveillance")
require(abind) #install.packages("abind")
require(qcc) #install.packages("qcc")
require(caTools) #install.packages("caTools")
require(tseries) #install.packages("tseries")
require(np) #install.packages("np")
require(fitdistrplus) #install.packages("fitdistrplus")
require(pscl) #install.packages("pscl")
require(sendmailR) #install.packages("mail")
require(rgdal)
require(ggmap)
require(mapproj)
require(maps)
require(stringr)
require(ISOweek)
require(devtools)
require(lubridate)
require(data.table)
require(dplyr)
rm(list.of.packages)
rm(new.packages)
install_github("nandadorea/vetsyn")
require(vetsyn)
#rm(list=ls())
wd.running <- "I:/ESS/SVASSS2/SVASSS/"
setwd(wd.running)
source("Functions.r",local=TRUE,encoding="native.enc")
source("Definitions.r",local=TRUE,encoding="native.enc")
#add something again for testing
# new data in ----
current.SVA.data <- read.csv(paste0(wd.sourcefiles,"classified_data_SVA.csv"), as.is=TRUE, header=T, sep=";", encoding="UTF-8")
source("SVA.data_classification_fixes.r",local=TRUE)
current.SVA.data <- current.SVA.data[which(current.SVA.data$active2==0),]
#current.CDB.data <- read.csv(paste0(wd.sourcefiles,"classified_data_CDB.csv"), as.is=TRUE, header=T, sep=";")
#current.SJV.data <- read.csv2(paste0(wd.sourcefiles,"classified_data_SJV.csv"))
PPN.database <- read.csv2(paste0(wd.sourcefiles,"ppn_alla.csv"), as.is=TRUE)
PPN.database$X <- as.numeric(PPN.database$X)
PPN.database$Y <- as.numeric(PPN.database$Y)
load(paste0(wd.history,"classified.species.data.RData"))
# historical data in ----
for(species in species.acronyms){
eval(parse(text=paste0("load('",wd.history,species,".RData')")))
}
daily.object <- list(CAT=CAT.daily,
BOV=BOV.daily,
DOG=DOG.daily,
ENV=ENV.daily,
FOD=FOD.daily,
FSK=FSK.daily,
EQU=EQU.daily,
AVI=AVI.daily,
SRU=SRU.daily,
SWI=SWI.daily,
VLT=VLT.daily)
weekly.object <- list(CAT=CAT.weekly,
BOV=BOV.weekly,
DOG=DOG.weekly,
ENV=ENV.weekly,
FOD=FOD.weekly,
FSK=FSK.weekly,
EQU=EQU.weekly,
AVI=AVI.weekly,
SRU=SRU.weekly,
SWI=SWI.weekly,
VLT=VLT.weekly)
svaga.object <- list(CAT=CAT.svaga,
BOV=BOV.svaga,
DOG=DOG.svaga,
ENV=ENV.svaga,
FOD=FOD.svaga,
FSK=FSK.svaga,
EQU=EQU.svaga,
AVI=AVI.svaga,
SRU=SRU.svaga,
SWI=SWI.svaga,
VLT=VLT.svaga)
# removed 2018-10-08 - already saving all classified data, which is basically what this is.
# non.svaga.object <- list(CAT=CAT.non.svaga,
# BOV=BOV.non.svaga,
# DOG=DOG.non.svaga,
# ENV=ENV.non.svaga,
# FOD=FOD.non.svaga,
# FSK=FSK.non.svaga,
# EQU=EQU.non.svaga,
# AVI=AVI.non.svaga,
# SRU=SRU.non.svaga,
# SWI=SWI.non.svaga,
# VLT=VLT.non.svaga)
# define border between historical and new data ----
new.data.start <- min(as.Date(current.SVA.data$ANKOMSTDATUM,format="%d/%m/%Y"))
new.data.end <- max(as.Date(current.SVA.data$ANKOMSTDATUM,format="%d/%m/%Y"))
next.monday <- nextweekday(new.data.start,2)
#make sure the new data always starts on a Monday so that it has full weeks:
current.SVA.data <- current.SVA.data[as.Date(current.SVA.data$ANKOMSTDATUM,format="%d/%m/%Y")>=next.monday,]
new.data.start <- next.monday
last.historical.row.5days <- (which(BOV.daily@dates[,1]==new.data.start)-1)
last.historical.row.week <- (which(as.character(BOV.weekly@dates[,1])==as.character(date2ISOweek(new.data.start)))-1)
today <- strptime(as.character(new.data.end), format = "%Y-%m-%d")
#on 2018-05-23 SVASSS was converted to WEEKLY only and always
#daily analysis are still ran and recorded, so that we can always revert,
#but they generate no emails or html output
weekly <- TRUE
#weekly <- FALSE
#if(today$wday==5|PlotDaily==1)(weekly<-TRUE)
# create alarm objects ----
true.alarms.daily <- list(
CAT=NA,
BOV=NA,
DOG=NA,
ENV=NA,
FOD=NA,
FSK=NA,
EQU=NA,
AVI=NA,
SRU=NA,
SWI=NA,
VLT=NA
)
scnd.alarms.daily <- true.alarms.daily
true.alarms.weekly <- true.alarms.daily
scnd.alarms.weekly <- true.alarms.daily
# species loops ----
source("BOV.r",local=TRUE,encoding="native.enc")
source("AVI.r",local=TRUE,encoding="native.enc")
source("CAT.r",local=TRUE,encoding="native.enc")
source("EQU.r",local=TRUE,encoding="native.enc")
source("DOG.r",local=TRUE,encoding="native.enc")
source("SWI.r",local=TRUE,encoding="native.enc")
source("SRU.r",local=TRUE,encoding="native.enc")
source("VLT.r",local=TRUE,encoding="native.enc")
source("ENV.r",local=TRUE,encoding="native.enc")
source("FOD.r",local=TRUE,encoding="native.enc")
source("FSK.r",local=TRUE,encoding="native.enc")
# saving data for apps ----
# load(paste0(wd.sourcefiles,"svala.data.RData"))
# #svala.data.dates
# #current.data.dates
# #min.date.current
# #max.data.current
#
# keep.svala <- which((svala.data.dates<min(as.Date(current.SVA.data$ANKOMSTDATUM,format="%d/%m/%Y")))&(svala.data.dates>(max(as.Date(current.SVA.data$ANKOMSTDATUM,format="%d/%m/%Y"))-550)))
# svala.data <- rbind(svala.data[keep.svala,],
# current.SVA.data)
# svala.data.dates <- as.Date(svala.data$ANKOMSTDATUM, format = "%d/%m/%Y", origin="01/01/1970")
# save(svala.data,svala.data.dates,file=paste0(wd.sourcefiles,"/svala.data.RData"))
#
#on 2018-08-09 substituted by saving data after processing within each species.
classified.species.data <- classified.species.data[
as.Date(classified.species.data$ANKOMSTDATUM,format="%d/%m/%Y")>(max(
as.Date(classified.species.data$ANKOMSTDATUM,format="%d/%m/%Y"))-366),]
display.data <- classified.species.data[,c("UPPDRAG","ANKOMSTDATUM","ÖVERORDNATUPPDRAG","ÖVERORDNADEUPPDRAG","PROVTAGNINGSORSAK","INSÄNTMATERIAL",
"DJURSLAG","DIAGNOSER","RESULTATUNDERSÖKNING","RESULTATANALYS",
"AGENS","PÅVISAD","ANALYSBESKRIVNING","ANALYSMATERIAL","UNDERSÖKNINGBESKRIVNING","MATERIAL",
"PPN_original","CITY","SYNDROMIC","SPECIES")]
display.data <- fix.encoding(display.data)
week <- date2ISOweek(as.Date(display.data$ANKOMSTDATUM,format="%d/%m/%Y"))
week <- substr(week,1,8)
display.data <- cbind(display.data,week)
columns.display.data <- eliminate.swedish.the.wrong.way(colnames(display.data))
colnames(display.data) <- columns.display.data
display.data$PAVISAD[display.data$PAVISAD==""]<-"_No SVAGA information"
week.options <- unique(as.character(display.data$week))
pavisad.options <- unique(as.character(display.data$PAVISAD))
save(columns.display.data,week.options,pavisad.options,file=paste0(wd.history,"/menu.summaries.RData"))
save(classified.species.data,file=paste0(wd.history,"/classified.species.data.RData"))
save(display.data,file=paste0(wd.history,"/display.data.RData"))
# emails ----
#on 2018-05-23 SVASSS was converted to WEEKLY only and always
#daily analysis are still ran and recorded, so that we can always revert,
#but they generate no emails or html output
# status.true <- sapply(true.alarms.daily,sum,na.rm=TRUE)
# status.scnd <- sapply(scnd.alarms.daily,sum,na.rm=TRUE)
#
# if(weekly){
# status.true <- status.true + sapply(true.alarms.weekly,sum,na.rm=TRUE)
# status.scnd <- status.scnd + sapply(scnd.alarms.weekly,sum,na.rm=TRUE)
# }
status.true <- sapply(true.alarms.weekly,sum,na.rm=TRUE)
status.scnd <- sapply(scnd.alarms.weekly,sum,na.rm=TRUE)
save(status.true,status.scnd,file=paste0(wd.history,"/status.RData"))
body <- list("please visit <http://ubuntu1:3838/sample-apps/SVASSS/>")
Emaillist<-"<ESS-alla@sva.se>"
#Emaillist<-"<fernanda.dorea@sva.se>"
#Emaillist<-EmailRecipient1
if (sum(status.true,na.rm=TRUE)>0)({
from <- "<fernanda.dorea@sva.se>"
to <- Emaillist
subject <- paste((Sys.Date()-1),"TRUE-ALARM: There are TRUE alarms today",sep=",")
sendmail(from, to, subject, body,control=list(smtpServer="smtp1.sva.se"))
})
if (sum(status.true,na.rm=TRUE)==0&sum(status.scnd,na.rm=TRUE)>0)({
from <- "<fernanda.dorea@sva.se>"
to <- Emaillist
subject <- paste((Sys.Date()-1),"sec.alarm: There are secondary alarms today",sep=",")
sendmail(from, to, subject, body,control=list(smtpServer="smtp1.sva.se"))
})
if (sum(status.true,na.rm=TRUE)==0&sum(status.scnd,na.rm=TRUE)==0)({
from <- "<fernanda.dorea@sva.se>"
to <- "<fernanda.dorea@sva.se>"
subject <- paste((Sys.Date()-1),"calculations finished",sep=",")
msg="No alarms today"
sendmail(from, to, subject,msg, control=list(smtpServer="smtp1.sva.se"))
})
# index/main html ----
#2018-09-11 deactivated html, shiny workign now
# setwd(wd.html)
#
# html <- file("index.html", "w+")
#
# cat("<html>\n", file=html)
# cat("<head>\n", file=html)
# cat(sprintf("<title>%s</title>\n", "SVASS"), file=html)
# cat("</head>\n", file=html)
#
# cat("<frameset cols=\"150px,*\">\n", file=html)
# cat("<frame noresize=\"noresize\" src=\"nav.html\" name=\"nav\"/>\n", file=html)
# cat("<frame noresize=\"noresize\" src=\"main.html\" name=\"main\"/>\n", file=html)
# cat("</frameset>\n", file=html)
#
# cat("</html>\n", file=html)
#
# close(html)
#
#
#
# html <- file("main.html", "w+")
#
# cat("<html>\n", file=html)
# cat("<head>\n", file=html)
# cat(sprintf("<title>%s</title>\n", "SVASS main page"), file=html)
# cat("</head>\n", file=html)
#
# cat("<body>\n", file=html)
#
# cat(sprintf('<h1 align="center">%s</h1>\n', "Syndromic surveillance at SVA"), file=html)
# cat(sprintf('<h1 align="center">%s</h1>\n', Sys.Date()), file=html)
# cat(sprintf('<h2 align="center">%s</h2>\n', "Select a group on the navigation menu to the left to see results"), file=html)
#
# cat("<TABLE border=\"0\" align=\"center\">\n", file=html)
# cat("<tr>\n", file=html)
# cat("<td>System outputs are based on data up to the end of the PREVIOUS WEEK</td>\n", file=html)
# cat("</tr>\n", file=html)
#
# cat("<tr>\n", file=html)
# cat("<td>Number of events per week correspond to the number of laboratory submissions, classified into syndromic groups by a computer system. </td>\n", file=html)
# cat("</tr>\n", file=html)
#
# cat("<tr>\n", file=html)
# cat("<td> They reflect the number of ANIMALS tested per day for CATS and DOGS, and the number of HERDS otherwise</td>\n", file=html)
# cat("</tr>\n", file=html)
#
#
# cat("<tr>\n", file=html)
# cat("<td>For questions contact Fernanda Dorea (fernanda.dorea@sva.se)</td>\n", file=html)
# cat("</tr>\n", file=html)
#
#
# cat("</table> \n", file=html)
# cat("</p>\n", file=html)
#
#
#
# cat("<hr/>\n", file=html)
#
# # cat("<p align=\"center\">\n", file=html)
# # cat("<img src=\"summary.png\"/>\n", file=html)
# # cat("</p>\n", file=html)
# #
# cat("</body>\n", file=html)
#
# cat("</html>\n", file=html)
#
# close(html)
#
#
#
#
#
# html <- file('nav.html', "w+")
#
# cat("<html>\n", file=html)
# cat("<head>\n", file=html)
# cat(sprintf("<title>%s</title>\n", "SVASS menu"), file=html)
# cat("</head>\n", file=html)
#
# cat("<body>\n", file=html)
#
# # Create navigation.
# cat("<table border=0>\n", file=html)
#
# cat("<tr>", file=html)
# cat("<td colspan=3><a href=\"main.html\" target=\"main\">Main page</a></td>\n", file=html)
# cat("</tr>\n", file=html)
# cat("<tr>", file=html)
# cat("<td colspan=3><a href=\"help.pdf\" target=\"main\">HELP</a></td>\n", file=html)
# cat("</tr>\n", file=html)
#
#
# cat("<tr><td colspan=3> </td></tr>\n", file=html)
#
#
# #on 2018-05-23 SVASSS was converted to WEEKLY only and always
# #daily analysis are still ran and recorded, so that we can always revert,
# #but they generate no emails or html output
# # for(species in 1:length(species.acronyms)) {
# # cat("<tr>", file=html)
# #
# # cat("<td> </td>", file=html)
# #
# # if(sapply(true.alarms.daily,sum,na.rm=TRUE)[species]>0) {
# # cat("<td bgcolor='red'>", file=html)
# # } else {
# # if(sapply(scnd.alarms.daily,sum,na.rm=TRUE)[species]>0){
# # cat("<td bgcolor='yellow'>", file=html)
# # } else{
# # cat("<td bgcolor='springgreen'>", file=html)
# # }
# # }
# #
# # cat(" </td>", file=html)
# #
# # cat(sprintf("<td><a href=\"%s.html\" target=\"main\">%s</a></td>",
# # paste0("html/",species.acronyms[species]), species.names[species]), file=html)
# #
# # cat("</tr>\n", file=html)
# #
# # }
#
#
#
# for(species in 1:length(species.acronyms)) {
# cat("<tr>", file=html)
#
# cat("<td> </td>", file=html)
#
#
# if(sapply(true.alarms.weekly,sum,na.rm=TRUE)[species]>0) {
# cat("<td bgcolor='red'>", file=html)
# } else {
# if(sapply(scnd.alarms.weekly,sum,na.rm=TRUE)[species]>0){
# cat("<td bgcolor='yellow'>", file=html)
# } else{
# cat("<td bgcolor='springgreen'>", file=html)
# }
# }
#
# cat(" </td>", file=html)
#
# cat(sprintf("<td><a href=\"%s.html\" target=\"main\">%s</a></td>",
# paste0("html/",species.acronyms[species]),
# species.names[species]), file=html)
#
# cat("</tr>\n", file=html)
#
# }
#
#
#
# cat("</table>\n\n", file=html)
#
# cat("</body>\n", file=html)
#
# cat("</html>\n", file=html)
#
# close(html)
#
#
#
#
#
#