-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfig_4C.Rmd
193 lines (166 loc) · 6.93 KB
/
fig_4C.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
191
192
193
---
title: "Fig_4C_linmodel"
author: "Max Schubert"
date: "6/18/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)
require(ggplot2)
require(data.table)
require(scales)
library(ggpubr)
parent_folder = here::here('fig_4C')
source(here::here('constants.R'))
#function to turn spaces into newlines
addline_format <- function(x,...){
gsub(' ','\n',x)
}
#function for linear modeling
lm_group <- function(dt_subset, byval) {
lmod <- lm(log10(ratio_median_neutrals) ~ timepoint, data= dt_subset[is.finite(ratio_median_neutrals)])
return(list(intercept= lmod$coefficients[1], slope= lmod$coefficients[2], mut_class= dt_subset$mut_class[1]))
}
```
```{r get data}
#get RLR trajectory data
lin.mod.path = parent_folder
start.data.file5 = paste(here::here('fig_4B'), "fig4Bdata_megatable5.RData", sep = "/")
start.data.file100 = paste(here::here('fig_4B'), "fig4Bdata_megatable100.RData", sep = "/")
load(start.data.file5)#this creates the file megatable5
load(start.data.file100)
#get clonal growth data
clonal_growth_path = paste(parent_folder, 'summary_growth.csv', sep = "/")
clonalgrowth.dt = data.table(fread(clonal_growth_path))
```
####
## set up figure, looking at linear models, rif5
```{r prep_fig_rif5}
#resolve naming issue
megatable5[,
ratio_median_neutrals := ratio_to_neutrals,
by=c("nickname", "mut_class","replicate")]
#create summary table
summary <- megatable5[,
list(
Count = Count,
N= .N,
meanRatio= mean(ratio_median_neutrals),
sd= sd(ratio_median_neutrals),
se= sd(ratio_median_neutrals) / sqrt(.N)),
by=c("ratio_expt", "timepoint", "sample_type", "induction", "bc_read", "nickname", "mut_class")]
#run linear model on rif5, using all 3 time points
megatable_slopes_3points <- megatable5[
(ratio_expt == "A" | ratio_expt == "A_5") & (ratio_median_neutrals > 0) &
(timepoint %in% c(1,2,3)) &
sample_type != 'msDNA' & induction != 'n',
lm_group(.SD,.BY),
by='nickname']
simple_slopes3 <- megatable_slopes_3points[mut_class %in% c('Lethal','Neutral','rpoB Alleles')]
#harmonize allele names
simple_slopes3[nickname=='rpoB_CACTCGGCCCAG_1595_nan', nickname := 'rpoB_CACTCGGCCCAG_1594_nan']
simple_slopes3[nickname=='rpoB_C_1534_T', nickname := 'rpoB_C_1535_T']
# add wildtype explicitly
simple_slopes_final3 <- rbind(simple_slopes3, data.table(nickname='WT', intercept=0, slope=0, mut_class='Neutral'))
indiv_v_rlr3 <- merge(clonalgrowth.dt, simple_slopes_final3, by.x='hard_desc', by.y='nickname')
slope_fit3 <- indiv_v_rlr3[, cor.test(slope, log10(mean), method=c("pearson"))]
#annotate
p.val <- format(slope_fit3$p.value, scientific=TRUE, digits=3)
pearson_r <- round(slope_fit3$estimate, 3)
```
#exploratory plot first, visually examing linear models
```{r plot_linmodel}
summary <- summary[ratio_expt == "A" | ratio_expt == "A_5", ] #looking at rif5
summary <- summary[timepoint != "-1", ]
summary <- summary[timepoint != "0", ]
summary <- summary[sample_type != "msDNA", ]
summary <- summary[induction != "n", ]
ggplot(summary, aes(x=timepoint, y=log10(meanRatio), color=nickname)) +
geom_errorbar(aes(ymin=log10(meanRatio-se), ymax=log10(meanRatio+se)), width=.1, show.legend = FALSE) +
geom_line(show.legend = FALSE) +
geom_point(aes(group = nickname),show.legend = FALSE) +
scale_y_continuous(labels = c(.001, .01, .1, 1, 10, 100), breaks=log10(c(.001, .01, .1, 1, 10, 100))) +
theme_bw() +
geom_abline(show.legend = FALSE, data=megatable_slopes_3points, aes(intercept=intercept, slope=slope, color=nickname), linetype=2) +
facet_wrap(~mut_class)
```
### exploratory plot: examine just a subset:
```{r look_at_fewer}
simple_summ <- summary[mut_class %in% c('Lethal','Neutral','rpoB Alleles')]
simple_slopes <- megatable_slopes_3points[mut_class %in% c('Lethal','Neutral','rpoB Alleles')]
#plot
ggplot(simple_summ, aes(x=timepoint, y=log10(meanRatio), color=nickname)) +
geom_errorbar(aes(ymin=log10(meanRatio-se), ymax=log10(meanRatio+se)), width=.1, show.legend = FALSE) +
geom_line(show.legend = FALSE) +
geom_point(aes(group = nickname),show.legend = FALSE) +
scale_y_continuous(labels = c(.001, .01, .1, 1, 10, 100), breaks=log10(c(.001, .01, .1, 1, 10, 100))) +
theme_bw() +
geom_abline(show.legend = FALSE, data=simple_slopes, aes(intercept=intercept, slope=slope, color=nickname),
linetype=2) +
facet_wrap(~mut_class)
```
###figure 3D
```{r plot_fig}
fig_3d <- ggplot(indiv_v_rlr3,
aes(x=slope, y=log10(mean))) +
stat_smooth(method = "lm", col = "red",
se=FALSE, linetype = "dashed") +
geom_point() +
geom_errorbar(width=0.05, aes(ymin=log10(mean-se), ymax=log10(mean+se))) +
labs(x='RLR relative enrichment', y='Clonal relative log(µ)') +
geom_rect(xmin = 0, xmax = 0.7, ymin = .15, ymax = .21,
fill = "white", color = "#000000") +
annotate(geom='text', label=paste0('r = ',pearson_r,'\n','p = ',p.val), y=.18, x=0.3) +
theme_linedraw() +
theme(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),
axis.title.x = element_text(size = fig_title_font_size),
panel.grid.minor=element_blank(),
panel.grid.major.x=element_blank(),
panel.border=element_blank(),
axis.line=element_line(),
legend.position = 'none')
fig_3d
```
```{r save_fig}
ggsave(here::here('fig_3D_linmod.pdf'), plot=fig_3d, width=3,
height=2.75, units='in', dpi=300, device = cairo_pdf)
```
#### supplemental fig S4C
```{r plot slopes by mut class}
more_summ <- summary
more_slopes <- megatable_slopes_3points
#reformat x labels with newlines
#careful! check factor levels so you do not mis-label!
x_labels = c("Lethal", "Unrelated\nResistance",
"Neutral","Deleterious", "rpoB\nAlleles")
fig_s4C <- ggplot(more_slopes, aes(x=reorder(mut_class, slope), y=slope, group = nickname, color = mut_class))+
geom_hline(yintercept=0, linetype=2, size=0.5) + #baseline no slope
geom_point()+
theme_linedraw() +
labs(y = "RLR slopes (Rif5)") +
scale_x_discrete(labels = x_labels) +
theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1,
size = fig_font_size),
axis.text.y = element_text(size = fig_font_size),
axis.title.y = element_text(size = fig_title_font_size),
axis.title.x = element_blank(),
strip.background=element_blank(),
panel.grid.minor=element_blank(),
panel.grid.major.x=element_blank(),
panel.border=element_blank(),
axis.line=element_line(),
legend.position = 'none')
fig_s4C
#ggplotly()
```
```{r save s4C}
ggsave(here::here('supplemental_figures/fig_S4C.pdf'), plot=fig_s4C, width=3.5,
height=3, units='in', dpi=300, device = cairo_pdf)
```