Contents

1 Simulate an example data set

To illustrate the MOFA workflow we simulate a small example data set with 3 different views. makeExampleData generates an untrained MOFAobject containing the simulated data. If you work on your own data use createMOFAobject to create the untrained MOFA object (see our vignettes for CLL and scMT data).

library(MOFA)

By default the function makeExampleData produces a small data set containing 3 views with 100 features each and 50 samples. These parameters can be varied using the arguments n_views, n_features and n_samples.

set.seed(1234)
data <- makeExampleData()
MOFAobject <- createMOFAobject(data)
## Creating MOFA object from a list of matrices 
##  please make sure that samples are stored in the columns and features in the rows...
MOFAobject
## Untrained MOFA model with the following characteristics: 
##   Number of views: 3 
##   View names: view_1 view_2 view_3 
##   Number of features per view: 100 100 100
##   Number of samples: 50

2 Prepare MOFA: Set the training and model options

Once the untrained MOFAobject was created, we can specify details on data processing, model specifications and training options such as the number of factors, the likelihood models etc. Default option can be obtained using the functions getDefaultTrainOptions, getDefaultModelOptions and getDefaultDataOptions. We describe details on these option in our vignettes for CLL and scMT data.

Using prepareMOFA the model is set up for training.

TrainOptions <- getDefaultTrainOptions()
ModelOptions <- getDefaultModelOptions(MOFAobject)
DataOptions <- getDefaultDataOptions()

TrainOptions$DropFactorThreshold <- 0.01

3 Run MOFA

Once the MOFAobject is set up we can use runMOFA to train the model. As depending on the random initilization the results might differ, we recommend to use runMOFA multiple times (e.g.ย ten times, here we use a smaller number for illustration as the model training can take some time). As a next step we will compare the different fits and select the best model for downstream analyses.

n_inits <- 3
MOFAlist <- lapply(seq_len(n_inits), function(it) {
  
  TrainOptions$seed <- 2018 + it
  
  MOFAobject <- prepareMOFA(
  MOFAobject, 
  DataOptions = DataOptions,
  ModelOptions = ModelOptions,
  TrainOptions = TrainOptions
)
  
  runMOFA(MOFAobject)
})
## Checking data options...
## Checking training options...
## Checking model options...
## [1] "No output file provided, using a temporary file..."
## Warning: Factor 2 is strongly correlated with the total expression for each sample in view_1
## Such (strong) factors usually appear when count-based assays are not properly normalised by library size.
## Checking data options...
## Checking training options...
## Checking model options...
## [1] "No output file provided, using a temporary file..."
## Warning: Factor 2 is strongly correlated with the total expression for each sample in view_1
## Such (strong) factors usually appear when count-based assays are not properly normalised by library size.
## Checking data options...
## Checking training options...
## Checking model options...
## [1] "No output file provided, using a temporary file..."
## Warning: Factor 2 is strongly correlated with the total expression for each sample in view_1
## Such (strong) factors usually appear when count-based assays are not properly normalised by library size.

4 Compare different random inits and select the best model

Having a list of trained models we can use compareModels to get an overview of how many factors were inferred in each run and what the optimized ELBO value is (a model with larger ELBO is preferred).

compareModels(MOFAlist)

With compareFactors we can get an overview of how robust the factors are between different model instances.

compareFactors(MOFAlist)

For down-stream analyses we recommned to choose the model with the best ELBO value as is done by selectModel.

MOFAobject <- selectModel(MOFAlist, plotit = FALSE)
MOFAobject
## Trained MOFA model with the following characteristics:
##   Number of views: 3 
##  View names: view_1 view_2 view_3 
##   Number of features per view: 100 100 100 
##   Number of samples: 50 
##   Number of factors: 5

5 Downstream analyses

On the trained MOFAobject we can now start looking into the inferred factors, its weights etc. Here the data was generated using five factors, whose activity patterns we can recover using plotVarianceExplained.

plotVarianceExplained(MOFAobject)

For details on downstream analyses please have a look at the vignettes on the CLL data and scMT data.

6 SessionInfo

sessionInfo()
## R Under development (unstable) (2020-10-17 r79346)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.1 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.13-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.13-bioc/R/lib/libRlapack.so
## 
## 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       
## 
## attached base packages:
## [1] parallel  stats4    stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## 
## other attached packages:
##  [1] ggplot2_3.3.2                MOFAdata_1.5.0              
##  [3] MOFA_1.5.0                   MultiAssayExperiment_1.15.11
##  [5] SummarizedExperiment_1.19.9  Biobase_2.49.1              
##  [7] GenomicRanges_1.41.6         GenomeInfoDb_1.25.11        
##  [9] IRanges_2.23.10              S4Vectors_0.27.14           
## [11] BiocGenerics_0.35.4          MatrixGenerics_1.1.8        
## [13] matrixStats_0.57.0           BiocStyle_2.17.1            
## 
## loaded via a namespace (and not attached):
##  [1] splines_4.1.0          jsonlite_1.7.1         foreach_1.5.1         
##  [4] BiocManager_1.30.10    GenomeInfoDbData_1.2.4 vipor_0.4.5           
##  [7] yaml_2.2.1             ggrepel_0.8.2          corrplot_0.84         
## [10] pillar_1.4.6           lattice_0.20-41        glue_1.4.2            
## [13] reticulate_1.16        digest_0.6.26          RColorBrewer_1.1-2    
## [16] XVector_0.29.3         colorspace_1.4-1       cowplot_1.1.0         
## [19] htmltools_0.5.0        Matrix_1.2-18          plyr_1.8.6            
## [22] pkgconfig_2.0.3        pheatmap_1.0.12        magick_2.5.0          
## [25] bookdown_0.21          zlibbioc_1.35.0        purrr_0.3.4           
## [28] scales_1.1.1           tibble_3.0.4           mgcv_1.8-33           
## [31] generics_0.0.2         farver_2.0.3           ellipsis_0.3.1        
## [34] withr_2.3.0            magrittr_1.5           crayon_1.3.4          
## [37] evaluate_0.14          GGally_2.0.0           nlme_3.1-149          
## [40] doParallel_1.0.16      beeswarm_0.2.3         tools_4.1.0           
## [43] lifecycle_0.2.0        stringr_1.4.0          Rhdf5lib_1.11.3       
## [46] munsell_0.5.0          DelayedArray_0.15.16   compiler_4.1.0        
## [49] rlang_0.4.8            rhdf5_2.33.11          grid_4.1.0            
## [52] RCurl_1.98-1.2         iterators_1.0.13       rhdf5filters_1.1.3    
## [55] rappdirs_0.3.1         bitops_1.0-6           labeling_0.4.2        
## [58] rmarkdown_2.4          gtable_0.3.0           codetools_0.2-16      
## [61] reshape_0.8.8          reshape2_1.4.4         R6_2.4.1              
## [64] knitr_1.30             dplyr_1.0.2            stringi_1.5.3         
## [67] ggbeeswarm_0.6.0       Rcpp_1.0.5             vctrs_0.3.4           
## [70] tidyselect_1.1.0       xfun_0.18