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       9       1      81     131      31       7      11       5     302
gene2      32      31      63      60       2       8     226      20     202
gene3     247       2      41       7     324     124      68       6      29
gene4       3      38     146       1      95       3       2     262      16
gene5     296      23     135     702       1      22     533      28      20
gene6     167      56      87      30       1      40      88       3      33
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1       85       20      108        2      131       11       16       57
gene2       91        1       97      436      648      322      220        5
gene3       14        3      266       32       26        1       61       14
gene4       43       63      768       30        1        1        5       94
gene5       86       45      343       15        1      534      241      395
gene6       20       41       11      110       87        6       37        3
      sample18 sample19 sample20
gene1      178        2        3
gene2       80      258       81
gene3       42        1        5
gene4      324       29      111
gene5       16      147        1
gene6      506      343       21

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 47.98536 -0.09039582 -0.3578216 -0.22005341    2
sample2 65.20287 -0.08481904  1.6631906  0.26550284    0
sample3 48.15160  0.40395238  0.2217212  2.52502103    2
sample4 66.55341  0.92871328 -0.9479591  0.09557784    2
sample5 44.17860 -0.17564905 -0.6235148 -0.44547915    0
sample6 67.47988  1.03898110  0.0544212  0.45801537    1

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   46.2180   1.00003 1.20448757 0.2724488  0.524079   202.890   209.860
gene2  130.4580   1.00006 1.20416808 0.2725211  0.524079   246.765   253.736
gene3   53.0048   1.00004 0.34130120 0.5591231  0.838439   210.278   217.248
gene4   73.2682   1.00017 4.77984471 0.0288025  0.238816   215.438   222.409
gene5  154.4572   1.00005 0.00181812 0.9664340  0.986157   244.213   251.183
gene6   66.7085   1.00007 1.22771202 0.2678691  0.524079   220.776   227.746

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   46.2180 -0.436800  0.522725 -0.835620 0.4033685  0.720301   202.890
gene2  130.4580 -0.651483  0.558134 -1.167251 0.2431090  0.578831   246.765
gene3   53.0048  0.968998  0.581780  1.665575 0.0957981  0.349580   210.278
gene4   73.2682 -0.393427  0.588047 -0.669041 0.5034696  0.778532   215.438
gene5  154.4572  1.107906  0.569464  1.945526 0.0517117  0.349580   244.213
gene6   66.7085  0.202817  0.520304  0.389805 0.6966805  0.955774   220.776
            BIC
      <numeric>
gene1   209.860
gene2   253.736
gene3   217.248
gene4   222.409
gene5   251.183
gene6   227.746

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   46.2180  0.3078278  0.828264  0.371654 0.7101503  0.848438   202.890
gene2  130.4580  0.0979007  0.875806  0.111783 0.9109951  0.972725   246.765
gene3   53.0048 -0.3391768  0.920809 -0.368347 0.7126148  0.848438   210.278
gene4   73.2682 -1.1497883  0.925671 -1.242114 0.2141945  0.563670   215.438
gene5  154.4572  0.9708217  0.897839  1.081287 0.2795694  0.610707   244.213
gene6   66.7085 -1.3525525  0.815905 -1.657733 0.0973714  0.336967   220.776
            BIC
      <numeric>
gene1   209.860
gene2   253.736
gene3   217.248
gene4   222.409
gene5   251.183
gene6   227.746

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>
gene23  107.8084   2.33154  14.47304 0.00220059  0.110030   204.846   213.142
gene48   94.8511   1.00004   7.84224 0.00510501  0.127625   219.179   226.149
gene42   54.5328   1.00003   6.30730 0.01202726  0.200454   204.805   211.775
gene50   98.0570   1.00004   5.30283 0.02129483  0.238816   211.780   218.750
gene4    73.2682   1.00017   4.77984 0.02880253  0.238816   215.438   222.409
gene12  114.6674   1.00010   4.65861 0.03091398  0.238816   220.279   227.249
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 Under development (unstable) (2025-11-04 r88984)
Platform: aarch64-apple-darwin20
Running under: macOS Ventura 13.7.8

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.6-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1

locale:
[1] C/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_4.0.1               BiocParallel_1.45.0        
 [3] NBAMSeq_1.27.0              SummarizedExperiment_1.41.0
 [5] Biobase_2.71.0              GenomicRanges_1.63.1       
 [7] Seqinfo_1.1.0               IRanges_2.45.0             
 [9] S4Vectors_0.49.0            BiocGenerics_0.57.0        
[11] generics_0.1.4              MatrixGenerics_1.23.0      
[13] matrixStats_1.5.0          

loaded via a namespace (and not attached):
 [1] KEGGREST_1.51.1      gtable_0.3.6         xfun_0.54           
 [4] bslib_0.9.0          lattice_0.22-7       vctrs_0.6.5         
 [7] tools_4.6.0          parallel_4.6.0       tibble_3.3.0        
[10] AnnotationDbi_1.73.0 RSQLite_2.4.5        blob_1.2.4          
[13] pkgconfig_2.0.3      Matrix_1.7-4         RColorBrewer_1.1-3  
[16] S7_0.2.1             lifecycle_1.0.4      compiler_4.6.0      
[19] farver_2.1.2         Biostrings_2.79.2    DESeq2_1.51.6       
[22] codetools_0.2-20     htmltools_0.5.9      sass_0.4.10         
[25] yaml_2.3.11          crayon_1.5.3         pillar_1.11.1       
[28] jquerylib_0.1.4      DelayedArray_0.37.0  cachem_1.1.0        
[31] abind_1.4-8          nlme_3.1-168         genefilter_1.93.0   
[34] tidyselect_1.2.1     locfit_1.5-9.12      digest_0.6.39       
[37] dplyr_1.1.4          labeling_0.4.3       splines_4.6.0       
[40] fastmap_1.2.0        grid_4.6.0           cli_3.6.5           
[43] SparseArray_1.11.8   magrittr_2.0.4       S4Arrays_1.11.1     
[46] survival_3.8-3       dichromat_2.0-0.1    XML_3.99-0.20       
[49] withr_3.0.2          scales_1.4.0         bit64_4.6.0-1       
[52] rmarkdown_2.30       XVector_0.51.0       httr_1.4.7          
[55] bit_4.6.0            png_0.1-8            memoise_2.0.1       
[58] evaluate_1.0.5       knitr_1.50           mgcv_1.9-4          
[61] rlang_1.1.6          Rcpp_1.1.0.8.1       xtable_1.8-4        
[64] glue_1.8.0           DBI_1.2.3            annotate_1.89.0     
[67] jsonlite_2.0.0       R6_2.6.1            

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.