forked from Molmed/SDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
327 lines (252 loc) · 12.6 KB
/
server.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
#-------------------------------------------------------
# Shiny server code to deploy SDashboard
#
# The purpose of this script is to serve up visulizations
# of the ProjectMan database which tracks sequencing metrics
# at the SEQ&SEQ technology platform.
#
# Please note that the 'data.R' should defines functions to
# load data from the database and that global configurations
# such as database users, passwords, etc should be set in the
# 'config.R' file.
#
# Have fun adding plots!
#
# Author: Johan Dahlberg, 2014
#
# Requires that the shiny, ggplot2, reshape2 and RODBC
# libraries are installed.
#--------------------------------------------------------
library(shiny)
library(ggplot2)
library(reshape2)
source("config.R")
source("data.R")
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
# Suppress warnings. Make sure that this is reenabled when debugging.
# The production variable is loaded from the config.R file.
if(production)
options(warn=-1)
shinyServer(function(input, output) {
# Plot the number of GB sequenced per week.
output$weekplot <- renderPlot({
current.year <- as.numeric(format(Sys.time(), "%Y"))
x <- queryGigaBasesPerWeek(current.year)
giga.bases.per.week.m <- melt(x , id.vars=c("Instrument", "Week"), measure.vars=c("GB"))
p <- ggplot(data=giga.bases.per.week.m, aes(x = Week, y = as.numeric(value))) +
geom_bar(stat="identity", aes(fill=Instrument)) +
ylab("Giga bases sequenced")
print(p)
})
# Plot the number of GB sequenced per month.
output$monthplot <- renderPlot({
current.year <- as.numeric(format(Sys.time(), "%Y"))
giga.bases.per.month.m <- melt(queryGigaBasesPerMonth(2012, current.year), id.vars=c("Instrument","Month","Year"), measure.vars=c("GB"))
giga.bases.per.month.m$Year <- as.factor(giga.bases.per.month.m$Year)
aggregated.data <- aggregate(as.numeric(giga.bases.per.month.m$value), by=list(giga.bases.per.month.m$Year, giga.bases.per.month.m$Month), sum)
colnames(aggregated.data) <- c("Year", "Month", "GB")
p <- ggplot(data = aggregated.data, aes(x = Month, y = GB, fill = Year)) +
geom_bar(position="dodge", stat="identity") +
scale_x_discrete(breaks = c(1:12), limits = c(1:12),
labels = months) +
ylab("Giga bases sequenced")
print(p)
})
# Plot the cumulative coverage per month and year.
output$cumulativeplot <- renderPlot({
current.year <- as.numeric(format(Sys.time(), "%Y"))
giga.bases.per.month.m <- melt(queryGigaBasesPerMonth(2012, current.year), id.vars=c("Instrument","Month","Year"), measure.vars=c("GB"))
giga.bases.per.month.m$Year <- as.factor(giga.bases.per.month.m$Year)
aggregated.data <- aggregate(as.numeric(giga.bases.per.month.m$value), by=list(giga.bases.per.month.m$Year, giga.bases.per.month.m$Month), sum)
colnames(aggregated.data) <- c("Year", "Month", "GB")
split.by.year <- split(aggregated.data, aggregated.data$Year)
cumulative.results <-
do.call(rbind,
lapply(split.by.year, function(x) {
x$cumulativeGB <- cumsum(x$GB)
x
}))
cumulative.results
p <- ggplot(data = cumulative.results, aes(x = Month, y = cumulativeGB)) +
geom_line(size = 2, aes(colour = Year)) +
scale_x_discrete(breaks = c(1:12),
labels = months) +
ylab("Giga bases sequenced")
print(p)
})
cumulativeGBPerInstrumentData <- function() {
# A helper function to return the cumulative data per instrument
#
# Args:
#
# Returns: The cumulative data in the following format:
# Year Month Instrument GB cumulativeGB
# 2012.HiSeq 1.1 2012 1 HiSeq 1 1190.16157 1190.162
# 2012.HiSeq 1.4 2012 2 HiSeq 1 369.35514 1559.517
#
current.year <- as.numeric(format(Sys.time(), "%Y"))
giga.bases.per.month.m <- melt(queryGigaBasesPerMonth(2012, current.year), id.vars=c("Instrument","Month","Year"), measure.vars=c("GB"))
giga.bases.per.month.m$Year <- as.factor(giga.bases.per.month.m$Year)
aggregated.data <- aggregate(as.numeric(giga.bases.per.month.m$value), by=list(giga.bases.per.month.m$Year,
giga.bases.per.month.m$Month,
giga.bases.per.month.m$Instrument),
sum)
colnames(aggregated.data) <- c("Year", "Month", "Instrument", "GB")
split.by.year.and.instrument <- split(aggregated.data, list(aggregated.data$Year,aggregated.data$Instrument))
cumulative.results <-
do.call(rbind,
lapply(split.by.year.and.instrument, function(x) {
x$cumulativeGB <- cumsum(x$GB)
x
}))
cumulative.results
}
# Plot the cumulativ data for the HiSeqs
output$cumulativeplotperinstrument <- renderPlot({
cumulative.results <- cumulativeGBPerInstrumentData()
# Exclude the MiSeqs and HiSeqX
cumulative.results <- cumulative.results[grepl("*HiSeq \\d", cumulative.results$Instrument),]
p <- ggplot(data = cumulative.results, aes(x = Month, y = cumulativeGB)) +
geom_line(size = 2, aes(colour = Year)) +
scale_x_discrete(breaks = c(1:12),
labels = months) +
ylab("Giga bases sequenced") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
facet_grid(. ~ Instrument)
print(p)
})
# Plot the cumulativ data for the HiSeqs
output$cumulativeplotperinstrumentforHiSeqX <- renderPlot({
cumulative.results <- cumulativeGBPerInstrumentData()
# Only include the HiSeqXs
cumulative.results <- cumulative.results[grepl("HiSeqX \\d", cumulative.results$Instrument),]
p <- ggplot(data = cumulative.results, aes(x = Month, y = cumulativeGB)) +
geom_line(size = 2, aes(colour = Year)) +
scale_x_discrete(breaks = c(1:12),
labels = months) +
ylab("Giga bases sequenced") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
facet_grid(. ~ Instrument)
print(p)
})
# Plot the cumulativ data for the MiSeqs
output$cumulativeplotperinstrumentforMiSeq <- renderPlot({
cumulative.results <- cumulativeGBPerInstrumentData()
# Only include the MiSeqs
cumulative.results <- cumulative.results[grepl("*MiSeq*", cumulative.results$Instrument),]
p <- ggplot(data = cumulative.results, aes(x = Month, y = cumulativeGB)) +
geom_line(size = 2, aes(colour = Year)) +
scale_x_discrete(breaks = c(1:12),
labels = months) +
ylab("Giga bases sequenced") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
facet_grid(. ~ Instrument)
print(p)
})
# Plot the error rate per instrument
output$errorrateplot <- renderPlot({
current.date <- format(Sys.time(), "%Y-%m-%d")
one.year.ago <- paste(as.numeric(format(Sys.time(), "%Y")) -1 ,format(Sys.time(), "%m-%d"), sep="-")
quality.metrics <- queryQualityValues(one.year.ago, current.date)[,c(1:4,7)]
quality.metrics$Year <- as.factor(quality.metrics$Year)
quality.metrics$Month <- as.factor(quality.metrics$Month)
quality.metrics.m <- melt(quality.metrics, id.vars=c("Instrument", "Year", "Month"), measure.vars=c("ErrorRate"))
p <- ggplot(data = quality.metrics.m, aes(x = Month, y = value, colour = Year)) +
geom_boxplot() +
facet_grid(. ~ Instrument) +
scale_x_discrete(breaks = c(1:12),
labels = months) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ylab("Error Rate")
print(p)
})
# Plot the percent Q30 per instrument
output$percentq30plot <- renderPlot({
current.date <- format(Sys.time(), "%Y-%m-%d")
one.year.ago <- paste(as.numeric(format(Sys.time(), "%Y")) -1 ,format(Sys.time(), "%m-%d"), sep="-")
quality.metrics <- queryQualityValues(one.year.ago, current.date)[,c(1:3,5,7)]
quality.metrics$Year <- as.factor(quality.metrics$Year)
quality.metrics$Month <- as.factor(quality.metrics$Month)
quality.metrics.m <- melt(quality.metrics, id.vars=c("Instrument", "Year", "Month"), measure.vars=c("PercentQ30"))
p <- ggplot(data = quality.metrics.m, aes(x = Month, y = value, colour = Year)) +
geom_boxplot() +
facet_grid(. ~ Instrument) +
scale_x_discrete(breaks = c(1:12),
labels = months) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ylab("Percent Q30")
print(p)
})
# Plot the mean quality per instrument
output$meanqualityplot <- renderPlot({
current.date <- format(Sys.time(), "%Y-%m-%d")
one.year.ago <- paste(as.numeric(format(Sys.time(), "%Y")) -1 ,format(Sys.time(), "%m-%d"), sep="-")
quality.metrics <- queryQualityValues(one.year.ago, current.date)[,c(1:3,6,7)]
quality.metrics$Year <- as.factor(quality.metrics$Year)
quality.metrics$Month <- as.factor(quality.metrics$Month)
quality.metrics.m <- melt(quality.metrics, id.vars=c("Instrument", "Year", "Month"), measure.vars=c("MeanQuality"))
p <- ggplot(data = quality.metrics.m, aes(x = Month, y = value, colour = Year)) +
geom_boxplot() +
facet_grid(. ~ Instrument) +
scale_x_discrete(breaks = c(1:12),
labels = months) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ylab("Mean Quality")
print(p)
})
# Plot the cumulative numver of samples per month
output$cumulativesamples <- renderPlot({
samples <- querySamples()
# Select only the first read, otherwise all numbers will be doubled for paired end runs.
relevant.sample.info <- samples[samples$read_num == 1, c("run_date", "project_id","sample_name")]
relevant.sample.info$counts <- 1
aggregated.by.year.and.month <- aggregate(relevant.sample.info$counts, by=list(format(relevant.sample.info$run_date, "%Y-%m")), sum)
aggregated.by.year <- aggregate(relevant.sample.info$counts, by=list(format(relevant.sample.info$run_date, "%Y")), sum)
p <- ggplot(data = aggregated.by.year.and.month, aes(x = Group.1, y = x, fill = "#8dc63f")) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
ylab("Samples") +
xlab("Year and month") +
scale_fill_identity()
print(p)
})
# Plot the cumulative number of samples totaled per year.
output$cumulativesamplesyeartotal <- renderPlot({
samples <- querySamples()
# Select only the first read, otherwise all numbers will be doubled for paired end runs.
relevant.sample.info <- samples[samples$read_num == 1, c("run_date", "project_id","sample_name")]
split.per.project <- split(relevant.sample.info, list(relevant.sample.info$project_id))
# Make the samples unique per project and year sequenced.
unique.samples.per.year <-
do.call(rbind,
lapply(split.per.project, function(x) {
x$year <- format(x$run_date, "%Y")
unique(x[, c("year","sample_name")])
}))
unique.samples.per.year$counts <- 1
aggregated.by.year <- aggregate(unique.samples.per.year$counts, by=list(unique.samples.per.year$year), sum)
p <- ggplot(data = aggregated.by.year, aes(x = Group.1, y = x, fill = "#8dc63f")) +
geom_bar(stat = "identity") +
geom_text(aes(label=x, y= x - 200)) +
ylab("Samples") +
xlab("Year") +
scale_fill_identity()
print(p)
})
# Plot the cumulative number of projects per month
output$cumulativeprojects <- renderPlot({
samples <- querySamples()
project.info <- samples[, c("run_date","project_id")]
project.info$year <- format(project.info$run_date, "%Y")
project.info <- unique(project.info[,c("year", "project_id")])
project.info$counts <- 1
aggregated.by.year <- aggregate(project.info$counts, by=list(project.info$year), sum)
p <- ggplot(data = aggregated.by.year, aes(x = Group.1, y = x, fill = "#8dc63f")) +
geom_bar(stat = "identity") +
geom_text(aes(label=x, y = x - 5)) +
ylab("Projects") +
xlab("Year") +
scale_fill_identity()
print(p)
})
})