From f56e0c92af90f34080e7ea72fdf76d93d61bec65 Mon Sep 17 00:00:00 2001 From: jorainer Date: Tue, 2 Feb 2021 19:46:29 +0100 Subject: [PATCH] fix: ensure header returns the same columns for all backends - Fix for issue #238: ensure `header` returns the same columns for all backends. --- DESCRIPTION | 2 +- NEWS | 5 +++ R/methods-mzRnetCDF.R | 73 +++++++++++++++++++----------------- R/zzz.R | 2 +- inst/unitTests/test_cdf.R | 6 +-- inst/unitTests/test_header.R | 20 ++++++++++ 6 files changed, 68 insertions(+), 40 deletions(-) create mode 100644 inst/unitTests/test_header.R diff --git a/DESCRIPTION b/DESCRIPTION index 40570068f..8114b0f21 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: mzR Type: Package Title: parser for netCDF, mzXML, mzData and mzML and mzIdentML files (mass spectrometry data) -Version: 2.25.1 +Version: 2.25.2 Author: Bernd Fischer, Steffen Neumann, Laurent Gatto, Qiang Kou, Johannes Rainer Maintainer: Steffen Neumann , Laurent Gatto , diff --git a/NEWS b/NEWS index aa2e7a977..fe7909388 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +CHANGES IN VERSION 2.25.2 +------------------------- + o Fix issue #238: ensure `header` call returns the same columns for all + backends. + CHANGES IN VERSION 2.25.1 ------------------------- o Bump version to trigger new build using latest Rcpp diff --git a/R/methods-mzRnetCDF.R b/R/methods-mzRnetCDF.R index a2eabe579..8236c3af2 100644 --- a/R/methods-mzRnetCDF.R +++ b/R/methods-mzRnetCDF.R @@ -51,41 +51,44 @@ setMethod("header", signature=c("mzRnetCDF","missing"), function(object) return(header(object, 1:length(object)))) -setMethod("header", - signature=c("mzRnetCDF","numeric"), - function(object, scans) { - empty_val <- rep(-1, length(scans)) - result <- data.frame(seqNum=scans, - acquisitionNum=scans, - msLevel=rep(1, length(scans)), - peaksCount=rep(1, length(scans)), - totIonCurrent=netCDFVarDouble(object@backend, "total_intensity")[scans], - retentionTime=netCDFVarDouble(object@backend, "scan_acquisition_time")[scans], - basePeakMZ = empty_val, - basePeakIntensity = empty_val, - collisionEnergy = empty_val, - ionisationEnergy = empty_val, - highMZ = empty_val, - precursorScanNum = empty_val, - precursorMZ = empty_val, - precursorCharge = empty_val, - precursorIntensity = empty_val, - mergedScan = empty_val, - mergedResultScanNum = empty_val, - mergedResultStartScanNum = empty_val, - mergedResultEndScanNum = empty_val, - injectionTime = empty_val, - spectrumId = paste0("scan=", scans), - centroided = NA, - ionMobilityDriftTime = empty_val, - stringsAsFactors = FALSE) - result$isolationWindowTargetMZ <- NA_real_ - result$isolationWindowLowerOffset <- NA_real_ - result$isolationWindowUpperOffset <- NA_real_ - result$scanWindowLowerLimit <- NA_real_ - result$scanWindowUpperLimit <- NA_real_ - return(result) - }) +setMethod("header", c("mzRnetCDF", "numeric"), function(object, scans) { + ls <- length(scans) + empty_val <- rep(-1, ls) + na_real <- rep(NA_real_, ls) + result <- data.frame( + seqNum=scans, + acquisitionNum=scans, + msLevel=rep(1, length(scans)), + polarity = empty_val, + peaksCount=rep(1, length(scans)), + totIonCurrent=netCDFVarDouble(object@backend, "total_intensity")[scans], + retentionTime=netCDFVarDouble(object@backend, "scan_acquisition_time")[scans], + basePeakMZ = empty_val, + basePeakIntensity = empty_val, + collisionEnergy = empty_val, + ionisationEnergy = empty_val, + lowMZ = empty_val, + highMZ = empty_val, + precursorScanNum = empty_val, + precursorMZ = empty_val, + precursorCharge = empty_val, + precursorIntensity = empty_val, + mergedScan = empty_val, + mergedResultScanNum = empty_val, + mergedResultStartScanNum = empty_val, + mergedResultEndScanNum = empty_val, + injectionTime = empty_val, + filterString = NA_character_, + spectrumId = paste0("scan=", scans), + centroided = NA, + ionMobilityDriftTime = empty_val, + isolationWindowTargetMZ = na_real, + isolationWindowLowerOffset = na_real, + isolationWindowUpperOffset = na_real, + scanWindowLowerLimit = na_real, + scanWindowUpperLimit = na_real) + return(result) +}) setMethod("close", signature="mzRnetCDF", diff --git a/R/zzz.R b/R/zzz.R index 5200c90c8..829611844 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,4 +1,4 @@ -BUILT_RCPP_VERSION = package_version("0.12.19") +BUILT_RCPP_VERSION = package_version("1.0.2") .onLoad <- function(libname, pkgname) { diff --git a/inst/unitTests/test_cdf.R b/inst/unitTests/test_cdf.R index 9209fa825..3ccb21d11 100644 --- a/inst/unitTests/test_cdf.R +++ b/inst/unitTests/test_cdf.R @@ -64,7 +64,7 @@ test_header <- function() { cdf <- openMSfile(file, backend="netCDF") h <- header(cdf) - checkEquals(ncol(h), 28) + checkEquals(ncol(h), 31) checkEquals(nrow(h), 1278) checkTrue(any(colnames(h) == "centroided")) checkTrue(all(is.na(h$centroided))) @@ -80,11 +80,11 @@ test_header <- function() { checkTrue(all(is.na(h$scanWindowUpperLimit))) h <- header(cdf, 1) - checkEquals(ncol(h), 28) + checkEquals(ncol(h), 31) checkEquals(nrow(h), 1) h <- header(cdf, 2:3) - checkEquals(ncol(h), 28) + checkEquals(ncol(h), 31) checkEquals(nrow(h), 2) close(cdf) diff --git a/inst/unitTests/test_header.R b/inst/unitTests/test_header.R new file mode 100644 index 000000000..b4519fa27 --- /dev/null +++ b/inst/unitTests/test_header.R @@ -0,0 +1,20 @@ +test_header_all <- function() { + ## Check that the header call returns the same columns irrespectively of the + ## backend. Issue #238 + file <- system.file('cdf/ko15.CDF', package = "msdata") + cdf <- openMSfile(file, backend="netCDF") + header_cdf <- header(cdf) + close(cdf) + + file <- system.file("threonine", "threonine_i2_e35_pH_tree.mzXML", package = "msdata") + mzxml <- openMSfile(file, backend="pwiz") + header_pwiz <- header(mzxml) + close(mzxml) + + mzxml <- openMSfile(file, backend="Ramp") + header_ramp <- header(mzxml) + close(mzxml) + + checkEquals(colnames(header_cdf), colnames(header_pwiz)) + checkEquals(colnames(header_ramp), colnames(header_pwiz)) +}