Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/nz-enhancements' into dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	DESCRIPTION
  • Loading branch information
Michele Stravs committed Mar 9, 2022
2 parents 01dae46 + f1bd05a commit b896a8a
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 114 deletions.
7 changes: 4 additions & 3 deletions 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.3.3
Version: 3.3.4
Authors@R: c(
person(given = "RMassBank at Eawag", email = "[email protected]",
role=c("cre")),
Expand Down Expand Up @@ -35,13 +35,14 @@ Imports:
XML,rjson,S4Vectors,digest,
rcdk,yaml,mzR,methods,Biobase,MSnbase,httr,
enviPat,assertthat,logger,RCurl,readJDX,webchem,
ChemmineR,ChemmineOB
ChemmineR,ChemmineOB,R.utils
Suggests:
BiocStyle,gplots,RMassBankData,
xcms (>= 1.37.1),
CAMERA,
RUnit,
knitr
knitr,
rmarkdown
Collate:
'alternateAnalyze.R'
'formulaCalculator.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ exportMethods(show)
exportMethods(spectraCount)
exportMethods(toMassbank)
import(MSnbase)
import(R.utils)
import(RCurl)
import(Rcpp)
import(S4Vectors)
Expand Down
4 changes: 2 additions & 2 deletions R/RmbSpectrum2Update.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@

.updateObject.RmbSpectraSet.updatePolarity <- function(w)
{
w@parent@polarity <- .polarity[[w@mode]]
w@parent@polarity <- getAdductPolarity(w@mode)
for(n in seq_len(length(w@children)))
{
w@children[[n]]@polarity <- .polarity[[w@mode]]
w@children[[n]]@polarity <- getAdductPolarity(w@mode)
}
w
}
5 changes: 3 additions & 2 deletions R/buildRecord.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ getAnalyticalInfo <- function(cpd = NULL)
ai <- list()
# define positive or negative, based on processing mode.
if(!is.null(cpd))
mode <- .ionModes[[cpd@mode]]
mode <- getIonMode(cpd@mode)

# again, these constants are read from the options:
ai[['AC$INSTRUMENT']] <- getOption("RMassBank")$annotations$instrument
Expand Down Expand Up @@ -234,9 +234,10 @@ setMethod("buildRecord", "RmbSpectrum2", function(o, ..., cpd = NULL, mbdata = l
ms_fi <- list()
if(!is.null(cpd))
{
adductInfo <- getAdductInformation("")
ms_fi[['BASE_PEAK']] <- round(mz(cpd@parent)[which.max(intensity(cpd@parent))],4)
ms_fi[['PRECURSOR_M/Z']] <- round(cpd@mz,4)
ms_fi[['PRECURSOR_TYPE']] <- .precursorTypes[cpd@mode]
ms_fi[['PRECURSOR_TYPE']] <- adductInfo[adductInfo$mode == cpd@mode, "adductString"]

if(all(!is.na(spectrum@precursorIntensity),
spectrum@precursorIntensity != 0,
Expand Down
5 changes: 4 additions & 1 deletion R/createMassBank.R
Original file line number Diff line number Diff line change
Expand Up @@ -1717,7 +1717,10 @@ makeMollist <- function(compiled)
# For every "compiled" entry (here, compiled is not one "compiled" entry but the total
# list of all compiled spectra), extract the uppermost CH$NAME and the ID (from the
# first spectrum.) Make the ID into 0000 format.


emptySpectra <- unlist(lapply(compiled, function(cpd) length(cpd@children) == 0))
compiled <- compiled[!emptySpectra]

tsvlist <- t(sapply(compiled, function(entry)
{
name <- entry@children[[1]]@info[["CH$NAME"]][[1]]
Expand Down
34 changes: 0 additions & 34 deletions R/formulaCalculator.R
Original file line number Diff line number Diff line change
Expand Up @@ -364,38 +364,4 @@ split.formula.posneg <- function(f, as.formula = TRUE, as.list=FALSE)
return(list(pos=pos, neg=neg))
}

.precursorTypes <- list(
"pH" = "[M+H]+",
"pNa" = "[M+Na]+",
"mH" = "[M-H]-",
"mFA" = "[M+HCOO-]-",
"pM" = "[M]+",
"mM" = "[M]-",
"pNH4" = "[M+NH4]+")

.ionModes <- list(
"pH" = "POSITIVE",
"pNa" = "POSITIVE",
"mH" = "NEGATIVE",
"mFA" = "NEGATIVE",
"pM" = "POSITIVE",
"mM" = "NEGATIVE",
"pNH4" = "POSITIVE")

.formulaTag <- list(
"pH" = "+",
"pNa" = "+",
"mH" = "-",
"mFA" = "-",
"pM" = "+",
"mM" = "-",
"pNH4" = "+")

.polarity <- list(
"pH" = as.integer(1),
"pNa" = as.integer(1),
"mH" = as.integer(0),
"mFA" = as.integer(0),
"pM" = as.integer(1),
"mM" = as.integer(0),
"pNH4" = as.integer(1))
17 changes: 16 additions & 1 deletion R/leCsvAccess.R
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,19 @@ getMonoisotopicMass <- function(formula){
}
return(monoisotopicMass)
}

getAdductPolarity <- function(mode) {
df <- getAdductInformation("")
charge <- df[df$mode == mode,"charge"]
ifelse(charge > 0, 1L, 0L)
}

getIonMode <- function(mode) {
df <- getAdductInformation("")
charge <- df[df$mode == mode,"charge"]
ifelse(charge > 0, "POSITIVE", "NEGATIVE")
}

getAdductInformation <- function(formula){
adductDf <- as.data.frame(rbind(

Expand Down Expand Up @@ -451,7 +464,6 @@ getAdductInformation <- function(formula){
c(mode = "mH", addition = "H-1", charge = -1, adductString = "[M-H]-"),
c(mode = "mCl", addition = "Cl1", charge = -1, adductString = "[M+Cl]-"),
c(mode = "mFA", addition = "C1O2H", charge = -1, adductString = "[M+HCOOH-H]-"),
c(mode = "mAc", addition = "C2O2H3", charge = -1, adductString = "[M+CH3COOH-H]-"),
c(mode = "mH_pTFA", addition = "C2F3O2", charge = -1, adductString = "[M+CF3CO2H-H]-"),

c(mode = "mH_mC6H10O5", addition = "C-6H-11O-5", charge = -1, adductString = "[M-C6H10O5-H]-"),
Expand Down Expand Up @@ -482,12 +494,15 @@ getAdductInformation <- function(formula){
c(mode = "m3H_pM_p2Na", addition = add.formula(formula, "Na2H-3"), charge = -1, adductString = "[2M+2Na-3H]-"),
c(mode = "m3H_pM", addition = add.formula(formula, "H-3"), charge = -1, adductString = "[2M-3H]-"),
c(mode = "mH_p2M", addition = add.formula(formula, add.formula(formula, "H-1")), charge = -1, adductString = "[3M-H]-"),
c(mode = "mAc", addition = "C2O2H3", charge = -1, adductString = "[M+CH3COO]-"),

## ???
c(mode = "", addition = "", charge = 0, adductString = "[M]")
), stringsAsFactors = F)
adductDf$charge <- as.integer(adductDf$charge)



if(any(any(duplicated(adductDf$mode)), any(duplicated(adductDf$adductString)))) stop("Invalid adduct table")

return(adductDf)
Expand Down
74 changes: 39 additions & 35 deletions R/leMsMs.r
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ msmsWorkflow <- function(w, mode="pH", steps=c(1:8), confirmMode = FALSE, newRec
progressbar = "progressBarHook", MSe = FALSE)
{
.checkMbSettings()
if(!any(mode %in% knownAdducts())) stop(paste("The ionization mode", mode, "is unknown."))

if(!is.na(mode))
if(!all(mode %in% knownAdducts())) stop(paste("The ionization mode", mode, "is unknown."))
if(is.na(mode) && (1 %in% steps) && is.null(filetable))
stop("If step 1 (reading) is included, mode must be specified either as argument or in the filetable.")


if(!is.na(archivename))
w@archivename <- archivename
Expand Down Expand Up @@ -146,7 +151,7 @@ msmsWorkflow <- function(w, mode="pH", steps=c(1:8), confirmMode = FALSE, newRec
}
pb <- do.call(progressbar, list(object=NULL, value=0, min=0, max=nLen))
w@spectra <- as(lapply(w@spectra, function(spec) {
s <- analyzeMsMs(msmsPeaks = spec, mode=mode, detail=TRUE, run="preliminary",
s <- analyzeMsMs(msmsPeaks = spec, mode=spec@mode, detail=TRUE, run="preliminary",
filterSettings = settings$filterSettings,
spectraList = settings$spectraList, method = analyzeMethod)
# Progress:
Expand Down Expand Up @@ -195,7 +200,7 @@ msmsWorkflow <- function(w, mode="pH", steps=c(1:8), confirmMode = FALSE, newRec
if(newRecalibration)
{
# note: makeRecalibration takes w as argument now, because it needs to get the MS1 spectra from @spectra
recal <- makeRecalibration(w, mode,
recal <- makeRecalibration(w,
recalibrateBy = settings$recalibrateBy,
recalibrateMS1 = settings$recalibrateMS1,
recalibrator = settings$recalibrator,
Expand All @@ -205,7 +210,7 @@ msmsWorkflow <- function(w, mode="pH", steps=c(1:8), confirmMode = FALSE, newRec
}
w@parent <- w
w@aggregated <- data.frame()
spectra <- recalibrateSpectra(mode, w@spectra, w = w,
spectra <- recalibrateSpectra(w@spectra, w = w,
recalibrateBy = settings$recalibrateBy,
recalibrateMS1 = settings$recalibrateMS1)
w@spectra <- spectra
Expand All @@ -218,21 +223,21 @@ msmsWorkflow <- function(w, mode="pH", steps=c(1:8), confirmMode = FALSE, newRec
pb <- do.call(progressbar, list(object=NULL, value=0, min=0, max=nLen))

w@spectra <- as(lapply(w@spectra, function(spec) {
#print(spec$id)
if(findLevel(spec@id,TRUE) == "unknown"){
analyzeMethod <- "intensity"
} else {
analyzeMethod <- "formula"
}
s <- analyzeMsMs(msmsPeaks = spec, mode=mode, detail=TRUE, run="recalibrated",
filterSettings = settings$filterSettings,
spectraList = settings$spectraList, method = analyzeMethod)
# Progress:
nProg <<- nProg + 1
pb <- do.call(progressbar, list(object=pb, value= nProg))
return(s)
}), "SimpleList")
#print(spec$id)
if(findLevel(spec@id,TRUE) == "unknown"){
analyzeMethod <- "intensity"
} else {
analyzeMethod <- "formula"
}
s <- analyzeMsMs(msmsPeaks = spec, mode=spec@mode, detail=TRUE, run="recalibrated",
filterSettings = settings$filterSettings,
spectraList = settings$spectraList, method = analyzeMethod)
# Progress:
nProg <<- nProg + 1
pb <- do.call(progressbar, list(object=pb, value= nProg))
return(s)
}), "SimpleList")
## for(f in w@files)
## w@spectra[[basename(as.character(f))]]@name <- basename(as.character(f))
suppressWarnings(do.call(progressbar, list(object=pb, close=TRUE)))
Expand Down Expand Up @@ -268,7 +273,7 @@ msmsWorkflow <- function(w, mode="pH", steps=c(1:8), confirmMode = FALSE, newRec
{
rmb_log_info("msmsWorkflow: Step 7. Reanalyze fail peaks for N2 + O")
w <- reanalyzeFailpeaks(
w, custom_additions="N2O", mode=mode,
w, custom_additions="N2O",
filterSettings=settings$filterSettings,
progressbar=progressbar)
if(!is.na(archivename))
Expand Down Expand Up @@ -1133,6 +1138,10 @@ aggregateSpectra <- function(spec, addIncomplete=FALSE)
table.c$rawOK <- NULL
table.c$low <- NULL
table.c$satellite <- NULL
if(!("formulaSource" %in% colnames(table.c)))
table.c$formulaSource <- character(nrow(table.c))


# add scan no
table.c$scan <- rep(c@acquisitionNum, nrow(table.c))
return(table.c)
Expand Down Expand Up @@ -1405,8 +1414,6 @@ processProblematicPeaks <- function(w, archivename = NA)
#' the \code{msmsWorkspace} which contains the recalibration curves (alternatively to specifying \code{rc, rc.ms1}).
#' @param spectrum For \code{recalibrateSingleSpec}:
#' a \code{MSnbase} \code{Spectrum}-derived object, commonly a \code{RmbSpectrum2} for MS2 or \code{Spectrum1} for MS1.
#' @param mode \code{"pH", "pNa", "pM", "mH", "mM", "mFA"} for different ions
#' ([M+H]+, [M+Na]+, [M]+, [M-H]-, [M]-, [M+FA]-).
#' @param rawspec For \code{recalibrateSpectra}:an \code{RmbSpectraSetList} of \code{RmbSpectraSet} objects
#' , as the \code{w@@spectra} slot from \code{msmsWorkspace} or any object returned by \code{\link{findMsMsHR}}.
#' If empty, no spectra are recalibrated, but the recalibration curve is
Expand Down Expand Up @@ -1435,7 +1442,7 @@ processProblematicPeaks <- function(w, archivename = NA)
#'
#' @author Michael Stravs, Eawag <michael.stravs@@eawag.ch>
#' @export
makeRecalibration <- function(w, mode,
makeRecalibration <- function(w,
recalibrateBy = getOption("RMassBank")$recalibrateBy,
recalibrateMS1 = getOption("RMassBank")$recalibrateMS1,
recalibrator = getOption("RMassBank")$recalibrator,
Expand All @@ -1457,7 +1464,7 @@ makeRecalibration <- function(w, mode,
if(nrow(rcdata) == 0)
stop("No peaks matched to generate recalibration curve.")

ms1data <- recalibrate.addMS1data(w@spectra, mode, recalibrateMS1Window)
ms1data <- recalibrate.addMS1data(w@spectra, recalibrateMS1Window)
ms1data <- ms1data[,c("mzFound", "dppm", "mzCalc")]

if (recalibrateMS1 != "none") {
Expand Down Expand Up @@ -1581,7 +1588,7 @@ plotRecalibration.direct <- function(rcdata, rc, rc.ms1, title, mzrange,


#' @export
recalibrateSpectra <- function(mode, rawspec = NULL, rc = NULL, rc.ms1=NULL, w = NULL,
recalibrateSpectra <- function(rawspec = NULL, rc = NULL, rc.ms1=NULL, w = NULL,
recalibrateBy = getOption("RMassBank")$recalibrateBy,
recalibrateMS1 = getOption("RMassBank")$recalibrateMS1)
{
Expand Down Expand Up @@ -1739,7 +1746,6 @@ filterPeakSatellites <- function(peaks, filterSettings = getOption("RMassBank")$
#' filterSettings = getOption("RMassBank")$filterSettings)
#' @param aggregated A peake aggregate table (\code{w@@aggregate}) (after processing electronic noise removal!)
#' @param custom_additions The allowed additions, e.g. "N2O".
#' @param mode Processing mode (\code{"pH", "pNa", "mH"} etc.)
#' @param mass (Usually recalibrated) m/z value of the peak.
#' @param cpdID Compound ID of this spectrum.
#' @param counter Current peak index (used exclusively for the progress
Expand All @@ -1764,7 +1770,7 @@ filterPeakSatellites <- function(peaks, filterSettings = getOption("RMassBank")$
#' \dontrun{
#' reanalyzedRcSpecs <- reanalyzeFailpeaks(w@@aggregated, custom_additions="N2O", mode="pH")
#' # A single peak:
#' reanalyzeFailpeak("N2O", 105.0447, 1234, 1, 1, "pH")
#' reanalyzeFailpeak("N2O", 105.0447, 1234, 1, 1)
#' }
#'
#'
Expand All @@ -1774,7 +1780,7 @@ filterPeakSatellites <- function(peaks, filterSettings = getOption("RMassBank")$
#'
#'
#' @export
reanalyzeFailpeaks <- function(w, custom_additions, mode, filterSettings =
reanalyzeFailpeaks <- function(w, custom_additions, filterSettings =
getOption("RMassBank")$filterSettings, progressbar = "progressBarHook")
{
nProg <- 0
Expand All @@ -1786,7 +1792,7 @@ reanalyzeFailpeaks <- function(w, custom_additions, mode, filterSettings =
return(sp)

children <- lapply(sp@children, function(ch) {
if(!ch@ok)
if(!isTRUE(ch@ok))
return(ch)
peaks <- getData(ch)
# get the peaks that have no matching formula, but are considered not noise etc.
Expand All @@ -1799,7 +1805,7 @@ reanalyzeFailpeaks <- function(w, custom_additions, mode, filterSettings =
fp <- fp[!duplicated(fp$mz),,drop=FALSE]
peaks.rean <- lapply(fp$mz, reanalyzeFailpeak,
custom_additions = custom_additions, cpdID = sp@id,
mode = mode, filterSettings = filterSettings)
mode = sp@mode, filterSettings = filterSettings)
matched <- (unlist(lapply(peaks.rean, nrow))) >
0
df.rean <- do.call(rbind, peaks.rean[matched])
Expand Down Expand Up @@ -2101,7 +2107,7 @@ filterMultiplicity <- function(w, archivename=NA, mode="pH", recalcBest = TRUE,
return(sp)
children <- lapply(sp@children, function(ch)
{
if(ch@ok == FALSE)
if(!isTRUE(ch@ok))
return(ch)
# filterOK TRUE if multiplicity is sufficient
ch <- addProperty(ch, "filterOK", "logical", NA)
Expand Down Expand Up @@ -2139,8 +2145,6 @@ filterMultiplicity <- function(w, archivename=NA, mode="pH", recalcBest = TRUE,
#' @usage recalibrate.addMS1data(spec,mode="pH", recalibrateMS1Window =
#' getOption("RMassBank")$recalibrateMS1Window)
#' @param spec A \code{msmsWorkspace} or \code{RmbSpectraSetList} containing spectra for which MS1 "peaks" should be "constructed".
#' @param mode \code{"pH", "pNa", "pM", "pNH4", "mH", "mM", "mFA"} for different ions
#' ([M+H]+, [M+Na]+, [M]+, [M+NH4]+, [M-H]-, [M]-, [M+FA]-).
#' @param recalibrateMS1Window Window width to look for MS1 peaks to recalibrate (in ppm).
#' @return A dataframe with columns \code{mzFound, formula, mzCalc, dppm, dbe, int,
#' dppmBest, formulaCount, good, cpdID, scan, parentScan, dppmRc}. However,
Expand All @@ -2156,7 +2160,7 @@ filterMultiplicity <- function(w, archivename=NA, mode="pH", recalcBest = TRUE,
#' }
#' @author Michael Stravs, EAWAG <michael.stravs@@eawag.ch>
#' @export
recalibrate.addMS1data <- function(spec,mode="pH", recalibrateMS1Window =
recalibrate.addMS1data <- function(spec, recalibrateMS1Window =
getOption("RMassBank")$recalibrateMS1Window)
{
## which_OK <- lapply(validPrecursors, function(pscan)
Expand All @@ -2175,7 +2179,7 @@ recalibrate.addMS1data <- function(spec,mode="pH", recalibrateMS1Window =
ms1peaks <- lapply(specFound, function(cpd){
if(cpd@formula == "")
return(NULL)
mzL <- findMz.formula(cpd@formula,mode,recalibrateMS1Window,0)
mzL <- findMz.formula(cpd@formula,cpd@mode,recalibrateMS1Window,0)
mzCalc <- mzL$mzCenter
ms1 <- mz(cpd@parent)

Expand Down
Loading

0 comments on commit b896a8a

Please sign in to comment.