Appendix E — Example solutions for exercises

E.1 Introduction

These example solutions are related to exercises in Chapter 1 and Chapter 2.

E.2 Data containers

These example solutions are related to exercises in Chapter 3.

E.3 Import

These example solutions are related to exercises in Chapter 4.

E.4 Convert & export

These example solutions are related to exercises in Chapter 5.

Show the solution
# Load example dataset
library(mia)
data("GlobalPatterns", package = "mia")
tse <- GlobalPatterns

# Convert to Phyloseq and reconvert to a new Dataset
ps <- convertToPhyloseq(tse)
tse_new <- convertFromPhyloseq(ps)

# Save the reconverted file to a RDS file and read it
saveRDS(tse_new, "tse.rds")
tse_read <- readRDS("tse.rds")

# Converting TreeSE to long format and writing it in a CSV file
lf <- meltSE(tse_read)
write.csv(lf, "LongFormat_tse.csv")

E.5 Taxonomic information

These example solutions are related to exercises in Chapter 6.

E.6 Data wrangling

These example solutions are related to exercises in Chapter 7.

Show the solution
# Load first example dataset
library(mia)
data("enterotype", package = "mia")
tse <- enterotype

# Visualize column metadata
colData(tse) |> head()

# Create variable 'group' containing 3 random classifications for each entry
tse[["group"]] <- sample(
    c("group_1", "group_2", "group_3"),
    ncol(tse),
    replace = TRUE
)

# Plot how groups are distributed
library(miaViz)
plotBarplot(tse, col.var = "group")

# Split groups into two different tse objects and outputs a list
tse_list <- splitOn(tse, by = 2, group = "group")

# Count numbers of entries in the group
tse_list[[1]][["group"]] |> table()
tse_list[[2]][["group"]] |> table()
tse_list[[3]][["group"]] |> table()

# Sum the abundances correspondent to each group and
# merge them back into a single tse object
tse_list <- lapply(tse_list, function(x){
    mat <- assay(x, "counts")
    sum_val <- sum(mat)
    x[["summed_value"]] <- sum_val
    return(x)
})
tse <- unsplitOn(tse_list)

# Load second example dataset and merges them
data("GlobalPatterns")
tse <- mergeSEs(tse, GlobalPatterns)
colData(tse)

E.7 Agglomeration

These example solutions are related to exercises in Chapter 11.

E.8 Transformation

These example solutions are related to exercises in Chapter 12.

E.9 Exploration & quality control

These example solutions are related to exercises in Chapter 9.

E.10 Community composition

These example solutions are related to exercises in Chapter 13.

E.11 Alpha Diversity

These example solutions are related to exercises in Chapter 14.

E.12 Community similarity

These exercises are related to exercises in Chapter 15.

E.13 Community typing

These example solutions are related to exercises in Chapter 16.

E.14 Differential abundance

These example solutions are related to exercises in Chapter 17.

E.15 Correlation

These example solutions are related to exercises in Chapter 18.

E.16 Mediation

These example solutions are related to exercises in Chapter 19.

E.17 Network learning & analysis

These example solutions are related to exercises in Chapter 20.

E.18 Network comparison

These example solutions are related to exercises in Chapter 21.

E.19 Cross-association

These example solutions are related to exercises in Chapter 23.

E.20 Ordination-based multiassay analysis

These example solutions are related to exercises in Chapter 24.

E.21 Multi-omics prediction and classification

These example solutions are related to exercises in Chapter 25.

E.22 Machine learning

These example solutions are related to exercises in Chapter 26.

Back to top