For experiments in which analyzed samples come from different classes or conditions, a common goal of supervised analysis is classification: given a labeled training set for which classes are already known, we want to predict the class of a new sample.
Unlike unsupervised analysis such as segmentation, classification requires biological replicates for testing and validation, to avoid biased reporting of accuracy. Cardinal provides cross-validation for this purpose.
In this vignette, we present an example classification workflow using Cardinal.
We begin by loading the package:
library(Cardinal)
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. The goal of the workflow is to develop classifiers for predicting whether a new tissue sample is normal or cancer.
MH0204_33 | UH0505_12 | UH0710_33 | UH9610_15 |
---|---|---|---|
UH9812_03 | UH9905_18 | UH9911_05 | UH9912_01 |
---|---|---|---|
In this RCC dataset, we expect that normal tissue and cancerous tissue will have unique chemical profiles, which we can use to classify new tissue based on the mass spectra.
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
First, let’s subset the dataset to include only the pixels where we have manually selected the tissue regions and labeled the diagnosis.
Then, we normalize the mass spectra to a common TIC.
rcc <- rcc %>%
subsetPixels(!is.na(diagnosis)) %>%
normalize(method="tic") %>%
process()
rcc
## An object of class 'MSContinuousImagingExperiment'
## <10200 feature, 6077 pixel> imaging dataset
## imageData(1): intensity
## featureData(0):
## pixelData(1): diagnosis
## processing complete(1): normalize
## processing pending(0):
## run(8): MH0204_33 UH0505_12 ... UH9911_05 UH9912_01
## raster dimensions: 90 x 36
## coord(2): x = 2..91, y = 2..37
## mass range: 150.08 to 1000.00
## centroided: FALSE
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 bin the rest of the dataset to the reference peaks.
rcc_peaks <- rcc %>%
peakBin(ref=mz(rcc_ref),
tolerance=0.5,
units="mz") %>%
process()
rcc_peaks
## An object of class 'MSContinuousImagingExperiment'
## <103 feature, 6077 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: 90 x 36
## coord(2): x = 2..91, y = 2..37
## mass range: 157.2720 to 890.5343
## centroided: TRUE
This produces a centroided dataset with 103 peaks.
Note that this centroided dataset will be useful for our exploratory analysis and for fitting the final classification model, but it cannot be used for cross-validation. This is because the mean spectrum was calculated from the whole dataset, so if we then used it for cross-validation, each CV fold would include some pre-processing steps that depended on the test set.
Pre-processing for cross-validation will require a different strategy.
Before proceeding with the statistical analysis, we’ll first perform some and exploratory visual analysis of the dataset.
Let’s plot the images for m/z 810, which appears abundant in both normal and tumor tissue, and doesn’t seem to be very predictive.
image(rcc_peaks, mz=810, layout=c(4,2),
contrast.enhance="suppress", normalize.image="linear")
As can be seen above, each matched pair of tissues belonging to the same subject are on the same slide (and therefore belong to the same run). Note also the the cancer tissue is on the left and the normal tissue is on the right on each slide.
Principal component analysis (PCA) is an unsupervised method for exploring a dataset. PCA is available in Cardinal through the PCA()
method.
Below, we calculate the first 2 principal components. Note that PCA does not use any information about the diagnosis.
rcc_pca <- PCA(rcc_peaks, ncomp=2)
We can overlay the PC scores of the first 2 principal components. It doesn’t appear that either component distinguishes the diagnoses.
image(rcc_pca, layout=c(4,2), normalize.image="linear")
We can plot the scores of the first 2 components against each other to see how the separate the diagnoses (or don’t, in this case).
pc_scores <- DataFrame(resultData(rcc_pca, 1, "scores"))
It doesn’t appear that PCA separates cancer versus normal tissue. At least, not the first 2 components.
plot(pc_scores, PC1 ~ PC2, groups=rcc$diagnosis)
PCA is also a useful way to visualize how much each run clusters together. A large amount of variation in the data tends to be variation between experimental runs. This is why it’s useful to have matched pairs on the same slide.
plot(pc_scores, PC1 ~ PC2, groups=run(rcc))
To classify the dataset and automatically select peaks that distinguish each class, 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 classification.
In order to avoid over-fitting due to the pre-processing, each CV fold will be processed separately. For each CV fold, peak picking will be performed on the mean spectrum of the training set, and the test set will be binned to the peaks of the training set.
We use the crossValidate()
method to perform cross-validation, and we treat each run as a separate fold.
rcc_ssc_cv <- crossValidate(rcc, rcc$diagnosis,
.fun="spatialShrunkenCentroids",
r=1, s=c(0,3,6,9,12,15),
.fold=run(rcc), .process=TRUE,
.processControl=list(SNR=3,
tolerance=0.5,
units="mz"))
summary(rcc_ssc_cv)
## Cross validation:
##
## Classification on 2 classes: cancer normal
## Summarized 8 folds: MH0204_33 UH0505_12 ... UH9911_05 UH9912_01
##
## r s Accuracy Sensitivity Specificity
## 1 1 0 0.9173528 0.7590643 0.9996052
## 2 1 3 0.9207369 0.7708707 1.0000000
## 3 1 6 0.9229160 0.7798719 0.9995791
## 4 1 9 0.9237138 0.7891882 0.9983165
## 5 1 12 0.9187112 0.7848581 0.9972651
## 6 1 15 0.9108550 0.7807598 0.9916393
It appears s = 9 produces the best accuracy.
plot(summary(rcc_ssc_cv), Accuracy ~ s, type='b')
abline(v=9, lty=2, col="red")
Having selected the parameters, we can re-fit the model on the full dataset in order to interpret it.
rcc_ssc <- spatialShrunkenCentroids(rcc_peaks, rcc$diagnosis, r=1, s=9)
summary(rcc_ssc)
## Spatially-aware nearest shrunken centroids:
##
## Classification on 2 classes: cancer normal
## Method = gaussian
## Distance = chebyshev
##
## Radius (r) Shrinkage (s) Features/Class Accuracy Sensitivity Specificity
## 1 1 9 45 0.9414185 0.8717117 1
We should ignore the accuracy here though, as it is too optimistic.
Now we plot the classified images. Opacity is used to reflect the probability of class membership.
image(rcc_ssc, layout=c(4,2))
Below, we plot the centroids for each class separately.
setup.layout(c(2,1))
plot(rcc_ssc, column=1, col=discrete.colors(2)[1],
lwd=2, layout=NULL)
plot(rcc_ssc, column=2, col=discrete.colors(2)[2],
lwd=2, layout=NULL)
Although some differences are obvious, it is difficult to tell just from the mean spectra what peaks distinguish each diagnosis.
Plotting the t-statistics tells us exactly the relationship between each class’s centroid and the global mean spectrum. The t-statistics are the difference between a class’s centroid and the global mean, divided by a standard error.
Positive t-statistics indicate that peak is systematically higher in that class as compared to the global mean spectrum.
Negative t-statistics indicate that peak is systematically lower in that class as compared to the global mean spectrum.
Due to the shrinkage parameter s
, unimportant peaks will have a t-statistic of 0 and will effectively have no effect on the classification.
plot(rcc_ssc, values="statistic", lwd=2)
This lets us clearly see which peaks are distinguishing cancer versus normal tissue.
We can use the topFeatures()
method to retrive the m/z values associated with each class.
Let’s find the m/z values associated with cancer tissue.
topFeatures(rcc_ssc, class=="cancer")
## Top-ranked features:
## mz r k s class centers statistic
## 1 885.4261 1 2 9 cancer 610.25019 35.209656
## 2 886.4591 1 2 9 cancer 329.46216 31.204847
## 3 773.5288 1 2 9 cancer 77.27058 21.863606
## 4 750.5209 1 2 9 cancer 82.06698 20.113910
## 5 887.4839 1 2 9 cancer 175.88687 18.371609
## 6 774.5303 1 2 9 cancer 35.33959 12.576057
## 7 771.5246 1 2 9 cancer 50.33745 11.217757
## 8 751.5262 1 2 9 cancer 31.97388 9.927498
## 9 748.5373 1 2 9 cancer 79.82732 9.397856
## 10 747.5387 1 2 9 cancer 111.61472 8.526437
image(rcc_peaks, mz=885, contrast.enhance="suppress", layout=c(4,2))
And let’s find the m/z values associated with normal tissue.
topFeatures(rcc_ssc, class=="normal")
## Top-ranked features:
## mz r k s class centers statistic
## 1 215.2512 1 2 9 normal 66.96234 25.29491
## 2 217.2943 1 2 9 normal 168.46712 18.76560
## 3 810.2735 1 2 9 normal 174.00922 17.40817
## 4 738.3918 1 2 9 normal 46.43561 16.70453
## 5 353.2642 1 2 9 normal 32.31149 16.14226
## 6 171.2716 1 2 9 normal 46.67170 11.65482
## 7 219.2789 1 2 9 normal 45.48309 11.64791
## 8 271.2813 1 2 9 normal 76.11224 11.48278
## 9 811.3728 1 2 9 normal 78.78064 11.02743
## 10 227.3371 1 2 9 normal 44.60540 10.56278
image(rcc_peaks, mz=215, contrast.enhance="suppress", layout=c(4,2))
Because we used a matched pairs experiment, with one subject per run, it was straightforward to treat the experimental runs as our CV folds. However, it does not always work out this way.
In general, a CV fold needs to include examples of both positive and negative classes. In addition, spectra from the same sample should not be split across multiple CV folds. In MS imaging, it is important to remember that the sample size does NOT equal the number of spectra or the number of pixels.
sessionInfo()
## R version 4.2.2 (2022-10-31)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
##
## 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
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] CardinalWorkflows_1.30.0 Cardinal_3.0.1 S4Vectors_0.36.0
## [4] EBImage_4.40.0 BiocParallel_1.32.1 BiocGenerics_0.44.0
## [7] ProtGenerics_1.30.0 BiocStyle_2.26.0
##
## loaded via a namespace (and not attached):
## [1] biglm_0.9-2.1 locfit_1.5-9.6 xfun_0.35
## [4] bslib_0.4.1 lattice_0.20-45 htmltools_0.5.3
## [7] viridisLite_0.4.1 yaml_2.3.6 rlang_1.0.6
## [10] jquerylib_0.1.4 DBI_1.1.3 sp_1.5-1
## [13] jpeg_0.1-9 stringr_1.4.1 htmlwidgets_1.5.4
## [16] codetools_0.2-18 evaluate_0.18 Biobase_2.58.0
## [19] knitr_1.40 fastmap_1.1.0 irlba_2.3.5.1
## [22] parallel_4.2.2 highr_0.9 Rcpp_1.0.9
## [25] BiocManager_1.30.19 cachem_1.0.6 magick_2.7.3
## [28] jsonlite_1.8.3 abind_1.4-5 png_0.1-7
## [31] digest_0.6.30 stringi_1.7.8 tiff_0.1-11
## [34] bookdown_0.30 grid_4.2.2 cli_3.4.1
## [37] tools_4.2.2 bitops_1.0-7 magrittr_2.0.3
## [40] sass_0.4.2 RCurl_1.98-1.9 MASS_7.3-58.1
## [43] Matrix_1.5-3 matter_2.0.1 rmarkdown_2.18
## [46] R6_2.5.1 fftwtools_0.9-11 mclust_6.0.0
## [49] signal_0.7-7 nlme_3.1-160 compiler_4.2.2