-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreport.Rmd
437 lines (350 loc) · 11.5 KB
/
report.Rmd
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
---
title: "Single Cell Analysis Report"
author: "scShinyHub"
date: "3/8/2018"
output:
html_document:
fig_caption: yes
fig_height: 7
fig_width: 9
number_sections: yes
toc: yes
code_folding: hide
params:
#__PARAMPLACEHOLDER__
---
```{r checkDEBUG, include=FALSE}
if (!exists("DEBUG")) {
DEBUG <- TRUE
# DEBUG=FALSE
}
if (!exists("DEBUGSAVE")) {
# DEBUGSAVE=TRUE
DEBUGSAVE <- FALSE
}
```
```{r loadData, include=FALSE}
# LIBRARIES -----------------------------------------------------------------
library(shiny)
library(shinyTree)
library(tibble)
library(shinyBS)
library(plotly)
library(shinythemes)
library(ggplot2)
library(DT)
library(pheatmap)
library(threejs)
library(sm)
library(RColorBrewer)
library(mclust)
library(reshape)
library(ggplot2)
library(knitr)
library(kableExtra)
library(shinyWidgets)
library(scater)
library(shinyMCE)
library(kohonen)
library(Rsomoclu)
library(gtools)
library(SingleCellExperiment)
library(Matrix)
library(colourpicker)
library(shinytest)
library(scran)
# we overwirte this function because it doesn't make sense in the report and causes problems otherwise
exportTestValues <- function(...){return(NULL)}
# params only exsits if called from somewhere with parameters
if (exists("params") & is.list(params)) {
cat(file = stderr(), paste("params:", params$calledFromShiny, "\n"))
cat(file = stderr(), paste("params exists:", "calledFromShiny" %in% names(params), "\n"))
LOCALEXECUTION <- FALSE
if (DEBUGSAVE) {
base::save(file = "~/scShinyHubDebug/tempReport-rmd.RData", list = c(ls()))
}
} else {
# rm(list = ls())
base::load(file = "geneLists.RData")
source("serverFunctions.R")
source("defaultValues.R")
source("reactives.R", local = TRUE)
uiFiles <- dir(path = "contributions", pattern = "reactives.R", full.names = TRUE, recursive = TRUE)
for (fp in uiFiles) {
if (DEBUG) cat(file = stderr(), paste("loading: ", fp, "\n"))
source(fp, local = TRUE)
}
cp = load("~/scShinyHubDebug/tempReport.RData")
params <- myparams
LOCALEXECUTION <- TRUE # to know that we are debugging.
}
```
__LOAD_REACTIVES__
# parameters
```{r setup, include=TRUE, echo=TRUE, warning=FALSE, eval=FALSE}
# not needed anymore
# input <- params
print(str(input$file1$name))
if (file.exists(input$file1$datapath)) {
load(input$file1$datapath)
} else {
cat(file = stderr(), paste("Error input file not existing\n"))
}
# print(ls())
# will be printed on console, for debugging purposes only
cat(file = stderr(), paste("env:", ls(), "\n"))
cat(file = stderr(), paste("input:", names(input), input, "\n"))
# cat(file = stderr(), paste("dataTables:", names(dataTables),"\n"))
```
```{r save, eval=FALSE}
save(file = "report.RData", list = c(ls(), "params"))
```
```{r loadata, eval=FALSE}
load(file = "/var/folders/_h/vtcnd09n2jdby90zkb6wyd740000gp/T/RtmpzXuS03/report.RData")
```
```{r check Variables, include=FALSE, eval=TRUE}
# TODO this needs to go to individual packages
if (is.null(input$cluster)) {
input$cluster <- "0"
print("Warning: setting cluster to 0")
}
if (is.null(input$cluster5)) {
input$cluster5 <- "0"
print("Warning: setting cluster5 to 0")
}
if (is.null(input$clusters)) {
input$clusters <- "0"
print("Warning: setting clusters to 0")
}
if (is.null(input$sCA_dgeClustersSelection)) {
input$sCA_dgeClustersSelection <- "0"
print("Warning: setting sCA_dgeClustersSelection to All")
}
if (is.null(input$clusters2)) {
input$clusters2 <- "0"
print("Warning: setting clusters2 to 0")
}
if (is.null(input$clusters3)) {
input$clusters3 <- "0"
print("Warning: setting clusters4 to 0")
}
if (is.null(input$DE_clusterSelectionPanelPlot)) {
input$DE_clusterSelectionPanelPlot <- "All"
print("Warning: setting clusters4 to All")
}
if (is.null(input$geneSelectionClustering)) {
input$geneSelectionClustering <- ""
print("Warning: setting geneSelectionClustering to All")
}
```
```{r inputData, echo=TRUE, eval=TRUE}
# if ( !exists("dataTables")) {# needed when working with ~/scShinyHubDebug/reports.Data
cat(file = stderr(), paste("loading dataTables\n"))
# in case the file is not in the root directory is has to be manually loaded during debugging
# input$file1 = data.frame("datapath" = dir(path = "~/Rstudio/shHubgit/", pattern="scEx.Rds", full.names = T), stringsAsFactors = F)
# inFile = input$file1
dataTables <- inputData
# }
```
```{r useCells, echo=TRUE, eval=FALSE}
# not needed anymore
geneNames <- input$minExpGenes
rmCells <- input$cellsFiltersOut
rmPattern <- input$cellPatternRM
keepCells <- input$cellKeep
# cat(file = stderr(), paste("dataTables:", names(dataTables),"\n"))
useCells <- useCellsFunc(dataTables,
geneNames = input$minExpGenes,
rmCells = input$cellsFiltersOut,
rmPattern = input$cellPatternRM,
keepCells = input$cellKeep,
cellKeepOnly = input$cellKeepOnly
)
```
```{r useGenes, echo=TRUE, eval=FALSE}
# not needed anymore
# base::save(file = "~/scShinyHubDebug/report.useGenes.RData", list = ls())
ipIDs <- input$selectIds
genesKeep <- input$genesKeep
geneListSelection <- input$geneListSelection
useGenes <- useGenesFunc(dataTables, ipIDs, geneListSelection, genesKeep, geneLists)
```
```{r featureDataReact, echo=TRUE, eval=TRUE}
featureDataReact <- dataTables$featuredata[useGenes, ]
featureData <- dataTables$featuredata[useGenes, ]
```
```{r scEx, echo=TRUE, eval=FALSE}
# not needed anymore
if (DEBUG) cat(file = stderr(), "scEx\n")
minGene <- input$minGenesGS # min number of reads per gene
minG <- input$minGenes # min number of reads per cell
maxG <- input$maxGenes # max number of reads per cell
scEx <- scExFunc(scExOrg = dataTables$scEx, useCells = useCells, useGenes = useGenes, minGene = minGene, minG = minG, maxG = maxG)
if (DEBUG) cat(file = stderr(), "scEx:DONE\n")
if (DEBUG) cat(file = stderr(), paste("scEx:", class(scEx), "\n"))
```
```{r medianENSG, echo=TRUE, eval=TRUE}
medianENSG <- medianENSGfunc(as.matrix(assays(scEx)[[1]]))
```
```{r medianUMI, echo=TRUE, eval=TRUE}
medianUMI <- medianUMIfunc(as.matrix(assays(scEx)[[1]]))
```
```{r scEx_matrix, include=FALSE, eval=TRUE}
scEx_matrix <- as.matrix(assays(scEx)[[1]])
```
```{r rawNormalization, include=FALSE, eval=FALSE}
# not needed anymore
rawNormalization <- scEx
```
```{r scExLogMatrixDisplay, include=FALSE, eval=FALSE}
# not needed anymore
retVal <- as.data.frame(as.matrix(assays(scEx_log)[[1]]))
rownames(retVal) <- make.names(rowData(scEx_log)$symbol, unique = TRUE)
scExLogMatrixDisplay <- retVal
```
```{r pca, eval=TRUE, echo=TRUE, eval=FALSE}
# not needed anymore
if (DEBUG) cat(file = stderr(), "pca\n")
pca <- pcaFunc(scEx_log)
```
```{r scran_Cluster, echo=TRUE, eval=FALSE}
# not needed anymore output is dbCluster
if (DEBUG) cat(file = stderr(), "scran_Cluster\n")
geneSelectionClustering <- input$geneSelectionClustering
seed <- input$seed
kNr <- input$kNr
clusterSource <- input$clusterSource
geneSelectionClustering <- input$geneSelectionClustering
minClusterSize <- input$minClusterSize
clusterMethod <- input$clusterMethod
#save(file = "~/scShinyHubDebug/scran_cluster.report.RData", list = c(ls()))
# load("~/scShinyHubDebug/scran_cluster.report.RData")
# retVal <- scran_ClusterFunc(pca, seed = seed, kNr = kNr)
retVal <- scranCluster(
pca, scEx_log, seed, clusterSource,
geneSelectionClustering, minClusterSize, clusterMethod,
featureData
)
if (DEBUG) cat(file = stderr(), "scran_Cluster:done\n")
```
Creating `r input$gQC_tsneDim` tSNE clusters.
```{r tsne, echo=TRUE, eval=FALSE}
#not needed anymore
if (DEBUG) cat(file = stderr(), "tsne\n")
gQC_tsneDim <- input$gQC_tsneDim
gQC_tsnePerplexity <- input$gQC_tsnePerplexity
gQC_tsneTheta <- input$gQC_tsneTheta
gQC_tsneSeed <- input$gQC_tsneSeed
set.seed(seed = gQC_tsneSeed)
tsne <- run_tsne(pca,
dims = gQC_tsneDim,
perplexity = gQC_tsnePerplexity,
theta = gQC_tsneTheta, check_duplicates = FALSE
)
if (DEBUG) cat(file = stderr(), "tsne: done\n")
```
```{r projections, include=FALSE, eval=FALSE}
# not needed anynmore
# TODO not working with current projection model and debugging.
# the correct projections are used via saving/loading of variables during report creation in server.R
# base::save(file = tmpPrjFile, list = c("projections", "scEx_log", "gNames"))
# reactiveFiles <- paste0(reactiveFiles, "load(file=\"", tmpPrjFile, "\")\n", collapse = "\n")
if (DEBUG) cat(file = stderr(), "projections\n")
clustering <- scran_Cluster
projections <- data.frame(tsne$Y)
colnames(projections) <- paste0("tsne", c(1:ncol(projections)))
# projections$dbCluster <- factor(clustering$kmeans_10_clusters$Cluster - 1)
projections$dbCluster <- factor(clustering$kmeans_10_clusters$Cluster )
projections <- cbind(projections, data.frame(pca$x[, c(1, 2, 3)]))
rownames(projections) <- clustering$kmeans_10_clusters$Barcode
samp <- gsub(".*-(.*)", "\\1", rownames(projections))
if (length(levels(as.factor(samp))) > 1) {
projections$sample <- samp
} else {
projections$sample <- "1"
}
n <- length(projectionFunctions)
for (proj in projectionFunctions) {
if (DEBUG) cat(file = stderr(), paste("forceCalc ", proj[1], "\n"))
assign("tmp", eval(parse(text = paste0(proj[2], "()"))))
cn <- make.names(c(colnames(projections), make.names(proj[1])))
if (length(tmp) == 0) {
next()
}
if (ncol(projections) == 0) {
projections <- data.frame(tmp = tmp)
} else {
if (nrow(projections) == length(tmp)) {
projections <- cbind(projections, tmp)
}
}
colnames(projections) <- cn
}
projections$UmiCountPerGenes <- 0
projections$UmiCountPerGenes2 <- 0
if (DEBUG) cat(file = stderr(), "projections: done\n")
```
```{r sampleInfo, include=FALSE, eval=FALSE}
# not needed anymore
sampleInfo <- sampleInfoFunc(scEx)
```
```{r inputSample, include=FALSE, eval=FALSE}
# not needed anymore
sampInf <- gsub(".*-(.*)", "\\1", dataTables$scEx$barcode)
inputSample <- data.frame(
cellName = colnames(dataTables$scEx),
sample = sampInf,
ncells = colSums(as.matrix(assays(dataTables$scEx)[[1]]))
)
```
```{r log2cpm, include=FALSE, eval=TRUE}
if (DEBUG) cat(file = stderr(), "log2cpm\n")
log2cpm <- as.data.frame(as.matrix(assays(scEx_log)[[1]]))
if (DEBUG) cat(file = stderr(), "log2cpm:Done\n")
```
```{r summaryStats, include=TRUE, eval=TRUE}
line1 <- paste("No. of cells:", dim(log2cpm)[2], sep = "\t")
line2 <- paste("Median UMIs:", medianUMI, sep = "\t")
line3 <- paste("Median Genes:", medianENSG, sep = "\t")
line5 <- paste("No. of reads:", dim(log2cpm)[1], sep = "\t")
HTML(
paste0(
"Summary statistics of this dataset:", "<br/>", "<br/>",
line1, "<br/>",
line2, "<br/>",
line3, "<br/>",
line5
)
)
```
# Input/General
input file: **`r input$file1$name`**
Description:
**`r input$descriptionOfWork`**
## Normalization used
Normalization method used: **`r input$normalizationRadioButton`**.
## Variables used
Variables that are used can be found here: **[variables.used.txt](variables.used.txt)
__CHILDREPORTS__
```{r variables, include=FALSE, results='asis', eval=FALSE}
# cat(file = stderr(), paste(ls(), collapse = "\n"))
# save(file = "~/scShinyHubDebug/report.vars.RData", list = ls())
# load(file = "~/scShinyHubDebug/report.vars.RData")
fileConn <- (paste0(reportTempDir, "/variables.used.txt"))
printVars <- function(x, fileConn) {
for (varN in names(x)) {
# if (is.data.frame(x[[varN]])) {
cat(file = fileConn, x = paste(varN, "=", paste(x[[varN]], collapse = "\n"), collapse = "\n"), append = TRUE)
cat(file = fileConn, x = "\n\t", append = TRUE)
}
}
printVars(params, fileConn)
# close(fileConn)
```
# Session information
```{r sessionInfo, echo=TRUE}
if (!LOCALEXECUTION) {
# save(file = "~/scShinyHubDebug/report.RData", list=ls())
}
sessionInfo()
```