-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatadriven_normalization.Rmd
500 lines (388 loc) · 28.4 KB
/
datadriven_normalization.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
---
title: "**Normalization** strategy comparison for **Data-driven** analysis of isobarically labeled proteomic data."
author: "Joris Van Houtven, Piotr Prostko"
date: '`r format(Sys.time(), "%B %d, %Y,%H:%M")`'
output:
html_document:
toc: true
toc_depth: 2
toc_float: true
number_sections: true
theme: flatly
code_folding: "hide"
editor_options:
chunk_output_type: console
params:
input_data_p: 'data/input_data.rds'
suffix_p: ''
load_outputdata_p: FALSE
save_outputdata_p: FALSE
subsample_p: 0
---
```{r, setup, include=FALSE}
# default knitr options for all chunks
knitr::opts_chunk$set(
message=FALSE,
warning=FALSE,
fig.width=12,
fig.height=7
)
```
<span style="color: grey;">
_This notebook is one in a series of many, where we explore how different data analysis strategies affect the outcome of a proteomics experiment based on isobaric labeling and mass spectrometry. Each analysis strategy or 'workflow' can be divided up into different components; it is recommend you read more about that in the [introduction notebook](intro.html)._
</span>
In this notebook specifically, we investigate the effect of varying the **Normalization** component on the outcome of the differential expression results. The five component variants are: **median sweeping**, **CONSTANd**, **NOMAD**, **quantile1 (first on PSM level)**, **quantile2 (only on protein level)**.
<span style="color: grey;">
_The R packages and helper scripts necessary to run this notebook are listed in the next code chunk: click the 'Code' button. Each code section can be expanded in a similar fashion. You can also download the [entire notebook source code](datadriven_normalization.Rmd)._
</span>
```{r}
library(stringi)
library(gridExtra)
library(dendextend)
library(kableExtra)
library(psych)
library(limma)
library(tidyverse)
library(preprocessCore)
library(CONSTANd)
library(NOMAD) # devtools::install_github("carlmurie/NOMAD")
source('util/other_functions.R')
source('util/plotting_functions.R')
```
Let's load our PSM-level data set:
```{r}
data.list <- readRDS(params$input_data_p)
dat.l <- data.list$dat.l # data in long format
display_dataframe_head(dat.l)
```
After the filtering done in `data_prep.R`, there are 19 UPS1 proteins remaining, even though 48 were originally spiked in.
```{r}
# which proteins were spiked in?
spiked.proteins <- dat.l %>% distinct(Protein) %>% filter(stri_detect(Protein, fixed='ups')) %>% pull %>% as.character
remove_factors(spiked.proteins)
tmp=dat.l %>% distinct(Protein) %>% pull %>% as.character
# protein subsampling
if (params$subsample_p>0 & params$subsample_p==floor(params$subsample_p) & params$subsample_p<=length(tmp)){
sub.prot <- tmp[sample(1:length(tmp), size=params$subsample_p)]
if (length(spiked.proteins)>0) sub.prot <- c(sub.prot,spiked.proteins)
dat.l <- dat.l %>% filter(Protein %in% sub.prot)
}
```
We store the metadata in `sample.info` and show some entries below. We also pick technical replicates with a dilution factor of 0.5 as the reference condition of interest. Each condition is represented by two of eight reporter Channels in each Run.
```{r,echo=FALSE}
# specify # of varying component variants and their names
variant.names <- c('median_sweeping', 'CONSTANd', 'NOMAD', 'quantile1')
n.comp.variants <- length(variant.names)
scale.vec <- 'log'
# get some data parameters created in the data_prep script
referenceCondition <- data.list$data.params$referenceCondition
condition.color <- data.list$data.params$condition.color
ma.onesample.num <- data.list$data.params$ma.onesample.num
ma.onesample.denom <- data.list$data.params$ma.onesample.denom
ma.allsamples.num <- data.list$data.params$ma.allsamples.num
ma.allsamples.denom <- data.list$data.params$ma.allsamples.denom
# create data frame with sample information
sample.info <- get_sample_info(dat.l, condition.color)
# get channel names
channelNames <- remove_factors(unique(sample.info$Channel))
```
```{r}
display_dataframe_head(sample.info)
referenceCondition
channelNames
```
# Unit scale component: log2 transformation of reporter ion intensities
We use the default unit scale: the log2-transformed reportion ion intensities.
```{r}
dat.unit.l <- dat.l %>% mutate(response=log2(intensity)) %>% select(-intensity)
display_dataframe_head(dat.unit.l)
```
# Normalization component
In the next three subsections, let's look at our different ways to normalize our data on the PSM level.
Since these methods all need to be applied on matrix-like data, let's switch to wide format.
(Actually, this is semi-wide, since the Channel columns still have contributions form all Runs, but that's OK because in the next step we split by Run.)
```{r}
# switch to wide format
dat.unit.w <- pivot_wider(data = dat.unit.l, id_cols=-one_of(c('Condition', 'BioReplicate')), names_from=Channel, values_from=response)
display_dataframe_head(dat.unit.w)
```
```{r}
dat.norm.w <- emptyList(variant.names)
```
## median sweeping (1)
Median sweeping means subtracting from each PSM quantification value the spectrum median (i.e., the row median computed across samples/channels) and the sample median (i.e., the column median computed across features). If the unit scale is set to intensities or ratios, the multiplicative variant of this procedure is applied: subtraction is replaced by division.
First, let's sweep the medians of all the rows, and do the columns later as suggested by [Herbrich at al.](https://doi.org/10.1021/pr300624g).
No need to split this per Run, because each row in this semi-wide format contains only values from one Run and each median calculation is independent of the other rows.
```{r, eval=!params$load_outputdata_p}
# subtract the spectrum median log2intensity from the observed log2intensities
dat.norm.w$median_sweeping <- dat.unit.w
dat.norm.w$median_sweeping[,channelNames] <- median_sweep(dat.norm.w$median_sweeping[,channelNames], 1, '-')
```
## CONSTANd
[CONSTANd](https://doi.org/doi:10.18129/B9.bioc.CONSTANd) ([Van Houtven et al.](https://doi.org/10.1016/j.jmb.2021.166966)) normalizes a data matrix by 'raking' iteratively along the rows and columns (i.e. multiplying each row or column with a particular number) such that the row means and column means equal 1.
One can never attain these row and column constraints simultaneously, but the algorithm converges very fast to the desired precision.
```{r, eval=!params$load_outputdata_p}
# dat.unit.l entries are in long format so all have same colnames and no channelNames
x.split <- split(dat.unit.w, dat.unit.w$Run) # apply CONSTANd to each Run separately
x.split.norm <- lapply(x.split, function(y) {
y[,channelNames] <- CONSTANd(y[,channelNames])$normalized_data
return(y)
})
dat.norm.w$CONSTANd <- bind_rows(x.split.norm)
display_dataframe_head(dat.norm.w$CONSTANd[, channelNames])
```
## NOMAD
NOMAD ([Murie et al.](https://doi.org/10.1016/j.jbior.2017.11.005)) is a computationally efficient implementation akin to linear regression, similar to Tukey's median polish but using the mean: it subtracts the mean from each subset (defined by a categorical covariate) of quantification values, sequentially for each covariate.
We apply NOMAD on the PSM level instead of the peptide level.
```{r, eval=!params$load_outputdata_p}
# doRobust=F: use means, like CONSTANd; doLog=F: values are already transformed.
dat.nomadnorm <- nomadNormalization(dat.unit.l$response, dat.unit.l %>% rename(iTRAQ=Channel) %>% as.data.frame, doRobust = FALSE, doiTRAQCorrection = FALSE, doLog = FALSE)
dat.nomadnorm$x$response <- dat.nomadnorm$y
dat.norm.w$NOMAD <- pivot_wider(data = dat.nomadnorm$x, id_cols=-one_of(c('Condition', 'BioReplicate')), names_from=iTRAQ, values_from=response)
display_dataframe_head(dat.norm.w$NOMAD[, channelNames])
```
## Quantile (1)
Quantile normalization (As implemented by ([Bolstad et al.](https://doi.org/10.1093/bioinformatics/19.2.185)) makes the distribution (i.e., the values of the quantiles) of quantification values in different samples identical.
We first apply it to each Run separately, and then re-scale the observations so that the mean observation within in each run is set equal to the mean observation across all runs.
After summarization, we do a second pass on the matrix with data from all runs.
```{r, eval=!params$load_outputdata_p}
grand_average <- mean(as.matrix(dat.unit.w[,channelNames]))
x.split <- split(dat.unit.w, dat.unit.w$Run)
x.split.norm <- lapply(x.split, function(y) {
# apply normalizeQuantiles to each Run separately
y[,channelNames] <- normalize.quantiles(as.matrix(y[,channelNames]))
# make averages of all runs equal.
y[,channelNames] <- y[,channelNames] / mean(colMeans(y[,channelNames])) * grand_average
return(y)
})
dat.norm.w$quantile1 <- bind_rows(x.split.norm)
display_dataframe_head(dat.norm.w$quantile1[, channelNames])
```
# Summarization component: Median summarization
Within each Run and within each Channel, we replace multiple related observations with their median. First, for each Peptide (median of the PSM values), then for each Protein (median of the peptide values).
```{r, eval=!params$load_outputdata_p}
# normalized data
dat.norm.summ.w <- lapply(dat.norm.w, function(x){
y <- x %>% group_by(Run, Protein, Peptide) %>% summarise_at(.vars = channelNames, .funs = median, na.rm=T) %>% summarise(across(channelNames, .fns = median, na.rm=T)) %>% ungroup()
return(y)})
```
Let's also summarize the non-normalized data for comparison later on.
```{r, eval=!params$load_outputdata_p}
# non-normalized data
# group by (run,)protein,peptide then summarize twice (once on each level)
# add select() statement because summarise_at is going bananas over character columns
dat.nonnorm.summ.w <- dat.unit.w %>% group_by(Run, Protein, Peptide) %>% select(Run, Protein, Peptide, channelNames) %>% summarise(across(channelNames, .fns = median)) %>% select(Run, Protein, channelNames) %>% summarise(across(channelNames, .fns = median)) %>% ungroup()
```
```{r, eval=!params$load_outputdata_p}
# make data completely wide (also across runs)
## normalized data
dat.norm.summ.w2 <- lapply(dat.norm.summ.w, function(x) {
return(x %>% pivot_wider(names_from = Run, values_from = all_of(channelNames), names_glue = "{Run}:{.value}"))
})
```
# Normalization component (2)
Some normalization methods require a second stage after summarization, and the quantile2 method is applied only on the protein-level data.
## Median sweeping (2)
Now that the data is on the protein level, let's sweep all values separately per protein in the columns/samples. This is _slightly_ different from sweeping before the summarization step because the median of medians is not the same as the grand median, but this does not introduce any bias.
```{r, eval=!params$load_outputdata_p}
# median sweeping: in each channel, subtract median computed across all proteins within the channel, separately for each MS run.
tmp <- dat.norm.summ.w2$median_sweeping %>% select(-Protein)
dat.norm.summ.w2$median_sweeping[, colnames(dat.norm.summ.w2$median_sweeping)!='Protein'] <- median_sweep(tmp, 2, '-')
# propagate the second round of median sweeping normalization from dat.norm.summ.w2 to dat.norm.summ.w
dat.norm.summ.w$median_sweeping <- dat.norm.summ.w2$median_sweeping %>% pivot_longer(cols=-one_of('Protein'), names_to='Sample', values_to='response', values_drop_na = T) %>% inner_join(sample.info[,c('Sample', 'Run', 'Channel', 'Condition')], by='Sample') %>% pivot_wider(id_cols=-one_of('Condition', 'Sample'), names_from=Channel, values_from=response)
```
## Quantile (2)
Quantiles have been equalized within-run, but not yet across runs, so let's do that now.
```{r, eval=!params$load_outputdata_p}
# apply normalizeQuantiles again, now to the data from all runs simultaneously
dat.norm.summ.w2$quantile1[,sample.info$Sample] <- normalize.quantiles(as.matrix(dat.norm.summ.w2$quantile1[,sample.info$Sample]))
# propagate the second round of quantile1 normalization from dat.norm.summ.w2 to dat.norm.summ.w
dat.norm.summ.w$quantile1 <- dat.norm.summ.w2$quantile1 %>% pivot_longer(cols=-one_of('Protein'), names_to='Sample', values_to='response', values_drop_na = T) %>% inner_join(sample.info[,c('Sample', 'Run', 'Channel', 'Condition')], by='Sample') %>% pivot_wider(id_cols=-one_of('Condition', 'Sample'), names_from=Channel, values_from=response)
```
## Quantile norm of protein-level data by condition
As an alternative, we also perform quantile normalization on protein-level data only.
```{r, eval=!params$load_outputdata_p}
dat.nonnorm.summ.l <- to_long_format(dat.nonnorm.summ.w, sample.info)
x.split <- split(dat.nonnorm.summ.l, dat.nonnorm.summ.l$Condition)
x.split.norm <- lapply(x.split, function(x){
tmp.wide <- pivot_wider(data=x, id_cols='Protein', names_from=Sample, values_from=response)
tmp.wide[, colnames(tmp.wide)!='Protein'] <- normalize.quantiles(as.matrix(tmp.wide %>% select(-Protein)))
tmp <- pivot_longer(tmp.wide, cols=-one_of('Protein'), names_to='Sample', values_to='response') %>% drop_na() %>% inner_join(sample.info[,c('Sample', 'Run', 'Channel', 'Condition')], by='Sample')
return(tmp)
})
x <- bind_rows(x.split.norm)
dat.norm.summ.w$quantile2 <- pivot_wider(data = x, id_cols=-one_of('Condition', 'Sample'), names_from=Channel, values_from=response)
dat.norm.summ.w2$quantile2 <- dat.norm.summ.w$quantile2 %>% pivot_wider(names_from = Run, values_from = all_of(channelNames), names_glue = "{Run}:{.value}")
variant.names <- c('median_sweeping', 'CONSTANd', 'NOMAD', 'quantile1', 'quantile2')
n.comp.variants <- length(variant.names)
```
# QC plots
Before getting to the DEA section, let's do some basic quality control and take a sneak peek at the differences between the component variants we've chosen. First, however, we should make the data completely wide, so that each sample gets it's own unique column.
```{r, eval=!params$load_outputdata_p}
# make data completely wide (also across runs)
## non-normalized data
dat.nonnorm.summ.w2 <- dat.nonnorm.summ.w %>% pivot_wider(names_from = Run, values_from = all_of(channelNames), names_glue = "{Run}:{.value}")
```
```{r, eval=params$load_outputdata_p}
load(paste0('datadriven_normalization_outdata', params$suffix_p, '.rda'))
```
## Boxplots
These boxplots of both the raw and normalized intensities show that the distributions are all symmetrical. The shapes of the median sweeping, CONSTANd and NOMAD distributions are very similar, just like those of the two quantile approaches which also retain an absolute magnitude.
```{r}
# use (half-)wide format
par(mfrow=c(3,2))
boxplot_w(dat.nonnorm.summ.w,sample.info, 'raw')
for (i in 1:n.comp.variants){
boxplot_w(dat.norm.summ.w[[variant.names[i]]], sample.info, paste('normalized', variant.names[i], sep='_'))}
```
## MA plots
We then make MA plots of two single samples taken from condition `r ma.allsamples.num` and condition `r ma.allsamples.denom`, measured in different MS runs (samples *`r ma.onesample.num`* and *`r ma.onesample.denom`*, respectively).
Clearly, the normalization had a strong variance-reducing effect on the fold changes. It seems that fold changes after CONSTANd summarization are disproportionately small, because as we will see later, CONSTANd is only suitable for use with untransformed intensities. The two plots of quantile approaches are near-indistinguishable.
```{r}
# use wide2 format
p <- vector('list', n.comp.variants+1)
p[[1]] <- maplot_ils(dat.nonnorm.summ.w2, ma.onesample.num, ma.onesample.denom, scale.vec, 'raw', spiked.proteins)
for (i in 1: n.comp.variants){
p[[i+1]]<- maplot_ils(dat.norm.summ.w2[[variant.names[i]]], ma.onesample.num, ma.onesample.denom, scale.vec, paste('normalized', variant.names[i], sep='_'), spiked.proteins)}
grid.arrange(grobs = p, ncol=2, nrow=3)
```
To increase the robustness of these results, let's make some more MA plots, but now for all samples from condition `r ma.allsamples.num` and condition `r ma.allsamples.denom` (quantification values averaged within condition).
Indeed, even the raw, unnormalized data now show less variability. The two plots of quantile approaches are still near-indistinguishable.
```{r}
channels.num <- sample.info %>% filter(Condition==ma.allsamples.num) %>% select(Sample) %>% pull
channels.denom <- sample.info %>% filter(Condition==ma.allsamples.denom) %>% select(Sample) %>% pull
p <- vector('list', n.comp.variants+1)
p[[1]] <- maplot_ils(dat.nonnorm.summ.w2, channels.num, channels.denom, scale.vec, 'raw', spiked.proteins)
for (i in 1: n.comp.variants){
p[[i+1]]<- maplot_ils(dat.norm.summ.w2[[variant.names[i]]], channels.num, channels.denom, scale.vec, paste('normalized', variant.names[i], sep='_'), spiked.proteins)}
grid.arrange(grobs = p, ncol=2)
```
```{r, echo=FALSE}
#dat.nonnorm.summ.l <- to_long_format(dat.nonnorm.summ.w, sample.info)
dat.norm.summ.l <- lapply(dat.norm.summ.w, to_long_format, sample.info)
```
## PCA plots
Now, let's check if these multi-dimensional data contains some kind of grouping; It's time to make PCA plots.
### Using all proteins
Even though PC1 does seem to capture the conditions, providing a gradient for the dilution number, only the 0.125 condition is completely separable in the normalized data. Here, clearly both quantile approaches are insufficient as they only barely change the variance structure. Meanwhile, median sweeping, CONSTANd and NOMAD produce very similar PCA plots (CONSTANd's PC1 direction is just inverted).
```{r}
par(mfrow=c(3,2))
pcaplot_ils(dat.nonnorm.summ.w2 %>% select(-'Protein'), info=sample.info, 'raw')
for (i in 1:n.comp.variants){
pcaplot_ils(dat.norm.summ.w2[[variant.names[i]]] %>% select(-'Protein'), info=sample.info, paste('normalized', variant.names[i], sep='_'))}
```
There are only 19 proteins supposed to be differentially expressed in this data set, which is only a very small amount in both relative (to the 4083 proteins total) and absolute (for a biological sample) terms.
### Using spiked proteins only
Therefore, let's see what the PCA plots look like if we were to only use the spiked proteins in the PCA.
After normalization, the non-quantile variants produce similar plots where only conditions 0.5 and 0.667 aren't clearly separable. The quantile approaches again barely change the variance structure of the raw data.
```{r, eval=length(spiked.proteins)>0}
par(mfrow=c(3,2))
pcaplot_ils(dat.nonnorm.summ.w2 %>% filter(Protein %in% spiked.proteins) %>% select(-'Protein'), info=sample.info, 'raw')
for (i in 1:n.comp.variants){
pcaplot_ils(dat.norm.summ.w2[[variant.names[i]]] %>% filter(Protein %in% spiked.proteins) %>% select(-'Protein'), info=sample.info, paste('normalized', variant.names[i], sep='_'))}
```
Notice how for all PCA plots, the percentage of variance explained by PC1 is now much greater than when using data from all proteins.
In a real situation without spiked proteins, you might plot data corresponding to the top X most differential proteins instead.
## HC (hierarchical clustering) plots
The PCA plots of all proteins has a rather lower fraction of variance explained by PC1. We can confirm this using the hierarchical clustering dendrograms below: when considering the entire multidimensional space, the different conditions are not very separable at all (except for condition 0.125 after the non-quantile normalization approaches). This is not surprising as there is little biological variation between the conditions: there are only 19 truly differential proteins, and they all (ought to) covary in exactly the same manner (i.e., their variation can be captured in one dimension).
```{r, fig.width=12, fig.height=15}
par(mfrow=c(3,2))
dendrogram_ils(dat.nonnorm.summ.w2 %>% select(-Protein), info=sample.info, 'raw')
for (i in 1:n.comp.variants){
dendrogram_ils(dat.norm.summ.w2[[variant.names[i]]] %>% select(-Protein), info=sample.info, paste('normalized', variant.names[i], sep='_'))}
```
## Run effect p-value plot
Our last quality check involves a measure of how well each variant was able to assist in removing the run effect.
Below are the distributions of p-values from a linear model for the `response` variable with `Run` as a covariate.
If the run effect was removed successfully, these p-values ought to be large.
Clearly, the raw data contains a run effect, which is partially removed by median sweeping and as good as completely removed by CONSTANd and NOMAD. Both quantile approaches do not remove the run effect (their curves overlaps with the raw data curve).
```{r}
dat <- vector('list',length(dat.norm.summ.l)+1)
dat[[1]] <- dat.nonnorm.summ.l; dat[2:length(dat)] <- dat.norm.summ.l[1:length(dat.norm.summ.l)]
names(dat) <- c('raw', names(dat.norm.summ.l))
run_effect_plot(dat)
```
# DEA component: Moderated t-test
We look at the log2 fold changes of each condition w.r.t. the reference condition with dilution ratio `r ma.allsamples.denom`.
Since we are working with a log2 unit scale already, this means that for each protein we just look at the difference in mean observation across all channels between one condition and the reference condition.
Note that this is not the same as looking at the log2 of the ratio of mean raw intensities for each condition (left hand side below), nor the mean ratio of raw intensities for each condition (right hand side below), since $log_2 (\frac{mean(B)}{mean(A)}) \neq \frac{mean(log_2 (B))}{mean(log_2 (A))} \neq mean(\frac{log_2 (B)}{log_2 (A)})$.
To check whether these fold changes are significant (criterium: $q<0.05$), we use a [moderated t-test](http://www.biostat.jhsph.edu/~kkammers/software/eupa/R_guide.html) slightly adapted from the `limma` package, which in use cases like ours should improve statistical power over a regular t-test. In a nutshell, this is a t-test done independently for each protein, although the variance used in the calculation of the t-statistic is [moderated using some empirical Bayes estimation](https://doi.org/10.2202/1544-6115.1027).
After testing, we make a correction for multiple testing using the Benjamini-Hochberg method in order to keep the FDR under control.
<!--NOTE:
- actually, lmFit (used in moderated_ttest) was built for log2-transformed data. However, supplying untransformed intensities can also work. This just means that the effects in the linear model are also additive on the untransformed scale, whereas for log-transformed data they are multiplicative on the untransformed scale. Also, there may be a bias which occurs from biased estimates of the population means in the t-tests, as mean(X) is not equal to exp(mean(log(X))).-->
```{r, eval=!params$load_outputdata_p}
# design matrix as used in ANOVA testing.
design.matrix <- get_design_matrix(referenceCondition, sample.info)
dat.dea <- emptyList(names(dat.norm.summ.w2))
for (i in 1:n.comp.variants) {
this_scale <- scale.vec
d <- column_to_rownames(as.data.frame(dat.norm.summ.w2[[variant.names[i]]]), 'Protein')
dat.dea[[variant.names[i]]] <- moderated_ttest(dat=d, design.matrix, scale=this_scale)}
# also see what the unnormalized results would look like
n.comp.variants <- n.comp.variants + 1
variant.names <- c(variant.names, 'raw')
dat.dea$raw <- moderated_ttest(dat=column_to_rownames(dat.nonnorm.summ.w2, 'Protein'), design.matrix, scale='log')
```
For each condition, we now get the fold changes, moderated and unmoderated p-values, moderated and unmoderated q-values (BH-adjusted p-values), and some other details (head of dataframe below).
```{r}
display_dataframe_head(dat.dea[[1]])
```
# Results comparison
Now, the most important part: let's find out how our component variants have affected the outcome of the DEA.
## Confusion matrix
A confusion matrix shows how many true and false positives/negatives each variant has given rise to. Spiked proteins that are DE are true positives, background proteins that are not DE are true negatives. We calculate this matrix for all conditions and then calculate some other informative metrics based on the confusion matrices: accuracy, sensitivity, specificity, positive predictive value and negative predictive value.
Clearly, across the board, the quantile normalization approaches underperform while median sweeping, CONSTANd and NOMAD are on equal footing, with an acceptable but not spectacular sensitivity. That said, the contrast between conditions 0.667 and 0.5 seems not large enough to yield many significant results.
```{r, results='asis'}
cm <- conf_mat(dat.dea, 'q.mod', 0.05, spiked.proteins)
print_conf_mat(cm, referenceCondition)
```
## Correlation scatter plots
To see whether the three normalization methods produce similar results on the detailed level of individual proteins, we make scatter plots and check the correlation between their fold changes and between their significance estimates (q-values, in our case).
```{r,echo=FALSE,results=FALSE}
# character vectors containing logFC and p-values columns
dea.cols <- colnames(dat.dea[[1]])
logFC.cols <- dea.cols[stri_detect_fixed(dea.cols, 'logFC')]
significance.cols <- dea.cols[stri_detect_fixed(dea.cols, 'q.mod')]
n.contrasts <- length(logFC.cols)
```
For all conditions, we see the q-values of the non-quantile methods correlate well ($>0.874$) with each other, especially for the spike-in proteins with low q-values. Both quantile approaches, have highly correlated q-values and highly correlated fold changes with respect to each other, but not with respect to the other methods.
```{r}
scatterplot_ils(dat.dea, significance.cols, 'q-values', spiked.proteins, referenceCondition)
```
The fold change estimates of both the non-quantile and quantile methods correlate very well ($>0.972$), though the CONSTANd graph seems rotated: the ratios are disproportionately compressed. It is remarkable that even though the quantile methods seem to underperform across the board, they still produce reliable fold change estimates.
```{r}
scatterplot_ils(dat.dea, logFC.cols, 'log2FC', spiked.proteins, referenceCondition)
```
## Volcano plots
The volcano plot combines information on fold changes and statistical significance. The spike-in proteins are colored blue, and immediately it is clear that their fold changes dominate the region of statistical significance, which suggests the experiment and analysis were carried out successfully. The magenta, dashed line indicates the theoretical fold change of the spike-ins.
The quantile plots are extremely flat, suggesting the approach is not powerufl enough. We can again see how CONSTANd fold changes are disproportionately low in absolute value.
```{r}
for (i in 1:n.contrasts){
volcanoplot_ils(dat.dea, i, spiked.proteins, referenceCondition)}
```
## Violin plots
A good way to assess the general trend of the fold change estimates on a more 'macroscopic' scale is to make a violin plot. Ideally, there will be some spike-in proteins that attain the expected fold change (red dashed line) that corresponds to their condition, while most (background) protein log2 fold changes are situated around zero.
Clearly, the empirical results _tend towards_ the theoretical truth, but not a single observation attained the fold change it should have attained. There is clearly a strong bias towards zero fold change, which may partly be explained by the ratio compression phenomenon in mass spectrometry, although the effect seems quite extreme here.
In general, all fold change distributions (except those of CONSTANd whose distribution is too narrow) are quite similar, even those of the raw data, although it is noticeably more spread out.
```{r}
# plot theoretical value (horizontal lines) and violin per variant
if (length(spiked.proteins)>0) violinplot_ils(lapply(dat.dea, function(x) x[spiked.proteins, logFC.cols]), referenceCondition) else violinplot_ils(lapply(dat.dea, function(x) x[,logFC.cols]), referenceCondition, show_truth = FALSE)
```
```{r, echo=FALSE, eval=params$save_outputdata_p}
save(dat.nonnorm.summ.l
,dat.norm.summ.l
,dat.nonnorm.summ.w
,dat.norm.summ.w
,dat.nonnorm.summ.w2
,dat.norm.summ.w2
,dat.dea, file=paste0('datadriven_normalization_outdata', params$suffix_p, '.rda'))
```
# Conclusions
For the given data set, the differences in proteomic outcomes between median sweeping and NOMAD normalization are quite small, both on the global and individual scale.
The quantile methods seem to underperform across the board, but they still produce reliable fold change estimates.
Finally, CONSTANd naturally reduces the variance in the distribution of quantification values and is only suitable for use with untransformed intensities. When used on log2-transformed values like we did here, there is a double variance-reducing effect that ends up over-compressing the fold change estimates.
However, when applied to untransformed intensities like in [this bonus notebook](CONSTANd_vs_medianSweeping.html), the CONSTANd method performs at least on par with median sweeping!
# Session information
```{r}
sessionInfo()
```