annotate gff_to_bed.py @ 11:5c6f33e20fcc default tip

requirement tag added
author vipints <vipin@cbio.mskcc.org>
date Fri, 24 Apr 2015 18:04:27 -0400
parents c42c69aa81f8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
1 #!/usr/bin/env python
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
2 """
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
3 Convert genome annotation data in GFF/GTF to a 12 column BED format.
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
4 BED format typically represents the transcript models.
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
5
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
6 Usage: python gff_to_bed.py in.gff > out.bed
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
7
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
8 Requirement:
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
9 GFFParser.py: https://github.com/vipints/GFFtools-GX/blob/master/GFFParser.py
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
10
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
11 Copyright (C)
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
12 2009-2012 Friedrich Miescher Laboratory of the Max Planck Society, Tubingen, Germany.
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
13 2012-2015 Memorial Sloan Kettering Cancer Center New York City, USA.
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
14 """
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
15
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
16 import re
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
17 import sys
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
18 import GFFParser
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
19
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
20 def limitBEDWrite(tinfo):
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
21 """
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
22 Write a three column BED file
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
23
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
24 @args tinfo: list of genes
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
25 @type tinfo: numpy object
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
26 """
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
27
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
28 for contig_id, feature in tinfo.items():
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
29 uns_line = dict()
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
30 for tid, tloc in feature.items():
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
31 uns_line[(int(tloc[0])-1, int(tloc[1]))]=1
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
32 for ele in sorted(uns_line):
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
33 pline = [contig_id,
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
34 str(ele[0]-1),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
35 str(ele[1])]
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
36
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
37 sys.stdout.write('\t'.join(pline)+"\n")
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
38
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
39
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
40 def writeBED(tinfo):
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
41 """
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
42 writing result files in bed format
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
43
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
44 @args tinfo: list of genes
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
45 @type tinfo: numpy object
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
46 """
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
47
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
48 for ent1 in tinfo:
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
49 child_flag = False
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
50
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
51 for idx, tid in enumerate(ent1['transcripts']):
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
52 child_flag = True
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
53 exon_cnt = len(ent1['exons'][idx])
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
54 exon_len = ''
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
55 exon_cod = ''
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
56 rel_start = None
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
57 rel_stop = None
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
58 for idz, ex_cod in enumerate(ent1['exons'][idx]):#check for exons of corresponding transcript
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
59 exon_len += '%d,' % (ex_cod[1]-ex_cod[0]+1)
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
60 if idz == 0: #calculate the relative start position
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
61 exon_cod += '0,'
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
62 rel_start = int(ex_cod[0])-1
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
63 rel_stop = int(ex_cod[1])
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
64 else:
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
65 exon_cod += '%d,' % (ex_cod[0]-1-rel_start) ## shifting the coordinates to zero
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
66 rel_stop = int(ex_cod[1])
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
67
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
68 if exon_len:
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
69 score = 0
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
70 score = ent1['transcript_score'][idx] if ent1['transcript_score'].any() else score ## getting the transcript score
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
71 out_print = [ent1['chr'],
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
72 str(rel_start),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
73 str(rel_stop),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
74 tid[0],
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
75 str(score),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
76 ent1['strand'],
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
77 str(rel_start),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
78 str(rel_stop),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
79 '0',
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
80 str(exon_cnt),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
81 exon_len,
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
82 exon_cod]
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
83 sys.stdout.write('\t'.join(out_print)+"\n")
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
84
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
85 if not child_flag: # file just contains only a single parent type i.e, gff3 defines only one feature type
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
86 score = 0
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
87 score = ent1['transcript_score'][0] if ent1['transcript_score'].any() else score
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
88
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
89 out_print = [ent1['chr'],
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
90 '%d' % int(ent1['start'])-1,
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
91 '%d' % int(ent1['stop']),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
92 ent1['name'],
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
93 str(score),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
94 ent1['strand'],
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
95 '%d' % int(ent1['start']),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
96 '%d' % int(ent1['stop']),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
97 '0',
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
98 '1',
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
99 '%d,' % (int(ent1['stop'])-int(ent1['start'])+1),
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
100 '0,']
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
101
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
102 sys.stdout.write('\t'.join(out_print)+"\n")
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
103
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
104
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
105 def __main__():
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
106 try:
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
107 query_file = sys.argv[1]
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
108 except:
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
109 print __doc__
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
110 sys.exit(-1)
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
111
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
112 Transcriptdb = GFFParser.Parse(query_file)
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
113 writeBED(Transcriptdb)
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
114
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
115 if __name__ == "__main__":
c42c69aa81f8 fixed manually the upload of version 2.1.0 - deleted accidentally added files to the repo
vipints <vipin@cbio.mskcc.org>
parents:
diff changeset
116 __main__()