|
0
|
1 #!/usr/bin/env Rscript
|
|
|
2
|
|
5
|
3 suppressPackageStartupMessages(library("data.table"))
|
|
0
|
4 suppressPackageStartupMessages(library("optparse"))
|
|
|
5
|
|
|
6 option_list <- list(
|
|
39
|
7 make_option(c("--build"), action="store", dest="build", help="Genome build"),
|
|
|
8 make_option(c("--chrom_len_file"), action="store", dest="chrom_len_file", help="Chromosome length file"),
|
|
|
9 make_option(c("--email"), action="store", dest="email", help="User email address"),
|
|
|
10 make_option(c("--galaxy_url"), action="store", dest="galaxy_url", help="Galaxy instance base URL"),
|
|
|
11 make_option(c("--hub_name"), action="store", dest="hub_name", default=NULL, help="Hub name without spaces"),
|
|
|
12 make_option(c("--input_dir_para"), action="store", dest="input_dir_para", help="Directory containing .para outputs from IDEAS"),
|
|
|
13 make_option(c("--input_dir_state"), action="store", dest="input_dir_state", help="Directory containing .state outputs from IDEAS"),
|
|
|
14 make_option(c("--long_label"), action="store", dest="long_label", help="Hub long label"),
|
|
|
15 make_option(c("--output_trackhub"), action="store", dest="output_trackhub", help="Output hub file"),
|
|
|
16 make_option(c("--output_trackhub_files_path"), action="store", dest="output_trackhub_files_path", help="Output hub extra files path"),
|
|
|
17 make_option(c("--output_trackhub_id"), action="store", dest="output_trackhub_id", help="Encoded output_trackhub dataset id"),
|
|
|
18 make_option(c("--short_label"), action="store", dest="short_label", help="Hub short label"),
|
|
|
19 make_option(c("--track_color"), action="store", dest="track_color", default=NULL, help="User specified track color")
|
|
0
|
20 )
|
|
|
21
|
|
|
22 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
|
|
|
23 args <- parse_args(parser, positional_arguments=TRUE)
|
|
|
24 opt <- args$options
|
|
|
25
|
|
|
26 create_color_scheme = function(input_dir_para) {
|
|
|
27 # Create the color scheme.
|
|
5
|
28 para_files <- list.files(path=input_dir_para, full.names=TRUE);
|
|
0
|
29 mc = NULL;
|
|
5
|
30 x = read.table(para_files[1], comment="!", nrows=1);
|
|
0
|
31 l = as.integer(regexpr("\\*", as.matrix(x)));
|
|
|
32 l = min(which(l>0)) - 2;
|
|
5
|
33 x = as.matrix(read.table(para_files[1]));
|
|
0
|
34 if (length(para_files) > 1) {
|
|
|
35 for (i in 2:length(para_files)) {
|
|
5
|
36 x = x + as.matrix(read.table(para_files[i]));
|
|
0
|
37 }
|
|
|
38 }
|
|
|
39 p = x[,1] / sum(x[,1]);
|
|
|
40 m = array(as.matrix(x[,1:l+1] / x[,1]), dim=c(dim(x)[1], l));
|
|
25
|
41 track_color <- get_track_color(m, mc);
|
|
|
42 return(track_color);
|
|
0
|
43 }
|
|
|
44
|
|
39
|
45 create_primary_html = function(output_trackhub, tracks_dir, build) {
|
|
|
46 track_files <- list.files(path=tracks_dir);
|
|
|
47 s <- paste('<html><head></head><body>', sep="\n");
|
|
|
48 s <- paste(s, '<h3>Contents of directory ~/myHub/', build, ' required by UCSC TrackHub</h3>\n', sep="");
|
|
|
49 s <- paste(s, '<ul>\n', sep="")
|
|
|
50 for (i in 1:length(track_files)) {
|
|
|
51 s <- paste(s, '<li><a href="', 'myHub/', build, "/", track_files[i], '">', track_files[i], '</a></li>\n', sep="");
|
|
0
|
52 }
|
|
39
|
53 s <- paste(s, '</ul>\n</body>\n</html>', sep="");
|
|
22
|
54 cat(s, file=output_trackhub);
|
|
20
|
55 }
|
|
|
56
|
|
25
|
57 create_track = function(input_dir_state, chrom_len_file, base_track_file_name, track_color) {
|
|
5
|
58 state_files <- list.files(path=input_dir_state, full.names=TRUE);
|
|
0
|
59 genome_size = read.table(chrom_len_file);
|
|
|
60 g = NULL;
|
|
|
61 for(i in state_files) {
|
|
|
62 tg = as.matrix(fread(i));
|
|
|
63 t = NULL;
|
|
|
64 for(j in 1:dim(genome_size)[1]) {
|
|
|
65 t = c(t, which(tg[,2]==as.character(genome_size[j, 1]) & as.numeric(tg[,4]) > as.numeric(genome_size[j, 2])));
|
|
33
|
66 }
|
|
0
|
67 if (length(t) > 0) {
|
|
|
68 tg = tg[-t,];
|
|
|
69 }
|
|
|
70 t = which(is.na(match(tg[,2], genome_size[,1]))==T);
|
|
|
71 if (length(t)>0) {
|
|
|
72 tg = tg[-t,];
|
|
|
73 }
|
|
|
74 g = rbind(g, tg);
|
|
|
75 }
|
|
|
76 uchr = sort(unique(as.character(g[,2])));
|
|
|
77 g1 = NULL;
|
|
5
|
78 for(i in uchr) {
|
|
0
|
79 t = which(g[,2]==i);
|
|
|
80 g1 = rbind(g1, g[t[order(as.integer(g[t, 3]))],]);
|
|
|
81 }
|
|
|
82 g = NULL;
|
|
|
83 chr = as.character(g1[,2]);
|
|
|
84 posst = as.numeric(g1[,3]);
|
|
|
85 posed = as.numeric(g1[,4]);
|
|
|
86 state = as.matrix(g1[,5:(dim(g1)[2]-1)]);
|
|
20
|
87 state_name = 0:max(state);
|
|
0
|
88 L = dim(g1)[1];
|
|
|
89 n = dim(state)[2];
|
|
|
90 cells = as.character(colnames(g1)[5:(dim(g1)[2]-1)]);
|
|
|
91 g1 = NULL;
|
|
|
92 options(scipen=999);
|
|
|
93 tt = which(chr[2:L]!=chr[2:L-1]);
|
|
11
|
94 tt = c(tt, which(posst[2:L]!=posed[2:L-1]));
|
|
0
|
95 tt = sort(unique(tt));
|
|
|
96 for(i in 1:n) {
|
|
|
97 tstate = state[,i];
|
|
5
|
98 t = c(tt, which(tstate[2:L]!=tstate[2:L-1]));
|
|
0
|
99 t = sort(unique(t));
|
|
|
100 t0 = c(0, t) + 1;
|
|
|
101 t = c(t, L);
|
|
|
102 np = cbind(chr[t], posst[t0], posed[t], tstate[t]);
|
|
25
|
103 x = cbind(np[,1:3], state_name[as.integer(np[,4])+1], 1000, ".", np[,2:3], track_color[as.numeric(np[,4])+1]);
|
|
20
|
104 track_file_name_bed <- get_track_file_name(base_track_file_name, i, "bed");
|
|
|
105 track_file_name_bigbed <- get_track_file_name(base_track_file_name, i, "bigbed");
|
|
11
|
106 write.table(as.matrix(x), track_file_name_bed, quote=F, row.names=F, col.names=F);
|
|
|
107 system(paste("bedToBigBed ", track_file_name_bed, chrom_len_file, " ", track_file_name_bigbed));
|
|
15
|
108 system(paste("rm ", track_file_name_bed));
|
|
0
|
109 }
|
|
|
110 return(cells);
|
|
|
111 }
|
|
|
112
|
|
39
|
113 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) {
|
|
20
|
114 base_track_file_name <- paste(tracks_dir, hub_name, sep="");
|
|
25
|
115 cells = create_track(input_dir_state, chrom_len_file, base_track_file_name, track_color);
|
|
20
|
116 cell_info = cbind(cells, cells, cells, "#000000");
|
|
|
117 cell_info = array(cell_info, dim=c(length(cells), 4));
|
|
0
|
118 cell_info = as.matrix(cell_info);
|
|
|
119 track_db = NULL;
|
|
25
|
120 for (i in 1:length(cells)) {
|
|
39
|
121 big_data_url <- get_big_data_url(galaxy_url, encoded_dataset_id, tracks_dir, i, build);
|
|
22
|
122 ii = which(cells[i] == cell_info[,1]);
|
|
0
|
123 if (length(ii) == 0) {
|
|
|
124 next;
|
|
|
125 }
|
|
|
126 ii = ii[1];
|
|
20
|
127 # trackDb.txt track entry.
|
|
32
|
128 track_db = c(track_db, paste("hub ", hub_name, "_track_", i, sep=""));
|
|
20
|
129 track_db = c(track_db, "type bigBed");
|
|
39
|
130 track_db = c(track_db, paste("bigDataUrl", big_data_url, sep=" "));
|
|
22
|
131 track_db = c(track_db, paste("shortLabel", short_label, sep=" "));
|
|
|
132 track_db = c(track_db, paste("longLabel", long_label, sep=" "));
|
|
|
133 track_db = c(track_db, paste("priority", ii));
|
|
0
|
134 track_db = c(track_db, "itemRgb on");
|
|
|
135 track_db = c(track_db, "maxItems 100000");
|
|
22
|
136 track_db = c(track_db, paste("color", paste(c(col2rgb(cell_info[ii,4])), collapse=","), sep=" "));
|
|
0
|
137 track_db = c(track_db, "visibility dense");
|
|
33
|
138 track_db = c(track_db, "");
|
|
0
|
139 }
|
|
11
|
140 return(track_db);
|
|
0
|
141 }
|
|
|
142
|
|
39
|
143 get_big_data_url = function(galaxy_url, encoded_dataset_id, tracks_dir, index, build) {
|
|
|
144 track_files <- list.files(path=tracks_dir, pattern="\\.bigbed");
|
|
|
145 s <- paste(galaxy_url, 'datasets/', encoded_dataset_id, '/display/myHub/', build, '/', track_files[index], sep="");
|
|
|
146 return(s)
|
|
|
147 }
|
|
|
148
|
|
|
149 get_hue = function(val, min=0, max=1) {
|
|
|
150 if (is.null(val)) {
|
|
|
151 val = max;
|
|
|
152 } else if (is.na(val)) {
|
|
|
153 val = max;
|
|
|
154 } else if (val < min) {
|
|
|
155 val = min;
|
|
|
156 } else if (val > max) {
|
|
|
157 val = max;
|
|
|
158 }
|
|
|
159 return(val);
|
|
|
160 }
|
|
|
161
|
|
|
162 get_track_color = function(statemean, markcolor=NULL) {
|
|
|
163 sm_width = dim(statemean)[1];
|
|
|
164 sm_height = dim(statemean)[2];
|
|
|
165 if (length(markcolor) == 0) {
|
|
|
166 markcolor = rep("", sm_height);
|
|
|
167 markcolor[order(apply(statemean, 2, sd), decreasing=T)] = hsv((1:sm_height-1)/sm_height, 1, 1);
|
|
|
168 markcolor = t(col2rgb(markcolor));
|
|
|
169 }
|
|
|
170 rg = apply(statemean, 1, range);
|
|
|
171 mm = NULL;
|
|
|
172 for (i in 1:sm_width) {
|
|
|
173 mm = rbind(mm, statemean[i,] - rg[1,i] + 1e-10 / rg[2,i] - rg[1,i] + 1e-10);
|
|
|
174 }
|
|
|
175 mm = mm^6;
|
|
|
176 if(dim(mm)[2] > 1) {
|
|
|
177 mm = mm / (apply(mm, 1, sum) + 1e-10);
|
|
|
178 }
|
|
|
179 mycol = mm%*%markcolor;
|
|
|
180 s = apply(statemean, 1, max);
|
|
|
181 s = (s - min(s)) / (max(s) - min(s) + 1e-10);
|
|
|
182 h = t(apply(mycol, 1, function(x) {rgb2hsv(r=get_hue(x[1]), g=get_hue(x[2]), b=get_hue(x[3]))}));
|
|
|
183 h[,2] = h[,2] * s;
|
|
|
184 h = apply(h, 1, function(x) {hsv(x[1], x[2], x[3])});
|
|
|
185 rt = cbind(apply(t(col2rgb(h)), 1, function(x) {paste(x, collapse=",")}), h);
|
|
|
186 return(rt);
|
|
|
187 }
|
|
|
188
|
|
|
189 get_track_file_name = function(base_track_file_name, index, ext) {
|
|
|
190 track_file_name <- paste(base_track_file_name, index, ext, sep=".");
|
|
|
191 return(track_file_name);
|
|
|
192 }
|
|
|
193
|
|
0
|
194 # Create the color scheme.
|
|
28
|
195 if (length(opt$track_color) == 0) {
|
|
25
|
196 track_color <- create_color_scheme(opt$input_dir_para);
|
|
28
|
197 } else {
|
|
|
198 track_color <- col2rgb(opt$track_color);
|
|
25
|
199 }
|
|
0
|
200
|
|
20
|
201 # Create the hub.txt output.
|
|
39
|
202 hub_name_line <- paste("hub ", opt$hub_name, sep="");
|
|
|
203 short_label_line <- paste("shortLabel ", opt$short_label, sep="");
|
|
|
204 long_label_line <- paste("longLabel ", opt$long_label, sep="");
|
|
35
|
205 genomes_txt_line <- paste("genomesFile genomes.txt", sep="");
|
|
39
|
206 email_line <- paste("email ", opt$email, sep="");
|
|
|
207 contents <- paste(hub_name_line, short_label_line, long_label_line, genomes_txt_line, email_line, sep="\n");
|
|
35
|
208 hub_dir <- paste(opt$output_trackhub_files_path, "/", "myHub", "/", sep="");
|
|
22
|
209 dir.create(hub_dir, showWarnings=FALSE);
|
|
35
|
210 hub_file_path <- paste(hub_dir, "hub.txt", sep="");
|
|
20
|
211 write.table(contents, file=hub_file_path, quote=F, row.names=F, col.names=F);
|
|
|
212
|
|
|
213 # Create the genomes.txt output.
|
|
39
|
214 genome_line <- paste("genome ", opt$build, sep="");
|
|
35
|
215 track_db_line <- paste("trackDb ", opt$build, "/", "trackDb.txt", sep="");
|
|
39
|
216 contents <- paste(genome_line, track_db_line, sep="\n");
|
|
30
|
217 genomes_file_path <- paste(hub_dir, "genomes.txt", sep="");
|
|
20
|
218 write.table(contents, file=genomes_file_path, quote=F, row.names=F, col.names=F);
|
|
|
219
|
|
0
|
220 # Create the tracks.
|
|
36
|
221 tracks_dir <- paste(hub_dir, opt$build, "/", sep="");
|
|
11
|
222 dir.create(tracks_dir, showWarnings=FALSE);
|
|
39
|
223 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);
|
|
0
|
224
|
|
20
|
225 # Create the trackDb.txt output.
|
|
35
|
226 track_db_file_path <- paste(tracks_dir, "trackDb.txt", sep="");
|
|
13
|
227 write.table(track_db, file=track_db_file_path, quote=F, row.names=F, col.names=F);
|
|
11
|
228
|
|
20
|
229 # Create the primary HTML dataset.
|
|
39
|
230 create_primary_html(opt$output_trackhub, tracks_dir, opt$build); |