comparison mutational_patterns.R @ 19:69f09dff98f9 draft

"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mutational_patterns commit db5fd077ec6c840b7b6eda331820999f85fd7c26"
author artbio
date Sun, 17 Oct 2021 23:05:01 +0000
parents 8d9f31389f33
children a2cb7dc9250a
comparison
equal deleted inserted replaced
18:8d9f31389f33 19:69f09dff98f9
234 cosmic_urls <- read.delim(paste0(opt$tooldir, "cosmic_urls.tsv"), sep = "\t", header = TRUE) 234 cosmic_urls <- read.delim(paste0(opt$tooldir, "cosmic_urls.tsv"), sep = "\t", header = TRUE)
235 cosmic_sbs_file <- cosmic_urls$url[cosmic_urls$genome == opt$genome & 235 cosmic_sbs_file <- cosmic_urls$url[cosmic_urls$genome == opt$genome &
236 cosmic_urls$cosmic_version == opt$cosmic_version] 236 cosmic_urls$cosmic_version == opt$cosmic_version]
237 sbs_signatures <- read.table(paste0(opt$tooldir, cosmic_sbs_file), 237 sbs_signatures <- read.table(paste0(opt$tooldir, cosmic_sbs_file),
238 sep = "\t", header = TRUE) 238 sep = "\t", header = TRUE)
239 tag <- paste(opt$genome, "COSMIC", opt$cosmic_version, sep = " ") 239 tag <- paste(gsub("BSgenome.Hsapiens.UCSC.", "", opt$genome), "COSMIC", opt$cosmic_version, sep = " ")
240 } 240 }
241 # Prepare user-defined signatures 241 # Prepare user-defined signatures
242 if (!is.na(opt$own_signatures)) { 242 if (!is.na(opt$own_signatures)) {
243 sbs_signatures <- read.table(opt$own_signatures, sep = "\t", header = TRUE) 243 sbs_signatures <- read.table(opt$own_signatures, sep = "\t", header = TRUE)
244 tag <- paste(opt$genome, "User-Defined Signatures", sep = " ") 244 tag <- paste(opt$genome, "User-Defined Signatures", sep = " ")
265 sbs_signatures <- as.matrix(sbs_signatures) 265 sbs_signatures <- as.matrix(sbs_signatures)
266 266
267 267
268 # Plot mutational profiles of the COSMIC signatures 268 # Plot mutational profiles of the COSMIC signatures
269 269
270 # to do: this is largely optional and should be graphically improved anyway
271
272 pdf(opt$output_sigpattern, paper = "special", width = 11.69, height = 11.69) 270 pdf(opt$output_sigpattern, paper = "special", width = 11.69, height = 11.69)
273 for (i in head(seq(1, ncol(sbs_signatures), by = 20), -1)) { 271 for (i in head(seq(1, ncol(sbs_signatures), by = 20), -1)) {
274 p6 <- plot_96_profile(sbs_signatures[, i:(i + 19)], condensed = TRUE, ymax = 0.3) 272 p6 <- plot_96_profile(sbs_signatures[, i:(i + 19)], condensed = TRUE, ymax = 0.3)
275 grid.arrange(p6, top = textGrob(paste0(tag, " profiles (", trunc((i + 1) / 20) + 1, " of ", 273 grid.arrange(p6, top = textGrob(paste0(tag, " profiles (", trunc((i + 1) / 20) + 1, " of ",
276 trunc(ncol(sbs_signatures) / 20) + 1, " pages)"), 274 trunc(ncol(sbs_signatures) / 20) + 1, " pages)"),
279 p6 <- plot_96_profile(sbs_signatures[, (trunc(ncol(sbs_signatures) / 20) * 20):(ncol(sbs_signatures))], 277 p6 <- plot_96_profile(sbs_signatures[, (trunc(ncol(sbs_signatures) / 20) * 20):(ncol(sbs_signatures))],
280 condensed = TRUE, ymax = 0.3) 278 condensed = TRUE, ymax = 0.3)
281 grid.arrange(p6, top = textGrob(paste0(tag, " profiles (", trunc(ncol(sbs_signatures) / 20) + 1, " of ", 279 grid.arrange(p6, top = textGrob(paste0(tag, " profiles (", trunc(ncol(sbs_signatures) / 20) + 1, " of ",
282 trunc(ncol(sbs_signatures) / 20) + 1, " pages)"), 280 trunc(ncol(sbs_signatures) / 20) + 1, " pages)"),
283 gp = gpar(fontsize = 12, font = 3))) 281 gp = gpar(fontsize = 12, font = 3)))
282
283
284 # Find optimal contribution of COSMIC signatures to reconstruct 96 mutational profiles 284 # Find optimal contribution of COSMIC signatures to reconstruct 96 mutational profiles
285 pseudo_mut_mat <- mut_mat + 0.0001 # First add a small pseudocount to the mutation count matrix 285 pseudo_mut_mat <- mut_mat + 0.0001 # First add a small pseudocount to the mutation count matrix
286 fit_res <- fit_to_signatures(pseudo_mut_mat, sbs_signatures) 286 fit_res <- fit_to_signatures(pseudo_mut_mat, sbs_signatures)
287 287
288 # Plot contribution barplots 288 # Plot contribution barplots
291 if (is.na(opt$levels)[1]) { # if there are NO levels to display in graphs 291 if (is.na(opt$levels)[1]) { # if there are NO levels to display in graphs
292 pc3_data <- pc3$data 292 pc3_data <- pc3$data
293 pc3 <- ggplot(pc3_data, aes(x = Sample, y = Contribution, fill = as.factor(Signature))) + 293 pc3 <- ggplot(pc3_data, aes(x = Sample, y = Contribution, fill = as.factor(Signature))) +
294 geom_bar(stat = "identity", position = "stack") + 294 geom_bar(stat = "identity", position = "stack") +
295 coord_flip() + 295 coord_flip() +
296 scale_fill_manual(name = "Cosmic\nSignatures", values = signature_colors[]) + 296 scale_fill_manual(name = tag, values = signature_colors[]) +
297 labs(x = "Samples", y = "Absolute contribution") + theme_bw() + 297 labs(x = "Samples", y = "Absolute contribution") + theme_bw() +
298 theme(panel.grid.minor.x = element_blank(), 298 theme(panel.grid.minor.x = element_blank(),
299 panel.grid.major.x = element_blank(), 299 panel.grid.major.x = element_blank(),
300 legend.position = "right", 300 legend.position = "right",
301 text = element_text(size = 8), 301 text = element_text(size = 8),
302 axis.text.x = element_text(angle = 90, hjust = 1)) 302 axis.text.x = element_text(angle = 90, hjust = 1))
303 pc4_data <- pc4$data 303 pc4_data <- pc4$data
304 pc4 <- ggplot(pc4_data, aes(x = Sample, y = Contribution, fill = as.factor(Signature))) + 304 pc4 <- ggplot(pc4_data, aes(x = Sample, y = Contribution, fill = as.factor(Signature))) +
305 geom_bar(stat = "identity", position = "fill") + 305 geom_bar(stat = "identity", position = "fill") +
306 coord_flip() + 306 coord_flip() +
307 scale_fill_manual(name = "Cosmic\nSignatures", values = signature_colors) + 307 scale_fill_manual(name = tag, values = signature_colors) +
308 scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + 308 scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
309 labs(x = "Samples", y = "Relative contribution") + theme_bw() + 309 labs(x = "Samples", y = "Relative contribution") + theme_bw() +
310 theme(panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(), legend.position = "right", 310 theme(panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(), legend.position = "right",
311 text = element_text(size = 8), 311 text = element_text(size = 8),
312 axis.text.x = element_text(angle = 90, hjust = 1)) 312 axis.text.x = element_text(angle = 90, hjust = 1))
316 if (!is.na(opt$levels)[1]) { # if there are levels to display in graphs 316 if (!is.na(opt$levels)[1]) { # if there are levels to display in graphs
317 pc3_data <- pc3$data 317 pc3_data <- pc3$data
318 pc3_data <- merge(pc3_data, metadata_table[, c(1, 3)], by.x = "Sample", by.y = "element_identifier") 318 pc3_data <- merge(pc3_data, metadata_table[, c(1, 3)], by.x = "Sample", by.y = "element_identifier")
319 pc3 <- ggplot(pc3_data, aes(x = Sample, y = Contribution, fill = as.factor(Signature))) + 319 pc3 <- ggplot(pc3_data, aes(x = Sample, y = Contribution, fill = as.factor(Signature))) +
320 geom_bar(stat = "identity", position = "stack") + 320 geom_bar(stat = "identity", position = "stack") +
321 scale_fill_manual(name = "Cosmic\nSignatures", values = signature_colors) + 321 scale_fill_manual(name = tag, values = signature_colors) +
322 labs(x = "Samples", y = "Absolute contribution") + theme_bw() + 322 labs(x = "Samples", y = "Absolute contribution") + theme_bw() +
323 theme(panel.grid.minor.x = element_blank(), 323 theme(panel.grid.minor.x = element_blank(),
324 panel.grid.major.x = element_blank(), 324 panel.grid.major.x = element_blank(),
325 legend.position = "right", 325 legend.position = "right",
326 text = element_text(size = 8), 326 text = element_text(size = 8),
328 facet_grid(~level, scales = "free_x", space = "free") 328 facet_grid(~level, scales = "free_x", space = "free")
329 pc4_data <- pc4$data 329 pc4_data <- pc4$data
330 pc4_data <- merge(pc4_data, metadata_table[, c(1, 3)], by.x = "Sample", by.y = "element_identifier") 330 pc4_data <- merge(pc4_data, metadata_table[, c(1, 3)], by.x = "Sample", by.y = "element_identifier")
331 pc4 <- ggplot(pc4_data, aes(x = Sample, y = Contribution, fill = as.factor(Signature))) + 331 pc4 <- ggplot(pc4_data, aes(x = Sample, y = Contribution, fill = as.factor(Signature))) +
332 geom_bar(stat = "identity", position = "fill") + 332 geom_bar(stat = "identity", position = "fill") +
333 scale_fill_manual(name = "Cosmic\nSignatures", values = signature_colors) + 333 scale_fill_manual(name = tag, values = signature_colors) +
334 scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + 334 scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
335 labs(x = "Samples", y = "Relative contribution") + theme_bw() + 335 labs(x = "Samples", y = "Relative contribution") + theme_bw() +
336 theme(panel.grid.minor.x = element_blank(), 336 theme(panel.grid.minor.x = element_blank(),
337 panel.grid.major.x = element_blank(), 337 panel.grid.major.x = element_blank(),
338 legend.position = "right", 338 legend.position = "right",