|
| 1 | +#' mutSigs |
| 2 | +#' |
| 3 | +#' Calculate and plot the mutational signatures accross samples using the package `deconstructSigs` |
| 4 | +#' @param samples Calculates and plots mutational signatures on a per-sample basis [Default no] |
| 5 | +#' @param pie Plot a pie chart shwoing contribution of each signature to overall profile [Default no] |
| 6 | +#' @import deconstructSigs |
| 7 | +#' @import BSgenome.Dmelanogaster.UCSC.dm6 |
| 8 | +#' @keywords signatures |
| 9 | +#' @export |
| 10 | + |
| 11 | + |
| 12 | +mutSigs <- function(samples=NA, pie=NA){ |
| 13 | + suppressMessages(require(BSgenome.Dmelanogaster.UCSC.dm6)) |
| 14 | + suppressMessages(require(deconstructSigs)) |
| 15 | + |
| 16 | + if(!exists('scaling_factor')){ |
| 17 | + source('R/dmel6.trinucs.R') |
| 18 | + cat("calculationg trinucleotide frequencies in genome\n") |
| 19 | + scaling_factor <-triFreq() |
| 20 | + } |
| 21 | + |
| 22 | + data<-getData() |
| 23 | + genome <- BSgenome.Dmelanogaster.UCSC.dm6 |
| 24 | + |
| 25 | + if(is.na(samples)){ |
| 26 | + data$tissue = 'All' |
| 27 | + sigs.input <- mut.to.sigs.input(mut.ref = data, sample.id = "tissue", chr = "chrom", pos = "pos", alt = "alt", ref = "ref", bsg = genome) |
| 28 | + sig_plot<-whichSignatures(tumor.ref = sigs.input, signatures.ref = signatures.cosmic, sample.id = 'All', |
| 29 | + contexts.needed = TRUE, |
| 30 | + tri.counts.method = scaling_factor |
| 31 | + ) |
| 32 | + |
| 33 | + cat("Writing to file 'plots/all_signatures.pdf'\n") |
| 34 | + pdf('plots/all_signatures.pdf', width = 20, height = 10) |
| 35 | + plotSignatures(sig_plot) |
| 36 | + dev.off() |
| 37 | + plotSignatures(sig_plot) |
| 38 | + |
| 39 | + |
| 40 | + if(!is.na(pie)){ |
| 41 | + makePie(sig_plot) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + else{ |
| 46 | + sigs.input <- mut.to.sigs.input(mut.ref = data, sample.id = "sample", chr = "chrom", pos = "pos", alt = "alt", ref = "ref", bsg = genome) |
| 47 | + cat("sample", "snv_count", sep="\t", "\n") |
| 48 | + for(s in levels(data$sample)) { |
| 49 | + snv_count<-nrow(filter(data, sample == s)) |
| 50 | + |
| 51 | + if(snv_count > 50){ |
| 52 | + cat(s, snv_count, sep="\t", "\n") |
| 53 | + |
| 54 | + sig_plot<-whichSignatures(tumor.ref = sigs.input, signatures.ref = signatures.nature2013, sample.id = s, |
| 55 | + contexts.needed = TRUE, |
| 56 | + tri.counts.method = scaling_factor) |
| 57 | + |
| 58 | + outfile<-(paste('plots/', s, '_signatures.pdf', sep = '')) |
| 59 | + cat("Writing to file", outfile, "\n") |
| 60 | + pdf(outfile, width = 20, height = 10) |
| 61 | + plotSignatures(sig_plot) |
| 62 | + dev.off() |
| 63 | + plotSignatures(sig_plot) |
| 64 | + |
| 65 | + if(!is.na(pie)){ |
| 66 | + makePie(sig_plot) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments