changeset 22:a3740127fc5e draft

Uploaded
author greg
date Mon, 20 Nov 2017 14:48:31 -0500
parents 5201e615dd7f
children 048a6d8d26a8
files ideas_genome_tracks.R
diffstat 1 files changed, 41 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/ideas_genome_tracks.R	Mon Nov 20 14:48:21 2017 -0500
+++ b/ideas_genome_tracks.R	Mon Nov 20 14:48:31 2017 -0500
@@ -40,22 +40,36 @@
     return(state_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_state_color = function(statemean, markcolor=NULL)
-{   
+get_state_color = function(statemean, markcolor=NULL) {   
+    sm_width = dim(statemean)[1];
+    sm_height = dim(statemean)[2];
     if (length(markcolor) == 0) {
-        markcolor = rep("", dim(statemean)[2]);
-        markcolor[order(apply(statemean, 2, sd), decreasing=T)] = hsv((1:dim(statemean)[2]-1)/dim(statemean)[2], 1, 1);
+        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:dim(statemean)[1]) {
-       mm = rbind(mm, (statemean[i,]-rg[1,i])+1e-10)/(rg[2,i]-rg[1,i]+1e-10));
+    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) {
@@ -64,7 +78,7 @@
     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(x[1], x[2], x[3])}));
+    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);
@@ -72,15 +86,14 @@
 }
 
 create_primary_html = function(output_trackhub, tracks_dir) {
-    track_files <- list.files(path=tracks_dir, full.names=TRUE);
-    s <- "<html><head></head><body><ul>"
+    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)) {
-        track_file <- track_files[i];
-        s <- paste(s, "<li><a href=\"", track_file, "\">", track_file, "</a></li>");
-    s <- paste(s, "</ul></body></html>"
-    sink(output_trackhub);
-    cat(s);
-    sink();
+            track_file <- paste("tracks", track_files[i], sep="/");
+            s <- paste(s, '<li>\n<a href="', track_file, '">', track_file, '</a>\n</li>', sep="");
+        }
+    s <- paste(s, '</ul>\n</body>\n</html>', sep="");
+    cat(s, file=output_trackhub);
 }
 
 create_track = function(input_dir_state, chrom_len_file, base_track_file_name, state_color) {
@@ -148,7 +161,7 @@
     track_db = NULL;
     for(i in 1:length(cells)) {
 
-        ii = which(cells[i] == cellinfo[,1]);
+        ii = which(cells[i] == cell_info[,1]);
         if (length(ii) == 0) {
             next;
         }
@@ -156,13 +169,13 @@
         # trackDb.txt track entry.
         track_db = c(track_db, paste(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"));
-        track_db = c(track_db, paste("shortLabel", short_label));
-        track_db = c(track_db, paste("longLabel", long_label);
-        track_db = c(track_db, paste("priority", i));
+        track_db = c(track_db, paste("bigDataUrl", get_track_file_name(base_track_file_name, i, "bigbed"), 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));
         track_db = c(track_db, "itemRgb on");
         track_db = c(track_db, "maxItems 100000");
-        track_db = c(track_db, paste("color", paste(c(col2rgb(cellinfo[ii,4])), collapse=",")));
+        track_db = c(track_db, paste("color", paste(c(col2rgb(cell_info[ii,4])), collapse=","), sep=" "));
         track_db = c(track_db, "visibility dense");
         track_db = c(track_db, ""); 
     }
@@ -173,13 +186,15 @@
 state_color <- create_color_scheme(opt$input_dir_para);
 
 # Create the hub.txt output.
-contents <- c(paste("hub", opt$hub_name), paste("shortLabel",opt$short_label), paste("longLabel", opt$long_label), paste("genomesFile genomes.txt", sep=""), paste("email", opt$email))
-hub_file_path <- paste(opt$output_trackhub_files_path, "/", "hub.txt", sep="")
+contents <- c(paste("hub", opt$hub_name), paste("shortLabel",opt$short_label), paste("longLabel", opt$long_label), paste("genomesFile genomes.txt", sep=""), paste("email", opt$email));
+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.
-contents <- c(paste("genome", opt$build), paste("trackDb ", opt$build, "/", "trackDb.txt", sep=""))
-genomes_file_path <- paste(opt$output_trackhub_files_path, "/", "genomes.txt", sep="")
+contents <- c(paste("genome", opt$build), paste("trackDb ", opt$build, "/", "trackDb.txt", sep=""));
+genomes_file_path <- paste(opt$output_trackhub_files_path, "/", "genomes.txt", sep="");
 write.table(contents, file=genomes_file_path, quote=F, row.names=F, col.names=F);
 
 # Create the tracks.
@@ -188,7 +203,7 @@
 track_db <- create_track_db(opt$input_dir_state, opt$chrom_len_file, tracks_dir, opt$hub_name, opt$short_label, opt$long_label, state_color);
 
 # Create the trackDb.txt output.
-track_db_file_path <- paste(tracks_dir, "/", paste("trackDb.txt", sep=""), sep="")
+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.