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