-
Notifications
You must be signed in to change notification settings - Fork 4
/
TargetDecoyGenerator.R
353 lines (297 loc) · 14.4 KB
/
TargetDecoyGenerator.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
# This script generates a PRELIMINARY training set using the best quality detected and deconvoluted peak groups as targets and producing their corresponding decoys.
# For decoy generation, the peak groups are associated by pairs and used as targets.
# For each pair of targets, A and B, a pair of decoys is generated by keeping the same precursor and its properties and swapping the m/z values of 60% of the fragments.
# A list with generated targets and decoys is saved in Skyline format.
#
# INPUT:
# - PeakID...csv (aligned matrix with deconvoluted MS2s)
# OUTPUT:
# - SkylineTargetList_preliminary-target-decoy_[sample]_[date].csv
# - TargetDecoy-preliminary-figures_[sample]_[date].pdf
basePathData = "/Users/xxx/Documents" # <--- update this path!
setwd(file.path(basePathData,"data/Asper"))
alignmentTable = "xMSDIAL/PeakID_0_20221211731.txt"
#setwd(file.path(basePathData,"data/Pput"))
#alignmentTable = "xMSDIAL/PeakID_0_202111201516.txt"
#setwd(file.path(basePathData,"data/Rhodo"))
#alignmentTable = "xMSDIAL/PeakID_0_2022126180.txt"
alignmentTable = "xMSDIAL/PeakID_1_20221171618.txt"
sampleString = basename(getwd())
minSignalToNoise = 15
minMzDistanceMSMS = 0.4
maxCountFragments = 16
ExplicitRTwindow = 2 # For Skyline target list
pdfFileName = paste("TargetDecoy-preliminary-figures_", sampleString, "_", Sys.Date(), ".pdf", sep='')
dat = read.csv(file = alignmentTable, sep = '\t', stringsAsFactors = FALSE, skip = 3)
colnames(dat) = gsub(" |/|\\(|\\)|\\%", "", dat[1,])
dat = dat[-1,]
dat = dat[-which(dat$AverageCCS == "-1"), ] # remove MSDIAL results with CCS = -1
dat = dat[which(dat$MSMSspectrum != ""), ] # remove features without MS/MS
dat$SNaverage = as.numeric(dat$SNaverage)
dat = dat[which(dat$SNaverage >= minSignalToNoise), ] # remove low S/N features
dat$AlignmentID = as.numeric(dat$AlignmentID)
dat$AverageMz = as.numeric(dat$AverageMz)
dat$AverageRtmin = as.numeric(dat$AverageRtmin)
dat$AverageCCS = as.numeric(dat$AverageCCS)
#hist(log10(dat$SNaverage), 100)
#__________________________________________________
# Remove redundant peaks from spectrum keeping the most intense within tolerance (e.g., from deficient peak centroding):
RemoveRedundantPeaks = function(spectrum, minMzDistanceMSMS)
{
spectrum = spectrum[with(spectrum, order(-ProductHeight)), ]
spectrum$index = 1:length(spectrum[,1])
spectrum$cluster = 0
k = 1
while(k <= length(spectrum[,1]))
{
if(spectrum$cluster[k] > 0) # continue if it is already clustered
{
k = k + 1
next
}
mzx = spectrum$ProductMz[k]
indexes = which(abs(spectrum$ProductMz - mzx) <= minMzDistanceMSMS
& spectrum$cluster == 0)
spectrum$cluster[indexes] = k
k = k + 1
}
spectrum = spectrum[which(spectrum$index == spectrum$cluster),]
spectrum$cluster = NULL
spectrum$index = NULL
return(spectrum)
}
#__________________________________________________
# Parse fragment list:
# -------------------------------------------------
dat$MSMSspectrum = gsub(":", " ", dat$MSMSspectrum)
dat$MS1isotopicspectrum = gsub(":", " ", dat$MS1isotopicspectrum)
ms2 = NULL
for(k in 1:length(dat[,1]))
{
print(paste("Processing feature:", k))
tokens = unlist(strsplit(dat$MSMSspectrum[k], " "))
if(length(tokens) == 0)
{
# this is the MS1 detected without fragments
next
}
# values separated by space: m/z intensity, e.g.:
# 133.01199 2438 168.98599 281
msx = NULL
precHeight = unlist(strsplit(dat$MS1isotopicspectrum[k], " "))[2]
for(j in seq(1, length(tokens), 2))
{
x = data.frame(AlignmentID = dat$AlignmentID[k],
PrecursorMz = dat$AverageMz[k],
PrecursorHeight = precHeight,
ProductMz = as.numeric(tokens[j]),
ProductHeight = as.numeric(tokens[j + 1]))
msx = rbind(msx, x)
}
msx = RemoveRedundantPeaks(msx, minMzDistanceMSMS)
ms2 = rbind(ms2, msx)
}
x = NULL
msx = NULL
ms2$MS1isotopes = NULL
ms2$MSMSspectrum = NULL
ms2$PrecursorHeight = as.numeric(ms2$PrecursorHeight)
# Keep best fragments: ---------------
# remove fragments with too large intensity
ms2 = ms2[which(ms2$ProductHeight <= (ms2$PrecursorHeight * 1.3)), ]
# remove fragments with too small intensity
ms2 = ms2[which(ms2$ProductHeight >= (ms2$PrecursorHeight * 0.01)), ]
# remove fragments with close m/z to precursor m/z:
ms2 = ms2[which(abs(ms2$ProductMz - ms2$PrecursorMz) > 0.2), ]
# Compute fragment intensity rank:
ms2 = ms2[with(ms2, order(AlignmentID, -ProductHeight)), ]
ms2$intensityRank = ave(ms2$ProductHeight, ms2$AlignmentID, FUN=seq_along)
# Keep only top n fragments:
ms2 = ms2[which(ms2$intensityRank <= maxCountFragments), ]
# compute number of fragments:
ms2$CountFragments = ave(ms2$intensityRank, ms2$AlignmentID, FUN=max)
# Keep features with at least 3 fragments:
ms2 = ms2[which(ms2$CountFragments >= 3),]
dat = dat[dat$AlignmentID %in% unique(ms2$AlignmentID), ]
#__________________________________________________
# Generate decoys:
# -------------------------------------------------
decoys = NULL
mztoldecoys = 50
rtExclusionWindow = 3
ms2$hasdecoy = 0
ms2 = merge(ms2, dat[,c("AlignmentID", "AverageRtmin", "AverageCCS", "Spectrumreferencefilename")], by="AlignmentID")
ms2 = ms2[with(ms2, order(-ms2$PrecursorHeight, AlignmentID, ProductMz)), ] # sort by Precursor Height to start by the high intensity features
ms2$ProductName = ""
for(featID in unique(ms2$AlignmentID))
{
indexesMs2 = which(ms2$AlignmentID == featID & ms2$hasdecoy == 0)
original = ms2[indexesMs2, ]
original$indexesMs2 =indexesMs2
if(length(original$AlignmentID) < 1)
next
indexes = which(ms2$Spectrumreferencefilename == original$Spectrumreferencefilename[1]
& abs(ms2$PrecursorMz - original$PrecursorMz[1]) <= mztoldecoys
& abs(ms2$AverageRtmin - original$AverageRtmin[1]) > rtExclusionWindow
& ms2$CountFragments == original$CountFragments[1]
& ms2$AlignmentID != featID
& ms2$hasdecoy == 0)
if(length(indexes) < 1)
next
candidates = ms2[indexes, ]
candidates = candidates[!duplicated(candidates$AlignmentID), ]
# select the candidate feature with largest rt difference:
candidates$rtdiff = abs(candidates$AverageRtmin - original$AverageRtmin[1])
candidates = candidates[with(candidates, order(rtdiff)), ]
candfeatID = candidates$AlignmentID[1]
indexesMs2 = which(ms2$AlignmentID == candfeatID)
paired = ms2[indexesMs2, ]
paired$indexesMs2 = indexesMs2
#------- Flag any fragments with close mz values:
original$closeMz = 0
paired$closeMz = 0
# For each original.mz, find the index with closest smaller mz value in paired.mz:
closestSmallerIndex = findInterval(original$ProductMz, paired$ProductMz, rightmost.closed = FALSE, all.inside = TRUE)
indexesCloseMz = which(abs(original$ProductMz - paired$ProductMz[closestSmallerIndex]) <= 4)
if(length(indexesCloseMz) > 0)
{
original$closeMz[indexesCloseMz] = 1
paired$closeMz[closestSmallerIndex[indexesCloseMz]] = 1
}
# check the next element too:
closestSmallerIndex = closestSmallerIndex + 1
indexesCloseMz = which(abs(original$ProductMz - paired$ProductMz[closestSmallerIndex]) <= 4)
if(length(indexesCloseMz) > 0)
{
original$closeMz[indexesCloseMz] = 1
paired$closeMz[closestSmallerIndex[indexesCloseMz]] = 1
}
original = original[with(original, order(closeMz, intensityRank)), ]
paired = paired[with(paired, order(closeMz, intensityRank)), ]
# Regenerate product name to ensure rank correspondence of swapped fragments:
original$ProductName = paste("raw", original$AlignmentID, "-f", (1:nrow(original)), sep='')
paired$ProductName = paste("raw", paired$AlignmentID, "-f", (1:nrow(paired)), sep='')
#--------
swapIndexes = 1:(ceiling(length(original[,1])*0.6)) # get indexes top n most intense fragments for swapping (excluding the ones with close mz values)
if(max(original$closeMz[swapIndexes]) == 1 || max(paired$closeMz[swapIndexes]) == 1)
{
print(" Features cannot be paired, they have too many fragments with close m/z values:")
print(paste(" AlignmentID =", original$AlignmentID[1], ", paired AlignmentID =", paired$AlignmentID[1]))
next
}
original$closeMz = NULL
paired$closeMz = NULL
decoy1 = rbind(paired[swapIndexes,], original[-(swapIndexes),])
decoy1$AlignmentID = original$AlignmentID[1]
decoy1$PrecursorHeight = original$PrecursorHeight[1]
decoy1$PrecursorMz = original$PrecursorMz[1]
decoy1$AverageRtmin = original$AverageRtmin[1]
decoy1$AverageCCS = original$AverageCCS[1]
decoy2 = rbind(original[swapIndexes,], paired[-swapIndexes,])
decoy2$AlignmentID = paired$AlignmentID[1]
decoy2$PrecursorHeight = paired$PrecursorHeight[1]
decoy2$PrecursorMz = paired$PrecursorMz[1]
decoy2$AverageRtmin = paired$AverageRtmin[1]
decoy2$AverageCCS = paired$AverageCCS[1]
decoys = rbind(decoys, decoy1, decoy2)
#indexes = which(ms2$AlignmentID == original$AlignmentID[1] | ms2$AlignmentID == paired$AlignmentID[1])
ms2$ProductName[original$indexesMs2] = original$ProductName
ms2$ProductName[paired$indexesMs2] = paired$ProductName
ms2$hasdecoy[original$indexesMs2] = 1
ms2$hasdecoy[paired$indexesMs2] = 1
}
decoy1 = NULL
decoy2 = NULL
candidates = NULL
original = NULL
paired = NULL
decoys$indexesMs2 = NULL
# keep only features with decoys:
ms2 = ms2[which(ms2$hasdecoy == 1),]
ms2$hasdecoy = NULL
decoys$hasdecoy = NULL
decoys = decoys[which(decoys$AlignmentID %in% unique(ms2$AlignmentID)),]
# update feature to use molecule name:
# raw: targets
# xxx: decoys
ms2$PrecursorName = paste("raw", ms2$AlignmentID, sep = '')
ms2$Label = "target"
decoys$PrecursorName = paste("xxx", decoys$AlignmentID, sep = '')
decoys$Label = "decoy"
ms2 = rbind(ms2, decoys)
# sort
ms2 = ms2[with(ms2, order(PrecursorName, ProductMz)), ]
# Format transition list for Skyline:
newtargets = data.frame(ms2$PrecursorName, ms2[,c("PrecursorName", "PrecursorMz", "ProductName", "ProductMz", "AverageRtmin", "AverageCCS")])
colnames(newtargets) = c("MoleculeGroup", "PrecursorName", "PrecursorMz", "ProductName", "ProductMz", "Explicit Retention Time", "Collisional Cross Section (sq A)")
newtargets$'Explicit Retention Time Window' = ExplicitRTwindow
newtargets$PrecursorCharge = -1
newtargets$ProductCharge = -1
write.table(newtargets, paste("SkylineTargetList_preliminary-target-decoy_", sampleString, "_", Sys.Date(), ".csv", sep=''),
col.names = TRUE, row.names = FALSE, sep = ",")
newtargets$Label = ms2$Label
# Generate global statistics figures:
library(ggplot2)
pdf(pdfFileName, paper="a4r", useDingbats=FALSE) # create .pdf file
# histogram by m/z:
p = ggplot(newtargets[!duplicated(newtargets$PrecursorName),], aes(PrecursorMz, fill=Label))
p = p + ylab("Number of Precursor")
p = p + theme_bw()
p = p + theme(text=element_text(size = 15), axis.title = element_text(size = 20),
axis.text.x = element_text(angle = 55, hjust = 1, size = 8))
plot(p + geom_histogram(position="dodge", alpha = 0.8, colour="black", bins = 30))
p = ggplot(newtargets, aes(ProductMz, fill=Label))
p = p + ylab("Number of Fragments")
p = p + theme_bw()
p = p + theme(text=element_text(size = 15), axis.title = element_text(size = 20),
axis.text.x = element_text(angle = 55, hjust = 1, size = 8))
plot(p + geom_histogram(position="dodge", alpha = 0.8, colour="black", bins = 30))
# histogram by abundance:
p = ggplot(ms2[!duplicated(ms2$PrecursorName),], aes(log10(PrecursorHeight), fill=Label))
p = p + ylab("Number of Precursor")
p = p + theme_bw()
p = p + theme(text=element_text(size = 15), axis.title = element_text(size = 20),
axis.text.x = element_text(angle = 55, hjust = 1, size = 8))
plot(p + geom_histogram(position="dodge", alpha = 0.8, colour="black", bins = 30))
p = ggplot(ms2, aes(log10(ProductHeight), fill=Label))
p = p + ylab("Number of Fragments")
p = p + theme_bw()
p = p + theme(text=element_text(size = 15), axis.title = element_text(size = 20),
axis.text.x = element_text(angle = 55, hjust = 1, size = 8))
plot(p + geom_histogram(position="dodge", alpha = 0.8, colour="black", bins = 30))
# histogram by number of fragments:
p = ggplot(ms2[!duplicated(ms2$PrecursorName),], aes(factor(CountFragments), fill=Label))
p = p + ylab("Number of Precursor")
p = p + xlab("Number of Fragments per peak group")
p = p + theme_bw()
p = p + theme(text=element_text(size = 15), axis.title = element_text(size = 20),
axis.text.x = element_text(angle = 55, hjust = 1, size = 8))
plot(p + geom_bar(position="dodge", alpha = 0.8, colour="black"))
# histogram by RT:
p = ggplot(newtargets[!duplicated(newtargets$PrecursorName),], aes(`Explicit Retention Time`, fill=Label))
p = p + ylab("Number of Precursor")
p = p + theme_bw()
p = p + theme(text=element_text(size = 15), axis.title = element_text(size = 20),
axis.text.x = element_text(angle = 55, hjust = 1, size = 8))
plot(p + geom_histogram(position="dodge", alpha = 0.8, colour="black", bins = 30))
# histogram by CCS:
p = ggplot(newtargets[!duplicated(newtargets$PrecursorName),], aes(`Collisional Cross Section (sq A)`, fill=Label))
p = p + ylab("Number of Precursor")
p = p + theme_bw()
p = p + theme(text=element_text(size = 15), axis.title = element_text(size = 20),
axis.text.x = element_text(angle = 55, hjust = 1, size = 8))
plot(p + geom_histogram(position="dodge", alpha = 0.8, colour="black", bins = 30))
p = ggplot(newtargets[!duplicated(newtargets$PrecursorName),], aes(`Explicit Retention Time`, PrecursorMz, colour=Label))
p = p + theme_bw()
p = p + theme(text=element_text(size = 15), axis.title = element_text(size = 20),
axis.text.x = element_text(angle = 55, hjust = 1, size = 8))
p = p + facet_wrap( ~ Label, ncol=1)
plot(p + geom_point(alpha = 0.3, size=3))
p = ggplot(newtargets[!duplicated(newtargets$PrecursorName),], aes(`Collisional Cross Section (sq A)`, PrecursorMz, colour=Label))
p = p + theme_bw()
p = p + theme(text=element_text(size = 15), axis.title = element_text(size = 20),
axis.text.x = element_text(angle = 55, hjust = 1, size = 8))
p = p + facet_wrap( ~ Label, ncol=1)
plot(p + geom_point(alpha = 0.3, size=3))
dev.off() # close .pdf file
table(ms2[!duplicated(ms2$PrecursorName),"Label"])