Mercurial > repos > artbio > mutational_patterns
comparison mutational_patterns.R @ 15:8182d1625433 draft
"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mutational_patterns commit 6ca5597637439c87b61af2dbd6c38089b29eca37"
| author | artbio |
|---|---|
| date | Sun, 03 Oct 2021 09:29:04 +0000 |
| parents | 56c8869a231e |
| children | 31e7a33ecd71 |
comparison
equal
deleted
inserted
replaced
| 14:56c8869a231e | 15:8182d1625433 |
|---|---|
| 182 # Assign signature names | 182 # Assign signature names |
| 183 colnames(nmf_res$signatures) <- paste0("NewSig_", 1:opt$newsignum) | 183 colnames(nmf_res$signatures) <- paste0("NewSig_", 1:opt$newsignum) |
| 184 rownames(nmf_res$contribution) <- paste0("NewSig_", 1:opt$newsignum) | 184 rownames(nmf_res$contribution) <- paste0("NewSig_", 1:opt$newsignum) |
| 185 # Plot the 96-profile of the signatures: | 185 # Plot the 96-profile of the signatures: |
| 186 p5 <- plot_96_profile(nmf_res$signatures, condensed = TRUE) | 186 p5 <- plot_96_profile(nmf_res$signatures, condensed = TRUE) |
| 187 new_sig_matrix <- reshape2::dcast(p5$data, substitution + context ~ variable, value.var = "value") | 187 new_sig_matrix <- reshape2::dcast(p5$data, substitution + context ~ sample, value.var = "freq") |
| 188 new_sig_matrix <- format(new_sig_matrix, scientific = TRUE) | 188 new_sig_matrix <- format(new_sig_matrix, scientific = TRUE) |
| 189 write.table(new_sig_matrix, file = opt$sigmatrix, quote = FALSE, row.names = FALSE, sep = "\t") | 189 write.table(new_sig_matrix, file = opt$sigmatrix, quote = FALSE, row.names = FALSE, sep = "\t") |
| 190 grid.arrange(p5) | 190 grid.arrange(p5) |
| 191 # Visualize the contribution of the signatures in a barplot | 191 # Visualize the contribution of the signatures in a barplot |
| 192 pc1 <- plot_contribution(nmf_res$contribution, nmf_res$signature, mode = "relative", coord_flip = TRUE) | 192 pc1 <- plot_contribution(nmf_res$contribution, nmf_res$signature, mode = "relative", coord_flip = TRUE) |
| 227 row.names(cancer_signatures) <- cancer_signatures$Somatic.Mutation.Type | 227 row.names(cancer_signatures) <- cancer_signatures$Somatic.Mutation.Type |
| 228 cancer_signatures <- as.matrix(cancer_signatures[, 4:33]) | 228 cancer_signatures <- as.matrix(cancer_signatures[, 4:33]) |
| 229 colnames(cancer_signatures) <- gsub("Signature.", "", colnames(cancer_signatures)) # shorten signature labels | 229 colnames(cancer_signatures) <- gsub("Signature.", "", colnames(cancer_signatures)) # shorten signature labels |
| 230 cosmic_tag <- "Signatures (Cosmic v2, March 2015)" | 230 cosmic_tag <- "Signatures (Cosmic v2, March 2015)" |
| 231 cosmic_colors <- col_vector[1:30] | 231 cosmic_colors <- col_vector[1:30] |
| 232 names(cosmic_colors) <- colnames(cancer_signatures) | |
| 232 } else { | 233 } else { |
| 233 sp_url <- "https://raw.githubusercontent.com/ARTbio/startbio/master/sigProfiler_SBS_signatures_2019_05_22.tsv" | 234 sp_url <- "https://raw.githubusercontent.com/ARTbio/startbio/master/sigProfiler_SBS_signatures_2019_05_22.tsv" |
| 234 cancer_signatures <- read.table(sp_url, sep = "\t", header = TRUE) | 235 cancer_signatures <- read.table(sp_url, sep = "\t", header = TRUE) |
| 235 new_order <- match(row.names(pseudo_mut_mat), cancer_signatures$Somatic.Mutation.Type) | 236 new_order <- match(row.names(pseudo_mut_mat), cancer_signatures$Somatic.Mutation.Type) |
| 236 cancer_signatures <- cancer_signatures[as.vector(new_order), ] | 237 cancer_signatures <- cancer_signatures[as.vector(new_order), ] |
| 237 row.names(cancer_signatures) <- cancer_signatures$Somatic.Mutation.Type | 238 row.names(cancer_signatures) <- cancer_signatures$Somatic.Mutation.Type |
| 238 cancer_signatures <- as.matrix(cancer_signatures[, 4:70]) | 239 cancer_signatures <- as.matrix(cancer_signatures[, 4:70]) |
| 239 colnames(cancer_signatures) <- gsub("SBS", "", colnames(cancer_signatures)) # shorten signature labels | 240 colnames(cancer_signatures) <- gsub("SBS", "", colnames(cancer_signatures)) # shorten signature labels |
| 240 cosmic_tag <- "Signatures (Cosmic v3, May 2019)" | 241 cosmic_tag <- "Signatures (Cosmic v3, May 2019)" |
| 241 cosmic_colors <- col_vector[1:67] | 242 cosmic_colors <- col_vector[1:67] |
| 243 names(cosmic_colors) <- colnames(cancer_signatures) | |
| 242 } | 244 } |
| 243 | 245 |
| 244 # Plot mutational profiles of the COSMIC signatures | 246 # Plot mutational profiles of the COSMIC signatures |
| 245 if (opt$cosmic_version == "v2") { | 247 if (opt$cosmic_version == "v2") { |
| 246 p6 <- plot_96_profile(cancer_signatures, condensed = TRUE, ymax = 0.3) | 248 p6 <- plot_96_profile(cancer_signatures, condensed = TRUE, ymax = 0.3) |
| 257 | 259 |
| 258 # Find optimal contribution of COSMIC signatures to reconstruct 96 mutational profiles | 260 # Find optimal contribution of COSMIC signatures to reconstruct 96 mutational profiles |
| 259 fit_res <- fit_to_signatures(pseudo_mut_mat, cancer_signatures) | 261 fit_res <- fit_to_signatures(pseudo_mut_mat, cancer_signatures) |
| 260 | 262 |
| 261 # Plot contribution barplots | 263 # Plot contribution barplots |
| 262 pc3 <- plot_contribution(fit_res$contribution, cancer_signatures, coord_flip = T, mode = "absolute") | 264 pc3 <- plot_contribution(fit_res$contribution, cancer_signatures, coord_flip = T, mode = "absolute") |
| 263 pc4 <- plot_contribution(fit_res$contribution, cancer_signatures, coord_flip = T, mode = "relative") | 265 pc4 <- plot_contribution(fit_res$contribution, cancer_signatures, coord_flip = T, mode = "relative") |
| 266 if (is.na(opt$levels)[1]) { # if there are NO levels to display in graphs | |
| 264 pc3_data <- pc3$data | 267 pc3_data <- pc3$data |
| 265 pc3 <- ggplot(pc3_data, aes(x = Sample, y = Contribution, fill = as.factor(Signature))) + | 268 pc3 <- ggplot(pc3_data, aes(x = Sample, y = Contribution, fill = as.factor(Signature))) + |
| 266 geom_bar(stat = "identity", position = "stack") + | 269 geom_bar(stat = "identity", position = "stack") + |
| 267 coord_flip() + | 270 coord_flip() + |
| 268 scale_fill_manual(name = "Cosmic\nSignatures", values = cosmic_colors) + | 271 scale_fill_manual(name = "Cosmic\nSignatures", values = cosmic_colors) + |
| 280 scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + | 283 scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + |
| 281 labs(x = "Samples", y = "Relative contribution") + theme_bw() + | 284 labs(x = "Samples", y = "Relative contribution") + theme_bw() + |
| 282 theme(panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(), legend.position = "right", | 285 theme(panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(), legend.position = "right", |
| 283 text = element_text(size = 8), | 286 text = element_text(size = 8), |
| 284 axis.text.x = element_text(angle = 90, hjust = 1)) | 287 axis.text.x = element_text(angle = 90, hjust = 1)) |
| 288 } | |
| 285 ##### | 289 ##### |
| 286 # ggplot2 alternative | 290 # ggplot2 alternative |
| 287 if (!is.na(opt$levels)[1]) { # if there are levels to display in graphs | 291 if (!is.na(opt$levels)[1]) { # if there are levels to display in graphs |
| 288 pc3_data <- pc3$data | 292 pc3_data <- pc3$data |
| 289 pc3_data <- merge(pc3_data, metadata_table[, c(1, 3)], by.x = "Sample", by.y = "element_identifier") | 293 pc3_data <- merge(pc3_data, metadata_table[, c(1, 3)], by.x = "Sample", by.y = "element_identifier") |
| 340 worklist[[i]] <- worklist[[i]][1:opt$signum, ] | 344 worklist[[i]] <- worklist[[i]][1:opt$signum, ] |
| 341 worklist[[i]] <- worklist[[i]][, -length(as.data.frame(worklist[[i]]))] | 345 worklist[[i]] <- worklist[[i]][, -length(as.data.frame(worklist[[i]]))] |
| 342 } | 346 } |
| 343 worklist <- as.data.frame(melt(worklist)) | 347 worklist <- as.data.frame(melt(worklist)) |
| 344 worklist[, 2] <- paste0(worklist[, 4], " - ", worklist[, 2]) | 348 worklist[, 2] <- paste0(worklist[, 4], " - ", worklist[, 2]) |
| 345 head(worklist) | |
| 346 } | 349 } |
| 347 | 350 |
| 348 colnames(worklist) <- c("signature", "sample", "value", "level") | 351 colnames(worklist) <- c("signature", "sample", "value", "level") |
| 349 worklist <- as.data.frame(worklist %>% group_by(sample) %>% mutate(value = value / sum(value) * 100)) | 352 worklist <- as.data.frame(worklist %>% group_by(sample) %>% mutate(value = value / sum(value) * 100)) |
| 350 worklist$pos <- cumsum(worklist$value) - worklist$value / 2 | 353 worklist$pos <- cumsum(worklist$value) - worklist$value / 2 |
| 354 geom_bar(width = 1, stat = "identity") + | 357 geom_bar(width = 1, stat = "identity") + |
| 355 geom_text(aes(label = label), position = position_stack(vjust = 0.5), color = "black", size = 3) + | 358 geom_text(aes(label = label), position = position_stack(vjust = 0.5), color = "black", size = 3) + |
| 356 coord_polar("y", start = 0) + facet_wrap(.~sample) + | 359 coord_polar("y", start = 0) + facet_wrap(.~sample) + |
| 357 labs(x = "", y = "Samples", fill = cosmic_tag) + | 360 labs(x = "", y = "Samples", fill = cosmic_tag) + |
| 358 scale_fill_manual(name = paste0(opt$signum, " most contributing\nsignatures\n(in each label/tissue)"), | 361 scale_fill_manual(name = paste0(opt$signum, " most contributing\nsignatures\n(in each label/tissue)"), |
| 359 values = cosmic_colors[as.numeric(levels(factor(worklist$label)))]) + | 362 values = cosmic_colors[levels(worklist$signature)]) + |
| 360 theme(axis.text = element_blank(), | 363 theme(axis.text = element_blank(), |
| 361 axis.ticks = element_blank(), | 364 axis.ticks = element_blank(), |
| 362 panel.grid = element_blank()) | 365 panel.grid = element_blank()) |
| 363 grid.arrange(p7) | 366 grid.arrange(p7) |
| 364 | 367 |
