|
0
|
1 #!/usr/bin/env Rscript
|
|
|
2
|
|
5
|
3 suppressPackageStartupMessages(library("data.table"))
|
|
0
|
4 suppressPackageStartupMessages(library("optparse"))
|
|
|
5
|
|
|
6 option_list <- list(
|
|
83
|
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_long_label"), action="store", dest="hub_long_label", help="Hub long label"),
|
|
|
12 make_option(c("--hub_name"), action="store", dest="hub_name", default=NULL, help="Hub name without spaces"),
|
|
|
13 make_option(c("--hub_short_label"), action="store", dest="hub_short_label", help="Hub short label"),
|
|
|
14 make_option(c("--input_dir_para"), action="store", dest="input_dir_para", help="Directory containing .para outputs from IDEAS"),
|
|
|
15 make_option(c("--input_dir_state"), action="store", dest="input_dir_state", help="Directory containing .state outputs from IDEAS"),
|
|
|
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("--script_dir"), action="store", dest="script_dir", help="R script source directory"),
|
|
|
20 make_option(c("--state_colors"), action="store", dest="state_colors", default=NULL, help="List of state colors"),
|
|
|
21 make_option(c("--state_indexes"), action="store", dest="state_indexes", default=NULL, help="List of state indexes"),
|
|
|
22 make_option(c("--state_names"), action="store", dest="state_names", default=NULL, help="List of state names")
|
|
0
|
23 )
|
|
|
24
|
|
|
25 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
|
|
|
26 args <- parse_args(parser, positional_arguments=TRUE)
|
|
|
27 opt <- args$options
|
|
|
28
|
|
78
|
29 create_primary_html = function(output_trackhub, tracks_dir, build) {
|
|
|
30 track_files <- list.files(path=tracks_dir);
|
|
54
|
31 s <- paste('<html><head></head><body>', sep="\n");
|
|
78
|
32 s <- paste(s, '<h3>Contents of directory ~/myHub/', build, ' required by UCSC TrackHub</h3>\n', sep="");
|
|
54
|
33 s <- paste(s, '<ul>\n', sep="")
|
|
78
|
34 for (i in 1:length(track_files)) {
|
|
|
35 s <- paste(s, '<li><a href="', 'myHub/', build, "/", track_files[i], '">', track_files[i], '</a></li>\n', sep="");
|
|
54
|
36 }
|
|
|
37 s <- paste(s, '</ul>\n</body>\n</html>', sep="");
|
|
|
38 cat(s, file=output_trackhub);
|
|
20
|
39 }
|
|
|
40
|
|
77
|
41 create_cells = function(input_dir_state, chrom_len_file, base_track_file_name, state_indexes, state_names, state_colors) {
|
|
66
|
42 # Create everything needed, including the bigbed file,
|
|
54
|
43 # to render the tracks within the UCSC track hub.
|
|
83
|
44 state_files = list.files(path=input_dir_state, full.names=TRUE);
|
|
|
45 cell_type_names = get_cell_type_names(state_files[1]);
|
|
54
|
46 genome_size = read.table(chrom_len_file);
|
|
|
47 g = NULL;
|
|
|
48 for(i in state_files) {
|
|
|
49 tg = as.matrix(fread(i));
|
|
|
50 t = NULL;
|
|
|
51 for(j in 1:dim(genome_size)[1]) {
|
|
|
52 t = c(t, which(tg[,2]==as.character(genome_size[j, 1]) & as.numeric(tg[,4]) > as.numeric(genome_size[j, 2])));
|
|
|
53 }
|
|
|
54 if (length(t) > 0) {
|
|
|
55 tg = tg[-t,];
|
|
|
56 }
|
|
|
57 t = which(is.na(match(tg[,2], genome_size[,1]))==T);
|
|
|
58 if (length(t)>0) {
|
|
|
59 tg = tg[-t,];
|
|
|
60 }
|
|
|
61 g = rbind(g, tg);
|
|
|
62 }
|
|
|
63 uchr = sort(unique(as.character(g[,2])));
|
|
|
64 g1 = NULL;
|
|
|
65 for(i in uchr) {
|
|
|
66 t = which(g[,2]==i);
|
|
|
67 g1 = rbind(g1, g[t[order(as.integer(g[t, 3]))],]);
|
|
|
68 }
|
|
|
69 g = NULL;
|
|
|
70 chr = as.character(g1[,2]);
|
|
|
71 posst = as.numeric(g1[,3]);
|
|
|
72 posed = as.numeric(g1[,4]);
|
|
|
73 state = as.matrix(g1[,5:(dim(g1)[2]-1)]);
|
|
|
74 L = dim(g1)[1];
|
|
83
|
75 # Here n will be the same length as the
|
|
|
76 # list of cell_type_names defined above.
|
|
54
|
77 n = dim(state)[2];
|
|
|
78 cells = as.character(colnames(g1)[5:(dim(g1)[2]-1)]);
|
|
|
79 options(scipen=999);
|
|
|
80 tt = which(chr[2:L]!=chr[2:L-1]);
|
|
|
81 tt = c(tt, which(posst[2:L]!=posed[2:L-1]));
|
|
|
82 tt = sort(unique(tt));
|
|
|
83 for(i in 1:n) {
|
|
83
|
84 state_name = cell_type_names[i];
|
|
54
|
85 tstate = state[,i];
|
|
|
86 t = c(tt, which(tstate[2:L]!=tstate[2:L-1]));
|
|
|
87 t = sort(unique(t));
|
|
|
88 t0 = c(0, t) + 1;
|
|
|
89 t = c(t, L);
|
|
|
90 np = cbind(chr[t], posst[t0], posed[t], tstate[t]);
|
|
57
|
91 track_file_name_bed_unsorted <- get_track_file_name(base_track_file_name, i, "bed_unsorted");
|
|
58
|
92 track_file_name_bed <- get_track_file_name(base_track_file_name, i, "bed");
|
|
56
|
93 track_file_name_bigbed <- get_track_file_name(base_track_file_name, i, "bigbed");
|
|
83
|
94 x = cbind(np[, 1:3], state_name, 1000, ".", np[,2:3]);
|
|
57
|
95 write.table(as.matrix(x), track_file_name_bed_unsorted, quote=F, row.names=F, col.names=F);
|
|
59
|
96 cmd = paste("LC_COLLATE=C sort -k1,1 -k2,2n < ", track_file_name_bed_unsorted, " > ", track_file_name_bed);
|
|
|
97 system(cmd);
|
|
|
98 cmd = paste("bedToBigBed ", track_file_name_bed, chrom_len_file, " ", track_file_name_bigbed);
|
|
|
99 system(cmd);
|
|
65
|
100 system(paste("rm ", track_file_name_bed_unsorted));
|
|
|
101 system(paste("rm ", track_file_name_bed));
|
|
54
|
102 }
|
|
|
103 return(cells);
|
|
0
|
104 }
|
|
|
105
|
|
77
|
106 create_track_db = function(galaxy_url, encoded_dataset_id, input_dir_para, input_dir_state, build, chrom_len_file,
|
|
|
107 tracks_dir, hub_name, hub_short_label, hub_long_label, state_indexes, state_names, state_colors) {
|
|
54
|
108 # Create a trackDb.txt file that includes each state.
|
|
80
|
109 s_indexes = c();
|
|
|
110 s_names = c();
|
|
|
111 s_colors = c();
|
|
|
112 if (is.null(state_indexes)) {
|
|
|
113 # Generate state colors automatically.
|
|
|
114 index = 0;
|
|
|
115 para_files = list.files(path=input_dir_para, full.names=TRUE);
|
|
|
116 for (para_file in para_files) {
|
|
|
117 data_frame = read.table(para_file, comment="!", header=T);
|
|
|
118 color_hex_code = create_heatmap(data_frame);
|
|
|
119 s_indexes = c(s_indexes, index);
|
|
|
120 s_names = c(s_names, toString(index));
|
|
|
121 s_colors = c(s_colors, color_hex_code);
|
|
|
122 }
|
|
|
123 } else {
|
|
61
|
124 # Split state_indexes into a list of integers.
|
|
80
|
125 index_str = as.character(state_indexes);
|
|
|
126 items = strsplit(index_str, ",")
|
|
61
|
127 for (item in items) {
|
|
80
|
128 s_indexes = c(s_indexes, as.integer(item));
|
|
61
|
129 }
|
|
77
|
130 # Split state_names into a list of strings.
|
|
83
|
131 name_str = as.character(state_names);
|
|
80
|
132 items = strsplit(name_str, ",");
|
|
|
133 item_index = 0;
|
|
77
|
134 for (item in items) {
|
|
|
135 item_index = item_index + 1;
|
|
|
136 # Handle the special string "use state index".
|
|
|
137 if (identical(item, "use state index")) {
|
|
80
|
138 item = s_indexes[item_index];
|
|
77
|
139 }
|
|
80
|
140 s_names = c(s_names, item);
|
|
77
|
141 }
|
|
61
|
142 # Split state_colors into a list of strings.
|
|
80
|
143 color_str = as.character(state_colors);
|
|
|
144 items = strsplit(color_str, ",");
|
|
61
|
145 for (item in items) {
|
|
80
|
146 s_colors = c(s_colors, item);
|
|
61
|
147 }
|
|
|
148 }
|
|
77
|
149 base_track_file_name <- paste(tracks_dir, hub_name, sep="");
|
|
|
150 cells = create_cells(input_dir_state, chrom_len_file, base_track_file_name, s_indexes, s_names, s_colors);
|
|
83
|
151 cell_info = cbind(cells, cells, cells, "#000000");
|
|
|
152 cell_info = array(cell_info, dim=c(length(cells), 4));
|
|
|
153 cell_info = as.matrix(cell_info);
|
|
54
|
154 track_db = NULL;
|
|
|
155 for (i in 1:length(cells)) {
|
|
83
|
156 ii = which(cells[i] == cell_info[,1]);
|
|
|
157 if(length(ii) == 0) {
|
|
|
158 next;
|
|
|
159 }
|
|
|
160 ii = ii[1];
|
|
71
|
161 # Get the color for the current state.
|
|
80
|
162 color <- paste(c(col2rgb(s_colors[i])), collapse=",");
|
|
71
|
163 # Get the bigDataUrl.
|
|
54
|
164 big_data_url <- get_big_data_url(galaxy_url, encoded_dataset_id, tracks_dir, i, build);
|
|
71
|
165 # trackDb.txt track hub entry.
|
|
54
|
166 track_db = c(track_db, paste("track ", hub_name, "_track_", i, sep=""));
|
|
|
167 track_db = c(track_db, "type bigBed");
|
|
|
168 track_db = c(track_db, paste("bigDataUrl", big_data_url, sep=" "));
|
|
83
|
169 track_db = c(track_db, paste("shortLabel", cell_info[ii, 2], sep=" "));
|
|
|
170 track_db = c(track_db, paste("longLabel", paste(hub_name, cell_info[ii, 3], sep=" ")));
|
|
54
|
171 track_db = c(track_db, paste("priority", i));
|
|
|
172 track_db = c(track_db, "itemRgb on");
|
|
|
173 track_db = c(track_db, "maxItems 100000");
|
|
66
|
174 track_db = c(track_db, paste("color", color, sep=" "));
|
|
54
|
175 track_db = c(track_db, "visibility dense");
|
|
|
176 track_db = c(track_db, "");
|
|
|
177 }
|
|
|
178 return(track_db);
|
|
0
|
179 }
|
|
|
180
|
|
39
|
181 get_big_data_url = function(galaxy_url, encoded_dataset_id, tracks_dir, index, build) {
|
|
56
|
182 track_files <- list.files(path=tracks_dir, pattern="\\.bigbed");
|
|
54
|
183 s <- paste(galaxy_url, 'datasets/', encoded_dataset_id, '/display/myHub/', build, '/', track_files[index], sep="");
|
|
|
184 return(s)
|
|
50
|
185 }
|
|
|
186
|
|
83
|
187 get_cell_type_names = function(state_file) {
|
|
|
188 # The first line of a state file is a comment
|
|
|
189 # that looks something like this:
|
|
|
190 # ID CHR POSst POSed E001 E002 PosClass
|
|
|
191 # The cell type names are the elemets whose
|
|
|
192 # 1-based indexes start at 5 and end with -1,
|
|
|
193 # which in the above case are E001 and E002.
|
|
|
194 fh = file(state_file,"r");
|
|
|
195 line = readLines(fh, n=1);
|
|
|
196 close(fh);
|
|
|
197 # Split line into a list of strings.
|
|
|
198 items = strsplit(line, "\\s+")[[1]];
|
|
|
199 # Extract the cell type names into a list.
|
|
|
200 last_cell_type_name_index = length(items) -1;
|
|
|
201 cell_type_names = c();
|
|
|
202 for (i in 5:last_cell_type_name_index) {
|
|
|
203 cell_type_names = c(cell_type_names, items[i]);
|
|
|
204 }
|
|
|
205 return(cell_type_names);
|
|
|
206 }
|
|
|
207
|
|
39
|
208 get_track_file_name = function(base_track_file_name, index, ext) {
|
|
54
|
209 track_file_name <- paste(base_track_file_name, index, ext, sep=".");
|
|
|
210 return(track_file_name);
|
|
39
|
211 }
|
|
|
212
|
|
20
|
213 # Create the hub.txt output.
|
|
53
|
214 hub_name_line <- paste("hub ", opt$hub_name, sep="");
|
|
77
|
215 hub_short_label_line <- paste("shortLabel ", opt$hub_short_label, sep="");
|
|
|
216 hub_long_label_line <- paste("longLabel ", opt$hub_long_label, sep="");
|
|
35
|
217 genomes_txt_line <- paste("genomesFile genomes.txt", sep="");
|
|
39
|
218 email_line <- paste("email ", opt$email, sep="");
|
|
77
|
219 contents <- paste(hub_name_line, hub_short_label_line, hub_long_label_line, genomes_txt_line, email_line, sep="\n");
|
|
35
|
220 hub_dir <- paste(opt$output_trackhub_files_path, "/", "myHub", "/", sep="");
|
|
22
|
221 dir.create(hub_dir, showWarnings=FALSE);
|
|
35
|
222 hub_file_path <- paste(hub_dir, "hub.txt", sep="");
|
|
20
|
223 write.table(contents, file=hub_file_path, quote=F, row.names=F, col.names=F);
|
|
|
224
|
|
|
225 # Create the genomes.txt output.
|
|
39
|
226 genome_line <- paste("genome ", opt$build, sep="");
|
|
35
|
227 track_db_line <- paste("trackDb ", opt$build, "/", "trackDb.txt", sep="");
|
|
39
|
228 contents <- paste(genome_line, track_db_line, sep="\n");
|
|
30
|
229 genomes_file_path <- paste(hub_dir, "genomes.txt", sep="");
|
|
20
|
230 write.table(contents, file=genomes_file_path, quote=F, row.names=F, col.names=F);
|
|
|
231
|
|
0
|
232 # Create the tracks.
|
|
68
|
233 heatmap_path <- paste(opt$script_dir, "create_heatmap.R", sep="/");
|
|
|
234 source(heatmap_path);
|
|
36
|
235 tracks_dir <- paste(hub_dir, opt$build, "/", sep="");
|
|
11
|
236 dir.create(tracks_dir, showWarnings=FALSE);
|
|
70
|
237 track_db <- create_track_db(opt$galaxy_url, opt$output_trackhub_id, opt$input_dir_para, opt$input_dir_state, opt$build,
|
|
77
|
238 opt$chrom_len_file, tracks_dir, opt$hub_name, opt$hub_short_label, opt$hub_long_label, opt$state_indexes,
|
|
|
239 opt$state_names, opt$state_colors);
|
|
0
|
240
|
|
20
|
241 # Create the trackDb.txt output.
|
|
35
|
242 track_db_file_path <- paste(tracks_dir, "trackDb.txt", sep="");
|
|
13
|
243 write.table(track_db, file=track_db_file_path, quote=F, row.names=F, col.names=F);
|
|
11
|
244
|
|
20
|
245 # Create the primary HTML dataset.
|
|
78
|
246 create_primary_html(opt$output_trackhub, tracks_dir, opt$build);
|