-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.R
277 lines (190 loc) · 6.95 KB
/
global.R
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
library(tools)
library(Seurat)
library(dplyr)
library(shinyjs)
library(shiny)
library(DT)
library(shinydashboard)
library(shinydashboardPlus)
library(ggplot2)
library(shinybusy)
library(glue)
library(markdown)
library(ggthemes)
library(plotly)
library(monocle3)
library(dplyr)
library(tidyverse)
library(SeuratWrappers)
library(SingleCellExperiment)
# Read in file and perform validation.
load_seurat_obj <- function(path){
errors <- c()
# check file extension
if (!tolower(tools::file_ext(path)) == "rds") { # ignores case
errors <- c(errors, "Invalid rds file.")
return(errors)
}
# try to read in file
tryCatch(
{
obj <- readRDS(path)
},
error = function(e) {
errors <- c(errors, "Invalid rds file.")
return(errors)
}
)
# Validate obj is a seurat object
if (!inherits(obj, "Seurat")){
errors <- c(errors, "File is not a seurat object")
return(errors)
}
return(obj)
}
#2d viloen plot
violen_plot_gene <- function(obj, gene) {
if (gene %in% rownames(obj)) {
vp <- VlnPlot(obj, features = gene)
} else {
vp <- ggplot() +
theme_void() +
VlnPlot(obj, features = gene) +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
}
return(vp)
}
# dimplot
generate_dim_plot <- function(obj, group.by) {
dim_plot <- DimPlot(obj, reduction = "umap", group.by = group.by,label =TRUE)
return(dim_plot)
}
#2d viloen plot
rp_plot_gene <- function(obj, gene) {
if (gene %in% rownames(obj)) {
rp <- RidgePlot(obj, features = gene)
} else {
rp <- ggplot() +
theme_void() +
RidgePlot(obj, features = gene) +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
}
return(rp)
}
#function without trajectory
create_plot_indent <- function(obj) {
cluster_plot <- plot_cells(obj, color_cells_by = "ident", label_groups_by_cluster = FALSE, group_label_size = 5) +
theme(legend.position = "right")
return(cluster_plot)
}
create_plot_clusters <- function(obj) {
cluster_plot <- plot_cells(obj, color_cells_by = "seurat_clusters", label_groups_by_cluster = FALSE, group_label_size = 5) +
theme(legend.position = "right")
return(cluster_plot)
}
# function for trajectory learning
learning_trajectories <- function(obj) {
[email protected][["ident"]] <- [email protected]
cds <- as.cell_data_set(obj, group.by = "ident")
DefaultAssay(obj) <- "RNA"
fData(cds)$gene_short_name <- rownames(fData(cds))
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
list_cluster <- [email protected]
cds@clusters$UMAP$clusters <- list_cluster
# Assign UMAP coordinates - cell embeddings
cds@int_colData@listData$reducedDims$UMAP <- obj@[email protected]
cds <- learn_graph(cds, use_partition = FALSE)
return(cds)
}
create_trajectory_plot_clusters <- function(obj) {
cluster_plot <- plot_cells(obj, color_cells_by = "seurat_clusters", label_groups_by_cluster = FALSE, group_label_size = 5) +
theme(legend.position = "right")
return(cluster_plot)
}
create_trajectory_plot_indent <- function(obj) {
cluster_plot <- plot_cells(obj, color_cells_by = "ident", label_groups_by_cluster = FALSE, group_label_size = 5) +
theme(legend.position = "right")
return(cluster_plot)
}
# for Pseudotime analysis
pseudotime_analysis <- function(cds) {
# Order the cells based on the pseudotime analysis
root_cells <- colnames(cds[, clusters(cds) == "Adventitia"])
cds <- order_cells(cds, reduction_method = 'UMAP', root_cells = root_cells)
return(cds)
}
# getting the pseudataframe for box plot
pseudotime_df <-function(pseudo_obj){
pseudo_obj$molocle3_pseudotime <- pseudotime(pseudo_obj)
data.pseudo <- as.data.frame(colData(pseudo_obj))
return(data.pseudo)
}
# estimate size factors
estimate_size <- function(cds){
cds <- estimate_size_factors(cds)
cds@rowRanges@elementMetadata@listData[["gene_short_name"]] <- rownames(cds[["RNA"]])
return(cds)
}
# getting the diffrential genes
df_genes <- function(cds){
Differenial_expressed_genes<- graph_test(cds,neighbor_graph = "principal_graph",cores = 3)
differential_genes_filtered <-Differenial_expressed_genes %>%
arrange(q_value) %>%
filter(status=="OK")
}
#creating the UMAP
create_metadata_UMAP <- function(obj, col){
if (col %in% c("nCount_RNA", "nFeature_RNA", "percent.mt")){
col_df <- data.frame(obj@[email protected], data = [email protected][,col])
umap <- ggplot(data = col_df) +
geom_point(mapping = aes(umap_1, umap_2, color = log10(data)), size = 0.01) +
scale_colour_gradientn(colours = rainbow(7))
} else if (col %in% colnames([email protected])) {
umap <- DimPlot(obj, pt.size = .1, label = F, label.size = 4, group.by = col, reduction = "umap")
} else {
umap <- ggplot() +
theme_void() +
geom_text(aes(x = 0.5, y = 0.5, label = "col doesn't exist"), size = 20, color = "gray73", fontface = "bold") +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
}
class_of_umap <- class(umap)
print(class_of_umap)
return(umap)
}
#create feature plot
create_feature_plot <- function(obj, gene) {
if (gene %in% rownames(obj)) {
FP <- FeaturePlot(obj, features = gene, pt.size = 0.001, combine = FALSE)
} else {
FP <- ggplot() +
theme_void() +
geom_text(aes(x = 0.5, y = 0.5, label = "Gene doesn't exist"), size = 20, color = "gray73", fontface = "bold") +
theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
}
return(FP)
}
#create violin plot for genes
create_violen_plot <- function(obj, gene, col) {
if (gene %in% rownames(obj)) {
df <- [email protected]
vp <- plot_ly(data = df, y = ~df[[col]], x = obj@assays$RNA_Seq$data[, gene], type = "violin", box = list(visible = TRUE), line = list(color = 'black')) %>%
layout(title = gene)
} else {
vp <- plot_ly() %>%
layout(title = gene) %>%
layout(showlegend = FALSE) %>%
add_trace(type = "violin")
}
return(vp)
}
create_boxplot <- function(data, x_col, y_col,fill_v,a = median) {
ggplot(data, aes_string(x = x_col, y = reorder(y_col, fill =fill_v,FUN = a ))) +
geom_boxplot() +
labs(title = paste("Boxplot of", y_col, "by", x_col),
x = x_col,
y = y_col) +
theme_minimal()
}