-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.Rmd
227 lines (188 loc) · 8.29 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
---
title: "Olink Assay Plots"
output: html_document
params:
rp_upload_data: NA
rp_bridging_str: NA
rp_combined_data: NA
rp_combined_meta: NA
---
```{r eval = FALSE, echo = FALSE}
# For PDF output, change the header to have "output: pdf_document".
#
# Note that due to an issue in rmarkdown, the default value of a parameter in
# the header cannot be `NULL`, so I used a default of `NA` for the default value
# of `n`.
library(dplyr)
library(DT)
```
## Summary of plate level information.
```{r, echo=F, message=F, warning=F}
DT::datatable(
data.frame(software_version = sapply(params$rp_upload_data, function(x) x@metadata$software_version) %>% unlist(),
filename = sapply(params$rp_upload_data, function(x) x$file_name[1]) %>% unlist(),
panel = sapply(params$rp_upload_data, function(x) x@metadata$panel)%>% unlist(),
total_samples = sapply(params$rp_upload_data, function(x) ncol(x)) %>% unlist(),
total_analyte = sapply(params$rp_upload_data, function(x) nrow(x)) %>% unlist()),
rownames = NULL,
options = list(scrollX = TRUE))
```
### Reference sample identifier: `r paste(strsplit(params$rp_bridging_str, split = ","), collapse = ",")`
```{r, echo=F, message=F, warning=F}
# needs sample_id, Plate.ID, in metafile and "lot" only appear once in column names
lapply(strsplit(params$rp_bridging_str, split = ",")%>%unlist(), function(x){
bdg_ls <- pull_bdg(params$rp_upload_data, pattern = x)
data.frame(filename = names(bdg_ls),
identifier = x,
total_samples = sapply(bdg_ls, function(x) ncol(x)) %>% unlist(),
total_analyte = sapply(bdg_ls, function(x) paste(x$Assay, collapse = ",")) %>% unlist())
})%>%
do.call(what = "rbind")%>%
DT::datatable(rownames = NULL,
options = list(scrollX = TRUE))
```
### Reference trace plot
```{r, echo=F, out.width= "100%", message=F, warning=F, results="asis"}
# needs sample_id, Plate.ID, in metafile and "lot" only appear once in column names
if(length(unique(params$rp_combined_meta$ref_sample)) == 1){
print("No reference sample was defined\n")
}
if(sum(grepl("lot", ignore.case = T, colnames(params$rp_combined_meta))) > 0){
df <- lapply(strsplit(params$rp_bridging_str, split = ",")%>%unlist(), function(x){
data.frame(Plate.ID = trim_string_bycommon(params$rp_combined_meta$Plate.ID, split = "_"),
sample_id = params$rp_combined_meta$sample_id,
Lot = params$rp_combined_meta[ ,grep("lot", ignore.case = T, colnames(params$rp_combined_meta))],
t(params$rp_combined_data@assays@data$npx))%>%
filter(grepl(x, sample_id, ignore.case = T))%>%
mutate(Sample = x)
})%>%
do.call(what = "rbind")
# tracking plot
if(length(unique(df$Plate.ID)) > 1){
print(df%>%
select(-sample_id)%>%
gather(-Plate.ID, -Lot, -Sample, key = "analyte", value = "NPX")%>%
ggplot(aes(Plate.ID, NPX))+
geom_line(aes(group = analyte, color = Lot))+
facet_wrap(~Sample)+
labs(subtitle = "each line indicate a analyte")+
theme_bw()+
theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5),
legend.position="bottom", legend.box="vertical", legend.margin=margin()))
}
}else{
df <- lapply(strsplit(params$rp_bridging_str, split = ",")%>%unlist(), function(x){
data.frame(Plate.ID = trim_string_bycommon(params$rp_combined_meta$Plate.ID, split = "_"),
sample_id = params$rp_combined_meta$sample_id,
t(params$rp_combined_data@assays@data$npx))%>%
filter(grepl(x, sample_id, ignore.case = T))%>%
mutate(Sample = x)
})%>%
do.call(what = "rbind")
# tracking plot
if(length(unique(df$Plate.ID)) > 1){
print(df%>%
select(-sample_id)%>%
gather(-Plate.ID, -Sample, key = "analyte", value = "NPX")%>%
ggplot(aes(Plate.ID, NPX))+
geom_line(aes(group = analyte))+
facet_wrap(~Sample)+
labs(subtitle = "each line indicate a analyte")+
theme_bw()+
theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5),
legend.position="bottom", legend.box="vertical", legend.margin=margin()))
}
}
```
### PCA
```{r, echo=F, out.width= "100%", message=F, warning=F}
mat <- impute::impute.knn(t(params$rp_combined_data@assays@data$npx))$data
fit <- prcomp(scale(mat))
if(length(unique(params$rp_combined_meta$ref_sample)) > 1){
idf <- paste(strsplit(params$rp_bridging_str, split = ",")%>%unlist(), collapse = "|")
idf <- paste0("(", idf, ")")
idf_all <- strsplit(params$rp_bridging_str, split = ",")%>%unlist()
which_idf <- function(check, idf){
idx <- sapply(idf, function(y){
grepl(y, ignore.case = T, check)
})
idf[idx][1]
}
}
```
```{r, echo=F, out.width= "100%", message=F, warning=F}
if(length(unique(params$rp_combined_meta$ref_sample)) > 1){
data.frame(pc_1 = fit$x[ ,1], #fit$layout[ ,1],
pc_2 = fit$x[ ,2],#fit$layout[ ,2],
sample_id = params$rp_combined_meta$sample_id,
type = ifelse(grepl(idf, ignore.case = T, params$rp_combined_meta$sample_id), "Ref", "Study_Sample"),
identifier = sapply(params$rp_combined_meta$sample_id, which_idf, idf = idf_all),
Plate.ID = trim_string_bycommon(params$rp_combined_meta$Plate.ID, split = "_"))%>%
mutate(identifier = ifelse(is.na(identifier), "Study_Sample", identifier))%>%
filter(!grepl("^(NC|IPC|Randox)", ignore.case = T, sample_id))%>%
ggplot()+
geom_point(aes(pc_1, pc_2, color = Plate.ID, shape = identifier))+
facet_wrap(~type)+
theme_bw()+
theme(legend.position="bottom", legend.box="vertical", legend.margin=margin())+
guides(color = guide_legend(ncol=4))
}else{
data.frame(pc_1 = fit$x[ ,1], #fit$layout[ ,1],
pc_2 = fit$x[ ,2],#fit$layout[ ,2],
sample_id = params$rp_combined_meta$sample_id,
QC.Warning = params$rp_combined_meta$QC.Warning,
Plate.ID = trim_string_bycommon(params$rp_combined_meta$Plate.ID, split = "_"))%>%
filter(!grepl("^(NC|IPC|Randox)", ignore.case = T, sample_id))%>%
ggplot()+
geom_point(aes(pc_1, pc_2, color = Plate.ID, shape = QC.Warning))+
scale_shape_manual(values = c(21, 16))+
theme_bw()+
theme(legend.position="bottom", legend.box="vertical", legend.margin=margin())+
guides(color = guide_legend(ncol=4))
}
```
### Detailed Reference Trace Plots
**Yellow dots indicate LOD**
```{r, echo=F, out.width= "100%", message=F, warning=F, results="asis"}
if((length(unique(params$rp_combined_meta$Plate.ID)) > 1) & (length(unique(params$rp_combined_meta$ref_sample)) > 1)){
bridge.cmb <- data.frame(
Plate.ID = params$rp_combined_meta$Plate.ID[grepl(idf, ignore.case = T, params$rp_combined_meta$sample_id)],
t(params$rp_combined_data@assays@data$npx[ ,grepl(idf, ignore.case = T, params$rp_combined_meta$sample_id)]))%>%
mutate(Plate.ID = trim_string_bycommon(Plate.ID, split = "_"))
lod <- params$rp_combined_data@elementMetadata$LOD
names(lod) <- make.names(rownames(params$rp_combined_data))
plot.ls <- lapply(names(lod), function(x){
ylims <- c(min(bridge.cmb[ ,x], na.rm = T) - 2, max(bridge.cmb[ ,x], na.rm = T)+2)
bridge.cmb%>%
dplyr::select(Plate.ID, x)%>%
ggplot(aes(Plate.ID, get(x)))+
geom_point()+
theme_bw()+
ylim(ylims)+
geom_hline(yintercept = lod[names(lod) == x], linetype = 2)+ # too slow
labs(y = "NPX", title = x, x = "")+
theme_bw(base_size = 8)+
theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5))
})
per_panel <- 6
n_panel <- ceiling(length(plot.ls)/per_panel)
design <- "
ABC
DEF
"
for (i in 1 : n_panel) {
ls <- plot.ls[(1 + (i-1) * per_panel) : min(length(plot.ls), (per_panel + (i-1) * per_panel))]
design_name <- c("A", "B", "C", "D", "E", "F")
names(ls) <- design_name[1 : length(ls)]
print(
wrap_plots(ls,
design = design, #ncol = 3, nrow = 2, byrow = T,
guides = "collect") & theme(legend.position = 'bottom')
)
}
}else if(length(unique(params$rp_combined_meta$ref_sample)) == 1){
print("No reference sample was defined\n")
}else{
print("Not applicable for single file\n")
}
```