-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.R
87 lines (72 loc) · 2.81 KB
/
utils.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
# Libraries
library(AnnotationDbi)
library(org.Mm.eg.db)
library(org.Hs.eg.db)
library(topGO)
# Enrichment database
anno <- AnnotationDbi::select(org.Hs.eg.db,keys = keys(org.Hs.eg.db,keytype = "SYMBOL"),columns = c("ENSEMBL","SYMBOL","GENENAME"),keytype = "SYMBOL")
bg <- unique(anno$SYMBOL)
# Reactable functions
render.reactable.cell.with.tippy <- function(text,tooltip){
div(
style = "text-decoration: underline;
text-decoration-style: dotted;
text-decoration-color: #FF6B00;
cursor: info;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;",
tippy(text = text,tooltip = tooltip)
)
}
dataListFilter <- function(tableId,style = "width: 100%; height: 28px;") {
function(values,name) {
dataListId <- sprintf("%s-%s-list",tableId,name)
tagList(
tags$input(
type = "text",
list = dataListId,
oninput = sprintf("Reactable.setFilter('%s', '%s', event.target.value || undefined)",tableId,name),
"aria-label" = sprintf("Filter %s",name),
style = style
),
tags$datalist(
id = dataListId,
lapply(unique(values),function(value) tags$option(value = value))
)
)
}
}
bar_chart <- function(label,width = "100%",height = "1rem",fill = "#00bfc4",background = NULL) {
bar <- div(
style = list(background = fill,width = width,height = height)
)
chart <- div(
style = list(flexGrow = 1,marginLeft = "0.5rem",background = background),
bar
)
div(
style = list(display = "flex",alignItems = "center"),
label,
chart
)
}
# GO enrichment function
Run_enrichment <- function(sig_genes,anno) {
an_sig <- as.data.frame(subset(anno,SYMBOL %in% sig_genes))
in_universe <- bg %in% c(an_sig$SYMBOL,bg)
in_selection <- bg %in% an_sig$SYMBOL
alg <- factor(as.integer(in_selection[in_universe]))
names(alg) <- bg[in_universe]
tgd <- new("topGOdata",ontology = "BP",allGenes = alg,nodeSize = 5,annot = annFUN.org,mapping = "org.Mm.eg.db",ID = "symbol")
result_topgo <- runTest(tgd,algorithm = "classic",statistic = "Fisher")
all_go <- usedGO(tgd)
go_results <- GenTable(tgd,Fisher.classic = result_topgo,orderBy = "Fisher.classic",topNodes = length(all_go),numChar = 10000)
go_results$Fisher.classic <- ifelse(grepl("<",go_results$Fisher.classic),0,go_results$Fisher.classic)
go_results$Fisher.classic <- round(p.adjust(go_results$Fisher.classic,method = "BH"),digits = 4)
go_results <- go_results[go_results$Fisher.classic < 0.1,]
names(go_results) <- c("Term Id","Term label","# Reference","# Genes","Fold enrichment","Adj. p-value")
go_results$`Adj. p-value` <- round(go_results$`Adj. p-value`,3)
go_results$`Fold enrichment` <- round(go_results$`Fold enrichment`,3)
return(go_results)
}