NBAMSeq: Negative Binomial Additive Model for RNA-Seq Data

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:

  • Step 1: Data input using NBAMSeqDataSet;

  • Step 2: Differential expression (DE) analysis using NBAMSeq function;

  • Step 3: Pulling out DE results using results function.

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      34       7      19      10     313      27     122      28       4
gene2      85       1       1     291       1     142      17      15      12
gene3       5      62       2       3     149     154       4       4     264
gene4      35       6     205      37      59      10      61       2     148
gene5       2      30      95      27     226      41      94     160     100
gene6     111      20     385       3      53      34       9       1     347
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1      212        1      126      335       39        1      363      180
gene2        8        1        1        1       84        1        1       62
gene3       74       74      174       27        1       35       43       23
gene4       67        1       20        1        2       37       12        1
gene5       53      128      319      136      366        6        1       20
gene6       17       23       74        2        1        1       30       63
      sample18 sample19 sample20
gene1      167       20      240
gene2        9      235      184
gene3        1       66        1
gene4        5       34       20
gene5        8        1      332
gene6        3        2       52

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 28.92191 -0.8507693 -0.16092644  1.2432171    2
sample2 73.44578  0.9233076 -0.24329573  1.1044623    0
sample3 20.28537 -0.2496096  2.97167277  0.5553854    1
sample4 33.25581  0.1087562  0.68536813 -0.6115827    0
sample5 47.05413  0.6288326  1.71220901  0.1420902    2
sample6 66.42307 -1.0239032  0.08275015 -0.2482983    1

design is a formula which specifies how to model the samples. Compared with other packages performing DE analysis including DESeq2 (Love et al. 2014), edgeR (Robinson et al. 2010), NBPSeq (Di et al. 2015) and BBSeq (Zhou et al. 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:

  • multiple nonlinear covariates are supported, e.g. design = ~ s(pheno) + s(var1) + var2 + var3 + var4;

  • the nonlinear covariate cannot be a discrete variable, e.g.  design = ~ s(pheno) + var1 + var2 + var3 + s(var4) as var4 is a factor, and it makes no sense to model a factor as nonlinear;

  • at least one nonlinear covariate should be provided in design. If all covariates are assumed to have linear effect on gene count, use DESeq2 (Love et al. 2014), edgeR (Robinson et al. 2010), NBPSeq (Di et al. 2015) or BBSeq (Zhou et al. 2011) instead. e.g.  design = ~ pheno + var1 + var2 + var3 + var4 is not supported in NBAMSeq;

  • design matrix is not supported.

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.

  • gamma argument can be used to control the smoothness of the nonlinear function. Higher gamma means the nonlinear function will be more smooth. See the gamma argument of gam function in mgcv (Wood and Wood 2015) for details. Default gamma is 2.5;

  • fitlin is either TRUE or FALSE indicating whether linear model should be fitted after fitting the nonlinear model;

  • parallel is either TRUE or FALSE indicating whether parallel should be used. e.g. Run NBAMSeq with parallel = TRUE:

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   93.4595   1.00013  4.47669621 0.0343701 0.1562276   233.672   240.643
gene2   44.9589   1.00027 10.58525683 0.0011420 0.0285501   189.225   196.196
gene3   63.8758   1.00004  5.47480584 0.0192984 0.1329876   211.861   218.831
gene4   33.4229   1.00003  0.00385257 0.9507129 0.9968277   190.083   197.054
gene5  109.7819   1.00007  0.21841409 0.6403455 0.8463241   227.588   234.558
gene6   53.8805   1.11718  2.35883277 0.2371349 0.5457550   202.202   209.289

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   93.4595  0.1866049  0.397104  0.4699147 0.63841598 0.9672969   233.672
gene2   44.9589 -0.7260410  0.443084 -1.6386069 0.10129515 0.5387138   189.225
gene3   63.8758  0.0625218  0.448122  0.1395195 0.88903962 0.9712091   211.861
gene4   33.4229 -0.2236073  0.382839 -0.5840772 0.55916838 0.9017776   190.083
gene5  109.7819  1.2161503  0.375348  3.2400622 0.00119504 0.0298759   227.588
gene6   53.8805 -0.0144431  0.429768 -0.0336067 0.97319077 0.9731908   202.202
            BIC
      <numeric>
gene1   240.643
gene2   196.196
gene3   218.831
gene4   197.054
gene5   234.558
gene6   209.289

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   93.4595  0.762186  1.047066  0.727925 0.46665925 0.7777654   233.672
gene2   44.9589 -2.072096  1.209919 -1.712591 0.08678786 0.3091022   189.225
gene3   63.8758  3.224267  1.188908  2.711957 0.00668872 0.0751604   211.861
gene4   33.4229  0.430351  1.001857  0.429554 0.66752039 0.9020546   190.083
gene5  109.7819  0.977004  0.970842  1.006347 0.31424882 0.6614532   227.588
gene6   53.8805  2.704138  1.144129  2.363490 0.01810370 0.1131481   202.202
            BIC
      <numeric>
gene1   240.643
gene2   196.196
gene3   218.831
gene4   197.054
gene5   234.558
gene6   209.289

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>
gene38   96.8527   1.00005  11.54121 0.000680767 0.0285501   208.326   215.296
gene2    44.9589   1.00027  10.58526 0.001142005 0.0285501   189.225   196.196
gene28   48.6423   1.00004   6.06606 0.013781771 0.1329876   197.903   204.873
gene42   58.6681   1.00009   5.97568 0.014505238 0.1329876   213.527   220.497
gene13   68.7209   1.00007   5.56603 0.018318156 0.1329876   200.734   207.705
gene3    63.8758   1.00004   5.47481 0.019298434 0.1329876   211.861   218.831
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.6.1 (2026-06-24)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 26.04 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.32.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] ggplot2_4.0.3               BiocParallel_1.46.0        
 [3] NBAMSeq_1.28.0              SummarizedExperiment_1.42.0
 [5] Biobase_2.72.0              GenomicRanges_1.64.0       
 [7] Seqinfo_1.2.0               IRanges_2.46.0             
 [9] S4Vectors_0.50.1            BiocGenerics_0.58.1        
[11] generics_0.1.4              MatrixGenerics_1.24.0      
[13] matrixStats_1.5.0           rmarkdown_2.31             

loaded via a namespace (and not attached):
 [1] KEGGREST_1.52.2      gtable_0.3.6         xfun_0.60           
 [4] bslib_0.11.0         lattice_0.22-9       vctrs_0.7.3         
 [7] tools_4.6.1          parallel_4.6.1       AnnotationDbi_1.74.0
[10] RSQLite_3.53.3       blob_1.3.0           Matrix_1.7-6        
[13] RColorBrewer_1.1-3   S7_0.2.2             lifecycle_1.0.5     
[16] compiler_4.6.1       farver_2.1.2         Biostrings_2.80.1   
[19] DESeq2_1.52.0        codetools_0.2-20     htmltools_0.5.9     
[22] sys_3.4.3            buildtools_1.0.0     sass_0.4.10         
[25] yaml_2.3.12          crayon_1.5.3         jquerylib_0.1.4     
[28] DelayedArray_0.38.2  cachem_1.1.0         abind_1.4-8         
[31] nlme_3.1-170         genefilter_1.94.0    locfit_1.5-9.12     
[34] digest_0.6.39        labeling_0.4.3       splines_4.6.1       
[37] maketools_1.3.2      fastmap_1.2.0        grid_4.6.1          
[40] cli_3.6.6            SparseArray_1.12.2   S4Arrays_1.12.0     
[43] survival_3.8-9       XML_3.99-0.23        withr_3.0.3         
[46] scales_1.4.0         bit64_4.8.2          XVector_0.52.0      
[49] httr_1.4.8           bit_4.6.0            otel_0.2.0          
[52] png_0.1-9            memoise_2.0.1        evaluate_1.0.5      
[55] knitr_1.51           mgcv_1.9-4           rlang_1.3.0         
[58] Rcpp_1.1.2           xtable_1.8-8         glue_1.8.1          
[61] DBI_1.3.0            annotate_1.90.0      jsonlite_2.0.0      
[64] 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.