Cardinal 3 provides statistical methods for both supervised and unsupervised analysis of mass spectrometry (MS) imaging experiments. Class comparison can also be performed, provided an appropriate experimental design and sample size.
Before statistical analysis, it is important to identify the statistical goal of the experiment:
Unsupervised analysis. The data has no class labels or conditions, and we are interested in exploratory analysis to discover regions of interest in the data.
Supervised analysis. The data has class labels and we want to train a statistical or machine learning model to predict the class labels of new data.
Class comparison. The data has class labels or conditions, and we want to test whether the abundance of the mass features is different between conditions.
CardinalWorkflows provides real experimental data and more detailed discussion of the statistical methods than will be covered in this brief overview.
Suppose we are exploring an unlabeled dataset, and wish to understand the structure of the data.
set.seed(2020, kind="L'Ecuyer-CMRG")
mse <- simulateImage(preset=2, dim=c(32,32), sdnoise=0.5,
peakheight=c(2,4), centroided=TRUE)
mse$design <- makeFactor(circle=mse$circle,
square=mse$square, bg=!(mse$circle | mse$square))
image(mse, "design")
Principal components analysis is an unsupervised dimension reduction technique. It reduces the data to some number of “principal components” that are a linear combination of the original mass features, where each component is orthogonal to the last, and explains as much of the variance in the data as possible.
Use PCA()
to perform PCA on a
MSImagingExperiment
.
## SpatialPCA on 30 variables and 1024 observations
## names(5): sdev, rotation, center, scale, x
## coord(2): x = 1...32, y = 1...32
## runNames(1): run0
## modelData(): Principal components (k=3)
##
## Standard deviations (1, .., k=3):
## PC1 PC2 PC3
## 6.636906 3.436595 1.046463
##
## Rotation (n x k) = (30 x 3):
## PC1 PC2 PC3
## [1,] -0.03402269 0.21483865 0.04784900
## [2,] -0.02817299 0.18845959 0.16568654
## [3,] -0.02989212 0.19056694 0.15361428
## [4,] -0.05446889 0.32630730 -0.05332356
## [5,] -0.05922941 0.33706101 -0.21719160
## [6,] -0.06619176 0.39891098 -0.22349677
## ... ... ... ...
We can see that the first 2 principal components explain most of the variation in the data.
The loadings of the components show how each feature contributes to each component.
Plotting the principal component scores against each other is a useful way of visualization the separation between data classes.
Non-negative matrix factorization is a popular alternative to PCA when the data is naturally non-negative. The main difference between PCA and NMF is that, for NMF, all of the loadings are required to be non-negative.
Use NMF()
to perform NMF on a
MSImagingExperiment
.
## SpatialNMF on 30 variables and 1024 observations
## names(4): activation, x, iter, transpose
## coord(2): x = 1...32, y = 1...32
## runNames(1): run0
## modelData(): Non-negative matrix factorization (k=3)
##
## Activation (n x k) = (30 x 3):
## C1 C2 C3
## [1,] 0.21338134 2.62754500 0.56069166
## [2,] 0.31418203 2.44048129 0.32407636
## [3,] 0.32028017 2.45586013 0.36841506
## [4,] 0.22196108 3.83050497 0.74385792
## [5,] 0.04236733 3.92938519 2.56207820
## [6,] 0.13729507 4.55135247 0.96935625
## ... ... ... ...
We can see that NMF can pick up the variation somewhat better when the data is non-negative, as is the case for mass spectra. As before, we still only need 2 components.
As with PCA, the loadings of the NMF components show how each feature contributes to each component. The NMF components can be easier to interpret as they must be non-negative.
Plotting the principal component scores against each other is a useful way of visualization the separation between data classes.
Finding other mass features colocalized with a particular image is a common task in analysis of MS imaging experiments.
Use colocalize()
to find mass features that are
colocalized with another image.
## DataFrame with 30 rows and 7 columns
## i mz cor MOC M1 M2 Dice
## <integer> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
## 1 11 1003.36 1.000000 1.000000 1.000000 1.000000 1.000000
## 2 17 1162.23 0.874814 0.938284 0.940310 0.937382 0.906250
## 3 16 1160.55 0.860895 0.925035 0.917932 0.911279 0.869141
## 4 14 1063.12 0.859409 0.923858 0.917603 0.917799 0.878906
## 5 12 1011.54 0.833750 0.914091 0.910218 0.909627 0.867188
## ... ... ... ... ... ... ... ...
## 26 7 796.933 0.284653 0.570582 0.672447 0.559862 0.562500
## 27 9 860.483 0.282950 0.592743 0.670401 0.601311 0.593750
## 28 1 513.751 0.272135 0.574656 0.675525 0.575830 0.582031
## 29 3 707.896 0.233648 0.554803 0.656335 0.561944 0.570312
## 30 2 610.262 0.231395 0.551446 0.639217 0.560083 0.558594
By default, Pearson correlation is used to rank the colocalized features. Manders overlap coefficient (MOC), colocalization coefficients (M1 and M2), and Dice scores are also provided.
Segmentation (clustering) a dataset is a useful way to summarize an MS imaging experiment and discover regions of interest within the sample.
Spatially-aware nearest shrunken centroids clustering allows simultaneous image segmentation and feature selection.
A smoothing radius r
, initial number of clusters
k
, and sparsity parameters s
must be
provided.
The larger the sparsity parameter s
, the fewer mass
features will contribute to the segmentation.
Spatial shrunken centroids may result in fewer clusters than the
initial number of clusters k
, so it is recommended to use a
value for k
that is larger than the expected number of
clusters, and allow the method to automatically choose the number of
clusters.
set.seed(2020, kind="L'Ecuyer-CMRG")
ssc <- spatialShrunkenCentroids(mse, r=1, k=3, s=c(0,6,12,18))
ssc
## ResultsList of length 4
## names(4): r=1,k=3,s=0 r=1,k=3,s=6 r=1,k=3,s=12 r=1,k=3,s=18
## model: SpatialShrunkenCentroids
## r k s weights clusters sparsity AIC BIC
## r=1,k=3,s=0 1 3 0 gaussian 3 0.00 268.1358 859.9125
## r=1,k=3,s=6 1 3 6 gaussian 3 0.27 242.5458 715.9671
## r=1,k=3,s=12 1 3 12 gaussian 3 0.56 428.9441 774.1471
## r=1,k=3,s=18 1 3 18 gaussian 3 0.62 782.5525 1098.1667
Plotting the predicted cluster probabilities shows a clear segmentation into the ground truth image.
Spatial shrunken centroids calculates t-statistics for each segment and each mass feature. These t-statistics a measure of the difference between the cluster center and the global mean.
Mass features with t-statistics of zero do not contribute to the segmentation. The sign of the t-statistic indicates whether the mass feature is over- or under-expressed in the given cluster relative to the global mean.
Use topFeatures()
to rank mass features by
t-statistic.
## DataFrame with 90 rows and 6 columns
## i mz class statistic centers sd
## <integer> <numeric> <character> <numeric> <numeric> <numeric>
## 1 30 1983.41 1 23.2059 4.81977 0.958125
## 2 22 1340.73 1 20.7688 4.79185 1.004950
## 3 21 1286.70 1 17.9398 4.37746 0.990831
## 4 25 1524.34 1 17.9343 4.73777 1.054879
## 5 28 1721.92 1 17.5223 3.52447 0.901676
## ... ... ... ... ... ... ...
## 86 30 1983.41 3 -19.9765 1.72160 0.958125
## 87 11 1003.36 3 -19.9778 1.57904 0.979531
## 88 16 1160.55 3 -20.5388 1.31939 0.962451
## 89 14 1063.12 3 -21.2236 1.97803 0.995305
## 90 17 1162.23 3 -21.7653 1.48680 1.028952
Spatially-aware Dirichlet Gaussian mixture models (spatial-DGMM) is a method of image segmentation applied to each mass feature individually, rather than the dataset as a whole.
This is useful for summarizing molecular ion images, and for discovering structures that clustering using all mass features together may miss.
## SpatialDGMM on 30 variables and 1024 observations
## names(10): class, mu, sigma, ..., weights, r, k
## coord(2): x = 1...32, y = 1...32
## runNames(1): run0
## modelData(): Spatial Gaussian mixture model (k=3, channels=30)
##
## Groups: run0
##
## Parameter estimates:
## $mu
## , , 1
## 1 2 3
## run0 3.6332371 1.6076574 0.4928379
## , , ...
##
## $sigma
## , , 1
## 1 2 3
## run0 0.8967212 0.4318986 0.3117729
## , , ...
A different segmentation is fit for each mass feature.
Each image is modeled as a mixture of Gaussian distributions.
Spatial-DGMM segmentations can be especially useful for finding mass features colocalized with a region-of-interest.
When applied to a SpatialDGMM
object,
colocalize()
is able to use match scores that can have a
higher specificity than using Pearson correlation on the raw ion
images.
## DataFrame with 30 rows and 6 columns
## i mz MOC M1 M2 Dice
## <integer> <numeric> <numeric> <numeric> <numeric> <numeric>
## 1 22 1340.73 0.902542 0.933566 0.872549 0.902027
## 2 25 1524.34 0.887815 0.883495 0.892157 0.887805
## 3 21 1286.70 0.868424 0.919414 0.820261 0.867012
## 4 30 1983.41 0.863199 0.995652 0.748366 0.854478
## 5 29 1797.43 0.838798 0.928000 0.758170 0.834532
## ... ... ... ... ... ... ...
## 26 2 610.262 0.510958 0.351938 0.741830 0.477392
## 27 8 843.577 0.505037 0.377049 0.676471 0.484211
## 28 1 513.751 0.502791 0.353226 0.715686 0.473002
## 29 5 769.648 0.492028 0.400433 0.604575 0.481771
## 30 9 860.483 0.474327 0.351254 0.640523 0.453704
Classification of pixels into different known classes (e.g., cancer vs normal) based on the mass spectra is a common application for MS imaging.
set.seed(2020, kind="L'Ecuyer-CMRG")
mse2 <- simulateImage(preset=7, dim=c(32,32), sdnoise=0.3,
nrun=3, peakdiff=2, centroided=TRUE)
mse2$class <- makeFactor(A=mse2$circleA, B=mse2$circleB)
image(mse2, "class", layout=c(1,3))
When performing classification, it is important to use cross-validation so that reported accuracies are not overly optimistic.
We strongly recomend making sure that all spectra from the same experiment run belong to the same fold, to reduce predictive bias due to run effects.
Projection to latent structures (PLS), also called partial least squares, is a supervised dimension reduction technique. It can be thought of as being similar to PCA, but for classification or regression.
## SpatialCV on 30 variables and 3072 observations
## names(4): average, scores, folds, fitted.values
## coord(2): x = 1...32, y = 1...32
## runNames(3): run0, run1, run2
## modelData(): Cross validation with 3 folds
##
## Average accuracy:
## MacroRecall MacroPrecision
## ncomp=1 0.6298995 0.7621948
## ncomp=2 0.6489648 0.7505800
## ncomp=3 0.8905553 0.9405018
## ncomp=4 0.9443562 0.9635675
## ncomp=5 0.9000440 0.9327320
We can see that 4 components gives the best accuracy.
## SpatialPLS on 30 variables and 3072 observations
## names(16): coefficients, projection, residuals, ..., y.center, y.scale, algorithm
## coord(2): x = 1...32, y = 1...32
## runNames(3): run0, run1, run2
## modelData(): Partial least squares (k=4)
##
## Covariances (1, .., k=4):
## C1 C2 C3 C4
## 128803.687 21559.656 5671.603 17340.726
##
## Coefficients:
## [,1] [,2] [,3] [,4] [,5] [,6] ...
## A -0.04217073 -0.04367902 -0.02538701 -0.02839424 -0.05470121 -0.03897474 ...
## B 0.04911078 0.04080329 0.03473352 0.03534424 0.05303916 0.04852552 ...
We can plot the fitted response values to visualize the prediction.
The PLS regression coefficients can be used to find influential features.
Like PCA or NMF, it can be useful to plot the PLS scores against each other to visualize the separation between classes.
Note that orthgonal PLS (O-PLS) is also available via
method="opls"
or by using the separate OPLS()
method. Typically, both methods perform similarly, although O-PLS can
sometimes produce more easily interpretable regression coefficients.
Spatially-aware nearest shrunken centroids classification is an extension of nearest shrunken centroids that incorporates spatial information into the model.
Like in the clustering case of spatial shrunken centroids, a
smoothing radius r
must be provided along with sparsity
parameters s
.
cv_ssc <- crossValidate(spatialShrunkenCentroids, x=mse2, y=mse2$class,
r=2, s=c(0,3,6,9,12,15,18), folds=run(mse2))
cv_ssc
## SpatialCV on 30 variables and 3072 observations
## names(4): average, scores, folds, fitted.values
## coord(2): x = 1...32, y = 1...32
## runNames(3): run0, run1, run2
## modelData(): Cross validation with 3 folds
##
## Average accuracy:
## MacroRecall MacroPrecision
## r=2,s=0 0.7585407 0.8187899
## r=2,s=3 0.7779484 0.8273597
## r=2,s=6 0.7917592 0.8342095
## r=2,s=9 0.7934147 0.8331224
## r=2,s=12 0.7934188 0.8269159
## r=2,s=15 0.7865585 0.8123568
## r=2,s=18 0.7826594 0.8023697
We can see that the model with s=9
has the best
cross-validated accuracy for the data. However, it does not perform as
well as the PLS model.
## SpatialShrunkenCentroids on 30 variables and 3072 observations
## names(12): class, probability, scores, ..., transpose, weights, r
## coord(2): x = 1...32, y = 1...32
## runNames(3): run0, run1, run2
## modelData(): Nearest shrunken centroids (s=9.00) with 2 classes
##
## Priors (1, .., k=2):
## A B
## 0.5118644 0.4881356
##
## Statistics:
## A B
## [1,] 2.5272228 33.2508362
## [2,] . 32.0924783
## [3,] 3.9335033 37.2254149
## [4,] 1.7678840 37.9907897
## [5,] 0.5325838 33.3179476
## [6,] 6.0201243 51.8815543
## ... ... ...
Again, we can plot the predicted class probabilities to visualize the prediction.
Plotting t-statistics shows most relevant peaks have a higher abundance in class “B”.
## DataFrame with 30 rows and 6 columns
## i mz class statistic centers sd
## <integer> <numeric> <character> <numeric> <numeric> <numeric>
## 1 6 795.911 B 51.8816 5.46947 0.824042
## 2 7 796.933 B 51.2422 5.40777 0.842293
## 3 9 860.483 B 47.1777 4.86009 0.670828
## 4 10 934.117 B 39.6140 3.88333 0.718556
## 5 4 735.257 B 37.9908 4.20438 0.878579
## ... ... ... ... ... ... ...
## 26 26 1629.57 B 0 2.93571 0.747214
## 27 27 1663.66 B 0 2.49509 0.718575
## 28 28 1721.92 B 0 3.64686 0.784547
## 29 29 1797.43 B 0 3.07770 0.785953
## 30 30 1983.41 B 0 3.07946 0.893279
Statistical hypothesis testing is used to determine whether the abundance of a feature is different between two or more conditions.
In order to account for additional factors like the effect of experimental runs, subject-to-subject variability, etc., this is often done most appropriately using linear models.
This example uses a simple experiment with two conditions “A” and “B”, with three replicates in each condition.
set.seed(2020, kind="L'Ecuyer-CMRG")
mse3 <- simulateImage(preset=4, npeaks=10, dim=c(32,32), sdnoise=0.3,
nrun=4, peakdiff=2, centroided=TRUE)
mse3$trt <- makeFactor(A=mse3$circleA, B=mse3$circleB)
image(mse3, "trt", layout=c(2,4))
We know from the design of the simulation that the first 5 (of 10) m/z values differ between the conditions.
## MassDataFrame with 10 rows and 4 columns
## mz circleA circleB diff
## <numeric> <numeric> <numeric> <logical>
## 1 707.896 2.78062 4.78062 TRUE
## 2 796.933 2.94643 4.94643 TRUE
## 3 860.483 2.69110 4.69110 TRUE
## 4 1011.540 2.89764 4.89764 TRUE
## 5 1063.117 2.80560 4.80560 TRUE
## 6 1173.871 2.93415 2.93415 FALSE
## 7 1340.725 2.57144 2.57144 FALSE
## 8 1497.288 2.63123 2.63123 FALSE
## 9 1524.336 2.92315 2.92315 FALSE
## 10 1983.406 2.62873 2.62873 FALSE
## mz(1): mz
Use meansTest()
to fit linear models with the most basic
summarization. The samples must be specified with samples
.
Each sample is summarized by its mean, and then a linear model is fit to
the summaries. In this case, each sample is a separate run.
Here, we specify condition
as the sole fixed effect.
Internally, the model will call either lm()
or
lme()
depending on whether any random effects are
provided.
## MeansTest of length 10
## model: lm
## i mz fixed statistic pvalue
## 1 1 707.8959 intensity ~ condition 7.37687222 0.006606797
## 2 2 796.9335 intensity ~ condition 7.61797505 0.005778934
## 3 3 860.4833 intensity ~ condition 7.85079526 0.005079836
## 4 4 1011.5401 intensity ~ condition 2.87991660 0.089690667
## 5 5 1063.1168 intensity ~ condition 0.49033024 0.483780029
## 6 6 1173.8713 intensity ~ condition 1.95780996 0.161747733
## 7 7 1340.7251 intensity ~ condition 0.02377288 0.877464200
## 8 8 1497.2877 intensity ~ condition 0.07729537 0.780996664
## 9 9 1524.3358 intensity ~ condition 9.78299475 0.001761332
## 10 10 1983.4065 intensity ~ condition 0.01523487 0.901766910
By default, the models are summarized by performing likelihood ratio tests against the null model (with no fixed effects, retaining any random effects).
Box-and-whisker plots can be used to visualize the differences (if any) between the conditions.
Use topFeatures()
to rank the results.
## DataFrame with 4 rows and 5 columns
## i mz statistic pvalue fdr
## <integer> <numeric> <numeric> <numeric> <numeric>
## 1 9 1524.336 9.78299 0.00176133 0.016517
## 2 3 860.483 7.85080 0.00507984 0.016517
## 3 2 796.933 7.61798 0.00577893 0.016517
## 4 1 707.896 7.37687 0.00660680 0.016517
We find 3 of the 5 differentially abundant features (and 1 false discovery).
Testing of SpatialDGMM
objects is also supported by
meansTest()
. The key idea here is that spatial-DGMM
segmentation captures within-sample heterogeneity, so testing between
spatial-DGMM segments is more sensitive that simply summarizing a whole
sample by its mean.
First, we must segment the data with spatialDGMM()
,
while making sure that each sample is segmented independently (by
specifying the samples as groups
).
Now use segmentationTest()
to fit the models.
## MeansTest of length 10
## model: lm
## i mz fixed statistic pvalue
## 1 1 707.8959 intensity ~ condition 10.092264853 0.001488930
## 2 2 796.9335 intensity ~ condition 9.002933920 0.002695465
## 3 3 860.4833 intensity ~ condition 10.366078663 0.001283519
## 4 4 1011.5401 intensity ~ condition 4.831155378 0.027949894
## 5 5 1063.1168 intensity ~ condition 4.008309814 0.045276518
## 6 6 1173.8713 intensity ~ condition 1.858141969 0.172839397
## 7 7 1340.7251 intensity ~ condition 0.048039558 0.826510392
## 8 8 1497.2877 intensity ~ condition 0.003775222 0.951006539
## 9 9 1524.3358 intensity ~ condition 7.989730031 0.004704343
## 10 10 1983.4065 intensity ~ condition 0.296388806 0.586155222
As with meansTest()
, the models are summarized by
performing likelihood ratio tests against the null model (with no fixed
effects, retaining any random effects).
Box-and-whisker plots can be used to visually compare the conditions.
Use topFeatures()
to rank the results.
## DataFrame with 4 rows and 5 columns
## i mz statistic pvalue fdr
## <integer> <numeric> <numeric> <numeric> <numeric>
## 1 3 860.483 10.36608 0.00128352 0.00744465
## 2 1 707.896 10.09226 0.00148893 0.00744465
## 3 2 796.933 9.00293 0.00269547 0.00898488
## 4 9 1524.336 7.98973 0.00470434 0.01176086
We find 3 of the 5 differentially abundant features (and 1 false discovery).
## R version 4.4.2 (2024-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.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_US.UTF-8 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: Etc/UTC
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] Cardinal_3.9.0 S4Vectors_0.45.2 ProtGenerics_1.39.1
## [4] BiocGenerics_0.53.3 generics_0.1.3 BiocParallel_1.41.0
## [7] BiocStyle_2.35.0
##
## loaded via a namespace (and not attached):
## [1] Matrix_1.7-1 EBImage_4.49.0 jsonlite_1.8.9
## [4] matter_2.9.1 compiler_4.4.2 BiocManager_1.30.25
## [7] Biobase_2.67.0 bitops_1.0-9 parallel_4.4.2
## [10] jquerylib_0.1.4 CardinalIO_1.5.0 png_0.1-8
## [13] yaml_2.3.10 fastmap_1.2.0 lattice_0.22-6
## [16] R6_2.5.1 knitr_1.49 htmlwidgets_1.6.4
## [19] ontologyIndex_2.12 fftwtools_0.9-11 maketools_1.3.1
## [22] bslib_0.8.0 tiff_0.1-12 rlang_1.1.4
## [25] cachem_1.1.0 xfun_0.49 sass_0.4.9
## [28] sys_3.4.3 cli_3.6.3 digest_0.6.37
## [31] grid_4.4.2 locfit_1.5-9.10 irlba_2.3.5.1
## [34] nlme_3.1-166 lifecycle_1.0.4 evaluate_1.0.1
## [37] codetools_0.2-20 buildtools_1.0.0 RCurl_1.98-1.16
## [40] abind_1.4-8 rmarkdown_2.29 tools_4.4.2
## [43] jpeg_0.1-10 htmltools_0.5.8.1