Contents

1 Introduction

For experiments in which analyzed samples come from different classes or conditions, a common goal of statistical analysis is class comparison via hypothesis testing.

Statistical testing is performed to find peaks that are differentially abundant among different classes or conditions.

Valid statistical testing requires biological replicates in order to compare between different conditions. It should never be performed with less than 3 samples per condition.

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

We begin by loading the package:

library(Cardinal)

2 Statistical testing for a renal cell carcinoma (RCC) cancer dataset

This example uses DESI spectra collected from a renal cell carcinoma (RCC) cancer dataset consisting of 8 matched pairs of human kidney tissue. Each tissue pair consists of a normal tissue sample and a cancerous tissue sample.

MH0204_33 UH0505_12 UH0710_33 UH9610_15
UH9812_03 UH9905_18 UH9911_05 UH9912_01

For the RCC cancer dataset, the goal is to find m/z features differentially abundant between normal and cancer tissue.

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(rcc, package="CardinalWorkflows")
rcc <- as(rcc, "MSImagingExperiment")

The dataset contains 16,000 spectra with 10,200 m/z-values.

rcc
## An object of class 'MSContinuousImagingExperiment'
##   <10200 feature, 16000 pixel> imaging dataset
##     imageData(1): intensity
##     featureData(0):
##     pixelData(1): diagnosis
##     run(8): MH0204_33 UH0505_12 ... UH9911_05 UH9912_01
##     raster dimensions: 99 x 38
##     coord(2): x = 1..99, y = 1..38
##     mass range:  150.08 to 1000.00 
##     centroided: FALSE

2.1 Pre-processing

Before fitting any statistical model, pre-processing is necessary to remove noise and the number of m/z values.

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.

rcc_mean <- summarizeFeatures(rcc, "mean")
rcc_ref <- rcc_mean %>%
  peakPick(SNR=3) %>%
  peakAlign(ref="mean",
            tolerance=0.5,
            units="mz") %>%
  peakFilter() %>%
  process()

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

rcc_peaks <- rcc %>%
  normalize(method="tic") %>%
  peakBin(ref=mz(rcc_ref),
          tolerance=0.5,
          units="mz") %>%
  process()

rcc_peaks
## An object of class 'MSContinuousImagingExperiment'
##   <82 feature, 16000 pixel> imaging dataset
##     imageData(1): intensity
##     featureData(0):
##     pixelData(1): diagnosis
##     processing complete(2): normalize peakBin
##     processing pending(0):
##     run(8): MH0204_33 UH0505_12 ... UH9911_05 UH9912_01
##     raster dimensions: 99 x 38
##     coord(2): x = 1..99, y = 1..38
##     mass range: 157.3369 to 888.5270 
##     centroided: TRUE

This produces a centroided dataset with 82 peaks.

Rather than rely on the manual region-of-interest selection, we will rely on the fact that cancer tissue is on the left and the normal tissue is on the right on each slide.

xcutoff<-c(35, 23, 28, 39, 29, 28, 44, 32)

rcc_peaks$rough_diagnosis <- factor("normal", level=c("cancer", "normal"))

for ( i in 1:nlevels(run(rcc_peaks)) ) {
  cur_run <- run(rcc_peaks) == runNames(rcc_peaks)[i]
  pData(rcc_peaks)$rough_diagnosis[cur_run & coord(rcc_peaks)$x < xcutoff[i]] <- "cancer"
}

rcc_peaks$groups <- interaction(run(rcc_peaks), rcc_peaks$rough_diagnosis)
image(rcc_peaks, mz=810, groups=rough_diagnosis,
      contrast.enhance="histogram", layout=c(4,2))

2.1.1 Non-specific filtering to reduce data size

In order to reduce the size of the dataset further (because the computation we are working toward can be time consuming), we will perform non-specific filtering.

This means filtering our peaks based on a summary statistic unrelated to the condition. We will use the variance.

rcc_var <- summarizeFeatures(rcc_peaks, "var", as="DataFrame")

plot(rcc_var, var ~ mz, main="variance")

Now we keep only the peaks above the top 80% quantile of variance among peaks.

rcc_peaks2 <- rcc_peaks[rcc_var$var >= quantile(rcc_var$var, 0.8),]

2.1.2 Segmentation with spatial Dirichlet Gaussian mixture model (DGMM)

Spatial-DGMM performs peak-specific segmentation. It detects peak-specific tissue segments with homogeneous spatial composition. It can estimate the number of segments and the mean and variance of each segment.

This gives us a useful summary of the spatial distribution of each peak.

set.seed(1)
rcc_dgmm1 <- spatialDGMM(rcc_peaks2[16,], r=1, k=4, groups=1)

summary(rcc_dgmm1)
## Spatially-aware Dirichlet Gaussian mixture models:
##  
##  Segmentation on 1 group: 1 
##  Method = gaussian 
##  Distance = chebyshev
##  
##   Radius (r) Init (k) Feature Classes/Group
## 1          1        4       1             3
image(rcc_dgmm1, layout=c(4,2))

This is useful because we can use it to automatically detect segments to compare for statistical testing (e.g., “cancer” vs “normal” tissue). However, to do this without bias, we must make sure the segmentation is performed independently for each sample.

set.seed(1)
rcc_dgmm <- spatialDGMM(rcc_peaks2, r=1, k=4, groups=rcc_peaks2$groups)

summary(rcc_dgmm)
## Spatially-aware Dirichlet Gaussian mixture models:
##  
##  Segmentation on 16 groups: MH0204_33.cancer UH0505_12.cancer ... UH9911_05.normal  Segmentation on     UH9912_01.normal 
##  Method = gaussian 
##  Distance = chebyshev
##  
##    Radius (r) Init (k) Feature Classes/Group
## 1           1        4       1          2.81
## 2           1        4       2          2.94
## 3           1        4       3          3.25
## 4           1        4       4          3.19
## 5           1        4       5          3.25
## 6           1        4       6          3.12
## 7           1        4       7          3.38
## 8           1        4       8          2.88
## 9           1        4       9          2.81
## 10          1        4      10          2.88
## 11          1        4      11          2.75
## 12          1        4      12          3.31
## 13          1        4      13          3.25
## 14          1        4      14          2.50
## 15          1        4      15          3.31
## 16          1        4      16          3.00
## 17          1        4      17          2.81

2.2 Visualization

2.3 Class comparison with means-based testing

As introduced earlier, statistical testing is performed to find peaks differentially abundant among different groups. Since MS imaging produces many hundreds of measurements on the same sample, we can’t treat each mass spectrum as a separate observation. Rather, we need to compare entire samples rather than individual pixels.

One way to do this is to summarize each sample by calculating its mean intensity. We can then fit linear models to the means-summarized data.

2.3.1 Fitting models with means-summarized groups

In Cardinal, we can simply use meansTest() to do means-based testing in a MS imaging experiment. We use a one-sided formula to specify the fixed effects (the diagnosis in this particular dataset). The groups indicating the observational units must also be provided. Each group is summarized by its mean, and then a linear model is fit to the summaries.

mtest <- meansTest(rcc_peaks2, ~ rough_diagnosis, groups=rcc_peaks2$groups)

summary(mtest)
## Means-summarized linear model testing:
##  
##  Fixed effects: ~rough_diagnosis 
## 
##  Summarized 16 groups: MH0204_33.cancer UH0505_12.cancer ... UH9911_05.normal 
##  Summarized     UH9912_01.normal 
## 
##  Likelihood ratio test for fixed effects:
##  
##    Feature     LR     PValue       FDR
## 1        1 2.7438 0.09763066 0.5532404
## 2        2 0.0116 0.91432768 0.9810415
## 3        3 0.0006 0.98104154 0.9810415
## 4        4 0.0024 0.96124348 0.9810415
## 5        5 0.1067 0.74390759 0.9810415
## 6        6 0.2510 0.61636353 0.9810415
## 7        7 0.0414 0.83875285 0.9810415
## 8        8 1.9178 0.16609785 0.7059159
## 9        9 0.1591 0.68998050 0.9810415
## 10      10 0.1712 0.67906259 0.9810415
## 11      11 0.0040 0.94973143 0.9810415
## 12      12 0.0168 0.89678999 0.9810415
## 13      13 0.0337 0.85425039 0.9810415
## 14      14 0.2154 0.64253739 0.9810415
## 15      15 0.1848 0.66731186 0.9810415
## 16      16 4.0039 0.04539423 0.3858509
## 17      17 4.3245 0.03756654 0.3858509

The summarized results are automatically adjusted for multiple comparisons using FDR.

2.3.2 Interpreting the results

We can use the topFeatures() method to find differentially abundant peaks.

topFeatures(mtest, p.adjust="fdr", AdjP < .1)
## Top-ranked tests: ~rough_diagnosis vs ~1 
##      mz feature LR PValue AdjP

But we don’t find any.

2.4 Class comparison with segmentation-based testing

Means-based testing is fast and simple and can work well for homogeneous samples. However, doesn’t use the spatial structure of each peak, so it doesn’t take the advantage of MS imaging, and may result in missing differences that actually exist.

Rather than simply average the intensities, we can summarize each sample by segmenting it with spatial-DGMM, and comparing the resulting segments. This gives us a bias-free way to keep the spatial heterogeneous information.

2.4.1 Fitting models with spatial-DGMM-summarized groups

First, we must segment the data with spatialDGMM(), while making sure that each observational unit is segmented within a different group (as specified by groups). We’ve already done this. Now we use segmentationTest() to fit the models.

In order to fit the models, a representative spatial-DGMM segment must be selected for each group. There are two automated ways to do this via classControl: “Ymax” (default) means use the segments with the highest means, and “Mscore” means use the segments with the highest match scores with the fixed effects.

stest <- segmentationTest(rcc_dgmm, ~ rough_diagnosis)

summary(stest)
## Segmentation-based linear model testing:
##  
##  Fixed effects: ~rough_diagnosis 
## 
##  Summarized 16 groups: MH0204_33.cancer UH0505_12.cancer ... UH9911_05.normal 
##  Summarized     UH9912_01.normal 
## 
##  Likelihood ratio test for fixed effects:
##  
##    Radius (r) Init (k) Feature     LR      PValue        FDR
## 1           1        4       1 2.1251 0.144900111 0.49266038
## 2           1        4       2 2.1441 0.143123140 0.49266038
## 3           1        4       3 0.0354 0.850713962 0.96255666
## 4           1        4       4 0.1602 0.688997637 0.96255666
## 5           1        4       5 0.2977 0.585333362 0.96255666
## 6           1        4       6 3.1365 0.076559639 0.43383796
## 7           1        4       7 0.6921 0.405440899 0.86156191
## 8           1        4       8 1.3260 0.249518316 0.70696856
## 9           1        4       9 0.1119 0.737979099 0.96255666
## 10          1        4      10 0.0098 0.921145343 0.96255666
## 11          1        4      11 0.0022 0.962556665 0.96255666
## 12          1        4      12 0.0547 0.815059692 0.96255666
## 13          1        4      13 0.0457 0.830643326 0.96255666
## 14          1        4      14 0.7041 0.401401696 0.86156191
## 15          1        4      15 0.1038 0.747266065 0.96255666
## 16          1        4      16 9.0744 0.002592176 0.04406698
## 17          1        4      17 3.9445 0.047023441 0.39969925

2.4.2 Interpreting the results

Again, we can use the topFeatures() method to find differentially abundant peaks.

topFeatures(stest, p.adjust="fdr", AdjP < .1)
## Top-ranked tests: ~rough_diagnosis vs ~1 
##         mz r k feature     LR      PValue       AdjP
## 1 885.4684 1 4      16 9.0744 0.002592176 0.04406698

This time we find 2 differentially abundant peaks (though one is likely an isotope of the other).

plot(stest, model=list(feature=16))

image(rcc_peaks2, mz=885, layout=c(4,2),
      contrast.enhance="suppress",
      normalize.image="linear")

3 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