forked from wasimaftab/LIMMA-pipeline-proteomics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limma_main.R
545 lines (505 loc) · 20.1 KB
/
limma_main.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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#This is a pipeline to analyze proteiomic data in a proteinGroups.txt (MaxQuant output) file for two group comparision
#Author:Wasim Aftab
cat('\014')
rm(list = ls())
## Installing Bioconductor packages
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
list.of.packages <- c("limma", "qvalue")
new.packages <-
list.of.packages[!(list.of.packages %in% installed.packages()[, "Package"])]
if (length(new.packages))
BiocManager::install(new.packages)
## Installing CRAN packages
list.of.packages <-
c(
"dplyr",
"stringr",
"MASS",
"matlab",
"plotly",
"htmlwidgets",
"rstudioapi",
"webshot",
"matrixStats"
)
new.packages <-
list.of.packages[!(list.of.packages %in% installed.packages()[, "Package"])]
if (length(new.packages))
install.packages(new.packages)
if (is.null(webshot:::find_phantom())) {
webshot::install_phantomjs()
}
library(dplyr)
library(stringr)
library(MASS)
library(matlab)
library(plotly)
library(limma)
library(qvalue)
library(htmlwidgets)
library(rstudioapi)
## Chdir to source dir
path <- rstudioapi::getActiveDocumentContext()$path
Encoding(path) <- "UTF-8"
setwd(dirname(path))
cur_dir <- getwd()
source("limma_helper_functions.R")
## Load the proteingroups file
myFilePath <- file.choose()
temp <- unlist(strsplit(myFilePath, "\\", fixed = TRUE))
proteingroups <-
as.data.frame(read.table(myFilePath, header = TRUE, sep = "\t"))
## Remove contaminant proteins("+" identified rows) from proteingroups dataframe
temp <-
select(
proteingroups,
matches("(Reverse|Potential.contaminant|Only.identified.by.site)")
)
if (!nrow(temp) * ncol(temp)) {
print(
"File does not contain columns to determine contaminant proteins. Therefore, assuming all contaminants are aleady removed"
)
} else{
idx <- NULL
for (i in 1:ncol(temp)) {
index <- which(unlist(!is.na(match(temp[, i], "+"))))
idx <- union(idx, index)
}
proteingroups <- proteingroups[-idx, ] # removing indexed rows
print(paste("Removed", length(idx), "contaminat proteins"))
}
######################################################################################################
## Choose if you want to remove outliers before analysis
flag <- readline(
cat(
'Enter 1: if you want to remove exclusively enriched proteins before analysis\n',
'\bEnter 0: if you want to use full data for analysis\n',
'\b(for definition of "exclusively enriched proteins" see README)= '
)
)
flag <- as.integer(flag)
## Sanity check flag values
if (!length(which(c(0, 1) == flag))) {
stop('WRONG value in flag: it can hold either 0 or 1')
}
if (!flag) {
##Display data to faciliate choice of treatment and control
temp <- select(proteingroups, matches("(ibaq|lfq)"))
print(names(temp))
# ################################################################################################
#Extract Uniprot and gene symbols
Uniprot <- character(length = nrow(proteingroups))
Symbol <- character(length = nrow(proteingroups))
for (i in 1:nrow(proteingroups)) {
temp <- as.character(proteingroups$Fasta.headers[i])
splits <- unlist(strsplit(temp, '\\|'))
Uniprot[i] <- splits[2]
splits <- unlist(str_match(splits[3], "GN=(.*?) PE="))
Symbol[i] <- splits[2]
}
#Extract required data for Limma
treatment <-
readline('Enter treatment name(case insensitive) as it appeared in the iBAQ/LFQ column= ')
control <-
readline('Enter control name(case insensitive) as it appeared in the iBAQ/LFQ column= ')
ibaq <- readinteger_binary('Enter 1 for iBAQ or 0 for LFQ= ')
if (ibaq) {
temp <-
select(proteingroups, matches(paste('^.*', "ibaq", '.*$', sep = '')))
treatment_reps <-
data_sanity_check(temp, 'treatment', treatment)
control_reps <- select(temp, matches(control))
control_reps <- data_sanity_check(temp, 'control', control)
data <-
cbind(treatment_reps,
control_reps,
select(proteingroups, matches("^id$")),
Uniprot,
Symbol)
} else {
temp <-
select(proteingroups, matches(paste('^.*', "lfq", '.*$', sep = '')))
treatment_reps <- select(temp, matches(treatment))
treatment_reps <-
data_sanity_check(temp, 'treatment', treatment)
control_reps <- select(temp, matches(control))
control_reps <- data_sanity_check(temp, 'control', control)
data <-
cbind(treatment_reps,
control_reps,
select(proteingroups, matches("^id$")),
Uniprot,
Symbol)
}
## Impute data
print(names(data))
rep_treats <-
readinteger("Enter the number of treatment replicates=")
rep_conts <-
readinteger("Enter the number of control replicates=")
FC_Cutoff <- readfloat("Enter the log fold change cut off=")
## removing blank rows
temp <-
as.matrix(rowSums(apply(data[, 1:(rep_treats + rep_conts)], 2, as.numeric)))
idx <- which(temp == 0)
if (length(idx)) {
data <- data[-idx,]
}
data_limma <- log2(as.matrix(data[c(1:(rep_treats + rep_conts))]))
data_limma[is.infinite(data_limma)] <- NA
nan_idx <- which(is.na(data_limma))
# temp <- reshape(temp, nrow(data_limma)*ncol(data_limma), 1)
# hist(temp, na.rm = TRUE, xlab = "log2(intensity)", ylab = "Frequency",
# main = "All data: before imputation")
fit <- fitdistr(c(na.exclude(data_limma)), "normal")
mu <- as.double(fit$estimate[1])
sigma <- as.double(fit$estimate[2])
sigma_cutoff <- 6
new_width_cutoff <- 0.3
downshift <- 1.8
width <- sigma_cutoff * sigma
new_width <- width * new_width_cutoff
new_sigma <- new_width / sigma_cutoff
new_mean <- mu - downshift * sigma
## set seed to reproduce results
set.seed(100)
imputed_vals_my = rnorm(length(nan_idx), new_mean, new_sigma)
# scaling_factor <- readfloat_0_1("Enter a number > 0 and <=1 to scale imputed values = ")
# data_limma[nan_idx] <- imputed_vals_my*scaling_factor
data_limma[nan_idx] <- imputed_vals_my
## Median Normalization Module
want_normalization <- as.integer(readline(
cat(
'Enter 1: if you want to normalize the protein intensities in each experiemnt by substrating the median of the corresponding experiment\n',
'\bEnter 2: if you want to perform column wise median normalization of the data matrix\n',
'\b(for definition of "column wise median normalization" see README)= '
)
))
if (want_normalization == 1) {
browser()
# boxplot(data_limma[,1:rep_treats], main = paste(treatment, "replicates before normalization"))
# boxplot(data_limma[,(rep_treats+1):(rep_treats+rep_conts)], main = paste(control, "replicates before normalization"))
par(mar=c(1,1,1,1))
boxplot(data_limma[, 1:(rep_treats + rep_conts)], main = "data before median substract normalization")
col_med <- matrixStats::colMedians(data_limma)
med_mat <- matlab::repmat(col_med, nrow(data_limma), 1)
data_limma <- data_limma - med_mat
par(mar=c(1,1,1,1))
boxplot(data_limma[, 1:(rep_treats + rep_conts)], main = "data after median substract normalization")
# browser()
# boxplot(data_limma[,1:rep_treats], main = paste(treatment, "replicates after normalization"))
# boxplot(data_limma[,(rep_treats+1):(rep_treats+rep_conts)], main = paste(control, "replicates after normalization"))
} else if (want_normalization == 2) {
par(mar=c(1,1,1,1))
boxplot(data_limma[, 1:(rep_treats + rep_conts)], main = "data before column wise median normalization")
data_limma <- median_normalization(data_limma)
par(mar=c(1,1,1,1))
boxplot(data_limma[, 1:(rep_treats + rep_conts)], main = "data after column wise median normalization")
# browser()
# boxplot(data_limma[,1:rep_treats], main = paste(treatment, "replicates after normalization"))
# boxplot(data_limma[,(rep_treats+1):(rep_treats+rep_conts)], main = paste(control, "replicates after normalization"))
}
# temp <- reshape(temp, nrow(data_limma)*ncol(data_limma), 1)
# hist(temp, na.rm = TRUE, xlab = "log2(intensity)", ylab = "Frequency",
# main = "All data: after imputation")
Symbol <- data$Symbol
Uniprot <- data$Uniprot
##Limma main code
design <-
model.matrix( ~ factor(c(rep(2, rep_treats), rep(1, rep_conts))))
colnames(design) <- c("Intercept", "Diff")
res.eb <- eb.fit(data_limma, design, Symbol)
Sig_FC_idx <-
union(which(res.eb$logFC < (-FC_Cutoff)), which(res.eb$logFC > FC_Cutoff))
Sig_Pval_mod_idx <- which(res.eb$p.mod < 0.05)
Sig_Pval_ord_idx <- which(res.eb$p.ord < 0.05)
Sig_mod_idx <- intersect(Sig_FC_idx, Sig_Pval_mod_idx)
Sig_ord_idx <- intersect(Sig_FC_idx, Sig_Pval_ord_idx)
categ_Ord <- rep(c("Not Significant"), times = length(Symbol))
categ_Mod <- categ_Ord
categ_Mod[Sig_mod_idx] <- "Significant"
categ_Ord[Sig_ord_idx] <- "Significant"
dat <-
cbind(
res.eb,
categ_Ord,
categ_Mod,
NegLogPvalMod = (-log10(res.eb$p.mod)),
NegLogPvalOrd = (-log10(res.eb$p.ord))
)
##Save the data file
final_data <-
cbind(select(data, matches("^id$")),
Uniprot,
Symbol,
data_limma,
dat)
final_data <- select(final_data, -matches("^gene$"))
filename_final_data <-
paste0(format(Sys.time(), "%Y%m%d_%H%M%S"), '_final_data')
# readline('Enter a filename for final data= ')
##Create plotly object and save plot as html
filename_mod <-
paste0(format(Sys.time(), "%Y%m%d_%H%M%S"), '_limma_plot')
# readline('Enter a filename for limma plot= ')
filename_ord <-
paste0(format(Sys.time(), "%Y%m%d_%H%M%S"), '_ord_plot')
# readline('Enter a filename for ordinary t-test plot= ')
display_plotly_figs(final_data, FC_Cutoff, filename_mod, filename_ord)
write.table(
final_data,
paste(filename_final_data, '.tsv', sep = ''),
sep = '\t',
row.names = FALSE,
col.names = TRUE
)
setwd(cur_dir)
} else{
## Display data to faciliate choice of treatment and control
temp <- select(proteingroups, matches("(ibaq|lfq)"))
print(names(temp))
#Extract Uniprot and gene symbols
Uniprot <- character(length = nrow(proteingroups))
Symbol <- character(length = nrow(proteingroups))
for (i in 1:nrow(proteingroups)) {
temp <- as.character(proteingroups$Fasta.headers[i])
splits <- unlist(strsplit(temp, '\\|'))
Uniprot[i] <- splits[2]
splits <- unlist(str_match(splits[3], "GN=(.*?) PE="))
Symbol[i] <- splits[2]
}
## Extract data for Limma
treatment <-
readline('Enter treatment name(case insensitive) as it appeared in the iBAQ/LFQ column= ')
control <-
readline('Enter control name(case insensitive) as it appeared in the iBAQ/LFQ column= ')
ibaq <-
readinteger_binary('Enter 1 for iBAQ or 0 for LFQ= ')
if (ibaq) {
temp <-
select(proteingroups, matches(paste('^.*', "ibaq", '.*$', sep = '')))
# browser()
treatment_reps <-
data_sanity_check(temp, 'treatment', treatment)
control_reps <- select(temp, matches(control))
control_reps <- data_sanity_check(temp, 'control', control)
data <-
cbind(treatment_reps,
control_reps,
select(proteingroups, matches("^id$")),
Uniprot,
Symbol)
} else {
temp <-
select(proteingroups, matches(paste('^.*', "lfq", '.*$', sep = '')))
treatment_reps <- select(temp, matches(treatment))
treatment_reps <-
data_sanity_check(temp, 'treatment', treatment)
control_reps <- select(temp, matches(control))
control_reps <- data_sanity_check(temp, 'control', control)
data <-
cbind(treatment_reps,
control_reps,
select(proteingroups, matches("^id$")),
Uniprot,
Symbol)
}
## Find out Blank rows, i.e. proteins with all zeros in treatment and in control, see followig example
## iBAQ.Mrpl40_1 iBAQ.Mrpl40_2 iBAQ.Mrpl40_3 iBAQ.Kgd4_1 iBAQ.Kgd4_2 iBAQ.Kgd4_3 id Uniprot Symbol
## -------------------------------------------------------------------------------------------------
## 0 0 0 0 0 0 84 Q02888 INA17
print(names(data))
rep_treats <-
readinteger("Enter the number of treatment replicates=")
rep_conts <-
readinteger("Enter the number of control replicates=")
FC_Cutoff <- readfloat("Enter the log fold change cut off=")
temp <-
as.matrix(rowSums(apply(data[, 1:(rep_treats + rep_conts)], 2, as.numeric)))
idx <- which(temp == 0)
if (length(idx)) {
data <- data[-idx,] # removing blank rows
}
## Find out exclusive proteins in control group, i.e. proteins with all zeros in treatment and all/some values in control, see followig example
## Uniprot Symbol treat_1 treat_2 treat_3 contrl_1 contrl_2 contrl_3
## -----------------------------------------------------------------------------------
## P25554 SGF29 0 0 0 2810900 0 0
## -----------------------------------------------------------------------------------
temp <-
as.matrix(rowSums(apply(data[, 1:rep_treats], 2, as.numeric)))
idx <- which(temp == 0)
if (length(idx)) {
outliers <- data[idx,]
filename_outliers <-
paste("exclusive_proteins_control_",
treatment,
"_",
control,
sep = "")
data <- data[-idx, ] # removing indexed rows
}
## Find out exclisive proteins in treatment group, i.e. proteins with all zeros in control and all/some values in treatment, see followig example
## iBAQ.Mrpl40_1 iBAQ.Mrpl40_2 iBAQ.Mrpl40_3 iBAQ.Kgd4_1 iBAQ.Kgd4_2 iBAQ.Kgd4_3 id Uniprot Symbol
## -----------------------------------------------------------------------------------------------------
## 662810 505600 559130 0 0 0 79 P38845 CRP1
## -----------------------------------------------------------------------------------------------------
temp <-
as.matrix(rowSums(apply(data[, (rep_treats + 1):(rep_conts + rep_treats)], 2, as.numeric)))
idx <- which(temp == 0)
if (length(idx)) {
outliers_control <- data[idx,]
filename_outliers_control <-
paste("exclusive_proteins_treatment_",
treatment,
"_",
control,
sep = "")
data <- data[-idx, ] # removing indexed rows
}
## Extract only those proteins/peptides that has intensity values in
## K out of N replicates in each group
# k_out_N_treatment <- read_k_out_of_N(rep_treats, 'treatment')
# k_out_N_control <- read_k_out_of_N(rep_treats, 'control')
k_out_N_treatment <- get_k_out_of_N(rep_treats, 'treatment')
k_out_N_control <- get_k_out_of_N(rep_conts, 'control')
idx_nz_treatment <-
which(rowSums(data[, 1:rep_treats] != 0) >= k_out_N_treatment)
idx_nz_control <-
which(rowSums(data[, (rep_treats + 1):(rep_conts + rep_treats)] != 0) >= k_out_N_control)
idx_nz_both <- intersect(idx_nz_control, idx_nz_treatment)
if (length(idx_nz_both)) {
data <- data[idx_nz_both, ]
}
## Impute missing values
data_limma <-
log2(apply(data[c(1:(rep_treats + rep_conts))], 2, as.numeric))
data_limma[is.infinite(data_limma)] <- NA
nan_idx <- which(is.na(data_limma))
# temp <- reshape(data_limma, nrow(data_limma)*ncol(data_limma), 1)
# hist(temp, na.rm = TRUE, xlab = "log2(intensity)", ylab = "Frequency",
# main = "All data: before imputation (nan ignored)")
fit <- fitdistr(c(na.exclude(data_limma)), "normal")
mu <- as.double(fit$estimate[1])
sigma <- as.double(fit$estimate[2])
sigma_cutoff <- 6
new_width_cutoff <- 0.3
downshift <- 1.8
width <- sigma_cutoff * sigma
new_width <- width * new_width_cutoff
new_sigma <- new_width / sigma_cutoff
new_mean <- mu - downshift * sigma
## set seed to reproduce results
set.seed(100)
imputed_vals_my = rnorm(length(nan_idx), new_mean, new_sigma)
# scaling_factor <- readfloat_0_1("Enter a number > 0 and <=1 to scale imputed values = ")
# data_limma[nan_idx] <- imputed_vals_my*scaling_factor
data_limma[nan_idx] <- imputed_vals_my
## Median Normalization Module
want_normalization <- as.integer(readline(
cat(
'Enter 1: if you want to normalize the protein intensities in each experiemnt by substrating the median of the corresponding experiment\n',
'\bEnter 2: if you want to perform column wise median normalization of the data matrix\n',
'\b(for definition of "column wise median normalization" see README)= '
)
))
if (want_normalization == 1) {
# browser()
# boxplot(data_limma[,1:rep_treats], main = paste(treatment, "replicates before normalization"))
# boxplot(data_limma[,(rep_treats+1):(rep_treats+rep_conts)], main = paste(control, "replicates before normalization"))
par(mar=c(1,1,1,1))
boxplot(data_limma[, 1:(rep_treats + rep_conts)], main = "data before median substract normalization")
col_med <- matrixStats::colMedians(data_limma)
med_mat <- matlab::repmat(col_med, nrow(data_limma), 1)
data_limma <- data_limma - med_mat
par(mar=c(1,1,1,1))
boxplot(data_limma[, 1:(rep_treats + rep_conts)], main = "data after median substract normalization")
# browser()
# boxplot(data_limma[,1:rep_treats], main = paste(treatment, "replicates after normalization"))
# boxplot(data_limma[,(rep_treats+1):(rep_treats+rep_conts)], main = paste(control, "replicates after normalization"))
} else if (want_normalization == 2) {
par(mar=c(1,1,1,1))
boxplot(data_limma[, 1:(rep_treats + rep_conts)], main = "data before column wise median normalization")
data_limma <- median_normalization(data_limma)
par(mar=c(1,1,1,1))
boxplot(data_limma[, 1:(rep_treats + rep_conts)], main = "data after column wise median normalization")
# browser()
# boxplot(data_limma[,1:rep_treats], main = paste(treatment, "replicates after normalization"))
# boxplot(data_limma[,(rep_treats+1):(rep_treats+rep_conts)], main = paste(control, "replicates after normalization"))
}
# temp <- reshape(temp, nrow(data_limma)*ncol(data_limma), 1)
# hist(temp, na.rm = TRUE, xlab = "log2(intensity)", ylab = "Frequency",
# main = "All data: after imputation")
Symbol <- data$Symbol
Uniprot <- data$Uniprot
##Limma main code
design <-
model.matrix( ~ factor(c(rep(2, rep_treats), rep(1, rep_conts))))
colnames(design) <- c("Intercept", "Diff")
res.eb <- eb.fit(data_limma, design, Symbol)
Sig_FC_idx <-
union(which(res.eb$logFC < (-FC_Cutoff)), which(res.eb$logFC > FC_Cutoff))
Sig_Pval_mod_idx <- which(res.eb$p.mod < 0.05)
Sig_Pval_ord_idx <- which(res.eb$p.ord < 0.05)
Sig_mod_idx <- intersect(Sig_FC_idx, Sig_Pval_mod_idx)
Sig_ord_idx <- intersect(Sig_FC_idx, Sig_Pval_ord_idx)
categ_Ord <- rep(c("Not Significant"), times = length(Symbol))
categ_Mod <- categ_Ord
categ_Mod[Sig_mod_idx] <- "Significant"
categ_Ord[Sig_ord_idx] <- "Significant"
dat <-
cbind(
res.eb,
categ_Ord,
categ_Mod,
NegLogPvalMod = (-log10(res.eb$p.mod)),
NegLogPvalOrd = (-log10(res.eb$p.ord))
)
##Save the data file
final_data <-
cbind(select(data, matches("^id$")),
Uniprot,
Symbol,
data_limma,
dat)
final_data <- select(final_data, -matches("^gene$"))
filename_final_data <-
paste0(format(Sys.time(), "%Y%m%d_%H%M%S"), '_final_data')
# readline('Enter a filename for final data= ')
##Create plotly object and save plot as html
filename_mod <-
paste0(format(Sys.time(), "%Y%m%d_%H%M%S"), '_limma_plot')
# readline('Enter a filename for limma plot= ')
filename_ord <-
paste0(format(Sys.time(), "%Y%m%d_%H%M%S"), '_ord_plot')
# readline('Enter a filename for ordinary t-test plot= ')
display_plotly_figs(final_data, FC_Cutoff, filename_mod, filename_ord)
write.table(
final_data,
paste(filename_final_data, '.tsv', sep = ''),
sep = '\t',
row.names = FALSE,
col.names = TRUE
)
## Write outliers in treatment
if (exists('outliers')) {
write.table(
outliers,
paste(filename_outliers, '.tsv', sep = ''),
sep = '\t',
row.names = FALSE,
col.names = TRUE
)
}
## Write outliers in control
if (exists('outliers_control')) {
write.table(
outliers_control,
paste(filename_outliers_control, '.tsv', sep = ''),
sep = '\t',
row.names = FALSE,
col.names = TRUE
)
}
setwd(cur_dir)
} ### END of if(flag)else