1 |
#!/usr/bin/env python |
2 |
# encoding: utf-8 |
3 |
""" |
4 |
bed_to_juncs.py |
5 |
|
6 |
Created by Cole Trapnell on 2008-09-19. |
7 |
Copyright (c) 2008 Cole Trapnell. All rights reserved. |
8 |
""" |
9 |
|
10 |
import sys |
11 |
import getopt |
12 |
|
13 |
|
14 |
help_message = ''' |
15 |
This script converts junctions in BED format produced by TopHat to the |
16 |
internal .juncs format for re-use with future runs. |
17 |
|
18 |
Usage: |
19 |
|
20 |
bed_to_juncs.py < junctions.bed |
21 |
''' |
22 |
|
23 |
|
24 |
class Usage(Exception): |
25 |
def __init__(self, msg): |
26 |
self.msg = msg |
27 |
|
28 |
|
29 |
def main(argv=None): |
30 |
if argv is None: |
31 |
argv = sys.argv |
32 |
try: |
33 |
try: |
34 |
opts, args = getopt.getopt(argv[1:], "h", ["help"]) |
35 |
except getopt.error, msg: |
36 |
raise Usage(msg) |
37 |
|
38 |
for option, value in opts: |
39 |
if option in ("-h", "--help"): |
40 |
raise Usage(help_message) |
41 |
|
42 |
line_num = 0 |
43 |
for line in sys.stdin.readlines(): |
44 |
line = line.strip() |
45 |
cols = line.split() |
46 |
line_num += 1 |
47 |
if len(cols) < 12: |
48 |
print >> sys.stderr, "Warning: malformed line %d, missing columns" % line_num |
49 |
print >> sys.stderr, "\t", line |
50 |
continue |
51 |
chromosome = cols[0] |
52 |
orientation = cols[5] |
53 |
block_starts = [int(x) for x in cols[11].split(",")] |
54 |
block_sizes = [int(x) for x in cols[10].split(",")] |
55 |
|
56 |
left_pos = int(cols[1]) + block_starts[0] + block_sizes[0] - 1 |
57 |
right_pos = int(cols[1]) + block_starts[1] |
58 |
print "%s\t%d\t%d\t%s" % (chromosome, left_pos, right_pos, orientation) |
59 |
|
60 |
|
61 |
except Usage, err: |
62 |
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) |
63 |
print >> sys.stderr, "\t for help use --help" |
64 |
return 2 |
65 |
|
66 |
|
67 |
if __name__ == "__main__": |
68 |
sys.exit(main()) |