forked from lgatto/physalia2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S03-id.R
363 lines (226 loc) · 6.93 KB
/
S03-id.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
library(msdata)
library(rpx)
library(Spectra)
library(PSMatch)
library(tidyverse)
idf <- ident(full.names = TRUE)
basename(idf)
id <- PSM(idf)
id
dim(id)
names(id)
head(id$spectrumID)
head(id$spectrumFile)
head(id$sequence)
head(id$DatabaseAccess)
## Verify that this table contains 5802 matches for 5343 scans and
## 4938 peptides sequences.
length(unique(id$spectrumID))
length(unique(id$sequence))
length(unique(id$DatabaseAccess))
as.data.frame(id) |>
as_tibble() |>
group_by(isDecoy) |>
summarise(ms = mean(MS.GF.RawScore))
## Target: protein.fasta -> peptide
## Decoy: reverse protein.fasta -> peptide
table(id$isDecoy)
## Compare the distribution of raw identification scores of the decoy
## and non-decoy hits. Interpret the figure.
as.data.frame(id) |>
as_tibble() |>
ggplot(aes(x = MS.GF.RawScore,
colour = isDecoy)) +
geom_density()
table(table(id$spectrumID))
i <- which(id$spectrumID == "controllerType=0 controllerNumber=1 scan=1774")
id[i, ] |>
as.data.frame() |>
DT::datatable()
id2 <- reducePSMs(id, id$spectrumID)
id2
(j <- which(id2$spectrumID == "controllerType=0 controllerNumber=1 scan=1774"))
id2[j, "DatabaseAccess"]
## Filtering
idtbl <- as_tibble(id)
## - Remove decoy hits
idtbl <- idtbl |>
filter(!isDecoy)
## - Keep first rank matches
idtbl <- idtbl |>
filter(rank == 1)
## - Remove shared peptides. Start by identifying scans that match
## different proteins. For example scan 4884 matches proteins
## XXX_ECA3406 and ECA3415. Scan 4099 match XXX_ECA4416_1,
## XXX_ECA4416_2 and XXX_ECA4416_3. Then remove the scans that match
## any of these proteins.
mltm <- group_by(spectrumID) |>
mutate(nProts = length(unique(DatabaseAccess))) |>
filter(nProts > 1) |>
pull(spectrumID)
mltm
idtbl <- idtbl |>
filter(!spectrumID %in% mltm)
idtbl
idf <- filterPSMs(id)
idf <- id |>
filterPsmDecoy() |>
filterPsmRank()
## PSMatch vignette: Understanding protein groups with adjacency
## matrices
data.frame(idf[1:10, c("sequence", "DatabaseAccess")])
data.frame(idf) |>
as_tibble() |>
filter(DatabaseAccess == 'ECA2006')
data.frame(idf) |>
as_tibble() |>
filter(sequence == 'RQCRTDFLNYLR')
adj <- makeAdjacencyMatrix(idf)
adj[1:15, 1:5]
dim(adj)
describePeptides(idf)
describeProteins(idf)
cc <- ConnectedComponents(adj)
connectedComponents(cc, 1)
connectedComponents(cc, 527)
connectedComponents(cc, 38)
connectedComponents(cc, 920)
i <- which(nrows(cc) > 2 & ncols(cc) > 2)
dims(cc)[i, ]
cx <- connectedComponents(cc, 1082)
plotAdjacencyMatrix(cx)
## https://lgatto.github.io/2023_06_15_CSAMA_Brixen
## Combining raw and id data
sp <- Spectra(f)
spectraVariables(sp)
head(sp$spectrumId)
idf <- filterPSMs(id)
names(idf)
head(idf$spectrumID)
table(table(idf$spectrumID))
which(table(idf$spectrumID) == 4)
idf[idf$spectrumID == "controllerType=0 controllerNumber=1 scan=5490", ] |>
as.data.frame() |>
DT::datatable()
idf <- reducePSMs(idf, idf$spectrumID)
spid <- joinSpectraData(sp, idf,
by.x = "spectrumId",
by.y = "spectrumID")
spectraVariables(spid)
all(is.na(filterMsLevel(spid, 1L)$sequence))
table(is.na(filterMsLevel(spid, 2L)$sequence))
## Visualise MS2 scans
i <- which(spid$MS.GF.RawScore > 100)[1]
plotSpectra(spid[i])
spid[i]$sequence
calculateFragments("THSQEEMQHMQR")
mz(spid[i])
mz(spid[i])[[1]]
pdi <- data.frame(peaksData(spid[i])[[1]])
pdi$label <- addFragments(spid[i])
addFragments(spid[i])
plotSpectra(spid[i], labels = addFragments,
labelCol = "steelblue",
labelPos = 3)
filter(pdi,
!is.na(label)) |>
arrange(intensity)
spid[i] |>
filterIntensity(200) |>
plotSpectra(labels = addFragments,
labelCol = "steelblue",
labelPos = 3)
spid <- countIdentifications(spid)
table(msLevel(spid),
spid$countIdentifications)
spid |>
filterMsLevel(1) |>
spectraData() |>
as_tibble() |>
ggplot(aes(x = rtime,
y = totIonCurrent)) +
geom_line() +
geom_point(aes(colour =
ifelse(countIdentifications == 0,
NA, countIdentifications)),
size = 4) +
labs(colour = "Number of ids")
## Comparing spectra - distances
## - Create a new Spectra object containing the MS2 spectra with
## sequences "SQILQQAGTSVLSQANQVPQTVLSLLR" and
## "TKGLNVMQNLLTAHPDVQAVFAQNDEMALGALR".
k <- which(spid$sequence %in% c("SQILQQAGTSVLSQANQVPQTVLSLLR", "TKGLNVMQNLLTAHPDVQAVFAQNDEMALGALR"))
spk <- spid[k]
plotSpectra(spk)
spk$sequence
## - Calculate the 5 by 5 similarity matrix between all spectra using
## compareSpectra(). See the ?Spectra man page for details. Draw a
## heatmap of that matrix, for example pheatmap::pheatmap()
mat <- compareSpectra(spk)
colnames(mat) <- rownames(mat) <- strtrim(spk$sequence, 2)
mat
library(pheatmap)
pheatmap(mat)
plotSpectraMirror(spk[1], spk[2])
plotSpectraOverlay(spk[3:5],
col = c("red", "steelblue", "green"))
plotSpectraMirror(spk[3], spk[4])
## Recap exercise
## Download the 3 first mzML and mzID files from the PXD022816
## project (Morgenstern, Barzilay, and Levin 2021).
library(rpx)
px <- PXDataset("PXD022816")
pxfiles(px)
pxtax(px)
pxref(px)
fmzml <- pxget(px, grep("mzML", pxfiles(px), value = TRUE)[1:3])
basename(fmzml)
fid <- pxget(px, grep("mzID", pxfiles(px), value = TRUE)[1:3])
basename(fid)
## Generate a Spectra object and a table of filtered PSMs. Visualise
## the total ion chromatograms and check the quality of the
## identification data by comparing the density of the decoy and
## target PSMs id scores for each file.
sp <- Spectra(fmzml)
sp
sp$file <- basename(dataOrigin(sp))
table(basename(dataOrigin(sp)), msLevel(sp))
filterMsLevel(sp, 1) |>
spectraData() |>
as_tibble() |>
ggplot(aes(x = rtime,
y = totIonCurrent,
colour = file)) +
geom_line() +
facet_wrap(~ file)
id <- PSM(fid)
id$file <- sub("^.+QEP2", "QEP2", id$spectrumFile)
data.frame(id) |>
as_tibble() |>
count(file)
data.frame(id) |>
as_tibble() |>
ggplot(aes(x = MetaMorpheus.score,
colour = isDecoy)) +
geom_density() +
facet_wrap(~ file)
summary(id$PSM.level.q.value)
data.frame(id) |>
as_tibble() |>
count(file, isDecoy)
idf <- filterPSMs(id)
## Join the raw and identification data. Beware though that the
## joining must now be performed by spectrum ids and by files.
## primary keys
sp$pkey <- paste0(sub("^.+QEP", "QEP", sp$file),
sub("^.+scan=", "::", sp$spectrumId))
idf$pkey <- paste0(idf$file,
sub("^.+scan=", "::", idf$spectrumID))
idf[which(idf$pkey == idf$pkey[7]), ] |>
data.frame() |>
DT::datatable()
table(table(idf$pkey))
sp <- joinSpectraData(sp, idf, by.x = "pkey")
table(filterMsLevel(sp, 2)$file,
!is.na(filterMsLevel(sp, 2)$sequence))
length(unique(sp$sequence))