From dd2aa171386dc513b72c478f550ec66ffdd44920 Mon Sep 17 00:00:00 2001 From: Tuomas Borman Date: Tue, 24 Mar 2026 13:44:53 +0200 Subject: [PATCH] up --- inst/pages/example_solutions.qmd | 94 +++++++++++++++++++--------- inst/pages/multiassay_ordination.qmd | 13 ++-- 2 files changed, 74 insertions(+), 33 deletions(-) diff --git a/inst/pages/example_solutions.qmd b/inst/pages/example_solutions.qmd index a68be250..00ad1ca3 100644 --- a/inst/pages/example_solutions.qmd +++ b/inst/pages/example_solutions.qmd @@ -412,26 +412,6 @@ These example solutions are related to exercises in [@sec-mediation]. #| code-summary: "Show the solution" ``` -## Network learning & analysis - -These example solutions are related to exercises in [@sec-network-learning]. - -```{r} -#| label: network1 -#| code-fold: true -#| code-summary: "Show the solution" -``` - -## Network comparison - -These example solutions are related to exercises in [@sec-network-comparison]. - -```{r} -#| label: network2 -#| code-fold: true -#| code-summary: "Show the solution" -``` - ## Cross-association These example solutions are related to exercises in [@sec-cross-correlation]. @@ -440,6 +420,37 @@ These example solutions are related to exercises in [@sec-cross-correlation]. #| label: cross-corr #| code-fold: true #| code-summary: "Show the solution" + +# Load the example dataset +library(mia) +data(HintikkaXOData) +mae <- HintikkaXOData + +# Agglomerate microbiome data +mae[[1]] <- agglomerateByPrevalence(mae[[1]], rank = "Phylum") + +# Apply CLR +mae[[1]] <- transformAssay(mae[[1]], assay.type = "counts", method = "rclr") + +# Apply relative and log10 +mae[[2]] <- transformAssay(mae[[2]], assay.type = "nmr", method = "relabundance") +mae[[2]] <- transformAssay(mae[[2]], assay.type = "relabundance", method = "log10", pseudocount = TRUE) + +# Cross correlates data sets +res <- getCrossAssociation( + mae, + experiment1 = 1, + experiment2 = 2, + assay.type1 = "rclr", + assay.type2 = "log10", + method = "spearman", + mode = "matrix" +) + +# Create a heatmap +library(ComplexHeatmap) +p <- Heatmap(res) +p ``` ## Ordination-based multiassay analysis @@ -451,17 +462,44 @@ These example solutions are related to exercises in #| label: mofa #| code-fold: true #| code-summary: "Show the solution" -``` -## Multi-omics prediction and classification +# Load the example dataset +library(mia) +data(HintikkaXOData) +mae <- HintikkaXOData -These example solutions are related to exercises in -[@sec-multi-omics-integration]. +# Select only microbiome and metabolites +mae <- mae[, , c(1, 2)] -```{r} -#| label: integratedlearner -#| code-fold: true -#| code-summary: "Show the solution" +# Agglomerate microbiome data +mae[[1]] <- agglomerateByPrevalence(mae[[1]], rank = "Phylum") + +# Apply CLR +mae[[1]] <- transformAssay(mae[[1]], assay.type = "counts", method = "rclr") + +# Apply relative and log10 transformations +mae[[2]] <- transformAssay(mae[[2]], assay.type = "nmr", method = "relabundance") +mae[[2]] <- transformAssay(mae[[2]], assay.type = "relabundance", method = "log10", pseudocount = TRUE) + +# Scale features +mae[[1]] <- transformAssay(mae[[1]], assay.type = "rclr", method = "standardize", MARGIN = "rows") +mae[[2]] <- transformAssay(mae[[2]], assay.type = "log10", method = "standardize", MARGIN = "rows") + +# Removing the assays no longer needed +assays(mae[[1]]) <- assays(mae[[1]])["standardize"] +assays(mae[[2]]) <- assays(mae[[2]])["standardize"] + +# Create a MOFA object +library(MOFA2) +model <- create_mofa_from_MultiAssayExperiment(mae) + +# Run MOFA +model <- prepare_mofa(object = model) +model <- run_mofa(model, use_basilisk = TRUE) + +# Visualize variance explained +p <- plot_variance_explained(model, x = "view", y = "factor") +p ``` ## Machine learning diff --git a/inst/pages/multiassay_ordination.qmd b/inst/pages/multiassay_ordination.qmd index 7bfae43e..a80e6b6d 100644 --- a/inst/pages/multiassay_ordination.qmd +++ b/inst/pages/multiassay_ordination.qmd @@ -224,17 +224,20 @@ interpret MOFA results. 4. Apply relative transformation and log10 transformation to other metabolite or other omic. -5. Create a MOFA2 object from the `MultiAssayExperiment`. +5. Standardize each feature (within each dataset) by centering to mean 0 and +scaling to unit variance so that all variables are on a comparable scale. -6. Run MOFA. +6. Create a MOFA2 object from the `MultiAssayExperiment`. -7. Visualize the explained variance. How much variance is explained in total, +7. Run MOFA. + +8. Visualize the explained variance. How much variance is explained in total, and which omic accounts the most variance? -8. For each omic, visualize the features with the largest weights. Can you +9. For each omic, visualize the features with the largest weights. Can you identify features that are co-varying? -9. If you completed exercises in [@sec-cross-correlation], did you get similar +10. If you completed exercises in [@sec-cross-correlation], did you get similar results? Do the co-varying features also exhibited correlation? Useful functions: