Skip to content

Commit 9d216a8

Browse files
authored
Merge pull request #20 from guideflandre/main
Fix error when calling multiple Spectra objects in addFragments
2 parents 407b2c1 + 3aaa30f commit 9d216a8

File tree

10 files changed

+154
-79
lines changed

10 files changed

+154
-79
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export(filterPsmRank)
1616
export(filterPsmShared)
1717
export(getAminoAcids)
1818
export(getAtomicMass)
19+
export(labelFragments)
1920
export(makeAdjacencyMatrix)
2021
export(makePeptideProteinVector)
2122
export(plotAdjacencyMatrix)

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# PSMatch 1.11
22

3+
## PSMatch 1.11.3
4+
5+
- Deprecated `addFragments`. The use of `labelFragments` is endorsed instead.
6+
[PR #20](https://github.com/rformassspectrometry/PSMatch/pull/20)
7+
38
## PSMatch 1.11.2
49

510
- Replace `calculateFragments` with `calculateFragments2`.

R/PSMatch-package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
#'
5151
#' The package also provides functionality to calculate ions produced
5252
#' by the fragmentation of a peptides (see [calculateFragments()]) and
53-
#' annotated MS2 [Spectra::Spectra()] objects (see [addFragments()]).
53+
#' annotated MS2 [Spectra::Spectra()] objects (see [labelFragments()]).
5454
#'
5555
#' @section Vignettes:
5656
#'

R/fragments-calculate.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -379,37 +379,37 @@ setMethod("calculateFragments", c("character", "missing"),
379379
max_mods = Inf) {
380380
modifiable_positions_var <-
381381
which(fragment.seq %in% names(variable_modifications))
382-
382+
383383
l <- length(modifiable_positions_var)
384-
384+
385385
## take the maximum amount of modifications possible
386386
max_mods <- min(max_mods, l)
387-
387+
388388
if (!length(variable_modifications) || max_mods <= 0)
389389
return(
390390
list(setNames(integer(length(fragment.seq)), fragment.seq))
391391
)
392-
392+
393393
.mod <- function(cmb,
394394
seq_split = fragment.seq,
395395
var_mods = variable_modifications) {
396396
m <- setNames(integer(length(seq_split)), seq_split)
397397
m[cmb] <- var_mods[seq_split[cmb]]
398398
m
399399
}
400-
400+
401401
c(
402402
list(setNames(integer(length(fragment.seq)), fragment.seq)),
403403
if (length(modifiable_positions_var) == 1)
404404
lapply(modifiable_positions_var, .mod)
405405
else
406406
unlist(
407407
lapply(seq_len(max_mods),
408-
function(n)combn(
409-
modifiable_positions_var, n,
410-
FUN = .mod,
411-
simplify = FALSE
412-
)
408+
function(n)combn(
409+
modifiable_positions_var, n,
410+
FUN = .mod,
411+
simplify = FALSE
412+
)
413413
),
414414
recursive = FALSE
415415
)
Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
##' @title Adds MS2 Fragments
1+
##' @name labelFragments
2+
##'
3+
##' @title labels MS2 Fragments
4+
##'
5+
##' @description
6+
##' Creates a list of annotations based on `calculateFragments` results.
27
##'
38
##' @param x An instance of class `Spectra` of length 1, containing a
49
##' spectra variable `"sequence"` with a `character(1)`
@@ -16,16 +21,17 @@
1621
##' [calculateFragments()] to calculate fragment m/z values to be
1722
##' added to the spectra in `x`.
1823
##'
19-
##' @return Return a `character()` with fragment ion labels.
24+
##' @return Return a `list()` of `character()` with fragment ion labels. The
25+
##' elements are named after the peptide they belong to (variable
26+
##' modifications included).
2027
##'
2128
##' @importFrom MsCoreUtils common
2229
##'
2330
##' @export
2431
##'
25-
##' @author Johannes Rainer, Sebastian Gibb, Laurent Gatto
32+
##' @author Johannes Rainer, Guillaume Deflandre, Sebastian Gibb, Laurent Gatto
2633
##'
2734
##' @examples
28-
##'
2935
##' library("Spectra")
3036
##'
3137
##' sp <- DataFrame(msLevel = 2L, rtime = 2345, sequence = "SIGFEGDSIGR")
@@ -62,39 +68,57 @@
6268
##' sp <- Spectra(sp)
6369
##'
6470
##' ## The fragment ion labels
65-
##' addFragments(sp)
71+
##' labelFragments(sp)
6672
##'
6773
##' ## Annotate the spectum with the fragment labels
68-
##' plotSpectra(sp, labels = addFragments, labelPos = 3)
69-
addFragments <- function(x,
70-
tolerance = 0,
71-
ppm = 20,
72-
...) {
74+
##' plotSpectra(sp, labels = labelFragments, labelPos = 3)
75+
labelFragments <- function(x, tolerance = 0, ppm = 20, ...) {
7376
stopifnot(requireNamespace("Spectra"))
74-
stopifnot(inherits(x, "Spectra"),
75-
length(x) == 1)
76-
stopifnot("sequence" %in% Spectra::spectraVariables(x))
77-
y <- Spectra::spectraData(x)[["sequence"]]
78-
79-
## Prepare x and y data
80-
x_data <- Spectra::peaksData(x)[[1L]]
81-
y_data <- calculateFragments(y, verbose = FALSE, ...)
82-
y_data <- y_data[order(y_data$mz), ]
77+
stopifnot(inherits(x, "Spectra"))
78+
super_labels <- vector("list", length = length(x))
79+
k <- integer()
8380

84-
## stop if variable modifications used
85-
## Temporary check to allow plotSpectra to work fine
86-
## Will need to be removed once plotSpectra accepts variable modifications
87-
## See issue: https://github.com/rformassspectrometry/Spectra/issues/346
88-
stopifnot(length(unique(y_data[["peptide"]])) == 1)
81+
for (j in seq_along(x)) {
82+
stopifnot("sequence" %in% Spectra::spectraVariables(x[j]))
83+
y <- Spectra::spectraData(x[j])[["sequence"]]
84+
x_data <- Spectra::peaksData(x[j])[[1L]]
85+
y_data <- calculateFragments(y, verbose = FALSE, ...)
86+
87+
y_data <- split(y_data, y_data$peptide)
88+
89+
labels <- vector("list", length = length(y_data))
90+
names(labels) <- names(y_data)
91+
92+
for (i in seq_along(y_data)) {
93+
k <- c(k, j)
94+
y_data[[i]] <- y_data[[i]][order(y_data[[i]]$mz), ]
95+
idx <- which(MsCoreUtils::common(x_data[, "mz"],
96+
y_data[[i]][, "mz"],
97+
tolerance = tolerance,
98+
ppm = ppm))
99+
idy <- which(MsCoreUtils::common(y_data[[i]][, "mz"],
100+
x_data[, "mz"],
101+
tolerance = tolerance,
102+
ppm = ppm))
103+
104+
labels[[i]] <- rep(NA_character_, nrow(x_data))
105+
labels[[i]][idx] <- y_data[[i]][idy, "ion"]
106+
}
107+
super_labels[[j]] <- labels
108+
}
109+
super_labels <- unlist(super_labels, recursive = FALSE)
110+
attr(super_labels, "group") <- k
111+
super_labels
112+
}
89113

90-
## Find common peaks and prepare annotations
91-
idx <- which(MsCoreUtils::common(x_data[, "mz"], y_data[, "mz"],
92-
tolerance = tolerance, ppm = ppm))
93-
idy <- which(MsCoreUtils::common(y_data[, "mz"], x_data[, "mz"],
94-
tolerance = tolerance, ppm = ppm))
95114

96-
## Prepare labels
97-
labels <- rep(NA_character_, nrow(x_data))
98-
labels[idx] <- y_data[idy, "ion"]
99-
labels
115+
##' @rdname labelFragments
116+
##'
117+
##' @export
118+
##'
119+
##' @details
120+
##' `addFragments` is deprecated and will be made defunct; use `labelFragments` instead.
121+
addFragments <- function(x, tolerance = 0, ppm = 20, ...) {
122+
.Deprecated("labelFragments")
123+
labelFragments(x, tolerance, ppm, ...)
100124
}

man/PSMatch.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_addFragments.R

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
test_that("labelFragments() works", {
2+
library("Spectra")
3+
seq <- "PQR"
4+
frags <- calculateFragments(seq)
5+
o <- order(frags$mz)
6+
sp <- DataFrame(msLevel = 2L, rtime = 2345, sequence = seq)
7+
sp$mz <- list(frags$mz[o])
8+
sp$intensity <- list(rep(1, 7))
9+
sp <- Spectra(sp)
10+
## all fragments
11+
ans <- labelFragments(sp)[[1]]
12+
exp <- frags$ion[o]
13+
## Remove attribute
14+
expect_identical(ans[1:length(ans)], exp)
15+
## 2nd fragment missing
16+
sp$mz[[1]][2] <- sp$mz[[1]][2] * 1.1
17+
exp[2] <- NA
18+
ans <- labelFragments(sp)[[1]]
19+
expect_identical(ans[1:length(ans)], exp)
20+
## 7th fragment missing
21+
sp$mz[[1]][7] <- sp$mz[[1]][7] * 1.1
22+
exp[7] <- NA
23+
ans <- labelFragments(sp)[[1]]
24+
expect_identical(ans[1:length(ans)], exp)
25+
})
26+
27+
28+
test_that("labelFragments() works with multiple Spectra", {
29+
library("Spectra")
30+
seq <- c("PQR", "ACE")
31+
frags_pqr <- calculateFragments(seq)[1:7,]
32+
frags_ace <- calculateFragments(seq)[8:13,]
33+
o_pqr <- order(frags_pqr$mz)
34+
o_ace <- order(frags_ace$mz)
35+
sp <- DataFrame(msLevel = c(2L, 2L), rtime = c(2345, 2346), sequence = seq)
36+
sp$mz <- c(list(frags_pqr$mz[o_pqr]), list(frags_ace$mz[o_ace]))
37+
sp$intensity <- c(list(rep(1, 7)), list(rep(1, 6)))
38+
sp <- Spectra(sp)
39+
## all fragments
40+
ans <- labelFragments(sp)
41+
## Number of elements equal the possibilities of peptide sequences
42+
## This instance: no mod, 1 mod on Q: 2 possibilities
43+
expect_equal(length(ans), 2)
44+
expect_identical(names(ans), seq)
45+
})
46+
47+
test_that("labelFragments() works with modifications", {
48+
library("Spectra")
49+
seq <- "PQR"
50+
frags <- calculateFragments(seq)
51+
o <- order(frags$mz)
52+
sp <- DataFrame(msLevel = 2L, rtime = 2345, sequence = seq)
53+
sp$mz <- list(frags$mz[o])
54+
sp$intensity <- list(rep(1, 7))
55+
sp <- Spectra(sp)
56+
## all fragments
57+
ans <- labelFragments(sp, variable_modifications = c(Q = 45))
58+
## Number of elements equal the possibilities of peptide sequences
59+
## This instance: no mod, 1 mod on Q: 2 possibilities
60+
expect_equal(length(ans), 2)
61+
})
62+

vignettes/Fragments.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ plotSpectra(sp5449[2])
102102
```
103103

104104
```{r}
105-
plotSpectra(sp5449[2], labels = addFragments, labelPos = 3)
105+
plotSpectra(sp5449[2], labels = labelFragments, labelPos = 3)
106106
```
107107

108108
# Session information

0 commit comments

Comments
 (0)