-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfigs.R
103 lines (86 loc) · 3.24 KB
/
figs.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
library(dplyr)
library(maftools)
library(VennDiagram)
library(reshape2)
library(ggplot2)
library(readr)
library(tibble)
library(stringr)
# https://drsimonj.svbtle.com/creating-corporate-colour-palettes-for-ggplot2
ch_palettes <- list(
`flat` = c("#5380a2", "#15203a", "#327138", "#d05c39", "#d79e35"),
`hl` = c("#d11141", "#858585", "#15304A")
)
ch_pal <- function(palette = "hl", reverse = FALSE, ...) {
pal <- ch_palettes[[palette]]
if (reverse) pal <- rev(pal)
colorRampPalette(pal, ...)
}
# get Tumor_Sample_Barcode
tsb <- function(m){ m@data$Tumor_Sample_Barcode }
p_plots <- function(mutsig_p, cancer_names, gene_names, main = 'MutSigCV sigificance', pal_name = 'hl', pq = 'p', lvls = NULL){
if(pq == 'p'){xlab = expression(-log[10](p-value))
}else{xlab = expression(-log[10](q-value))}
mutsig_p %>%
melt() %>%
mutate(Gene = rep(factor(goi, ordered = T), ncol(mutsig_p)),
cancer = cancer_names$brief[match(variable, cancer_names$abbrev)],
value = pmin(-log10(value), 5)) -> df
if (is.null(lvls)){
df %>%
subset(Gene %in% c('PIK3CA', 'PTEN', 'MTOR')) %>%
arrange(desc(value), desc(Gene)) %>%
select(cancer) %>% unique() %>% unlist() -> lvls
}
df %>%
subset(Gene %in% gene_names) %>%
mutate(cancer = ordered(cancer, levels = rev(lvls))) %>%
ggplot(aes(x=value, y=cancer, fill = Gene)) +
geom_bar(position="dodge", stat="identity") +
geom_vline(xintercept = -log10(0.05/length(lvls)), size=0.2, linetype="dashed") +
annotate('text', y = 1, x = -log10(0.05/length(lvls)) + 0.65,
label = paste0(pq, ' = 0.05 / ', length(lvls) ), size = 3.5) +
#geom_vline(xintercept = -log10(0.1/length(lvls)), size=0.2, linetype="dashed") +
#annotate('text', y = 1, x = -log10(0.1/length(lvls)) + 0.5,
# label = paste0(pq, ' = 0.1 / ', length(lvls) ), size = 3.5) +
labs(title= main, y = '', x = xlab ) +
discrete_scale("fill", '', palette = ch_pal(pal_name)) +
theme_classic() +
xlim(0, 5) +
theme(axis.text.y = element_text(colour = "black", size = 10)) %>%
return()
}
# bonferroni correct pvalues
bonferroni <- function(p_mat, axis=1){
p_mat <- p_mat * dim(p_mat)[axis]
p_mat[p_mat >1] <- 1
return(p_mat)
}
is.driver <- function(str){
return ( !is.na(str) & (grepl('driver', str) | grepl('_rec', str)) )
}
mut_venn <- function(df, cex = 2, cat.dist = c(0.06, 0.06, 0.06), cat.pos = c(-27, 27, 135),
main = ''){
venn.diagram(
x = list(colnames(df)[as.logical(colSums(df[grepl('PIK3CA', rownames(df)),])) ],
colnames(df)[as.logical(colSums(df[grepl('PTEN', rownames(df)),])) ],
colnames(df)[as.logical(colSums(df[grepl('MTOR', rownames(df)),])) ]),
category.names = c("PIK3CA", "PTEN", "MTOR"),
filename = NULL,
output = TRUE ,
col= rev(ch_pal('hl')(3)),
fill = alpha( rev(ch_pal('hl')(3)), 0.3 ),
cex = cex,
ext.text = T,
fontfamily = "sans",
cat.cex = 2,
cat.default.pos = "outer",
cat.pos = cat.pos,
cat.dist = cat.dist,
cat.fontfamily = "sans",
rotation = 1,
main = paste0(main, " (n=", sum(colSums(df)>0), ")"),
main.cex = 1,
main.fontfamily = 'sans'
) %>% grid.draw()
}