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      88      59      29      76       5       1       5     316      52
gene2     104     374       6      23       1       7       1       3     104
gene3     231     243       1     151      75      93       1      51       1
gene4      85       6       1     238     393      50       7      45       1
gene5      95       1       8       7       8      11      52     332      82
gene6       1      27       4      88       1     140      10      47       1
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1      173       14      496      356       53       71        2      195
gene2        5       97        1      181       98      139       11       35
gene3        2       11        1       68      352       63       11       13
gene4        2       24       83        9       14       55      322        3
gene5        3       19      237      278        1        3       98       66
gene6      336       85        2       16      105      388       96      257
      sample18 sample19 sample20
gene1       38        9      102
gene2      741       46      332
gene3      209       83       24
gene4      245       29        2
gene5      224        3        5
gene6      166      277       74

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 20.43057 -0.1724116788  0.87827995  0.5001326    2
sample2 46.03175  0.0243048940 -0.13275538 -1.3997528    2
sample3 50.23663 -0.0001535236 -0.66098183 -0.4139497    2
sample4 43.58145 -0.4691252360 -0.07641307 -0.9515732    0
sample5 79.56687 -2.6383135539 -0.21832630 -0.1268177    1
sample6 52.78352  1.0867948512 -0.39693743 -0.5377321    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  116.1983   1.00005 0.1758578  0.674973  0.888122   238.698   245.669
gene2   93.9918   1.00011 0.0443517  0.833449  0.905923   230.774   237.744
gene3   71.5139   1.00006 0.0593034  0.807703  0.905923   217.063   224.033
gene4   71.5176   1.00006 0.3407305  0.559454  0.847658   205.150   212.120
gene5   83.4670   1.00006 0.0584695  0.809055  0.905923   221.815   228.785
gene6  108.0406   1.00018 0.5959644  0.440142  0.720443   235.606   242.576

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  116.1983  0.507738  0.375361  1.352664 0.17616290 0.5554333   238.698
gene2   93.9918 -0.428628  0.409724 -1.046138 0.29549742 0.6543103   230.774
gene3   71.5139 -0.803994  0.362808 -2.216030 0.02668946 0.1906390   217.063
gene4   71.5176 -1.002984  0.352478 -2.845524 0.00443385 0.0554231   205.150
gene5   83.4670 -0.427596  0.424741 -1.006721 0.31406895 0.6543103   221.815
gene6  108.0406  0.202912  0.398469  0.509228 0.61059238 0.8208329   235.606
            BIC
      <numeric>
gene1   245.669
gene2   237.744
gene3   224.033
gene4   212.120
gene5   228.785
gene6   242.576

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  116.1983 -1.146756  0.836543 -1.3708270  0.170429  0.655496   238.698
gene2   93.9918  0.647494  0.912663  0.7094556  0.478042  0.839010   230.774
gene3   71.5139 -0.069360  0.796764 -0.0870522  0.930630  0.997733   217.063
gene4   71.5176 -0.695986  0.742880 -0.9368760  0.348822  0.824082   205.150
gene5   83.4670 -0.353683  0.944703 -0.3743852  0.708118  0.973802   221.815
gene6  108.0406  0.800512  0.888494  0.9009767  0.367601  0.824082   235.606
            BIC
      <numeric>
gene1   245.669
gene2   237.744
gene3   224.033
gene4   212.120
gene5   228.785
gene6   242.576

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>
gene41  120.8680   1.00012  11.15473 0.000839531 0.0419766   224.955   231.925
gene37   52.8471   1.00007   8.85421 0.002926677 0.0646522   177.585   184.555
gene48   73.8833   1.00007   8.22877 0.004125567 0.0646522   210.047   217.018
gene40   95.9986   1.00005   7.81871 0.005172174 0.0646522   202.359   209.329
gene10  206.4794   1.00013   7.12030 0.007625829 0.0762583   231.206   238.176
gene45  157.5003   1.00004   6.29149 0.012135047 0.1011254   241.130   248.100
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.3.0 RC (2023-04-13 r84266)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.6.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

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_3.4.2               BiocParallel_1.34.1        
 [3] NBAMSeq_1.16.0              SummarizedExperiment_1.30.1
 [5] Biobase_2.60.0              GenomicRanges_1.52.0       
 [7] GenomeInfoDb_1.36.0         IRanges_2.34.0             
 [9] S4Vectors_0.38.1            BiocGenerics_0.46.0        
[11] MatrixGenerics_1.12.0       matrixStats_0.63.0         

loaded via a namespace (and not attached):
 [1] KEGGREST_1.40.0         gtable_0.3.3            xfun_0.38              
 [4] bslib_0.4.2             lattice_0.21-8          vctrs_0.6.1            
 [7] tools_4.3.0             bitops_1.0-7            generics_0.1.3         
[10] parallel_4.3.0          RSQLite_2.3.1           AnnotationDbi_1.62.1   
[13] tibble_3.2.1            fansi_1.0.4             highr_0.10             
[16] blob_1.2.4              pkgconfig_2.0.3         Matrix_1.5-4           
[19] lifecycle_1.0.3         GenomeInfoDbData_1.2.10 farver_2.1.1           
[22] compiler_4.3.0          Biostrings_2.68.0       munsell_0.5.0          
[25] DESeq2_1.40.1           codetools_0.2-19        htmltools_0.5.5        
[28] sass_0.4.5              RCurl_1.98-1.12         yaml_2.3.7             
[31] pillar_1.9.0            crayon_1.5.2            jquerylib_0.1.4        
[34] DelayedArray_0.26.2     cachem_1.0.7            nlme_3.1-162           
[37] genefilter_1.82.1       tidyselect_1.2.0        locfit_1.5-9.7         
[40] digest_0.6.31           dplyr_1.1.1             labeling_0.4.2         
[43] splines_4.3.0           fastmap_1.1.1           grid_4.3.0             
[46] colorspace_2.1-0        cli_3.6.1               magrittr_2.0.3         
[49] S4Arrays_1.0.1          survival_3.5-5          XML_3.99-0.14          
[52] utf8_1.2.3              withr_2.5.0             scales_1.2.1           
[55] bit64_4.0.5             rmarkdown_2.21          XVector_0.40.0         
[58] httr_1.4.5              bit_4.0.5               png_0.1-8              
[61] memoise_2.0.1           evaluate_0.20           knitr_1.42             
[64] mgcv_1.8-42             rlang_1.1.0             Rcpp_1.0.10            
[67] DBI_1.1.3               xtable_1.8-4            glue_1.6.2             
[70] annotate_1.78.0         jsonlite_1.8.4          R6_2.5.1               
[73] zlibbioc_1.46.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–78.