-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrank_rsa.Rmd
501 lines (396 loc) · 12 KB
/
rank_rsa.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
---
title: "Rank 0 amino acids by RSA"
author: "Mackenzie Johnson"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Analysis of amino acid identities occupying rank 0 by RSA
RMarkdown document that shows how the location of a site within a protein impacts which amino acid is most frequent (rank 0), produces Supplementary Figure 7.
```{r, message=FALSE}
# load packages
library(tidyverse)
library(stringr)
library(cowplot)
library(broom)
library(Biostrings)
```
Read in and extract RSA data:
```{r, message=FALSE}
# read in all .dat files in /data/real/
empirical_files <- dir(
"data/real",
pattern = "*.dat",
full.names = T
)
# read in all .dat files in /data/simulated/
simulated_files <- dir(
"data/simulated",
pattern = "*_evolved.dat",
full.names = T
)
# function to extract rsa values from empirical and simulated alignment
extr_rsa <- function(filename){
read_delim(
filename,
delim = " ",
col_names = c(
"py_index", "site0", "RSA", "aa1", "aa2", "aa3",
"aa4", "aa5", "aa6", "aa7", "aa8", "aa9", "aa10",
"aa11", "aa12", "aa13", "aa14", "aa15", "aa16",
"aa17", "aa18", "aa19", "aa20"
),
skip = 1
) %>% select(site0, RSA)
}
# extract rsa values for sites in all empirical proteins
protein_rsa1 <- tibble(filename_emp = empirical_files) %>%
extract(
filename_emp,
"protein",
"array_(.*).dat",
remove = FALSE
) %>%
mutate(
rsa_emp = map(filename_emp, extr_rsa)
) %>%
select(-filename_emp) %>%
unnest(cols = rsa_emp) %>%
mutate(site = site0+1) %>%
select(-site0)
# extract rsa values for sites in all simulated proteins
protein_rsa2 <- tibble(filename_sim = simulated_files) %>%
extract(
filename_sim,
"protein",
"array_(.*)_evolved.dat",
remove = FALSE
) %>%
mutate(
rsa_sim = map(filename_sim, extr_rsa)
) %>%
select(-filename_sim) %>%
unnest(cols = rsa_sim) %>%
mutate(site = site0+1) %>%
select(-site0)
# combine rsa data frames (note RSA_emp = RSA_sim bc they were calculated on PDB sequence)
colnames(protein_rsa1) <- c("protein", "RSA_emp", "site")
colnames(protein_rsa2) <- c("protein", "RSA_sim", "site")
protein_rsa_full <- full_join(protein_rsa1, protein_rsa2)
rm(protein_rsa1, protein_rsa2, empirical_files, simulated_files)
```
Read in empirical alignments and extract most abundant amino acid (rank 0):
```{r, message=FALSE}
# read in all .csv files in /data/real/
empirical_files_ali <- dir(
"data/real",
pattern = "*_Aligned_Sequences.fasta",
full.names = T
)
# import alignment
import_alignment <- function(filename) {
fasta_file <- readAAStringSet(filename, format = "fasta")
seq_name = names(fasta_file)
aa_seq = paste(fasta_file)
seq_num = seq(1:length(seq_name))
real_align <- data.frame(seq_num, aa_seq, stringsAsFactors = F)
}
# change sequences from strings into character vectors
aa_string_to_vect <- function(real_align) {
as.data.frame(strsplit(real_align$aa_seq, split = ""),
row.names = NULL) -> int_df
as.data.frame(t(int_df), row.names = NULL) -> int_df
}
# tidy data frame
tidy_df <- function(int_df) {
int_df %>%
mutate(num_seq = seq(1:nrow(int_df))) %>%
pivot_longer(
-num_seq,
names_to = "site_raw",
values_to = "aa"
) -> int_df
int_df %>%
extract(site_raw, "site", "V(.*)", remove = TRUE) -> int_df
}
#import_alignment(empirical_files[1]) %>% View()
tibble(filename = empirical_files_ali) %>%
extract(
filename,
"protein",
"data/real/(.*)_A",
remove = FALSE
) %>%
mutate(
raw_data = map(filename, import_alignment),
long_data = map(raw_data, aa_string_to_vect),
tidy_data = map(long_data, tidy_df)
) %>%
select(-filename, -raw_data, -long_data) %>%
unnest(cols = tidy_data) -> alignments
# replace undefined aas
alignments$site <- as.numeric(alignments$site)
levels(alignments$aa) # "X" = any aa, "B" = N or D, "Z" = Q or D
alignments$aa <- recode(alignments$aa, X = "-", B = "N", Z = "Q")
levels(alignments$aa)
alignments$aa <- as.character(alignments$aa)
# determine the number of each AA at each site
count_aa <- function(protein_alignment) {
protein_alignment %>%
group_by(site, aa) %>%
summarize(count = n())
}
alignments %>%
nest(data = c(num_seq, site, aa)) %>%
mutate(counts = map(data, count_aa)) %>%
select(-data) %>%
unnest(cols = counts) -> site_aa_count
empty_counts <- tibble(
aa = c(
'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L',
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y'
),
count = rep(0, 20)
)
# 1. First write a function that can take a data frame with amino acids at one site, and return a data frame with counts, including zeros.
count_zero_aa_site <- function(site_df) {
site_df %>%
group_by(aa) %>%
summarize(count = n()) -> counts
full_join(counts, anti_join(empty_counts, counts, by = 'aa'))
}
# 2. Function to call that function for each protein.
count_zero_aa_protein <- function(protein_df) {
protein_df %>%
nest(data = -site) %>%
mutate(counts = map(data, count_zero_aa_site)) %>%
select(-data) %>%
unnest(cols = counts)
}
# 3. Apply to all proteins
alignments %>%
nest(data = c(num_seq, site, aa)) %>%
mutate(all_counts = map(data, count_zero_aa_protein)) %>%
select(-data) %>%
unnest(cols = all_counts) -> all_count
# USING ONLY OBSERVED AA
# order aa by relative frequency
order_aa <- function(protein_df) {
protein_df %>%
group_by(site) %>%
arrange(desc(count), .by_group = T)
}
all_count %>%
nest(data = c(site, aa, count)) %>%
mutate(ordered_count = map(data, order_aa)) %>%
select(-data) %>%
unnest(cols = ordered_count) -> ordered_count
# EXCLUDE all '-' entries/gaps in alignment
ordered_count %>%
filter(aa != "-") -> no_gaps
# make the categorical variable (aa) numerical (k) by numbering aa's 1-20 in order of freq
number_aa <- function(protein_df) {
protein_df %>%
group_by(site) %>%
mutate(k = as.numeric(0:19))
}
no_gaps %>%
nest(data = c(site, aa, count)) %>%
mutate(add_k = map(data, number_aa)) %>%
select(-data) %>%
unnest(cols = add_k) -> ordered_count
ordered_count %>%
filter(k == 0) %>%
select(protein, site, aa) -> rank_aa_emp
rm(alignments, empirical_files_ali, all_count, empty_counts,
no_gaps, site_aa_count, aa_string_to_vect, count_aa,
count_zero_aa_protein, count_zero_aa_site, import_alignment,
number_aa, order_aa, tidy_df, ordered_count)
```
Read in simulated alignments and extract most adundant amino acid (rank 0):
```{r, message=FALSE}
# read in all .csv files in /data/simulated/
simulated_files_ali <- dir(
"data/simulated",
pattern = "*.csv",
full.names = TRUE
)
# tidy individual alignments
tidy_alignments <- function(alignment) {
alignment %>%
mutate(sequence = as.numeric(1:nrow(alignment)) ) %>%
gather(key = "site", value = "aa", 1:ncol(alignment)) %>%
mutate(site = as.numeric(site))
}
alignments <- tibble(filename = simulated_files_ali) %>%
extract(
filename,
"protein",
"results_(.*)_evolved_split",
remove = FALSE
) %>%
mutate(
raw_data = map(
filename,
function(x) read_csv(x, col_types = cols(.default = "c"))
),
alignment = map(raw_data, tidy_alignments)
) %>%
select(-raw_data, -filename) %>%
unnest(cols = alignment)
# determine the number of each AA at each site
count_aa <- function(protein_alignment) {
protein_alignment %>%
group_by(site, aa) %>%
summarize(count = n())
}
alignments %>%
nest(data = c(sequence, site, aa)) %>%
mutate(counts = map(data, count_aa)) %>%
select(-data) %>%
unnest(cols = counts) -> site_aa_count
# add unobserved amino acids to counts
empty_counts <- tibble(
aa = c('A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K',
'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V',
'W', 'Y'),
count = rep(0, 20)
)
# 1. First write a function that can take a data frame with amino acids at one site, and return a data frame with counts, including zeros.
count_zero_aa_site <- function(site_df) {
site_df %>%
group_by(aa) %>%
summarize(count = n()) -> counts
full_join(counts, anti_join(empty_counts, counts, by = 'aa'))
}
# 2. Function to call that function for each protein.
count_zero_aa_protein <- function(protein_df) {
protein_df %>%
nest(data = -site) %>%
mutate(counts = map(data, count_zero_aa_site)) %>%
select(-data) %>%
unnest(cols = counts)
}
# 3. Apply to all proteins
alignments %>%
nest(data = c(sequence, site, aa)) %>%
mutate(all_counts = map(data, count_zero_aa_protein)) %>%
select(-data) %>%
unnest(cols = all_counts) -> all_count
# USING ONLY OBSERVED AA
# order aa by relative frequency
order_aa <- function(protein_df) {
protein_df %>%
group_by(site) %>%
arrange(desc(count), .by_group = T)
}
all_count %>%
nest(data = c(site, aa, count)) %>%
mutate(ordered_count = map(data, order_aa)) %>%
select(-data) %>%
unnest(cols = ordered_count) -> ordered_count
# make the categorical variable (aa) numerical (k) by numbering aa's 1-20 in order of freq
number_aa <- function(protein_df) {
protein_df %>%
group_by(site) %>%
mutate(k = as.numeric(0:19))
#mutate(k = as.numeric(1:20))
}
ordered_count %>%
nest(data = c(site, aa, count)) %>%
mutate(add_k = map(data, number_aa)) %>%
select(-data) %>%
unnest(cols = add_k) -> ordered_count
ordered_count %>%
filter(k == 0) %>%
select(protein, site, aa) -> rank_aa_sim
rm(alignments, all_count, empty_counts, site_aa_count,
ordered_count, tidy_alignments, count_zero_aa_protein,
count_zero_aa_site, count_aa, number_aa, order_aa,
simulated_files_ali)
```
Combine RSA and aa data:
```{r, message=FALSE}
colnames(rank_aa_emp)[3] <- "aa_emp"
colnames(rank_aa_sim)[3] <- "aa_sim"
ranked_aa <- left_join(rank_aa_sim, rank_aa_emp)
all_data <- full_join(ranked_aa, protein_rsa_full)
# classify sites by location within structure based on RSA values
all_data %>%
mutate(
site_location = case_when(
RSA_emp < 0.05 ~ "Buried",
RSA_emp > 0.25 ~ "Exposed",
TRUE ~ "Intermediate"
)
) -> all_data
all_data %>%
group_by(site_location) %>%
summarise(count = n())
# turn aa to factors, set levels based on kyte doolittle hydrophobicity scale
all_data$aa_emp <- factor(
all_data$aa_emp,
levels = c(
'I', 'V', 'L', 'F', 'C', 'M', 'A', 'G', 'T',
'S', 'W', 'Y', 'P', 'H', 'E', 'Q', 'D', 'N',
'K', 'R'
)
)
all_data$aa_sim <- factor(
all_data$aa_sim,
levels = c(
'I', 'V', 'L', 'F', 'C', 'M', 'A', 'G', 'T',
'S', 'W', 'Y', 'P', 'H', 'E', 'Q', 'D', 'N',
'K', 'R'
)
)
all_data %>%
group_by(site_location, aa_sim) %>%
summarise(count = n()) -> summary1
colnames(summary1) <- c("site_location", "aa", "simulated")
all_data %>%
group_by(site_location, aa_emp) %>%
summarise(count = n()) -> summary2
colnames(summary2) <- c("site_location", "aa", "empirical")
plot_df <- full_join(summary1, summary2)
plot_df %>%
pivot_longer(
cols = c(simulated, empirical),
names_to = "type",
values_to = "count"
) -> plot_df
plot_df$site_location <- factor(
plot_df$site_location,
levels = c("Buried", "Intermediate", "Exposed"),
ordered = TRUE
)
plot_df %>%
group_by(site_location, type) %>%
mutate(freq = count/sum(count)) -> plot_df
```
Supplementary Figure 7:
```{r, message=FALSE}
plot_df %>%
ggplot(aes(x = aa, y = freq, fill = type)) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(vars(site_location)) +
scale_fill_manual(
name = "Alignment",
values = c("#CC79A7","#F0E442"), # #009E73
labels = c("Empirical", "Simulated")
) +
scale_y_continuous(
breaks = c(0.00, 0.05, 0.10, 0.15),
expand = c(0,0)
) +
#scale_x_discrete(expand = c(0.1,0.1)) +
labs(x = "Amino acids", y = "Frequency") +
theme_cowplot() +
panel_border() -> figs7
figs7
save_plot("figures7.pdf", figs7, ncol = 1, nrow = 1,
base_height = 5,
base_asp = 1.618, base_width = NULL)
```