# Introduction to netSmooth package

Jonathan Ronen1 and Altuna Akalin1

1Berlin Institute for Medical Systems Biology, Max Delbrück Center

#### 2019-05-03

# 1 Introduction

netSmooth implements a network-smoothing framework to smooth single-cell gene expression data as well as other omics datasets. The algorithm is a graph based diffusion process on networks. The intuition behind the algorithm is that gene networks encoding coexpression patterns may be used to smooth scRNA-seq expression data, since the gene expression values of connected nodes in the network will be predictive of each other. Protein-protein interaction (PPI) networks and coexpression networks are among the networks that could be used for such procedure.

More precisely, netSmooth works as follows. First, the gene expression values or other quantitative values per gene from each sample is projected on to the provided network. Then, the diffusion process is used to smooth the expression values of adjacent genes in the graph, so that a genes expression value represent an estimate of expression levels based the gene it self, as well as the expression values of the neighbors in the graph. The rate at which expression values of genes diffuse to their neighbors is degree-normalized, so that genes with many edges will affect their neighbors less than genes with more specific interactions. The implementation has one free parameter, `alpha`, which controls if the diffusion will be local or will reach further in the graph. Higher the value, the further the diffusion will reach. The netSmooth package implements strategies to optimize the value of `alpha`.

Figure 1: Network-smoothing concept

In summary, netSmooth enables users to smooth quantitative values associated with genes using a gene interaction network such as a protein-protein interaction network. The following sections of this vignette demonstrate functionality of `netSmooth` package.

# 2 Smoothing single-cell gene expression data with netSmooth() function

The workhorse of the netSmooth package is the `netSmooth()` function. This function takes at least two arguments, a network and genes-by-samples matrix as input, and performs smoothing on genes-by-samples matrix. The network should be organized as an adjacency matrix and its row and column names should match the row names of genes-by-samples matrix.

We will demonstrate the usage of the `netSmooth()` function using a subset of human PPI and a subset of single-cell RNA-seq data from GSE44183-GPL11154. We will first load the example datasets that are available through netSmooth package.

    data(smallPPI)
    data(smallscRNAseq)

We can now smooth the gene expression network now with `netSmooth()` function. We will use `alpha=0.5`.

    smallscRNAseq.sm.se <- netSmooth(smallscRNAseq, smallPPI, alpha=0.5)

    ## Using given alpha: 0.5

    smallscRNAseq.sm.sce <- SingleCellExperiment(
        assays=list(counts=assay(smallscRNAseq.sm.se)),
        colData=colData(smallscRNAseq.sm.se)
    )

Now, we can look at the smoothed and raw expression values using a heatmap.

    anno.df <- data.frame(cell.type=colData(smallscRNAseq)$source_name_ch1)
    rownames(anno.df) <- colnames(smallscRNAseq)
    pheatmap(log2(assay(smallscRNAseq)+1), annotation_col = anno.df,
             show_rownames = FALSE, show_colnames = FALSE,
             main="before netSmooth")

    pheatmap(log2(assay(smallscRNAseq.sm.sce)+1), annotation_col = anno.df,
             show_rownames = FALSE, show_colnames = FALSE,
             main="after netSmooth")

## 2.1 Optimizing the smoothing parameter `alpha`

By default, the parameter `alpha` will be optimized using a robust clustering statistic. Briefly, this approach will try different clustering algorithms and/or parameters and find clusters that can be reproduced with different algorithms. The `netSmooth()` function will try different `alpha` values controlled by additional arguments to maximize the number of samples in robust clusters.

Now, we smooth the expression values using automated alpha optimization and plot the heatmaps of raw and smooth versions.

    smallscRNAseq.sm.se <- netSmooth(smallscRNAseq, smallPPI, alpha='auto')
    smallscRNAseq.sm.sce <- SingleCellExperiment(
        assays=list(counts=assay(smallscRNAseq.sm.se)),
        colData=colData(smallscRNAseq.sm.se)
    )

    pheatmap(log2(assay(smallscRNAseq.sm.sce)+1), annotation_col = anno.df,
             show_rownames = FALSE, show_colnames = FALSE,
             main="after netSmooth (optimal alpha)")

# 3 Getting robust clusters from data

There is no standard method especially for clustering single cell RNAseq data, as different studies produce data with different topologies, which respond differently to the various clustering algorithms. In order to avoid optimizing different clustering routines for the different datasets, we have implemented a robust clustering routine based on clusterExperiment. The clusterExperiment framework for robust clustering is based on consensus clustering of clustering assignments obtained from different views of the data and different clustering algorithms. The different views are different reduced dimensionality projections of the data based on different techniques; thus, no single clustering result will dominate the data, and only cluster structures which are robust to different analyses will prevail. We implemented a clustering framework using the components of clusterExperiment and different dimensionality reduction methods.

We can directly use the robust clustering function `robustClusters`.

    yhat <- robustClusters(smallscRNAseq, makeConsensusMinSize=2, makeConsensusProportion=.9)$clusters

    ## Picked dimReduceFlavor: umap

    ## Note: Merging will be done on ' makeConsensus ', with clustering index 1

    yhat.sm <- robustClusters(smallscRNAseq.sm.se, makeConsensusMinSize=2, makeConsensusProportion=.9)$clusters

    ## Picked dimReduceFlavor: pca
    ##
    ## Note: Merging will be done on ' makeConsensus ', with clustering index 1

    cell.types <- colData(smallscRNAseq)$source_name_ch1
    knitr::kable(
      table(cell.types, yhat), caption = 'Cell types and `robustClusters` in the raw data.'
    )

Table 1: Cell types and `robustClusters` in the raw data
| -1 | 1 | 2
--- | --- | --- | ---
2-cell blastomere | 1 | 2 | 0
4-cell blastomere | 2 | 2 | 0
8-cell blastomere | 6 | 0 | 4
8-cell embryo | 1 | 0 | 0
morula | 1 | 0 | 2
oocyte | 0 | 3 | 0
pronucleus | 2 | 1 | 0
zygote | 1 | 1 | 0

    knitr::kable(
      table(cell.types, yhat.sm), caption = 'Cell types and `robustClusters` in the smoothed data.'
    )

Table 1: Cell types and `robustClusters` in the smoothed data
| -1 | 1 | 2 | 3 | 4 | 5
--- | --- | --- | --- | --- | --- | ---
2-cell blastomere | 1 | 2 | 0 | 0 | 0 | 0
4-cell blastomere | 1 | 0 | 0 | 0 | 3 | 0
8-cell blastomere | 8 | 0 | 0 | 0 | 0 | 2
8-cell embryo | 1 | 0 | 0 | 0 | 0 | 0
morula | 0 | 0 | 0 | 3 | 0 | 0
oocyte | 0 | 1 | 2 | 0 | 0 | 0
pronucleus | 0 | 2 | 1 | 0 | 0 | 0
zygote | 0 | 1 | 1 | 0 | 0 | 0

A cluster assignment of `-1` indicates that the cell could not be placed in a robust cluster, and has consequently been omitted. We see that the clusters are completely uninformative in the raw data, while the smoothed data at least permitted the `robustClusters` procedure to identify a subset of the 8-cell blastomeres as a separate cluster.

# 4 Deciding for the best dimension reduction method for visualization and clustering

The `robustClusters()` function works by clustering samples in a lower dimension embedding using either PCA or t-SNE. Different single cell datasets might respond better to different dimensionality reduction techniques. In order to pick the right technique algorithmically, we compute the entropy in a 2D embedding. We obtained 2D embeddings from the 500 most variable genes using either PCA or t-SNE, binned them in a 20x20 grid, and computed the entropy. The entropy in the 2D embedding is a measure for the information captured by it. We pick the embedding with the highest information content. `pickDimReduction()` function implements this strategy and returns the best embedding according to this strategy.

Below, we pick the best embedding for our example dataset and plot scatter plots for different 2D embedding methods.

    plotPCA(smallscRNAseq, colour_by='source_name_ch1') + ggtitle("PCA plot")

    plotTSNE(smallscRNAseq, colour_by='source_name_ch1') + ggtitle("tSNE plot")

    plotUMAP(smallscRNAseq, colour_by='source_name_ch1') + ggtitle("UMAP plot")

The `pickDimReduction` method picks the dimensionality reduction method which produces the highest entropy embedding:

    pickDimReduction(smallscRNAseq)

    ## [1] "umap"

# 5 Frequently asked questions

### 5.0.1 How can I make smoothing faster ?

Make sure you compile R with openBLAS or variants that are faster.

### 5.0.2 What happens if all the genes are not in my network ?

The smoothing will only be done using the genes in the network then unsmoothed genes will be attached to the gene expression matrix.

    sessionInfo()

    ## R version 3.6.0 (2019-04-26)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows Server 2012 R2 x64 (build 9600)
    ##
    ## 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] pheatmap_1.0.12             netSmooth_1.4.0
    ##  [3] clusterExperiment_2.4.0     scater_1.12.0
    ##  [5] ggplot2_3.1.1               SingleCellExperiment_1.6.0
    ##  [7] SummarizedExperiment_1.14.0 DelayedArray_0.10.0
    ##  [9] BiocParallel_1.18.0         matrixStats_0.54.0
    ## [11] Biobase_2.44.0              GenomicRanges_1.36.0
    ## [13] GenomeInfoDb_1.20.0         IRanges_2.18.0
    ## [15] S4Vectors_0.22.0            BiocGenerics_0.30.0
    ## [17] BiocStyle_2.12.0
    ##
    ## loaded via a namespace (and not attached):
    ##   [1] uuid_0.1-2               copula_0.999-19.1
    ##   [3] blme_1.0-4               NMF_0.21.0
    ##   [5] plyr_1.8.4               lazyeval_0.2.2
    ##   [7] splines_3.6.0            pspline_1.0-18
    ##   [9] entropy_1.2.1            rncl_0.8.3
    ##  [11] gridBase_0.4-7           digest_0.6.18
    ##  [13] foreach_1.4.4            htmltools_0.3.6
    ##  [15] viridis_0.5.1            magrittr_1.5
    ##  [17] memoise_1.1.0            cluster_2.0.9
    ##  [19] doParallel_1.0.14        limma_3.40.0
    ##  [21] annotate_1.62.0          RcppParallel_4.4.2
    ##  [23] stabledist_0.7-1         prettyunits_1.0.2
    ##  [25] colorspace_1.4-1         blob_1.1.1
    ##  [27] xfun_0.6                 dplyr_0.8.0.1
    ##  [29] crayon_1.3.4             RCurl_1.95-4.12
    ##  [31] lme4_1.1-21              genefilter_1.66.0
    ##  [33] phylobase_0.8.6          survival_2.44-1.1
    ##  [35] iterators_1.0.10         ape_5.3
    ##  [37] glue_1.3.1               registry_0.5-1
    ##  [39] gtable_0.3.0             zlibbioc_1.30.0
    ##  [41] XVector_0.24.0           BiocSingular_1.0.0
    ##  [43] kernlab_0.9-27           Rhdf5lib_1.6.0
    ##  [45] HDF5Array_1.12.0         abind_1.4-5
    ##  [47] scales_1.0.0             mvtnorm_1.0-10
    ##  [49] DBI_1.0.0                edgeR_3.26.0
    ##  [51] rngtools_1.3.1.1         bibtex_0.4.2
    ##  [53] Rcpp_1.0.1               viridisLite_0.3.0
    ##  [55] xtable_1.8-4             progress_1.2.0
    ##  [57] bit_1.1-14               rsvd_1.0.0
    ##  [59] glmnet_2.0-16            httr_1.4.0
    ##  [61] FNN_1.1.3                RColorBrewer_1.1-2
    ##  [63] pkgconfig_2.0.2          XML_3.98-1.19
    ##  [65] uwot_0.1.3               locfit_1.5-9.1
    ##  [67] labeling_0.3             howmany_0.3-1
    ##  [69] tidyselect_0.2.5         rlang_0.3.4
    ##  [71] softImpute_1.4           reshape2_1.4.3
    ##  [73] AnnotationDbi_1.46.0     munsell_0.5.0
    ##  [75] tools_3.6.0              RSQLite_2.1.1
    ##  [77] ade4_1.7-13              evaluate_0.13
    ##  [79] stringr_1.4.0            yaml_2.2.0
    ##  [81] knitr_1.22               bit64_0.9-7
    ##  [83] purrr_0.3.2              nlme_3.1-139
    ##  [85] xml2_1.2.0               compiler_3.6.0
    ##  [87] beeswarm_0.2.3           tibble_2.1.1
    ##  [89] RNeXML_2.3.0             pcaPP_1.9-73
    ##  [91] stringi_1.4.3            gsl_2.1-6
    ##  [93] highr_0.8                RSpectra_0.14-0
    ##  [95] lattice_0.20-38          Matrix_1.2-17
    ##  [97] nloptr_1.2.1             pillar_1.3.1
    ##  [99] ADGofTest_0.3            BiocManager_1.30.4
    ## [101] zinbwave_1.6.0           BiocNeighbors_1.2.0
    ## [103] cowplot_0.9.4            data.table_1.12.2
    ## [105] bitops_1.0-6             irlba_2.3.3
    ## [107] R6_2.4.0                 bookdown_0.9
    ## [109] gridExtra_2.3            vipor_0.4.5
    ## [111] codetools_0.2-16         boot_1.3-22
    ## [113] MASS_7.3-51.4            assertthat_0.2.1
    ## [115] MAST_1.10.0              rhdf5_2.28.0
    ## [117] pkgmaker_0.27            withr_2.1.2
    ## [119] GenomeInfoDbData_1.2.1   locfdr_1.1-8
    ## [121] hms_0.4.2                grid_3.6.0
    ## [123] minqa_1.2.4              tidyr_0.8.3
    ## [125] rmarkdown_1.12           DelayedMatrixStats_1.6.0
    ## [127] Rtsne_0.15               numDeriv_2016.8-1
    ## [129] ggbeeswarm_0.6.0