-
Notifications
You must be signed in to change notification settings - Fork 0
/
fig_5C.rmd
190 lines (169 loc) · 6.73 KB
/
fig_5C.rmd
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
---
title: "fig_5C"
author: "Max Schubert"
date: "10/17/2019"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(data.table)
#library(stringr)
library(plyr)
library(gridExtra)
require(ggplot2)
require(data.table)
require(scales)
library(ggpubr)
#dan's suggestion for plotting:
library(ggrepel)
library(ggnewscale)
#for plotting
library(cowplot)
library(gridGraphics) #first install.packages('gridGraphics')
parent_folder = here::here('fig_5C/')
source(here::here('constants.R'))
```
```{r import_data}
bedpath = parent_folder
#import depth
AB3_folA_nonuniq_dp = read.delim(paste(bedpath, "AB3_folA_nonuniq_dp", sep = ""), sep = "\t")
AB4_folA_nonuniq_dp = read.delim(paste(bedpath, "AB4_folA_nonuniq_dp", sep = ""), sep = "\t")
WT_folA_dp = read.delim(paste(bedpath, "s2_folA_dp", sep = ""), sep = "\t")
names(AB3_folA_nonuniq_dp) <- c('chr','pos','dp')
names(AB4_folA_nonuniq_dp) <- c('chr','pos','dp')
names(WT_folA_dp) <- c('chr','pos','dp')
#label and bind two replicates
AB3_folA_nonuniq_dp$replicate = "A"
AB4_folA_nonuniq_dp$replicate = "B"
WT_folA_dp$replicate = "WT"
dp_folA_region = rbind(AB3_folA_nonuniq_dp, AB4_folA_nonuniq_dp, WT_folA_dp)
rm(AB3_folA_nonuniq_dp, AB4_folA_nonuniq_dp, WT_folA_dp) #remove original files
#trim outer 50bp off
dp_folA_region = data.table(dp_folA_region)
dp_folA_region <- dp_folA_region[pos > 48050]
dp_folA_region <- dp_folA_region[pos < 50950]
#import stranded counted bed
AB3_cutadapt_aln_folAzoom.bedgraph = read.delim(paste(bedpath, "AB3_cutadapt_aln_folAzoom.bedgraph", sep = ""), sep = "")
names(AB3_cutadapt_aln_folAzoom.bedgraph) <- c('chr','start', 'end','strand','count')
AB3_cutadapt_aln_folAzoom.bedgraph = data.table(AB3_cutadapt_aln_folAzoom.bedgraph)
dt_folA_bedgraph = data.table(AB3_cutadapt_aln_folAzoom.bedgraph)
```
###
```{r set up figure}
replicate_cols <- brewer.pal(n=9, name='YlGnBu')[c(5,8)]
gene_cols <- brewer.pal(n=9, name='Greys')[c(5,4)]
donor_cols <- brewer.pal(n=9, name='YlGn')[c(3,9)]
#change order of replicates, set colors
dp_folA_region$replicate = factor(dp_folA_region$replicate, levels = c('WT','A','B'))
colormapping = c(A=replicate_cols[1], B=replicate_cols[2], WT='#888888')
dp_folA_region[, dp_norm := dp / max(dp), by='replicate']
snp_lines = data.table(
pos= c(49765, 49903, 50280),
lbl = c('49765: C > T','49903: T > G','50280: T > G'))
snp_lines[, dp_norm := dp_folA_region[pos == .BY[1], max(dp_norm)], by='pos']
genome_start = 48050
genome_end = 50950
genome_breaks = c(48500, 49000, 49500, 50000, 50500)
genome_br_lbl = paste0(genome_breaks/1e3,'kb')
# polygon shapes
gene_y_pos = 1*10^1.3
gene_pos <- data.table(
name=c('folA','kefC'), start=c(49823,48050), end=c(50302, 49631))
#gen uniq IDs for each row, trim data
data = data.table(dt_folA_bedgraph)
data[, serial := .I]
datatrim <- data[count > 1000]
datatrim$plotrow <- c(1,2,3,4,5,6,7,1,2,3,4,5)
```
```{r depth plot}
##### LOG VERSION
depth_plot <- ggplot(dp_folA_region) +
theme_bw() +
geom_segment(data=gene_pos,
aes(x=start, xend=end, color=name,
y=gene_y_pos, yend=gene_y_pos),
size = 5,
arrow=arrow(length = unit(0.02, "npc"), type='closed'),
linejoin = 'mitre', lineend='butt') +
geom_text(data=gene_pos, aes(x=start, y=gene_y_pos,
label=name), hjust=-0.3, vjust=-0.8, size=fig_font_size/2) +
scale_color_manual(values=gene_cols, guide=F) +
new_scale_color() +
labs( y = "Coverage Depth (/maximum)",
x = "Genome Position") +
scale_x_continuous(limits = c(genome_start, genome_end), expand=c(0,0),
breaks=genome_breaks, labels=genome_br_lbl) +
scale_y_continuous(trans = log_trans(base=10),
breaks = c(.00001, 0.0001, 0.001, 0.01, 0.1, 1)) +
coord_cartesian(xlim=c(genome_start, genome_end), ylim = c(.5e-5,gene_y_pos*1.5), clip='on') +
theme(
strip.background=element_blank(), #strip.placement='outside',
strip.text=element_text(color='black'),
panel.border=element_blank(),
axis.line=element_line(),
axis.title.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.x = element_text(size = fig_font_size),
axis.text.y = element_text(size = fig_font_size),
axis.title.y = element_text(size = fig_title_font_size),
panel.grid.minor=element_blank(),
legend.position = c(0.08, .50)) +
geom_line(aes(y=dp_norm, x=pos, color = replicate), size=1, alpha=0.8) +
scale_fill_manual(values=colormapping) +
scale_color_manual(values=colormapping) +
scale_alpha(guide = FALSE) +
#add vert lines for snps
geom_segment(data=snp_lines, aes(x=pos, xend=pos, y=1e-6, yend=dp_norm), linetype=2) +
#add snp annotations
annotate("text", x= 49765, y = 10^(0.1), label = "49765: C>T", hjust=0, angle=45, size=fig_font_size/2) +
annotate("text", x= 49903, y = 10^(-0.15), label = "49903: T>G", hjust=0, angle=45, size=fig_font_size/2) +
annotate("text", x= 50280, y = 10^(-0.3), label = "50280: T>G", hjust=0, angle=45, size=fig_font_size/2)
#50280 U00096.2 T G
depth_plot
```
```{r segment plot}
#plot segments
#this output gets cleaned up in illustrator
col_plot <- ggplot(datatrim) +
theme_bw() +
labs(x = "Genome Position") +
scale_y_continuous(name='Unique Retrons') +
scale_x_continuous(limits = c(genome_start, genome_end), expand=c(0,0),
breaks=genome_breaks, labels=genome_br_lbl) +
coord_cartesian(xlim=c(genome_start, genome_end), clip='off') +
theme(
strip.background=element_blank(), #strip.placement='outside',
strip.text=element_text(color='black'), #, size = fig_font_size),
axis.line.y=element_blank(),
axis.text=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_text(size = fig_title_font_size),
axis.ticks=element_blank(),
panel.grid.major.y=element_blank(),
panel.grid.minor=element_blank(),
panel.border=element_blank(),
legend.position = c(0.9, .55),
legend.title = element_text(size = fig_font_size)) +
# legend.position = c(0.1, .8)) +
geom_segment(aes(y=plotrow/2, yend = plotrow/2,
x=start, xend = end, group = serial,
color = count),
size = 4,
stat = "identity") +
scale_color_gradientn(
name='# Reads', colors=donor_cols, limits=c(1000,60000),
breaks=c(1,2,3,4,5)*10^4, labels=paste0(c(1,2,3,4,5),' x 10^4'),
guide=guide_legend(keyheight=0.9)) +
scale_size(guide = FALSE) +
geom_vline(data=snp_lines[1:2], aes(xintercept=pos), linetype=2)
col_plot
```
```{r combined}
combined_5c <- plot_grid(depth_plot, col_plot, ncol = 1, align = "v", rel_heights = c(1,0.4))
combined_5c
```
```{r save_plot}
ggsave('fig_5C_combined_out.pdf', plot=combined_5c, width=4, height=4.5, units='in', dpi=300, device = cairo_pdf) #useDingbats = FALSE,
```