ClearScatterplot MAE Example

Example Usage of ClearScatterplot with MAE

This vignette demonstrates how to use MultiModalGraphics to create a ClearScatterplot visualization.

# Load required libraries
library(MultiAssayExperiment)
## Loading required package: SummarizedExperiment
## Loading required package: MatrixGenerics
## Loading required package: matrixStats
## 
## Attaching package: 'MatrixGenerics'
## The following objects are masked from 'package:matrixStats':
## 
##     colAlls, colAnyNAs, colAnys, colAvgsPerRowSet, colCollapse,
##     colCounts, colCummaxs, colCummins, colCumprods, colCumsums,
##     colDiffs, colIQRDiffs, colIQRs, colLogSumExps, colMadDiffs,
##     colMads, colMaxs, colMeans2, colMedians, colMins, colOrderStats,
##     colProds, colQuantiles, colRanges, colRanks, colSdDiffs, colSds,
##     colSums2, colTabulates, colVarDiffs, colVars, colWeightedMads,
##     colWeightedMeans, colWeightedMedians, colWeightedSds,
##     colWeightedVars, rowAlls, rowAnyNAs, rowAnys, rowAvgsPerColSet,
##     rowCollapse, rowCounts, rowCummaxs, rowCummins, rowCumprods,
##     rowCumsums, rowDiffs, rowIQRDiffs, rowIQRs, rowLogSumExps,
##     rowMadDiffs, rowMads, rowMaxs, rowMeans2, rowMedians, rowMins,
##     rowOrderStats, rowProds, rowQuantiles, rowRanges, rowRanks,
##     rowSdDiffs, rowSds, rowSums2, rowTabulates, rowVarDiffs, rowVars,
##     rowWeightedMads, rowWeightedMeans, rowWeightedMedians,
##     rowWeightedSds, rowWeightedVars
## Loading required package: GenomicRanges
## Loading required package: stats4
## Loading required package: BiocGenerics
## Loading required package: generics
## 
## Attaching package: 'generics'
## The following objects are masked from 'package:base':
## 
##     as.difftime, as.factor, as.ordered, intersect, is.element, setdiff,
##     setequal, union
## 
## Attaching package: 'BiocGenerics'
## The following objects are masked from 'package:stats':
## 
##     IQR, mad, sd, var, xtabs
## The following objects are masked from 'package:base':
## 
##     Filter, Find, Map, Position, Reduce, anyDuplicated, aperm, append,
##     as.data.frame, basename, cbind, colnames, dirname, do.call,
##     duplicated, eval, evalq, get, grep, grepl, is.unsorted, lapply,
##     mapply, match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
##     rank, rbind, rownames, sapply, saveRDS, table, tapply, unique,
##     unsplit, which.max, which.min
## Loading required package: S4Vectors
## 
## Attaching package: 'S4Vectors'
## The following object is masked from 'package:utils':
## 
##     findMatches
## The following objects are masked from 'package:base':
## 
##     I, expand.grid, unname
## Loading required package: IRanges
## Loading required package: GenomeInfoDb
## Loading required package: Biobase
## Welcome to Bioconductor
## 
##     Vignettes contain introductory material; view with
##     'browseVignettes()'. To cite Bioconductor, see
##     'citation("Biobase")', and for packages 'citation("pkgname")'.
## 
## Attaching package: 'Biobase'
## The following object is masked from 'package:MatrixGenerics':
## 
##     rowMedians
## The following objects are masked from 'package:matrixStats':
## 
##     anyMissing, rowMedians
library(MultiModalGraphics)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:Biobase':
## 
##     combine
## The following objects are masked from 'package:GenomicRanges':
## 
##     intersect, setdiff, union
## The following object is masked from 'package:GenomeInfoDb':
## 
##     intersect
## The following objects are masked from 'package:IRanges':
## 
##     collapse, desc, intersect, setdiff, slice, union
## The following objects are masked from 'package:S4Vectors':
## 
##     first, intersect, rename, setdiff, setequal, union
## The following objects are masked from 'package:BiocGenerics':
## 
##     combine, intersect, setdiff, setequal, union
## The following object is masked from 'package:generics':
## 
##     explain
## The following object is masked from 'package:matrixStats':
## 
##     count
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Parameters
num_genes <- 1000
num_samples <- 1000  # Total number of samples
conditions <- list(
  Condition1 = c("Cond1A", "Cond1B"),
  Condition2 = c("Cond2A", "Cond2B"),
  Condition3 = c("Cond3A", "Cond3B")
)

# Function to generate expression data
generate_expression_data <- function(num_genes, num_samples) {
  matrix(rnorm(num_genes * num_samples, mean = 6, sd = 2), nrow = num_genes, ncol = num_samples)
}

# Initialize lists
expression_data_list <- list()
metadata_list <- list()

# Generate samples for each combination of conditions
all_combinations <- expand.grid(conditions)
num_combinations <- nrow(all_combinations)
samples_per_combination <- num_samples %/% num_combinations
remaining_samples <- num_samples %% num_combinations

for (i in 1:num_combinations) {
  combination <- all_combinations[i, ]
  combination_name <- paste(combination, collapse = "_")

  # Calculate the number of samples for this combination
  num_samples_for_this_combination <- samples_per_combination + ifelse(i <= remaining_samples, 1, 0)

  expression_data <- generate_expression_data(num_genes, num_samples_for_this_combination)
  colnames(expression_data) <- paste0(combination_name, "_Sample", 1:num_samples_for_this_combination)
  rownames(expression_data) <- paste0("Gene", 1:num_genes)

  sample_metadata <- data.frame(
    SampleID = colnames(expression_data),
    Group = rep(c("Control", "Treatment"), length.out = num_samples_for_this_combination),
    stringsAsFactors = FALSE,
    TimePoint = rep(c("T1","T1" ,"T2",  "T2"), length.out = num_samples_for_this_combination),  # Example time points
    SampleType = rep(c("Tissue", "Organ"), length.out = num_samples_for_this_combination)  # Example sample types
  )

  for (cond in names(combination)) {
    sample_metadata[[cond]] <- combination[[cond]]
  }

  expression_data_list[[combination_name]] <- expression_data
  metadata_list[[combination_name]] <- sample_metadata
}

# Combine metadata into a single data frame
combined_metadata <- do.call(rbind, metadata_list)
rownames(combined_metadata) <- combined_metadata$SampleID

# Select only the relevant columns for the final metadata
final_metadata <- combined_metadata[, c("SampleID", "TimePoint", "SampleType", "Group", "Condition1", "Condition2", "Condition3")]

# Check the size of the final metadata
print(dim(final_metadata))  # Should be 1000 rows
## [1] 1000    7
head(final_metadata)
##                    SampleID TimePoint SampleType     Group Condition1
## 1_1_1_Sample1 1_1_1_Sample1        T1     Tissue   Control     Cond1A
## 1_1_1_Sample2 1_1_1_Sample2        T1      Organ Treatment     Cond1A
## 1_1_1_Sample3 1_1_1_Sample3        T2     Tissue   Control     Cond1A
## 1_1_1_Sample4 1_1_1_Sample4        T2      Organ Treatment     Cond1A
## 1_1_1_Sample5 1_1_1_Sample5        T1     Tissue   Control     Cond1A
## 1_1_1_Sample6 1_1_1_Sample6        T1      Organ Treatment     Cond1A
##               Condition2 Condition3
## 1_1_1_Sample1     Cond2A     Cond3A
## 1_1_1_Sample2     Cond2A     Cond3A
## 1_1_1_Sample3     Cond2A     Cond3A
## 1_1_1_Sample4     Cond2A     Cond3A
## 1_1_1_Sample5     Cond2A     Cond3A
## 1_1_1_Sample6     Cond2A     Cond3A
# Create the MultiAssayExperiment object
mae <- MultiAssayExperiment::MultiAssayExperiment(experiments = expression_data_list, colData = final_metadata)

# Check available assay names
print(names(MultiAssayExperiment::experiments(mae)))
## [1] "1_1_1" "2_1_1" "1_2_1" "2_2_1" "1_1_2" "2_1_2" "1_2_2" "2_2_2"
# Use a valid assay name (example: select the first one)
assayNames <- names(MultiAssayExperiment::experiments(mae))
assayName <- assayNames[1]
print(paste("Using assay name:", assayName))
## [1] "Using assay name: 1_1_1"
# Assuming ClearScatterplot_MAE is already defined in your script
scatterplotObject <- ClearScatterplot_MAE(
  mae = mae,
  assayName = assayName,
  timepoint = "TimePoint",
  sampleType = "SampleType"
)
## Removing intercept from test coefficients
# Assuming createPlot is already defined in your script
# Generate and display the plot
scatterplotObject <- createPlot(
  scatterplotObject,
  color1 = "cornflowerblue",
  color2 = "grey",
  color3 = "indianred",
  highLog2fc = 0.585,
  lowLog2fc = -0.585,
  negLog10pValue = 1.301
)
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
# Show the plot
print(scatterplotObject@plot)