5796 |
16 Dec 19 |
nicklas |
#!/usr/bin/env Rscript |
5796 |
16 Dec 19 |
nicklas |
2 |
|
5807 |
10 Jan 20 |
nicklas |
## NOTE! Since we need a specific R version (3.6.2) this script need |
5807 |
10 Jan 20 |
nicklas |
## to be started via the Rscript.sh wrapper. |
5796 |
16 Dec 19 |
nicklas |
5 |
# |
5796 |
16 Dec 19 |
nicklas |
# $Id$ |
5796 |
16 Dec 19 |
nicklas |
# A simple helper script for calculating mutation signature scores |
5796 |
16 Dec 19 |
nicklas |
# based on COSMIC_Cancer_signatures_probabilities.RData |
5796 |
16 Dec 19 |
nicklas |
9 |
# |
5796 |
16 Dec 19 |
nicklas |
# The script need 3 or 4 parameters |
5796 |
16 Dec 19 |
nicklas |
# 1: Name of sample (used as table header and in the plot) |
5796 |
16 Dec 19 |
nicklas |
# 2: Path to VCF file with mutations |
5796 |
16 Dec 19 |
nicklas |
# 3: Path to COSMIC_Cancer_signatures_probabilities.RData |
5796 |
16 Dec 19 |
nicklas |
# 4: (Optional) Path to output pdf file |
5796 |
16 Dec 19 |
nicklas |
15 |
# |
5796 |
16 Dec 19 |
nicklas |
# The script will output scores for each of the 30 signatures as a |
5796 |
16 Dec 19 |
nicklas |
# tab-separated table to stdout |
5796 |
16 Dec 19 |
nicklas |
18 |
# |
5796 |
16 Dec 19 |
nicklas |
19 |
|
5796 |
16 Dec 19 |
nicklas |
## Get command line arguments |
5796 |
16 Dec 19 |
nicklas |
args = commandArgs(trailingOnly=TRUE) |
5796 |
16 Dec 19 |
nicklas |
if (length(args) < 3) stop('At least 3 parameters are required') |
5796 |
16 Dec 19 |
nicklas |
23 |
|
5796 |
16 Dec 19 |
nicklas |
sampleName <- args[1] |
5796 |
16 Dec 19 |
nicklas |
vcfPath <- args[2] |
5796 |
16 Dec 19 |
nicklas |
cosmicSignature <- args[3] |
5796 |
16 Dec 19 |
nicklas |
27 |
|
5796 |
16 Dec 19 |
nicklas |
if (!file.exists(vcfPath)) stop(paste('Can\'t find VCF file: ', vcfPath, sep='')) |
5796 |
16 Dec 19 |
nicklas |
if (!file.exists(cosmicSignature)) stop(paste('Can\'t find COSMIC signature file: ', cosmicSignature, sep='')) |
5796 |
16 Dec 19 |
nicklas |
30 |
|
5796 |
16 Dec 19 |
nicklas |
plot <- length(args) >= 4 |
5796 |
16 Dec 19 |
nicklas |
if (plot) |
5796 |
16 Dec 19 |
nicklas |
33 |
{ |
5796 |
16 Dec 19 |
nicklas |
pdfPath = args[4] |
5796 |
16 Dec 19 |
nicklas |
if (!dir.exists(dirname(pdfPath))) stop(paste('Can`t find directory for PDF file: ', pdfPath, sep='')) |
5796 |
16 Dec 19 |
nicklas |
36 |
} |
5796 |
16 Dec 19 |
nicklas |
37 |
|
5796 |
16 Dec 19 |
nicklas |
## Other configuration options |
5796 |
16 Dec 19 |
nicklas |
ref_genome <- "BSgenome.Hsapiens.UCSC.hg38" |
5796 |
16 Dec 19 |
nicklas |
40 |
|
5796 |
16 Dec 19 |
nicklas |
## Load libraries |
5796 |
16 Dec 19 |
nicklas |
library(GenomeInfoDbData) |
5796 |
16 Dec 19 |
nicklas |
library(MutationalPatterns) |
5796 |
16 Dec 19 |
nicklas |
library(BSgenome) |
5796 |
16 Dec 19 |
nicklas |
library(ref_genome, character.only = TRUE) |
5796 |
16 Dec 19 |
nicklas |
46 |
|
5796 |
16 Dec 19 |
nicklas |
## Load the mutation signatures |
5796 |
16 Dec 19 |
nicklas |
load(cosmicSignature) |
5796 |
16 Dec 19 |
nicklas |
## reduce object matrix to signature columns only |
5796 |
16 Dec 19 |
nicklas |
cancer_signatures = as.matrix(cancer_signatures[,4:33]) |
5796 |
16 Dec 19 |
nicklas |
51 |
|
5796 |
16 Dec 19 |
nicklas |
## read vcf |
5796 |
16 Dec 19 |
nicklas |
vcfs <- read_vcfs_as_granges(vcfPath, sampleName, ref_genome) |
5796 |
16 Dec 19 |
nicklas |
54 |
|
5796 |
16 Dec 19 |
nicklas |
## extract mutations |
5796 |
16 Dec 19 |
nicklas |
mut_mat <- mut_matrix(vcf_list = vcfs, ref_genome = ref_genome) |
5796 |
16 Dec 19 |
nicklas |
57 |
|
5796 |
16 Dec 19 |
nicklas |
## fit mutations to cancer signatures |
5796 |
16 Dec 19 |
nicklas |
fit_res <- fit_to_signatures(mut_mat, cancer_signatures) |
5796 |
16 Dec 19 |
nicklas |
60 |
|
5796 |
16 Dec 19 |
nicklas |
## get contribution |
5796 |
16 Dec 19 |
nicklas |
x <- fit_res$contribution |
5796 |
16 Dec 19 |
nicklas |
63 |
|
5796 |
16 Dec 19 |
nicklas |
## plot as pdf? |
5796 |
16 Dec 19 |
nicklas |
if (plot) |
5796 |
16 Dec 19 |
nicklas |
66 |
{ |
5796 |
16 Dec 19 |
nicklas |
signature.color<-c( |
5796 |
16 Dec 19 |
nicklas |
rgb(85/255,46/255,141/255), #s1 |
5796 |
16 Dec 19 |
nicklas |
rgb(240/255,105/255,160/255), #s2 |
5796 |
16 Dec 19 |
nicklas |
rgb(7/255,83/255,157/255),#s3 |
5796 |
16 Dec 19 |
nicklas |
"lightgray",#s4 |
5796 |
16 Dec 19 |
nicklas |
rgb(140/255,108/255,175/255),#s5 |
5796 |
16 Dec 19 |
nicklas |
rgb(10/255,110/255,56/255),#s6 |
5796 |
16 Dec 19 |
nicklas |
"darkgray",#s7 |
5796 |
16 Dec 19 |
nicklas |
rgb(68/255,147/255,198/255),#s8 |
5796 |
16 Dec 19 |
nicklas |
"beige",#s9 |
5796 |
16 Dec 19 |
nicklas |
"darkslategray",#s10 |
5796 |
16 Dec 19 |
nicklas |
"goldenrod2",#s11 |
5796 |
16 Dec 19 |
nicklas |
"firebrick4",#s12 |
5796 |
16 Dec 19 |
nicklas |
rgb(250/255,197/255,192/255),#s13 |
5796 |
16 Dec 19 |
nicklas |
"azure2",#s14 |
5796 |
16 Dec 19 |
nicklas |
"burlywood3",#s15 |
5796 |
16 Dec 19 |
nicklas |
"darksalmon",#s16 |
5796 |
16 Dec 19 |
nicklas |
rgb(218/255,218/255,234/255),#s17 |
5796 |
16 Dec 19 |
nicklas |
rgb(158/255,154/255,200/255),#s18 |
5796 |
16 Dec 19 |
nicklas |
"darkorchid4",#s19 |
5796 |
16 Dec 19 |
nicklas |
rgb(66/255,171/255,93/255),#s20 |
5796 |
16 Dec 19 |
nicklas |
"orange",#s21 |
5796 |
16 Dec 19 |
nicklas |
"darkred",#s22 |
5796 |
16 Dec 19 |
nicklas |
"black",#s23 |
5796 |
16 Dec 19 |
nicklas |
"chocolate",#s24 |
5796 |
16 Dec 19 |
nicklas |
"cornflowerblue",#s25 |
5796 |
16 Dec 19 |
nicklas |
"cadetblue4",#s26 |
5796 |
16 Dec 19 |
nicklas |
"aquamarine4",#s27 |
5796 |
16 Dec 19 |
nicklas |
"aquamarine",#s28 |
5796 |
16 Dec 19 |
nicklas |
"antiquewhite",#s29 |
5796 |
16 Dec 19 |
nicklas |
"yellow" #s30 |
5796 |
16 Dec 19 |
nicklas |
98 |
) |
5796 |
16 Dec 19 |
nicklas |
99 |
|
5796 |
16 Dec 19 |
nicklas |
names(signature.color) <- paste("Signature.",1:30,sep="") |
5796 |
16 Dec 19 |
nicklas |
101 |
|
5796 |
16 Dec 19 |
nicklas |
main.label.file <- basename(vcfPath) |
5796 |
16 Dec 19 |
nicklas |
103 |
|
5796 |
16 Dec 19 |
nicklas |
####### Plot and calculate proportions |
5796 |
16 Dec 19 |
nicklas |
pdf(pdfPath) |
5796 |
16 Dec 19 |
nicklas |
106 |
|
5796 |
16 Dec 19 |
nicklas |
prop.x <- prop.table(x, 2) |
5796 |
16 Dec 19 |
nicklas |
ii <- match(rownames(prop.x), names(signature.color)) |
5796 |
16 Dec 19 |
nicklas |
signature.color <- signature.color[ii] |
5796 |
16 Dec 19 |
nicklas |
110 |
|
5796 |
16 Dec 19 |
nicklas |
#select signatures with some contribution |
5796 |
16 Dec 19 |
nicklas |
select <- which(rowSums(prop.x) > 0) |
5796 |
16 Dec 19 |
nicklas |
113 |
|
5796 |
16 Dec 19 |
nicklas |
## plot |
5796 |
16 Dec 19 |
nicklas |
my.xlim <- c(0, ncol(prop.x)) |
5796 |
16 Dec 19 |
nicklas |
if (ncol(prop.x)==1) |
5796 |
16 Dec 19 |
nicklas |
117 |
{ |
5796 |
16 Dec 19 |
nicklas |
my.xlim <- c(0, 2) #increases margins on right side |
5796 |
16 Dec 19 |
nicklas |
prop.x <- as.matrix(prop.x[select,]) |
5796 |
16 Dec 19 |
nicklas |
signature.color <- signature.color[select] |
5796 |
16 Dec 19 |
nicklas |
121 |
} |
5796 |
16 Dec 19 |
nicklas |
barplot(prop.x*100, |
5796 |
16 Dec 19 |
nicklas |
xlim=my.xlim, |
5796 |
16 Dec 19 |
nicklas |
ylab="%", |
5796 |
16 Dec 19 |
nicklas |
main=sampleName, |
5796 |
16 Dec 19 |
nicklas |
cex.main=0.7, |
5796 |
16 Dec 19 |
nicklas |
las=2, |
5796 |
16 Dec 19 |
nicklas |
cex.names = 0.6, |
5796 |
16 Dec 19 |
nicklas |
col=signature.color, |
5796 |
16 Dec 19 |
nicklas |
legend.text=rownames(prop.x), |
5796 |
16 Dec 19 |
nicklas |
args.legend=list("top", bg="white", ncol=1, cex=0.7) |
5796 |
16 Dec 19 |
nicklas |
132 |
) |
5796 |
16 Dec 19 |
nicklas |
133 |
|
5796 |
16 Dec 19 |
nicklas |
my.at <- as.numeric(filter(x=c(0,cumsum(round(prop.x[,1], 3)*100)), filter=rep(1, 2)) / 2)[1:length(prop.x[,1])] |
5796 |
16 Dec 19 |
nicklas |
my.sign <- paste("s.", sapply(row.names(prop.x), function(x) strsplit(x, "\\.")[[1]][2]), sep="") |
5796 |
16 Dec 19 |
nicklas |
my.pcr <- paste(round(prop.x[,1], 3)*100, "%", sep="") |
5796 |
16 Dec 19 |
nicklas |
my.labels <- paste(my.pcr, " (", my.sign,")", sep="") |
5796 |
16 Dec 19 |
nicklas |
axis(4, at=my.at, labels=my.labels, las=2, cex.axis=0.7, line=-12, lwd=0) |
5796 |
16 Dec 19 |
nicklas |
139 |
|
5796 |
16 Dec 19 |
nicklas |
mtext(paste("signature proportions:", main.label.file), line=0.5, cex=0.6) |
5796 |
16 Dec 19 |
nicklas |
foo <- dev.off() |
5796 |
16 Dec 19 |
nicklas |
142 |
|
5796 |
16 Dec 19 |
nicklas |
}# end if plot |
5796 |
16 Dec 19 |
nicklas |
144 |
|
5796 |
16 Dec 19 |
nicklas |
## print table to stdout |
5796 |
16 Dec 19 |
nicklas |
write.table(x, file=stdout(), row.names=TRUE, col.names=FALSE, quote=FALSE, sep="\t") |