forked from NSafarian/Faiz_StrokeProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript2_Overlay_scRNAseq_onto_Visium.Rmd
86 lines (60 loc) · 2.92 KB
/
Script2_Overlay_scRNAseq_onto_Visium.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
title: "Faiz_Strokedata_Part2_Overlay_scRNAseq_on_Visium"
author: "NS"
date: '2022-03-10'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Load Libraries:
```{r, echo=FALSE}
library(Seurat)
library(tidyverse)
library(ggplot2)
library(patchwork)
library(dplyr)
library(hdf5r)
```
## Import the day10_Visium_data:
```{r setup, include=FALSE}
#Define the path to the visium data directory
data_dir_d10 <- "/path/to/data/directory"
#List the files
list.files(data_dir_d10, all.files = TRUE)
#Read the spatial data using Seurat
FV.d10 <- Load10X_Spatial(data.dir = data_dir_d10)
#Normalize the data (sctransform normalizes the data, detects high-variance features, and stores the data in the SCT assay)
FV.d10 <- SCTransform(FV.d10, assay = "Spatial", verbose = FALSE)
#Dimensionality reduction, clustering, and visualization
FV.d10<- RunPCA(FV.d10, assay = "SCT", verbose = FALSE)
FV.d10<- FindNeighbors(FV.d10, reduction = "pca", dims = 1:30)
FV.d10<- FindClusters(FV.d10, verbose = FALSE)
FV.d10<- RunUMAP(FV.d10, reduction = "pca", dims = 1:30)
#Visualize the results of the clustering
P1.d10 <- DimPlot(FV.d10, reduction = "umap", label = TRUE)
P2.d10 <- SpatialDimPlot(FV.d10, label = TRUE, label.size = 3)
P1.d10 + P2.d10
```
## Integration Visium data with the scRNA seqdata:
```{r setup, include=FALSE}
#1) Define the reference and query datasets
query1 = FV.d10
ref1 = seu.combined
#2) Pre-process the scRNA-seq reference (note that setting ncells=3000 normalizes the full dataset but learns noise models on 3k cells, this speeds up SCTransform dramatically with no loss in performance)
ref1 <- SCTransform(ref1, ncells = 3000, verbose = FALSE) %>%
RunPCA(verbose = FALSE) %>%
RunUMAP(dims = 1:10)
#3) Find transfer anchors
anchors.1 <- FindTransferAnchors(reference = ref1, query = query1, normalization.method = "SCT")
#4) Transfer the identified labels/anchors (The procedure outputs, for each spot, a probabilistic classification for each of the scRNA-seq derived classes)
predictions.1 <- TransferData(anchorset = anchors.1, refdata = ref1$seurat_clusters, prediction.assay = TRUE, weight.reduction = query1[["pca"]], dims = 1:30) # note that you need to use 'refdata = ref1$new_clusters' for plotting with the corrected cluster.id. Please check Script 3 for more information.
#5) We add these predictions as a new assay in the Seurat object.
query1[["predictions"]] <- predictions.1
#Now we get prediction scores for each spot for each class.
DefaultAssay(query1) <- "predictions"
#6) Visualize (see how scRNA-seq driven cluster map onto the Visium dataset)
## this code is also presented in script 3, as 'Figure 2-part F'
SpatialFeaturePlot(query1,features = c( "1","2","3","4","5","6","7", "8") , # for cluster.no starting from 1 please check script-3
pt.size.factor = 1.4, ncol = 4, crop = TRUE)
```