Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when calling multiple Spectra objects in addFragments #20

Open
wants to merge 54 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
11d943c
Create fragments-calculate2.R
guideflandre Dec 19, 2024
32dd91b
Create test_fragments2.R
guideflandre Dec 19, 2024
ab01551
Update fragments-calculate2.R
guideflandre Dec 19, 2024
d1951bb
Update fragments-calculate2.R
guideflandre Dec 19, 2024
d505a3d
Update fragments-calculate2.R
guideflandre Dec 19, 2024
b75af03
Update documentation
guideflandre Dec 19, 2024
f3b7ba7
Update documentation
guideflandre Dec 20, 2024
ea262a0
Delete PSMatch.Rproj
guideflandre Dec 24, 2024
dfeaa2e
Update NEWS.md
guideflandre Dec 24, 2024
7c8f6c9
Update DESCRIPTION
guideflandre Dec 24, 2024
9d1ea08
Update fragments-calculate2.R
guideflandre Dec 24, 2024
befd86a
Update test_fragments2.R
guideflandre Dec 24, 2024
046cca0
Update R/fragments-calculate2.R
guideflandre Jan 2, 2025
ebb8673
Update fragments-calculate2.R
guideflandre Jan 2, 2025
5c4ce49
Update fragments-calculate2.R
guideflandre Jan 6, 2025
7c8afd6
Update DESCRIPTION
guideflandre Jan 6, 2025
6d25915
git ignore Rproj file
guideflandre Jan 7, 2025
f5b8b06
update .cumsumFragmentMasses
guideflandre Jan 7, 2025
9d5b9ff
update: testthat .cumsumFragmentMasses
guideflandre Jan 7, 2025
5c0344e
update variable_modifications default to empty numeric
guideflandre Jan 7, 2025
a39cbee
Update fragments-calculate2.R
guideflandre Jan 10, 2025
0525f86
Add warning for modifications parameter
guideflandre Jan 17, 2025
f4a5d0f
Remove redundancy in strplit(sequence)
guideflandre Jan 20, 2025
2526ccd
Update documentation
guideflandre Jan 20, 2025
bdd7cd8
update documentation
guideflandre Jan 20, 2025
3d6bc0c
update documentaton
guideflandre Jan 20, 2025
7107718
Merge calculateFragments and calculateFragments2 into one true calcul…
guideflandre Jan 20, 2025
4f5795a
Remove redundancies and update calculateFragments based on PR
guideflandre Jan 21, 2025
5f46152
Updated failed unit test
guideflandre Jan 21, 2025
56aec17
Update comments in addFragments
guideflandre Jan 21, 2025
11834aa
Merge branch 'main' of https://github.com/guideflandre/PSMatch
guideflandre Jan 22, 2025
775859c
Update addFragments to generate a list
guideflandre Jan 22, 2025
8203f6d
Remove check for variable modifications as they are now included in S…
guideflandre Jan 22, 2025
f0b1f2a
Update documentation
guideflandre Jan 22, 2025
a3c3ada
addFragments correcion
guideflandre Jan 22, 2025
264390c
Update tests addFragments
guideflandre Jan 23, 2025
7c3c368
Remove attribute in addFragments
guideflandre Jan 24, 2025
b07d1a4
add PlotSpectraPTM
guideflandre Jan 24, 2025
868918d
remove plotting function for PR
guideflandre Jan 27, 2025
b2b12d9
Update R/fragments-add.R
guideflandre Jan 31, 2025
8a62849
Update test_addFragments.R
guideflandre Jan 31, 2025
898d8f5
Update documentation
guideflandre Jan 31, 2025
df39f37
Remove unnecessary test
guideflandre Jan 31, 2025
70edf96
Update based on PR review
guideflandre Jan 31, 2025
7b1d929
Add attribute
guideflandre Feb 3, 2025
fb4403d
deprecate addLabels
guideflandre Feb 3, 2025
f97c14b
remove old addFragments test and R
guideflandre Feb 3, 2025
a7367d0
Replace addFragments documentation with labelFragments
guideflandre Feb 3, 2025
aedbcbd
Change addFragments name and update NEWS
guideflandre Feb 20, 2025
302ab52
Update documentations
guideflandre Feb 28, 2025
10071d1
Update documentations
guideflandre Feb 28, 2025
924cb46
Resolved merge conflict in <file>
guideflandre Feb 28, 2025
21c2f30
fix indent
guideflandre Feb 28, 2025
d8d8239
Decprecate addFragments correctly
guideflandre Feb 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
export("reduced<-")
export(ConnectedComponents)
export(PSM)
export(addFragments)
export(ccMatrix)
export(connectedComponents)
export(defaultNeutralLoss)
Expand All @@ -16,6 +15,7 @@ export(filterPsmRank)
export(filterPsmShared)
export(getAminoAcids)
export(getAtomicMass)
export(labelFragments)
export(makeAdjacencyMatrix)
export(makePeptideProteinVector)
export(plotAdjacencyMatrix)
Expand Down
2 changes: 1 addition & 1 deletion R/PSMatch-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#'
#' The package also provides functionality to calculate ions produced
#' by the fragmentation of a peptides (see [calculateFragments()]) and
#' annotated MS2 [Spectra::Spectra()] objects (see [addFragments()]).
#' annotated MS2 [Spectra::Spectra()] objects (see [labelFragments()]).
#'
#' @section Vignettes:
#'
Expand Down
80 changes: 48 additions & 32 deletions R/fragments-add.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
##' @title Adds MS2 Fragments
##' @name labelFragments
##'
##' @title labels MS2 Fragments
##'
##' @param x An instance of class `Spectra` of length 1, containing a
##' spectra variable `"sequence"` with a `character(1)`
Expand All @@ -16,7 +18,8 @@
##' [calculateFragments()] to calculate fragment m/z values to be
##' added to the spectra in `x`.
##'
##' @return Return a `character()` with fragment ion labels.
##' @return Return a `list()` of `character()` with fragment ion labels. The
##' elements are named after the peptide they belong to (modifications included).
##'
##' @importFrom MsCoreUtils common
##'
Expand Down Expand Up @@ -62,39 +65,52 @@
##' sp <- Spectra(sp)
##'
##' ## The fragment ion labels
##' addFragments(sp)
##' labelFragments(sp)
##'
##' ## Annotate the spectum with the fragment labels
##' plotSpectra(sp, labels = addFragments, labelPos = 3)
addFragments <- function(x,
tolerance = 0,
ppm = 20,
...) {
##' plotSpectra(sp, labels = labelFragments, labelPos = 3)
labelFragments <- function(x, tolerance = 0, ppm = 20, ...) {
stopifnot(requireNamespace("Spectra"))
stopifnot(inherits(x, "Spectra"),
length(x) == 1)
stopifnot("sequence" %in% Spectra::spectraVariables(x))
y <- Spectra::spectraData(x)[["sequence"]]

## Prepare x and y data
x_data <- Spectra::peaksData(x)[[1L]]
y_data <- calculateFragments(y, verbose = FALSE, ...)
y_data <- y_data[order(y_data$mz), ]
stopifnot(inherits(x, "Spectra"))
super_labels <- vector("list", length = length(x))
k <- integer()

## stop if variable modifications used
## Temporary check to allow plotSpectra to work fine
## Will need to be removed once plotSpectra accepts variable modifications
## See issue: https://github.com/rformassspectrometry/Spectra/issues/346
stopifnot(length(unique(y_data[["peptide"]])) == 1)
for (j in seq_along(x)) {
stopifnot("sequence" %in% Spectra::spectraVariables(x[j]))
y <- Spectra::spectraData(x[j])[["sequence"]]
x_data <- Spectra::peaksData(x[j])[[1L]]
y_data <- calculateFragments(y, verbose = FALSE, ...)

y_data <- split(y_data, y_data$peptide)

labels <- vector("list", length = length(y_data))
names(labels) <- names(y_data)

for (i in seq_along(y_data)) {
k <- c(k, j)
y_data[[i]] <- y_data[[i]][order(y_data[[i]]$mz), ]
idx <- which(MsCoreUtils::common(x_data[, "mz"],
y_data[[i]][, "mz"],
tolerance = tolerance,
ppm = ppm))
idy <- which(MsCoreUtils::common(y_data[[i]][, "mz"],
x_data[, "mz"],
tolerance = tolerance,
ppm = ppm))

labels[[i]] <- rep(NA_character_, nrow(x_data))
labels[[i]][idx] <- y_data[[i]][idy, "ion"]
}
super_labels[[j]] <- labels
}
super_labels <- unlist(super_labels, recursive = FALSE)
attr(super_labels, "group") <- k
super_labels
}

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

## Prepare labels
labels <- rep(NA_character_, nrow(x_data))
labels[idx] <- y_data[idy, "ion"]
labels
}
#' @rdname labelFragments
addFragments <- function(x, tolerance = 0, ppm = 20, ...) {
.Deprecated("labelFragments")
labelFragments(x, tolerance, ppm, ...)
}
2 changes: 1 addition & 1 deletion man/PSMatch.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions man/addFragments.Rd → man/labelFragments.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions tests/testthat/test_addFragments.R

This file was deleted.

62 changes: 62 additions & 0 deletions tests/testthat/test_labelFragments.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
test_that("labelFragments() works", {
library("Spectra")
seq <- "PQR"
frags <- calculateFragments(seq)
o <- order(frags$mz)
sp <- DataFrame(msLevel = 2L, rtime = 2345, sequence = seq)
sp$mz <- list(frags$mz[o])
sp$intensity <- list(rep(1, 7))
sp <- Spectra(sp)
## all fragments
ans <- labelFragments(sp)[[1]]
exp <- frags$ion[o]
## Remove attribute
expect_identical(ans[1:length(ans)], exp)
## 2nd fragment missing
sp$mz[[1]][2] <- sp$mz[[1]][2] * 1.1
exp[2] <- NA
ans <- labelFragments(sp)[[1]]
expect_identical(ans[1:length(ans)], exp)
## 7th fragment missing
sp$mz[[1]][7] <- sp$mz[[1]][7] * 1.1
exp[7] <- NA
ans <- labelFragments(sp)[[1]]
expect_identical(ans[1:length(ans)], exp)
})


test_that("labelFragments() works with multiple Spectra", {
library("Spectra")
seq <- c("PQR", "ACE")
frags_pqr <- calculateFragments(seq)[1:7,]
frags_ace <- calculateFragments(seq)[8:13,]
o_pqr <- order(frags_pqr$mz)
o_ace <- order(frags_ace$mz)
sp <- DataFrame(msLevel = c(2L, 2L), rtime = c(2345, 2346), sequence = seq)
sp$mz <- c(list(frags_pqr$mz[o_pqr]), list(frags_ace$mz[o_ace]))
sp$intensity <- c(list(rep(1, 7)), list(rep(1, 6)))
sp <- Spectra(sp)
## all fragments
ans <- labelFragments(sp)
## Number of elements equal the possibilities of peptide sequences
## This instance: no mod, 1 mod on Q: 2 possibilities
expect_equal(length(ans), 2)
expect_identical(names(ans), seq)
})

test_that("labelFragments() works with modifications", {
library("Spectra")
seq <- "PQR"
frags <- calculateFragments(seq)
o <- order(frags$mz)
sp <- DataFrame(msLevel = 2L, rtime = 2345, sequence = seq)
sp$mz <- list(frags$mz[o])
sp$intensity <- list(rep(1, 7))
sp <- Spectra(sp)
## all fragments
ans <- labelFragments(sp, variable_modifications = c(Q = 45))
## Number of elements equal the possibilities of peptide sequences
## This instance: no mod, 1 mod on Q: 2 possibilities
expect_equal(length(ans), 2)
})

2 changes: 1 addition & 1 deletion vignettes/Fragments.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ plotSpectra(sp5449[2])
```

```{r}
plotSpectra(sp5449[2], labels = addFragments, labelPos = 3)
plotSpectra(sp5449[2], labels = labelFragments, labelPos = 3)
```

# Session information
Expand Down
Loading