Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement mofa2 wrapper for MAE #144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ License: file LICENSE
Description: The MOFA2 package contains a collection of tools for training and analysing multi-omic factor analysis (MOFA). MOFA is a probabilistic factor model that aims to identify principal axes of variation from data sets that can comprise multiple omic layers and/or groups of samples. Additional time or space information on the samples can be incorporated using the MEFISTO framework, which is part of MOFA2. Downstream analysis functions to inspect molecular features underlying each factor, vizualisation, imputation etc are available.
Encoding: UTF-8
Depends: R (>= 4.0)
Imports: rhdf5, dplyr, tidyr, reshape2, pheatmap, ggplot2, methods, RColorBrewer, cowplot, ggrepel, reticulate, HDF5Array, grDevices, stats, magrittr, forcats, utils, corrplot, DelayedArray, Rtsne, uwot, basilisk, stringi
Suggests: knitr, testthat, Seurat, SeuratObject, ggpubr, foreach, psych, MultiAssayExperiment, SummarizedExperiment, SingleCellExperiment, ggrastr, mvtnorm, GGally, rmarkdown, data.table, tidyverse, BiocStyle, Matrix, markdown
Imports: rhdf5, dplyr, tidyr, reshape2, pheatmap, ggplot2, methods, RColorBrewer, cowplot, ggrepel, reticulate, HDF5Array, grDevices, stats, magrittr, forcats, utils, corrplot, DelayedArray, Rtsne, uwot, basilisk, stringi, MultiAssayExperiment, SummarizedExperiment,
Suggests: knitr, testthat, Seurat, SeuratObject, ggpubr, foreach, psych, SingleCellExperiment, ggrastr, mvtnorm, GGally, rmarkdown, data.table, tidyverse, BiocStyle, Matrix, markdown, mia
biocViews: DimensionReduction, Bayesian, Visualization
URL: https://biofam.github.io/MOFA2/index.html
BugReports: https://github.com/bioFAM/MOFA2
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export(impute)
export(interpolate_factors)
export(load_model)
export(make_example_data)
export(mofa2)
export(plot_alignment)
export(plot_ascii_data)
export(plot_data_heatmap)
Expand Down Expand Up @@ -106,6 +107,7 @@ exportMethods(factors_names)
exportMethods(features_metadata)
exportMethods(features_names)
exportMethods(groups_names)
exportMethods(mofa2)
exportMethods(samples_metadata)
exportMethods(samples_names)
exportMethods(views_names)
Expand All @@ -121,8 +123,12 @@ import(reticulate)
import(tidyr)
importFrom(DelayedArray,DelayedArray)
importFrom(HDF5Array,HDF5ArraySeed)
importFrom(MultiAssayExperiment,experiments)
importFrom(RColorBrewer,brewer.pal)
importFrom(Rtsne,Rtsne)
importFrom(SummarizedExperiment,assay)
importFrom(SummarizedExperiment,assayNames)
importFrom(SummarizedExperiment,assays)
importFrom(basilisk,BasiliskEnvironment)
importFrom(corrplot,corrplot)
importFrom(cowplot,plot_grid)
Expand Down
106 changes: 106 additions & 0 deletions R/create_mofa_from_mae.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#' Create MOFA from MAE
#'
#' \code{mofa2} produces a prepared MOFA2 model from a MAE.
#'
#' @param mae a \code{\link[MultiAssayExperiment:MultiAssayExperiment]{MultiAssayExperiment}}
#'
#' @param assay.types List of assays to include in MOFA model (default is "counts").
#'
#' @param groups One of the variable names of coldata from which groups should be derived (default is NULL).
#'
#' @param extract_metadata Boolean specifying whethet metadata should be extracted (default is FALSE).
#'
#' @param ... additional parameters for MOFA, named as the options
#' of \code{\link[MOFA2:prepare_mofa]{prepare_mofa}}.
#'
#' @return
#' Returns an untrained \code{\link[MOFA2:MOFA]{MOFA}} with specified options
#' filled in the corresponding slots
#'
#' @name create_mofa_from_mae
#'
#' @examples
#' # Load package and import dataset
#' library(mia)
#' data("HintikkaXOData", package = "mia")
#' mae <- HintikkaXOData
#'
#' # Prepare basic model with selected assays
#' prep_model <- mofa2(mae, assay.types = c("counts", "nmr", "signals"))
#'
#' # Specify grouping variable and extract metadata
#' prep_model <- mofa2(mae, assay.types = c("counts", "nmr", "signals"),
#' groups = "Diet", extract_metadata = TRUE)
#'
#' # Modify MOFA options with corresponding arguments
#' prep_model <- mofa2(mae, assay.types = c("counts", "nmr", "signals"),
#' num_factors = 5, stochastic = TRUE)
NULL

#' @rdname create_mofa_from_mae
#' @export
setGeneric("mofa2", signature = c("mae"),
function(mae, ...) standardGeneric("mofa2")
)

#' @rdname create_mofa_from_mae
#' @export
setMethod("mofa2", signature = c(mae = "MultiAssayExperiment"),
function(mae, assay.types = rep("counts", length(mae)),
groups = NULL, extract_metadata = FALSE, ...){
# Select assays of each experiment for MOFA
mae <- .select_assays(mae, assay.types)
# Create MOFA from selected experiments
mdl <- create_mofa_from_MultiAssayExperiment(
mae,
groups = groups,
extract_metadata = extract_metadata
)
# Make a list of arguments for MOFA
mofa_args <- list(
object = mdl,
data_options = .set_opts(get_default_data_options(mdl), ...),
model_options = .set_opts(get_default_model_options(mdl), ...),
training_options = .set_opts(get_default_training_options(mdl), ...),
mefisto_options = .set_opts(get_default_mefisto_options(mdl), ...)
)
# Add stochastic options if stochastic is turned on
if ( mofa_args[["training_options"]][["stochastic"]] ){
mofa_args[["stochastic_options"]] <- .set_opts(get_default_stochastic_options(mdl), ...)
}
# Prepare MOFA
prep_mdl <- do.call("prepare_mofa", mofa_args)
return(prep_mdl)
}
)

########################## HELP FUNCTIONS ##########################

# Select assays to be included in MOFA
#' @importFrom MultiAssayExperiment experiments
#' @importFrom SummarizedExperiment assay assays assayNames
.select_assays <- function(mae, assay.types) {
# Give corresponding experiment names to assay.types
names(assay.types) <- names(experiments(mae))
# For every experiment in MAE
for ( exp in names(experiments(mae)) ){
# Keep only selected assay.type from a given experiment
assays(mae[[exp]]) <- list(assay(mae[[exp]], assay.types[[exp]]))
# Update assay names
assayNames(mae[[exp]]) <- assay.types[[exp]]
}
return(mae)
}
Comment on lines +82 to +93

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can lead to an errors that might be hard for user to solve:

  1. MAE can have also other datatypes than SEs. The object just has to have rows and columns.
  2. What if the length of assay.types does not match with length of experiments?


# Combine custom options found in ... with default options
.set_opts <- function(default, ...) {
# For every option in a set (data, model, train, ...)
for ( opt in names(default) ){
# If that option is found among arguments
if ( opt %in% names(list(...)) ){
# Replace default with value specified in arguments
default[[opt]] <- list(...)[[opt]]
}
}
Comment on lines +97 to +104

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Came to my mind while resting, does this work to avoid loops?

user_options <- list(...)
set_options <- intersect(names(default), names(user_options))
default[set_options] <- user_options[set_options]

return(default)
}
4 changes: 2 additions & 2 deletions man/create_mofa_from_Seurat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions man/create_mofa_from_mae.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.