Predicting MCIA global (factor) scores for new test samples

It may be of interest to use the embedding that is calculated on a training sample set to predict scores on a test set (or, equivalently, on new data).

After loading the nipalsMCIA library, we randomly split the NCI60 cancer cell line data into training and test sets.

Installation

# devel version

# install.packages("devtools")
devtools::install_github("Muunraker/nipalsMCIA", ref = "devel",
                         force = TRUE, build_vignettes = TRUE) # devel version
# release version
if (!require("BiocManager", quietly = TRUE))
  install.packages("BiocManager")

BiocManager::install("nipalsMCIA")
library(ggplot2)
library(MultiAssayExperiment)
library(nipalsMCIA)

Split the data

data(NCI60)
set.seed(8)

num_samples <- dim(data_blocks[[1]])[1]
num_train <- round(num_samples * 0.7, 0)
train_samples <- sample.int(num_samples, num_train)

data_blocks_train <- data_blocks
data_blocks_test <- data_blocks

for (i in seq_along(data_blocks)) {
  data_blocks_train[[i]] <- data_blocks_train[[i]][train_samples, ]
  data_blocks_test[[i]] <- data_blocks_test[[i]][-train_samples, ]
}

# Split corresponding metadata
metadata_train <- data.frame(metadata_NCI60[train_samples, ],
                             row.names = rownames(data_blocks_train$mrna))
colnames(metadata_train) <- c("cancerType")

metadata_test <- data.frame(metadata_NCI60[-train_samples, ],
                            row.names = rownames(data_blocks_test$mrna))
colnames(metadata_test) <- c("cancerType")

# Create train and test mae objects
data_blocks_train_mae <- simple_mae(data_blocks_train, row_format = "sample",
                                    colData = metadata_train)
data_blocks_test_mae <- simple_mae(data_blocks_test, row_format = "sample",
                                   colData = metadata_test)

Run nipalsMCIA on training data

MCIA_train <- nipals_multiblock(data_blocks = data_blocks_train_mae,
                                col_preproc_method = "colprofile", num_PCs = 10,
                                plots = "none", tol = 1e-9)

Visualize model on training data using metadata on cancer type

The get_metadata_colors() function returns an assignment of a color for the metadata columns. The nmb_get_gs() function returns the global scores from the input NipalsResult object.

meta_colors <- get_metadata_colors(mcia_results = MCIA_train, color_col = 1,
                                   color_pal_params = list(option = "E"))

global_scores <- nmb_get_gs(MCIA_train)
MCIA_out <- data.frame(global_scores[, 1:2])
MCIA_out$cancerType <- nmb_get_metadata(MCIA_train)$cancerType
colnames(MCIA_out) <- c("Factor.1", "Factor.2", "cancerType")

# plot the results
ggplot(data = MCIA_out, aes(x = Factor.1, y = Factor.2, color = cancerType)) +
  geom_point(size = 3) +
  labs(title = "MCIA for NCI60 training data") +
  scale_color_manual(values = meta_colors) +
  theme_bw()

Generate factor scores for test data using the MCIA_train model

We use the function to generate new factor scores on the test data set using the MCIA_train model. The new dataset in the form of an MAE object is input using the parameter test_data.

MCIA_test_scores <- predict_gs(mcia_results = MCIA_train,
                               test_data = data_blocks_test_mae)

Visualize new scores with old

We once again plot the top two factor scores for both the training and test datasets

MCIA_out_test <- data.frame(MCIA_test_scores[, 1:2])
MCIA_out_test$cancerType <-
  MultiAssayExperiment::colData(data_blocks_test_mae)$cancerType

colnames(MCIA_out_test) <- c("Factor.1", "Factor.2", "cancerType")
MCIA_out_test$set <- "test"
MCIA_out$set <- "train"
MCIA_out_full <- rbind(MCIA_out, MCIA_out_test)
rownames(MCIA_out_full) <- NULL

# plot the results
ggplot(data = MCIA_out_full,
       aes(x = Factor.1, y = Factor.2, color = cancerType, shape = set)) +
  geom_point(size = 3) +
  labs(title = "MCIA for NCI60 training and test data") +
  scale_color_manual(values = meta_colors) +
  theme_bw()

Session Info

Session Info

sessionInfo()
## R version 4.4.0 beta (2024-04-15 r86425)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.4 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.19-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_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [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    grid      stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## 
## other attached packages:
##  [1] MultiAssayExperiment_1.30.0 SummarizedExperiment_1.34.0
##  [3] Biobase_2.64.0              GenomicRanges_1.56.0       
##  [5] GenomeInfoDb_1.40.0         IRanges_2.38.0             
##  [7] S4Vectors_0.42.0            BiocGenerics_0.50.0        
##  [9] MatrixGenerics_1.16.0       matrixStats_1.3.0          
## [11] Seurat_5.0.3                SeuratObject_5.0.1         
## [13] sp_2.1-4                    piggyback_0.1.5            
## [15] BiocFileCache_2.12.0        dbplyr_2.5.0               
## [17] stringr_1.5.1               nipalsMCIA_1.2.0           
## [19] ggpubr_0.6.0                ggplot2_3.5.1              
## [21] fgsea_1.30.0                dplyr_1.1.4                
## [23] ComplexHeatmap_2.20.0       BiocStyle_2.32.0           
## 
## loaded via a namespace (and not attached):
##   [1] RcppAnnoy_0.0.22        splines_4.4.0           later_1.3.2            
##   [4] filelock_1.0.3          tibble_3.2.1            polyclip_1.10-6        
##   [7] fastDummies_1.7.3       httr2_1.0.1             lifecycle_1.0.4        
##  [10] rstatix_0.7.2           doParallel_1.0.17       globals_0.16.3         
##  [13] lattice_0.22-6          MASS_7.3-60.2           backports_1.4.1        
##  [16] magrittr_2.0.3          plotly_4.10.4           sass_0.4.9             
##  [19] rmarkdown_2.26          jquerylib_0.1.4         yaml_2.3.8             
##  [22] httpuv_1.6.15           sctransform_0.4.1       spam_2.10-0            
##  [25] spatstat.sparse_3.0-3   reticulate_1.36.1       cowplot_1.1.3          
##  [28] pbapply_1.7-2           DBI_1.2.2               RColorBrewer_1.1-3     
##  [31] lubridate_1.9.3         abind_1.4-5             zlibbioc_1.50.0        
##  [34] Rtsne_0.17              purrr_1.0.2             pracma_2.4.4           
##  [37] rappdirs_0.3.3          circlize_0.4.16         GenomeInfoDbData_1.2.12
##  [40] ggrepel_0.9.5           irlba_2.3.5.1           gitcreds_0.1.2         
##  [43] spatstat.utils_3.0-4    listenv_0.9.1           goftest_1.2-3          
##  [46] RSpectra_0.16-1         spatstat.random_3.2-3   fitdistrplus_1.1-11    
##  [49] parallelly_1.37.1       leiden_0.4.3.1          codetools_0.2-20       
##  [52] DelayedArray_0.30.0     tidyselect_1.2.1        shape_1.4.6.1          
##  [55] UCSC.utils_1.0.0        farver_2.1.1            spatstat.explore_3.2-7 
##  [58] jsonlite_1.8.8          GetoptLong_1.0.5        progressr_0.14.0       
##  [61] ggridges_0.5.6          survival_3.6-4          iterators_1.0.14       
##  [64] foreach_1.5.2           tools_4.4.0             ica_1.0-3              
##  [67] Rcpp_1.0.12             glue_1.7.0              gridExtra_2.3          
##  [70] SparseArray_1.4.0       BiocBaseUtils_1.6.0     xfun_0.43              
##  [73] withr_3.0.0             BiocManager_1.30.22     fastmap_1.1.1          
##  [76] fansi_1.0.6             digest_0.6.35           timechange_0.3.0       
##  [79] R6_2.5.1                mime_0.12               colorspace_2.1-0       
##  [82] scattermore_1.2         Cairo_1.6-2             tensor_1.5             
##  [85] spatstat.data_3.0-4     RSQLite_2.3.6           utf8_1.2.4             
##  [88] tidyr_1.3.1             generics_0.1.3          data.table_1.15.4      
##  [91] htmlwidgets_1.6.4       httr_1.4.7              S4Arrays_1.4.0         
##  [94] uwot_0.2.2              pkgconfig_2.0.3         gtable_0.3.5           
##  [97] blob_1.2.4              lmtest_0.9-40           XVector_0.44.0         
## [100] htmltools_0.5.8.1       carData_3.0-5           dotCall64_1.1-1        
## [103] bookdown_0.39           clue_0.3-65             scales_1.3.0           
## [106] png_0.1-8               knitr_1.46              reshape2_1.4.4         
## [109] rjson_0.2.21            nlme_3.1-164            curl_5.2.1             
## [112] cachem_1.0.8            zoo_1.8-12              GlobalOptions_0.1.2    
## [115] KernSmooth_2.23-22      vipor_0.4.7             parallel_4.4.0         
## [118] miniUI_0.1.1.1          ggrastr_1.0.2           pillar_1.9.0           
## [121] vctrs_0.6.5             RANN_2.6.1              promises_1.3.0         
## [124] car_3.1-2               xtable_1.8-4            cluster_2.1.6          
## [127] beeswarm_0.4.0          evaluate_0.23           tinytex_0.50           
## [130] magick_2.8.3            cli_3.6.2               compiler_4.4.0         
## [133] rlang_1.1.3             crayon_1.5.2            future.apply_1.11.2    
## [136] ggsignif_0.6.4          labeling_0.4.3          ggbeeswarm_0.7.2       
## [139] plyr_1.8.9              stringi_1.8.3           deldir_2.0-4           
## [142] viridisLite_0.4.2       BiocParallel_1.38.0     munsell_0.5.1          
## [145] gh_1.4.1                lazyeval_0.2.2          spatstat.geom_3.2-9    
## [148] Matrix_1.7-0            RcppHNSW_0.6.0          patchwork_1.2.0        
## [151] bit64_4.0.5             future_1.33.2           shiny_1.8.1.1          
## [154] highr_0.10              ROCR_1.0-11             igraph_2.0.3           
## [157] broom_1.0.5             memoise_2.0.1           bslib_0.7.0            
## [160] fastmatch_1.1-4         bit_4.0.5