Debian Codegraph @

edu/mit/compbio/ChromHMM/ConvertGeneTable.java

raw symbols SCIP
1 /**
2 * ChromHMM - automating chromatin state discovery and characterization
3 * Copyright (C) 2008-2012 Massachusetts Institute of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 **/
18
19 package edu.mit.compbio.ChromHMM;
20
21 import java.io.*;
22 import java.util.*;
23 import java.util.zip.GZIPOutputStream;
24
25 public class ConvertGeneTable
26 {
27
28 /**
29 * Reads in a RefSeq or equivalently formatted gene table from the UCSC browser and outputs converted files
30 * the columns in order from the RefSeq gene table are (additional subsequent columns are ignored if present)
31 * bin
32 * name
33 * chrom
34 * strand
35 * txStart
36 * txEnd
37 * cdsStart
38 * cdsEnd
39 * exonCount
40 * exonStarts
41 * exonEnds
42
43
44 * or a bigGenePred format, where only the first 12 columns are considered:
45 * chrom
46 * chromStart
47 * chromEnd
48 * name
49 * score
50 * strand
51 * thickStart
52 * thickEnd
53 * reserved
54 * blockCount
55 * blockSizes
56 * chromStarts
57 */
58
59 static void convertGeneTableToAnnotations(String sztable, String szprefix,
60 String szassembly, String szcoorddir,
61 String szanchordir, String szchromlengths,
62 int npromoterwindow, boolean bgzip,
63 boolean bnobin, boolean bnoheader, boolean bbiggenepred) throws IOException
64 {
65
66 //String sztable = args[0];
67 //String szassembly = args[1];
68 //String szoutdir = args[2];
69 //String szanchordir = args[3];
70 //String szchromlengths = args[4];
71 String szLine;
72 HashMap hmlengths = new HashMap();
73 //BufferedReader brlength = new BufferedReader(new FileReader(szchromlengths));
74 BufferedReader brlength = Util.getBufferedReader(szchromlengths);
75 while ((szLine = brlength.readLine())!=null)
76 {
77 StringTokenizer st = new StringTokenizer(szLine,"\t ");
78 if (st.countTokens()==0)
79 {
80 throw new IllegalArgumentException("empty line found in "+szchromlengths);
81 }
82 else if (st.countTokens()==1)
83 {
84 throw new IllegalArgumentException("Only one token found in line "+szLine+" of "+szchromlengths);
85 }
86 hmlengths.put(st.nextToken(),Integer.valueOf(st.nextToken()));
87 }
88 brlength.close();
89 BufferedReader br = Util.getBufferedReader(sztable);
90
91
92 PrintWriter pwtss = null;
93 PrintWriter pwanchortss = null;
94 PrintWriter pwanchortes = null;
95 PrintWriter pwgene = null;
96 PrintWriter pwexon = null;
97 PrintWriter pwtes = null;
98 PrintWriter pwtss2kb = null;
99
100 GZIPOutputStream pwtsszip = null;
101 GZIPOutputStream pwanchortsszip = null;
102 GZIPOutputStream pwanchorteszip = null;
103 GZIPOutputStream pwgenezip = null;
104 GZIPOutputStream pwexonzip = null;
105 GZIPOutputStream pwteszip = null;
106 GZIPOutputStream pwtss2kbzip = null;
107
108 if (bgzip)
109 {
110 pwtsszip = new GZIPOutputStream(new FileOutputStream(szcoorddir+"/"+szprefix+"TSS."+szassembly+".bed.gz"));
111 pwanchortsszip = new GZIPOutputStream(new FileOutputStream(szanchordir+"/"+szprefix+"TSS."+szassembly+".txt.gz"));
112 pwanchorteszip = new GZIPOutputStream(new FileOutputStream(szanchordir+"/"+szprefix+"TES."+szassembly+".txt.gz"));
113 pwgenezip = new GZIPOutputStream(new FileOutputStream(szcoorddir+"/"+szprefix+"Gene."+szassembly+".bed.gz"));
114
115 pwexonzip = new GZIPOutputStream(new FileOutputStream(szcoorddir+"/"+szprefix+"Exon."+szassembly+".bed.gz"));
116 pwteszip = new GZIPOutputStream(new FileOutputStream(szcoorddir+"/"+szprefix+"TES."+szassembly+".bed.gz"));
117
118 if (npromoterwindow % 1000 == 0)
119 {
120 pwtss2kbzip = new GZIPOutputStream(new FileOutputStream(szcoorddir+"/"+szprefix+"TSS"+(npromoterwindow/1000)+"kb."+szassembly+".bed.gz"));
121 }
122 else
123 {
124 pwtss2kbzip = new GZIPOutputStream(new FileOutputStream(szcoorddir+"/"+szprefix+"TSS"+npromoterwindow+"bp."+szassembly+".bed.gz"));
125 }
126 }
127 else
128 {
129 pwtss = new PrintWriter(szcoorddir+"/"+szprefix+"TSS."+szassembly+".bed");
130 pwanchortss = new PrintWriter(szanchordir+"/"+szprefix+"TSS."+szassembly+".txt");
131 pwanchortes = new PrintWriter(szanchordir+"/"+szprefix+"TES."+szassembly+".txt");
132 pwgene = new PrintWriter(szcoorddir+"/"+szprefix+"Gene."+szassembly+".bed");
133
134 pwexon = new PrintWriter(szcoorddir+"/"+szprefix+"Exon."+szassembly+".bed");
135 pwtes = new PrintWriter(szcoorddir+"/"+szprefix+"TES."+szassembly+".bed");
136
137 if (npromoterwindow % 1000 == 0)
138 {
139 pwtss2kb = new PrintWriter(szcoorddir+"/"+szprefix+"TSS"+(npromoterwindow/1000)+"kb."+szassembly+".bed");
140 }
141 else
142 {
143 pwtss2kb = new PrintWriter(szcoorddir+"/"+szprefix+"TSS"+npromoterwindow+"bp."+szassembly+".bed");
144 }
145 }
146
147 HashSet hstss = new HashSet();
148 HashSet hsanchortss = new HashSet();
149 HashSet hsanchortes = new HashSet();
150 HashSet hsgene = new HashSet();
151 HashSet hsexon = new HashSet();
152 HashSet hstes = new HashSet();
153 HashSet hstss2kb = new HashSet();
154
155 if (!bnoheader)
156 {
157 br.readLine();
158 }
159
160 while ((szLine = br.readLine())!=null)
161 {
162 StringTokenizer st = new StringTokenizer(szLine,"\t",true);
163 String szchrom = null;
164 String szstrand = null;
165 String sztxStart = null;
166 String sztxEnd = null;
167 String szexonStarts = null;
168 String szexonEnds = null;
169 String szexonSizes = null;
170 String szchromStarts = null;
171
172 if (bbiggenepred)
173 {
174 szchrom = st.nextToken().trim();
175 if (!szchrom.equals("\t"))
176 st.nextToken().trim();
177 sztxStart = st.nextToken().trim();
178 if (!sztxStart.equals("\t"))
179 st.nextToken().trim();
180 sztxEnd = st.nextToken().trim();
181 if (!sztxEnd.equals("\t"))
182 st.nextToken().trim();
183 String szname = st.nextToken().trim();
184 if (!szname.equals("\t"))
185 st.nextToken().trim();
186 String szscore = st.nextToken().trim();
187 if (!szscore.equals("\t"))
188 st.nextToken().trim();
189 szstrand = st.nextToken().trim();
190 if (!szstrand.equals("\t"))
191 st.nextToken().trim();
192 String szthickStart = st.nextToken().trim();
193 if (!szthickStart.equals("\t"))
194 st.nextToken().trim();
195 String szthickEnd = st.nextToken().trim();
196 if (!szthickEnd.equals("\t"))
197 st.nextToken().trim();
198 String szreserved = st.nextToken().trim();
199 if (!szreserved.equals("\t"))
200 st.nextToken().trim();
201 String szexonCount = st.nextToken().trim();
202 if (!szexonCount.equals("\t"))
203 st.nextToken().trim();
204 szexonSizes = st.nextToken().trim();
205 if (!szexonSizes.equals("\t"))
206 st.nextToken().trim();
207 szchromStarts = st.nextToken().trim();
208 }
209 else
210 {
211 if (!bnobin)
212 {
213 //updated in 1.25
214 String szbin = null;
215 szbin = st.nextToken().trim();
216 if (!szbin.equals("\t"))
217 st.nextToken().trim();
218 }
219 String szname = st.nextToken().trim();
220 if (!szname.equals("\t"))
221 st.nextToken().trim();
222 szchrom = st.nextToken().trim();
223 if (!szchrom.equals("\t"))
224 st.nextToken().trim();
225 szstrand = st.nextToken().trim();
226 if (!szstrand.equals("\t"))
227 st.nextToken().trim();
228 sztxStart = st.nextToken().trim();
229 if (!sztxStart.equals("\t"))
230 st.nextToken().trim();
231 sztxEnd = st.nextToken().trim();
232 if (!sztxEnd.equals("\t"))
233 st.nextToken().trim();
234 String szcdsStart = st.nextToken().trim();
235 if (!szcdsStart.equals("\t"))
236 st.nextToken().trim();
237 String szcdsEnd = st.nextToken().trim();
238 if (!szcdsEnd.equals("\t"))
239 st.nextToken().trim();
240 String szexonCount = st.nextToken().trim();
241 if (!szexonCount.equals("\t"))
242 st.nextToken().trim();
243 szexonStarts = st.nextToken().trim();
244 if (!szexonStarts.equals("\t"))
245 st.nextToken().trim();
246 szexonEnds = st.nextToken().trim();
247
248 }
249 //removed 1.22
250 //if (!szexonEnds.equals("\t"))
251 // st.nextToken();
252
253 //String szscore = st.nextToken();
254 //if (!szscore.equals("\t"))
255 //st.nextToken();
256 //String szname2 = st.nextToken();
257 //if (!szname2.equals("\t"))
258 //st.nextToken();
259 //String szcdsStartStat = st.nextToken();
260 //if (!szcdsStartStat.equals("\t"))
261 // st.nextToken();
262 //String szcdsEndStat = st.nextToken();
263 //if (!szcdsEndStat.equals("\t"))
264 //st.nextToken();
265 //String szexonFrames = st.nextToken();
266
267
268 int ntes=-1;
269 int ntss=-1;
270 if (szstrand.equals("+"))
271 {
272 ntss = Integer.parseInt(sztxStart);
273 ntes = Integer.parseInt(sztxEnd)-1;
274 }
275 else if (szstrand.equals("-"))
276 {
277 ntss = Integer.parseInt(sztxEnd)-1;
278 ntes = Integer.parseInt(sztxStart);
279 }
280 else
281 {
282 throw new IllegalArgumentException("invalid strand\t"+szstrand);
283 }
284
285 //updated in 1.18 to give better error message if chromosome not found
286 Integer objchromlength = ((Integer) hmlengths.get(szchrom));
287 int nchromlength = -1;
288
289 if (objchromlength != null)
290 {
291 nchromlength = objchromlength.intValue();
292 }
293 else
294 {
295 throw new IllegalArgumentException("did not find length in chromosome file for chromosome: "+szchrom);
296 // =((Integer) hmlengths.get(szchrom)).intValue();
297 }
298
299 String sztssOut = szchrom+"\t"+ntss+"\t"+(ntss+1);
300 String sztesOut = szchrom+"\t"+ntes+"\t"+(ntes+1);
301 String sztss2kbOut = szchrom+"\t"+Math.max((ntss-npromoterwindow),0)+"\t"+Math.min(ntss+npromoterwindow+1,nchromlength);
302 String szgeneOut = szchrom+"\t"+sztxStart+"\t"+sztxEnd;
303 String szanchorTSSOut = szchrom+"\t"+ntss+"\t"+szstrand;
304 String szanchorTESOut = szchrom+"\t"+ntes+"\t"+szstrand;
305 //added two allow " quotes for exon list
306 StringTokenizer stexonStarts = null;
307 StringTokenizer stexonEnds = null;
308 StringTokenizer stexonSizes = null;
309 StringTokenizer stchromStarts = null;
310
311 if (bbiggenepred)
312 {
313 stexonSizes = new StringTokenizer(szexonSizes,",\"");
314 stchromStarts = new StringTokenizer(szchromStarts,",\"");
315 }
316 else
317 {
318 stexonStarts = new StringTokenizer(szexonStarts,",\"");
319 stexonEnds = new StringTokenizer(szexonEnds,",\"");
320 }
321
322 if (bgzip)
323 {
324 if (!hstss.contains(sztssOut))
325 {
326 byte[] btformat = (sztssOut+"\n").getBytes();
327 pwtsszip.write(btformat,0,btformat.length);
328 //pwtss.println(sztssOut);
329 hstss.add(sztssOut);
330 }
331
332 if (!hsanchortss.contains(szanchorTSSOut))
333 {
334 byte[] btformat = (szanchorTSSOut+"\n").getBytes();
335 pwanchortsszip.write(btformat,0,btformat.length);
336
337 //pwanchortss.println(szanchorTSSOut);
338 hsanchortss.add(szanchorTSSOut);
339 }
340
341 if (!hsanchortes.contains(szanchorTESOut))
342 {
343 byte[] btformat = (szanchorTESOut+"\n").getBytes();
344 pwanchorteszip.write(btformat,0,btformat.length);
345
346 //pwanchortes.println(szanchorTESOut);
347 hsanchortes.add(szanchorTESOut);
348 }
349
350 if (!hstes.contains(sztesOut))
351 {
352 byte[] btformat = (sztesOut+"\n").getBytes();
353 pwteszip.write(btformat,0,btformat.length);
354
355 //pwtes.println(sztesOut);
356 hstes.add(sztesOut);
357 }
358
359 if (!hstss2kb.contains(sztss2kbOut))
360 {
361 byte[] btformat = (sztss2kbOut+"\n").getBytes();
362 pwtss2kbzip.write(btformat,0,btformat.length);
363
364 //pwtss2kb.println(sztss2kbOut);
365 hstss2kb.add(sztss2kbOut);
366 }
367
368 if (!hsgene.contains(szgeneOut))
369 {
370 byte[] btformat = (szgeneOut+"\n").getBytes();
371 pwgenezip.write(btformat,0,btformat.length);
372
373 //pwgene.println(szgeneOut);
374 hsgene.add(szgeneOut);
375 }
376
377 StringTokenizer stStarts = null;
378 if (bbiggenepred)
379 {
380 stStarts = stchromStarts;
381 }
382 else
383 {
384 stStarts = stexonStarts;
385 }
386
387 while (stStarts.hasMoreTokens())
388 {
389 String szexonOut = null;
390 if (bbiggenepred)
391 {
392 int nchromstart = Integer.parseInt(sztxStart);
393 int nexonchromstart = Integer.parseInt(stStarts.nextToken().trim());
394 int nexonsize = Integer.parseInt(stexonSizes.nextToken().trim());
395 szexonOut = szchrom+"\t"+(nchromstart+nexonchromstart)+"\t"+(nchromstart+nexonchromstart+nexonsize)+"\n";
396 }
397 else
398 {
399 szexonOut = szchrom+"\t"+stStarts.nextToken().trim()+"\t"+stexonEnds.nextToken().trim()+"\n";
400 }
401
402 if (!hsexon.contains(szexonOut))
403 {
404 byte[] btformat = szexonOut.getBytes();
405 pwexonzip.write(btformat,0,btformat.length);
406
407 hsexon.add(szexonOut);
408 }
409
410 //pwexon.println(szexonOut);
411 }
412 }
413 else
414 {
415 if (!hstss.contains(sztssOut))
416 {
417 pwtss.println(sztssOut);
418 hstss.add(sztssOut);
419 }
420
421 if (!hsanchortss.contains(szanchorTSSOut))
422 {
423 pwanchortss.println(szanchorTSSOut);
424 hsanchortss.add(szanchorTSSOut);
425 }
426
427 if (!hsanchortes.contains(szanchorTESOut))
428 {
429 pwanchortes.println(szanchorTESOut);
430 hsanchortes.add(szanchorTESOut);
431 }
432
433 if (!hstes.contains(sztesOut))
434 {
435 pwtes.println(sztesOut);
436 hstes.add(sztesOut);
437 }
438
439 if (!hstss2kb.contains(sztss2kbOut))
440 {
441 pwtss2kb.println(sztss2kbOut);
442 hstss2kb.add(sztss2kbOut);
443 }
444
445 if (!hsgene.contains(szgeneOut))
446 {
447 pwgene.println(szgeneOut);
448 hsgene.add(szgeneOut);
449 }
450
451 StringTokenizer stStarts = null;
452 if (bbiggenepred)
453 {
454 stStarts = stchromStarts;
455 }
456 else
457 {
458 stStarts = stexonStarts;
459 }
460
461 while (stStarts.hasMoreTokens())
462 {
463
464 String szexonOut = null;
465 if (bbiggenepred)
466 {
467 int nchromstart = Integer.parseInt(sztxStart);
468 int nexonchromstart = Integer.parseInt(stStarts.nextToken().trim());
469 int nexonsize = Integer.parseInt(stexonSizes.nextToken().trim());
470 szexonOut = szchrom+"\t"+(nchromstart+nexonchromstart)+"\t"+(nchromstart+nexonchromstart+nexonsize);
471 }
472 else
473 {
474 szexonOut = szchrom+"\t"+stStarts.nextToken().trim()+"\t"+stexonEnds.nextToken().trim();
475 }
476
477 if (!hsexon.contains(szexonOut))
478 {
479 pwexon.println(szexonOut);
480 hsexon.add(szexonOut);
481 }
482 }
483 }
484 }
485 br.close();
486
487 if (bgzip)
488 {
489 pwtsszip.finish(); //tss
490 pwgenezip.finish(); //gene
491 pwanchortsszip.finish();
492 pwanchorteszip.finish();
493 pwexonzip.finish(); //exon
494 pwteszip.finish(); //tss
495 pwtss2kbzip.finish(); //2kb
496
497
498 pwtsszip.close(); //tss
499 pwgenezip.close(); //gene
500 pwanchortsszip.close();
501 pwanchorteszip.close();
502 pwexonzip.close(); //exon
503 pwteszip.close(); //tss
504 pwtss2kbzip.close(); //2kb
505 }
506 else
507 {
508 pwtss.close(); //tss
509 pwgene.close(); //gene
510 pwanchortss.close();
511 pwanchortes.close();
512 pwexon.close(); //exon
513 pwtes.close(); //tss
514 pwtss2kb.close(); //2kb
515 }
516
517 }
518
519 }