-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathglobal.R
74 lines (66 loc) · 2.07 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
# library(gdata)
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)
# 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)
}
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"))
}
return(umap)
}
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)
}