1 | #!/usr/bin/env python |
---|
2 | import sys |
---|
3 | |
---|
4 | assert sys.version_info[:2] >= ( 2, 4 ) |
---|
5 | |
---|
6 | def __main__(): |
---|
7 | input_name = sys.argv[1] |
---|
8 | output_name = sys.argv[2] |
---|
9 | skipped_lines = 0 |
---|
10 | first_skipped_line = 0 |
---|
11 | out = open( output_name, 'w' ) |
---|
12 | i = 0 |
---|
13 | for i, line in enumerate( file( input_name ) ): |
---|
14 | line = line.rstrip( '\r\n' ) |
---|
15 | if line and not line.startswith( '#' ): |
---|
16 | try: |
---|
17 | elems = line.split( '\t' ) |
---|
18 | start = str( int( elems[3] ) - 1 ) |
---|
19 | strand = elems[6] |
---|
20 | if strand not in ['+', '-']: |
---|
21 | strand = '+' |
---|
22 | # GFF format: chrom source, name, chromStart, chromEnd, score, strand |
---|
23 | # Bed format: chrom, chromStart, chromEnd, name, score, strand |
---|
24 | # |
---|
25 | # Replace any spaces in the name with underscores so UCSC will not complain |
---|
26 | name = elems[2].replace(" ", "_") |
---|
27 | out.write( "%s\t%s\t%s\t%s\t0\t%s\n" %( elems[0], start, elems[4], name, strand ) ) |
---|
28 | except: |
---|
29 | skipped_lines += 1 |
---|
30 | if not first_skipped_line: |
---|
31 | first_skipped_line = i + 1 |
---|
32 | else: |
---|
33 | skipped_lines += 1 |
---|
34 | if not first_skipped_line: |
---|
35 | first_skipped_line = i + 1 |
---|
36 | out.close() |
---|
37 | info_msg = "%i lines converted to BED. " % ( i + 1 - skipped_lines ) |
---|
38 | if skipped_lines > 0: |
---|
39 | info_msg += "Skipped %d blank/comment/invalid lines starting with line #%d." %( skipped_lines, first_skipped_line ) |
---|
40 | print info_msg |
---|
41 | |
---|
42 | if __name__ == "__main__": __main__() |
---|