Contents

1 Introduction

The goal of unsupervised analysis of mass spectrometry (MS) imaging experiments is to discover regions in the data with distinct chemical profiles, and to select the m/z-values that uniquely distinguish these different regions from each other.

Algorithmically, this means clustering the data. In imaging experiments, the resulting cluster configurations are called spatial segmentations, and the clusters are called segments.

In this vignette, we present an example segmentation workflow using Cardinal.

We begin by loading the package:

library(Cardinal)

2 Segmentation of a pig fetus wholy body cross section

This example uses the PIGII_206 dataset: a cross section of a pig fetus captured using a Thermo LTQ instrument using desorption electrospray ionization (DESI).

First, we load the dataset from the CardinalWorkflows package. The data is stored in an older format, so we need to coerce it to an MSImagingExperiment.

data(pig206, package="CardinalWorkflows")
pig206 <- as(pig206, "MSImagingExperiment")

The dataset contains 4,959 spectra with 10,200 m/z-values.

pig206
## An object of class 'MSContinuousImagingExperiment'
##   <10200 feature, 4959 pixel> imaging dataset
##     imageData(1): intensity
##     featureData(0):
##     pixelData(0):
##     run(1): PIGII_206
##     raster dimensions: 111 x 66
##     coord(2): x = 10..120, y = 1..66
##     mass range:  150.0833 to 1000.0000 
##     centroided: FALSE

Optical image of the pig fetus section

In the optical image shown above, the brain (left), heart (center), and liver (large dark region) are clearly visible.

image(pig206, mz=885.5, plusminus=0.25)

The dataset has been cropped to remove the background slide pixels, leaving only the tissue section itself for analysis.

2.1 Pre-processing

For statistical analysis, it is useful to reduce the dataset to include only the peaks.

We calculate the mean spectrum using summarizeFeatures().

pig206_mean <- summarizeFeatures(pig206, "mean")
plot(pig206_mean)

In order to make the mass spectra comparable between different pixels, it is necessary to normalize the data. We will use TIC normalization.

Let’s calculate the TIC to see how it currently varies across the dataset in the raw, unprocessed specra.

pig206_tic <- summarizePixels(pig206, c(tic="sum"))
image(pig206_tic)

To process the dataset, we will first perform peak picking on the mean spectrum to create a set of reference peaks. We will then bin the peaks in the entire dataset to this reference.

Note that peak picking on the mean spectrum is the fastest option, but may miss low-intensity peaks or peaks that only occur in a small part of the dataset. If we wanted to be more thorough, we could use a similar procedure to perform peak picking on the entire dataset (or on a random sample of many spectra) to create the set of reference peaks.

pig206_ref <- pig206_mean %>%
  peakPick(SNR=3) %>%
  peakAlign(ref="mean",
            tolerance=0.5,
            units="mz") %>%
  peakFilter() %>%
  process()

Now we bin the rest of the dataset to the reference peaks.

pig206_peaks <- pig206 %>%
  normalize(method="tic") %>%
  peakBin(ref=mz(pig206_ref),
          tolerance=0.5,
          units="mz") %>%
  process()

pig206_peaks
## An object of class 'MSContinuousImagingExperiment'
##   <110 feature, 4959 pixel> imaging dataset
##     imageData(1): intensity
##     featureData(0):
##     pixelData(0):
##     processing complete(2): normalize peakBin
##     processing pending(0):
##     run(1): PIGII_206
##     raster dimensions: 111 x 66
##     coord(2): x = 10..120, y = 1..66
##     mass range: 157.3966 to 889.6261 
##     centroided: TRUE

This produces a centroided dataset with 110 peaks.

2.2 Visualization

Before proceeding with the statistical analysis, we’ll first perform some and exploratory visual analysis of the dataset.

2.2.1 Ion images

Below, we plot several hand-selected peaks corresponding to major organs.

m/z 187 appears highly abundant in the heart.

image(pig206_peaks, mz=187)

m/z 840 appears highly abundant in the brain and spinal cord.

image(pig206_peaks, mz=840)

m/z 537 appears highly abundant in the liver.

image(pig206_peaks, mz=537)

Rather than manually going the full dataset and hand-selecting peaks, the goal of our statistical analysis will be to automatically select the peaks that distinguish such regions (e.g., the major organs).

2.2.2 Principal components analysis (PCA)

Principal component analysis (PCA) is a popular method for exploring a dataset. PCA is available in Cardinal through the PCA() method.

Below, we calculate the first 3 principal components.

pig206_pca <- PCA(pig206_peaks, ncomp=3)

Next, we overlay the first 3 principal components.

The overlay requires some contrast enhancement to see the structures clearly. In addition, the range of the PC scores are normalized to the same range (between 0 and 1).

image(pig206_pca, contrast.enhance="histogram", normalize.image="linear")

We can plot the loadings for the principal components as well.

plot(pig206_pca, lwd=2)

PCA can sometimes be useful for exploring a dataset. For example, here, we can see that PC3 appears to distinguish the liver, but also includes several other structures. This makes it difficult to fully utilize PCA for analysis.

2.3 Segmentation with spatial shrunken centroids (SSC)

To segment the dataset and automatically select peaks that distinguish each region, we will use the spatialShrunkenCentroids() method provided by Cardinal.

Important parameters to this method include:

  • method The type of spatial weights to use:

    • “gaussian” weights use a simple Gaussian smoothing kernel

    • “adaptive” weights use an adaptive kernel that sometimes preserve edges better

  • r The neighborhood smoothing radius; this should be selected based on the size and granularity of the spatial regions in your dataset

  • s The shrinkage or sparsity parameter; the higher this number, the fewer peaks will be used to determine the final segmentation.

  • k The maximum number of segments to try; empty segments are dropped, so the resulting segmentation may use fewer than this number.

It is typically best to set k relatively high and let the algorithm drop empty segments. You typically also want to try a wide range of sparsity with the s parameter.

set.seed(1)
pig206_ssc <- spatialShrunkenCentroids(pig206_peaks, method="adaptive",
                                       r=2, s=c(0,5,10,15,20,25), k=10)
summary(pig206_ssc)
## Spatially-aware nearest shrunken centroids:
##  
##  Segmentation / clustering 
##  Method = adaptive 
##  Distance = chebyshev
##  
##   Radius (r) Init (k) Shrinkage (s) Classes Features/Class
## 1          2       10             0      10         110.00
## 2          2       10             5      10          73.50
## 3          2       10            10       8          47.00
## 4          2       10            15       7          36.71
## 5          2       10            20       6          23.67
## 6          2       10            25       5          18.80

As shown in the summary, the resulting number of segments typically decreases as s increases. This is because fewer peaks are used to determine the segmentation.

First, non-informative peaks are removed, but as s increases meaningful peaks may be removed as well. The most interesting and useful segmentations tend to be the ones with the highest value of s just before the resulting number of segments decreases too much.

2.3.1 Plotting the segmentation

Let’s plot the resulting segmentations for s = 10, 15, 20, 25.

image(pig206_ssc, model=list(s=c(10,15,20,25)))

It is useful to see how the segmentation changes as fewer peaks are used and the number of segments decreases. Noisy, less-meaningful segments tend to be removed first, so we want to explore the segmentation with the highest value of s that still captures most of the regions we would expect to see.

image(pig206_ssc, model=list(s=20))

Here, we can see the heart, brain, and liver distinguished as segments 1, 5, and 6.

2.3.2 Plotting the (shrunken) mean spectra

Plotting the shrunken centroids is analogous to plotting the mean spectrum of each segment.

plot(pig206_ssc, model=list(s=20), lwd=2)

Let’s break out the centroids for the heart, brain, and liver segments.

cols <- discrete.colors(6)
setup.layout(c(3,1))
plot(pig206_ssc, model=list(s=20), column=1, col=cols[1], lwd=2, layout=NULL)
plot(pig206_ssc, model=list(s=20), column=5, col=cols[5], lwd=2, layout=NULL)
plot(pig206_ssc, model=list(s=20), column=6, col=cols[6], lwd=2, layout=NULL)

Some differences are visible, but it can be difficult to tell exactly which peaks are changing between different segments based on the mean spectra alone.

2.3.3 Plotting and interpretting t-statistics of the m/z values

Plotting the t-statistics tells us exactly the relationship between each segment’s centroid and the global mean spectrum. The t-statistics are the difference between a segment’s centroid and the global mean, divided by a standard error.

Positive t-statistics indicate that peak is systematically higher in that segment as compared to the global mean spectrum.

Negative t-statistics indicate that peak is systematically lower in that segment as compared to the global mean spectrum.

Spatial shrunken centroids works by shrinking these t-statistics toward 0 by s, and using the new t-statistics to recompute the segment centroids. The effect is that peaks that are not very different between a specific segment and the global mean are effectively eliminated from the segmentation.

plot(pig206_ssc, model=list(s=20), values="statistic", lwd=2)

If we break out the t-statistics for the heart, brain, and liver segments we can learn something interesting.

setup.layout(c(3,1))
plot(pig206_ssc, model=list(s=20), values="statistic",
     column=1, col=cols[1], lwd=2, layout=NULL)
plot(pig206_ssc, model=list(s=20), values="statistic",
     column=5, col=cols[5], lwd=2, layout=NULL)
plot(pig206_ssc, model=list(s=20), values="statistic",
     column=6, col=cols[6], lwd=2, layout=NULL)

Very few peaks distinguish the heart, while many more distinguish the brain and liver.

2.3.4 Retrieving the top m/z-values

Use the topFeatures() method to extract the m/z values of the peaks that most distinguish each segment, ranked by t-statistic.

Peaks associated with the heart:

topFeatures(pig206_ssc, model=list(s=20), class==1)
## Top-ranked features: 
##          mz r  k  s class  centers statistic
## 1  563.0232 2 10 20     1 234.6725 106.75772
## 2  537.0414 2 10 20     1 245.9083 105.13947
## 3  535.0847 2 10 20     1 175.8859 104.04832
## 4  509.0931 2 10 20     1 107.2622  62.20064
## 5  887.6398 2 10 20     1 240.7269  48.09415
## 6  885.6166 2 10 20     1 201.4155  46.44928
## 7  281.6121 2 10 20     1 357.9152  41.92664
## 8  327.3885 2 10 20     1 138.8101  41.63053
## 9  253.6589 2 10 20     1 104.0811  39.44738
## 10 888.6141 2 10 20     1 110.4084  32.23164

Peaks associated with the brain:

topFeatures(pig206_ssc, model=list(s=20), class==5)
## Top-ranked features: 
##          mz r  k  s class   centers statistic
## 1  885.6166 2 10 20     5 165.56178  39.62499
## 2  834.4763 2 10 20     5  81.19949  34.89002
## 3  840.5077 2 10 20     5  82.12082  30.99796
## 4  838.4872 2 10 20     5  54.62071  28.13423
## 5  812.5135 2 10 20     5  95.06220  27.37759
## 6  810.5023 2 10 20     5  73.65170  26.81760
## 7  886.6237 2 10 20     5  80.86167  26.31694
## 8  305.5679 2 10 20     5  83.80559  26.07845
## 9  887.6398 2 10 20     5 164.86936  24.89625
## 10 303.5484 2 10 20     5  96.59730  22.67046

Peaks associated with the liver:

topFeatures(pig206_ssc, model=list(s=20), class==6)
## Top-ranked features: 
##          mz r  k  s class   centers statistic
## 1  215.3414 2 10 20     6 518.82123 59.209501
## 2  217.3652 2 10 20     6 212.64818 44.517154
## 3  269.3483 2 10 20     6 377.73823 42.345774
## 4  271.3883 2 10 20     6  70.85251 21.847360
## 5  166.3010 2 10 20     6  72.52988 13.449839
## 6  201.3838 2 10 20     6  58.13568 10.004044
## 7  211.3150 2 10 20     6  36.84694  8.280814
## 8  157.3966 2 10 20     6  18.37128  0.000000
## 9  159.3753 2 10 20     6  28.46826  0.000000
## 10 167.3431 2 10 20     6  35.07239  0.000000

The top m/z values for each segment match up well with the hand-selected peaks.

3 Segmentation of a cardinal painting

It can be difficult to evaluate unsupervised methods (like segmentation) on data where we do not know the ground truth.

In this section, we use an MS image of a painting, where we know the ground truth.

data(cardinal, package="CardinalWorkflows")
cardinal <- as(cardinal, "MSImagingExperiment")

Cardinal painting

In this experiment, DESI spectra were collected from an oil painting of a cardinal.

cardinal
## An object of class 'MSContinuousImagingExperiment'
##   <10800 feature, 12600 pixel> imaging dataset
##     imageData(1): intensity
##     featureData(0):
##     pixelData(0):
##     run(1): Bierbaum_demo_
##     raster dimensions: 120 x 105
##     coord(2): x = 1..120, y = 1..105
##     mass range:  100.0833 to 1000.0000 
##     centroided: FALSE

The dataset includes 12,600 spectra with 10,800 m/z values.

3.1 Pre-processing

First, we will pre-process the dataset as before, by applying peak picking to the mean spectrum.

cardinal_mean <- summarizeFeatures(cardinal, "mean")
cardinal_ref <- cardinal_mean %>%
  peakPick(SNR=3) %>%
  peakAlign(ref="mean",
            tolerance=0.5,
            units="mz") %>%
  peakFilter() %>%
  process()
cardinal_peaks <- cardinal %>%
  normalize(method="tic") %>%
  peakBin(ref=mz(cardinal_ref),
          tolerance=0.5,
          units="mz") %>%
  process()

cardinal_peaks
## An object of class 'MSContinuousImagingExperiment'
##   <106 feature, 12600 pixel> imaging dataset
##     imageData(1): intensity
##     featureData(0):
##     pixelData(0):
##     processing complete(2): normalize peakBin
##     processing pending(0):
##     run(1): Bierbaum_demo_
##     raster dimensions: 120 x 105
##     coord(2): x = 1..120, y = 1..105
##     mass range: 101.0506 to 650.1482 
##     centroided: TRUE

This results in a centroided dataset with 106 peaks.

3.2 Segmetation with SSC

Now we use spatial shrunken centroids to segment the dataset.

set.seed(1)
cardinal_ssc <- spatialShrunkenCentroids(cardinal_peaks, method="adaptive",
                                       r=2, s=c(10,20,30,40), k=10)
summary(cardinal_ssc)
## Spatially-aware nearest shrunken centroids:
##  
##  Segmentation / clustering 
##  Method = adaptive 
##  Distance = chebyshev
##  
##   Radius (r) Init (k) Shrinkage (s) Classes Features/Class
## 1          2       10            10      10          38.30
## 2          2       10            20       9          21.22
## 3          2       10            30       6          17.33
## 4          2       10            40       5          14.60
image(cardinal_ssc)

We can see that with s = 10 and s = 20, two segmments are capturing an unwanted background gradient. At s = 30, this background gradient is eliminated.

Now we can use the segmentation to re-construct the original painting.

image(cardinal_ssc, model=list(s=40),
      col=c("1"=NA, "2"="gray", "3"="black", "4"="firebrick",
            "5"="brown", "6"="darkred", "7"="red"))

Let’s find the m/z values associated with the cardinal’s body.

topFeatures(cardinal_ssc, model=list(s=40), class==7)
## Top-ranked features: 
##      mz r k s class centers statistic
image(cardinal, mz=207)

And let’s find the m/z values associated with the “DESI-MS” text.

topFeatures(cardinal_ssc, model=list(s=40), class==3)
## Top-ranked features: 
##          mz r  k  s class    centers statistic
## 1  649.1446 2 10 40     3 1198.75619 136.08767
## 2  650.1482 2 10 40     3  387.97529  98.55677
## 3  101.0506 2 10 40     3   48.04136   0.00000
## 4  112.9770 2 10 40     3   55.29567   0.00000
## 5  115.0566 2 10 40     3  123.31049   0.00000
## 6  120.0000 2 10 40     3   20.40871   0.00000
## 7  121.0259 2 10 40     3   15.61081   0.00000
## 8  127.0170 2 10 40     3   33.20068   0.00000
## 9  129.0556 2 10 40     3   83.95605   0.00000
## 10 133.9931 2 10 40     3   44.12196   0.00000
image(cardinal, mz=649)

4 Session information

sessionInfo()
## R version 4.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.18-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## Random number generation:
##  RNG:     L'Ecuyer-CMRG 
##  Normal:  Inversion 
##  Sample:  Rejection 
##  
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: America/New_York
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] CardinalWorkflows_1.34.0 Cardinal_3.4.0           S4Vectors_0.40.0        
## [4] EBImage_4.44.0           BiocParallel_1.36.0      BiocGenerics_0.48.0     
## [7] ProtGenerics_1.34.0      BiocStyle_2.30.0        
## 
## loaded via a namespace (and not attached):
##  [1] sass_0.4.7          tiff_0.1-11         bitops_1.0-7       
##  [4] jpeg_0.1-10         lattice_0.22-5      digest_0.6.33      
##  [7] magrittr_2.0.3      evaluate_0.22       grid_4.3.1         
## [10] bookdown_0.36       fftwtools_0.9-11    fastmap_1.1.1      
## [13] jsonlite_1.8.7      Matrix_1.6-1.1      ontologyIndex_2.11 
## [16] matter_2.4.0        DBI_1.1.3           mclust_6.0.0       
## [19] biglm_0.9-2.1       BiocManager_1.30.22 viridisLite_0.4.2  
## [22] codetools_0.2-19    jquerylib_0.1.4     abind_1.4-5        
## [25] cli_3.6.1           rlang_1.1.1         CardinalIO_1.0.0   
## [28] Biobase_2.62.0      cachem_1.0.8        yaml_2.3.7         
## [31] tools_4.3.1         parallel_4.3.1      locfit_1.5-9.8     
## [34] R6_2.5.1            png_0.1-8           magick_2.8.1       
## [37] htmlwidgets_1.6.2   MASS_7.3-60         irlba_2.3.5.1      
## [40] bslib_0.5.1         Rcpp_1.0.11         xfun_0.40          
## [43] knitr_1.44          htmltools_0.5.6.1   nlme_3.1-163       
## [46] rmarkdown_2.25      signal_0.7-7        compiler_4.3.1     
## [49] RCurl_1.98-1.12