In this vignette, we will
demonstrate how to perform TCR trajectory analysis starting from data
that has already been processed by dandelion
in python.
This is to demonstrate that the original method/results in the
dandelion
paper can be reproduced.
We will also load scRepertoire
and scater
for the analysis.
First, we will load the demo data. This is a down-sampled dataset from Suo et al 2024.
It contains 10,000 cells with the TCR information and the
dimensionality reduced data (scVI) that we need for this tutorial. The
gene expression matrix is not required for this tutorial so it is not
included in the demo data. We will show in a separate tutorial how to
start with data from scRepertoire
.
We will set the seed so that the plots and results are consistent.
To begin, we will filter the data and extract the TCR information so that we can construct pseudobulks.
Because the colData
of this single-cell object is
populated with the TCR information from dandelion
in python
(through this method: dandelion
-> anndata
-> anndata2ri
, which essentially converts an
AnnData
object in python to
SingleCellExperiment
in R), we can directly use the
setupVdjPseudobulk
function to extract the TCR information
and construct the pseudobulks.
Here, we also need to specify the allowed_chain_status
to keep to the relevant contigs. Default for
allowed_chain_status
is NULL
, which will keep
all contigs. In the standard R workflow that starts from
scRepertoire
, we will assume that all the QC and filtering
has already been handled by scRepertoire
.
sce_vdj <- setupVdjPseudobulk(sce_vdj,
already.productive = FALSE,
allowed_chain_status = c(
"Single pair", "Extra pair",
"Extra pair-exception", "Orphan VDJ",
"Orphan VDJ-exception"
)
)
We can visualise the UMAP of the filtered data.
We will use miloR to create the pseudobulks based on the gene expression data. The goal is to construct a neighbourhood graph with many neighbors with which we can sample the representative neighbours to form the objects.
Next, we will construct the pseudobulked VDJ feature space using the neighbourhood graph constructed above. We will also run PCA on the pseudobulked VDJ feature space.
pb.milo <- vdjPseudobulk(milo_object, col_to_take = "anno_lvl_2_final_clean")
pb.milo <- runPCA(pb.milo, assay.type = "Feature_space")
We can visualise the PCA of the pseudobulked VDJ feature space.
In the original dandelion
python package, the trajectory
inference is done using the palantir
package. Here, we
implement the absorbing markov chain approach in dandelionR to infer the
trajectory, leveraging on destiny
for diffusion map
computation.
library(SingleCellExperiment)
# extract the PCA matrix
pca <- t(as.matrix(reducedDim(pb.milo, type = "PCA")))
# define the CD8 terminal cell as the top-most cell and CD4 terminal cell as
# the bottom-most cell
branch.tips <- c(which.max(pca[2, ]), which.min(pca[2, ]))
names(branch.tips) <- c("CD8+T", "CD4+T")
# define the start of our trajectory as the right-most cell
root <- which.max(pca[1, ])
# the root is automatically called DPT + index of the root cell
DPTroot <- paste0("DPT", root)
# store pseudotime in milo object
pb.milo$pseudotime <- dif.pse[[DPTroot]]
# set the colours for pseudotime
pal <- colorRampPalette(rev((RColorBrewer::brewer.pal(9, "RdYlBu"))))(255)
plotPCA(pb.milo, color_by = "pseudotime") +
scale_colour_gradientn(colours = pal)
With the Markov chain probabilities computed, we can visualise the branch probabilities towards CD4+ or CD8+ T-cell fate on the PCA plot.
The next step is to project the pseudotime and the branch probability information from the pseudobulks back to each cell in the dataset. If the cell do not belong to any of the pseudobulk, it will be removed. If a cell belongs to multiple pseudobulk samples, its value should be calculated as a weighted average of the corresponding values from each pseudobulk, where each weight is inverse of the size of the pseudobulk.
## R version 4.4.2 (2024-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.2 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: Etc/UTC
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] destiny_3.21.0 miloR_2.3.0
## [3] edgeR_4.5.2 limma_3.63.3
## [5] scater_1.35.1 scuttle_1.17.0
## [7] SingleCellExperiment_1.29.1 SummarizedExperiment_1.37.0
## [9] Biobase_2.67.0 GenomicRanges_1.59.1
## [11] GenomeInfoDb_1.43.4 IRanges_2.41.3
## [13] S4Vectors_0.45.4 BiocGenerics_0.53.6
## [15] generics_0.1.3 MatrixGenerics_1.19.1
## [17] matrixStats_1.5.0 scRepertoire_2.3.2
## [19] ggplot2_3.5.1 dandelionR_0.99.10
## [21] BiocStyle_2.35.0
##
## loaded via a namespace (and not attached):
## [1] cubature_2.1.1 splines_4.4.2 tibble_3.2.1
## [4] polyclip_1.10-7 xts_0.14.1 lifecycle_1.0.4
## [7] globals_0.16.3 lattice_0.22-6 MASS_7.3-64
## [10] magrittr_2.0.3 vcd_1.4-13 sass_0.4.9
## [13] rmarkdown_2.29 jquerylib_0.1.4 yaml_2.3.10
## [16] spam_2.11-1 sp_2.2-0 cowplot_1.1.3
## [19] buildtools_1.0.0 RColorBrewer_1.1-3 abind_1.4-8
## [22] purrr_1.0.4 ggraph_2.2.1 nnet_7.3-20
## [25] pracma_2.4.4 tweenr_2.0.3 evmix_2.12
## [28] GenomeInfoDbData_1.2.13 ggrepel_0.9.6 irlba_2.3.5.1
## [31] listenv_0.9.1 maketools_1.3.2 iNEXT_3.0.1
## [34] MatrixModels_0.5-3 RSpectra_0.16-2 parallelly_1.42.0
## [37] codetools_0.2-20 smoother_1.3 DelayedArray_0.33.6
## [40] ggforce_0.4.2 tidyselect_1.2.1 UCSC.utils_1.3.1
## [43] farver_2.1.2 ScaledMatrix_1.15.0 viridis_0.6.5
## [46] jsonlite_1.9.0 BiocNeighbors_2.1.2 e1071_1.7-16
## [49] tidygraph_1.3.1 progressr_0.15.1 Formula_1.2-5
## [52] survival_3.8-3 ggalluvial_0.12.5 tools_4.4.2
## [55] stringdist_0.9.15 Rcpp_1.0.14 glue_1.8.0
## [58] gridExtra_2.3 SparseArray_1.7.6 laeken_0.5.3
## [61] xfun_0.51 ranger_0.17.0 TTR_0.24.4
## [64] ggthemes_5.1.0 dplyr_1.1.4 withr_3.0.2
## [67] numDeriv_2016.8-1.1 BiocManager_1.30.25 fastmap_1.2.0
## [70] boot_1.3-31 bluster_1.17.0 SparseM_1.84-2
## [73] VIM_6.2.2 digest_0.6.37 rsvd_1.0.5
## [76] R6_2.6.1 colorspace_2.1-1 gtools_3.9.5
## [79] tidyr_1.3.1 hexbin_1.28.5 data.table_1.16.4
## [82] robustbase_0.99-4-1 class_7.3-23 graphlayouts_1.2.2
## [85] httr_1.4.7 S4Arrays_1.7.3 scatterplot3d_0.3-44
## [88] uwot_0.2.2 pkgconfig_2.0.3 gtable_0.3.6
## [91] lmtest_0.9-40 XVector_0.47.2 sys_3.4.3
## [94] htmltools_0.5.8.1 carData_3.0-5 dotCall64_1.2
## [97] SeuratObject_5.0.2 scales_1.3.0 knn.covertree_1.0
## [100] ggdendro_0.2.0 knitr_1.49 rjson_0.2.23
## [103] reshape2_1.4.4 curl_6.2.1 proxy_0.4-27
## [106] cachem_1.1.0 zoo_1.8-12 stringr_1.5.1
## [109] parallel_4.4.2 vipor_0.4.7 pillar_1.10.1
## [112] grid_4.4.2 vctrs_0.6.5 pcaMethods_1.99.0
## [115] VGAM_1.1-13 car_3.1-3 BiocSingular_1.23.0
## [118] beachmat_2.23.6 cluster_2.1.8 beeswarm_0.4.0
## [121] evaluate_1.0.3 truncdist_1.0-2 cli_3.6.4
## [124] locfit_1.5-9.11 compiler_4.4.2 rlang_1.1.5
## [127] crayon_1.5.3 future.apply_1.11.3 labeling_0.4.3
## [130] plyr_1.8.9 ggbeeswarm_0.7.2 stringi_1.8.4
## [133] viridisLite_0.4.2 BiocParallel_1.41.2 assertthat_0.2.1
## [136] munsell_0.5.1 gsl_2.1-8 quantreg_6.00
## [139] Matrix_1.7-2 RcppHNSW_0.6.0 RcppEigen_0.3.4.0.2
## [142] patchwork_1.3.0 future_1.34.0 statmod_1.5.0
## [145] evd_2.3-7.1 igraph_2.1.4 memoise_2.0.1
## [148] bslib_0.9.0 DEoptimR_1.1-3-1 ggplot.multistats_1.0.1