library(MAAS)
library(tidydr)
library(withr)

# Set seed for reproducibility
set.seed(1234)
# Load example data
data("maas_example")
# Run MAAS
maas.test <- MAAS(maas_example$Peak, maas_example$CNV, maas_example$SNV, dims = 2:5)
## Training when k=2
## Training when k=3
## Training when k=4
## Training when k=5
# Evaluate clustering performance
clusPerformance <- data.frame(matrix(nrow = length(maas.test)-1, ncol = 5),
                              row.names = paste0("dims=", 2:length(maas.test)))
colnames(clusPerformance) <- paste0("k=", 2:6)

for(i in 1:(length(maas.test)-1)){
  for(j in 2:6){
    df <- as.data.frame(maas.test[[i]]$W)
    maas.tmp.clu <- with_seed(2, kmeans(df, centers = j)$cluster)
    clusPerformance[i,j-1] <- clusteringMetric(maas.test[[i]]$W, clu = maas.tmp.clu, disMethod = "cosine")
  }
}
clusPerformance
##              k=2        k=3         k=4          k=5          k=6
## dims=2 7.6580070 0.04792606 0.001519815 0.0003074383 0.000000e+00
## dims=3 0.1610961 0.10854696 0.011809798 0.0666966181 3.026747e-03
## dims=4 0.1280585 0.05573401 0.015179780 0.0071901979 1.336539e-05
## dims=5 0.2274295 0.09587623 0.006809193 0.0052164072 1.387891e-02
# Select one layer and perform clustering
df <- as.data.frame(maas.test[[1]]$W)
maas.clu <- data.frame(Cluster = with_seed(2, kmeans(df, centers = 2)$cluster))
maas.clu$Cluster <- as.factor(maas.clu$Cluster)

# Compute UMAP
umap.axis <- with_seed(2, umap(df, n_neighbors = 10, metric = "correlation"))
umap.axis <- as.data.frame(umap.axis)
umap.axis$Cluster <- maas.clu$Cluster
colnames(umap.axis) <- c("UMAP-1", "UMAP-2", "Cluster")

# UMAP plot
ggplot(umap.axis, aes(`UMAP-1`, `UMAP-2`)) +
  geom_point(aes(color = Cluster), size = 1.75) +
  theme_dr()+
  labs(x = "UMAP-1", y = "UMAP-2")+
  theme(panel.grid = element_blank(),
        axis.title = element_text(size = 14),
        axis.text = element_blank(),
        legend.text = element_text(size = 12))

sessionInfo()
## R version 4.3.2 (2023-10-31)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## locale:
##  [1] LC_CTYPE=en_HK.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_HK.UTF-8        LC_COLLATE=en_HK.UTF-8    
##  [5] LC_MONETARY=en_HK.UTF-8    LC_MESSAGES=en_HK.UTF-8   
##  [7] LC_PAPER=en_HK.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_HK.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: Asia/Hong_Kong
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] MAAS_1.0.0         tidydr_0.0.5       uwot_0.1.16        Matrix_1.6-3      
##  [5] ggplot2_3.4.4      withr_3.0.2        clValid_0.7        fpc_2.2-12        
##  [9] parallelDist_0.2.6 clusterSim_0.51-3  MASS_7.3-60        cluster_2.1.5     
## 
## loaded via a namespace (and not attached):
##  [1] ade4_1.7-22        tidyselect_1.2.0   farver_2.1.1       dplyr_1.1.4       
##  [5] fastmap_1.1.1      promises_1.2.1     digest_0.6.33      mime_0.12         
##  [9] lifecycle_1.0.4    ellipsis_0.3.2     processx_3.8.2     magrittr_2.0.3    
## [13] kernlab_0.9-32     compiler_4.3.2     rlang_1.1.2        sass_0.4.7        
## [17] tools_4.3.2        utf8_1.2.4         yaml_2.3.7         knitr_1.45        
## [21] labeling_0.4.3     prettyunits_1.2.0  htmlwidgets_1.6.3  pkgbuild_1.4.2    
## [25] mclust_6.1.1       pkgload_1.3.3      R2HTML_2.3.4       miniUI_0.1.1.1    
## [29] purrr_1.0.2        nnet_7.3-19        grid_4.3.2         stats4_4.3.2      
## [33] fansi_1.0.5        urlchecker_1.0.1   profvis_0.3.8      xtable_1.8-4      
## [37] e1071_1.7-13       colorspace_2.1-0   scales_1.3.0       prabclus_2.3-3    
## [41] cli_3.6.1          rmarkdown_2.25     crayon_1.5.2       generics_0.1.3    
## [45] remotes_2.4.2.1    RcppParallel_5.1.7 rstudioapi_0.15.0  robustbase_0.99-2 
## [49] sessioninfo_1.2.2  cachem_1.0.8       proxy_0.4-27       stringr_1.5.1     
## [53] modeltools_0.2-23  parallel_4.3.2     vctrs_0.6.4        devtools_2.4.5    
## [57] jsonlite_1.8.7     callr_3.7.3        diptest_0.77-1     jquerylib_0.1.4   
## [61] glue_1.6.2         DEoptimR_1.1-3     codetools_0.2-19   ps_1.7.5          
## [65] RcppAnnoy_0.0.21   stringi_1.8.2      gtable_0.3.4       later_1.3.1       
## [69] munsell_0.5.0      tibble_3.2.1       pillar_1.9.0       htmltools_0.5.7   
## [73] R6_2.5.1           evaluate_1.0.3     shiny_1.8.0        lattice_0.22-5    
## [77] highr_0.10         memoise_2.0.1      httpuv_1.6.12      ggfun_0.1.3       
## [81] bslib_0.6.0        class_7.3-22       Rcpp_1.0.11        flexmix_2.3-19    
## [85] xfun_0.41          fs_1.6.3           usethis_2.2.2      pkgconfig_2.0.3