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      46      10     777      25      26       6       1      15     311
gene2       3       1     258     222      16      77     671      18     300
gene3     104       4     161       1       4      97     332       1     274
gene4       1     225       1       1       1      51       2       2       1
gene5     568      38       7     202      68     162      18      15      86
gene6       2       3       1      14     178       3      75       6       1
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1        3       43      452        1      139        1      285      117
gene2      107       53        1      115       47       23      119       14
gene3        1       19        2       43       21      176      112      142
gene4        2       93        2      461        1        1      429      602
gene5       79        7       19        1       28       67      293       39
gene6      276        1      163       15      503        1        5        1
      sample18 sample19 sample20
gene1      471        4       11
gene2        1      399      168
gene3        3       35       11
gene4       43      489       16
gene5        3      140        7
gene6       20       10       20

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 46.77770 -1.74233754 -1.8002320  0.48283260    2
sample2 63.79685  0.04285053  0.1526413  0.14450540    2
sample3 35.54135 -0.91582962 -0.1597201 -1.53164615    1
sample4 52.88065 -1.20234330 -0.5625411  0.09688919    2
sample5 37.44880 -1.07499035  0.9194973 -0.64529071    2
sample6 61.89169 -2.45345578 -0.9240242 -2.03646213    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  179.7779   1.00010 6.5335961 0.0105901  0.105901   232.663   239.634
gene2  118.6387   1.00006 3.9547689 0.0467412  0.212460   235.512   242.482
gene3   67.4753   1.00005 0.0178254 0.8938987  0.931144   215.312   222.282
gene4   96.3707   1.00012 4.1347696 0.0420311  0.212460   203.147   210.117
gene5   65.0917   1.00006 0.1205840 0.7283973  0.867140   224.087   231.057
gene6   50.4818   1.00008 0.0686221 0.7935611  0.901774   193.679   200.649

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  179.7779  0.7694516  0.485591  1.584568 0.1130644  0.388378   232.663
gene2  118.6387 -0.0889634  0.413081 -0.215366 0.8294822  0.961653   235.512
gene3   67.4753  0.3473905  0.433578  0.801217 0.4230060  0.783344   215.312
gene4   96.3707  0.4746237  0.514348  0.922767 0.3561288  0.712258   203.147
gene5   65.0917 -0.1548346  0.376609 -0.411128 0.6809785  0.917653   224.087
gene6   50.4818  0.9384425  0.477112  1.966921 0.0491923  0.204968   193.679
            BIC
      <numeric>
gene1   239.634
gene2   242.482
gene3   222.282
gene4   210.117
gene5   231.057
gene6   200.649

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  179.7779 -2.838085  1.216357 -2.333267 0.0196341  0.196341   232.663
gene2  118.6387  1.352764  1.031489  1.311467 0.1897002  0.632334   235.512
gene3   67.4753  1.147577  1.087517  1.055226 0.2913219  0.678330   215.312
gene4   96.3707  2.691469  1.302602  2.066225 0.0388072  0.238938   203.147
gene5   65.0917 -0.302507  0.941922 -0.321159 0.7480900  0.869872   224.087
gene6   50.4818 -1.279832  1.184650 -1.080346 0.2799882  0.678330   193.679
            BIC
      <numeric>
gene1   239.634
gene2   242.482
gene3   222.282
gene4   210.117
gene5   231.057
gene6   200.649

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   57.9296   1.00007  10.68767 0.00107920 0.0479990   207.289   214.259
gene33  119.7797   1.00019   9.62764 0.00191996 0.0479990   228.903   235.873
gene9    98.4810   1.00005   7.81434 0.00518515 0.0864191   219.449   226.420
gene15   99.2744   1.00007   7.28319 0.00696200 0.0870249   217.641   224.611
gene1   179.7779   1.00010   6.53360 0.01059012 0.1059012   232.663   239.634
gene26  228.6833   1.00007   4.82616 0.02804237 0.2124599   243.069   250.039
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.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 17763)

Matrix products: default

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

attached base packages:
[1] parallel  stats4    stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] ggplot2_3.3.3               BiocParallel_1.27.0        
 [3] NBAMSeq_1.9.0               SummarizedExperiment_1.23.0
 [5] Biobase_2.53.0              GenomicRanges_1.45.0       
 [7] GenomeInfoDb_1.29.0         IRanges_2.27.0             
 [9] S4Vectors_0.31.0            BiocGenerics_0.39.0        
[11] MatrixGenerics_1.5.0        matrixStats_0.59.0         

loaded via a namespace (and not attached):
 [1] httr_1.4.2             sass_0.4.0             bit64_4.0.5           
 [4] jsonlite_1.7.2         splines_4.1.0          bslib_0.2.5.1         
 [7] assertthat_0.2.1       highr_0.9              blob_1.2.1            
[10] GenomeInfoDbData_1.2.6 yaml_2.2.1             pillar_1.6.1          
[13] RSQLite_2.2.7          lattice_0.20-44        glue_1.4.2            
[16] digest_0.6.27          RColorBrewer_1.1-2     XVector_0.33.0        
[19] colorspace_2.0-1       htmltools_0.5.1.1      Matrix_1.3-4          
[22] DESeq2_1.33.1          XML_3.99-0.6           pkgconfig_2.0.3       
[25] genefilter_1.75.0      zlibbioc_1.39.0        purrr_0.3.4           
[28] xtable_1.8-4           snow_0.4-3             scales_1.1.1          
[31] tibble_3.1.2           annotate_1.71.0        mgcv_1.8-36           
[34] KEGGREST_1.33.0        farver_2.1.0           generics_0.1.0        
[37] ellipsis_0.3.2         withr_2.4.2            cachem_1.0.5          
[40] survival_3.2-11        magrittr_2.0.1         crayon_1.4.1          
[43] memoise_2.0.0          evaluate_0.14          fansi_0.5.0           
[46] nlme_3.1-152           tools_4.1.0            lifecycle_1.0.0       
[49] stringr_1.4.0          locfit_1.5-9.4         munsell_0.5.0         
[52] DelayedArray_0.19.0    AnnotationDbi_1.55.0   Biostrings_2.61.0     
[55] compiler_4.1.0         jquerylib_0.1.4        rlang_0.4.11          
[58] grid_4.1.0             RCurl_1.98-1.3         labeling_0.4.2        
[61] bitops_1.0-7           rmarkdown_2.8          gtable_0.3.0          
[64] DBI_1.1.1              R6_2.5.0               knitr_1.33            
[67] dplyr_1.0.6            fastmap_1.1.0          bit_4.0.4             
[70] utf8_1.2.1             stringi_1.6.2          Rcpp_1.0.6            
[73] vctrs_0.3.8            geneplotter_1.71.0     png_0.1-7             
[76] tidyselect_1.1.1       xfun_0.23             

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.