Installation

To install and load NBAMSeq

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("NBAMSeq")
library(NBAMSeq)

Introduction

High-throughput sequencing experiments followed by differential expression analysis is a widely used approach to detect genomic biomarkers. A fundamental step in differential expression analysis is to model the association between gene counts and covariates of interest. NBAMSeq is a flexible statistical model based on the generalized additive model and allows for information sharing across genes in variance estimation. Specifically, we model the logarithm of mean gene counts as sums of smooth functions with the smoothing parameters and coefficients estimated simultaneously by a nested iteration. The variance is estimated by the Bayesian shrinkage approach to fully exploit the information across all genes.

The workflow of NBAMSeq contains three main steps:

Here we illustrate each of these steps respectively.

Data input

Users are expected to provide three parts of input, i.e. countData, colData, and design.

countData is a matrix of gene counts generated by RNASeq experiments.

## An example of countData
n = 50  ## n stands for number of genes
m = 20   ## m stands for sample size
countData = matrix(rnbinom(n*m, mu=100, size=1/3), ncol = m) + 1
mode(countData) = "integer"
colnames(countData) = paste0("sample", 1:m)
rownames(countData) = paste0("gene", 1:n)
head(countData)
      sample1 sample2 sample3 sample4 sample5 sample6 sample7 sample8 sample9
gene1       1      51      71     362      10     280      12       5     102
gene2     133       3       6       6       4     389      22       3     841
gene3      45       1       5      74       5      11      26      73     531
gene4     119     167      29       3      30      45      13     104      47
gene5       1       4       5      73     224       1     259      80       1
gene6       1       1     204       1      11       3      53      47      17
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1       26      123      679       30       11        7      285        2
gene2        2       16       27       63        1      235        2       94
gene3        3       19      716       18      196       34        3       34
gene4        1      335       18      618        3      226       66        7
gene5        1        7       10       47        1       20       49       21
gene6       22        3       63        3      113        1       14      109
      sample18 sample19 sample20
gene1      455        2      181
gene2        1       39        9
gene3       29        3       64
gene4      443        1        2
gene5       86        1        1
gene6      114        1       84

colData is a data frame which contains the covariates of samples. The sample order in colData should match the sample order in countData.

## An example of colData
pheno = runif(m, 20, 80)
var1 = rnorm(m)
var2 = rnorm(m)
var3 = rnorm(m)
var4 = as.factor(sample(c(0,1,2), m, replace = TRUE))
colData = data.frame(pheno = pheno, var1 = var1, var2 = var2,
    var3 = var3, var4 = var4)
rownames(colData) = paste0("sample", 1:m)
head(colData)
           pheno        var1        var2       var3 var4
sample1 59.94230 -1.21513278  0.31225257  1.7655730    1
sample2 69.74864 -0.05990721 -2.97020958  1.3165137    1
sample3 28.25309  0.32975385  1.12298295  1.1505615    2
sample4 76.60283  1.26397338 -0.76528875  0.3954747    2
sample5 37.73285  0.77192120  0.57382761 -0.2823225    1
sample6 61.01322  1.45660380  0.09180152 -0.9023484    2

design is a formula which specifies how to model the samples. Compared with other packages performing DE analysis including DESeq2 (Love, Huber, and Anders 2014), edgeR (Robinson, McCarthy, and Smyth 2010), NBPSeq (Di et al. 2015) and BBSeq (Zhou, Xia, and Wright 2011), NBAMSeq supports the nonlinear model of covariates via mgcv (Wood and Wood 2015). To indicate the nonlinear covariate in the model, users are expected to use s(variable_name) in the design formula. In our example, if we would like to model pheno as a nonlinear covariate, the design formula should be:

design = ~ s(pheno) + var1 + var2 + var3 + var4

Several notes should be made regarding the design formula:

We then construct the NBAMSeqDataSet using countData, colData, and design:

gsd = NBAMSeqDataSet(countData = countData, colData = colData, design = design)
gsd
class: NBAMSeqDataSet 
dim: 50 20 
metadata(1): fitted
assays(1): counts
rownames(50): gene1 gene2 ... gene49 gene50
rowData names(0):
colnames(20): sample1 sample2 ... sample19 sample20
colData names(5): pheno var1 var2 var3 var4

Differential expression analysis

Differential expression analysis can be performed by NBAMSeq function:

gsd = NBAMSeq(gsd)

Several other arguments in NBAMSeq function are available for users to customize the analysis.

library(BiocParallel)
gsd = NBAMSeq(gsd, parallel = TRUE)

Pulling out DE results

Results of DE analysis can be pulled out by results function. For continuous covariates, the name argument should be specified indicating the covariate of interest. For nonlinear continuous covariates, base mean, effective degrees of freedom (edf), test statistics, p-value, and adjusted p-value will be returned.

res1 = results(gsd, name = "pheno")
head(res1)
DataFrame with 6 rows and 7 columns
       baseMean       edf      stat      pvalue       padj       AIC       BIC
      <numeric> <numeric> <numeric>   <numeric>  <numeric> <numeric> <numeric>
gene1  105.7944   1.00009  0.437679 5.08325e-01 0.98122535   234.631   241.602
gene2   92.5166   1.00008 10.869777 9.78578e-04 0.01630963   201.088   208.059
gene3   80.5895   1.00005  0.236796 6.26606e-01 0.98405227   221.754   228.724
gene4   82.5612   1.06027  1.002956 3.06072e-01 0.85395218   227.554   234.584
gene5   34.7947   1.00009  1.041759 3.07423e-01 0.85395218   186.523   193.494
gene6   41.3509   1.00004 17.317126 3.25637e-05 0.00162818   182.082   189.052

For linear continuous covariates, base mean, estimated coefficient, standard error, test statistics, p-value, and adjusted p-value will be returned.

res2 = results(gsd, name = "var1")
head(res2)
DataFrame with 6 rows and 8 columns
       baseMean       coef        SE      stat    pvalue      padj       AIC
      <numeric>  <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene1  105.7944 -0.0568675  0.509811 -0.111546 0.9111832  0.969344   234.631
gene2   92.5166 -1.2970850  0.563274 -2.302759 0.0212924  0.328890   201.088
gene3   80.5895 -0.1034140  0.541578 -0.190949 0.8485654  0.942850   221.754
gene4   82.5612 -0.5241137  0.527756 -0.993099 0.3206618  0.598992   227.554
gene5   34.7947 -1.0993198  0.551042 -1.994984 0.0460447  0.328890   186.523
gene6   41.3509  0.3770941  0.438912  0.859156 0.3902545  0.672853   182.082
            BIC
      <numeric>
gene1   241.602
gene2   208.059
gene3   228.724
gene4   234.584
gene5   193.494
gene6   189.052

For discrete covariates, the contrast argument should be specified. e.g.  contrast = c("var4", "2", "0") means comparing level 2 vs. level 0 in var4.

res3 = results(gsd, contrast = c("var4", "2", "0"))
head(res3)
DataFrame with 6 rows and 8 columns
       baseMean      coef        SE      stat    pvalue      padj       AIC
      <numeric> <numeric> <numeric> <numeric> <numeric> <numeric> <numeric>
gene1  105.7944  1.002982  0.861975  1.163586  0.244592  0.835002   234.631
gene2   92.5166  0.343397  0.899021  0.381967  0.702486  0.900623   201.088
gene3   80.5895 -0.206383  0.918207 -0.224767  0.822160  0.913231   221.754
gene4   82.5612  0.725237  0.891686  0.813333  0.416027  0.835002   227.554
gene5   34.7947  0.599717  0.918926  0.652628  0.513996  0.835002   186.523
gene6   41.3509  0.384633  0.754208  0.509982  0.610064  0.835002   182.082
            BIC
      <numeric>
gene1   241.602
gene2   208.059
gene3   228.724
gene4   234.584
gene5   193.494
gene6   189.052

Visualization

We suggest two approaches to visualize the nonlinear associations. The first approach is to plot the smooth components of a fitted negative binomial additive model by plot.gam function in mgcv (Wood and Wood 2015). This can be done by calling makeplot function and passing in NBAMSeqDataSet object. Users are expected to provide the phenotype of interest in phenoname argument and gene of interest in genename argument.

## assuming we are interested in the nonlinear relationship between gene10's 
## expression and "pheno"
makeplot(gsd, phenoname = "pheno", genename = "gene10", main = "gene10")

In addition, to explore the nonlinear association of covariates, it is also instructive to look at log normalized counts vs. variable scatter plot. Below we show how to produce such plot.

## here we explore the most significant nonlinear association
res1 = res1[order(res1$pvalue),]
topgene = rownames(res1)[1]  
sf = getsf(gsd)  ## get the estimated size factors
## divide raw count by size factors to obtain normalized counts
countnorm = t(t(countData)/sf) 
head(res1)
DataFrame with 6 rows and 7 columns
        baseMean       edf      stat      pvalue       padj       AIC       BIC
       <numeric> <numeric> <numeric>   <numeric>  <numeric> <numeric> <numeric>
gene6    41.3509   1.00004  17.31713 3.25637e-05 0.00162818   182.082   189.052
gene38   93.8438   1.00006  10.99034 9.16494e-04 0.01630963   223.655   230.625
gene2    92.5166   1.00008  10.86978 9.78578e-04 0.01630963   201.088   208.059
gene8    83.2773   1.07598   8.32338 4.43489e-03 0.05543616   198.576   205.622
gene48  116.9931   1.00006   3.64090 5.63918e-02 0.50923495   238.118   245.088
gene29   71.6065   1.00021   3.50774 6.11082e-02 0.50923495   213.177   220.147
library(ggplot2)
setTitle = topgene
df = data.frame(pheno = pheno, logcount = log2(countnorm[topgene,]+1))
ggplot(df, aes(x=pheno, y=logcount))+geom_point(shape=19,size=1)+
    geom_smooth(method='loess')+xlab("pheno")+ylab("log(normcount + 1)")+
    annotate("text", x = max(df$pheno)-5, y = max(df$logcount)-1, 
    label = paste0("edf: ", signif(res1[topgene,"edf"],digits = 4)))+
    ggtitle(setTitle)+
    theme(text = element_text(size=10), plot.title = element_text(hjust = 0.5))

Session info

sessionInfo()
R version 4.4.0 alpha (2024-03-27 r86216)
Platform: aarch64-apple-darwin20
Running under: macOS Ventura 13.6.5

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] ggplot2_3.5.0               BiocParallel_1.38.0        
 [3] NBAMSeq_1.20.0              SummarizedExperiment_1.34.0
 [5] Biobase_2.64.0              GenomicRanges_1.56.0       
 [7] GenomeInfoDb_1.40.0         IRanges_2.38.0             
 [9] S4Vectors_0.42.0            BiocGenerics_0.50.0        
[11] MatrixGenerics_1.16.0       matrixStats_1.2.0          

loaded via a namespace (and not attached):
 [1] KEGGREST_1.44.0         gtable_0.3.4            xfun_0.43              
 [4] bslib_0.6.2             lattice_0.22-6          vctrs_0.6.5            
 [7] tools_4.4.0             generics_0.1.3          parallel_4.4.0         
[10] RSQLite_2.3.5           tibble_3.2.1            fansi_1.0.6            
[13] AnnotationDbi_1.66.0    highr_0.10              blob_1.2.4             
[16] pkgconfig_2.0.3         Matrix_1.7-0            lifecycle_1.0.4        
[19] GenomeInfoDbData_1.2.12 farver_2.1.1            compiler_4.4.0         
[22] Biostrings_2.72.0       munsell_0.5.0           DESeq2_1.44.0          
[25] codetools_0.2-19        htmltools_0.5.8         sass_0.4.9             
[28] yaml_2.3.8              pillar_1.9.0            crayon_1.5.2           
[31] jquerylib_0.1.4         DelayedArray_0.30.0     cachem_1.0.8           
[34] abind_1.4-5             nlme_3.1-164            genefilter_1.86.0      
[37] tidyselect_1.2.1        locfit_1.5-9.9          digest_0.6.35          
[40] dplyr_1.1.4             labeling_0.4.3          splines_4.4.0          
[43] fastmap_1.1.1           grid_4.4.0              colorspace_2.1-0       
[46] cli_3.6.2               SparseArray_1.4.0       magrittr_2.0.3         
[49] S4Arrays_1.4.0          survival_3.5-8          XML_3.99-0.16.1        
[52] utf8_1.2.4              withr_3.0.0             scales_1.3.0           
[55] UCSC.utils_1.0.0        bit64_4.0.5             rmarkdown_2.26         
[58] XVector_0.44.0          httr_1.4.7              bit_4.0.5              
[61] png_0.1-8               memoise_2.0.1           evaluate_0.23          
[64] knitr_1.45              mgcv_1.9-1              rlang_1.1.3            
[67] Rcpp_1.0.12             DBI_1.2.2               xtable_1.8-4           
[70] glue_1.7.0              annotate_1.82.0         jsonlite_1.8.8         
[73] R6_2.5.1                zlibbioc_1.50.0        

References

Di, Y, DW Schafer, JS Cumbie, and JH Chang. 2015. “NBPSeq: Negative Binomial Models for RNA-Sequencing Data.” R Package Version 0.3. 0, URL Http://CRAN. R-Project. Org/Package= NBPSeq.
Love, Michael I, Wolfgang Huber, and Simon Anders. 2014. “Moderated Estimation of Fold Change and Dispersion for RNA-Seq Data with DESeq2.” Genome Biology 15 (12): 550.
Robinson, Mark D, Davis J McCarthy, and Gordon K Smyth. 2010. “edgeR: A Bioconductor Package for Differential Expression Analysis of Digital Gene Expression Data.” Bioinformatics 26 (1): 139–40.
Wood, Simon, and Maintainer Simon Wood. 2015. “Package ’Mgcv’.” R Package Version 1: 29.
Zhou, Yi-Hui, Kai Xia, and Fred A Wright. 2011. “A Powerful and Flexible Approach to the Analysis of RNA Sequence Count Data.” Bioinformatics 27 (19): 2672–78.