105
|
1 #!/usr/bin/env Rscript
|
|
2
|
|
3 suppressPackageStartupMessages(library("optparse"))
|
|
4
|
|
5 option_list <- list(
|
137
|
6 make_option(c("-i", "--input_dir"), action="store", dest="input_dir", help="IDEAS para files directory"),
|
|
7 make_option(c("-o", "--output_dir"), action="store", dest="output_dir", help="PDF output directory")
|
105
|
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
|
133
|
14 create_heatmap<-function(data_frame, output_file_name) {
|
|
15 # Plot a heatmap for a .para / .state combination
|
|
16 # based on the received data_frame which was created
|
|
17 # by reading the .para file.
|
|
18 num_columns = dim(data_frame)[2];
|
|
19 num_rows = dim(data_frame)[1];
|
136
|
20 p = (sqrt(9 + 8 * (num_columns-1)) - 3) / 2;
|
133
|
21 data_matrix = as.matrix(data_frame[,1+1:p] / data_frame[,1]);
|
135
|
22 colnames(data_matrix) = colnames(data_frame)[1+1:p];
|
136
|
23 histone_marks = colnames(data_matrix);
|
|
24 rownames(data_matrix) = paste(1:num_rows-1, " (", round(data_frame[,1]/sum(data_frame[,1])*10000)/100, "%)", sep="");
|
133
|
25 # Open the output PDF file.
|
|
26 pdf(file=output_file_name);
|
|
27 # Set graphical parameters.
|
|
28 par(mar=c(6, 1, 1, 6));
|
|
29 # Create a vector containing the minimum and maximum values in data_matrix.
|
|
30 min_max_vector = range(data_matrix);
|
136
|
31 # Create a color palette.
|
133
|
32 my_palette = colorRampPalette(c("white", "dark blue"))(n=100);
|
|
33 defpalette = palette(my_palette);
|
|
34 # Plot the heatmap for the current .para / .state combination.
|
135
|
35 plot(NA, NA, xlim=c(0, p+0.7), ylim=c(0, num_rows), xaxt="n", yaxt="n", xlab=NA, ylab=NA, frame.plot=F);
|
|
36 axis(1, at=1:p-0.5, labels=colnames(data_matrix), las=2);
|
|
37 axis(4, at=1:num_rows-0.5, labels=rownames(data_matrix), las=2);
|
136
|
38 color = round((t(data_matrix) - min_max_vector[1]) / (min_max_vector[2] - min_max_vector[1]) * 100);
|
|
39 rect(rep(1:p-1, num_rows), rep(1:num_rows-1, each=p), rep(1:p, num_rows), rep(1:num_rows, each=p), col=color);
|
|
40 histone_mark_color = t(col2rgb(terrain.colors(ceiling(p))[1:p]));
|
133
|
41
|
136
|
42 # Specify a color for common feature names like "h3k4me3".
|
|
43 # These are histone marks frequently used to identify
|
|
44 # promoter activities in a cell, and are often displayed
|
|
45 # in shades of red.
|
|
46 for(i in 1:length(histone_marks)) {
|
|
47 if(regexpr("h3k4me3", tolower(histone_marks[i])) > 0) {
|
|
48 histone_mark_color[i,] = c(255, 0, 0);
|
133
|
49 }
|
136
|
50 if(regexpr("h3k4me2", tolower(histone_marks[i])) > 0) {
|
|
51 histone_mark_color[i,] = c(250, 100, 0);
|
133
|
52 }
|
136
|
53 if(regexpr("h3k4me1", tolower(histone_marks[i])) > 0) {
|
|
54 histone_mark_color[i,] = c(250, 250, 0);
|
133
|
55 }
|
136
|
56 if(regexpr("h3k36me3", tolower(histone_marks[i]))>0) {
|
|
57 histone_mark_color[i,] = c(0, 150, 0);
|
133
|
58 }
|
136
|
59 if(regexpr("h2a", tolower(histone_marks[i])) > 0) {
|
|
60 histone_mark_color[i,] = c(0, 150, 150);
|
133
|
61 }
|
136
|
62 if(regexpr("dnase", tolower(histone_marks[i])) > 0) {
|
|
63 histone_mark_color[i,] = c(0, 200, 200);
|
133
|
64 }
|
136
|
65 if(regexpr("h3k9ac", tolower(histone_marks[i])) > 0) {
|
|
66 histone_mark_color[i,] = c(250, 0, 200);
|
133
|
67 }
|
136
|
68 if(regexpr("h3k9me3", tolower(histone_marks[i])) > 0) {
|
|
69 histone_mark_color[i,] = c(100, 100, 100);
|
133
|
70 }
|
136
|
71 if(regexpr("h3k27ac", tolower(histone_marks[i])) > 0) {
|
|
72 histone_mark_color[i,] = c(250, 150, 0);
|
133
|
73 }
|
136
|
74 if(regexpr("h3k27me3", tolower(histone_marks[i])) > 0) {
|
|
75 histone_mark_color[i,] = c(0, 0, 200);
|
133
|
76 }
|
136
|
77 if(regexpr("h3k79me2", tolower(histone_marks[i])) > 0) {
|
|
78 histone_mark_color[i,] = c(200, 0, 200);
|
133
|
79 }
|
136
|
80 if(regexpr("h4k20me1", tolower(histone_marks[i])) > 0) {
|
|
81 histone_mark_color[i,] = c(50, 200, 50);
|
133
|
82 }
|
136
|
83 if(regexpr("ctcf", tolower(histone_marks[i])) > 0) {
|
|
84 histone_mark_color[i,] = c(200, 0, 250);
|
|
85 }
|
|
86 state_color = get_state_color(data_matrix, histone_mark_color)[,2];
|
118
|
87 }
|
136
|
88 rect(rep(p+0.2, num_rows), 1:num_rows-0.8, rep(p+0.8, num_rows), 1:num_rows-0.2, col=state_color);
|
133
|
89 palette(defpalette);
|
|
90 dev.off();
|
|
91 }
|
118
|
92
|
136
|
93 get_state_color <- function(data_matrix, histone_mark_color) {
|
137
|
94 range_vector = apply(data_matrix, 1, range);
|
136
|
95 mm = NULL;
|
|
96 for(i in 1:dim(data_matrix)[1]) {
|
137
|
97 range_val1 = range_vector[1, i] + 1e-10
|
|
98 range_val2 = range_vector[2, i]
|
|
99 mm = rbind(mm, (data_matrix[i,] - range_val1) / (range_val2 - range_val1));
|
118
|
100 }
|
133
|
101 mm = mm^5;
|
118
|
102 if(dim(mm)[2] > 1) {
|
|
103 mm = mm / (apply(mm, 1, sum) + 1e-10);
|
|
104 }
|
136
|
105 state_color = mm%*%histone_mark_color;
|
|
106 s = apply(data_matrix, 1, max);
|
|
107 s = (s - min(s)) / (max(s) - min(s) + 1e-10);
|
|
108 state_color = round(255 - (255 - state_color) * s/0.5);
|
|
109 state_color[state_color<0] = 0;
|
|
110 rt = paste(state_color[,1], state_color[,2], state_color[,3], sep=",");
|
|
111 h = t(apply(state_color, 1, function(x){rgb2hsv(x[1], x[2], x[3])}));
|
118
|
112 h = apply(h, 1, function(x){hsv(x[1], x[2], x[3])});
|
133
|
113 rt = cbind(rt, h);
|
118
|
114 return(rt);
|
|
115 }
|
|
116
|
121
|
117 # Read the inputs.
|
122
|
118 para_files <- list.files(path=opt$input_dir, pattern="\\.para$", full.names=TRUE);
|
|
119 for (i in 1:length(para_files)) {
|
121
|
120 para_file <- para_files[i];
|
122
|
121 para_file_base_name <- strsplit(para_file, split="/")[[1]][2]
|
|
122 output_file_name <- gsub(".para", ".pdf", para_file_base_name)
|
133
|
123 output_file_path <- paste(opt$output_dir, output_file_name, sep="/");
|
121
|
124 data_frame <- read.table(para_file, comment="!", header=T);
|
122
|
125 create_heatmap(data_frame, output_file_path);
|
121
|
126 }
|