-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00_Custom_Functions_CTC.R
More file actions
172 lines (148 loc) · 6.8 KB
/
00_Custom_Functions_CTC.R
File metadata and controls
172 lines (148 loc) · 6.8 KB
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
library(ComplexHeatmap)
library(knitr)
#library(circlize)
#library(colorspace)
#library(GetoptLong)
#library(psych)
#library(FSA)
library(tidyverse)
library(sva)
library(limma)
library(Biobase)
library(patchwork)
library(data.table)
library(ggrepel)
library(DESeq2)
library(DT)
library(EnhancedVolcano)
#library(dplyr)
#library(ggforce)
library(ggplot2)
library(webshot)
# FUNCTION: TURNING P VALUES INTO ASTERISKS
p_to_asterisk <- function(p_val) {
if (p_val < 0.00001) {
return("*****")
} else if (p_val < 0.0001) {
return("****")
} else if (p_val < 0.001) {
return("***")
} else if (p_val < 0.01) {
return("**")
} else if (p_val < 0.05) {
return("*")
} else {
return("ns")
}
}
# FUNCTION: volcanoPlot() ####
# This function generates volcano plots and from the output of differential expression analysis FOR DESEQ2 NOT LIMMA
# input = limma/TopTable output, title_1/2 = string for the groups in order that they appear in the contrast (i.e. lvi vs stroma), batch = string
volcanoPlot <- function(input, title_1, title_2, batch, upcolor = "#619CFF", downcolor = "#F8766D", lfcthresh = 0.58, directory = "/Users/alexanderbell/OneDrive - Johns Hopkins/GeoMx_Chianchiano_Project/March_2023_Ting_CTC/outputs/") {
# Making uppers/mids/downers
data <- input %>%
mutate(neg.log.padj = -1*log10(padj)) %>%
filter(!is.na(neg.log.padj)) %>%
mutate(topstat = neg.log.padj * abs(log2FoldChange * 2))
#data$rn <- rownames(data)
#data
Uppers <- data %>%
filter((log2FoldChange >= lfcthresh) & (neg.log.padj >= 1.3)) %>%
mutate(Group = 'Uppers')
Mids <- data %>%
filter(((log2FoldChange < lfcthresh) & (log2FoldChange >-lfcthresh)) | (neg.log.padj < 1.3)) %>%
mutate(Group = 'Mids')
Lowers <- data %>%
filter((log2FoldChange <= -lfcthresh) & (neg.log.padj >= 1.3)) %>%
mutate(Group = 'Lowers')
# Topstat for labeling volcano plot
inUpperstopstat = character()
inLowerstopstat = character()
inMidstopstat = character ()
#Uppers
for(x in 1:nrow(Uppers)) {
if(round(Uppers$topstat[x]) %in% round(sort(Uppers$topstat, decreasing = TRUE)[1:6])) {
inUpperstopstat <- append(inUpperstopstat, TRUE) }
else{
inUpperstopstat <- append(inUpperstopstat, FALSE)
}
}
#Lowers
for(x in 1:nrow(Mids)){
inMidstopstat <- append(inMidstopstat, FALSE)
}
for(x in 1:nrow(Lowers)) {
if(round(Lowers$topstat[x]) %in% round(sort(Lowers$topstat, decreasing = TRUE)[1:6])) {
inLowerstopstat <- append(inLowerstopstat, TRUE) }
else{
inLowerstopstat <- append(inLowerstopstat, FALSE)
}
}
midupp <- append(inUpperstopstat, inMidstopstat)
tops <- append(midupp, inLowerstopstat)
Volcano_groups <- rbind(Uppers,Mids)
Volcano_groups2 <- rbind(Volcano_groups,Lowers)
Volcano_groups3 <- cbind(Volcano_groups2, tops)
print(Volcano_groups3)
setDT(Volcano_groups3, keep.rownames = TRUE)[]
#Making DEGs list
degs <- rbind(Uppers, Lowers)
degs <- degs %>% mutate(statistic = (log2FoldChange/abs(log2FoldChange))*neg.log.padj)
degs_ordered <- degs[order(degs$statistic, decreasing = TRUE),]
setDT(degs_ordered, keep.rownames = TRUE)[]
degs_ordered %>% dplyr::select(c('rn', 'statistic')) -> degsforgsea
degs_ordered %>% dplyr::select(c('rn', 'log2FoldChange', 'baseMean', "pvalue", "padj", "neg.log.padj", "Group")) -> degslist
colnames(degslist)[1] <- "Genes"
# making gsea lists for all genes ranked either by LFC or "statistic
allgenes <- rbind(Uppers, Mids, Lowers)
allgenes <- allgenes %>% mutate(statistic = (log2FoldChange/abs(log2FoldChange))*neg.log.padj)
setDT(allgenes, keep.rownames = TRUE)[]
allgenes_ranked_by_statistic <- allgenes[order(allgenes$statistic, decreasing = TRUE),]
allgenes_ranked_by_LFC <- allgenes[order(allgenes$log2FoldChange, decreasing = TRUE),]
allgenes_statistic_gsea <- allgenes_ranked_by_statistic %>% dplyr::select(c('rn', 'statistic'))
allgenes_LFC_gsea <- allgenes_ranked_by_LFC %>% dplyr::select(c('rn', 'statistic'))
# all genes
write.table(Volcano_groups3, file = paste0(directory, batch,"_", title_1, "vs", title_2, "_all_genes.csv"), row.names = TRUE, col.names = TRUE, sep = ',')
# just degs, gsea formatted
write.table(degsforgsea, file = paste0(directory, batch,"_", title_1, "vs", title_2, "_gseadegs.csv"), row.names = TRUE, col.names = TRUE, sep = ',')
# just degs
write.table(degslist, file = paste0(directory, batch, "_", title_1, "vs", title_2, "_DEGS.csv"), row.names = TRUE, col.names = TRUE, sep = ',')
# all genes, gsea formatted, ranked by "statistic" (Aka the neg.log.fold change and direction)
write.table(allgenes_statistic_gsea, file = paste0(directory, batch, "_", title_1, "vs", title_2, "_gsea_allgenes_rankedstatistic.csv"), row.names = TRUE, col.names = TRUE, sep = ',')
# all genes, gsea formatted, ranked by LFC
write.table(allgenes_LFC_gsea, file = paste0(directory, batch, "_", title_1, "vs", title_2, "_gsea_allgenes_rankedLFC.csv"), row.names = TRUE, col.names = TRUE, sep = ',')
volcano_plot <- ggplot(Volcano_groups3, mapping = aes(x = log2FoldChange, y=neg.log.padj, label = rn)) +
geom_point(mapping = aes(color = Group), size = 1) +
xlab(expression(paste(log[2], " fold change"))) +
ylab(expression(paste(-log[10],"(q)"))) +
geom_text_repel(data = . %>%
mutate(label = ifelse(Group %in% c("Uppers", "Lowers") & tops == TRUE,
rn, "")),
aes(label = label),
show.legend = FALSE,
max.overlaps = Inf,
force = 2,
box.padding = 0.2,
fontface = "italic") +
ggtitle(paste0(title_1," versus", title_2)) +
scale_color_manual(name = "Groups", labels = c(paste0("Increased in ", title_2, ": ", nrow(Lowers)), paste0("Unchanged: ", nrow(Mids)), paste0("Increased in ", title_1, ": ", nrow(Uppers))),
values = c(downcolor, "#CBCBCB" , upcolor )) +
theme_bw()+
theme(text = element_text(family = "Arial", color = "#2C2C2E"),
plot.title = element_text(size = 10, hjust = 0.5, face = "bold"),
legend.title = element_blank(),
axis.text=element_text(size=10),
axis.title=element_text(size=10,face="bold"),
legend.text = element_text(size=10),
legend.position=c(1.02,0.5),
legend.justification=c(0, 1),
legend.key.width=unit(1, "lines"),
legend.key.height=unit(1, "lines"),
plot.margin = unit(c(1, 13, 0.5, 0.5), "lines"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
geom_hline(yintercept = 1.3) +
geom_vline(xintercept = c(-lfcthresh,lfcthresh))
volcano_plot
ggsave(file = paste0(directory,batch,"_",title_1,"vs",title_2,".pdf"), device = cairo_pdf)
}