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     123       2     236       2       1      99      22     161     163
gene2     354     624     149      40       1      33     167       1       4
gene3       3       1      93       7      18      29      16     172       1
gene4       1      80       2     875       9       1     241      27      89
gene5      74      26      12       1      21      96      43      13       2
gene6      23       1      39      60      24    1384      12     193      72
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1        1       67       15       65      158       28       35       27
gene2        2        1      162        5      623        5        4       57
gene3        2       11       63       13       86       18      411        1
gene4       71        1       25      116        1      100      716       31
gene5      235      227        3       38        4       34       12       78
gene6       31       52       73       43       28       29        2        2
      sample18 sample19 sample20
gene1      150       57        1
gene2        1       42        1
gene3        6      140      105
gene4        6      448       40
gene5       87       36       30
gene6        5      363        7

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 53.81336 -0.4770509 -0.98788768 -0.7714960    0
sample2 73.68071  1.2712794  0.67611258  0.9138409    1
sample3 42.47431  1.2173849 -0.44440377  1.1367614    1
sample4 25.84396 -0.7299088 -0.00725924 -0.4265589    0
sample5 47.95385 -1.0185717 -0.85649827  1.1020512    0
sample6 41.15608 -0.3161889  2.57930047  0.6528291    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   64.9990   1.00015  0.5924989 0.441504968 0.6553397   224.906   231.876
gene2   94.1406   1.00008  0.0330549 0.855939402 0.9169568   218.582   225.552
gene3   58.2458   1.00013 13.1724492 0.000284349 0.0142174   195.771   202.741
gene4  120.4412   1.00005  5.9450122 0.014761899 0.1034176   223.342   230.313
gene5   48.7193   1.00004  1.5112270 0.218955373 0.4878139   201.062   208.033
gene6   93.5872   1.09292  7.2249822 0.015578291 0.1034176   222.053   229.115

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   64.9990  0.0859825  0.393663  0.218416 0.827104895 0.8439846   224.906
gene2   94.1406  0.1529427  0.462348  0.330796 0.740798739 0.8277851   218.582
gene3   58.2458  1.2746771  0.344625  3.698736 0.000216676 0.0108338   195.771
gene4  120.4412 -0.1501855  0.381505 -0.393666 0.693827566 0.8277851   223.342
gene5   48.7193  0.0850536  0.300147  0.283373 0.776891074 0.8277851   201.062
gene6   93.5872  0.2787329  0.372776  0.747723 0.454627439 0.7889796   222.053
            BIC
      <numeric>
gene1   231.876
gene2   225.552
gene3   202.741
gene4   230.313
gene5   208.033
gene6   229.115

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   64.9990  0.915450   1.32121  0.692886 0.4883812  0.652613   224.906
gene2   94.1406 -0.812237   1.55263 -0.523137 0.6008792  0.698697   218.582
gene3   58.2458 -1.553265   1.16175 -1.337007 0.1812205  0.396201   195.771
gene4  120.4412 -0.869939   1.27778 -0.680819 0.4959860  0.652613   223.342
gene5   48.7193 -2.402295   1.02748 -2.338040 0.0193852  0.161543   201.062
gene6   93.5872 -1.279696   1.23851 -1.033251 0.3014862  0.486268   222.053
            BIC
      <numeric>
gene1   231.876
gene2   225.552
gene3   202.741
gene4   230.313
gene5   208.033
gene6   229.115

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>
gene3    58.2458   1.00013  13.17245 0.000284349 0.0142174   195.771   202.741
gene33   94.2656   1.00013   8.80384 0.003007112 0.0568303   231.376   238.346
gene10   53.6530   1.00012   8.57587 0.003409821 0.0568303   205.095   212.065
gene21   59.8853   1.00033   7.95061 0.004820049 0.0602506   201.905   208.876
gene24   71.5148   1.00006   6.08529 0.013637091 0.1034176   213.243   220.213
gene4   120.4412   1.00005   5.94501 0.014761899 0.1034176   223.342   230.313
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.0 RC (2026-04-17 r89917)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.4 LTS

Matrix products: default
BLAS:   /home/biocbuild/bbs-3.24-bioc/R/lib/libRblas.so 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0  LAPACK version 3.12.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB              LC_COLLATE=C              
 [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: America/New_York
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.47.0        
 [3] NBAMSeq_1.29.0              SummarizedExperiment_1.43.0
 [5] Biobase_2.73.0              GenomicRanges_1.65.0       
 [7] Seqinfo_1.3.0               IRanges_2.47.0             
 [9] S4Vectors_0.51.0            BiocGenerics_0.59.0        
[11] generics_0.1.4              MatrixGenerics_1.25.0      
[13] matrixStats_1.5.0          

loaded via a namespace (and not attached):
 [1] KEGGREST_1.53.0      gtable_0.3.6         xfun_0.57           
 [4] bslib_0.10.0         lattice_0.22-9       vctrs_0.7.3         
 [7] tools_4.6.0          parallel_4.6.0       tibble_3.3.1        
[10] AnnotationDbi_1.75.0 RSQLite_2.4.6        blob_1.3.0          
[13] pkgconfig_2.0.3      Matrix_1.7-5         RColorBrewer_1.1-3  
[16] S7_0.2.2             lifecycle_1.0.5      compiler_4.6.0      
[19] farver_2.1.2         Biostrings_2.81.0    DESeq2_1.53.0       
[22] codetools_0.2-20     htmltools_0.5.9      sass_0.4.10         
[25] yaml_2.3.12          crayon_1.5.3         pillar_1.11.1       
[28] jquerylib_0.1.4      DelayedArray_0.39.0  cachem_1.1.0        
[31] abind_1.4-8          nlme_3.1-169         genefilter_1.95.0   
[34] tidyselect_1.2.1     locfit_1.5-9.12      digest_0.6.39       
[37] dplyr_1.2.1          labeling_0.4.3       splines_4.6.0       
[40] fastmap_1.2.0        grid_4.6.0           cli_3.6.6           
[43] SparseArray_1.13.0   magrittr_2.0.5       S4Arrays_1.13.0     
[46] survival_3.8-6       dichromat_2.0-0.1    XML_3.99-0.23       
[49] withr_3.0.2          scales_1.4.0         bit64_4.8.0         
[52] rmarkdown_2.31       XVector_0.53.0       httr_1.4.8          
[55] bit_4.6.0            otel_0.2.0           png_0.1-9           
[58] memoise_2.0.1        evaluate_1.0.5       knitr_1.51          
[61] mgcv_1.9-4           rlang_1.2.0          Rcpp_1.1.1-1.1      
[64] xtable_1.8-8         glue_1.8.1           DBI_1.3.0           
[67] annotate_1.91.0      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–8.