diff create_heatmap.R @ 141:a976dd6fcd1b draft

Uploaded
author greg
date Wed, 20 Dec 2017 11:24:23 -0500
parents 17e8829bbae2
children 8cd08d0aeb69
line wrap: on
line diff
--- a/create_heatmap.R	Tue Dec 19 12:29:29 2017 -0500
+++ b/create_heatmap.R	Wed Dec 20 11:24:23 2017 -0500
@@ -1,17 +1,6 @@
 #!/usr/bin/env Rscript
 
-suppressPackageStartupMessages(library("optparse"))
-
-option_list <- list(
-    make_option(c("-i", "--input_dir"), action="store", dest="input_dir", help="IDEAS para files directory"),
-    make_option(c("-o", "--output_dir"), action="store", dest="output_dir", help="PDF output directory")
-)
-
-parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
-args <- parse_args(parser, positional_arguments=TRUE)
-opt <- args$options
-
-create_heatmap<-function(data_frame, output_file_name) {
+create_heatmap<-function(data_frame, output_file_name=NULL) {
     # Plot a heatmap for a .para / .state combination
     # based on the received data_frame which was created
     # by reading the .para file.
@@ -22,8 +11,10 @@
     colnames(data_matrix) = colnames(data_frame)[1+1:p];
     histone_marks = colnames(data_matrix);
     rownames(data_matrix) = paste(1:num_rows-1, " (", round(data_frame[,1]/sum(data_frame[,1])*10000)/100, "%)", sep="");
-    # Open the output PDF file.
-    pdf(file=output_file_name);
+    if (!is.null(output_file_name)) {
+        # Open the output PDF file.
+        pdf(file=output_file_name);
+    }
     # Set graphical parameters.
     par(mar=c(6, 1, 1, 6));
     # Create a vector containing the minimum and maximum values in data_matrix.
@@ -87,15 +78,18 @@
     }
     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);
     palette(defpalette);
-    dev.off();
+    if (!is.null(output_file_name)) {
+        dev.off();
+    }
+    return(state_color);
 }
 
 get_state_color <- function(data_matrix, histone_mark_color) {
     range_vector = apply(data_matrix, 1, range);
     mm = NULL;
     for(i in 1:dim(data_matrix)[1]) {
-        range_val1 = range_vector[1, i] + 1e-10
-        range_val2 = range_vector[2, i]
+        range_val1 = range_vector[1, i] + 1e-10;
+        range_val2 = range_vector[2, i];
         mm = rbind(mm, (data_matrix[i,] - range_val1) / (range_val2 - range_val1));
     }
     mm = mm^5;
@@ -113,15 +107,3 @@
     rt = cbind(rt, h);
     return(rt);
 }
-
-# Read the inputs.
-para_files <- list.files(path=opt$input_dir, pattern="\\.para$", full.names=TRUE);
-for (i in 1:length(para_files)) {
-    para_file <- para_files[i];
-    para_file_base_name <- strsplit(para_file, split="/")[[1]][2]
-    output_file_base_name <- gsub(".para", "", para_file_base_name)
-    output_file_name <- paste(output_file_base_name, "state", i, "pdf", sep=".")
-    output_file_path <- paste(opt$output_dir, output_file_name, sep="/");
-    data_frame <- read.table(para_file, comment="!", header=T);
-    create_heatmap(data_frame, output_file_path);
-}