# mastR: Simplified Customized Design For Differential Expression Analysis

# Introduction

Simplified Customized Design For Differential Expression Analysis

`mastR` provides a simplified customized contrast design for differential expression analysis, which can help users handle the complex experimental design and data structure in one simple function call.

The function `process_data()` in `mastR` allows users to pass a customized contrast matrix to the function, which can give users more flexibility.

# Installation

`mastR` R package can be installed from Bioconductor or GitHub.

The most updated version of `mastR` is hosted on GitHub and can be installed using `devtools::install_github()` function provided by devtools.

    # if (!requireNamespace("devtools", quietly = TRUE)) {
    #   install.packages("devtools")
    # }
    # if (!requireNamespace("mastR", quietly = TRUE)) {
    #   devtools::install_github("DavisLaboratory/mastR")
    # }

    if (!requireNamespace("BiocManager", quietly = TRUE)) {
      install.packages("BiocManager")
    }
    if (!requireNamespace("mastR", quietly = TRUE)) {
      BiocManager::install("mastR")
    }
    packages <- c(
      "BiocStyle",
      "clusterProfiler",
      "ComplexHeatmap",
      "depmap",
      "enrichplot",
      "ggrepel",
      "Glimma",
      "gridExtra",
      "jsonlite",
      "knitr",
      "rmarkdown",
      "RobustRankAggreg",
      "rvest",
      "singscore",
      "UpSetR"
    )
    for (i in packages) {
      if (!requireNamespace(i, quietly = TRUE)) {
        install.packages(i)
      }
      if (!requireNamespace(i, quietly = TRUE)) {
        BiocManager::install(i)
      }
    }

    library(mastR)
    library(edgeR)
    library(ggplot2)
    library(GSEABase)

# Example

Here we use the example data `im_data_6` from GSE60424 (Download using `GEOquery::getGEO()`), consisting of immune cells from healthy individuals.

`im_data_6` is a `eSet` object, containing RNA-seq TMM normalized counts data of 6 sorted immune cell types each with 4 samples. More details in `?mastR::im_data_6`.

    data("im_data_6")
    im_data_6
    #> ExpressionSet (storageMode: lockedEnvironment)
    #> assayData: 50045 features, 24 samples
    #>   element names: exprs
    #> protocolData: none
    #> phenoData
    #>   sampleNames: GSM1479438 GSM1479439 ... GSM1479525 (24 total)
    #>   varLabels: title geo_accession ... years since diagnosis:ch1 (66
    #>     total)
    #>   varMetadata: labelDescription
    #> featureData: none
    #> experimentData: use 'experimentData(object)'
    #>   pubMedIds: 25314013
    #> Annotation: GPL15456

## 1. Customized contrast matrix

The customized contrast matrix can be created using `makeContrasts()` function from limma package.

Users can create the customized contrast matrix manually by specifying the contrast names and the levels of the groups.

    ## DE of NK vs B and B vs T
    con_mat <- makeContrasts(
      'NK-CD4' = 'NK - CD4',
      'NK-T' = 'NK - (CD4 + CD8)/2',
      levels = levels(factor(make.names(im_data_6$`celltype:ch1`)))
    )
    con_mat
    #>              Contrasts
    #> Levels        NK-CD4 NK-T
    #>   B.cells          0  0.0
    #>   CD4             -1 -0.5
    #>   CD8              0 -0.5
    #>   Monocytes        0  0.0
    #>   Neutrophils      0  0.0
    #>   NK               1  1.0

However, it is important to note that the levels of the groups should be consistent with the levels of the groups in the expression matrix. Otherwise, the contrast matrix will not be correct and the analysis will stop with an error.

So it is recommended and safer to create the customized contrast matrix from the design matrix generated from `process_data()` function.

What we need to do is to first process the data using `process_data()` function with random target group, then extract the design matrix from the `proc_data` object.

Next, we can create the customized contrast matrix from the design matrix.

    proc_data <- mastR::process_data(
      im_data_6,
      group_col = 'celltype:ch1',
      target_group = 'NK',
      summary = FALSE,
      gene_id = "ENSEMBL" ## rownames of im_data_6 is ENSEMBL ID
    )
    #> calcNormFactors has been renamed to normLibSizes
    con_mat2 <- makeContrasts(
      'NK-CD4' = 'NK - CD4',
      'NK-T' = 'NK - (CD4 + CD8)/2',
      levels = proc_data$vfit$design
    )
    con_mat2
    #>              Contrasts
    #> Levels        NK-CD4 NK-T
    #>   B.cells          0  0.0
    #>   CD4             -1 -0.5
    #>   CD8              0 -0.5
    #>   Monocytes        0  0.0
    #>   Neutrophils      0  0.0
    #>   NK               1  1.0

    identical(con_mat, con_mat2)
    #> [1] TRUE

## 2. Process data

Then, we can use the `process_data()` function to obtain the DE results with the customized contrast design.

At this point, the DE analysis is performed based on the customized contrast design, regardless of the target group.

    proc_data <- mastR::process_data(
      im_data_6,
      group_col = 'celltype:ch1',
      target_group = 'NK',
      contrast_mat = con_mat, ## specify contrast of NK vs B and B vs T
      summary = TRUE,
      gene_id = "ENSEMBL" ## rownames of im_data_6 is ENSEMBL ID
    )
    #> calcNormFactors has been renamed to normLibSizes
    #>        NK-CD4 NK-T
    #> Down     2697 2673
    #> NotSig   4980 4941
    #> Up       2734 2797

    ## plot mean-var
    mastR::plot_mean_var(proc_data)

## 3. Results

The DE results are stored in the `proc_data` object and can be easily accessed via `proc_data$tfit`.

    ## contrast names
    colnames(proc_data$tfit)
    #> [1] "NK-CD4" "NK-T"

    ## DE results for 'NK-B' contrast
    na.omit(limma::topTreat(
      proc_data$tfit,
      coef = 1, # or 'NK-B' for the first contrast
      number = Inf # get all DE results
    )) |> head()
    #>                     logFC  AveExpr         t      P.Value    adj.P.Val
    #> ENSG00000137078 -5.862090 1.745241 -27.64230 1.490757e-18 8.621116e-15
    #> ENSG00000229164 -5.277424 3.816770 -27.04422 2.378348e-18 8.621116e-15
    #> ENSG00000167286 -5.686564 2.309187 -26.82976 2.818723e-18 8.621116e-15
    #> ENSG00000172673 -6.208731 1.783905 -26.59978 3.386801e-18 8.621116e-15
    #> ENSG00000160185 -6.267757 1.707258 -26.35026 4.140388e-18 8.621116e-15
    #> ENSG00000135083  5.970905 1.559721  25.52403 8.158444e-18 1.187977e-14

Of course, users can also use the `get_de_table()` function to easily get all DE result tables for all contrasts on a single call.

    ## DE results for all contrasts
    DE_table <- mastR::get_de_table(
      im_data_6,
      group_col = 'celltype:ch1',
      target_group = 'NK',
      contrast_mat = con_mat, ## specify contrast of NK vs B and B vs T
      summary = TRUE,
      gene_id = "ENSEMBL" ## rownames of im_data_6 is ENSEMBL ID
    )
    #> calcNormFactors has been renamed to normLibSizes
    #>        NK-CD4 NK-T
    #> Down     2697 2673
    #> NotSig   4980 4941
    #> Up       2734 2797
    names(DE_table)
    #> [1] "NK-CD4" "NK-T"

    head(DE_table[[1]])
    #>                     logFC  AveExpr         t      P.Value    adj.P.Val
    #> ENSG00000137078 -5.862090 1.745241 -27.64230 1.490757e-18 8.621116e-15
    #> ENSG00000229164 -5.277424 3.816770 -27.04422 2.378348e-18 8.621116e-15
    #> ENSG00000167286 -5.686564 2.309187 -26.82976 2.818723e-18 8.621116e-15
    #> ENSG00000172673 -6.208731 1.783905 -26.59978 3.386801e-18 8.621116e-15
    #> ENSG00000160185 -6.267757 1.707258 -26.35026 4.140388e-18 8.621116e-15
    #> ENSG00000135083  5.970905 1.559721  25.52403 8.158444e-18 1.187977e-14

## Visualization

Users can use the `glimmaMDS()`, `glimmaMA()`, and `glimmaVolcano()` functions from Glimma package to visualize the data and DE results interactively.

    ## MDS plot
    Glimma::glimmaMDS(proc_data)

    ## MA plot
    Glimma::glimmaMA(proc_data$tfit, dge = proc_data)

    ## volcano plot
    Glimma::glimmaVolcano(proc_data$tfit, dge = proc_data)

# Session Info

    sessionInfo()
    #> R version 4.6.0 (2026-04-24)
    #> Platform: x86_64-pc-linux-gnu
    #> Running under: Ubuntu 24.04.4 LTS
    #>
    #> Matrix products: default
    #> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
    #> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
    #>
    #> locale:
    #>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
    #>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
    #>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
    #>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
    #>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
    #> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
    #>
    #> time zone: Etc/UTC
    #> tzcode source: system (glibc)
    #>
    #> attached base packages:
    #> [1] stats4    stats     graphics  grDevices utils     datasets  methods
    #> [8] base
    #>
    #> other attached packages:
    #>  [1] GSEABase_1.75.0      graph_1.91.0         annotate_1.91.0
    #>  [4] XML_3.99-0.23        AnnotationDbi_1.75.0 IRanges_2.47.1
    #>  [7] S4Vectors_0.51.2     Biobase_2.73.1       BiocGenerics_0.59.2
    #> [10] generics_0.1.4       ggplot2_4.0.3        edgeR_4.11.0
    #> [13] limma_3.69.1         mastR_1.13.1         BiocStyle_2.41.0
    #>
    #> loaded via a namespace (and not attached):
    #>  [1] DBI_1.3.0                   rlang_1.2.0
    #>  [3] magrittr_2.0.5              otel_0.2.0
    #>  [5] matrixStats_1.5.0           compiler_4.6.0
    #>  [7] RSQLite_3.52.0              png_0.1-9
    #>  [9] vctrs_0.7.3                 pkgconfig_2.0.3
    #> [11] crayon_1.5.3                fastmap_1.2.0
    #> [13] backports_1.5.1             XVector_0.53.0
    #> [15] labeling_0.4.3              rmarkdown_2.31
    #> [17] purrr_1.2.2                 bit_4.6.0
    #> [19] xfun_0.57                   cachem_1.1.0
    #> [21] jsonlite_2.0.0              blob_1.3.0
    #> [23] DelayedArray_0.39.2         broom_1.0.13
    #> [25] parallel_4.6.0              R6_2.6.1
    #> [27] bslib_0.11.0                RColorBrewer_1.1-3
    #> [29] parallelly_1.47.0           car_3.1-5
    #> [31] GenomicRanges_1.65.0        jquerylib_0.1.4
    #> [33] Rcpp_1.1.1-1.1              Seqinfo_1.3.0
    #> [35] SummarizedExperiment_1.43.0 knitr_1.51
    #> [37] future.apply_1.20.2         Matrix_1.7-5
    #> [39] tidyselect_1.2.1            abind_1.4-8
    #> [41] yaml_2.3.12                 codetools_0.2-20
    #> [43] listenv_0.10.1              lattice_0.22-9
    #> [45] tibble_3.3.1                withr_3.0.2
    #> [47] KEGGREST_1.53.0             S7_0.2.2
    #> [49] evaluate_1.0.5              future_1.70.0
    #> [51] Biostrings_2.81.1           pillar_1.11.1
    #> [53] BiocManager_1.30.27         ggpubr_0.6.3
    #> [55] MatrixGenerics_1.25.0       carData_3.0-6
    #> [57] sp_2.2-1                    scales_1.4.0
    #> [59] globals_0.19.1              xtable_1.8-8
    #> [61] glue_1.8.1                  maketools_1.3.2
    #> [63] tools_4.6.0                 sys_3.4.3
    #> [65] locfit_1.5-9.12             ggsignif_0.6.4
    #> [67] buildtools_1.0.0            dotCall64_1.2
    #> [69] grid_4.6.0                  tidyr_1.3.2
    #> [71] SingleCellExperiment_1.35.1 patchwork_1.3.2
    #> [73] Formula_1.2-5               msigdb_1.21.0
    #> [75] cli_3.6.6                   spam_2.11-3
    #> [77] S4Arrays_1.13.0             dplyr_1.2.1
    #> [79] gtable_0.3.6                rstatix_0.7.3
    #> [81] sass_0.4.10                 digest_0.6.39
    #> [83] progressr_0.19.0            SparseArray_1.13.2
    #> [85] org.Hs.eg.db_3.23.1         SeuratObject_5.4.0
    #> [87] farver_2.1.2                memoise_2.0.1
    #> [89] htmltools_0.5.9             lifecycle_1.0.5
    #> [91] httr_1.4.8                  statmod_1.5.2
    #> [93] bit64_4.8.0