|
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"),
|
|
42
|
18 make_option(c("--short_label"), action="store", dest="short_label", help="Hub short label")
|
|
0
|
19 )
|
|
|
20
|
|
|
21 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
|
|
|
22 args <- parse_args(parser, positional_arguments=TRUE)
|
|
|
23 opt <- args$options
|
|
|
24
|
|
39
|
25 create_primary_html = function(output_trackhub, tracks_dir, build) {
|
|
|
26 track_files <- list.files(path=tracks_dir);
|
|
|
27 s <- paste('<html><head></head><body>', sep="\n");
|
|
|
28 s <- paste(s, '<h3>Contents of directory ~/myHub/', build, ' required by UCSC TrackHub</h3>\n', sep="");
|
|
|
29 s <- paste(s, '<ul>\n', sep="")
|
|
|
30 for (i in 1:length(track_files)) {
|
|
|
31 s <- paste(s, '<li><a href="', 'myHub/', build, "/", track_files[i], '">', track_files[i], '</a></li>\n', sep="");
|
|
0
|
32 }
|
|
39
|
33 s <- paste(s, '</ul>\n</body>\n</html>', sep="");
|
|
22
|
34 cat(s, file=output_trackhub);
|
|
20
|
35 }
|
|
|
36
|
|
42
|
37 create_track = function(input_dir_para, input_dir_state, chrom_len_file, base_track_file_name) {
|
|
|
38 para_files <- list.files(path=input_dir_para, full.names=TRUE);
|
|
5
|
39 state_files <- list.files(path=input_dir_state, full.names=TRUE);
|
|
0
|
40 genome_size = read.table(chrom_len_file);
|
|
|
41 g = NULL;
|
|
|
42 for(i in state_files) {
|
|
|
43 tg = as.matrix(fread(i));
|
|
|
44 t = NULL;
|
|
|
45 for(j in 1:dim(genome_size)[1]) {
|
|
|
46 t = c(t, which(tg[,2]==as.character(genome_size[j, 1]) & as.numeric(tg[,4]) > as.numeric(genome_size[j, 2])));
|
|
33
|
47 }
|
|
0
|
48 if (length(t) > 0) {
|
|
|
49 tg = tg[-t,];
|
|
|
50 }
|
|
|
51 t = which(is.na(match(tg[,2], genome_size[,1]))==T);
|
|
|
52 if (length(t)>0) {
|
|
|
53 tg = tg[-t,];
|
|
|
54 }
|
|
|
55 g = rbind(g, tg);
|
|
|
56 }
|
|
|
57 uchr = sort(unique(as.character(g[,2])));
|
|
|
58 g1 = NULL;
|
|
5
|
59 for(i in uchr) {
|
|
0
|
60 t = which(g[,2]==i);
|
|
|
61 g1 = rbind(g1, g[t[order(as.integer(g[t, 3]))],]);
|
|
|
62 }
|
|
|
63 g = NULL;
|
|
|
64 chr = as.character(g1[,2]);
|
|
|
65 posst = as.numeric(g1[,3]);
|
|
|
66 posed = as.numeric(g1[,4]);
|
|
|
67 state = as.matrix(g1[,5:(dim(g1)[2]-1)]);
|
|
20
|
68 state_name = 0:max(state);
|
|
0
|
69 L = dim(g1)[1];
|
|
|
70 n = dim(state)[2];
|
|
|
71 cells = as.character(colnames(g1)[5:(dim(g1)[2]-1)]);
|
|
|
72 g1 = NULL;
|
|
|
73 options(scipen=999);
|
|
|
74 tt = which(chr[2:L]!=chr[2:L-1]);
|
|
11
|
75 tt = c(tt, which(posst[2:L]!=posed[2:L-1]));
|
|
0
|
76 tt = sort(unique(tt));
|
|
|
77 for(i in 1:n) {
|
|
42
|
78 state_color <- get_state_color(para_files[i])
|
|
0
|
79 tstate = state[,i];
|
|
5
|
80 t = c(tt, which(tstate[2:L]!=tstate[2:L-1]));
|
|
0
|
81 t = sort(unique(t));
|
|
|
82 t0 = c(0, t) + 1;
|
|
|
83 t = c(t, L);
|
|
|
84 np = cbind(chr[t], posst[t0], posed[t], tstate[t]);
|
|
20
|
85 track_file_name_bed <- get_track_file_name(base_track_file_name, i, "bed");
|
|
|
86 track_file_name_bigbed <- get_track_file_name(base_track_file_name, i, "bigbed");
|
|
42
|
87 x = cbind(np[, 1:3], state_name[as.integer(np[,4])+1], 1000, ".", np[,2:3], state_color[as.numeric(np[,4])+1]);
|
|
11
|
88 write.table(as.matrix(x), track_file_name_bed, quote=F, row.names=F, col.names=F);
|
|
|
89 system(paste("bedToBigBed ", track_file_name_bed, chrom_len_file, " ", track_file_name_bigbed));
|
|
15
|
90 system(paste("rm ", track_file_name_bed));
|
|
0
|
91 }
|
|
|
92 return(cells);
|
|
|
93 }
|
|
|
94
|
|
42
|
95 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) {
|
|
20
|
96 base_track_file_name <- paste(tracks_dir, hub_name, sep="");
|
|
42
|
97 cells = create_track(input_dir_para, input_dir_state, chrom_len_file, base_track_file_name);
|
|
20
|
98 cell_info = cbind(cells, cells, cells, "#000000");
|
|
|
99 cell_info = array(cell_info, dim=c(length(cells), 4));
|
|
0
|
100 cell_info = as.matrix(cell_info);
|
|
|
101 track_db = NULL;
|
|
25
|
102 for (i in 1:length(cells)) {
|
|
39
|
103 big_data_url <- get_big_data_url(galaxy_url, encoded_dataset_id, tracks_dir, i, build);
|
|
22
|
104 ii = which(cells[i] == cell_info[,1]);
|
|
0
|
105 if (length(ii) == 0) {
|
|
|
106 next;
|
|
|
107 }
|
|
|
108 ii = ii[1];
|
|
20
|
109 # trackDb.txt track entry.
|
|
32
|
110 track_db = c(track_db, paste("hub ", hub_name, "_track_", i, sep=""));
|
|
20
|
111 track_db = c(track_db, "type bigBed");
|
|
39
|
112 track_db = c(track_db, paste("bigDataUrl", big_data_url, sep=" "));
|
|
22
|
113 track_db = c(track_db, paste("shortLabel", short_label, sep=" "));
|
|
|
114 track_db = c(track_db, paste("longLabel", long_label, sep=" "));
|
|
|
115 track_db = c(track_db, paste("priority", ii));
|
|
0
|
116 track_db = c(track_db, "itemRgb on");
|
|
|
117 track_db = c(track_db, "maxItems 100000");
|
|
22
|
118 track_db = c(track_db, paste("color", paste(c(col2rgb(cell_info[ii,4])), collapse=","), sep=" "));
|
|
0
|
119 track_db = c(track_db, "visibility dense");
|
|
33
|
120 track_db = c(track_db, "");
|
|
0
|
121 }
|
|
11
|
122 return(track_db);
|
|
0
|
123 }
|
|
|
124
|
|
39
|
125 get_big_data_url = function(galaxy_url, encoded_dataset_id, tracks_dir, index, build) {
|
|
|
126 track_files <- list.files(path=tracks_dir, pattern="\\.bigbed");
|
|
|
127 s <- paste(galaxy_url, 'datasets/', encoded_dataset_id, '/display/myHub/', build, '/', track_files[index], sep="");
|
|
|
128 return(s)
|
|
|
129 }
|
|
|
130
|
|
42
|
131 get_rgb<-function(statemean, markcolor=NULL)
|
|
|
132 {
|
|
|
133 if(length(markcolor) == 0) {
|
|
|
134 markcolor = rep("",dim(statemean)[2]);
|
|
|
135 markcolor[order(apply(statemean,2,sd),decreasing=T)]=hsv((1:dim(statemean)[2]-1)/dim(statemean)[2],1,1)
|
|
39
|
136 markcolor = t(col2rgb(markcolor));
|
|
|
137 }
|
|
42
|
138
|
|
39
|
139 rg = apply(statemean, 1, range);
|
|
|
140 mm = NULL;
|
|
42
|
141 for(i in 1:dim(statemean)[1]) {
|
|
|
142 mm = rbind(mm, (statemean[i,]-rg[1, i]+1e-10)/(rg[2, i]-rg[1, i]+1e-10));
|
|
39
|
143 }
|
|
42
|
144 mm = mm^5;
|
|
|
145 if (dim(mm)[2]>1) {
|
|
|
146 mm = mm / (apply(mm, 1, sum)+1e-10);
|
|
39
|
147 }
|
|
|
148 mycol = mm%*%markcolor;
|
|
|
149 s = apply(statemean, 1, max);
|
|
42
|
150 s = (s-min(s))/(max(s)-min(s)+1e-10);
|
|
|
151 mycol = round(255-(255-mycol)*s/0.5);
|
|
|
152 mycol[mycol<0] = 0;
|
|
|
153 rt = paste(mycol[,1], mycol[,2], mycol[,3], sep=",");
|
|
|
154 h = t(apply(mycol, 1, function(x){rgb2hsv(x[1], x[2], x[3])}));
|
|
|
155 h = apply(h, 1, function(x){hsv(x[1], x[2], x[3])});
|
|
|
156 rt = cbind(rt, h);
|
|
|
157 return(rt);
|
|
|
158
|
|
|
159 h = t(apply(mycol, 1, function(x){rgb2hsv(x[1], x[2], x[3])}));
|
|
|
160 h[,2] = h[,2]*s;
|
|
|
161 h = apply(h, 1, function(x){hsv(x[1], x[2], x[3])});
|
|
|
162 rt = cbind(apply(t(col2rgb(h)), 1, function(x){paste(x, collapse=",")}), h);
|
|
39
|
163 return(rt);
|
|
|
164 }
|
|
|
165
|
|
42
|
166 get_state_color <- function(para_file, cols=c("white", "dark blue")) {
|
|
|
167 x = read.table(parafile, comment.char="#", header=T);
|
|
|
168 k = dim(x)[2];
|
|
|
169 l = dim(x)[1];
|
|
|
170 p = (sqrt(9+8*(k-1))-3)/2;
|
|
|
171 m = as.matrix(x[,1+1:p]/x[,1]);
|
|
|
172 colnames(m) = colnames(x)[1+1:p];
|
|
|
173 marks = colnames(m);
|
|
|
174 rownames(m) = paste(1:l-1," (",round(x[,1]/sum(x[,1])*10000)/100,"%)",sep="");
|
|
|
175 par(mar=c(6,1,1,6));
|
|
|
176 rg = range(m);
|
|
|
177 colors = 0:100/100*(rg[2]-rg[1])+rg[1];
|
|
|
178 my_palette = colorRampPalette(cols)(n=100);
|
|
|
179 defpalette = palette(my_palette);
|
|
|
180 plot(NA,NA,xlim=c(0,p+0.7),ylim=c(0,l),xaxt="n",yaxt="n",xlab=NA,ylab=NA,frame.plot=F);
|
|
|
181 axis(1,at=1:p-0.5,labels=colnames(m),las=2);
|
|
|
182 axis(4,at=1:l-0.5,labels=rownames(m),las=2);
|
|
|
183 rect(rep(1:p-1,l),rep(1:l-1,each=p),rep(1:p,l),rep(1:l,each=p),col=round((t(m)-rg[1])/(rg[2]-rg[1])*100));
|
|
|
184 markcolor = t(col2rgb(terrain.colors(ceiling(p))[1:p]));
|
|
|
185 for(i in 1:length(marks)) {
|
|
|
186 if (regexpr("h3k4me3",tolower(marks[i]))>0) {
|
|
|
187 markcolor[i,]=c(255,0,0);
|
|
|
188 }
|
|
|
189 if (regexpr("h3k4me2",tolower(marks[i]))>0) {
|
|
|
190 markcolor[i,]=c(250,100,0);
|
|
|
191 }
|
|
|
192 if (regexpr("h3k4me1",tolower(marks[i]))>0) {
|
|
|
193 markcolor[i,]=c(250,250,0);
|
|
|
194 }
|
|
|
195 if (regexpr("h3k36me3",tolower(marks[i]))>0) {
|
|
|
196 markcolor[i,]=c(0,150,0);
|
|
|
197 }
|
|
|
198 if (regexpr("h2a",tolower(marks[i]))>0) {
|
|
|
199 markcolor[i,]=c(0,150,150);
|
|
|
200 }
|
|
|
201 if (regexpr("dnase",tolower(marks[i]))>0) {
|
|
|
202 markcolor[i,]=c(0,200,200);
|
|
|
203 }
|
|
|
204 if (regexpr("atac",tolower(marks[i]))>0) {
|
|
|
205 markcolor[i,]=c(0,200,200);
|
|
|
206 }
|
|
|
207 if (regexpr("h3k9ac",tolower(marks[i]))>0) {
|
|
|
208 markcolor[i,]=c(250,0,200);
|
|
|
209 }
|
|
|
210 if (regexpr("h3k9me3",tolower(marks[i]))>0) {
|
|
|
211 markcolor[i,]=c(100,100,100);
|
|
|
212 }
|
|
|
213 if (regexpr("h3k27ac",tolower(marks[i]))>0) {
|
|
|
214 markcolor[i,]=c(250,150,0);
|
|
|
215 }
|
|
|
216 if (regexpr("h3k27me3",tolower(marks[i]))>0) {
|
|
|
217 markcolor[i,]=c(0,0,225);
|
|
|
218 }
|
|
|
219 if (regexpr("h3k79me2",tolower(marks[i]))>0) {
|
|
|
220 markcolor[i,]=c(200,0,200);
|
|
|
221 }
|
|
|
222 if (regexpr("h4k20me1",tolower(marks[i]))>0) {
|
|
|
223 markcolor[i,]=c(50,200,50);
|
|
|
224 }
|
|
|
225 if (regexpr("ctcf",tolower(marks[i]))>0) {
|
|
|
226 markcolor[i,]=c(200,0,250);
|
|
|
227 }
|
|
|
228 }
|
|
|
229 statecolor = get_rgb(m, markcolor)[,];
|
|
|
230 rect(rep(p+0.2,l),1:l-0.8,rep(p+0.8,l),1:l-0.2,col=statecolor[,2]);
|
|
|
231 palette(defpalette);
|
|
|
232 return(statecolor);
|
|
|
233 }
|
|
|
234
|
|
39
|
235 get_track_file_name = function(base_track_file_name, index, ext) {
|
|
|
236 track_file_name <- paste(base_track_file_name, index, ext, sep=".");
|
|
|
237 return(track_file_name);
|
|
|
238 }
|
|
|
239
|
|
20
|
240 # Create the hub.txt output.
|
|
39
|
241 hub_name_line <- paste("hub ", opt$hub_name, sep="");
|
|
|
242 short_label_line <- paste("shortLabel ", opt$short_label, sep="");
|
|
|
243 long_label_line <- paste("longLabel ", opt$long_label, sep="");
|
|
35
|
244 genomes_txt_line <- paste("genomesFile genomes.txt", sep="");
|
|
39
|
245 email_line <- paste("email ", opt$email, sep="");
|
|
|
246 contents <- paste(hub_name_line, short_label_line, long_label_line, genomes_txt_line, email_line, sep="\n");
|
|
35
|
247 hub_dir <- paste(opt$output_trackhub_files_path, "/", "myHub", "/", sep="");
|
|
22
|
248 dir.create(hub_dir, showWarnings=FALSE);
|
|
35
|
249 hub_file_path <- paste(hub_dir, "hub.txt", sep="");
|
|
20
|
250 write.table(contents, file=hub_file_path, quote=F, row.names=F, col.names=F);
|
|
|
251
|
|
|
252 # Create the genomes.txt output.
|
|
39
|
253 genome_line <- paste("genome ", opt$build, sep="");
|
|
35
|
254 track_db_line <- paste("trackDb ", opt$build, "/", "trackDb.txt", sep="");
|
|
39
|
255 contents <- paste(genome_line, track_db_line, sep="\n");
|
|
30
|
256 genomes_file_path <- paste(hub_dir, "genomes.txt", sep="");
|
|
20
|
257 write.table(contents, file=genomes_file_path, quote=F, row.names=F, col.names=F);
|
|
|
258
|
|
0
|
259 # Create the tracks.
|
|
36
|
260 tracks_dir <- paste(hub_dir, opt$build, "/", sep="");
|
|
11
|
261 dir.create(tracks_dir, showWarnings=FALSE);
|
|
42
|
262 track_db <- create_track_db(opt$galaxy_url, opt$output_trackhub_id, opt$input_dir_para, opt$input_dir_state, opt$build, opt$chrom_len_file, tracks_dir, opt$hub_name, opt$short_label, opt$long_label);
|
|
0
|
263
|
|
20
|
264 # Create the trackDb.txt output.
|
|
35
|
265 track_db_file_path <- paste(tracks_dir, "trackDb.txt", sep="");
|
|
13
|
266 write.table(track_db, file=track_db_file_path, quote=F, row.names=F, col.names=F);
|
|
11
|
267
|
|
20
|
268 # Create the primary HTML dataset.
|
|
42
|
269 create_primary_html(opt$output_trackhub, tracks_dir, opt$build);
|