binneR

hasData <- requireNamespace("metaboData", quietly = TRUE) 
if (!hasData) {
knitr::opts_chunk$set(eval = FALSE) 
msg <- paste("Note: Examples in this vignette require that the",
"`metaboData` package be installed. The system",
"currently running this vignette does not have that package",
"installed, so code examples will not be evaluated.")
msg <- paste(strwrap(msg), collapse = "\n")
message(msg)
}
packages <- c('binneR','metaboData','mzR','ggplot2','dplyr','magrittr')
lapply(packages,library,character.only = T)
htmltools::img(src = knitr::image_uri(system.file('sticker/binneRsticker.png',package = 'binneR')),
  style = 'display:block;margin-left:auto;margin-right:auto; height:200px;') 

Introduction

The binneR package provides a spectral binning approach for routine processing of flow infusion electrospray - high resolution mass spectrometry (FIE-HRMS) metabolomics fingerprinting experiments, the results of which can then be used for subsequent statistical analyses.

Spectral binning rounds high resolution fingerprinting data by a specified amu bin width. FIE-HRMS data consists of a 'plug flow', across which MS signal intensities can be averaged to provide a metabolome fingerprint. Below shows an animation of the spectrum change across ‘plug flow’ region of an example FIE-HRMS injection acquired in negative ionisation mode.

file <- filePaths('FIE-HRMS','BdistachyonEcotypes')[1]
ms <- openMSfile(file)
chrom <- mzR::header(ms)

is <- detectInfusionScans(file)
buffer <- 10
is <- c(1,is[2] + buffer)

spectrum <- peaks(ms) %>%
    purrr::map(as_tibble) %>%
    bind_rows(.id = 'seqNum') %>%
    rename(`m/z` = V1,Abundance = V2) %>%
    mutate(seqNum = as.numeric(seqNum)) %>%
    left_join(chrom %>%
                            select(seqNum,polarity),by = 'seqNum') %>%
    filter(polarity == 0) %>%
    left_join(tibble(seqNum = unique(.$seqNum),Scan = 1:length(unique(.$seqNum))),by = 'seqNum') %>%
    filter(Scan >= is[1] & Scan <= is[2]) %>%
    mutate(Bin = `m/z` %>% round(2)) %>%
    group_by(Scan,Bin) %>%
    summarise(Abundance = sum(Abundance))

lim <- max(spectrum$Abundance) + 1000

chrom <- chrom %>%
    filter(polarity == 0)
chrom$acquisitionNum <- 1:nrow(chrom)

    for (i in is[1]:is[2]) {
    p <- list()

    p$chromatogram <- ggplot(chrom,aes(x = acquisitionNum,y = totIonCurrent)) + 
        geom_line() +
        geom_vline(xintercept = i, linetype = "dashed",colour = 'red') +
        theme_bw(base_size = 10) +
        xlab('Scan Number') +
        ylab('Total Ion Count') +
        ggtitle('Chromatogram')

    p$spectrum <- spectrum %>%
        filter(Scan == i) %>%
        {
            ggplot(.,aes(x = Bin, y = 0, xend = Bin, yend = Abundance)) +
                geom_segment() +
                theme_bw() +
                labs(title = 'Spectrum',
                         x = 'm/z',
                         y = 'Abundance') +
                ylim(0,lim) +
                xlim(0,1000)
        }

    gridExtra::grid.arrange(gridExtra::arrangeGrob(p$chromatogram,p$spectrum))
}

Spectral binning is applied on a scan by scan basis where the data is rounded to the specified bin width, the signals are then sum aggregated and their intensities are averaged across the specified scans.

Prior to the use of binneR, vendor specific raw data files need to be converted to one of the open source file formats such as .mzXML or .mzML so that they can be parsed into R. Data should also be centroided to reduce bin splitting artifacts that profile data can introduce during spectral binning. The msconvert tool can be used for both data conversion and centroiding, allowing the use of vendor specific algorithms.

There are two main functionalities provided by this package.

The subsequent sections will outline the use of these two main functionalities.

Before we begin, the necessary packages need to be loaded.

library(binneR)
library(metaboData)

Infusion Scan Detection

In order to apply the spectral binning approach for FIE-HRMS data, the infusion scans need to be detected. For a set of specified file paths, the range of infusion scans can be detected using the following:

infusionScans <- detectInfusionScans(
        metaboData::filePaths('FIE-HRMS','BdistachyonEcotypes')[1],
    sranges = list(c(70,1000)),
    thresh = 0.5
)
infusionScans

The detected scans can then be checked by plotting an averaged chromatogram for these files. The infusion scans can also be plotted by supplying the range to the scans argument.

plotChromFromFile(
    metaboData::filePaths('FIE-HRMS','BdistachyonEcotypes')[1],
    sranges = list(c(70,1000)),
    scans = infusionScans
)

Simple Intensity Matrix Production - quick FIE-HRMS matrix investigations

The simplest funtionality of binneR is to read raw data vector of specified file paths, bin these to a given amu and aggregate across a given scan window. This can be useful for a quick assessment of FIE-HRMS data structures. Spectral binning can be performed using the readFiles() function as shown below. The example file within the package can be specified using the following.

file <- metaboData::filePaths('FIE-HRMS','BdistachyonEcotypes')[1]

Then the data can be spectrally binned using:

res <- readFiles(file,
                                 dp = 2,
                                 scans = infusionScans,
                                 sranges = list(c(50, 1000)),
                                 modes = c("n","p"),
                                 nCores = 1)

This will return a list containing the intensity matrices for each ionisation mode, with the rows being the individual samples and columns the spectral bins.

str(res)

binneRlyse - metabolomics fingerprinting experiments

Routine FIE-HRMS metabolomic fingerprinting experiments can require rapid the processing of hundereds of MS files that will also require sample information such as biological classes for subsequent statistical analyses. The package allows for a Binalysis that formalises the spectral binning approach using an S4 class that not only bins the data to 0.01 amu but will also extract accurate m/z for each of these bins based on 0.00001 amu binned data. The accurate m/z data can be aggregated based on a specified class structure from which the modal accurate m/z is extracted. Some bin measures are also computed that allow the assessment of the quality of the 0.01 amu bins.

Subsequent analyses of these data can easily be applied using the metabolyseR package. The metaboWorkflows package also provides customisable wrapper workflows for high resolution FIE-MS analyses.

The example data used here is from the metaboData package and consists of a comparison of leaf tissue from four B. distachyon ecotypes.

Basic Usage

There are two main functions for processing experimental data:

Sample information

binneRlyse() requires the provision of sample information (info) for the experimental run to be processed. This should be in csv format and the recommended column headers include:

The row orders of the info file should match the order in which the files paths are submitted to the binneRlyse() processing function.

Parameters

Prior to spectral binning the processing parameters first need to be selected. The default parameters can be initialised a BinParameters object using the binParameters() function as shown below.

binParameters()

These parameters specify the following:

Parameters can be altered upon initialisation of the BinParameters by specifying the parameter and it's value upon calling the binParameters function as shown below.

binParameters(scans = 6:14)

Alternatively for and already initialised BinParameters object, the slot containing the parameter of interest can be changed by directly accessing the slot as shown below.

parameters <- binParameters()
parameters@scans <- 6:14
parameters

Processing

Processing is simple and requires only the use of the binneRlyse() function. The input of this function is a vector of the paths of the data files to process, a tibble containing the sample info and BinParameters object. Below shows the files and info inputs for the example data set.

files <-  metaboData::filePaths('FIE-HRMS','BdistachyonEcotypes')

info <- metaboData::runinfo('FIE-HRMS','BdistachyonEcotypes')

It is crucial that the positions of the sample information in the info file match the sample positions within the files vector. Below shows an example of how this can be checked by matching the file names present in the info with those in the vector.

TRUE %in% (info$fileName != basename(files))

Spectral binning can then be performed with the following.

analysis <- binneRlyse(files,info,binParameters(scans = detectInfusionScans(files),cls = 'class'))
analysis <- binneRlyse(files,info,binParameters(scans = detectInfusionScans(files),cls = 'class',nCores = 2,clusterType = 'PSOCK'))
analysis

For data quality inspection, the infusion profiles this data can be plotted using:

plotChromatogram(analysis)

The spectrum fingerprints using:

plotFingerprint(analysis)

And the total ion counts using:

plotTIC(analysis)

Density profiles for individual bins can be plotted by:

plotBin(analysis,'n133.01',cls = TRUE)

Data Extraction

There are a number of functions that can be used to return processing data from a Binalysis object:

Bin Measures

There are a number of measures that can be computed that allow the assessment of the quality of a given 0.01 amu bin in terms of the accurate m/z peaks present within its boundaries. These include both purity and centrality.

dat <- readFiles(files[1],scans = detectInfusionScans(files[1]),sranges = list(c(70,1000)),dp = 5)$n
dat <- tidyr::gather(tibble::as_tibble(dat),'mz','Intensity')
dat <- mutate(dat,mz = as.numeric(stringr::str_replace(mz,'[:alpha:]','')))
dat <- mutate(dat,bin = round(mz,2))
measures <- group_by(dat,bin)
measures <- summarise(measures,purity = binneR:::binPurity(mz,Intensity),centrality = binneR:::binCentrality(mz,Intensity),Intensity = mean(Intensity))

Purity

Bin purity gives a measure of the spread of accurate m/z peaks found within a given bin and can be a signal for the presences of multiple real spectral peaks within a bin. When the total ion count (t) for a given bin is greater than 1, purity is calculated by

$$p = 1 - \frac{\sigma}{w} $$

Where p is purity, $\sigma$ is the standard deviation of the accurate m/z present within the bin and w is the width of the bin in amu. Else, when $t = 1$, p is also equal to 1. A purity closer to 1 indicates that the accurate m/z present within a bin are found over a narrow region and therefore likely only to be as the result of 1 real mass spectral peak. A reduction in purity could indicate the presence of multiple peaks present within a bin.

Below shows example density plots of two negative ionisation mode 0.01 amu bins showing high (n133.01) and low (n98.96) purity respectively.

Pure <- filter(measures,bin == 133.01)
Pure <- mutate(Pure,purity = paste('Purity = ',round(purity,3), sep = ''))
pure <- filter(dat,bin == Pure$bin)
pure <- tibble::tibble(mz = unlist(apply(pure,1,function(x){rep(x[1],x[2])})))


Impure <- filter(measures,bin == 98.96)
Impure <- mutate(Impure, purity = paste('Purity = ',round(purity,3), sep = ''))
impure <- filter(dat,bin == Impure$bin)
impure <- tibble::tibble(mz = unlist(apply(impure,1,function(x){rep(x[1],x[2])})))

p <- list()

p$pure <- ggplot(pure,aes(x = mz)) +
    geom_density() +
    theme_bw() +
    xlim(Pure$bin - 0.005,Pure$bin + 0.005) +
    ggtitle(paste('n',Pure$bin,'\t',Pure$purity,sep = '')) +
    xlab('m/z') +
    ylab('Density')

p$impure <- ggplot(impure,aes(x = mz)) +
    geom_density() +
    theme_bw() +
    xlim(Impure$bin - 0.005,Impure$bin + 0.005) +
    ggtitle(paste('n',Impure$bin,'\t',Impure$purity,sep = '')) +
    xlab('m/z') +
    ylab('Density')

gridExtra::grid.arrange(gridExtra::arrangeGrob(p$pure,p$impure))

Bin n133.01, that has a purity very close to 1, has only one peak present. Bin n98.96, that has a reduced purity, clearly has two peaks present.

Centrality

Bin centrality gives a measure of how close the mean of the accurate m/z are to the center of a given bin and can give indication of whether a peak could have been split between the boundary of tow adjacent bins. Centrality is calculated for a given bin using the equation below.

$$ c = 1 - \frac{|\mu - k|}{\frac{1}{2}w}$$

Where c is centrality, $\mu$ is the mean accurate m/z present in the bin, k is the center of the bin and w is the bin width in amu. A centrality close to 1 indicates that the accurate m/z present within the boundaries of the bin are located close to the center of the bin. Low centrality would indicate that the accurate m/z present within the bin are found close to the bin boundary and could therefore indicate bin splitting, were an mass spectral peak is split between two adjacent bins.

Below shows example density plots of two negative ionisation mode 0.01 amu bins showing high (n88.04) and low (n104.03) centrality respectively.

Pure <- filter(measures,bin == 88.04)
Pure <- mutate(Pure, centrality = paste('Centrality = ',round(centrality,3), sep = ''))
pure <- filter(dat,bin == Pure$bin)
pure <- tibble::tibble(mz = unlist(apply(pure,1,function(x){rep(x[1],x[2])})))


Impure <- filter(measures,bin == 104.03)
Impure <- mutate(Impure, centrality = paste('Centrality = ',round(centrality,3), sep = ''))
impure <- filter(dat,bin == Impure$bin)
impure <- tibble::tibble(mz = impure %>%
                                                    split(1:nrow(.)) %>%
                                                    purrr::map(~{rep(.$mz,.$Intensity)}) %>%
                                                    unlist())

p <- list()

p$pure <- ggplot(pure,aes(x = mz)) +
    geom_density() +
    theme_bw() +
    xlim(Pure$bin - 0.005,Pure$bin + 0.005) +
    ggtitle(paste('n',Pure$bin,'\t',Pure$centrality,sep = '')) +
    xlab('m/z') +
    ylab('Density')

p$impure <- ggplot(impure,aes(x = mz)) +
    geom_density() +
    theme_bw() +
    xlim(Impure$bin - 0.005,Impure$bin + 0.005) +
    ggtitle(paste('n',Impure$bin,'\t',Impure$centrality,sep = '')) +
    xlab('m/z') +
    ylab('Density')

gridExtra::grid.arrange(gridExtra::arrangeGrob(p$pure,p$impure))

Bin n88.04 has a high centrality with single peak that is located very close to the center of the bin. Whereas bin n104.03 as low centrality with a single peak that is located very close to the upper boundary of the bin and could indicate that it has been split between this bin and bin n104.04.



Try the binneR package in your browser

Any scripts or data that you put into this service are public.

binneR documentation built on May 1, 2019, 6:34 p.m.