-
Notifications
You must be signed in to change notification settings - Fork 0
/
gff2bed.py
executable file
·50 lines (42 loc) · 1.47 KB
/
gff2bed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
#
# Author: Alex Reynolds
# Project: Convert GFF3 to six-column UCSC BED
# Dependencies: Biopython (http://biopython.org/wiki/Biopython) and
# BCBio (https://github.com/chapmanb/bcbb)
#
# Example usage:
#
# $ gff2bed.py < foo.gff | sort-bed -
#
import sys
from BCBio import GFF
def main(*args):
max_target_lines = 1000
requiredVersion = (2,5)
checkInstallation( requiredVersion )
gff_handle = sys.stdin
for rec in GFF.parse(gff_handle, target_lines = max_target_lines):
elem_chr = rec.id
for feat in rec.features:
elem_start = str(feat.location._start)
end = feat.location._end
elem_stop = str(feat.location._end.position - 1)
elem_id = str(feat.id)
try:
elem_score = str(feat.qualifiers['score'][0])
except KeyError:
elem_score = '0'
elem_strand = '+' if feat.strand == 1 else '-'
print '\t'.join(["chr"+elem_chr, elem_start, elem_stop, elem_id, elem_score, elem_strand])
return 0
def checkInstallation(rv):
currentVersion = sys.version_info
if currentVersion[0] == rv[0] and currentVersion[1] >= rv[1]:
pass
else:
sys.stderr.write( "[%s] - Error: Your Python interpreter must be %d.%d or greater (within major version %d)\n" % (sys.argv[0], rv[0], rv[1], rv[0]) )
sys.exit(-1)
return 0
if __name__ == '__main__':
sys.exit(main(*sys.argv))