-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing_workflow.R
239 lines (199 loc) · 6.93 KB
/
preprocessing_workflow.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
rm(list=ls())
mainDir <- "~/MS/data/pos"
newDir <- "170626"
unlink(file.path(mainDir,newDir))
dir.create(file.path(mainDir,newDir))
setwd(file.path(mainDir,newDir))
load(file.path(mainDir,"df.RData")) # load raw data
load(file.path(mainDir,"mz_cf.RData")) # load consensus mz
load(file.path(mainDir,"rt_cf.RData")) # load consensus rt
load(file.path(mainDir,"spikes.RData")) # load spiked indices
source('Functions.R') # source function scripts
################################################
#
# Start of pipeline
#
################################################
### Catch output log
sink('log.txt')
### Remove the first five samples in analysis sequence
names(df[,c(1:5)])
df <- df[,-c(1:5)]
df[df==0] <- NA
### Extract and remove
mz_spikes <- mz_cf[spikes]
mz_cf <- mz_cf[-spikes]
rt_spikes <- rt_cf[spikes]
rt_cf <- rt_cf[-spikes]
ref_spikes <- df[spikes,]
df <- df[-spikes,]
cat("Initial number of features: ", nrow(df), "\n")
### Remove NA rows
ind = remove.narows(df)
if (any(ind)) {
df <- df[!ind, ]
mz_cf <- mz_cf[!ind]
rt_cf <- rt_cf[!ind]
}
### Blankfiltering - removing contaminants
blanks <- specgrep(df,"Blank")
samples.bf <- removegrep(df, "Blank")
to.remove <- BlankFilter(blanks,samples.bf,0.01)
samples.bf <- samples.bf[-to.remove,]
mz_cf <- mz_cf[-to.remove]
rt_cf <- rt_cf[-to.remove]
cat("Blankfiltering", "\n", "Removed by BlankFilter: ",length(to.remove), "\n")
rm(to.remove)
### Extract QC samples
qcs <- specgrep(samples.bf,"QC")
samples.bf.cov <- removegrep(samples.bf,"QC")
### Dilution based filtering
d <- specgrep(samples.bf.cov,"D")
samples.bf.cov.dil <- removegrep(samples.bf.cov,"D")
conc <- c(0.5,1,2,4,8,16,32)
# Calculate correlation between QC concentration and feature
c = list()
for (i in 1:dim(d)[1]) {
x <- as.numeric(d[i,])
y <- conc
ind <- which(is.na(x))
if (length(ind) > 0) {
x <- x[-ind]
y <- y[-ind]
}
if (length(x) > 2) {
c[[i]] = cor.test(x,y,use="p")
} else {
c[[i]] = 0
}
}
# Find features with an absolute correlation higher than 0.5
significant = list()
to.keep = c()
n = 1
for (j in 1:length(c)) {
if (length(c[[j]])>1){
if (c[[j]]$p.value < 0.1 && !is.na(c[[j]]$p.value)) {
if (c[[j]]$estimate < -0.5 | c[[j]]$estimate > 0.5) {
significant[[n]] <- c[[j]]
to.keep[n] <- j
n = n + 1
}
}
}
}
cat("Dilution based filtering", "\n", "Features which correlate with the dilution serie: ", length(to.keep), "\n")
samples.bf.cov.dil = samples.bf.cov.dil[to.keep,]
mz_cf <- mz_cf[to.keep]
rt_cf <- rt_cf[to.keep]
rm(to.keep)
### log2 transformation
cat("log2 transformation", "\n")
samples.bf.cov.dil.log = log2(samples.bf.cov.dil)
### Outlier exclusion
cat("Outlier exclusion", "\n")
TIC <- colSums(samples.bf.cov.dil.log, na.rm=T)
par(mfrow = c(1,2))
boxplot(TIC, ylab = "Extracted peaks area")
hist(TIC, breaks = 100, main = "", xlab = "Extracted peaks area")
samples.bf.cov.dil.log.oe <- samples.bf.cov.dil.log[,!(colnames(samples.bf.cov.dil.log)
%in% c(colnames(samples.bf.cov.dil.log)[TIC<mean(TIC)*0.7]))]
cat("Number of samples: ", ncol(samples.bf.cov.dil.log), "\n")
cat("Non-outlier samples: ", ncol(samples.bf.cov.dil.log.oe),"\n")
TIC <- colSums(samples.bf.cov.dil.log.oe, na.rm=T)
par(mfrow = c(1,2))
boxplot(TIC, ylab = "Extracted peaks area")
hist(TIC, breaks = 100, main = "", xlab = "Extracted peaks area")
### Remove NA rows
cat("Remove NA rows", "\n")
ind = remove.narows(samples.bf.cov.dil.log.oe)
if (any(ind)) {
samples.bf.cov.dil.log.oe <- samples.bf.cov.dil.log.oe[!ind, ]
mz_cf <- mz_cf[!ind]
rt_cf <- rt_cf[!ind]
cat("Number of NA rows: ", length(which(ind)), "\n")
}
### Normalize to reference a.k.a. spiked-ins
# Extract remaining samples and log2 tranform the spiked-ins intensities
cat("Normalize to reference spikes", "\n")
ref <- ref_spikes[,names(samples.bf.cov.dil.log.oe)]
ref <- log2(ref)
samples.bf.cov.dil.log.oe.norm <- norm2ref(samples.bf.cov.dil.log.oe, ref)
par(mfrow = c(2,1))
boxplot(samples.bf.cov.dil.log.oe, main = "Before normalization")
boxplot(samples.bf.cov.dil.log.oe.norm, main = "After normalization")
### Merge replicates
cat("Merge replicates", "\n")
substrRight <- function(x){
substr(x, nchar(x)-1, nchar(x))
}
samplesIDs <- gsub("_Rep1", "", names(samples.bf.cov.dil.log.oe.norm))
samplesIDs <- gsub("_Rep2", "", samplesIDs)
names <- unique(samplesIDs)
samplesIDs <- unlist(lapply(names,substrRight))
y = specgrep(samples.bf.cov.dil.log.oe.norm,paste(samplesIDs[1], "_", sep=""))
c = cor(y, use="complete.obs")[1,2]
corr = c
y = apply(y, 1, mean, na.rm = TRUE)
samples.bf.cov.dil.log.oe.norm.merged <- y
for (i in 2:length(samplesIDs)) {
y = specgrep(samples.bf.cov.dil.log.oe.norm,paste(samplesIDs[i], "_", sep=""))
if (dim(y)[2] > 2) {
warning("More than two samples were grepped")
}
c = cor(y, use="complete.obs")[1,2]
if (c > 0.80) {
corr = c(corr,c)
y = apply(y, 1, mean, na.rm=TRUE)
samples.bf.cov.dil.log.oe.norm.merged <- cbind(samples.bf.cov.dil.log.oe.norm.merged,y)
} else {
corr = c(corr,c)
cat("Correlation between duplicates in sample", samplesIDs[i], "had a correlation of: ", c, "\n")
names <- names[-i]
}
}
samples.bf.cov.dil.log.oe.norm.merged <- data.frame(samples.bf.cov.dil.log.oe.norm.merged)
names(samples.bf.cov.dil.log.oe.norm.merged) <- names
cat("Minimum duplicate correlation: ", min(corr), "\n")
cat("Average duplicate correlation: ", mean(corr), "\n")
### Albumin ratio filtering
#load albumindata
albumin <- read.table("~/Projects/MS/Albuminkvot.csv", sep=";", header=T)
row.names(albumin) < -albumin$Provtagningsidentitet
albumin <- albumin[names(samples.bf.cov.dil.log.oe.norm.merged),]
correlating <- data.frame(matrix(NA,nrow=nrow(samples.bf.cov.dil.log.oe.norm.merged),ncol=3))
names(correlating) <- c("ind", "corr","p-value")
row.names(correlating) <- row.names(samples.bf.cov.dil.log.oe.norm.merged)
for (i in 1:nrow(samples.bf.cov.dil.log.oe.norm.merged)) {
alb <- albumin$Albuminkvot
f <- as.numeric(samples.bf.cov.dil.log.oe.norm.merged[i,])
ind <- unique(c(which(is.na(alb)), which(is.na(f))))
if (length(ind)>0) {
alb <- alb[-ind]
f <- f[-ind]
}
if (length(f) > 15) {
c <- cor.test(f,alb,method = "spearman")
if (c$p.value < 0.05 & abs(c$estimate) > 0.5) {
correlating[i,1] <- i
correlating[i,2] <- c$estimate
correlating[i,3] <- c$p.value
}
}
}
# remove na rows
ind = remove.narows(correlating)
if (any(ind)) {
correlating <- correlating[!ind, ]
}
cat("Features correlating with the albumin ratio: ", length(correlating$ind), "\n")
samples.bf.cov.dil.log.oe.norm.merged.alb <- samples.bf.cov.dil.log.oe.norm.merged[-correlating$ind,]
df <- samples.bf.cov.dil.log.oe.norm.merged.alb
# coverage cutoff
df <- df[coverage(df,0.75),]
save.image("workspace_processed.RData")
save(df, file="processed_Msdata.RData")
save(mz_cf, file="mz_cf.RData")
save(rt_cf, file="rt_cf.RData")
cat("Final number of features: ", nrow(df), "\n")
sink()