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