Skip to content

Commit

Permalink
Merge pull request #30 from rformassspectrometry/jomain
Browse files Browse the repository at this point in the history
Complete unit test coverage
  • Loading branch information
jorainer authored Nov 20, 2024
2 parents f1bdb81 + 2596efa commit fd2ce6e
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ root = true
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = false
insert_final_newline = true

[*.R]
indent_style = space
Expand All @@ -22,4 +22,4 @@ indent_style = tab

[.travis.yml]
indent_style = space
indent_size = 2
indent_size = 2
6 changes: 3 additions & 3 deletions .github/workflows/check-bioc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ jobs:
fail-fast: false
matrix:
config:
- { os: ubuntu-latest, r: 'devel', bioc: '3.19', cont: "bioconductor/bioconductor_docker:devel", rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest" }
- { os: macOS-latest, r: 'devel', bioc: '3.19'}
- { os: windows-latest, r: 'devel', bioc: '3.19'}
- { os: ubuntu-latest, r: 'devel', bioc: 'devel', cont: "bioconductor/bioconductor_docker:devel", rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest" }
- { os: macOS-latest, r: '4.4', bioc: '3.20'}
- { os: windows-latest, r: '4.4', bioc: '3.20'}
env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: MsBackendMgf
Title: Mass Spectrometry Data Backend for Mascot Generic Format (mgf) Files
Version: 1.13.0
Version: 1.15.1
Authors@R:
c(person(given = "RforMassSpectrometry Package Maintainer",
email = "maintainer@rformassspectrometry.org",
Expand Down Expand Up @@ -55,7 +55,7 @@ BugReports: https://github.com/RforMassSpectrometry/MsBackendMgf/issues
URL: https://github.com/RforMassSpectrometry/MsBackendMgf
biocViews: Infrastructure, Proteomics, MassSpectrometry, Metabolomics, DataImport
Roxygen: list(markdown=TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Collate:
'hidden_aliases.R'
'MsBackendMgf.R'
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# MsBackendMgf 1.15

## Changes in 1.15.1

- Complete unit test coverage.

# MsBackendMgf 1.11

## Changes in 1.11.2
Expand Down
8 changes: 6 additions & 2 deletions R/functions-mgf.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ readMgfSplit <- function(f, msLevel = 2L,
as.double(unlist(spec, use.names = FALSE, recursive = FALSE)),
ncol = length(spec[[1L]]), byrow = TRUE)

if(nrow(ms) > 1 && is.unsorted(ms[, 1L]))
if(.is_unsorted(ms))
ms <- ms[order(ms[, 1L]), , drop = FALSE]

r <- regexpr("=", desc, fixed = TRUE)
Expand All @@ -220,6 +220,9 @@ readMgfSplit <- function(f, msLevel = 2L,
res
}

.is_unsorted <- function(x) {
nrow(x) && is.unsorted(x[, 1L])
}

#' Format MGF charge string into an integer compatible format.
#'
Expand Down Expand Up @@ -289,7 +292,8 @@ readMgfSplit <- function(f, msLevel = 2L,
paste0(z, "=", spd[, z], "\n")
})
if (exportTitle && !any(colnames(spd) == "TITLE")) {
if (!is.null(spectraNames(x)))
sn <- spectraNames(x)
if (!is.null(sn) && any(sn != as.character(seq_along(x))))
title <- paste0("TITLE=", spectraNames(x), "\n")
else
title <- paste0("TITLE=msLevel ", spd$msLevel, "; retentionTime ",
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test_MsBackendMgf.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ test_that("export,MsBackendMgf works", {
res <- readLines(fl)
expect_true(length(grep("TITLE", res)) == 0)

spectraNames(sps) <- NULL
export(MsBackendMgf(), sps, file = fl, exportTitle = TRUE)
res <- readLines(fl)
expect_true(length(grep("TITLE=msLevel", res)) == 0)

expect_error(export(MsBackendMgf(), file = fl), "missing")
expect_error(export(MsBackendMgf(), x = spd, file = fl), "spectra data to")

Expand Down
21 changes: 19 additions & 2 deletions tests/testthat/test_functions-mgf.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ test_that("readMgfSplit works", {
full.names = TRUE, pattern = "mgf$")

expect_error(readMgfSplit(fls), "Please provide a single mgf file.")
expect_error(readMgfSplit(fls[1], n = -3), "has to be an integer")
expect_error(readMgfSplit(fls[1], n = 3), "Consider increasing the value")

res1 <- readMgfSplit(fls[1])
cns <- c("rtime", "acquisitionNum", "precursorMz", "precursorIntensity",
Expand Down Expand Up @@ -90,14 +92,19 @@ test_that(".extract_mgf_spectrum works", {
end <- grep("END IONS", mgf) - 1L
n <- length(begin)

res <- MsBackendMgf:::.extract_mgf_spectrum(mgf[begin[1]:end[1]])
res <- .extract_mgf_spectrum(mgf[begin[1]:end[1]])
expect_true(is.data.frame(res))
expect_equal(names(res), c("TITLE", "PEPMASS", "CHARGE",
"RTINSECONDS", "SCANS", "PEPMASSINT",
"mz", "intensity"))
expect_true(is.na(res$PEPMASSINT))

res <- MsBackendMgf:::.extract_mgf_spectrum(mgf[begin[2]:end[2]])
with_mocked_bindings(
".is_unsorted" = function(...) TRUE,
code = expect_equal(res, .extract_mgf_spectrum(mgf[begin[1]:end[1]]))
)

res <- .extract_mgf_spectrum(mgf[begin[2]:end[2]])
expect_true(is.data.frame(res))
expect_equal(names(res), c("TITLE", "PEPMASS", "CHARGE",
"RTINSECONDS", "SCANS", "PEPMASSINT",
Expand All @@ -109,3 +116,13 @@ test_that(".format_charge works", {
res <- .format_charge(c("4+", "3-", NA, "45"))
expect_equal(res, c("4", "-3", NA_character_, "45"))
})

test_that(".is_unsorted works", {
x <- cbind(1:3, 1:3)
expect_false(.is_unsorted(x))

x <- cbind(c(1, 43, 2), 1:3)
expect_true(.is_unsorted(x))

expect_false(.is_unsorted(x[integer(), , drop = FALSE]))
})

0 comments on commit fd2ce6e

Please sign in to comment.