-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuhrman.R
134 lines (106 loc) · 4.1 KB
/
fuhrman.R
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
for (cur_genome in unique(fuhrman_df$genome)) {
consensus_base_df <- fuhrman_df %>%
filter(genome == cur_genome) %>%
select(scaffold,
position,
con_base,
sample) %>%
pivot_wider(names_from = sample,
values_from = con_base,
values_fill = NA,
names_prefix = "sample_") %>%
na.omit()
consensus_base_df <- consensus_base_df[ , order(names(consensus_base_df))]
consensus_base_mat <- consensus_base_df %>%
mutate(name = paste(scaffold, position, sep = ";")) %>%
column_to_rownames(var = "name") %>%
select(starts_with("sample")) %>%
as.matrix()
set.seed(123)
Heatmap(consensus_base_mat,
name = "Consensus Base",
show_row_names = FALSE,
column_labels = gsub("sample_", "", colnames(consensus_base_mat)),
column_title = "Sample",
row_title = "SNV")
base_count_df <- fuhrman_df %>%
filter(genome == cur_genome) %>%
transmute(scaffold,
position,
FreqA = A / (A + C + T + G),
FreqC = C / (A + C + T + G),
FreqT = T / (A + C + T + G),
FreqG = G / (A + C + T + G),
sample) %>%
pivot_wider(names_from = sample,
values_from = c(starts_with("Freq"))) %>%
na.omit()
base_count_df <- base_count_df[ , order(names(base_count_df))]
freq_df_list <- list()
for (cur_sample in 1:9) {
sample_consensus_base_df <- consensus_base_df %>%
select(scaffold,
position,
matches(paste0("sample_", cur_sample)))
colnames(sample_consensus_base_df) <- c("scaffold", "position", "con_base")
base_count_df$con_base <- sample_consensus_base_df$con_base
tmp_df <- sapply(iter(base_count_df, by = "row"),
freq_mapper) %>%
t() %>%
as.data.frame()
colnames(tmp_df) <- paste0("Freq", seq(1, 9))
tmp_df$con_base <- sample_consensus_base_df$con_base
freq_df_list[[cur_sample]] <- tmp_df
}
avg_freq_df <- sapply(freq_df_list, function(x) colMeans(x %>% select(-con_base))) %>%
t() %>%
as.data.frame() %>%
melt(variable_name = "Time") %>%
transmute(Time = gsub("Freq", "", Time),
Frequency = value)
avg_freq_df$RefSample <- as.factor(rep(seq(1, 9), 9))
p1 <- ggplot(avg_freq_df, aes(x = Time,
y = Frequency,
colour = RefSample,
group = RefSample)) +
geom_point() +
geom_line() +
scale_y_continuous(limits = c(0.25, 1),
breaks = c(0.25, seq(0.4, 1, by = 0.2))) +
labs(y = "Average Consensus Base Frequency",
colour = "Reference Sample") +
theme_bw() +
theme(panel.grid = element_blank())
consensus_base_mat <- consensus_base_df %>%
select(starts_with("sample")) %>%
as.matrix()
bar_array <- c()
for (i in 1:nrow(consensus_base_mat)) {
opts <- unique(consensus_base_mat[i, ])
bar_array <- append(bar_array,
max(sapply(opts, function(x) sum(x == consensus_base_mat[i, ]))))
}
bar_df <- as.data.frame(bar_array)
p2 <- ggplot(bar_df, aes(x = bar_array)) +
geom_bar(aes(y = (..count..) / sum(..count..))) +
scale_x_reverse(limits = c(10, 0),
breaks = seq(9, 1, by = -1)) +
scale_y_continuous(limits = c(0, 1)) +
labs(x = "Consensus Base Recurrence",
y = "Number of SNVs") +
theme_bw() +
theme(panel.grid = element_blank())
tax_df <- selected_df %>%
filter(genome == paste0("metabat2bin_", cur_genome)) %>%
transmute(Tax = paste(Order, Family, Genus, Species, sep = ";"))
p <- p2 + p1 +
plot_layout(guides = "collect") +
plot_annotation(title = "Dynamics of within-species Variation",
subtitle = paste0("Genome: ", cur_genome, ". Taxonomy: ", tax_df$Tax))
ggsave(paste0("fuhrman", cur_genome, ".pdf"),
plot = p,
height = 7,
width = 14,
path = "results",
device = "pdf")
}