|
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"),
|
|
|
9 make_option(c("-d", "--header"), action="store", dest="header", default=NULL, help="Track header"),
|
|
|
10 make_option(c("-e", "--state_name"), action="store", dest="state_name", help="State name"),
|
|
|
11 make_option(c("-f", "--hub_id"), action="store", dest="hub_id", help="Not sure what this is"),
|
|
|
12 make_option(c("-i", "--cell_info"), action="store", dest="cell_info", default=NULL, help="Not sure what this is"),
|
|
|
13 make_option(c("-n", "--hub_name"), action="store", dest="hub_name", default=NULL, help="Not sure what this is"),
|
|
|
14 make_option(c("-p", "--input_dir_para"), action="store", dest="input_dir_para", help="Directory containing .para outputs from IDEAS"),
|
|
|
15 make_option(c("-q", "--input_dir_state"), action="store", dest="input_dir_state", help="Directory containing .state outputs from IDEAS"),
|
|
|
16 make_option(c("-u", "--output_track_db"), action="store", dest="output_track_db", help="Output track db file"),
|
|
|
17 make_option(c("-v", "--output_build"), action="store", dest="output_build", help="Output genome build file"),
|
|
|
18 make_option(c("-w", "--output_hub"), action="store", dest="output_hub", help="Output hub file")
|
|
|
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));
|
|
5
|
40 state_color <- get_state_color(m, mc);
|
|
|
41 state_color
|
|
0
|
42 }
|
|
|
43
|
|
|
44 get_state_color = function(statemean, markcolor=NULL)
|
|
|
45 {
|
|
|
46 if (length(markcolor) == 0) {
|
|
|
47 markcolor = rep("", dim(statemean)[2]);
|
|
|
48 markcolor[order(apply(statemean, 2, sd), decreasing=T)] = hsv((1:dim(statemean)[2]-1)/dim(statemean)[2], 1, 1);
|
|
|
49 markcolor = t(col2rgb(markcolor));
|
|
|
50 }
|
|
|
51 rg = apply(statemean, 1, range);
|
|
|
52 mm = NULL;
|
|
|
53 for(i in 1:dim(statemean)[1]) {
|
|
|
54 mm = rbind(mm, (statemean[i,]-rg[1,i])/(rg[2,i]-rg[1,i]+1e-10));
|
|
|
55 }
|
|
|
56 mm = mm^6;
|
|
|
57 mm = mm / (apply(mm, 1, sum) + 1e-10);
|
|
|
58 mycol = mm%*%markcolor;
|
|
|
59 s = apply(statemean, 1, max);
|
|
|
60 s = (s - min(s)) / (max(s) - min(s) + 1e-10);
|
|
5
|
61 h = t(apply(mycol, 1, function(x) {rgb2hsv(x[1], x[2], x[3])}));
|
|
0
|
62 h[,2] = h[,2] * s;
|
|
5
|
63 h = apply(h, 1, function(x) {hsv(x[1], x[2], x[3])});
|
|
|
64 rt = cbind(apply(t(col2rgb(h)), 1, function(x) {paste(x, collapse=",")}), h);
|
|
0
|
65 return(rt);
|
|
|
66 }
|
|
|
67
|
|
|
68 create_track = function(input_dir_state, chrom_len_file, outpref, state_color, header, state_name) {
|
|
5
|
69 state_files <- list.files(path=input_dir_state, full.names=TRUE);
|
|
0
|
70 genome_size = read.table(chrom_len_file);
|
|
|
71 g = NULL;
|
|
|
72 for(i in state_files) {
|
|
|
73 tg = as.matrix(fread(i));
|
|
|
74 t = NULL;
|
|
|
75 for(j in 1:dim(genome_size)[1]) {
|
|
|
76 t = c(t, which(tg[,2]==as.character(genome_size[j, 1]) & as.numeric(tg[,4]) > as.numeric(genome_size[j, 2])));
|
|
|
77 }
|
|
|
78 if (length(t) > 0) {
|
|
|
79 tg = tg[-t,];
|
|
|
80 }
|
|
|
81 t = which(is.na(match(tg[,2], genome_size[,1]))==T);
|
|
|
82 if (length(t)>0) {
|
|
|
83 tg = tg[-t,];
|
|
|
84 }
|
|
|
85 g = rbind(g, tg);
|
|
|
86 }
|
|
|
87 uchr = sort(unique(as.character(g[,2])));
|
|
|
88 g1 = NULL;
|
|
5
|
89 for(i in uchr) {
|
|
0
|
90 t = which(g[,2]==i);
|
|
|
91 g1 = rbind(g1, g[t[order(as.integer(g[t, 3]))],]);
|
|
|
92 }
|
|
|
93 g = NULL;
|
|
|
94 chr = as.character(g1[,2]);
|
|
|
95 posst = as.numeric(g1[,3]);
|
|
|
96 posed = as.numeric(g1[,4]);
|
|
|
97 state = as.matrix(g1[,5:(dim(g1)[2]-1)]);
|
|
|
98 if (length(state_name)==0) {
|
|
|
99 state_name=0:max(state);
|
|
|
100 }
|
|
|
101 L = dim(g1)[1];
|
|
|
102 n = dim(state)[2];
|
|
|
103 if (length(header) > 0) {
|
|
|
104 colnames(g1) = header;
|
|
|
105 }
|
|
|
106 cells = as.character(colnames(g1)[5:(dim(g1)[2]-1)]);
|
|
|
107 g1 = NULL;
|
|
|
108 options(scipen=999);
|
|
|
109 tt = which(chr[2:L]!=chr[2:L-1]);
|
|
|
110 tt = c(tt,which(posst[2:L]!=posed[2:L-1]));
|
|
|
111 tt = sort(unique(tt));
|
|
|
112 for(i in 1:n) {
|
|
|
113 tstate = state[,i];
|
|
5
|
114 t = c(tt, which(tstate[2:L]!=tstate[2:L-1]));
|
|
0
|
115 t = sort(unique(t));
|
|
|
116 t0 = c(0, t) + 1;
|
|
|
117 t = c(t, L);
|
|
|
118 np = cbind(chr[t], posst[t0], posed[t], tstate[t]);
|
|
|
119 x = cbind(np[,1:3], state_name[as.integer(np[,4])+1], 1000, ".", np[,2:3], state_color[as.numeric(np[,4])+1]);
|
|
|
120 write.table(as.matrix(x), paste(outpref, i, "bed1", sep="."), quote=F, row.names=F, col.names=F);
|
|
|
121 system(paste("bedToBigBed ", outpref, ".", i, ".bed1 ", chrom_len_file, " ", outpref, ".", i, ".bb", sep=""));
|
|
|
122 system(paste("rm ", paste(outpref, i, "bed1", sep=".")));
|
|
|
123 }
|
|
|
124 return(cells);
|
|
|
125 }
|
|
|
126
|
|
|
127 create_track_db = function(input_dir_state, chrom_len_file, track_file_name, state_color, header, state_name, hub_name, cell_info) {
|
|
|
128 cells = create_track(input_dir_state, chrom_len_file, track_file_name, state_color, header, state_name);
|
|
|
129 if (length(cell_info) == 0) {
|
|
|
130 cell_info = cbind(cells, cells, cells, "#000000");
|
|
|
131 cell_info = array(cell_info, dim=c(length(cells), 4));
|
|
|
132 }
|
|
|
133 cell_info = as.matrix(cell_info);
|
|
|
134 track_db = NULL;
|
|
|
135 for(i in 1:length(cells)) {
|
|
|
136 ii = which(cells[i] == cell_info[,1]);
|
|
|
137 if (length(ii) == 0) {
|
|
|
138 next;
|
|
|
139 }
|
|
|
140 ii = ii[1];
|
|
|
141 track_db = c(track_db, paste("track bigBed", i, sep=""));
|
|
|
142 track_db = c(track_db, paste("priority", ii));
|
|
|
143 track_db = c(track_db, "type bigBed 9 .");
|
|
|
144 track_db = c(track_db, "itemRgb on");
|
|
|
145 track_db = c(track_db, "maxItems 100000");
|
|
|
146 track_db = c(track_db, paste("shortLabel", cell_info[ii, 2]));
|
|
|
147 track_db = c(track_db, paste("longLabel", paste(hub_name, cell_info[ii, 3])));
|
|
|
148 track_db = c(track_db, paste("color", paste(c(col2rgb(cell_info[ii, 4])), collapse=",")));
|
|
|
149 track_db = c(track_db, "visibility dense");
|
|
|
150 track_db = c(track_db, "");
|
|
|
151 }
|
|
|
152 }
|
|
|
153
|
|
|
154 if (length(opt$hub_name) == 0) {
|
|
|
155 hub_name = opt$hub_id;
|
|
|
156 }
|
|
|
157
|
|
|
158 # Create the tracks output directory.
|
|
|
159 tracks_output_dir = paste("tracks_", opt$hub_id, "/", sep="");
|
|
|
160 dir.create(tracks_output_dir, showWarnings=FALSE);
|
|
|
161
|
|
|
162 # Create the color scheme.
|
|
|
163 state_color <- create_color_scheme(opt$input_dir_para);
|
|
|
164
|
|
|
165 # Create the tracks.
|
|
|
166 track_db <- create_track_db(opt$input_dir_state, opt$chrom_len_file, paste(tracks_output_dir, opt$hub_id, sep=""), state_color, opt$header, opt$state_name, opt$hub_name, opt$cell_info);
|
|
|
167
|
|
|
168 # Write the outputs.
|
|
|
169 write.table(track_db, opt$output_track_db, quote=F, row.names=F, col.names=F);
|
|
|
170 write.table(c(paste("genome", opt$build), opt$output_build), paste(tracks_output_dir, "genomes_", opt$hub_id, ".txt", sep=""), quote=F, row.names=F, col.names=F);
|
|
|
171 write.table(c(paste("hub", opt$hub_id), paste("shortLabel", opt$hub_id), paste("longLabel", opt$hub_name), paste("genomesFile genomes_", opt$hub_id, ".txt", sep=""), "email yzz2 at psu.edu"), opt$output_hub, quote=F, row.names=F, col.names=F);
|
|
5
|
172
|