---
title: "Pseudotime analysis"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Pseudotime analysis}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  markdown: 
    wrap: 72
---

```{r setup, warning=FALSE, message=FALSE}
library(cyCONDOR)
library(pheatmap)
library(ggplot2)
library(dplyr)
```

With `cyCONDOR` we implemented `slingshot` for pseudotome analysis, following a workflow to calculate trajectories and pseudotime. 

This workflow can be applied to any type of HDFC data, nevertheless the interpretation of trajectories and preudotime should always be validated by other experiment of domain knowledge. We exemplified here with a subset of the cyTOF dataset from [Bendall et al. 2011](https://www.science.org/doi/10.1126/science.1198704)

If you use this workflow in your work please consider citing [cyCONDOR](XXX) and [Street et al. (2018)](https://bmcgenomics.biomedcentral.com/articles/10.1186/s12864-018-4772-0).

cyCONDOR implementation of `slingshot` follows the tutorial from the [NBIS tutorial](https://nbisweden.github.io/workshop-archive/workshop-scRNAseq/2020-01-27/labs/compiled/slingshot/slingshot.html)

## Load an example dataset

We start here by loading an example `condor` object already annotated.

```{r}
condor <- readRDS("../.test_files/condor_pseudotime_016.rds")
```

```{r}
plot_dim_red(fcd= condor,  
             expr_slot = NULL,
             reduction_method = "umap", 
             reduction_slot = "pca_orig", 
             cluster_slot = "phenograph_filter_pca_orig_k_10",
             param = "metaclusters",
             title = "UMAP colored by Phenograph",
             alpha= 1, dot_size = 1)
```

```{r, fig.height=7}
plot_marker_HM(fcd = condor,
               expr_slot = "orig",
               cluster_slot = "phenograph_filter_pca_orig_k_10",
               cluster_var = "metaclusters",
               cluster_rows = TRUE,
               cluster_cols = TRUE,
               title= "Marker expression Phenograph clustering")
```

# Pseudotime analysis

We can now calculate the pseudotime with different settings:

## With no constrains on the start of the trajectory

```{r}
condor <- runPseudotime(fcd = condor, 
                        reduction_method = "umap",
                        reduction_slot = "pca_orig",
                        cluster_slot= "phenograph_filter_pca_orig_k_10",
                        cluster_var = "metaclusters",
                        approx_points = NULL)
```

The output of is saved in `condor$pseudotime$slingshot_umap_pca_orig`.

```{r}
condor$pseudotime$slingshot_umap_pca_orig[1:5,]
```


## With a specific starting cluster

```{r}
condor <- runPseudotime(fcd = condor, 
                        reduction_method = "umap",
                        reduction_slot = "pca_orig",
                        cluster_slot= "phenograph_filter_pca_orig_k_10",
                        cluster_var = "metaclusters",
                        approx_points = NULL,
                        start.clus =  "CMPs",)
```

The output of is saved in `condor$pseudotime$slingshot_umap_pca_orig_CMPs`.

```{r}
condor$pseudotime$slingshot_umap_pca_orig_CMPs[1:5,]
```


## Testing all clusters as starting point

```{r}
for (i in unique(condor$clustering$phenograph_filter_pca_orig_k_10$metaclusters)[1:5]) {
  
  condor <- runPseudotime(fcd = condor, 
                          reduction_method = "umap",
                          reduction_slot = "pca_orig",
                          cluster_slot= "phenograph_filter_pca_orig_k_10",
                          cluster_var = "metaclusters",
                          approx_points = NULL,
                          start.clus =  i)
  
}
```

## Visualize the result of `slingshot` analysis

It is possible to use the `plot_dim_red` function to plot the result of pseudotime analysis overlayed on the UMAP coordinates.

```{r}
plot_dim_red(fcd = condor, 
             expr_slot = "orig", 
             reduction_method = "umap", 
             reduction_slot = "pca_orig", 
             cluster_slot = "phenograph_filter_pca_orig_k_10", 
             add_pseudotime = TRUE, 
             pseudotime_slot = "slingshot_umap_pca_orig",
             param = "mean", 
             order = T, 
             title = "UMAP colored by pseudotime", 
             facet_by_variable = FALSE, 
             raster = TRUE, 
             alpha = 1, 
             dot_size = 1) + 
  geom_path(data = condor$extras$slingshot_umap_pca_orig$lineages %>% arrange(Order), aes(group = Lineage), size = 0.5)
```



# Heatmap visualization of monocytes trajectory

We provide here some custom code to visualize the pseudotime, nevertheless many visualization can be performed on this data depending on the biological question.

```{r}
selections <- rownames(condor$clustering$phenograph_filter_pca_orig_k_10[condor$clustering$phenograph_filter_pca_orig_k_10$metaclusters %in% c("HSCs", "CMPs", "Myeloblast", "Monocytes"), ])

condor_mono <- filter_fcd(fcd = condor,
                            cell_ids = selections)
```

```{r}
expression <- condor_mono$expr$orig

anno <- cbind(condor_mono$clustering$phenograph_filter_pca_orig_k_10[, c("Phenograph", "metaclusters")], condor_mono$pseudotime$slingshot_umap_pca_orig)

anno <- anno[order(anno$Lineage2, decreasing = FALSE),]

expression <- expression[rownames(anno), c("148-CD34", "160-CD14", "144-CD11b")]

my_colour = list(metaclusters = c(Monocytes = "#CBD588", HSCs = "#689030", Myeloblast = "#DA5724", CMPs = "#F7941D"))
```

```{r}
pheatmap(mat = expression, 
         scale = "column", 
         show_rownames = FALSE, 
         cluster_rows = F, 
         cluster_cols = F, 
         annotation_row = anno[, c("metaclusters", "Lineage2")], 
         annotation_colors = my_colour, 
         breaks = scaleColors(expression, maxvalue = 2)[["breaks"]], 
         color = scaleColors(expression, maxvalue = 2)[["color"]], main = "Heatmap Monocytes pseudotime")
```

# Session Info
```{r}
info <- sessionInfo()

info
```
