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