-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPair-wise-comparison-analysis.qmd
More file actions
256 lines (205 loc) · 7.24 KB
/
Pair-wise-comparison-analysis.qmd
File metadata and controls
256 lines (205 loc) · 7.24 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
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
---
title: "Comparing DE Results - Pairwise"
author: "Harvard Chan Bioinformatics Core"
date: "`r Sys.Date()`"
format:
html:
code-fold: true
code-tools: true
code-overflow: wrap
df-print: paged
highlight-style: pygments
number-sections: true
self-contained: true
theme: default
toc: true
toc-location: right
toc-expand: false
lightbox: true
params:
project_file: ../information.R
---
```{r}
#| message: FALSE
#| warning: FALSE
# This set up the working directory to this file so all files can be found
library(rstudioapi)
library(tidyverse)
# NOTE: This code will check version, this is our recommendation, it may work
# . other versions
stopifnot(R.version$major >= 4) # requires R4
if (compareVersion(R.version$minor, "3.1") < 0) warning("We recommend >= R4.3.1")
stopifnot(compareVersion(as.character(BiocManager::version()), "3.18") >= 0)
```
This code is in this  revision.
```{r load_libraries}
#| cache: FALSE
#| message: FALSE
#| warning: FALSE
library(rtracklayer)
library(tidyverse)
library(stringr)
library(ggpubr)
library(knitr)
library(bcbioR)
library(ggprism)
library(viridis)
library(pheatmap)
library(janitor)
library(ggvenn)
library(ggplot2)
colors <- cb_friendly_cols(1:15)
ggplot2::theme_set(theme_prism(base_size = 14))
opts_chunk[["set"]](
cache = F,
cache.lazy = FALSE,
dev = c("png", "pdf"),
error = TRUE,
highlight = TRUE,
message = FALSE,
prompt = FALSE,
tidy = FALSE,
warning = FALSE,
echo = T,
fig.height = 4)
# set seed for reproducibility
set.seed(1234567890L)
```
# Compare two differential expression analysis
```{r load_params}
#| cache: FALSE
#| message: FALSE
#| warning: FALSE
# 1. Set up input files in this R file (params_pairwisecomp.R)
## Full results file (all genes) for contrastt 1
comp1_fn <- "https://raw.githubusercontent.com/bcbio/bcbioR-test-data/main/rnaseq/cross-comparison/all_results_DMSO_vs_Group1.csv.gz"
## Name of contrast 1. This will be displayed on the figures
comp1_name <- "DMSO vs. Group1"
## Full results file (all genes) for contrast 2
comp2_fn <- "https://raw.githubusercontent.com/bcbio/bcbioR-test-data/main/rnaseq/cross-comparison/all_results_DMSO_vs_Group2.csv.gz"
## Name of contrast 2. This will be displayed on the figures
comp2_name <- "DMSO vs. Group2"
## Adjusted P-value used for significance
padj_co <- 0.05
## Log2FC used for significance. If no cutoff used put 0
LFC <- 0.5
comp1 <- read_csv(comp1_fn) %>%
dplyr::filter(padj <= 1)
comp2 <- read_csv(comp2_fn) %>%
dplyr::filter(padj <= 1)
```
## Load Data
We load our dataset
```{r load_data}
# this code will load from bcbio or nf-core folder
# NOTE make sure to set numerator and denominator
comp1_sig <- comp1 %>%
dplyr::filter(padj < padj_co, abs(lfc) > LFC)
comp2_sig <- comp2 %>%
dplyr::filter(padj < padj_co, abs(lfc) > LFC)
```
# Comparisons
We start with a venn diagram looking at the overlap between our two contrasts
```{r}
#| fig-height: 8
#| fig-width: 8
name1 <- rlang::ensym(comp1_name)
name2 <- rlang::ensym(comp2_name)
names <- c(name1, name2)
full <- list(comp1_sig$gene_id, comp2_sig$gene_id)
names(full) <- names
ggvenn(full, show_percentage = F)
```
## Compare effect sizes and direction
We plot Log2FC for our contrasts and color points by whether or not they are significant in our contrasts. The black line is 1:1.
```{r fig.height=6}
#| fig-width: 8
# Edit based on the data you are using
# make sure to only use genes present in both results files
test_intersect <- intersect(comp1$gene_id, comp2$gene_id)
comp1_sub <- subset(comp1, comp1$gene_id %in% test_intersect)
comp2_sub <- subset(comp2, comp2$gene_id %in% test_intersect)
## Check that gene names match
all(comp1_sub$gene_id == comp2_sub$gene_id)
## Gather necessary data
lfc <- data.frame(comp1_sub$gene_id, comp1_sub$gene_name, comp1_sub$lfc, comp2_sub$lfc)
colnames(lfc) <- c("gene_id", "gene_name", "comp1", "comp2")
# subset to only include genes in both datasets and color by grouping
DE_comp1 <- setdiff(comp1_sig$gene_id, comp2_sig$gene_id)
DE_comp2 <- setdiff(comp2_sig$gene_id, comp1_sig$gene_id)
DE_both <- intersect(comp2_sig$gene_id, comp1_sig$gene_id)
not_sig <- comp1_sub$gene_id[!(comp1_sub$gene_id %in% c(DE_comp1, DE_comp2, DE_both))]
col <- rep(4, nrow(lfc))
col[lfc$gene_id %in% not_sig] <- 1
col[lfc$gene_id %in% DE_comp1] <- 2
col[lfc$gene_id %in% DE_comp2] <- 3
col[lfc$gene_id %in% DE_both] <- 4
lfc$col <- lfc %>%
dplyr::mutate(color = case_when(
gene_id %in% DE_both ~ 3,
gene_id %in% DE_comp1 ~ 1,
gene_id %in% DE_comp2 ~ 2,
gene_id %in% not_sig ~ 8
)) %>%
pull(color)
lfc$col <- as.factor(lfc$col)
ggplot(lfc, aes(x = comp1, y = comp2, color = col)) +
geom_point() +
labs(color = "Group") +
scale_color_discrete(name = "Group", labels = c(paste0("Only DE in ", paste0(comp1_name)), paste0("Only DE in ", paste0(comp2_name)), "DE in both comparisons", "Not Significant")) +
geom_abline(intercept = 0, slope = 1) +
geom_hline(aes(yintercept = 0)) +
geom_vline(aes(xintercept = 0)) +
scale_color_cb_friendly() +
xlab(paste0("Log2FC in ", paste0(comp1_name))) +
ylab(paste0("Log2FC in ", paste0(comp2_name)))
```
## Compare adjusted P-values
We plot adjusted P-values for our contrasts and color points by whether or not they are significant in our contrasts. The black line is 1:1.
```{r fig.height=6}
#| fig-width: 8
# Edit based on the data you are using
# make sure to only use genes present in both results files
test_intersect <- intersect(comp1$gene_id, comp2$gene_id)
comp1_sub <- subset(comp1, comp1$gene_id %in% test_intersect)
comp2_sub <- subset(comp2, comp2$gene_id %in% test_intersect)
## Check that gene names match
all(comp1_sub$gene_id == comp2_sub$gene_id)
## Gather necessary data
lfc <- data.frame(comp1_sub$gene_id, comp1_sub$gene_name, comp1_sub$padj, comp2_sub$padj)
colnames(lfc) <- c("gene_id", "gene_name", "comp1", "comp2")
# subset to only include genes in both datasets and color by grouping
DE_comp1 <- setdiff(comp1_sig$gene_id, comp2_sig$gene_id)
DE_comp2 <- setdiff(comp2_sig$gene_id, comp1_sig$gene_id)
DE_both <- intersect(comp2_sig$gene_id, comp1_sig$gene_id)
not_sig <- comp1_sub$gene_id[!(comp1_sub$gene_id %in% c(DE_comp1, DE_comp2, DE_both))]
col <- rep(4, nrow(lfc))
col[lfc$gene_id %in% not_sig] <- 1
col[lfc$gene_id %in% DE_comp1] <- 2
col[lfc$gene_id %in% DE_comp2] <- 3
col[lfc$gene_id %in% DE_both] <- 4
lfc$col <- lfc %>%
dplyr::mutate(color = case_when(
gene_id %in% DE_both ~ 3,
gene_id %in% DE_comp1 ~ 1,
gene_id %in% DE_comp2 ~ 2,
gene_id %in% not_sig ~ 8
)) %>%
pull(color)
lfc$col <- as.factor(lfc$col)
ggplot(lfc, aes(x = -log10(comp1), y = -log10(comp2), color = col)) +
geom_point() +
labs(color = "Group") +
scale_color_discrete(name = "Group", labels = c(paste0("-Log10 adjusted p-value ", paste0(comp1_name)), paste0("-Log10 adjusted p-value ", paste0(comp2_name)), "DE in both comparisons", "Not Significant")) +
geom_abline(intercept = 0, slope = 1) +
geom_hline(aes(yintercept = 0)) +
geom_vline(aes(xintercept = 0)) +
scale_color_cb_friendly() +
xlab(paste0("Log2FC in ", paste0(comp1_name))) +
ylab(paste0("Log2FC in ", paste0(comp2_name)))
```
# R session
List and version of tools used for the QC report generation.
```{r}
sessionInfo()
```