Skip to content

Commit

Permalink
Fixing mz calculation for multiply-charged ions and negative atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Stravs committed Mar 17, 2022
1 parent 27128b5 commit 7b780e2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: RMassBank
Type: Package
Title: Workflow to process tandem MS files and build MassBank records
Version: 3.5.2
Version: 3.5.2.1
Authors@R: c(
person(given = "RMassBank at Eawag", email = "[email protected]",
role=c("cre")),
Expand Down
1 change: 1 addition & 0 deletions R/formulaCalculator.R
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ split.formula.posneg <- function(f, as.formula = TRUE, as.list=FALSE)
if(!is.list(f)) f <- formulastring.to.list(f)
pos <- f[which(f > 0)]
neg <- f[which(f < 0)]
neg <- multiply.formula(neg, -1, as.list = TRUE)
if(as.formula & !as.list)
return(list(pos=list.to.formula(pos), neg=list.to.formula(neg)))
else
Expand Down
16 changes: 10 additions & 6 deletions R/leCsvAccess.R
Original file line number Diff line number Diff line change
Expand Up @@ -541,22 +541,26 @@ findMz.formula <- function(formula, mode="pH", ppm=10, deltaMz=0)
formula <- add.formula(formula, mzopt$addition)
# Since in special cases we want to use this with negative and zero number of atoms, we account for this case
# by splitting up the formula into positive and negative atom counts (this eliminates the zeroes.)
# Note: the previous implementation was incorrect, since
formula.split <- split.formula.posneg(formula)
m <- 0
if(formula.split$pos != "")
{
formula.pos <- get.formula(formula.split$pos, charge = mzopt$charge)
formula.pos <- get.formula(formula.split$pos, charge = 0)
m = m + formula.pos@mass
}
if(formula.split$neg != "")
{
formula.neg <- get.formula(formula.split$neg, charge = -mzopt$charge)
formula.neg <- get.formula(formula.split$neg, charge = 0)
m = m - formula.neg@mass
}
if((nchar(formula.split$pos)==0) & (nchar(formula.split$neg)==0))
{
m <- get.formula("H", charge = mzopt$charge)@mass - get.formula("H", charge = 0)@mass
}
m <- m + get.formula("H", charge = mzopt$charge)@mass - get.formula("H", charge = 0)@mass

# get.formula only takes "charge" into account to add the electrons - not to
# divide by z to get m/z. therefore, we do it ourselves
if(mzopt$charge != 0)
m <- m / abs(mzopt$charge)
# Note: technically there is no m/z for charge=0
delta <- ppm(m, ppm, l = TRUE)
return(list(mzMin = delta[[2]] - deltaMz, mzMax = delta[[1]] +
deltaMz, mzCenter = m))
Expand Down
35 changes: 35 additions & 0 deletions inst/tests_wip/test_leCsvAccess.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Test correct results of findMz.formula wiht positive and negative charge,
# single and multiple charge, no charge, and fictitious negative atoms
expect_equal(findMz.formula("C6", "")$mzCenter, 72)
expect_equal(
findMz.formula("C6", "mH")$mzCenter,
72 - 1.0078 + RMassBank:::.emass,
tolerance = 0.00001 )
expect_equal(
findMz.formula("C6", "pH")$mzCenter,
72 + 1.0078 - RMassBank:::.emass,
tolerance = 0.00001 )
expect_equal(
findMz.formula("C6H-1", "")$mzCenter,
72 - 1.0078,
tolerance = 0.00001 )
expect_equal(
findMz.formula("C6H-1", "mM")$mzCenter,
72 - 1.0078 + RMassBank:::.emass,
tolerance = 0.00001 )
expect_equal(
findMz.formula("C6H-1", "pM")$mzCenter,
72 - 1.0078 - RMassBank:::.emass,
tolerance = 0.00001 )
expect_equal(
findMz.formula("C6", "m2H_c2")$mzCenter,
(72 - (2*1.0078) + 2*RMassBank:::.emass) / 2,
tolerance = 0.00001 )
expect_equal(
findMz.formula("C6H-1", "m2H_c2")$mzCenter,
(72 - (3*1.0078) + 2*RMassBank:::.emass) / 2,
tolerance = 0.00001 )




0 comments on commit 7b780e2

Please sign in to comment.