1 Introduction

Clustering is a method to identify common pattern in highly dimensional data. This can be for example genes or proteins with similar quantitative changes, thus providing insights into the affected biological pathways.

Despite of numerous clustering algorithms, they do not account for feature variance, i.e. the uncertainty in the measurements across the different experimental conditions. VSClust determines the characteristic patterns in high-dimensional data while accounting for feature variance that is given through replicated measurements.

Here, we present an example script to run the full clustering analysis using the vsclust library. The same can be done by running the Shiny app (e.g. via its docker image or on ), or the corresponding command line script. For the source code, see .

2 Installation and additional packages

Use the common Bioconductor commands for installation:

if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("vsclust")

The full functionality of this vignette can be obtained by additionally installing and loading the packages matrixStats and clusterProfiler

3 Initialization

Here, we define the different parameters for the example data set protein_expressions. In the command-line version of VSClust (“runVSClust.R”), they can be given via yaml file.

Comments:

A. Data sets with different numbers of replicates per condition need to be adapted to contain the same number of columns per condition. These can be done by either removing excess replicates or adding empty columns.

B. We assume the input data to be of the following format: A1, B1, C1, …, A2, B2, C2, …, where letters denote sample type and numbers are the different replicates.

C. If you prefer to estimate feature variance different, use averages and add an estimate for the standard deviation as last column. You will need to set the last option of PreparedForVSClust to FALSE.

D. If you don’t have replicates, use the same format as in C. and set the standard deviations to 1.

#### Input parameters, only read when now parameter file was provided
## All principal parameters for running VSClust can be defined as in the 
## shinyapp at computproteomics.bmb.sdu.dk/Apps/VSClust 
# name of study
Experiment <- "ProtExample" 
# Number of replicates/sample per different experimental condition (sample 
# type)
NumReps <- 3  
# Number of different experimental conditions (e.g. time points or sample 
# types)
NumCond <- 4  
# Paired or unpaired statistical tests when carrying out LIMMA for 
# statistical testing
isPaired <- FALSE
# Number of threads to accelerate the calculation (use 1 in doubt)
cores <- 1 

# If 0 (default), then automatically estimate the cluster number for the 
# vsclust 
# run from the Minimum Centroid Distance
PreSetNumClustVSClust <- 0 
# If 0 (default), then automatically estimate the cluster number for the 
# original fuzzy c-means from the Minimum Centroid Distance
PreSetNumClustStand <- 0 

# max. number of clusters when estimating the number of clusters. Higher 
# numbers can drastically extend the computation time.
maxClust <- 10 

4 Statistics and data preprocessing

At first, we load the example proteomics data set and carry out statistical testing of all conditions version the first based on the LIMMA moderated t-test. The data consists of mice fed with four different diets (high fat, TTA, fish oil and TTA\(+\)fish oil). Understand more about the data set with ?protein_expressions

This will calculate the false discovery rates for the differentially regulated features (pairwise comparisons versus the first “high fat” condition) and most importantly, their expected individual variances, to be used in the variance-sensitive clustering. These variances can also be uploaded separately via a last column containing them as individual standard deviations.

The PrepareForVSClust function also creates a PCA plot to assess variability and control whether the samples have been loaded correctly (replicated samples should form groups).

After estimating the standard deviations, the matrix consists of the averaged quantitative feature values and a last column for the standard deviations of the features.

data(protein_expressions)
dat <- protein_expressions

#### running statistical analysis and estimation of individual variances
statOut <- PrepareForVSClust(dat, NumReps, NumCond, isPaired, TRUE)

dat <- statOut$dat
Sds <- dat[,ncol(dat)]
cat(paste("Features:",nrow(dat),"\nMissing values:",
            sum(is.na(dat)),"\nMedian standard deviations:",
            round(median(Sds,na.rm=TRUE),digits=3)))
## Features: 574 
## Missing values: 0 
## Median standard deviations: 0.22
## Write output into file 
write.csv(statOut$statFileOut,
          paste("",Experiment,"statFileOut.csv",sep=""))

5 Estimation of cluster number

There is no simple way to find the optimal number of clusters in a data set. For obtaining this number, we run the clustering for different cluster numbers and evaluate them via so-called validity indices, which provide information about suitable cluster numbers. VSClust uses mainly the “Maximum centroid distances” that denotes the shortest distance between any of the centroids. Alternatively, one can inspect the Xie Beni index.

The output of estimClustNum contains the suggestion for the number of clusters.

We further visualize the outcome.

#### Estimate number of clusters with maxClust as maximum number clusters 
#### to run the estimation with
ClustInd <- estimClustNum(dat, maxClust=maxClust, scaling="standardize", cores=cores)
## Running cluster number 3
## Running cluster number 4
## Running cluster number 5
## Running cluster number 6
## Running cluster number 7
## Running cluster number 8
## Running cluster number 9
## Running cluster number 10
#### Use estimate cluster number or use own
if (PreSetNumClustVSClust == 0)
  PreSetNumClustVSClust <- optimalClustNum(ClustInd)
if (PreSetNumClustStand == 0)
  PreSetNumClustStand <- optimalClustNum(ClustInd, method="FCM")
#### Visualize
  estimClust.plot(ClustInd)

6 Run final clustering

Now we run the clustering again with the optimal parameters from the estimation. One can take alternative numbers of clusters corresponding to large decays in the Minimum Centroid Distance or low values of the Xie Beni index.

First, we carry out the variance-sensitive method

#### Run clustering (VSClust and standard fcm clustering
ClustOut <- runClustWrapper(dat, 
                            PreSetNumClustVSClust, 
                            NULL, 
                            VSClust=TRUE, 
                            scaling="standardize",
                            cores=cores)
Bestcl <- ClustOut$Bestcl
VSClust_cl <- Bestcl
#ClustOut$p
## Write clustering results (VSClust)
write.csv(data.frame(cluster=Bestcl$cluster,
                     ClustOut$outFileClust,
                     isClusterMember=rowMaxs(Bestcl$membership)>0.5,
                     maxMembership=rowMaxs(Bestcl$membership),
                     Bestcl$membership), 
          paste(Experiment, 
                "FCMVarMResults", 
                Sys.Date(), 
                ".csv", 
                sep=""))
## Write coordinates of cluster centroids
write.csv(Bestcl$centers, 
          paste(Experiment,
                "FCMVarMResultsCentroids",
                Sys.Date(), 
                ".csv", 
                sep=""))

We see that most of the difference are between TTA diets and the rest. This shows that the TTA fatty acids have strong impact on the organisms. Cluster three shows the proteins that a commonly lower abundant in mice fed with fish oil and thus are related to biological processes affected this particular diet.

For comparison, this is the clustering using standard fuzzy c-means of the means over the replicates.

ClustOut <- runClustWrapper(dat, PreSetNumClustStand, NULL, VSClust=FALSE, 
                            scaling="standardize", cores=cores)
Bestcl <- ClustOut$Bestcl
## Write clustering results (standard fcm)
write.csv(data.frame(cluster=Bestcl$cluster,
                     ClustOut$outFileClust,
                     isClusterMember=rowMaxs(Bestcl$membership)>0.5,
                     maxMembership=rowMaxs(Bestcl$membership),
                     Bestcl$membership), 
          paste(Experiment, 
                "FCMResults", 
                Sys.Date(), 
                ".csv", 
                sep=""))
## Write coordinates of cluster centroids
write.csv(Bestcl$centers, paste(Experiment,
                                "FCMResultsCentroids", 
                                Sys.Date(),
                                ".csv", 
                                sep=""))

Here, the clusters look rather similar. VSClust best performs for larger numbers of different experimental conditions (one finds major improvements for \(D>6\)). For a 4-dimensional data set, the algorithm mostly filters out features with very high variance levels, making them unsuitable for belonging to a particular cluster.

This analysis is then followed by evaluating the features (here proteins) of each cluster for their biological relevance. This can be done by functional analysis with e.g. the clusterProfiler package.

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
## 
## 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] clusterProfiler_4.10.0      MultiAssayExperiment_1.28.0
##  [3] SummarizedExperiment_1.32.0 Biobase_2.62.0             
##  [5] GenomicRanges_1.54.0        GenomeInfoDb_1.38.0        
##  [7] IRanges_2.36.0              S4Vectors_0.40.0           
##  [9] BiocGenerics_0.48.0         MatrixGenerics_1.14.0      
## [11] matrixStats_1.0.0           vsclust_1.4.0              
## [13] BiocStyle_2.30.0           
## 
## loaded via a namespace (and not attached):
##   [1] RColorBrewer_1.1-3            jsonlite_1.8.7               
##   [3] magrittr_2.0.3                magick_2.8.1                 
##   [5] farver_2.1.1                  rmarkdown_2.25               
##   [7] fs_1.6.3                      zlibbioc_1.48.0              
##   [9] vctrs_0.6.4                   memoise_2.0.1                
##  [11] RCurl_1.98-1.12               ggtree_3.10.0                
##  [13] htmltools_0.5.6.1             S4Arrays_1.2.0               
##  [15] BiocBaseUtils_1.4.0           AnnotationHub_3.10.0         
##  [17] curl_5.1.0                    gridGraphics_0.5-1           
##  [19] SparseArray_1.2.0             sass_0.4.7                   
##  [21] bslib_0.5.1                   plyr_1.8.9                   
##  [23] cachem_1.0.8                  igraph_1.5.1                 
##  [25] mime_0.12                     lifecycle_1.0.3              
##  [27] pkgconfig_2.0.3               gson_0.1.0                   
##  [29] Matrix_1.6-1.1                R6_2.5.1                     
##  [31] fastmap_1.1.1                 GenomeInfoDbData_1.2.11      
##  [33] shiny_1.7.5.1                 digest_0.6.33                
##  [35] aplot_0.2.2                   enrichplot_1.22.0            
##  [37] colorspace_2.1-0              patchwork_1.1.3              
##  [39] AnnotationDbi_1.64.0          RSQLite_2.3.1                
##  [41] MPO.db_0.99.7                 filelock_1.0.2               
##  [43] fansi_1.0.5                   httr_1.4.7                   
##  [45] polyclip_1.10-6               abind_1.4-5                  
##  [47] HPO.db_0.99.2                 compiler_4.3.1               
##  [49] bit64_4.0.5                   withr_2.5.1                  
##  [51] BiocParallel_1.36.0           viridis_0.6.4                
##  [53] DBI_1.1.3                     ggforce_0.4.1                
##  [55] MASS_7.3-60                   rappdirs_0.3.3               
##  [57] DelayedArray_0.28.0           HDO.db_0.99.1                
##  [59] tools_4.3.1                   scatterpie_0.2.1             
##  [61] ape_5.7-1                     interactiveDisplayBase_1.40.0
##  [63] httpuv_1.6.12                 glue_1.6.2                   
##  [65] nlme_3.1-163                  GOSemSim_2.28.0              
##  [67] promises_1.2.1                shadowtext_0.1.2             
##  [69] grid_4.3.1                    reshape2_1.4.4               
##  [71] fgsea_1.28.0                  generics_0.1.3               
##  [73] gtable_0.3.4                  tidyr_1.3.0                  
##  [75] data.table_1.14.8             tidygraph_1.2.3              
##  [77] utf8_1.2.4                    XVector_0.42.0               
##  [79] ggrepel_0.9.4                 BiocVersion_3.18.0           
##  [81] pillar_1.9.0                  stringr_1.5.0                
##  [83] yulab.utils_0.1.0             limma_3.58.0                 
##  [85] later_1.3.1                   splines_4.3.1                
##  [87] dplyr_1.1.3                   tweenr_2.0.2                 
##  [89] treeio_1.26.0                 BiocFileCache_2.10.0         
##  [91] lattice_0.22-5                bit_4.0.5                    
##  [93] tidyselect_1.2.0              GO.db_3.18.0                 
##  [95] Biostrings_2.70.0             knitr_1.44                   
##  [97] gridExtra_2.3                 bookdown_0.36                
##  [99] xfun_0.40                     graphlayouts_1.0.1           
## [101] statmod_1.5.0                 stringi_1.7.12               
## [103] lazyeval_0.2.2                ggfun_0.1.3                  
## [105] yaml_2.3.7                    evaluate_0.22                
## [107] codetools_0.2-19              ggraph_2.1.0                 
## [109] tibble_3.2.1                  qvalue_2.34.0                
## [111] BiocManager_1.30.22           ggplotify_0.1.2              
## [113] cli_3.6.1                     xtable_1.8-4                 
## [115] munsell_0.5.0                 jquerylib_0.1.4              
## [117] Rcpp_1.0.11                   dbplyr_2.3.4                 
## [119] png_0.1-8                     parallel_4.3.1               
## [121] ellipsis_0.3.2                ggplot2_3.4.4                
## [123] blob_1.2.4                    DOSE_3.28.0                  
## [125] bitops_1.0-7                  tidytree_0.4.5               
## [127] viridisLite_0.4.2             scales_1.2.1                 
## [129] purrr_1.0.2                   crayon_1.5.2                 
## [131] rlang_1.1.1                   cowplot_1.1.1                
## [133] fastmatch_1.1-4               KEGGREST_1.42.0