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     164      78     104       5       1       1       6       1       8
gene2      22       1      28       3       7      55     403      46     179
gene3      19       1       3       7     235     202     466     186       1
gene4      15       9     251     159     300       9       3     122       1
gene5       1     131     149     125      55     380     186       4       1
gene6      14      27     128      14       3     382     413       4     162
      sample10 sample11 sample12 sample13 sample14 sample15 sample16 sample17
gene1        1       60       44       89        8       57       70        1
gene2       24      486       66      492        7        6      369        1
gene3      301        2      121      472       98       59      360        1
gene4      147      263      266       70        2      237       60        9
gene5       63       51       11       18      195       39       25        1
gene6       16      751       34        2       25       83      184        5
      sample18 sample19 sample20
gene1       28       84       30
gene2        1      159      141
gene3      303       69        2
gene4       42        1      281
gene5        7      102      154
gene6       12        3        1

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 31.65458 -0.1699300 -0.2868070 -1.1233824    1
sample2 40.85614  0.9186161  1.4822072 -0.4300385    0
sample3 55.20760  0.2069261 -1.1843105 -0.6091992    1
sample4 65.15571 -1.0138770 -0.6949176  0.8366814    2
sample5 61.31593  0.6011855 -0.9866629  0.4540759    1
sample6 26.88190  1.5008849  0.1690043 -0.1783239    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   30.7518   1.00005 0.0482581 0.8262010  0.918001   193.457   200.427
gene2  105.9935   1.00009 5.8326377 0.0157387  0.157387   227.127   234.097
gene3  125.3798   1.00006 0.8592767 0.3539785  0.573654   237.859   244.829
gene4   91.3419   1.00013 5.9158227 0.0150067  0.157387   227.586   234.556
gene5   82.3314   1.00018 1.7341189 0.1879657  0.469914   230.672   237.643
gene6  105.8434   1.00011 0.1156068 0.7339973  0.853485   228.587   235.557

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   30.7518  0.0825266  0.394580  0.209151  0.834331  0.834331   193.457
gene2  105.9935  0.3476968  0.439385  0.791327  0.428753  0.612505   227.127
gene3  125.3798  0.6716173  0.466004  1.441228  0.149520  0.442273   237.859
gene4   91.3419 -0.2638111  0.397832 -0.663122  0.507252  0.679190   227.586
gene5   82.3314  0.3891930  0.445956  0.872716  0.382818  0.589418   230.672
gene6  105.8434  0.1652133  0.486316  0.339724  0.734064  0.815627   228.587
            BIC
      <numeric>
gene1   200.427
gene2   234.097
gene3   244.829
gene4   234.556
gene5   237.643
gene6   235.557

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   30.7518  1.284028   1.14855  1.117958  0.263585  0.536587   193.457
gene2  105.9935 -0.476629   1.25355 -0.380225  0.703778  0.818347   227.127
gene3  125.3798 -1.747926   1.33165 -1.312606  0.189316  0.526634   237.859
gene4   91.3419 -0.467842   1.14068 -0.410142  0.681702  0.811550   227.586
gene5   82.3314 -0.347003   1.27611 -0.271923  0.785681  0.868714   230.672
gene6  105.8434  0.324240   1.39511  0.232412  0.816218  0.868714   228.587
            BIC
      <numeric>
gene1   200.427
gene2   234.097
gene3   244.829
gene4   234.556
gene5   237.643
gene6   235.557

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
       <numeric> <numeric> <numeric>   <numeric>   <numeric> <numeric>
gene22   54.6599   1.00004  22.67270 1.59771e-06 7.98857e-05   185.963
gene20   37.7694   1.00009   8.12414 4.37213e-03 1.09303e-01   188.420
gene23   86.4547   1.00007   7.19452 7.31643e-03 1.21941e-01   214.975
gene4    91.3419   1.00013   5.91582 1.50067e-02 1.57387e-01   227.586
gene2   105.9935   1.00009   5.83264 1.57387e-02 1.57387e-01   227.127
gene41  145.2649   1.00007   5.17348 2.29415e-02 1.71771e-01   231.465
             BIC
       <numeric>
gene22   192.933
gene20   195.390
gene23   221.945
gene4    234.556
gene2    234.097
gene41   238.435
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 RC (2021-05-10 r80283)
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.26.0        
 [3] NBAMSeq_1.8.0               SummarizedExperiment_1.22.0
 [5] Biobase_2.52.0              GenomicRanges_1.44.0       
 [7] GenomeInfoDb_1.28.0         IRanges_2.26.0             
 [9] S4Vectors_0.30.0            BiocGenerics_0.38.0        
[11] MatrixGenerics_1.4.0        matrixStats_0.58.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.32.0        
[19] colorspace_2.0-1       htmltools_0.5.1.1      Matrix_1.3-3          
[22] DESeq2_1.32.0          XML_3.99-0.6           pkgconfig_2.0.3       
[25] genefilter_1.74.0      zlibbioc_1.38.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.70.0        mgcv_1.8-35           
[34] KEGGREST_1.32.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.4.2           
[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.18.0    AnnotationDbi_1.54.0   Biostrings_2.60.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.70.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.