-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrainstorm_plot.R
130 lines (104 loc) · 4.36 KB
/
rainstorm_plot.R
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
##########################################
##### Rainstorm calculation - cohort-wide variation of rainfall plots based on a MAF file from many cancer genomes
##### Authors: Ryan Morin and Aixiang Jiang, 2017
########################################
library(argparse)
require(ggplot2)
library(data.table)
parser <- ArgumentParser(description="plot a genomic region with mutations and encode data");
parser$add_argument(
"--file_base_name","-bn",
help="characters before chr# or # in the input file names (required when plotting values from more than one chromosome)"
);
parser$add_argument(
"rainstorm_files",
help="Full path to one or more rainstorm output files", nargs='+', type="character"
);
parser$add_argument(
"--chromosome_name","-c",
help="optionally specify a single chromosome to plot",
default="all");
parser$add_argument(
"--xstart", "-s",
help="limit plot to points after this position",default=-1
);
parser$add_argument(
"--xend", "-e",
help="limit plot to points before this position",default=-1
);
parser$add_argument(
"--plot_style","-P",
help="use a circular or linear axis for your chromosomal coordinate?",
default="linear"
);
args = parser$parse_args();
infile = args$rainstorm_points;
plotstyle = args$plot_style;
chromname = args$chromosome_name;
chromfile = args$rainstorm_chr;
allfiles = args$rainstorm_files;
xmin = as.integer(args$xstart);
xmax=as.integer(args$xend)
separator = args$file_base_name;
if(args$xend<0){
xmax = NA
}else{
xmax = as.integer(args$xend);
}
if(args$xstart<0){
xmin = NA
}else{
xmin=as.integer(args$xstart)
}
load_raw_points <- function(files=NULL,separator=NULL){
points.list = list()
for(f in files){
trimmed = gsub(".tsv","",f)
chrom = gsub(separator,"",trimmed)
print(paste(f,chrom));
points=read.csv(f,sep="\t",stringsAsFactors=F);
points$chromosome = chrom;
points$relative_pos = points$position / max(points$position);
points.list[[chrom]]=points
}
points.all = do.call("rbind",points.list);
}
plot_single_chrom <- function(style="linear",chrom=NULL,base=NULL){
if(style == "circular"){
ggplot(points,aes(x=position,y=mutrate,colour=patient)) + geom_point(alpha=0.2,size=0.2) + theme_classic() + theme(legend.position="none") + ylim(NA,0) + coord_polar(theta ="x");
ggsave(file=paste(base,chrom,style,"_single",".png",sep=""),width=9,height=9)
ggsave(file=paste(base,chrom,style,"_single",".pdf",sep=""),width=9,height=9)
} else{
if(is.na(xmin) && is.na(xmax)){
ggplot(points,aes(x=position,y=mutrate,colour=patient)) + geom_point(alpha=0.2,size=0.2) + theme_classic() + theme(legend.position="none") + ylim(NA,0)
}else{
ggplot(points,aes(x=position,y=mutrate,colour=patient)) + geom_point(alpha=0.2,size=0.2) + theme_classic() + theme(legend.position="none") + ylim(NA,0) + xlim(xmin,xmax)
}
ggsave(file=paste(base,chrom,style,"_single",".png",sep=""),width=16,height=3)
ggsave(file=paste(base,chrom,style,"_single",".pdf",sep=""),width=16,height=3)
}
}
plot_all_chrom <-function(style="linear",basename=separator){
if(style == "circular"){
ggplot(points,aes(x=relative_pos,y=mutrate,colour=patient)) + geom_point(alpha=0.1,size=0.1) + theme_classic() + theme(legend.position="none") + ylim(NA,0) +
coord_polar(theta="x") + facet_wrap(~chromosome,ncol=5); #plot in a 5x5 grid
ggsave(file=paste(basename,"_",style,"_all_chr.pdf",sep=""),width=18,height=18)
}else{
ggplot(points) + geom_point(aes(colour=patient,x=position,y=mutrate),alpha=0.1,size=0.1) + facet_wrap(~chromosome,scale="free_x",ncol=2) +
theme_classic() + theme(legend.position = 'none') + ylim(NA,0)
print(paste("saving as",basename,"allchr_linear.pdf"))
ggsave(file=paste(basename,"allchr_linear.pdf",sep=""),width=14,height=22)
ggsave(file=paste(basename,"allchr_linear.png",sep=""),width=14,height=22)
}
}
if(length(allfiles)>0){
points = load_raw_points(allfiles,separator);
}else{
points=read.csv(infile,sep="\t",stringsAsFactors=F);
}
print(paste("chrom:",chromname))
if(chromname == "all"){
plot_all_chrom(plotstyle);
}else{
plot_single_chrom(plotstyle,chrom=chromname,separator);
}