|
0
|
1 #!/usr/bin/env Rscript
|
|
|
2
|
|
5
|
3 suppressPackageStartupMessages(library("data.table"))
|
|
0
|
4 suppressPackageStartupMessages(library("optparse"))
|
|
|
5
|
|
|
6 option_list <- list(
|
|
61
|
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"),
|
|
66
|
12 make_option(c("--input_dir_para"), action="store", dest="input_dir_para", help="Directory containing .para outputs from IDEAS"),
|
|
61
|
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"),
|
|
|
18 make_option(c("--short_label"), action="store", dest="short_label", help="Hub short label"),
|
|
|
19 make_option(c("--state_colors"), action="store", dest="state_colors", default=NULL, help="List of state_colors"),
|
|
|
20 make_option(c("--state_indexes"), action="store", dest="state_indexes", default=NULL, help="List of state_indexes")
|
|
0
|
21 )
|
|
|
22
|
|
|
23 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
|
|
|
24 args <- parse_args(parser, positional_arguments=TRUE)
|
|
|
25 opt <- args$options
|
|
|
26
|
|
39
|
27 create_primary_html = function(output_trackhub, tracks_dir, build) {
|
|
54
|
28 track_files <- list.files(path=tracks_dir);
|
|
|
29 s <- paste('<html><head></head><body>', sep="\n");
|
|
|
30 s <- paste(s, '<h3>Contents of directory ~/myHub/', build, ' required by UCSC TrackHub</h3>\n', sep="");
|
|
|
31 s <- paste(s, '<ul>\n', sep="")
|
|
|
32 for (i in 1:length(track_files)) {
|
|
|
33 s <- paste(s, '<li><a href="', 'myHub/', build, "/", track_files[i], '">', track_files[i], '</a></li>\n', sep="");
|
|
|
34 }
|
|
|
35 s <- paste(s, '</ul>\n</body>\n</html>', sep="");
|
|
|
36 cat(s, file=output_trackhub);
|
|
20
|
37 }
|
|
|
38
|
|
51
|
39 create_track = function(input_dir_state, chrom_len_file, base_track_file_name) {
|
|
66
|
40 # Create everything needed, including the bigbed file,
|
|
54
|
41 # to render the tracks within the UCSC track hub.
|
|
|
42 state_files <- list.files(path=input_dir_state, full.names=TRUE);
|
|
|
43 genome_size = read.table(chrom_len_file);
|
|
|
44 g = NULL;
|
|
|
45 for(i in state_files) {
|
|
|
46 tg = as.matrix(fread(i));
|
|
|
47 t = NULL;
|
|
|
48 for(j in 1:dim(genome_size)[1]) {
|
|
|
49 t = c(t, which(tg[,2]==as.character(genome_size[j, 1]) & as.numeric(tg[,4]) > as.numeric(genome_size[j, 2])));
|
|
|
50 }
|
|
|
51 if (length(t) > 0) {
|
|
|
52 tg = tg[-t,];
|
|
|
53 }
|
|
|
54 t = which(is.na(match(tg[,2], genome_size[,1]))==T);
|
|
|
55 if (length(t)>0) {
|
|
|
56 tg = tg[-t,];
|
|
|
57 }
|
|
|
58 g = rbind(g, tg);
|
|
|
59 }
|
|
|
60 uchr = sort(unique(as.character(g[,2])));
|
|
|
61 g1 = NULL;
|
|
|
62 for(i in uchr) {
|
|
|
63 t = which(g[,2]==i);
|
|
|
64 g1 = rbind(g1, g[t[order(as.integer(g[t, 3]))],]);
|
|
|
65 }
|
|
|
66 g = NULL;
|
|
|
67 chr = as.character(g1[,2]);
|
|
|
68 posst = as.numeric(g1[,3]);
|
|
|
69 posed = as.numeric(g1[,4]);
|
|
|
70 state = as.matrix(g1[,5:(dim(g1)[2]-1)]);
|
|
|
71 state_name = 0:max(state);
|
|
|
72 L = dim(g1)[1];
|
|
|
73 n = dim(state)[2];
|
|
|
74 cells = as.character(colnames(g1)[5:(dim(g1)[2]-1)]);
|
|
|
75 options(scipen=999);
|
|
|
76 tt = which(chr[2:L]!=chr[2:L-1]);
|
|
|
77 tt = c(tt, which(posst[2:L]!=posed[2:L-1]));
|
|
|
78 tt = sort(unique(tt));
|
|
|
79 for(i in 1:n) {
|
|
|
80 tstate = state[,i];
|
|
|
81 t = c(tt, which(tstate[2:L]!=tstate[2:L-1]));
|
|
|
82 t = sort(unique(t));
|
|
|
83 t0 = c(0, t) + 1;
|
|
|
84 t = c(t, L);
|
|
|
85 np = cbind(chr[t], posst[t0], posed[t], tstate[t]);
|
|
57
|
86 track_file_name_bed_unsorted <- get_track_file_name(base_track_file_name, i, "bed_unsorted");
|
|
58
|
87 track_file_name_bed <- get_track_file_name(base_track_file_name, i, "bed");
|
|
56
|
88 track_file_name_bigbed <- get_track_file_name(base_track_file_name, i, "bigbed");
|
|
54
|
89 x = cbind(np[, 1:3], state_name[as.integer(np[,4])+1], 1000, ".", np[,2:3]);
|
|
57
|
90 write.table(as.matrix(x), track_file_name_bed_unsorted, quote=F, row.names=F, col.names=F);
|
|
59
|
91 cmd = paste("LC_COLLATE=C sort -k1,1 -k2,2n < ", track_file_name_bed_unsorted, " > ", track_file_name_bed);
|
|
|
92 system(cmd);
|
|
|
93 cmd = paste("bedToBigBed ", track_file_name_bed, chrom_len_file, " ", track_file_name_bigbed);
|
|
|
94 system(cmd);
|
|
65
|
95 system(paste("rm ", track_file_name_bed_unsorted));
|
|
|
96 system(paste("rm ", track_file_name_bed));
|
|
54
|
97 }
|
|
|
98 return(cells);
|
|
0
|
99 }
|
|
|
100
|
|
66
|
101 create_track_db = function(galaxy_url, encoded_dataset_id, input_dir_para, input_dir_state, build,
|
|
|
102 chrom_len_file, tracks_dir, hub_name, short_label, long_label, state_indexes, state_colors) {
|
|
54
|
103 # Create a trackDb.txt file that includes each state.
|
|
66
|
104 para_files <- list.files(path=input_dir_para, full.names=TRUE);
|
|
54
|
105 base_track_file_name <- paste(tracks_dir, hub_name, sep="");
|
|
|
106 cells = create_track(input_dir_state, chrom_len_file, base_track_file_name);
|
|
63
|
107 if (!is.null(state_indexes)) {
|
|
61
|
108 # Split state_indexes into a list of integers.
|
|
63
|
109 s_indexes <- c();
|
|
61
|
110 index_str <- as.character(state_indexes);
|
|
|
111 items <- strsplit(index_str, ",")
|
|
|
112 for (item in items) {
|
|
63
|
113 s_indexes <- c(s_indexes, as.integer(item));
|
|
61
|
114 }
|
|
|
115 # Split state_colors into a list of strings.
|
|
63
|
116 s_colors <- c();
|
|
61
|
117 color_str <- as.character(state_colors);
|
|
|
118 items <- strsplit(color_str, ",");
|
|
|
119 for (item in items) {
|
|
63
|
120 s_colors <- c(s_colors, item);
|
|
61
|
121 }
|
|
|
122 }
|
|
54
|
123 track_db = NULL;
|
|
|
124 for (i in 1:length(cells)) {
|
|
64
|
125 if (is.null(state_indexes) || !is.element(i, s_indexes)) {
|
|
66
|
126 data_frame <- read.table(para_files[i], comment="!", header=T);
|
|
|
127 color <- create_heatmap(data_frame);
|
|
61
|
128 } else {
|
|
|
129 # Use the selected color for the current state.
|
|
63
|
130 color_hex_code <- s_colors[i];
|
|
66
|
131 color <- paste(c(col2rgb(color_hex_code)), collapse=",");
|
|
54
|
132 }
|
|
|
133 big_data_url <- get_big_data_url(galaxy_url, encoded_dataset_id, tracks_dir, i, build);
|
|
|
134 # trackDb.txt track entry.
|
|
|
135 track_db = c(track_db, paste("track ", hub_name, "_track_", i, sep=""));
|
|
|
136 track_db = c(track_db, "type bigBed");
|
|
|
137 track_db = c(track_db, paste("bigDataUrl", big_data_url, sep=" "));
|
|
|
138 track_db = c(track_db, paste("shortLabel", short_label, sep=" "));
|
|
|
139 track_db = c(track_db, paste("longLabel", long_label, sep=" "));
|
|
|
140 track_db = c(track_db, paste("priority", i));
|
|
|
141 track_db = c(track_db, "itemRgb on");
|
|
|
142 track_db = c(track_db, "maxItems 100000");
|
|
66
|
143 track_db = c(track_db, paste("color", color, sep=" "));
|
|
54
|
144 track_db = c(track_db, "visibility dense");
|
|
|
145 track_db = c(track_db, "");
|
|
|
146 }
|
|
|
147 return(track_db);
|
|
0
|
148 }
|
|
|
149
|
|
39
|
150 get_big_data_url = function(galaxy_url, encoded_dataset_id, tracks_dir, index, build) {
|
|
56
|
151 track_files <- list.files(path=tracks_dir, pattern="\\.bigbed");
|
|
54
|
152 s <- paste(galaxy_url, 'datasets/', encoded_dataset_id, '/display/myHub/', build, '/', track_files[index], sep="");
|
|
|
153 return(s)
|
|
50
|
154 }
|
|
|
155
|
|
39
|
156 get_track_file_name = function(base_track_file_name, index, ext) {
|
|
54
|
157 track_file_name <- paste(base_track_file_name, index, ext, sep=".");
|
|
|
158 return(track_file_name);
|
|
39
|
159 }
|
|
|
160
|
|
20
|
161 # Create the hub.txt output.
|
|
53
|
162 hub_name_line <- paste("hub ", opt$hub_name, sep="");
|
|
39
|
163 short_label_line <- paste("shortLabel ", opt$short_label, sep="");
|
|
|
164 long_label_line <- paste("longLabel ", opt$long_label, sep="");
|
|
35
|
165 genomes_txt_line <- paste("genomesFile genomes.txt", sep="");
|
|
39
|
166 email_line <- paste("email ", opt$email, sep="");
|
|
|
167 contents <- paste(hub_name_line, short_label_line, long_label_line, genomes_txt_line, email_line, sep="\n");
|
|
35
|
168 hub_dir <- paste(opt$output_trackhub_files_path, "/", "myHub", "/", sep="");
|
|
22
|
169 dir.create(hub_dir, showWarnings=FALSE);
|
|
35
|
170 hub_file_path <- paste(hub_dir, "hub.txt", sep="");
|
|
20
|
171 write.table(contents, file=hub_file_path, quote=F, row.names=F, col.names=F);
|
|
|
172
|
|
|
173 # Create the genomes.txt output.
|
|
39
|
174 genome_line <- paste("genome ", opt$build, sep="");
|
|
35
|
175 track_db_line <- paste("trackDb ", opt$build, "/", "trackDb.txt", sep="");
|
|
39
|
176 contents <- paste(genome_line, track_db_line, sep="\n");
|
|
30
|
177 genomes_file_path <- paste(hub_dir, "genomes.txt", sep="");
|
|
20
|
178 write.table(contents, file=genomes_file_path, quote=F, row.names=F, col.names=F);
|
|
|
179
|
|
0
|
180 # Create the tracks.
|
|
66
|
181 source("create_heatmap.R");
|
|
36
|
182 tracks_dir <- paste(hub_dir, opt$build, "/", sep="");
|
|
11
|
183 dir.create(tracks_dir, showWarnings=FALSE);
|
|
66
|
184 track_db <- create_track_db(opt$galaxy_url, opt$output_trackhub_id, opt$input_dir_state, opt$input_dir_state, opt$build,
|
|
|
185 opt$chrom_len_file, tracks_dir, opt$hub_name, opt$short_label, opt$long_label, opt$state_indexes, opt$state_colors);
|
|
0
|
186
|
|
20
|
187 # Create the trackDb.txt output.
|
|
35
|
188 track_db_file_path <- paste(tracks_dir, "trackDb.txt", sep="");
|
|
13
|
189 write.table(track_db, file=track_db_file_path, quote=F, row.names=F, col.names=F);
|
|
11
|
190
|
|
20
|
191 # Create the primary HTML dataset.
|
|
54
|
192 create_primary_html(opt$output_trackhub, tracks_dir, opt$build);
|