-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDE_protein_quantification
147 lines (125 loc) · 5.5 KB
/
DE_protein_quantification
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
Masspec for analysis
#Source of the script for massspec analysishttp://www.biostat.jhsph.edu/~kkammers/software/eupa/R_guide.html
#source("http://bioconductor.org/biocLite.R")
#biocLite()
#biocLite("limma")
#biocLite("qvalue")
#library(limma)
#library(qvalue)
dat <- read.table("exampledata.tab", header=TRUE, row.names=1)
dat <- overview_unique_peptide_counts
dim(dat)
str(dat)
cha <- c( "A5","A6","B5","B6","C5","C6","D5","D6")
#source("http://www.biostat.jhsph.edu/~kkammers/software/eupa/source.functions.r") (can also be loaded from the link )
#The function eb.fit (eb: empirical Bayes) performes the statistical analysis of interest: two sample t-tests.
#The output of this procedure is a data frame that especially contains for each protein its log2-fold-change, and ordinary as well as moderated p-values and q-values.
#The results are sorted by moderated p-values in increasing order. It can also be loaded from the link
#source("http://www.biostat.jhsph.edu/~kkammers/software/eupa/source.functions.r")
function(dat, design){
n <- dim(dat)[1]
fit <- lmFit(dat, design)
fit.eb <- eBayes(fit)
logFC <- fit.eb$coefficients[, 2]
df.r <- fit.eb$df.residual
df.0 <- rep(fit.eb$df.prior, n)
s2.0 <- rep(fit.eb$s2.prior, n)
s2 <- (fit.eb$sigma)^2
s2.post <- fit.eb$s2.post
t.ord <- fit.eb$coefficients[, 2]/fit.eb$sigma/fit.eb$stdev.unscaled[, 2]
t.mod <- fit.eb$t[, 2]
p.ord <- 2*pt(-abs(t.ord), fit.eb$df.residual)
p.mod <- fit.eb$p.value[, 2]
q.ord <- qvalue(p.ord)$q
q.mod <- qvalue(p.mod)$q
results.eb <- data.frame(logFC, t.ord, t.mod, p.ord, p.mod, q.ord, q.mod, df.r, df.0, s2.0, s2, s2.post)
results.eb <- results.eb[order(results.eb$p.mod), ]
return(results.eb)
}
#The function eb.fit.mult is an extension of eb.fit when multiple experiments are present (see next section).
function(dat, design){
n <- dim(dat)[1]
fit <- lmFit(dat, design)
fit.eb <- eBayes(fit)
logFC <- fit.eb$coef[, "tr2"]
df.0 <- rep(fit.eb$df.prior, n)
df.r <- fit.eb$df.residual
s2.0 <- rep(fit.eb$s2.prior, n)
s2 <- (fit.eb$sigma)^2
s2.post <- fit.eb$s2.post
t.ord <- fit.eb$coef[, "tr2"]/fit.eb$sigma/fit.eb$stdev.unscaled[, "tr2"]
t.mod <- fit.eb$t[, "tr2"]
p.ord <- 2*pt(-abs(t.ord), fit.eb$df.residual)
p.mod <- fit.eb$p.value[, "tr2"]
q.ord <- qvalue(p.ord)$q
q.mod <- qvalue(p.mod)$q
results.eb.mult <- data.frame(logFC, t.ord, t.mod, p.ord, p.mod, q.ord, q.mod, df.0, df.r, s2.0, s2, s2.post)
results.eb.mult <- results.eb.mult[order(results.eb.mult$p.mod), ]
return(results.eb.mult)
}
# identify proteins from peptide spectra
function(dat, cha){
e.function <- function(x, seq) tapply(x, seq, median)
output <- NULL
dat$Sequence <- toupper(dat$Sequence) # Capital letters
accessions <- as.character(unique(dat$Protein.Group.Accessions))
n.proteins <- length(accessions)
n.cha <- length(cha)
for(k in 1:n.proteins){
id <- accessions[k]
sdat <- subset(dat, Protein.Group.Accessions==id)[c("Sequence", cha)]
sdat[cha] <- log2(sdat[cha])
sdat[cha] <- sdat[cha] - apply(sdat[cha], 1, median)
pdat <- sdat[, -1]
n.spectra <- ifelse(is.integer(dim(pdat)), nrow(pdat), 1)
temp <- apply(sdat[,-1], 2, e.function,seq=sdat[, 1])
n.peptides <- ifelse(is.integer(dim(temp)), nrow(temp), 1)
if(is.integer(dim(pdat))) pdat <- apply(pdat, 2, median)
pdat <- c(pdat, n.peptides=n.peptides, n.spectra=n.spectra)
output <- rbind(output, pdat)
}
output[,1:n.cha] <- sweep(output[,1:n.cha],1,apply(output[,1:n.cha],1,median))
output[,1:n.cha] <- sweep(output[,1:n.cha],2,apply(output[,1:n.cha],2,median))
output[,1:n.cha] <- sweep(output[,1:n.cha],1,apply(output[,1:n.cha],1,median))
output[,1:n.cha] <- round(output[,1:n.cha],3)
row.names(output) <- accessions
output <- as.data.frame(output)
return(output)
}
dat <- read.peptides(dat, cha) #(This may give error depending upon the data you are using )
dim(dat)
# identify proteins from peptide spectra
dat <- quantify.proteins(dat, cha)
dat.onehit <- subset(dat, dat$n.peptides == 1)
dim(dat.onehit)
dat <- subset(dat, dat$n.peptides != 1)
dim(dat) # 307 proteins are identified by a
par(mfrow=c(1,1), font.lab=2, cex.lab=1.2, font.axis=2, cex.axis=1.2)
boxplot(dat[, 1:length(cha)], ylim = c(-3, 3), main="Boxplot normalized Intensities")
tr <- c("A5", "A6", "B5", "B6")
ct <- c("C5", "C6", "D5", "D6")
# define design according to syntax of limma package
design <- model.matrix(~factor(c(2,2,2,2,1,1,1,1)))
design
colnames(design) <- c("Intercept", "Diff")
res.eb <- eb.fit(dat[, c(tr,ct)], design)
head(res.eb)
# volcano plots for ordinary and moderated p-values
rx <- c(-1, 1)*max(abs(res.eb$logFC))*1.1
ry <- c(0, ceiling(max(-log10(res.eb$p.ord), -log10(res.eb$p.mod))))
par(mfrow=c(1,2), font.lab=2, cex.lab=1.2, font.axis=2, cex.axis=1.2)
par(las=1, xaxs="i", yaxs="i")
plot(res.eb$logFC, -log10(res.eb$p.ord), pch=21, bg="lightgrey", cex=0.9,
xlim=rx, ylim=ry, xaxt="n",
xlab="fold change", ylab="-log10 p-value")
abline(v=seq(-2,2,1), col="lightgray", lty="dotted")
abline(h=seq(0,ry[2],1), col="lightgray", lty="dotted")
axis(1, seq(-2,2,1), paste(c("1/4","1/2","1/1","2/1","4/1")))
title("volcano plot of ordinary p-values")
plot(res.eb$logFC, -log10(res.eb$p.mod), pch=21, bg="lightgrey", cex=0.9,
xlim=rx, ylim=ry, xaxt="n",
xlab="fold change", ylab="-log10 p-value")
abline(v=seq(-2,2,1), col="lightgray", lty="dotted")
abline(h=seq(0,6,1), col="lightgray", lty="dotted")
axis(1, seq(-2,2,1), paste(c("1/4","1/2","1/1","2/1","4/1")))
title("volcano plot of moderated p-values")