-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trajectory Workflow.Rmd
236 lines (152 loc) · 5.91 KB
/
Trajectory Workflow.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
---
title: "Seurat Trajectory Analyis"
output:
html_document:
df_print: paged
pdf_document: default
---
```{r,warning=FALSE,message=FALSE,echo=FALSE}
library(Seurat)
library(dplyr)
library(tidyverse)
library(SeuratWrappers)
library(monocle3)
library(ggplot2)
```
# Reading the Seurat object
```{r,warning=FALSE,message=FALSE,echo=FALSE}
IPF_processed <- readRDS(r"(D:\A\App\processed_data.rds)")
```
```{r,warning=FALSE,message=FALSE}
identity_labels <- IPF_processed$orig.ident
unique_identity_labels <- unique(identity_labels)
print(unique_identity_labels)
cluster_labels <- IPF_processed$seurat_clusters
clus_identity_labels <- unique(cluster_labels)
print(clus_identity_labels)
[email protected][["ident"]] <[email protected]
```
# **Step 1: convert to seurat object to celll data object**
```{r,warning=FALSE,message=FALSE,echo=FALSE}
cds <- as.cell_data_set(IPF_processed,group.by = 'ident')
```
```{r,warning=FALSE,message=FALSE,echo=FALSE}
# changing the assay name
DefaultAssay(IPF_processed) <- "RNA"
# setting gene annotaion
fData(cds)$gene_short_name <- rownames(fData(cds))
```
# **step 2 Cluster cells (using clustering info from seurat's UMAP)**
## Assing all the cells to one Partion
```{r,warning=FALSE,message=FALSE,echo=FALSE}
recreate.partion <- c(rep(1,length(cds@colData@rownames)))
names(recreate.partion) <- cds@colData@rownames
recreate.partion <- as.factor(recreate.partion)
cds@clusters$UMAP$partitions <- recreate.partion
```
## Assiging the cluster information
```{r,warning=FALSE,message=FALSE,echo=FALSE}
list_cluster <- [email protected]
cds@clusters$UMAP$clusters <- list_cluster
```
## Assign UMAP coordiantes - cell embeddings
```{r,warning=FALSE,message=FALSE,echo=FALSE}
cds@int_colData@listData$reducedDims$UMAP <- IPF_processed@[email protected]
```
# **Plot the cluster before learning the trajectory**
```{r,warning=FALSE,message=FALSE}
cluster_no_before <- plot_cells(cds,color_cells_by = 'seurat_clusters',
label_groups_by_cluster = FALSE,
group_label_size = 5)+
theme(legend.position = "right")
cluster_before <- plot_cells(cds,color_cells_by = 'ident',
label_groups_by_cluster = FALSE,
group_label_size = 5)+
theme(legend.position = "right")
print(cluster_before)
print(cluster_no_before)
```
# **step3 Learning the Trajectory **
```{r,warning=FALSE,message=FALSE,echo=FALSE}
cds <- learn_graph(cds, use_partition = FALSE)
```
# **Plot the cluster After learning the trajectory**
```{r,warning=FALSE,message=FALSE}
# plot after learning
cluster_no_after<- plot_cells(cds,color_cells_by = 'seurat_clusters',
label_groups_by_cluster = FALSE,
group_label_size = 5)+
theme(legend.position = "right")
cluster_after <- plot_cells(cds,color_cells_by = 'ident',
label_groups_by_cluster = FALSE,
group_label_size = 5)+
theme(legend.position = "right")
print(cluster_no_after)
print(cluster_after)
```
```{r,warning=FALSE,message=FALSE,echo=FALSE}
# getting the unique cluster names
cluster_names <- as.character(clusters(cds))
unique_clusters <- unique(cluster_names)
print(unique_clusters)
```
# **Step3 Order the cells based on the pseudotime analysis**
```{r,warning=FALSE,message=FALSE}
#setting the root to get the pseudotime , here ["Adventitia"] as taken root
cds <- order_cells(cds,reduction_method = 'UMAP',root_cells = colnames(cds[,clusters(cds) == "Alveolar"]))
```
# **Plot based on the oreder of cells of Pseudotime**
```{r,warning=FALSE,message=FALSE,echo=FALSE}
a<-plot_cells(cds,color_cells_by = 'pseudotime',
label_groups_by_cluster = FALSE,
label_branch_points = FALSE,
label_roots = FALSE,
group_label_size = 5)+
theme(legend.position = "right")
b <-plot_cells(cds,color_cells_by = 'pseudotime',
label_groups_by_cluster = FALSE,
label_branch_points = FALSE,
label_roots = FALSE,
group_label_size = 5)+
theme(legend.position = "right")
print(a)
print(b)
```
# **step4 Cells oredered by monocle3 pseudotime and visulaize the range of pseudotime**
```{r,warning=FALSE,message=FALSE,echo=FALSE}
cds$molocle3_pseudotime <- pseudotime(cds)
data.pseudo <- as.data.frame(colData(cds))
```
# **Box plot based on the Median value of Pseudotime**
```{r,warning=FALSE,message=FALSE,echo=FALSE}
boxplot <- ggplot(data.pseudo, aes(x = molocle3_pseudotime, y = reorder(ident, molocle3_pseudotime, FUN = median), fill = ident)) +
geom_boxplot() +
labs(title = "Boxplot of molocle3_pseudotime by ident",
x = "molocle3_pseudotime",
y = "ident") +
theme_minimal()
print(boxplot)
```
# **step 4 To find the genes that expressed as the moves in the trajectory **
```{r,warning=FALSE,message=FALSE,echo=FALSE}
cds <- estimate_size_factors(cds)
## Add gene names into CDS
cds@rowRanges@elementMetadata@listData[["gene_short_name"]] <- rownames(cds[["RNA"]])
Differenial_expressed_genes<- graph_test(cds,neighbor_graph = "principal_graph",cores = 3)
#View(Differenial_expressed_genes)
differential_genes_filtered <-Differenial_expressed_genes %>%
arrange(q_value) %>%
filter(status=="OK")
```
```{r}
head(differential_genes_filtered)
```
# **Ploting the Diffrential Expressed genes
```{r,warning=FALSE,message=FALSE,echo=FALSE}
FeaturePlot(IPF_processed,features = c("C1QC","ARHGAP30","CCM2L","SOX7"))
```
#**visuaalizion pseudotime in seurat**
```{r,warning=FALSE,message=FALSE,echo=FALSE}
IPF_processed$pseudotime <- pseudotime(cds)
FeaturePlot(IPF_processed,features ="pseudotime",,label = T )
```