annotate bed_to_gff.py @ 5:6e589f267c14

Uploaded
author devteam
date Tue, 04 Nov 2014 12:15:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
6e589f267c14 Uploaded
devteam
parents:
diff changeset
1 #!/usr/bin/env python
6e589f267c14 Uploaded
devteam
parents:
diff changeset
2 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
3 Convert genome annotation data in a 12 column BED format to GFF3.
6e589f267c14 Uploaded
devteam
parents:
diff changeset
4
6e589f267c14 Uploaded
devteam
parents:
diff changeset
5 Usage: python bed_to_gff.py in.bed > out.gff
6e589f267c14 Uploaded
devteam
parents:
diff changeset
6
6e589f267c14 Uploaded
devteam
parents:
diff changeset
7 Requirement:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
8 helper.py : https://github.com/vipints/GFFtools-GX/blob/master/helper.py
6e589f267c14 Uploaded
devteam
parents:
diff changeset
9
6e589f267c14 Uploaded
devteam
parents:
diff changeset
10 Copyright (C)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
11 2009-2012 Friedrich Miescher Laboratory of the Max Planck Society, Tubingen, Germany.
6e589f267c14 Uploaded
devteam
parents:
diff changeset
12 2012-2014 Memorial Sloan Kettering Cancer Center New York City, USA.
6e589f267c14 Uploaded
devteam
parents:
diff changeset
13 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
14
6e589f267c14 Uploaded
devteam
parents:
diff changeset
15 import re
6e589f267c14 Uploaded
devteam
parents:
diff changeset
16 import sys
6e589f267c14 Uploaded
devteam
parents:
diff changeset
17 import helper
6e589f267c14 Uploaded
devteam
parents:
diff changeset
18
6e589f267c14 Uploaded
devteam
parents:
diff changeset
19 def __main__():
6e589f267c14 Uploaded
devteam
parents:
diff changeset
20 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
21 main function
6e589f267c14 Uploaded
devteam
parents:
diff changeset
22 """
6e589f267c14 Uploaded
devteam
parents:
diff changeset
23
6e589f267c14 Uploaded
devteam
parents:
diff changeset
24 try:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
25 bed_fname = sys.argv[1]
6e589f267c14 Uploaded
devteam
parents:
diff changeset
26 except:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
27 print __doc__
6e589f267c14 Uploaded
devteam
parents:
diff changeset
28 sys.exit(-1)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
29
6e589f267c14 Uploaded
devteam
parents:
diff changeset
30 bed_fh = helper.open_file(bed_fname)
6e589f267c14 Uploaded
devteam
parents:
diff changeset
31
6e589f267c14 Uploaded
devteam
parents:
diff changeset
32 for line in bed_fh:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
33 line = line.strip( '\n\r' )
6e589f267c14 Uploaded
devteam
parents:
diff changeset
34
6e589f267c14 Uploaded
devteam
parents:
diff changeset
35 if not line or line[0] in ['#']:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
36 continue
6e589f267c14 Uploaded
devteam
parents:
diff changeset
37
6e589f267c14 Uploaded
devteam
parents:
diff changeset
38 parts = line.split('\t')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
39 assert len(parts) >= 12, line
6e589f267c14 Uploaded
devteam
parents:
diff changeset
40
6e589f267c14 Uploaded
devteam
parents:
diff changeset
41 rstarts = parts[-1].split(',')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
42 rstarts.pop() if rstarts[-1] == '' else rstarts
6e589f267c14 Uploaded
devteam
parents:
diff changeset
43
6e589f267c14 Uploaded
devteam
parents:
diff changeset
44 exon_lens = parts[-2].split(',')
6e589f267c14 Uploaded
devteam
parents:
diff changeset
45 exon_lens.pop() if exon_lens[-1] == '' else exon_lens
6e589f267c14 Uploaded
devteam
parents:
diff changeset
46
6e589f267c14 Uploaded
devteam
parents:
diff changeset
47 if len(rstarts) != len(exon_lens):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
48 continue # checking the consistency col 11 and col 12
6e589f267c14 Uploaded
devteam
parents:
diff changeset
49
6e589f267c14 Uploaded
devteam
parents:
diff changeset
50 if len(rstarts) != int(parts[-3]):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
51 continue # checking the number of exons and block count are same
6e589f267c14 Uploaded
devteam
parents:
diff changeset
52
6e589f267c14 Uploaded
devteam
parents:
diff changeset
53 if not parts[5] in ['+', '-']:
6e589f267c14 Uploaded
devteam
parents:
diff changeset
54 parts[5] = '.' # replace the unknown strand with '.'
6e589f267c14 Uploaded
devteam
parents:
diff changeset
55
6e589f267c14 Uploaded
devteam
parents:
diff changeset
56 # bed2gff result line
6e589f267c14 Uploaded
devteam
parents:
diff changeset
57 print '%s\tbed2gff\tgene\t%d\t%s\t%s\t%s\t.\tID=Gene:%s;Name=Gene:%s' % (parts[0], int(parts[1])+1, parts[2], parts[4], parts[5], parts[3], parts[3])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
58 print '%s\tbed2gff\ttranscript\t%d\t%s\t%s\t%s\t.\tID=%s;Name=%s;Parent=Gene:%s' % (parts[0], int(parts[1])+1, parts[2], parts[4], parts[5], parts[3], parts[3], parts[3])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
59
6e589f267c14 Uploaded
devteam
parents:
diff changeset
60 st = int(parts[1])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
61 for ex_cnt in range(int(parts[-3])):
6e589f267c14 Uploaded
devteam
parents:
diff changeset
62 start = st + int(rstarts[ex_cnt]) + 1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
63 stop = start + int(exon_lens[ex_cnt]) - 1
6e589f267c14 Uploaded
devteam
parents:
diff changeset
64 print '%s\tbed2gff\texon\t%d\t%d\t%s\t%s\t.\tParent=%s' % (parts[0], start, stop, parts[4], parts[5], parts[3])
6e589f267c14 Uploaded
devteam
parents:
diff changeset
65
6e589f267c14 Uploaded
devteam
parents:
diff changeset
66 bed_fh.close()
6e589f267c14 Uploaded
devteam
parents:
diff changeset
67
6e589f267c14 Uploaded
devteam
parents:
diff changeset
68
6e589f267c14 Uploaded
devteam
parents:
diff changeset
69 if __name__ == "__main__":
6e589f267c14 Uploaded
devteam
parents:
diff changeset
70 __main__()