diff ideas_genome_tracks.R @ 66:f0c488b73dbc draft

Uploaded
author greg
date Wed, 20 Dec 2017 11:26:04 -0500
parents 794d2104f83d
children 06401685fb92
line wrap: on
line diff
--- a/ideas_genome_tracks.R	Tue Dec 19 12:53:24 2017 -0500
+++ b/ideas_genome_tracks.R	Wed Dec 20 11:26:04 2017 -0500
@@ -2,7 +2,6 @@
 
 suppressPackageStartupMessages(library("data.table"))
 suppressPackageStartupMessages(library("optparse"))
-suppressPackageStartupMessages(library("viridisLite"))
 
 option_list <- list(
     make_option(c("--build"), action="store", dest="build", help="Genome build"),
@@ -10,6 +9,7 @@
     make_option(c("--email"),  action="store", dest="email", help="User email address"),
     make_option(c("--galaxy_url"),  action="store", dest="galaxy_url", help="Galaxy instance base URL"),
     make_option(c("--hub_name"),  action="store", dest="hub_name", default=NULL, help="Hub name without spaces"),
+    make_option(c("--input_dir_para"), action="store", dest="input_dir_para", help="Directory containing .para outputs from IDEAS"),
     make_option(c("--input_dir_state"), action="store", dest="input_dir_state", help="Directory containing .state outputs from IDEAS"),
     make_option(c("--long_label"), action="store", dest="long_label", help="Hub long label"),
     make_option(c("--output_trackhub"),  action="store", dest="output_trackhub", help="Output hub file"),
@@ -37,7 +37,7 @@
 }
 
 create_track = function(input_dir_state, chrom_len_file, base_track_file_name) {
-    # Create everythin needed, including the bigbed file,
+    # Create everything needed, including the bigbed file,
     # to render the tracks within the UCSC track hub.
     state_files <- list.files(path=input_dir_state, full.names=TRUE);
     genome_size = read.table(chrom_len_file);
@@ -98,15 +98,12 @@
     return(cells);
 }
 
-create_track_db = function(galaxy_url, encoded_dataset_id, input_dir_state, build, chrom_len_file, tracks_dir, hub_name, short_label, long_label, state_indexes, state_colors) {
+create_track_db = function(galaxy_url, encoded_dataset_id, input_dir_para, input_dir_state, build,
+        chrom_len_file, tracks_dir, hub_name, short_label, long_label, state_indexes, state_colors) {
     # Create a trackDb.txt file that includes each state.
+    para_files <- list.files(path=input_dir_para, full.names=TRUE);
     base_track_file_name <- paste(tracks_dir, hub_name, sep="");
     cells = create_track(input_dir_state, chrom_len_file, base_track_file_name);
-    # Create a a character vector of 1024 viridis color hex codes.
-    # This vector could be used even if the state_colors were received
-    # since colors may not have been chosen for all states.
-    viridis_vector <- viridis(1024, alpha=1, begin=0, end=1, direction=1, option="D");
-    colors_used <- vector();
     if (!is.null(state_indexes)) {
         # Split state_indexes into a list of integers.
         s_indexes <- c();
@@ -126,20 +123,12 @@
     track_db = NULL;
     for (i in 1:length(cells)) {
         if (is.null(state_indexes) || !is.element(i, s_indexes)) {
-            # Generate a random number between 1 and 1024 as
-            # the viridis_vector index for the next state color.
-            color_index <- sample(1:1024, 1);
-            # Make sure the color was not previously chosen.
-            while(is.element(color_index, colors_used)) {
-                color_index <- sample(1:1024, 1);
-            }
-            # Append the color to our list of chosen colors.
-            append(colors_used, color_index);
-            # Get the hex code from viridis_vector.
-            color_hex_code <- viridis_vector[color_index];
+            data_frame <- read.table(para_files[i], comment="!", header=T);
+            color <- create_heatmap(data_frame);
         } else {
             # Use the selected color for the current state.
             color_hex_code <- s_colors[i];
+            color <- paste(c(col2rgb(color_hex_code)), collapse=",");
         }
         big_data_url <- get_big_data_url(galaxy_url, encoded_dataset_id, tracks_dir, i, build);
         # trackDb.txt track entry.
@@ -151,7 +140,7 @@
         track_db = c(track_db, paste("priority", i));
         track_db = c(track_db, "itemRgb on");
         track_db = c(track_db, "maxItems 100000");
-        track_db = c(track_db, paste("color", paste(c(col2rgb(color_hex_code)), collapse=","), sep=" "));
+        track_db = c(track_db, paste("color", color, sep=" "));
         track_db = c(track_db, "visibility dense");
         track_db = c(track_db, "");
     }
@@ -189,9 +178,11 @@
 write.table(contents, file=genomes_file_path, quote=F, row.names=F, col.names=F);
 
 # Create the tracks.
+source("create_heatmap.R");
 tracks_dir <- paste(hub_dir, opt$build, "/", sep="");
 dir.create(tracks_dir, showWarnings=FALSE);
-track_db <- create_track_db(opt$galaxy_url, opt$output_trackhub_id, opt$input_dir_state, opt$build, opt$chrom_len_file, tracks_dir, opt$hub_name, opt$short_label, opt$long_label, opt$state_indexes, opt$state_colors);
+track_db <- create_track_db(opt$galaxy_url, opt$output_trackhub_id, opt$input_dir_state, opt$input_dir_state, opt$build,
+        opt$chrom_len_file, tracks_dir, opt$hub_name, opt$short_label, opt$long_label, opt$state_indexes, opt$state_colors);
 
 # Create the trackDb.txt output.
 track_db_file_path <- paste(tracks_dir, "trackDb.txt", sep="");