comparison create_heatmap.R @ 105:14659ab1e709 draft

Uploaded
author greg
date Tue, 14 Nov 2017 10:34:47 -0500
parents
children c89bf5bf4661
comparison
equal deleted inserted replaced
104:4e3a891ca4d8 105:14659ab1e709
1 #!/usr/bin/env Rscript
2
3 suppressPackageStartupMessages(library("optparse"))
4
5 option_list <- list(
6 make_option(c("-i", "--input"), action="store", dest="input", help="IDEAS para file"),
7 make_option(c("-o", "--output"), action="store", dest="output", help="Output PDF file")
8 )
9
10 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
11 args <- parse_args(parser, positional_arguments=TRUE)
12 opt <- args$options
13
14 create_heatmap<-function(data_matrix, statecolor=NULL, markcolor=NULL, cols=c("white","dark blue")) {
15 k = dim(data_matrix)[2];
16 l = dim(data_matrix)[1];
17 p = (sqrt(9 + 8 * (k - 1)) - 3) / 2;
18 m = as.matrix(data_matrix[,1+1:p]/data_matrix[,1]);
19 marks = colnames(m);
20 rownames(m) = paste(1:l-1," (", round(data_matrix[,1] / sum(data_matrix[,1]) * 10000) / 100, "%)", sep="");
21 pdf(file=opt$output);
22 par(mar=c(6,1,1,6));
23 rg = range(m);
24 colors = 0:100/100*(rg[2]-rg[1])+rg[1];
25 my_palette = colorRampPalette(cols)(n=100);
26 defpalette = palette(my_palette);
27
28 plot(NA, NA, xlim=c(0,p+0.7), ylim=c(0,l), xaxt="n", yaxt="n", xlab=NA, ylab=NA, frame.plot=F);
29 axis(1, at=1:p-0.5, labels=colnames(m), las=2);
30 axis(4, at=1:l-0.5, labels=rownames(m), las=2);
31 rect(rep(1:p-1,l), rep(1:l-1,each=p), rep(1:p,l), rep(1:l,each=p), col=round((t(m)-rg[1])/(rg[2]-rg[1])*100));
32
33 if(length(statecolor)==0) {
34 if(length(markcolor)==0) {
35 markcolor=t(col2rgb(terrain.colors(ceiling(p))[1:p]));
36 for(i in 1:length(marks)) {
37 if(regexpr("h3k4me3",tolower(marks[i]))>0) {
38 markcolor[i,]=c(255,0,0);
39 }
40 if(regexpr("h3k4me2",tolower(marks[i]))>0) {
41 markcolor[i,]=c(250,100,0);
42 }
43 if(regexpr("h3k4me1",tolower(marks[i]))>0) {
44 markcolor[i,]=c(250,250,0);
45 }
46 if(regexpr("h3k36me3",tolower(marks[i]))>0) {
47 markcolor[i,]=c(0,150,0);
48 }
49 if(regexpr("h2a",tolower(marks[i]))>0) {
50 markcolor[i,]=c(0,150,150);
51 }
52 if(regexpr("dnase",tolower(marks[i]))>0) {
53 markcolor[i,]=c(0,200,200);
54 }
55 if(regexpr("h3k9ac",tolower(marks[i]))>0) {
56 markcolor[i,]=c(250,0,200);
57 }
58 if(regexpr("h3k9me3",tolower(marks[i]))>0) {
59 markcolor[i,]=c(100,100,100);
60 }
61 if(regexpr("h3k27ac",tolower(marks[i]))>0) {
62 markcolor[i,]=c(250,150,0);
63 }
64 if(regexpr("h3k27me3",tolower(marks[i]))>0) {
65 markcolor[i,]=c(0,0,200);
66 }
67 if(regexpr("h3k79me2",tolower(marks[i]))>0) {
68 markcolor[i,]=c(200,0,200);
69 }
70 if(regexpr("h4k20me1",tolower(marks[i]))>0) {
71 markcolor[i,]=c(50,200,50);
72 }
73 if(regexpr("ctcf",tolower(marks[i]))>0) {
74 markcolor[i,]=c(200,0,250);
75 }
76 }
77 }
78 statecolor = stateColor(m, markcolor)[,2];
79 }
80 rect(rep(p+0.2,l), 1:l-0.8, rep(p+0.8,l), 1:l-0.2, col=statecolor);
81
82 palette(defpalette);
83 dev.off();
84 #return(statecolor);
85 }
86
87 # Read the input.
88 data_matrix <- read.table(opt$input, header=T);
89 create_heatmap(data_matrix);