105
|
1 #!/usr/bin/env Rscript
|
|
2
|
141
|
3 create_heatmap<-function(data_frame, output_file_name=NULL) {
|
133
|
4 # Plot a heatmap for a .para / .state combination
|
|
5 # based on the received data_frame which was created
|
|
6 # by reading the .para file.
|
|
7 num_columns = dim(data_frame)[2];
|
|
8 num_rows = dim(data_frame)[1];
|
136
|
9 p = (sqrt(9 + 8 * (num_columns-1)) - 3) / 2;
|
133
|
10 data_matrix = as.matrix(data_frame[,1+1:p] / data_frame[,1]);
|
135
|
11 colnames(data_matrix) = colnames(data_frame)[1+1:p];
|
136
|
12 histone_marks = colnames(data_matrix);
|
|
13 rownames(data_matrix) = paste(1:num_rows-1, " (", round(data_frame[,1]/sum(data_frame[,1])*10000)/100, "%)", sep="");
|
141
|
14 if (!is.null(output_file_name)) {
|
|
15 # Open the output PDF file.
|
|
16 pdf(file=output_file_name);
|
|
17 }
|
133
|
18 # Set graphical parameters.
|
|
19 par(mar=c(6, 1, 1, 6));
|
|
20 # Create a vector containing the minimum and maximum values in data_matrix.
|
|
21 min_max_vector = range(data_matrix);
|
136
|
22 # Create a color palette.
|
133
|
23 my_palette = colorRampPalette(c("white", "dark blue"))(n=100);
|
|
24 defpalette = palette(my_palette);
|
|
25 # Plot the heatmap for the current .para / .state combination.
|
135
|
26 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);
|
|
27 axis(1, at=1:p-0.5, labels=colnames(data_matrix), las=2);
|
|
28 axis(4, at=1:num_rows-0.5, labels=rownames(data_matrix), las=2);
|
144
|
29 col = round((t(data_matrix) - min_max_vector[1]) / (min_max_vector[2] - min_max_vector[1]) * 100);
|
|
30 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=col);
|
136
|
31 histone_mark_color = t(col2rgb(terrain.colors(ceiling(p))[1:p]));
|
133
|
32
|
136
|
33 # Specify a color for common feature names like "h3k4me3".
|
|
34 # These are histone marks frequently used to identify
|
|
35 # promoter activities in a cell, and are often displayed
|
|
36 # in shades of red.
|
|
37 for(i in 1:length(histone_marks)) {
|
|
38 if(regexpr("h3k4me3", tolower(histone_marks[i])) > 0) {
|
|
39 histone_mark_color[i,] = c(255, 0, 0);
|
133
|
40 }
|
136
|
41 if(regexpr("h3k4me2", tolower(histone_marks[i])) > 0) {
|
|
42 histone_mark_color[i,] = c(250, 100, 0);
|
133
|
43 }
|
136
|
44 if(regexpr("h3k4me1", tolower(histone_marks[i])) > 0) {
|
|
45 histone_mark_color[i,] = c(250, 250, 0);
|
133
|
46 }
|
136
|
47 if(regexpr("h3k36me3", tolower(histone_marks[i]))>0) {
|
|
48 histone_mark_color[i,] = c(0, 150, 0);
|
133
|
49 }
|
136
|
50 if(regexpr("h2a", tolower(histone_marks[i])) > 0) {
|
|
51 histone_mark_color[i,] = c(0, 150, 150);
|
133
|
52 }
|
136
|
53 if(regexpr("dnase", tolower(histone_marks[i])) > 0) {
|
|
54 histone_mark_color[i,] = c(0, 200, 200);
|
133
|
55 }
|
136
|
56 if(regexpr("h3k9ac", tolower(histone_marks[i])) > 0) {
|
|
57 histone_mark_color[i,] = c(250, 0, 200);
|
133
|
58 }
|
136
|
59 if(regexpr("h3k9me3", tolower(histone_marks[i])) > 0) {
|
|
60 histone_mark_color[i,] = c(100, 100, 100);
|
133
|
61 }
|
136
|
62 if(regexpr("h3k27ac", tolower(histone_marks[i])) > 0) {
|
|
63 histone_mark_color[i,] = c(250, 150, 0);
|
133
|
64 }
|
136
|
65 if(regexpr("h3k27me3", tolower(histone_marks[i])) > 0) {
|
|
66 histone_mark_color[i,] = c(0, 0, 200);
|
133
|
67 }
|
136
|
68 if(regexpr("h3k79me2", tolower(histone_marks[i])) > 0) {
|
|
69 histone_mark_color[i,] = c(200, 0, 200);
|
133
|
70 }
|
136
|
71 if(regexpr("h4k20me1", tolower(histone_marks[i])) > 0) {
|
|
72 histone_mark_color[i,] = c(50, 200, 50);
|
133
|
73 }
|
136
|
74 if(regexpr("ctcf", tolower(histone_marks[i])) > 0) {
|
|
75 histone_mark_color[i,] = c(200, 0, 250);
|
|
76 }
|
|
77 state_color = get_state_color(data_matrix, histone_mark_color)[,2];
|
118
|
78 }
|
136
|
79 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
|
80 palette(defpalette);
|
141
|
81 if (!is.null(output_file_name)) {
|
|
82 dev.off();
|
|
83 }
|
|
84 return(state_color);
|
133
|
85 }
|
118
|
86
|
136
|
87 get_state_color <- function(data_matrix, histone_mark_color) {
|
137
|
88 range_vector = apply(data_matrix, 1, range);
|
136
|
89 mm = NULL;
|
|
90 for(i in 1:dim(data_matrix)[1]) {
|
141
|
91 range_val1 = range_vector[1, i] + 1e-10;
|
|
92 range_val2 = range_vector[2, i];
|
137
|
93 mm = rbind(mm, (data_matrix[i,] - range_val1) / (range_val2 - range_val1));
|
118
|
94 }
|
133
|
95 mm = mm^5;
|
118
|
96 if(dim(mm)[2] > 1) {
|
|
97 mm = mm / (apply(mm, 1, sum) + 1e-10);
|
|
98 }
|
136
|
99 state_color = mm%*%histone_mark_color;
|
|
100 s = apply(data_matrix, 1, max);
|
|
101 s = (s - min(s)) / (max(s) - min(s) + 1e-10);
|
|
102 state_color = round(255 - (255 - state_color) * s/0.5);
|
|
103 state_color[state_color<0] = 0;
|
|
104 rt = paste(state_color[,1], state_color[,2], state_color[,3], sep=",");
|
|
105 h = t(apply(state_color, 1, function(x){rgb2hsv(x[1], x[2], x[3])}));
|
118
|
106 h = apply(h, 1, function(x){hsv(x[1], x[2], x[3])});
|
133
|
107 rt = cbind(rt, h);
|
118
|
108 return(rt);
|
|
109 }
|