-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMake_Heatmaps_SamplesAggregated.R
More file actions
421 lines (340 loc) · 10.9 KB
/
Copy pathMake_Heatmaps_SamplesAggregated.R
File metadata and controls
421 lines (340 loc) · 10.9 KB
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
# =============================================================================================
# Title: Aggregated Microbiome Heatmap - Top 30 Taxa (Relative Abundance)
# Author: Mateusz Glenszczyk
# E-mail: mateusz.glenszczyk@gmail.com
# Project: PhD project - spider-associated microbiome comparison
# =============================================================================================
#
# Description:
# This script generates heatmaps of the top n (30) bacterial taxa (family and genus level)
# based on QIIME2-derived feature tables.
#
# Workflow summary:
# - Taxonomic strings are simplified to the final rank (family/genus) and cleaned
# - Ambiguous taxa (e.g., "uncultured", "unknown", "subgroup", "soil/terrestrial group",
# "Incertae Sedis", "chloroplast", "mitochondria") are removed
# - Raw counts are converted to relative abundance (%)
# - Samples are aggregated into biological groups:
# PRD ENV, PRD SILK, PRD EGGS, PTSD ENV, PTSD SILK, PTSD EGGS
# - Mean relative abundance is calculated per group (to avoid replicate bias)
# - Top 30 taxa are selected based on mean abundance
# - Data are log10-transformed (with pseudocount) for visualization
#
# Heatmap:
# - Rows: taxa
# - Columns: aggregated sample groups (fixed order)
# - Row clustering enabled; column clustering disabled
# - Color scale represents % read abundance (log10 scale)
#
# Output:
# - PDF and high-resolution PNG heatmaps
# - Processed relative abundance tables
#
# Note:
# This script is for visualization only; statistical analyses (e.g., DESeq2)
# should be performed on raw count data.
#
# =============================================================================================
rm(list = ls())
library(pheatmap)
library(grid)
# ============================================================================================
# SETTINGS
# ============================================================================================
workdir <- "/home/Azu/Pulpit/data_NGS/HeatMapy/qiime_heatmap_work"
setwd(workdir)
family_file <- "family_table.filtered.tsv"
genus_file <- "genus_table.filtered.tsv"
top_n <- 30
pseudocount_percent <- 0.01
legend_percent_values <- c(0.01, 0.10, 1.00, 10.00)
legend_breaks <- log10(legend_percent_values)
scale_min <- log10(0.01)
scale_max <- log10(10.00)
scale_breaks <- seq(scale_min, scale_max, length.out = 101)
sample_type_levels <- c(
"PRD ENV",
"PRD SILK",
"PRD EGGS",
"PTSD ENV",
"PTSD SILK",
"PTSD EGGS"
)
sample_type_colors <- c(
"PRD ENV" = "#A1D99B",
"PRD SILK" = "#9ECAE1",
"PRD EGGS" = "#FEE391",
"PTSD ENV" = "#31A354",
"PTSD SILK" = "#3182BD",
"PTSD EGGS" = "#E6AB02"
)
heat_colors <- colorRampPalette(
c("#253494", "#2C7FB8", "#41B6C4", "#A1DAB4", "#FFFFBF", "#FDAE61", "#D7191C")
)(100)
# ============================================================================================
# FUNCTIONS
# ============================================================================================
read_qiime_tsv <- function(file) {
lines <- readLines(file, warn = FALSE)
if (length(lines) == 0) {
stop(paste("Empty file:", file))
}
if (startsWith(lines[1], "#")) {
df <- read.delim(file, skip = 1, check.names = FALSE, stringsAsFactors = FALSE)
} else {
df <- read.delim(file, check.names = FALSE, stringsAsFactors = FALSE)
}
colnames(df)[1] <- "Taxon"
df
}
clean_taxon_name <- function(x) {
x <- trimws(x)
parts <- strsplit(x, ";")[[1]]
x <- tail(parts, 1)
x <- sub("^[a-z]__", "", x, ignore.case = TRUE)
x <- trimws(x)
x <- gsub("^_+$", "", x)
x <- trimws(x)
if (is.na(x) || x == "") {
return(NA_character_)
}
x
}
is_unwanted_taxon <- function(x) {
if (is.na(x) || trimws(x) == "") {
return(TRUE)
}
x2 <- tolower(trimws(x))
patterns <- c(
"^_+$",
"uncultured",
"unclassified",
"unknown",
"incertae[ _-]?sedis",
"subgroup",
"soil[ _-]?group",
"terrestrial[ _-]?group",
"chloroplast",
"mitochondria",
"norank",
"metagenome",
"environmental",
"candidate",
"ambiguous",
"bacteriap[0-9]*",
"unknown_family",
"unknown_genus",
"^[0-9]+-[0-9]+$",
"^[a-z0-9]+-[a-z0-9-]+$"
)
any(sapply(patterns, function(p) grepl(p, x2, perl = TRUE)))
}
get_sample_group <- function(sample_name) {
s <- toupper(trimws(sample_name))
species <- if (grepl("^PRD", s)) {
"PRD"
} else if (grepl("^PTSD", s)) {
"PTSD"
} else {
NA_character_
}
compartment <- if (grepl("EGGS", s)) {
"EGGS"
} else if (grepl("SILK", s)) {
"SILK"
} else if (grepl("(^|[-_])E[0-9]+$", s)) {
"ENV"
} else {
NA_character_
}
if (is.na(species) || is.na(compartment)) {
return(NA_character_)
}
paste(species, compartment)
}
prepare_heatmap_object <- function(file, top_n = 30, pseudocount_percent = 0.01) {
df <- read_qiime_tsv(file)
df <- df[!is.na(df$Taxon), , drop = FALSE]
sample_cols <- setdiff(colnames(df), "Taxon")
for (col in sample_cols) {
df[[col]] <- as.numeric(df[[col]])
df[[col]][is.na(df[[col]])] <- 0
}
df$Taxon <- vapply(df$Taxon, clean_taxon_name, character(1))
df <- df[!is.na(df$Taxon), , drop = FALSE]
df <- df[!vapply(df$Taxon, is_unwanted_taxon, logical(1)), , drop = FALSE]
if (nrow(df) == 0) {
stop(paste("No taxa left after filtering in:", file))
}
# scal zduplikowane nazwy taksonów
agg_tax <- aggregate(df[, sample_cols, drop = FALSE], by = list(Taxon = df$Taxon), FUN = sum)
rownames(agg_tax) <- agg_tax$Taxon
agg_tax$Taxon <- NULL
# relative abundance per sample
col_sums <- colSums(agg_tax)
rel <- sweep(agg_tax, 2, col_sums, "/")
rel[is.na(rel)] <- 0
# sample metadata from names
sample_info <- data.frame(
Sample = colnames(rel),
SampleType = vapply(colnames(rel), get_sample_group, character(1)),
stringsAsFactors = FALSE
)
if (any(is.na(sample_info$SampleType))) {
print(sample_info)
stop("Some samples could not be assigned to Sample Type.")
}
sample_info$SampleType <- factor(sample_info$SampleType, levels = sample_type_levels)
sample_info <- sample_info[order(sample_info$SampleType, sample_info$Sample), , drop = FALSE]
# reorder sample columns
rel <- rel[, sample_info$Sample, drop = FALSE]
# aggregate replicates within Sample Type by MEAN relative abundance
rel_t <- as.data.frame(t(rel))
rel_t$SampleType <- sample_info$SampleType
rel_grouped <- aggregate(. ~ SampleType, data = rel_t, FUN = mean)
rownames(rel_grouped) <- rel_grouped$SampleType
rel_grouped$SampleType <- NULL
rel_grouped <- t(as.matrix(rel_grouped))
rel_grouped <- rel_grouped[, sample_type_levels, drop = FALSE]
# choose top taxa after aggregation
mean_abund <- rowMeans(rel_grouped)
rel_grouped <- rel_grouped[order(mean_abund, decreasing = TRUE), , drop = FALSE]
rel_grouped <- rel_grouped[seq_len(min(top_n, nrow(rel_grouped))), , drop = FALSE]
# output in percent
rel_percent <- rel_grouped * 100
rel_percent_out <- rel_percent
# plotting matrix
rel_percent_plot <- rel_percent
rel_percent_plot[rel_percent_plot < pseudocount_percent] <- pseudocount_percent
rel_plot_log <- log10(rel_percent_plot)
annotation_col <- data.frame(
"Sample Type" = factor(sample_type_levels, levels = sample_type_levels),
check.names = FALSE,
stringsAsFactors = FALSE
)
rownames(annotation_col) <- sample_type_levels
gaps_col <- cumsum(rep(1, length(sample_type_levels)))
gaps_col <- gaps_col[-length(gaps_col)]
list(
mat = rel_plot_log,
rel_percent = rel_percent_out,
annotation_col = annotation_col,
gaps_col = gaps_col
)
}
draw_heatmap <- function(mat, annotation_col, gaps_col, main_title) {
pheatmap(
mat,
scale = "none",
cluster_rows = TRUE,
cluster_cols = FALSE,
annotation_col = annotation_col,
annotation_colors = list("Sample Type" = sample_type_colors),
gaps_col = gaps_col,
show_colnames = TRUE,
show_rownames = TRUE,
fontsize_row = 10,
fontsize_col = 11,
cellwidth = 28,
cellheight = 16,
border_color = "grey85",
treeheight_row = 90,
treeheight_col = 0,
color = heat_colors,
breaks = scale_breaks,
legend_breaks = legend_breaks,
legend_labels = sprintf("%.2f", legend_percent_values),
main = main_title,
angle_col = 45,
silent = TRUE
)
}
save_heatmap_files <- function(obj, title_text, pdf_name, png_name) {
hm <- draw_heatmap(
mat = obj$mat,
annotation_col = obj$annotation_col,
gaps_col = obj$gaps_col,
main_title = title_text
)
# widen legend
legend_id <- which(hm$gtable$layout$name == "legend")
if (length(legend_id) > 0) {
legend_col <- hm$gtable$layout$l[legend_id]
hm$gtable$widths[legend_col] <- unit(2.4, "cm")
}
# remove borders only from top annotation
ann_id <- which(hm$gtable$layout$name %in% c("col_annotation", "annotation_col"))
if (length(ann_id) > 0) {
for (i in ann_id) {
grob <- hm$gtable$grobs[[i]]
if (!is.null(grob$children)) {
for (j in seq_along(grob$children)) {
child <- grob$children[[j]]
if (!is.null(child$gp)) {
child$gp$col <- NA
grob$children[[j]] <- child
}
}
}
if (!is.null(grob$gp)) {
grob$gp$col <- NA
}
hm$gtable$grobs[[i]] <- grob
}
}
pdf(pdf_name, width = 12, height = 11)
grid.newpage()
grid.draw(hm$gtable)
dev.off()
png(png_name, width = 4000, height = 3300, res = 300)
grid.newpage()
grid.draw(hm$gtable)
dev.off()
}
# ============================================================================================
# MAIN
# ============================================================================================
cat("Working directory:\n")
cat(getwd(), "\n\n")
family_obj <- prepare_heatmap_object(
file = family_file,
top_n = top_n,
pseudocount_percent = pseudocount_percent
)
genus_obj <- prepare_heatmap_object(
file = genus_file,
top_n = top_n,
pseudocount_percent = pseudocount_percent
)
cat("=== FAMILY aggregated columns ===\n")
print(colnames(family_obj$mat))
cat("\n=== GENUS aggregated columns ===\n")
print(colnames(genus_obj$mat))
write.table(
family_obj$rel_percent,
file = "family_heatmap_input_percent_aggregated.tsv",
sep = "\t",
quote = FALSE,
col.names = NA
)
write.table(
genus_obj$rel_percent,
file = "genus_heatmap_input_percent_aggregated.tsv",
sep = "\t",
quote = FALSE,
col.names = NA
)
save_heatmap_files(
obj = family_obj,
title_text = "Top 30 bacterial families across aggregated sample types",
pdf_name = "FINAL_family_heatmap_aggregated.pdf",
png_name = "FINAL_family_heatmap_aggregated.png"
)
save_heatmap_files(
obj = genus_obj,
title_text = "Top 30 bacterial genera across aggregated sample types",
pdf_name = "FINAL_genus_heatmap_aggregated.pdf",
png_name = "FINAL_genus_heatmap_aggregated.png"
)
cat("\nSaved files:\n")
print(list.files(pattern = "aggregated"))