This vignette contains a detailed tutorial on how to train a MOFA model using R. A concise template script can be found here. Many more examples on application of MOFA to various multi-omics data sets can be found here.
library(data.table) library(MOFA2)
MOFA (and factor analysis models in general) are useful to uncover variation in complex data sets that contain multiple sources of heterogeneity. This requires a relatively large sample size (at least ~15 samples). In addition, MOFA needs the multi-modal measurements to be derived from the same samples. It is fine if you have samples that are missing some data modality, but there has to be a significant degree of matched measurements.
Proper normalisation of the data is critical. The model can handle three types of data: continuous (modelled with a gaussian likelihood), small counts (modelled with a Poisson likelihood) and binary measurements (modelled with a bernoulli likelihood). Non-gaussian likelihoods give non-optimal results, we recommend the user to apply data transformations to obtain continuous measurements. For example, for count-based data such as RNA-seq or ATAC-seq we recommend size factor normalisation + variance stabilisation (i.e. a log transformation).
It is strongly recommended that you select highly variable features (HVGs) per assay before fitting the model. This ensures a faster training and a more robust inference procedure. Also, for data modalities that have very different dimensionalities we suggest a stronger feature selection fort he bigger views, with the aim of reducing the feature imbalance between data modalities.
To create a MOFA object you need to specify three dimensions: samples, features and view(s). Optionally, a group can also be specified for each sample (no group structure by default). MOFA objects can be created from a wide range of input formats, including:
A list of matrices, where each entry corresponds to one view. Samples are stored in columns and features in rows.
Let's simulate some data to start with
data <- make_example_data( n_views = 2, n_samples = 200, n_features = 1000, n_factors = 10 )[[1]] lapply(data,dim)
Create the MOFA object:
MOFAobject <- create_mofa(data)
Plot the data overview
plot_data_overview(MOFAobject)
In case you are using the multi-group functionality, the groups can be specified
using the groups
argument as a vector with the group ID for each sample.
Keep in mind that the multi-group functionality is a rather advanced option that we
discourage for beginners. For more details on how the multi-group inference works,
read the FAQ section and
check this vignette.
N = ncol(data[[1]]) groups = c(rep("A",N/2), rep("B",N/2)) MOFAobject <- create_mofa(data, groups=groups)
Plot the data overview
plot_data_overview(MOFAobject)
A long data.frame with columns sample
, feature
, view
, group
(optional), value
might be the best format for complex data sets with multiple omics and potentially multiple groups of data. Also, there is no need to add rows that correspond to missing data:
filepath <- system.file("extdata", "test_data.RData", package = "MOFA2") load(filepath) head(dt)
Create the MOFA object
MOFAobject <- create_mofa(dt) print(MOFAobject)
Plot data overview
plot_data_overview(MOFAobject)
FALSE
FALSE
data_opts <- get_default_data_options(MOFAobject) head(data_opts)
FALSE
.TRUE
.TRUE
if using multiple groups.TRUE
if using multiple views.Only change the default model options if you are familiar with the underlying mathematical model.
model_opts <- get_default_model_options(MOFAobject) model_opts$num_factors <- 10 head(model_opts)
train_opts <- get_default_training_options(MOFAobject) head(train_opts)
Prepare the MOFA object
MOFAobject <- prepare_mofa( object = MOFAobject, data_options = data_opts, model_options = model_opts, training_options = train_opts )
Train the MOFA model. Remember that in this step the MOFA2
R package connets with the
mofapy2
Python package using reticulate
. This is the source of most problems when
running MOFA. See our FAQ section
if you have issues.
outfile = file.path(getwd(),"model.hdf5") MOFAobject.trained <- run_mofa(MOFAobject, outfile)
If everything is successful, you should observe an output analogous to the following:
###################################### ## Training the model with seed 1 ## ###################################### Iteration 1: time=0.03, ELBO=-52650.68, deltaELBO=837116.802 (94.082647669%), Factors=10 (...) Iteration 9: time=0.04, ELBO=-50114.43, deltaELBO=23.907 (0.002686924%), Factors=10 ####################### ## Training finished ## ####################### Saving model in `/Users/ricard/data/mofa2/hdf5/model.hdf5...`r outfile`.
This finishes the tutorial on how to train a MOFA object from R. To continue with the downstream analysis, follow this tutorial
Session Info
sessionInfo()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.