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