-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00_merge_fitness_tables.Rmd
215 lines (175 loc) · 8.97 KB
/
00_merge_fitness_tables.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
---
title: "merge fitness tables"
output: html_document
date: "2023-12-19"
---
```{r load data, overview}
library(data.table)
library(ggplot2)
library(GGally)
library(viridis)
theme_set(theme_classic())
base_dir="path/to/your/files"
setwd(base_dir)
my_fn_WT <- function(data, mapping, ...){
p <- ggplot(data = data, mapping = mapping) +
geom_hex(bins=100) +
scale_fill_viridis()
p
}
#load dimsum table and merge with designed variants file
load_allvars<-function(allvars_path,variant_data_merge_path,aavars_path,lib_id){
#load allvariants table
load(allvars_path)
suppressWarnings(print(ggpairs(all_variants[,c("growthrate1","growthrate2","growthrate3")], lower=list(continuous=my_fn_WT))+
ggtitle(lib_id)))
ggsave(paste("../ED_Figure1b_",lib_id,".pdf",sep=""))
#add zero output variants
load(variant_data_merge_path)
colnames(variant_data_merge)[9:14]<-c("count_e1_s0","count_e2_s0","count_e3_s0","count_e1_s1","count_e2_s1","count_e3_s1")
variant_data_merge_zerooutput<-variant_data_merge[count_e1_s1+count_e2_s1+count_e3_s1==0,]
zerooutput_aggregated<-variant_data_merge_zerooutput[,.(
nt_seq=NA,
aa_seq=unique(aa_seq),
Nham_nt=NA,
Nham_aa=NA,
Nmut_codons=NA,
WT=NA,
indel=unique(indel),
STOP=unique(STOP),
STOP_readthrough=unique(STOP_readthrough),
count_e1_s0=sum(count_e1_s0),
count_e2_s0=sum(count_e2_s0),
count_e3_s0=sum(count_e3_s0),
count_e1_s1=sum(count_e1_s1),
count_e2_s1=sum(count_e2_s1),
count_e3_s1=sum(count_e3_s1),
mean_count=mean(c(count_e1_s0,count_e2_s0,count_e3_s0)),
fitness1_uncorr=NA,
fitness2_uncorr=NA,
fitness3_uncorr=NA,
sigma1_uncorr=NA,
sigma2_uncorr=NA,
sigma3_uncorr=NA,
fitness=NA,
sigma=NA,
growthrate1=NA,
growthrate1_sigma=NA,
growthrate2=NA,
growthrate2_sigma=NA,
growthrate3=NA,
growthrate3_sigma=NA,
growthrate=NA,
growthrate_sigma=NA),
by="aa_seq"]
zerooutput_aggregated$aa_seq<-NULL
all_variants<-rbind(all_variants,zerooutput_aggregated)
#merge with designed variants
all_variants<-merge(all_variants,fread(aavars_path),by="aa_seq",all=TRUE)
all_variants[is.na(mean_count),mean_count:=0]
#add library id and save
all_variants[,library:=lib_id]
return(all_variants)
}
A1<-load_allvars("dimsum_files/dimsum_output/A1_BGI_Q30_fitness_replicates.RData",
"dimsum_files/dimsum_output/A1_BGI_Q30_variant_data_merge.RData",
"dimsum_files/dimsum_scripts_and_inputfiles/A1_all_aa_variants.txt",
"A1")
B3<-load_allvars("dimsum_files/dimsum_output/B3_BGI_Q20_fitness_replicates.RData",
"dimsum_files/dimsum_output/B3_BGI_Q20_variant_data_merge.RData",
"dimsum_files/dimsum_scripts_and_inputfiles/B3_all_aa_variants.txt",
"B3")
C1<-load_allvars("dimsum_files/dimsum_output/C1_BGI_Q20_fitness_replicates.RData",
"dimsum_files/dimsum_output/C1_BGI_Q20_variant_data_merge.RData",
"dimsum_files/dimsum_scripts_and_inputfiles/C1_resyn_all_aa_variants.txt",
"C1")
C2<-load_allvars("dimsum_files/dimsum_output/C2_BGI_Q20_fitness_replicates.RData",
"dimsum_files/dimsum_output/C2_BGI_Q20_variant_data_merge.RData",
"dimsum_files/dimsum_scripts_and_inputfiles/C2_all_aa_variants.txt",
"C2")
C3<-load_allvars("dimsum_files/dimsum_output/C3_BGI_Q20_fitness_replicates.RData",
"dimsum_files/dimsum_output/C3_BGI_Q20_variant_data_merge.RData",
"dimsum_files/dimsum_scripts_and_inputfiles/C3_all_aa_variants.txt",
"C3")
C4<-load_allvars("dimsum_files/dimsum_output/C4_BGI_Q20_fitness_replicates.RData",
"dimsum_files/dimsum_output/C4_BGI_Q20_variant_data_merge.RData",
"dimsum_files/dimsum_scripts_and_inputfiles/C4_all_aa_variants.txt",
"C4")
C5<-load_allvars("dimsum_files/dimsum_output/C5_BGI_Q20_fitness_replicates.RData",
"dimsum_files/dimsum_output/C5_BGI_Q20_variant_data_merge.RData",
"dimsum_files/dimsum_scripts_and_inputfiles/C5_all_aa_variants.txt",
"C5")
C6<-load_allvars("dimsum_files/dimsum_output/C6_BGI_Q20_fitness_replicates.RData",
"dimsum_files/dimsum_output/C6_BGI_Q20_variant_data_merge.RData",
"dimsum_files/dimsum_scripts_and_inputfiles/C6_all_aa_variants.txt",
"C6")
C7<-load_allvars("dimsum_files/dimsum_output/C7_BGI_Q20_fitness_replicates.RData",
"dimsum_files/dimsum_output/C7_BGI_Q20_variant_data_merge.RData",
"dimsum_files/dimsum_scripts_and_inputfiles/C7_all_aa_variants.txt",
"C7")
mutated_domainome<-rbind(A1,B3,C1,C2,C3,C4,C5,C6,C7)
#load synonymous fitness data
A1_synvars<-fread("dimsum_files/dimsum_output/A1_BGI_Q30_fitness_synonymous.txt",header=TRUE)[,library:="A1"]
B3_synvars<-fread("dimsum_files/dimsum_output/B3_BGI_Q20_fitness_synonymous.txt",header=TRUE)[,library:="B3"]
C1_synvars<-fread("dimsum_files/dimsum_output/C1_BGI_Q20_fitness_synonymous.txt",header=TRUE)[,library:="C1"]
C2_synvars<-fread("dimsum_files/dimsum_output/C2_BGI_Q20_fitness_synonymous.txt",header=TRUE)[,library:="C2"]
C3_synvars<-fread("dimsum_files/dimsum_output/C3_BGI_Q20_fitness_synonymous.txt",header=TRUE)[,library:="C3"]
C4_synvars<-fread("dimsum_files/dimsum_output/C4_BGI_Q20_fitness_synonymous.txt",header=TRUE)[,library:="C4"]
C5_synvars<-fread("dimsum_files/dimsum_output/C5_BGI_Q20_fitness_synonymous.txt",header=TRUE)[,library:="C5"]
C6_synvars<-fread("dimsum_files/dimsum_output/C6_BGI_Q20_fitness_synonymous.txt",header=TRUE)[,library:="C6"]
C7_synvars<-fread("dimsum_files/dimsum_output/C7_BGI_Q20_fitness_synonymous.txt",header=TRUE)[,library:="C7"]
synvars<-rbind(A1_synvars,B3_synvars,C1_synvars,C2_synvars,C3_synvars,C4_synvars,C5_synvars,C6_synvars,C7_synvars)
#plot fitness correlations
ggplot(mutated_domainome)+
geom_histogram(aes(x=log10(mean_count+1)))+
facet_wrap(~library)
ggsave("output_files/ED_Figure1a.pdf")
mutated_domainome<-mutated_domainome[!duplicated(mutated_domainome[,c("aa_seq","variant_ID","library")]),]
mutated_domainome<-mutated_domainome[!is.na(dom_ID)]
table(mutated_domainome$mean_count>0,mutated_domainome$library)
table(mutated_domainome$mean_count>10,mutated_domainome$library)
#add dead peak
modes <- function(d){
i <- which(diff(sign(diff(d$y))) < 0) + 1
data.frame(x = d$x[i], y = d$y[i])
}
dead_modes<-c()
libs<-c()
for (lib in unique(mutated_domainome$library)){
dead_modes<-c(dead_modes,modes(density(mutated_domainome[STOP==TRUE & library==lib,]$growthrate,na.rm=TRUE))$x[which.max(modes(density(mutated_domainome[STOP==TRUE & library==lib,]$growthrate,na.rm=TRUE))$y)])
libs<-c(libs,lib)
}
dead_peaks<-data.table(dead_modes,libs)
for (lib in unique(mutated_domainome$library)){
mutated_domainome[library==lib,growthrate:=growthrate-dead_peaks[libs==lib,]$dead_modes]
mutated_domainome[library==lib,growthrate1:=growthrate1-dead_peaks[libs==lib,]$dead_modes]
mutated_domainome[library==lib,growthrate2:=growthrate2-dead_peaks[libs==lib,]$dead_modes]
mutated_domainome[library==lib,growthrate3:=growthrate3-dead_peaks[libs==lib,]$dead_modes]
synvars[library==lib,growthrate:=growthrate-dead_peaks[libs==lib,]$dead_modes]
synvars[library==lib,growthrate1:=growthrate1-dead_peaks[libs==lib,]$dead_modes]
synvars[library==lib,growthrate2:=growthrate2-dead_peaks[libs==lib,]$dead_modes]
synvars[library==lib,growthrate3:=growthrate3-dead_peaks[libs==lib,]$dead_modes]
}
#dead if input counts>9 in any of the inputs and 0 in all outputs
mutated_domainome[mean_count==0,missing:=TRUE]
mutated_domainome[mean_count>0,missing:=FALSE]
mutated_domainome[(count_e1_s0>=10 | count_e2_s0>=10 | count_e3_s0>=10) & is.na(fitness),dead:="yes"]
mutated_domainome[!(count_e1_s0>=10 | count_e2_s0>=10 | count_e3_s0>=10) & is.na(fitness),dead:="undetermined"]
mutated_domainome[!is.na(fitness),dead:="no"]
nrow(mutated_domainome)
table(mutated_domainome$missing)
table(mutated_domainome$dead)
mutated_domainome$WT.x<-NULL
colnames(mutated_domainome)[which(colnames(mutated_domainome)=="WT.y")]<-"WT"
write.table(mutated_domainome,file="analysis_files/mutated_domainome_merged.txt",
row.names = FALSE,
quote = FALSE,
sep = "\t")
write.table(synvars,file="analysis_files/synonymous_variants_merged.txt",
row.names = FALSE,
quote = FALSE,
sep = "\t")
nrow(mutated_domainome)
length(unique(mutated_domainome$aa_seq))
length(unique(mutated_domainome$dom_ID))
```