Additionally, we can use regression to remove any unwanted sources of variation from the dataset, such as `cell cycle`, `sequencing depth`, `percent mitocondria`. This is achieved by doing a generalized linear regression using these parameters as covariates in the model. Then the residuals of the model are taken as the "regressed data". Although perhaps not in the best way, batch effect regression can also be done here. By default variables are scaled in the PCA step and is not done separately. But it could be acheieved by running the commads below: ```{r} # sce@assays$data@listData$scaled.data <- apply(exprs(sce)[rownames(hvg.out),,drop=FALSE],2,function(x) scale(x,T,T)) # rownames(sce@assays$data@listData$scaled.data) <- rownames(hvg.out) ``` ## PCA *** Performing PCA has many useful applications and interpretations, which much depends on the data used. In the case of life sciences, we want to segregate samples based on gene expression patterns in the data. As said above, we use the `logcounts` and then set `scale_features` to TRUE in order to scale each gene. ```{r} #runPCA and specify the variable genes to use for dim reduction with subset_row sce <- runPCA(sce, exprs_values = "logcounts", ncomponents = 50, subset_row = hvg.out, scale = TRUE) ``` We then plot the first principal components. ```{r, fig.asp=.28} plot_grid(ncol = 3, plotReducedDim(sce,dimred = "PCA",colour_by = "sample",ncomponents = 1:2,point_size = 0.6), plotReducedDim(sce,dimred = "PCA",colour_by = "sample",ncomponents = 3:4,point_size = 0.6), plotReducedDim(sce,dimred = "PCA",colour_by = "sample",ncomponents = 5:6,point_size = 0.6) ) ``` To identify which genes (Seurat) or metadata paramters (Scater/Scran) contribute the most to each PC, one can retreive the loading matrix information. Unfortunatelly this is not implemented in Scater/Scran, so you will need to compute PCA using `logcounts`. ```{r,fig.asp=.4} plot_grid(ncol = 2, plotExplanatoryPCs(sce)) ``` We can also plot the amount of variance explained by each PC. ```{r,fig.width=5,fig.height=5} mypar() plot(attr(reducedDim(sce,"PCA"),"percentVar")[1:50]*100,type="l",ylab="% variance",xlab="Principal component #") points(attr(reducedDim(sce,"PCA"),"percentVar")[1:50]*100,pch=21,bg="grey",cex=.5) ``` Based on this plot, we can see that the top 8 PCs retain a lot of information, while other PCs contain pregressivelly less. However, it is still advisable to use more PCs since they might contain informaktion about rare cell types (such as platelets and DCs in this dataset) ## tSNE *** We can now run [BH-tSNE](https://arxiv.org/abs/1301.3342). ```{r, fig.asp=1} set.seed(42) sce <- runTSNE(sce, dimred = "PCA", n_dimred = 30, perplexity = 30,name = "tSNE_on_PCA") ``` We can now plot the tSNE colored per dataset. We can clearly see the effect of batches present in the dataset. ```{r,fig.asp=.28} plot_grid(ncol = 3,plotReducedDim(sce,dimred = "tSNE_on_PCA",colour_by = "sample")) ``` ## UMAP *** We can now run [UMAP](https://arxiv.org/abs/1802.03426) for cell embeddings. ```{r} sce <- runUMAP(sce,dimred = "PCA", n_dimred = 30, ncomponents = 2,name = "UMAP_on_PCA") #see ?umap and ?runUMAP for more info ``` Another usefullness of UMAP is that it is not limitted by the number of dimensions the data cen be reduced into (unlike tSNE). We can simply reduce the dimentions altering the `n.components` parameter. ```{r} sce <- runUMAP(sce,dimred = "PCA", n_dimred = 30, ncomponents = 10, name = "UMAP10_on_PCA") #see ?umap and ?runUMAP for more info ``` We can now plot the UMAP colored per dataset. Although less distinct as in the tSNE, we still see quite an effect of the different batches in the data. ```{r,fig.asp=.28} plot_grid(ncol = 3, plotReducedDim(sce,dimred = "UMAP_on_PCA",colour_by = "sample")+ ggplot2::ggtitle(label ="UMAP_on_PCA"), plotReducedDim(sce,dimred = "UMAP10_on_PCA",colour_by = "sample",ncomponents = 1:2)+ ggplot2::ggtitle(label ="UMAP10_on_PCA"), plotReducedDim(sce,dimred = "UMAP10_on_PCA",colour_by = "sample",ncomponents = 3:4)+ ggplot2::ggtitle(label ="UMAP10_on_PCA") ) ``` ## Using ScaledData and graphs for DR *** Althought running a sencond dimmensionality reduction (i.e tSNE or UMAP) on PCA would be a standard approach (because it allows higher computation efficiency), the options are actually limiteless. Below we will show a couple of other common options such as running directly on the scaled data (which was used for PCA) or on a graph built from scaled data. We will show from now on only UMAP, but the same applies for tSNE. ### Using ScaledData for UMAP To run tSNE or UMAP on the scaled data, one firts needs to select the number of variables to use. This is because including dimentions that do contribute to the separation of your cell types will in the end mask those differences. Another reason for it is because running with all genes/features also will take longer or might be computationally unfeasible. Therefore we will use the scaled data of the highly variable genes. ```{r,fig.asp=.28} sce <- runUMAP(sce, exprs_values='logcounts',name = "UMAP_on_ScaleData") ``` To run tSNE or UMAP on the a graph, we first need to build a graph from the data. In fact, both tSNE and UMAP first build a graph from the data using a specified distance metrix and then optimize the embedding. Since a graph is just a matrix containing distances from cell to cell and as such, you can run either UMAP or tSNE using any other distance metric desired. Euclidean and Correlation are ususally the most commonly used. ### Using a Graph for UMAP ```{r,fig.asp=.28} #Build Graph nn <- RANN::nn2(reducedDim(sce,"PCA"),k = 30) names(nn) <- c("idx","dist") g <- buildKNNGraph(sce, k=30, use.dimred="PCA") reducedDim(sce,"KNN") <- igraph::as_adjacency_matrix(g) #Run UMAP and rename it for comparisson # temp <- umap::umap.defaults try(reducedDim(sce,"UMAP_on_Graph") <- NULL) reducedDim(sce,"UMAP_on_Graph") <- uwot::umap(X=NULL, n_components = 2, nn_method=nn) ``` We can now plot the UMAP comparing both on PCA vs ScaledSata vs Graph. ```{r,fig.asp=.28} plot_grid(ncol = 3, plotReducedDim(sce, dimred = "UMAP_on_PCA", colour_by = "sample")+ ggplot2::ggtitle(label ="UMAP_on_PCA"), plotReducedDim(sce, dimred = "UMAP_on_ScaleData", colour_by = "sample")+ ggplot2::ggtitle(label ="UMAP_on_ScaleData"), plotReducedDim(sce, dimred = "UMAP_on_Graph", colour_by = "sample")+ ggplot2::ggtitle(label ="UMAP_on_Graph") ) ``` ## Ploting genes of interest *** Let's plot some marker genes for different celltypes onto the embedding. Some genes are: Markers | Cell Type --- | --- CD3E | T cells CD3E CD4 | CD4+ T cells CD3E CD8A | CD8+ T cells GNLY, NKG7 | NK cells MS4A1 | B cells CD14, LYZ, CST3, MS4A7 | CD14+ Monocytes FCGR3A, LYZ, CST3, MS4A7 | FCGR3A+ Monocytes FCER1A, CST3 | DCs ```{r,fig.asp=.9} plotlist <- list() for(i in c("CD3E","CD4","CD8A","NKG7","GNLY","MS4A1","CD14","LYZ","MS4A7","FCGR3A","CST3","FCER1A")){ plotlist[[i]] <- plotReducedDim(sce,dimred = "UMAP_on_PCA",colour_by = i,by_exprs_values = "logcounts") + scale_fill_gradientn(colours = colorRampPalette(c("grey90","orange3","firebrick","firebrick","red","red" ))(10)) + ggtitle(label = i)+ theme(plot.title = element_text(size=20)) } plot_grid(ncol=3, plotlist = plotlist) ``` We can finally save the object for use in future steps.

**Your turn**
Select some of your dimensionality reductions and plot some of the QC stats that were calculated in the previous lab. Can you see if some of the separation in your data is driven by quality of the cells?

```{r}
saveRDS(sce,"data/results/covid_qc_dm.rds")
```
### Session Info
***
```{r}
sessionInfo()
```