library(MOFA2)
library(tidyverse)
library(pheatmap)

1 Temporal data: Simulate an example data set

To illustrate the MEFISTO method in MOFA2 we simulate a small example data set with 4 different views and one covariates defining a timeline using make_example_data. The simulation is based on 4 factors, two of which vary smoothly along the covariate (with different lengthscales) and two are independent of the covariate.

set.seed(2020)

# set number of samples and time points
N <- 200
time <- seq(0,1,length.out = N)

# generate example data
dd <- make_example_data(sample_cov = time, n_samples = N,
                        n_factors = 4, n_features = 200, n_views = 4,
                        lscales = c(0.5, 0.2, 0, 0))
# input data
data <- dd$data

# covariate matrix with samples in columns
time <- dd$sample_cov
rownames(time) <- "time"

Let’s have a look at the simulated latent temporal processes, which we want to recover:

df <- data.frame(dd$Z, t(time))
df <- gather(df, key = "factor", value = "value", starts_with("simulated_factor"))
ggplot(df, aes(x = time, y = value)) + geom_point() + facet_grid(~factor)

2 MEFISTO framework

Using the MEFISTO framework is very similar to using MOFA2. In addition to the omics data, however, we now additionally specify the time points for each sample. If you are not familiar with the MOFA2 framework, it might be helpful to have a look at MOFA2 tutorials first.

2.1 Create a MOFA object with covariates

To create the MOFA object we need to specify the training data and the covariates for pattern detection and inference of smooth factors. Here, sample_cov is a matrix with samples in columns and one row containing the time points. The sample order must match the order in data columns. Alternatively, a data frame can be provided containing one sample columns with samples names matching the sample names in the data.

First, we start by creating a standard MOFA model.

sm <- create_mofa(data = dd$data)
## Creating MOFA object from a list of matrices (features as rows, sample as columns)...

Now, we can add the additional temporal covariate, that we want to use for training.

sm <- set_covariates(sm, covariates = time)
sm
## Untrained MEFISTO model with the following characteristics: 
##  Number of views: 4 
##  Views names: view_1 view_2 view_3 view_4 
##  Number of features (per view): 200 200 200 200 
##  Number of groups: 1 
##  Groups names: group1 
##  Number of samples (per group): 200 
##  Number of covariates per sample: 1 
## 

We now successfully created a MOFA object that contains 4 views, 1 group and 1 covariate giving the time point for each sample.

2.2 Prepare a MOFA object

Before training, we can specify various options for the model, the training and the data preprocessing. If no options are specified, the model will use the default options. See also get_default_data_options, get_default_model_options and get_default_training_options to have a look at the defaults and change them where required. For illustration, we only use a small number of iterations.

Importantly, to activate the use of the covariate for a functional decomposition (MEFISTO) we now additionally to the standard MOFA options need to specify mefisto_options. For this you can just use the default options (get_default_mefisto_options), unless you want to make use of advanced options such as alignment across groups.

data_opts <- get_default_data_options(sm)

model_opts <- get_default_model_options(sm)
model_opts$num_factors <- 4

train_opts <- get_default_training_options(sm)
train_opts$maxiter <- 100

mefisto_opts <- get_default_mefisto_options(sm)

sm <- prepare_mofa(sm, model_options = model_opts,
                   mefisto_options = mefisto_opts,
                   training_options = train_opts,
                   data_options = data_opts)

2.3 Run MOFA

Now, the MOFA object is ready for training. Using run_mofa we can fit the model, which is saved in the file specified as outfile. If none is specified the output is saved in a temporary location.

outfile = file.path(tempdir(),"model.hdf5")
sm <- run_mofa(sm, outfile, use_basilisk = TRUE)
## 
##         #########################################################
##         ###           __  __  ____  ______                    ### 
##         ###          |  \/  |/ __ \|  ____/\    _             ### 
##         ###          | \  / | |  | | |__ /  \ _| |_           ### 
##         ###          | |\/| | |  | |  __/ /\ \_   _|          ###
##         ###          | |  | | |__| | | / ____ \|_|            ###
##         ###          |_|  |_|\____/|_|/_/    \_\              ###
##         ###                                                   ### 
##         ######################################################### 
##        
##  
##         
## use_float32 set to True: replacing float64 arrays by float32 arrays to speed up computations...
## 
## Successfully loaded view='view_1' group='group1' with N=200 samples and D=200 features...
## Successfully loaded view='view_2' group='group1' with N=200 samples and D=200 features...
## Successfully loaded view='view_3' group='group1' with N=200 samples and D=200 features...
## Successfully loaded view='view_4' group='group1' with N=200 samples and D=200 features...
## 
## 
## Loaded 1 covariate(s) for each sample...
## 
## 
## Model options:
## - Automatic Relevance Determination prior on the factors: False
## - Automatic Relevance Determination prior on the weights: True
## - Spike-and-slab prior on the factors: False
## - Spike-and-slab prior on the weights: False
## Likelihoods:
## - View 0 (view_1): gaussian
## - View 1 (view_2): gaussian
## - View 2 (view_3): gaussian
## - View 3 (view_4): gaussian
## 
## 
## 
## 
## ######################################
## ## Training the model with seed 42 ##
## ######################################
## 
## 
## ELBO before training: -668156.67 
## 
## Iteration 1: time=0.07, ELBO=-112628.53, deltaELBO=555528.140 (83.14339537%), Factors=4
## Iteration 2: time=0.05, Factors=4
## Iteration 3: time=0.05, Factors=4
## Iteration 4: time=0.05, Factors=4
## Iteration 5: time=0.05, Factors=4
## Iteration 6: time=0.07, ELBO=-60038.13, deltaELBO=52590.401 (7.87096855%), Factors=4
## Iteration 7: time=0.05, Factors=4
## Iteration 8: time=0.05, Factors=4
## Iteration 9: time=0.05, Factors=4
## Iteration 10: time=0.05, Factors=4
## Iteration 11: time=0.07, ELBO=-59874.44, deltaELBO=163.686 (0.02449812%), Factors=4
## Iteration 12: time=0.05, Factors=4
## Iteration 13: time=0.05, Factors=4
## Iteration 14: time=0.05, Factors=4
## Iteration 15: time=0.05, Factors=4
## Iteration 16: time=0.07, ELBO=-59706.33, deltaELBO=168.114 (0.02516083%), Factors=4
## Iteration 17: time=0.05, Factors=4
## Iteration 18: time=0.05, Factors=4
## Iteration 19: time=0.05, Factors=4
## Optimising sigma node...
## Iteration 20: time=5.62, Factors=4
## Iteration 21: time=0.07, ELBO=-58693.09, deltaELBO=1013.235 (0.15164637%), Factors=4
## Iteration 22: time=0.05, Factors=4
## Iteration 23: time=0.05, Factors=4
## Iteration 24: time=0.08, Factors=4
## Iteration 25: time=0.07, Factors=4
## Iteration 26: time=0.15, ELBO=-58472.73, deltaELBO=220.363 (0.03298080%), Factors=4
## Iteration 27: time=0.22, Factors=4
## Iteration 28: time=0.09, Factors=4
## Iteration 29: time=0.06, Factors=4
## Optimising sigma node...
## Iteration 30: time=5.50, Factors=4
## Iteration 31: time=0.07, ELBO=-58287.07, deltaELBO=185.655 (0.02778612%), Factors=4
## Iteration 32: time=0.06, Factors=4
## Iteration 33: time=0.08, Factors=4
## Iteration 34: time=0.08, Factors=4
## Iteration 35: time=0.09, Factors=4
## Iteration 36: time=0.12, ELBO=-58181.29, deltaELBO=105.780 (0.01583157%), Factors=4
## Iteration 37: time=0.09, Factors=4
## Iteration 38: time=0.08, Factors=4
## Iteration 39: time=0.05, Factors=4
## Optimising sigma node...
## Iteration 40: time=5.02, Factors=4
## Iteration 41: time=0.08, ELBO=-58026.90, deltaELBO=154.391 (0.02310704%), Factors=4
## Iteration 42: time=0.05, Factors=4
## Iteration 43: time=0.05, Factors=4
## Iteration 44: time=0.05, Factors=4
## Iteration 45: time=0.05, Factors=4
## Iteration 46: time=0.07, ELBO=-57938.97, deltaELBO=87.928 (0.01315981%), Factors=4
## Iteration 47: time=0.05, Factors=4
## Iteration 48: time=0.05, Factors=4
## Iteration 49: time=0.06, Factors=4
## Optimising sigma node...
## Iteration 50: time=4.81, Factors=4
## Iteration 51: time=0.07, ELBO=-57831.57, deltaELBO=107.408 (0.01607527%), Factors=4
## Iteration 52: time=0.05, Factors=4
## Iteration 53: time=0.05, Factors=4
## Iteration 54: time=0.05, Factors=4
## Iteration 55: time=0.05, Factors=4
## Iteration 56: time=0.07, ELBO=-57760.45, deltaELBO=71.119 (0.01064408%), Factors=4
## Iteration 57: time=0.05, Factors=4
## Iteration 58: time=0.05, Factors=4
## Iteration 59: time=0.05, Factors=4
## Optimising sigma node...
## Iteration 60: time=3.30, Factors=4
## Iteration 61: time=0.07, ELBO=-57683.54, deltaELBO=76.905 (0.01150997%), Factors=4
## Iteration 62: time=0.05, Factors=4
## Iteration 63: time=0.05, Factors=4
## Iteration 64: time=0.05, Factors=4
## Iteration 65: time=0.05, Factors=4
## Iteration 66: time=0.07, ELBO=-57646.18, deltaELBO=37.364 (0.00559204%), Factors=4
## Iteration 67: time=0.05, Factors=4
## Iteration 68: time=0.05, Factors=4
## Iteration 69: time=0.05, Factors=4
## Optimising sigma node...
## Iteration 70: time=3.67, Factors=4
## Iteration 71: time=0.07, ELBO=-57618.67, deltaELBO=27.509 (0.00411721%), Factors=4
## Iteration 72: time=0.05, Factors=4
## Iteration 73: time=0.05, Factors=4
## Iteration 74: time=0.05, Factors=4
## Iteration 75: time=0.05, Factors=4
## Iteration 76: time=0.07, ELBO=-57601.36, deltaELBO=17.311 (0.00259081%), Factors=4
## Iteration 77: time=0.05, Factors=4
## Iteration 78: time=0.06, Factors=4
## Iteration 79: time=0.21, Factors=4
## Optimising sigma node...
## Iteration 80: time=8.52, Factors=4
## Iteration 81: time=0.09, ELBO=-57581.23, deltaELBO=20.125 (0.00301207%), Factors=4
## Iteration 82: time=0.09, Factors=4
## Iteration 83: time=0.10, Factors=4
## Iteration 84: time=0.09, Factors=4
## Iteration 85: time=0.09, Factors=4
## Iteration 86: time=0.11, ELBO=-57565.76, deltaELBO=15.473 (0.00231582%), Factors=4
## Iteration 87: time=0.09, Factors=4
## Iteration 88: time=0.09, Factors=4
## Iteration 89: time=0.09, Factors=4
## Optimising sigma node...
## Iteration 90: time=5.25, Factors=4
## Iteration 91: time=0.07, ELBO=-57548.37, deltaELBO=17.389 (0.00260249%), Factors=4
## Iteration 92: time=0.05, Factors=4
## Iteration 93: time=0.05, Factors=4
## Iteration 94: time=0.05, Factors=4
## Iteration 95: time=0.05, Factors=4
## Iteration 96: time=0.07, ELBO=-57534.15, deltaELBO=14.219 (0.00212813%), Factors=4
## Iteration 97: time=0.05, Factors=4
## Iteration 98: time=0.05, Factors=4
## Iteration 99: time=0.05, Factors=4
## 
## 
## #######################
## ## Training finished ##
## #######################
## 
## 
## Saving model in /tmp/Rtmp4Ki5AA/model.hdf5...

2.4 Down-stream analysis

2.4.1 Variance explained per factor

Using plot_variance_explained we can explore which factor is active in which view. plot_factor_cor shows us whether the factors are correlated.

plot_variance_explained(sm)

r <- plot_factor_cor(sm)