Skip to content

Commit

Permalink
injection time is now added by default in readMSData2
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentGatto committed Oct 5, 2016
1 parent f126027 commit 7aab7fe
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 40 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Expand Up @@ -63,6 +63,7 @@ Imports:
lattice,
ggplot2,
S4Vectors,
XML,
Rcpp
Suggests:
testthat,
Expand All @@ -85,7 +86,6 @@ Suggests:
imputeLCMD,
norm,
gplots,
XML,
shiny
LinkingTo: Rcpp
License: Artistic-2.0
Expand Down
4 changes: 3 additions & 1 deletion NAMESPACE
Expand Up @@ -12,6 +12,9 @@ import(impute)
import(lattice)
import(methods)

importFrom(XML, xmlInternalTreeParse, getDefaultNamespace, xpathApply,
xmlAttrs)

importFrom(graphics, abline, axis, grid, legend, lines, points, text,
barplot, layout, par)
importFrom(stats, ave, median, medpolish, quantile, reorder, setNames,
Expand Down Expand Up @@ -156,7 +159,6 @@ exportMethods(updateObject,
"sampleNames<-",
fileNames,
msInfo,
isolationWindow,
expemail, exptitle,
ionSource, ionSourceDetails,
analyser, analyzer,
Expand Down
1 change: 0 additions & 1 deletion R/AllGenerics.R
Expand Up @@ -142,4 +142,3 @@ setGeneric("filterFile", function (object, ...) standardGeneric("filterFile"))
setGeneric("filterAcquisitionNum", function (object, ...) standardGeneric("filterAcquisitionNum"))

## isolationWindow generic is in mzR
setGeneric("addInjectionTime", function(object, ...) standardGeneric("addInjectionTime"))
13 changes: 13 additions & 0 deletions R/header.R
@@ -0,0 +1,13 @@
## These data should eventually be extracted in mzR and added to the
## default header

injectionTimeFromFile1 <- function(f) {
xvals <- vector("list", length = length(f))
doc <- XML::xmlInternalTreeParse(f)
namespaceDef <- XML::getDefaultNamespace(doc)
ns <- c(x = namespaceDef[[1]]$uri)
x <- XML::xpathApply(doc, "//x:cvParam[@accession = 'MS:1000927']",
namespaces = ns, xmlAttrs)
xvals <- sapply(x, "[", "value")
as.numeric(unlist(xvals))
}
36 changes: 0 additions & 36 deletions R/methods-MSnExp.R
Expand Up @@ -344,39 +344,3 @@ setMethod("isolationWindow", "MSnExp",
## TODO - add to fData
mzR::isolationWindow(fileNames(object), ...)
})

setMethod("addInjectionTime", "MSnExp",
function(object, ...) {
stopifnot(requireNamespace("XML"))
if ("injectionTime" %in% fvarLabels(object))
stop("injectionTime feature variable already present.")
f <- fileNames(object)
xvals <- vector("list", length = length(f))
for (i in seq_along(f)) {
obj <- filterFile(object, i)
doc <- xmlInternalTreeParse(fileNames(obj))
namespaceDef <- getDefaultNamespace(doc)
ns <- c(x = namespaceDef[[1]]$uri)
x <- xpathApply(doc, "//x:cvParam[@accession = 'MS:1000927']",
namespaces = ns, xmlAttrs)
.xvals <- sapply(x, "[", "value")
k <- length(.xvals)
names(.xvals) <- sprintf(paste0("X%0",
ceiling(log10(k) + 1L),
"d.%s"),
1:k, i)
## if object only had a subset of ms levels
rw <- mzR::openMSfile(fileNames(obj))
hd <- mzR::header(rw)
sel <- hd$msLevel %in% unique(fData(obj)$msLevel)
stopifnot(k == length(sel))
stopifnot(length(obj) == sum(sel))
.xvals <- .xvals[sel]
xvals[[i]] <- .xvals
}
xvals <- as.numeric(unlist(xvals))
stopifnot(length(xvals) == length(object))
fData(object)$injectionTime <- xvals
if (validObject(object))
return(object)
})
5 changes: 4 additions & 1 deletion R/readMSData2.R
Expand Up @@ -46,11 +46,14 @@ readMSData2 <- function(files,
centroided = rep(as.logical(NA), nrow(fdData)),
smoothed = rep(as.logical(smoothed.), nrow(fdData)),
fdData, stringsAsFactors = FALSE)
injt <- injectionTimeFromFile1(f)
if (is.numeric(injt) && length(injt) == nrow(fdData))
fdData$injectionTime <- injt
## Order the fdData by acquisitionNum to force use of acquisitionNum
## as unique ID for the spectrum (issue #103). That way we can use
## the spIdx (is the index of the spectrum within the file) for
## subsetting and extracting.
fdData <- fdData[order(fdData$acquisitionNum), ]
fdData <- fdData[order(fdData$acquisitionNum), ] ## not sure this is needed
featureDataList <- c(featureDataList, list(fdData))
mzR::close(msdata)
rm(msdata)
Expand Down

0 comments on commit 7aab7fe

Please sign in to comment.