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