Skip to content

Commit

Permalink
refactor: use matrix instead of rbind to create peak matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
jorainer committed Jan 10, 2024
1 parent 14e3dfb commit 2f2a434
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/check-bioc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ jobs:
CPPFLAGS = "-w"),
{
BiocManager::install("RforMassSpectrometry/ProtGenerics", update = TRUE)
BiocManager::install("RforMassSpectrometry/MsCoreUtils", update = TRUE)
BiocManager::install("RforMassSpectrometry/Spectra", update = TRUE)
BiocManager::install(c("MsCoreUtils"), update = TRUE)
remotes::install_local(dependencies = TRUE, repos = BiocManager::repositories(), build_vignettes = TRUE, upgrade = TRUE)
remotes::install_local(dependencies = TRUE, repos = BiocManager::repositories(), build_vignettes = FALSE, upgrade = TRUE)
},
assignment = "+="
)
Expand Down
6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ Authors@R:
person(given = "Michael", family = "Witting",
comment = c(ORCID = "0000-0002-1462-4426"),
email = "michael.witting@helmholtz-muenchen.de",
role = "ctb"))
role = "ctb"),
person(given = "Adriano", family = "Rutz",
comment = c(ORCID = "0000-0003-0443-9902"),
email = "rutz@imsb.biol.ethz.ch",
role = "ctb"))
Description: Mass spectrometry (MS) data backend supporting import and
export of MS/MS spectra data from Mascot Generic Format
(mgf) files. Objects defined in this package are supposed to be
Expand Down
22 changes: 14 additions & 8 deletions R/functions-mgf.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,18 @@
##'
##' readMgf(fls)
readMgf <- function(f, msLevel = 2L,
mapping = spectraVariableMapping(MsBackendMgf()), ...,
BPPARAM = SerialParam()) {
mapping = spectraVariableMapping(MsBackendMgf()), ...,
BPPARAM = SerialParam()) {
requireNamespace("MsBackendMgf", quietly = TRUE)
if (length(f) != 1L)
stop("Please provide a single mgf file.")
## Note: using readLines instead has some performance advantages
## (few seconds) for very large files
mgf <- scan(file = f, what = "",
sep = "\n", quote = "",
allowEscapes = FALSE,
quiet = TRUE)

## From http://www.matrixscience.com/help/data_file_help.html#GEN
## Comment lines beginning with one of the symbols #;!/ can be
## included, but only outside of the BEGIN IONS and END IONS
Expand Down Expand Up @@ -192,11 +195,13 @@ readMgfSplit <- function(f, msLevel = 2L,
desc.idx <- grep("=", mgf, fixed = TRUE)
desc <- mgf[desc.idx]

ms <- do.call(rbind, strsplit(mgf[-desc.idx], "[[:space:]]+", perl = TRUE))
mode(ms) <- "double"

if (!length(ms) || length(ms) == 1L)
spec <- strsplit(mgf[-desc.idx], "[[:space:]]+", perl = TRUE)
if (!length(spec) || length(spec[[1L]]) == 1L)
ms <- matrix(numeric(), ncol = 2L)
else
ms <- matrix(
as.double(unlist(spec, use.names = FALSE, recursive = FALSE)),
ncol = length(spec[[1L]]), byrow = TRUE)

if(nrow(ms) > 1 && is.unsorted(ms[, 1L]))
ms <- ms[order(ms[, 1L]), , drop = FALSE]
Expand All @@ -210,11 +215,12 @@ readMgfSplit <- function(f, msLevel = 2L,

res <- as.data.frame.matrix(matrix(desc, nrow = 1,
dimnames = list(NULL, names(desc))))
res$mz = list(ms[, 1L])
res$intensity = list(ms[, 2L])
res$mz <- list(ms[, 1L])
res$intensity <- list(ms[, 2L])
res
}


#' Format MGF charge string into an integer compatible format.
#'
#' @param x `character`
Expand Down

0 comments on commit 2f2a434

Please sign in to comment.