-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymorph.R
278 lines (222 loc) · 9.68 KB
/
polymorph.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
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
# import snv data with minimal information on
# - genome: "UUJ150418_matabat2bin.303.fa"
# - scaffold
# - position
# - con_base: "A" / "C" / "T" / "G"
# - A, C, T, G: numerical (0 - ∞)
# - sample: "UUJ150318"
snvs_df <- read_csv("data/snvs.csv")
# select only relevant columns
snvs_df <- snvs_df %>%
transmute(genome = gsub("matabat2bin.", "", gsub(".fa", "", genome)),
scaffold,
position,
con_base,
A, C, T, G,
sample)
coverage_correction <- FALSE
# iterate over all MAGs of interest
for (cur_genome in selected_genomes) {
message(paste("analysing genome", cur_genome))
tax_df <- top_df %>%
filter(genome == cur_genome) %>%
transmute(Tax = paste(Phylum, Order, Family, sep = ";"))
genome_df <- snvs_df %>%
filter(genome == cur_genome) %>%
select(scaffold,
position,
con_base,
sample)
# skip MAGs that appear in less than 2 samples
if (length(unique(genome_df$sample)) < 2) {
message(paste("skipping", cur_genome))
next
}
# generate data.frame of the consensus bases
# of the current MAG across the samples
consensus_base_df <- genome_df %>%
pivot_wider(names_from = sample,
values_from = con_base,
values_fill = NA)
# generate a matrix of the consensus bases
# where each row is an SNV and each column
# is a sample, so that each cell tells you
# in sample "x" which consensus base was found
# for SNV "y"
consensus_base_mat <- consensus_base_df %>%
mutate(name = paste(scaffold, position, sep = ";")) %>%
column_to_rownames(var = "name") %>%
select(ends_with("UUM"),
ends_with("UUJ"),
ends_with("UUF")) %>%
as.matrix()
# sort columns so that samples are in
# chronological order
consensus_base_mat <- consensus_base_mat[ , order(colnames(consensus_base_mat))]
count_list <- list()
cov_df <- top_df %>%
filter(genome == cur_genome) %>%
select(starts_with("coverage")) %>%
melt() %>%
mutate(variable = gsub("coverage_", "", variable)) %>%
filter(variable %in% colnames(consensus_base_mat)) %>%
arrange(variable)
low_coverage_samples <- cov_df$value < 10
snv_likelyhood <- sapply(iter(consensus_base_mat, by = "row"), function(x) sum(!is.na(x)) / length(x))
if (coverage_correction) {
for (i in 1:nrow(consensus_base_mat)) {
consensus_base_mat[i, is.na(consensus_base_mat[i, ]) & low_coverage_samples & snv_likelyhood[i] > 0.7] <- max(consensus_base_mat[i, ], na.rm = TRUE)
}
}
# compute number of shared polymorphic sites
# each iteration taking a specific sample
# as reference and comparing it to all the others
for (cur_sample in 1:ncol(consensus_base_mat)) {
message(paste("computing sample", cur_sample))
sample_mat <- consensus_base_mat[!is.na(consensus_base_mat[ , cur_sample]), ]
count_list[[cur_sample]] <- sapply(iter(sample_mat, by = "col"),
function(x) sum(sample_mat[ , cur_sample] == x, na.rm = TRUE))
}
# melt counts of shared polymorphic sites
# into 2 columns:
# - L1 (reference sample identifier)
# - value (number of shared polymorphic sites)
count_df <- count_list %>%
melt() %>%
transmute(RefSample = colnames(consensus_base_mat)[L1],
Count = value)
# add column with information on sampling time
count_df$ObservedSample <- as.factor(rep(colnames(consensus_base_mat), ncol(consensus_base_mat)))
# compute percentage of shared polymorphic sites
count_df <- count_df %>%
group_by(RefSample) %>%
summarise(Count,
ObservedSample,
Percent = Count / max(Count))
# convert time to a date object type
count_df <- count_df %>%
mutate(Time = gsub("UU\\w", "", ObservedSample)) %>%
mutate(Time = as.Date(paste(substr(Time, 1, 2),
substr(Time, 3, 4),
substr(Time, 5, 6),
sep = "-")))
# index by season
winter17 <- substr(count_df$RefSample, 3, 4) %in% c("12", "01", "02") &
substr(count_df$RefSample, 1, 2) == "17"
summer17 <- substr(count_df$RefSample, 3, 4) %in% c("06", "07", "08") &
substr(count_df$RefSample, 1, 2) == "17"
spring17 <- substr(count_df$RefSample, 3, 4) %in% c("03", "04", "05") &
substr(count_df$RefSample, 1, 2) == "17"
autumn17 <- substr(count_df$RefSample, 3, 4) %in% c("09", "10", "11") &
substr(count_df$RefSample, 1, 2) == "17"
winter18 <- substr(count_df$RefSample, 3, 4) %in% c("12", "01", "02") &
substr(count_df$RefSample, 1, 2) == "18"
summer18 <- substr(count_df$RefSample, 3, 4) %in% c("06", "07", "08") &
substr(count_df$RefSample, 1, 2) == "18"
spring18 <- substr(count_df$RefSample, 3, 4) %in% c("03", "04", "05") &
substr(count_df$RefSample, 1, 2) == "18"
autumn18 <- substr(count_df$RefSample, 3, 4) %in% c("09", "10", "11") &
substr(count_df$RefSample, 1, 2) == "18"
# store seasonality information into
# a new column
count_df$Season <- ""
count_df$Season[winter17] <- "Winter 2017"
count_df$Season[summer17] <- "Summer 2017"
count_df$Season[spring17] <- "Spring 2017"
count_df$Season[autumn17] <- "Autumn 2017"
count_df$Season[winter18] <- "Winter 2018"
count_df$Season[summer18] <- "Summer 2018"
count_df$Season[spring18] <- "Spring 2018"
count_df$Season[autumn18] <- "Autumn 2018"
message("generating plot 1")
p1 <- ggplot(count_df, aes(x = Time,
y = Percent,
colour = Season,
group = RefSample)) +
geom_point() +
geom_line() +
scale_x_date(date_breaks = "1 month") +
scale_y_continuous(limits = c(0, 1),
breaks = seq(0, 1, by = 0.1)) +
labs(y = "Percent of Shared Polymorphic Sites",
colour = "Reference Sample") +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid = element_blank())
bar_array <- c()
for (i in 1:nrow(consensus_base_mat)) {
message(paste("computing recurrence", i))
opts <- unique(consensus_base_mat[i, ])
bar_array <- append(bar_array,
max(sapply(opts, function(x) sum(x == consensus_base_mat[i, ], na.rm = TRUE))))
}
bar_df <- as.data.frame(bar_array)
message("generating plot 2")
p2 <- ggplot(bar_df, aes(x = bar_array)) +
geom_bar() +
scale_x_reverse(limits = c(43, 0),
breaks = seq(42, 1, by = -1)) +
labs(x = "Consensus Base Recurrence",
y = "Number of Shared Polymorphic Sites") +
theme_bw() +
theme(panel.grid = element_blank())
genome_time_series_df <- time_series_df %>%
filter(genome == cur_genome)
up_lim <- ceiling(max(genome_time_series_df$RPKM) / 10) * 10
message("generating plot 3")
p3 <- ggplot(genome_time_series_df, aes(x = Sample)) +
geom_line(aes(y = RPKM), colour = "Black") +
geom_line(aes(y = NucDiv * 1000), colour = "Dark Gray") +
scale_x_date(date_breaks = "1 month") +
scale_y_continuous(name = "RPKM",
breaks = seq(0, up_lim, by = 10),
sec.axis = sec_axis(~./1000,
name = "Nucleotide Diversity")) +
theme_bw() +
labs(x = "Time") +
theme(axis.title.y = element_text(colour = "Black"),
axis.title.y.right = element_text(colour = "Dark Gray"),
panel.grid = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1))
count_mat <- count_df %>%
select(-Count, -Time) %>%
pivot_wider(values_from = Percent,
names_from = ObservedSample) %>%
column_to_rownames(var = "RefSample")
set.seed(1234)
row_ha <- HeatmapAnnotation(Season = gsub(" 201\\d", "", count_mat$Season),
which = "row")
count_mat <- count_mat %>%
select(-Season) %>%
as.matrix()
winter <- substr(colnames(count_mat), 3, 4) %in% c("12", "01", "02")
summer <- substr(colnames(count_mat), 3, 4) %in% c("06", "07", "08")
spring <- substr(colnames(count_mat), 3, 4) %in% c("03", "04", "05")
autumn <- substr(colnames(count_mat), 3, 4) %in% c("09", "10", "11")
col_ha_df_bottom <- data.frame(ObservedSample = colnames(count_mat))
col_ha_df_bottom$Season <- ""
col_ha_df_bottom$Season[winter] <- "Winter"
col_ha_df_bottom$Season[summer] <- "Summer"
col_ha_df_bottom$Season[spring] <- "Spring"
col_ha_df_bottom$Season[autumn] <- "Autumn"
set.seed(1234)
col_ha_bottom <- HeatmapAnnotation(Season = col_ha_df_bottom$Season,
which = "col",
show_legend = FALSE)
col_fun = colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"))
g_values <- 10 * sapply(1:ncol(consensus_base_mat), function(x) mean(snv_likelyhood[is.na(consensus_base_mat[ , x])]) / cov_df$value[x])
sig_threshold1 <- g_values < 0.05
sig_threshold2 <- g_values < 0.005
pch <- rep(NA, ncol(consensus_base_mat))
pch[sig_threshold1] <- "*"
pch[sig_threshold2] <- "**"
gvalue_col_fun <- colorRamp2(c(0, 1.5, 3), c("green", "white", "red"))
col_ha_top <- HeatmapAnnotation(Trust = anno_simple(-log10(g_values), col = gvalue_col_fun, pch = pch))
lgd_gvalue <- Legend(title = "Trust", col_fun = gvalue_col_fun, at = c(0, 1, 2, 3),
labels = c("1", "0.1", "0.01", "0.001"))
lgd_sig <- Legend(pch = c("*", "**"), type = "points", labels = c("< 0.05", "< 0.005"))
# report results
rmarkdown::render("magreport.Rmd",
output_format = "html_document",
output_file = paste0("results/", cur_genome, "_report.html"))
}