# Using traviz

#### Koen Van den Berge, Hector Roux de Bézieux, Kelly Street

#### 03/06/2021

# Introduction

The `traviz` package was created to collect visualization functions related to trajectory inference. Asides from general purpose functions useful to any user, it also contains visualizations that can be used in a `slingshot`, `tradeSeq` or `condiments` workflow.

# Visualization of a trajectory

First, we will demonstrate functions to visualize a trajectory. Here, we’ll work with a trajectory as estimated by Slingshot (Street et al. 2018). An example trajectory is provided along with the `traviz` package.

Below, we show how one can use the `lines` and `plot` functions for a `SlingshotDataSet` object. We also show how the `plotGeneCount` function can be used for a quick visualization of the trajectory.

    library(slingshot)
    library(traviz)

    ##
    ## Attaching package: 'traviz'

    ## The following object is masked from 'package:scater':
    ##
    ##     plotExpression

    data(crv, package = "traviz")
    class(crv)

    ## [1] "SlingshotDataSet"
    ## attr(,"package")
    ## [1] "slingshot"

    rd <- slingReducedDim(crv)
    cl <- apply(slingClusterLabels(crv),1, function(x) which(x==1))

    ## Only visualize the trajectory
    plot(crv)

    ## Visualize the trajectory on top of cells in reduced space
    plot(rd, pch=16, col=cl+1, cex=2/3)
    lines(crv, col="black", lwd=3)

    ## Visualizing trajectory and clusters using plotGeneCount
    plotGeneCount(crv, clusters=cl)

### 3D visualization

The same functions (i.e., `plot` and `lines`) can also be used to visualize trajectories in 3D space using the `rgl` package. This can be done using the `plot3d` and `lines3d` functions, in similar vein as the 2D visualizations above.

# Visualization of gene expression along a trajectory

## Visualizing gene expression along with the trajectory in reduced space

The `plotGeneCount` also allows you to visualize the trajectory in reduced space, where each cell is colored according to its expression of the gene as defined by the `gene` argument.

    data(counts, package="traviz")
    plotGeneCount(crv, counts, gene = "Mpo")

## Visualizing gene expression along pseudotime

The `plotExpression` function may be used to visualize gene expression along pseudotime along with a quick smooth estimate of average gene expression. It uses a loess smoother to smooth the raw gene expression values as a function of pseudotime. Note that no between-cell normalization occurs to do the smoothing.

    plotExpression(counts=counts,
                   sds=crv,
                   gene="Car1")

If smoothers have been fitted using `tradeSeq`, then one can use the `plotSmoothers` function to plot the estimated smoothers. Note that here normalization does happen within `tradeSeq`, and the smoothers are estimated using a negative binomial count model.

    data(sce, package="traviz")

    plotSmoothers(models = sce, counts = counts, gene = "Elane")

# Feedback and suggestions

Any comment, question, or feedback is welcome and can be posted as an issue on the traviz repo.

# Session Info

    sessionInfo()

    ## R version 4.2.0 RC (2022-04-19 r82224 ucrt)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows Server 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
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] traviz_1.2.0                scater_1.24.0
    ##  [3] scuttle_1.6.0               dplyr_1.0.8
    ##  [5] ggplot2_3.3.5               slingshot_2.4.0
    ##  [7] TrajectoryUtils_1.4.0       SingleCellExperiment_1.18.0
    ##  [9] SummarizedExperiment_1.26.0 Biobase_2.56.0
    ## [11] GenomicRanges_1.48.0        GenomeInfoDb_1.32.0
    ## [13] IRanges_2.30.0              S4Vectors_0.34.0
    ## [15] BiocGenerics_0.42.0         MatrixGenerics_1.8.0
    ## [17] matrixStats_0.62.0          princurve_2.1.6
    ## [19] knitr_1.38
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] nlme_3.1-157              fs_1.5.2
    ##  [3] bitops_1.0-7              RColorBrewer_1.1-3
    ##  [5] tools_4.2.0               bslib_0.3.1
    ##  [7] utf8_1.2.2                R6_2.5.1
    ##  [9] irlba_2.3.5               vipor_0.4.5
    ## [11] DBI_1.1.2                 mgcv_1.8-40
    ## [13] colorspace_2.0-3          withr_2.5.0
    ## [15] tidyselect_1.1.2          gridExtra_2.3
    ## [17] extrafontdb_1.0           compiler_4.2.0
    ## [19] cli_3.3.0                 BiocNeighbors_1.14.0
    ## [21] DelayedArray_0.22.0       labeling_0.4.2
    ## [23] sass_0.4.1                scales_1.2.0
    ## [25] pkgdown_2.0.3             stringr_1.4.0
    ## [27] digest_0.6.29             rmarkdown_2.14
    ## [29] XVector_0.36.0            pkgconfig_2.0.3
    ## [31] htmltools_0.5.2           extrafont_0.18
    ## [33] sparseMatrixStats_1.8.0   fastmap_1.1.0
    ## [35] highr_0.9                 htmlwidgets_1.5.4
    ## [37] rlang_1.0.2               DelayedMatrixStats_1.18.0
    ## [39] jquerylib_0.1.4           farver_2.1.0
    ## [41] generics_0.1.2            jsonlite_1.8.0
    ## [43] BiocParallel_1.30.0       RCurl_1.98-1.6
    ## [45] magrittr_2.0.3            BiocSingular_1.12.0
    ## [47] GenomeInfoDbData_1.2.8    Matrix_1.4-1
    ## [49] Rcpp_1.0.8.3              ggbeeswarm_0.6.0
    ## [51] munsell_0.5.0             fansi_1.0.3
    ## [53] viridis_0.6.2             lifecycle_1.0.1
    ## [55] stringi_1.7.6             yaml_2.3.5
    ## [57] zlibbioc_1.42.0           grid_4.2.0
    ## [59] parallel_4.2.0            ggrepel_0.9.1
    ## [61] crayon_1.5.1              lattice_0.20-45
    ## [63] cowplot_1.1.1             beachmat_2.12.0
    ## [65] splines_4.2.0             pillar_1.7.0
    ## [67] igraph_1.3.1              ScaledMatrix_1.4.0
    ## [69] glue_1.6.2                evaluate_0.15
    ## [71] vctrs_0.4.1               Rttf2pt1_1.3.10
    ## [73] gtable_0.3.0              purrr_0.3.4
    ## [75] assertthat_0.2.1          cachem_1.0.6
    ## [77] xfun_0.30                 rsvd_1.0.5
    ## [79] viridisLite_0.4.0         tibble_3.1.6
    ## [81] memoise_2.0.1             beeswarm_0.4.0
    ## [83] rgl_0.108.3               ellipsis_0.3.2

# References

Street, Kelly, Davide Risso, Russell B. Fletcher, Diya Das, John Ngai, Nir Yosef, Elizabeth Purdom, and Sandrine Dudoit. 2018. “Slingshot: cell lineage and pseudotime inference for single-cell transcriptomics.” BMC Genomics 19 (1): 477. https://doi.org/10.1186/s12864-018-4772-0.