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     356       1     352       1       3      14      78      83      53
gene2       2      88      13       2       3      23      14     190      54
gene3      28       1     115      57       5      22      96     149      41
gene4      18       1     172     108       1     784     223       3     201
gene5     168       3     144     746      81      12       1      52     245
gene6       1     218       6      47     179      33      78       1     204
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1        6      233      405       87        1        1        1      103
gene2        2        2        4        3      214      495      123        4
gene3       21       21       53       62       31      127       62       22
gene4       26        7       12       41      712      723        1        2
gene5      406        9       80        1      129      154      166       14
gene6        7        1        3       48       85       55       85        1
      sample18 sample19 sample20
gene1       46      180       50
gene2        1       71        4
gene3       83       25       11
gene4       32        1       52
gene5      200      171        1
gene6        1       19      274

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 25.61004 -1.2327874 -1.1179287 -1.1580542    1
sample2 35.30209 -0.6711554 -0.1106435 -0.2417916    0
sample3 42.30052  1.2168777  1.5992099 -0.4410217    1
sample4 51.85550 -1.2359325 -0.6960942 -0.7816302    0
sample5 59.57958 -1.8242780 -1.4091554 -0.2033634    2
sample6 56.38511 -1.3036284  1.0710648 -0.5117473    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   75.2480   1.00007  0.432404 0.5108561  0.715722   218.960   225.930
gene2   45.6657   1.00005  3.273728 0.0704154  0.352077   191.696   198.666
gene3   38.3879   1.00006  0.019897 0.8879465  0.935294   210.955   217.925
gene4  135.3228   1.00003  0.674892 0.4113666  0.706940   227.510   234.480
gene5  139.8446   1.00004  0.661426 0.4160780  0.706940   247.846   254.816
gene6   55.0088   1.00003  0.286720 0.5923781  0.800511   202.111   209.081

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   75.2480 0.01298663  0.606487 0.0214129  0.982916  0.992163   218.960
gene2   45.6657 0.64361879  0.571305 1.1265763  0.259922  0.618861   191.696
gene3   38.3879 0.00502848  0.448278 0.0112173  0.991050  0.992163   210.955
gene4  135.3228 0.22464124  0.678014 0.3313223  0.740401  0.974212   227.510
gene5  139.8446 0.47037886  0.665285 0.7070335  0.479546  0.773461   247.846
gene6   55.0088 0.12476811  0.561725 0.2221162  0.824223  0.981218   202.111
            BIC
      <numeric>
gene1   225.930
gene2   198.666
gene3   217.925
gene4   234.480
gene5   254.816
gene6   209.081

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   75.2480 -0.920109  0.966590 -0.951913 0.3411413  0.715663   218.960
gene2   45.6657  1.114578  0.915378  1.217615 0.2233705  0.601426   191.696
gene3   38.3879 -0.323839  0.714045 -0.453527 0.6501693  0.810101   210.955
gene4  135.3228  1.883769  1.079273  1.745406 0.0809142  0.367792   227.510
gene5  139.8446 -0.874070  1.059950 -0.824633 0.4095799  0.729042   247.846
gene6   55.0088  1.573402  0.893412  1.761116 0.0782188  0.367792   202.111
            BIC
      <numeric>
gene1   225.930
gene2   198.666
gene3   217.925
gene4   234.480
gene5   254.816
gene6   209.081

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>
gene8   107.4649   1.00004   8.11541 0.00439033  0.126907   220.794   227.764
gene10   91.0299   1.00010   7.61008 0.00580716  0.126907   218.334   225.304
gene23   30.7800   1.00005   7.01897 0.00806670  0.126907   189.221   196.191
gene34   68.2838   1.00008   6.51989 0.01066951  0.126907   217.614   224.584
gene45   25.1818   1.00003   5.99319 0.01436468  0.126907   167.253   174.223
gene32   79.2640   1.00011   5.89123 0.01522880  0.126907   213.481   220.451
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.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows Server 2022 x64 (build 20348)

Matrix products: default


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.1               BiocParallel_1.40.0        
 [3] NBAMSeq_1.22.0              SummarizedExperiment_1.36.0
 [5] Biobase_2.66.0              GenomicRanges_1.58.0       
 [7] GenomeInfoDb_1.42.0         IRanges_2.40.0             
 [9] S4Vectors_0.44.0            BiocGenerics_0.52.0        
[11] MatrixGenerics_1.18.0       matrixStats_1.4.1          

loaded via a namespace (and not attached):
 [1] KEGGREST_1.46.0         gtable_0.3.6            xfun_0.48              
 [4] bslib_0.8.0             lattice_0.22-6          vctrs_0.6.5            
 [7] tools_4.4.1             generics_0.1.3          parallel_4.4.1         
[10] RSQLite_2.3.7           tibble_3.2.1            fansi_1.0.6            
[13] AnnotationDbi_1.68.0    highr_0.11              blob_1.2.4             
[16] pkgconfig_2.0.3         Matrix_1.7-1            lifecycle_1.0.4        
[19] GenomeInfoDbData_1.2.13 farver_2.1.2            compiler_4.4.1         
[22] Biostrings_2.74.0       munsell_0.5.1           DESeq2_1.46.0          
[25] codetools_0.2-20        snow_0.4-4              htmltools_0.5.8.1      
[28] sass_0.4.9              yaml_2.3.10             pillar_1.9.0           
[31] crayon_1.5.3            jquerylib_0.1.4         DelayedArray_0.32.0    
[34] cachem_1.1.0            abind_1.4-8             nlme_3.1-166           
[37] genefilter_1.88.0       tidyselect_1.2.1        locfit_1.5-9.10        
[40] digest_0.6.37           dplyr_1.1.4             labeling_0.4.3         
[43] splines_4.4.1           fastmap_1.2.0           grid_4.4.1             
[46] colorspace_2.1-1        cli_3.6.3               SparseArray_1.6.0      
[49] magrittr_2.0.3          S4Arrays_1.6.0          survival_3.7-0         
[52] XML_3.99-0.17           utf8_1.2.4              withr_3.0.2            
[55] scales_1.3.0            UCSC.utils_1.2.0        bit64_4.5.2            
[58] rmarkdown_2.28          XVector_0.46.0          httr_1.4.7             
[61] bit_4.5.0               png_0.1-8               memoise_2.0.1          
[64] evaluate_1.0.1          knitr_1.48              mgcv_1.9-1             
[67] rlang_1.1.4             Rcpp_1.0.13             DBI_1.2.3              
[70] xtable_1.8-4            glue_1.8.0              annotate_1.84.0        
[73] jsonlite_1.8.9          R6_2.5.1                zlibbioc_1.52.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–8.