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      45       1     220       1      61      30       1       1     387
gene2      83     143      10     186     642      61      31     212     727
gene3     145       1     192     162       1      45     101      15     169
gene4     122      15       3     290       7     249     158     122       1
gene5       3     296       3      72      34      86       1       1      13
gene6     474      34      54     296      46       1      31      49       2
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1      174        1      133        8        1       39      859       13
gene2       44      275        1       74        2      790        3       96
gene3      223       44      749        2        7      920       26      298
gene4       28        5        1       16        1      197      610       32
gene5      674       39      151        1        4        2        4        1
gene6        1       87      121      106        2       53      189       72
      sample18 sample19 sample20
gene1        2        3        5
gene2        1       26       48
gene3       45      223       15
gene4        1        6        9
gene5       46        2        9
gene6        5      212       27

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 52.32611 -1.0816253  1.7897582 -0.2279812    2
sample2 26.71654  0.2840334  0.7357133 -0.9184919    1
sample3 41.70698  0.2527619 -0.3990051 -1.5988617    2
sample4 36.06898 -0.5359866  0.1256181 -0.2889754    2
sample5 68.87707 -1.0274084  1.4947898  2.2422929    2
sample6 71.59119  0.4194515 -0.3011543  1.7183602    0

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.3026   1.47159  1.961153 0.3244364  0.666099   195.735   203.175
gene2  109.7960   1.00004  0.800297 0.3710159  0.687066   239.903   246.873
gene3  139.5281   1.00011  0.379950 0.5375905  0.770866   248.794   255.765
gene4   54.6754   1.00010  2.558520 0.1097183  0.428936   205.954   212.925
gene5   60.7629   1.00001  6.182734 0.0129013  0.104895   188.366   195.336
gene6   86.4546   1.00026  2.679373 0.1016266  0.428936   223.071   230.042

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.3026  1.053961  0.657820  1.602202 0.1091109  0.418607   195.735
gene2  109.7960 -1.248590  0.594812 -2.099134 0.0358051  0.210423   239.903
gene3  139.5281  0.836127  0.650142  1.286070 0.1984188  0.620059   248.794
gene4   54.6754 -0.478043  0.614302 -0.778189 0.4364579  0.727430   205.954
gene5   60.7629  1.001980  0.639592  1.566593 0.1172099  0.418607   188.366
gene6   86.4546 -0.248882  0.574924 -0.432896 0.6650907  0.860347   223.071
            BIC
      <numeric>
gene1   203.175
gene2   246.873
gene3   255.765
gene4   212.925
gene5   195.336
gene6   230.042

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.3026 -1.150748  1.037248 -1.109424 2.67247e-01 0.61686378   195.735
gene2  109.7960 -1.611847  0.952326 -1.692538 9.05435e-02 0.30181159   239.903
gene3  139.5281  0.533674  1.046834  0.509798 6.10193e-01 0.80288517   248.794
gene4   54.6754 -2.999740  0.992400 -3.022714 2.50519e-03 0.03131484   205.954
gene5   60.7629 -1.937264  1.056612 -1.833468 6.67331e-02 0.23833234   188.366
gene6   86.4546  3.956240  0.975214  4.056790 4.97518e-05 0.00248759   223.071
            BIC
      <numeric>
gene1   203.175
gene2   246.873
gene3   255.765
gene4   212.925
gene5   195.336
gene6   230.042

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>
gene10   69.5151   1.00012  15.02895 0.000105344 0.00526719   202.812   209.782
gene34  109.1564   1.00004  11.76638 0.000603248 0.01508121   226.169   233.139
gene19   55.0658   1.00003   7.52477 0.006086488 0.07675576   207.604   214.574
gene24   92.0654   1.00005   7.50905 0.006140461 0.07675576   202.551   209.521
gene5    60.7629   1.00001   6.18273 0.012901253 0.10489484   188.366   195.336
gene36   65.4285   1.00013   6.17750 0.012940028 0.10489484   192.927   199.897
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.5.0 RC (2025-04-04 r88126 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows Server 2022 x64 (build 20348)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=C                          
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

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.2               BiocParallel_1.42.0        
 [3] NBAMSeq_1.24.1              SummarizedExperiment_1.38.1
 [5] Biobase_2.68.0              GenomicRanges_1.60.0       
 [7] GenomeInfoDb_1.44.0         IRanges_2.42.0             
 [9] S4Vectors_0.46.0            BiocGenerics_0.54.0        
[11] generics_0.1.3              MatrixGenerics_1.20.0      
[13] matrixStats_1.5.0          

loaded via a namespace (and not attached):
 [1] KEGGREST_1.48.0         gtable_0.3.6            xfun_0.52              
 [4] bslib_0.9.0             lattice_0.22-7          vctrs_0.6.5            
 [7] tools_4.5.0             parallel_4.5.0          tibble_3.2.1           
[10] AnnotationDbi_1.70.0    RSQLite_2.3.11          blob_1.2.4             
[13] pkgconfig_2.0.3         Matrix_1.7-3            RColorBrewer_1.1-3     
[16] lifecycle_1.0.4         GenomeInfoDbData_1.2.14 compiler_4.5.0         
[19] farver_2.1.2            Biostrings_2.76.0       DESeq2_1.48.1          
[22] codetools_0.2-20        snow_0.4-4              htmltools_0.5.8.1      
[25] sass_0.4.10             yaml_2.3.10             pillar_1.10.2          
[28] crayon_1.5.3            jquerylib_0.1.4         DelayedArray_0.34.1    
[31] cachem_1.1.0            abind_1.4-8             nlme_3.1-168           
[34] genefilter_1.90.0       tidyselect_1.2.1        locfit_1.5-9.12        
[37] digest_0.6.37           dplyr_1.1.4             labeling_0.4.3         
[40] splines_4.5.0           fastmap_1.2.0           grid_4.5.0             
[43] cli_3.6.5               SparseArray_1.8.0       magrittr_2.0.3         
[46] S4Arrays_1.8.0          survival_3.8-3          dichromat_2.0-0.1      
[49] XML_3.99-0.18           withr_3.0.2             scales_1.4.0           
[52] UCSC.utils_1.4.0        bit64_4.6.0-1           rmarkdown_2.29         
[55] XVector_0.48.0          httr_1.4.7              bit_4.6.0              
[58] png_0.1-8               memoise_2.0.1           evaluate_1.0.3         
[61] knitr_1.50              mgcv_1.9-3              rlang_1.1.6            
[64] Rcpp_1.0.14             xtable_1.8-4            glue_1.8.0             
[67] DBI_1.2.3               annotate_1.86.0         jsonlite_2.0.0         
[70] 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.