changeset 39:ecfe907615c5 draft

Uploaded
author greg
date Mon, 11 Dec 2017 13:13:07 -0500
parents 4f1bde55ae1b
children 97c14308fac9
files ideas_genome_tracks.R
diffstat 1 files changed, 84 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/ideas_genome_tracks.R	Mon Dec 11 13:13:00 2017 -0500
+++ b/ideas_genome_tracks.R	Mon Dec 11 13:13:07 2017 -0500
@@ -4,18 +4,19 @@
 suppressPackageStartupMessages(library("optparse"))
 
 option_list <- list(
-    make_option(c("-b", "--build"), action="store", dest="build", help="Genome build"),
-    make_option(c("-c", "--chrom_len_file"), action="store", dest="chrom_len_file", help="Chromosome length file"),
-    make_option(c("-e", "--email"),  action="store", dest="email", help="User email address"),
-    make_option(c("-l", "--long_label"), action="store", dest="long_label", help="Hub long label"),
-    make_option(c("-n", "--hub_name"),  action="store", dest="hub_name", default=NULL, help="Hub name without spaces"),
-    make_option(c("-p", "--input_dir_para"), action="store", dest="input_dir_para", help="Directory containing .para outputs from IDEAS"),
-    make_option(c("-q", "--input_dir_state"), action="store", dest="input_dir_state", help="Directory containing .state outputs from IDEAS"),
-    make_option(c("-s", "--short_label"), action="store", dest="short_label", help="Hub short label"),
-    make_option(c("-t", "--track_color"), action="store", dest="track_color", default=NULL, help="User specified track color"),
-    make_option(c("-u", "--output_track_db"),  action="store", dest="output_track_db", help="Output track db file"),
-    make_option(c("-w", "--output_trackhub"),  action="store", dest="output_trackhub", help="Output hub file"),
-    make_option(c("-x", "--output_trackhub_files_path"),  action="store", dest="output_trackhub_files_path", help="Output hub extra files path")
+            make_option(c("--build"), action="store", dest="build", help="Genome build"),
+            make_option(c("--chrom_len_file"), action="store", dest="chrom_len_file", help="Chromosome length file"),
+            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"),
+            make_option(c("--output_trackhub_files_path"),  action="store", dest="output_trackhub_files_path", help="Output hub extra files path"),
+            make_option(c("--output_trackhub_id"),  action="store", dest="output_trackhub_id", help="Encoded output_trackhub dataset id"),
+            make_option(c("--short_label"), action="store", dest="short_label", help="Hub short label"),
+            make_option(c("--track_color"), action="store", dest="track_color", default=NULL, help="User specified track color")
 )
 
 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
@@ -41,58 +42,15 @@
     return(track_color);
 }
 
-get_hue = function(val, min=0, max=1) {
-    if (is.null(val)) {
-        val = max;
-    } else if (is.na(val)) {
-        val = max;
-    } else if (val < min) {
-        val = min;
-    } else if (val > max) {
-        val = max;
-    }
-    return(val);
-}
-
-get_track_file_name = function(base_track_file_name, index, ext) {
-    track_file_name <- paste(base_track_file_name, index, ext, sep=".");
-    return(track_file_name);
-}
-
-get_track_color = function(statemean, markcolor=NULL) {
-    sm_width = dim(statemean)[1];
-    sm_height = dim(statemean)[2];
-    if (length(markcolor) == 0) {
-        markcolor = rep("", sm_height);
-        markcolor[order(apply(statemean, 2, sd), decreasing=T)] = hsv((1:sm_height-1)/sm_height, 1, 1);
-        markcolor = t(col2rgb(markcolor));
+create_primary_html = function(output_trackhub, tracks_dir, build) {
+    track_files <- list.files(path=tracks_dir);
+    s <- paste('<html><head></head><body>', sep="\n");
+    s <- paste(s, '<h3>Contents of directory ~/myHub/', build, ' required by UCSC TrackHub</h3>\n', sep="");
+    s <- paste(s, '<ul>\n', sep="")
+    for (i in 1:length(track_files)) {
+        s <- paste(s, '<li><a href="', 'myHub/', build, "/", track_files[i], '">', track_files[i], '</a></li>\n', sep="");
     }
-    rg = apply(statemean, 1, range);
-    mm = NULL;
-    for (i in 1:sm_width) {
-       mm = rbind(mm, statemean[i,] - rg[1,i] + 1e-10 / rg[2,i] - rg[1,i]  + 1e-10);
-    }
-    mm = mm^6;
-    if(dim(mm)[2] > 1) {
-        mm = mm / (apply(mm, 1, sum) + 1e-10);
-    }
-    mycol = mm%*%markcolor;
-    s = apply(statemean, 1, max);
-    s = (s - min(s)) / (max(s) - min(s) + 1e-10);
-    h = t(apply(mycol, 1, function(x) {rgb2hsv(r=get_hue(x[1]), g=get_hue(x[2]), b=get_hue(x[3]))}));
-    h[,2] = h[,2] * s;
-    h = apply(h, 1, function(x) {hsv(x[1], x[2], x[3])});
-    rt = cbind(apply(t(col2rgb(h)), 1, function(x) {paste(x, collapse=",")}), h);
-    return(rt);
-}
-
-create_primary_html = function(output_trackhub, tracks_dir) {
-    track_files <- list.files(path=tracks_dir);
-    s <- paste('<html>\n    <head>\n    </head>\n    <body>\n        <ul>\n', sep="");
-        for (i in 1:length(track_files)) {
-            s <- paste(s, '            <li><a href="', track_files[i], '">', track_files[i], '</a></li>\n', sep="");
-        }
-    s <- paste(s, '        </ul>\n    </body>\n</html>', sep="");
+    s <- paste(s, '</ul>\n</body>\n</html>', sep="");
     cat(s, file=output_trackhub);
 }
 
@@ -152,7 +110,7 @@
     return(cells);
 }
 
-create_track_db = function(input_dir_state, chrom_len_file, tracks_dir, hub_name, short_label, long_label, track_color) {
+create_track_db = function(galaxy_url, encoded_dataset_id, input_dir_state, build, chrom_len_file, tracks_dir, hub_name, short_label, long_label, track_color) {
     base_track_file_name <- paste(tracks_dir, hub_name, sep="");
     cells = create_track(input_dir_state, chrom_len_file, base_track_file_name, track_color);
     cell_info = cbind(cells, cells, cells, "#000000");
@@ -160,6 +118,7 @@
     cell_info = as.matrix(cell_info);
     track_db = NULL;
     for (i in 1:length(cells)) {
+        big_data_url <- get_big_data_url(galaxy_url, encoded_dataset_id, tracks_dir, i, build);
         ii = which(cells[i] == cell_info[,1]);
         if (length(ii) == 0) {
             next;
@@ -168,7 +127,7 @@
         # trackDb.txt track entry.
         track_db = c(track_db, paste("hub ", hub_name, "_track_", i, sep=""));
         track_db = c(track_db, "type bigBed");
-        track_db = c(track_db, paste("bigDataUrl", get_track_file_name(base_track_file_name, i, "bigbed"), sep=" "));
+        track_db = c(track_db, paste("bigDataUrl", big_data_url, sep=" "));
         track_db = c(track_db, paste("shortLabel", short_label, sep=" "));
         track_db = c(track_db, paste("longLabel", long_label, sep=" "));
         track_db = c(track_db, paste("priority", ii));
@@ -181,6 +140,57 @@
     return(track_db);
 }
 
+get_big_data_url = function(galaxy_url, encoded_dataset_id, tracks_dir, index, build) {
+    track_files <- list.files(path=tracks_dir, pattern="\\.bigbed");
+    s <- paste(galaxy_url, 'datasets/', encoded_dataset_id, '/display/myHub/', build, '/', track_files[index], sep="");
+    return(s)
+}
+
+get_hue = function(val, min=0, max=1) {
+    if (is.null(val)) {
+        val = max;
+    } else if (is.na(val)) {
+        val = max;
+    } else if (val < min) {
+        val = min;
+    } else if (val > max) {
+        val = max;
+    }
+    return(val);
+}
+
+get_track_color = function(statemean, markcolor=NULL) {
+    sm_width = dim(statemean)[1];
+    sm_height = dim(statemean)[2];
+    if (length(markcolor) == 0) {
+        markcolor = rep("", sm_height);
+        markcolor[order(apply(statemean, 2, sd), decreasing=T)] = hsv((1:sm_height-1)/sm_height, 1, 1);
+        markcolor = t(col2rgb(markcolor));
+    }
+    rg = apply(statemean, 1, range);
+    mm = NULL;
+    for (i in 1:sm_width) {
+        mm = rbind(mm, statemean[i,] - rg[1,i] + 1e-10 / rg[2,i] - rg[1,i]  + 1e-10);
+    }
+    mm = mm^6;
+    if(dim(mm)[2] > 1) {
+        mm = mm / (apply(mm, 1, sum) + 1e-10);
+    }
+    mycol = mm%*%markcolor;
+    s = apply(statemean, 1, max);
+    s = (s - min(s)) / (max(s) - min(s) + 1e-10);
+    h = t(apply(mycol, 1, function(x) {rgb2hsv(r=get_hue(x[1]), g=get_hue(x[2]), b=get_hue(x[3]))}));
+    h[,2] = h[,2] * s;
+    h = apply(h, 1, function(x) {hsv(x[1], x[2], x[3])});
+    rt = cbind(apply(t(col2rgb(h)), 1, function(x) {paste(x, collapse=",")}), h);
+    return(rt);
+}
+
+get_track_file_name = function(base_track_file_name, index, ext) {
+    track_file_name <- paste(base_track_file_name, index, ext, sep=".");
+    return(track_file_name);
+}
+
 # Create the color scheme.
 if (length(opt$track_color) == 0) {
     track_color <- create_color_scheme(opt$input_dir_para);
@@ -189,33 +199,32 @@
 }
 
 # Create the hub.txt output.
-hub_name_line <- paste("hub ", opt$hub_name, sep = "");
-short_label_line <- paste("shortLabel ", opt$short_label, sep = "");
-long_label_line <- paste("longLabel ", opt$longLabel, sep = "");
+hub_name_line <- paste("hub ", opt$hub_name, sep="");
+short_label_line <- paste("shortLabel ", opt$short_label, sep="");
+long_label_line <- paste("longLabel ", opt$long_label, sep="");
 genomes_txt_line <- paste("genomesFile genomes.txt", sep="");
-email_line <- paste("email ", opt$email, sep = "");
-contents <- c(paste(hub_name_line, short_label_line, long_label_line, genomes_txt_line, email_line), sep="");
+email_line <- paste("email ", opt$email, sep="");
+contents <- paste(hub_name_line, short_label_line, long_label_line, genomes_txt_line, email_line, sep="\n");
 hub_dir <- paste(opt$output_trackhub_files_path, "/", "myHub", "/", sep="");
 dir.create(hub_dir, showWarnings=FALSE);
 hub_file_path <- paste(hub_dir, "hub.txt", sep="");
 write.table(contents, file=hub_file_path, quote=F, row.names=F, col.names=F);
 
 # Create the genomes.txt output.
-genome_line <- paste("genome", opt$build, sep="");
+genome_line <- paste("genome ", opt$build, sep="");
 track_db_line <- paste("trackDb ", opt$build, "/", "trackDb.txt", sep="");
-contents <- c(paste(genome_line, track_db_line), sep="");
+contents <- paste(genome_line, track_db_line, sep="\n");
 genomes_file_path <- paste(hub_dir, "genomes.txt", sep="");
 write.table(contents, file=genomes_file_path, quote=F, row.names=F, col.names=F);
 
 # Create the tracks.
 tracks_dir <- paste(hub_dir, opt$build, "/", sep="");
 dir.create(tracks_dir, showWarnings=FALSE);
-track_db <- create_track_db(opt$input_dir_state, opt$chrom_len_file, tracks_dir, opt$hub_name, opt$short_label, opt$long_label, track_color);
+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, track_color);
 
 # Create the trackDb.txt output.
 track_db_file_path <- paste(tracks_dir, "trackDb.txt", sep="");
 write.table(track_db, file=track_db_file_path, quote=F, row.names=F, col.names=F);
 
 # Create the primary HTML dataset.
-cat("tracks_dir: ", tracks_dir, "\n")
-create_primary_html(opt$output_trackhub, tracks_dir);
+create_primary_html(opt$output_trackhub, tracks_dir, opt$build);
\ No newline at end of file