Skip to content

Commit

Permalink
Fix for issue #173
Browse files Browse the repository at this point in the history
- Stitching of mzML files did not fill in mz and intensity values for the last
  spectrum.
  • Loading branch information
jorainer committed May 19, 2017
1 parent 95a8dea commit 56c43dd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
4 changes: 2 additions & 2 deletions R/MPI.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ fillPeaksChromPar <- function(arg) {
if (length(params$dataCorrection) > 1) {
## Note: dataCorrection (as set in the xcmsSet function) is either
## 1 for all or for none.
if (any(params$dataCorrection) == 1)
if (any(params$dataCorrection == 1))
lcraw <- stitch(lcraw, AutoLockMass(lcraw))
}

if (exists("params$polarity") && length(params$polarity) >0) {
if (length(params$polarity) >0) {
if (length(params$polarity) > 0) {
## Retain wanted polarity only
lcraws <- split(lcraw, lcraw@polarity, DROP=TRUE)
lcraw <- lcraws[[params$polarity]]
Expand Down
38 changes: 32 additions & 6 deletions R/methods-xcmsRaw.R
Original file line number Diff line number Diff line change
Expand Up @@ -2302,17 +2302,35 @@ setMethod("stitch.xml", "xcmsRaw", function(object, lockMass) {
ob@tic<-object@tic
ob@profparam<-list()

arr<-array(dim=c(2,max(diff(ob@scanindex)), length(ob@scanindex)))
## Array [x, y, z] with
## - x: mz and intensity
## - y: spectrum (1: max measurements within one of the spectra)
## - z: scans (1: number of spectra)
arr <- array(dim = c(2, max(diff(ob@scanindex)), length(ob@scanindex)))
if(lockMass[1] == 1){
lockMass<-lockMass[3:length(lockMass)]
}

## Remove the last lock mass if it is too close by the end
if ((lockMass[length(lockMass)] + 2) > length(ob@scanindex))
lockMass <- lockMass[1:(length(lockMass) - 1)]

## If the number of lockMass values is not even splitting them into a
## two-column matrix is not OK (causes also the first lockMass spectrum to
## be overwritten twice. That's to get rid of the warning in issue #173.
if (length(lockMass) %% 2)

This comment has been minimized.

Copy link
@jorainer

jorainer May 19, 2017

Author Collaborator

This is to avoid the warning from issue #173

lockMass <- c(lockMass, -99)
lockMass<-matrix(lockMass, ncol=2, byrow=TRUE)
if((lockMass[nrow(lockMass),2]+2) > length(ob@scanindex)){
lockMass<-lockMass[1:(nrow(lockMass)-1),]
}
## if((lockMass[nrow(lockMass),2]+2) > length(ob@scanindex)){
## lockMass<-lockMass[1:(nrow(lockMass)-1),]
## }

## We're looping from 1 to length - 1, thus we have to fill in the last
## scan later.
for(i in 1:(length(ob@scanindex)-1)){
if(any(i == lockMass[,1])){
if(any(i == lockMass[, 1])){
## Place mz and intensity values from the previous scan into the
## array and fill the rest with NA.
arr[1,,i] <-c(object@env$mz[(object@scanindex[(i-1)]+1):object@scanindex[i]],
rep(NA, (max(diff(object@scanindex))-
length((object@scanindex[(i-1)]+1):object@scanindex[i])) ))
Expand All @@ -2321,7 +2339,8 @@ setMethod("stitch.xml", "xcmsRaw", function(object, lockMass) {
rep(NA, (max(diff(object@scanindex)) -
length((object@scanindex[(i-1)]+1):object@scanindex[i])) ))

} else if(any(i == lockMass[,2])){
} else if(any(i == lockMass[, 2])){
## Place mz and intensity values from the next scan into the array.
arr[1,,i] <-c(object@env$mz[(object@scanindex[i+1]+1):object@scanindex[(i+2)]],
rep(NA, (max(diff(object@scanindex)) -
length((object@scanindex[i+1]+1):object@scanindex[(i+2)])) ))
Expand All @@ -2331,6 +2350,7 @@ setMethod("stitch.xml", "xcmsRaw", function(object, lockMass) {
length((object@scanindex[i+1]+1):object@scanindex[(i+2)])) ))

} else{
## Just fill with the actual values.
arr[1,,i] <-c(object@env$mz[(object@scanindex[i]+1):object@scanindex[i+1]],
rep(NA, (max(diff(object@scanindex))-
length((object@scanindex[i]+1):object@scanindex[i+1])) ))
Expand All @@ -2352,6 +2372,12 @@ setMethod("stitch.xml", "xcmsRaw", function(object, lockMass) {
ob@scanindex[i]<-as.integer(length(na.omit(arr[1,,(i-1)]))+ob@scanindex[(i-1)])
}
}
## Fix for #173: fill also values for the last scan.

This comment has been minimized.

Copy link
@jorainer

jorainer May 19, 2017

Author Collaborator

Since we're looping above from 1:(length(ob@scanindex) - 1) we're missing to add the mz and intensity values for the last spectrum. This caused the error in issue #173 .

last_i <- length(ob@scanindex)
fetch_idx <- (object@scanindex[last_i] + 1):length(object@env$mz)
put_idx <- 1:length(fetch_idx)
arr[1, put_idx, length(ob@scanindex)] <- object@env$mz[fetch_idx]
arr[2, put_idx, length(ob@scanindex)] <- object@env$intensity[fetch_idx]

NAidx<-is.na(arr[1,,])
ob@env$mz<-as.numeric(arr[1,,][!NAidx])
Expand Down

0 comments on commit 56c43dd

Please sign in to comment.