annotate tools/filters/ucsc_gene_bed_to_exon_bed.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Read a table dump in the UCSC gene table format and print a tab separated
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 list of intervals corresponding to requested features of each gene.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 usage: ucsc_gene_table_to_intervals.py [options]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 options:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 -h, --help show this help message and exit
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 -rREGION, --region=REGION
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 Limit to region: one of coding, utr3, utr5, codon, intron, transcribed [default]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 -e, --exons Only print intervals overlapping an exon
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 -i, --input=inputfile input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 -o, --output=outputfile output file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 import optparse, string, sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 # Parse command line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 parser = optparse.OptionParser( usage="%prog [options] " )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 parser.add_option( "-r", "--region", dest="region", default="transcribed",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 help="Limit to region: one of coding, utr3, utr5, transcribed [default]" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 parser.add_option( "-e", "--exons", action="store_true", dest="exons",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 help="Only print intervals overlapping an exon" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 parser.add_option( "-s", "--strand", action="store_true", dest="strand",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 help="Print strand after interval" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 parser.add_option( "-i", "--input", dest="input", default=None,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 help="Input file" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 parser.add_option( "-o", "--output", dest="output", default=None,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 help="Output file" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 options, args = parser.parse_args()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 assert options.region in ( 'coding', 'utr3', 'utr5', 'transcribed', 'intron', 'codon' ), "Invalid region argument"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 out_file = open (options.output,"w")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 print >> sys.stderr, "Bad output file."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 sys.exit(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 in_file = open (options.input)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 print >> sys.stderr, "Bad input file."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 sys.exit(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 print "Region:", options.region+";"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 """print "Only overlap with Exons:",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 if options.exons:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 print "Yes"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 print "No"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 # Read table and handle each gene
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 for line in in_file:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 if line[0:1] == "#":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 # Parse fields from gene tabls
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 fields = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 chrom = fields[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 tx_start = int( fields[1] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 tx_end = int( fields[2] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 name = fields[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 strand = fields[5].replace(" ","_")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 cds_start = int( fields[6] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 cds_end = int( fields[7] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 # Determine the subset of the transcribed region we are interested in
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 if options.region == 'utr3':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 if strand == '-': region_start, region_end = tx_start, cds_start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 else: region_start, region_end = cds_end, tx_end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 elif options.region == 'utr5':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 if strand == '-': region_start, region_end = cds_end, tx_end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 else: region_start, region_end = tx_start, cds_start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 elif options.region == 'coding' or options.region == 'codon':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 region_start, region_end = cds_start, cds_end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 region_start, region_end = tx_start, tx_end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 # If only interested in exons, print the portion of each exon overlapping
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 # the region of interest, otherwise print the span of the region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 # options.exons is always TRUE
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 if options.exons:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 exon_starts = map( int, fields[11].rstrip( ',\n' ).split( ',' ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 exon_starts = map((lambda x: x + tx_start ), exon_starts)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 exon_ends = map( int, fields[10].rstrip( ',\n' ).split( ',' ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 exon_ends = map((lambda x, y: x + y ), exon_starts, exon_ends);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 #for Intron regions:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 if options.region == 'intron':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 i=0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 while i < len(exon_starts)-1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 intron_starts = exon_ends[i]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 intron_ends = exon_starts[i+1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 if strand: print_tab_sep(out_file, chrom, intron_starts, intron_ends, name, "0", strand )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 else: print_tab_sep(out_file, chrom, intron_starts, intron_ends )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 i+=1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 #for non-intron regions:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 for start, end in zip( exon_starts, exon_ends ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 start = max( start, region_start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 end = min( end, region_end )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 if start < end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 if options.region == 'codon':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 start += (3 - ((start-region_start)%3))%3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 c_start = start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 while c_start+3 <= end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 if strand:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 print_tab_sep(out_file, chrom, c_start, c_start+3, name, "0", strand )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 print_tab_sep(out_file, chrom, c_start, c_start+3)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 c_start += 3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 if strand:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 print_tab_sep(out_file, chrom, start, end, name, "0", strand )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 print_tab_sep(out_file, chrom, start, end )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 if options.region == 'codon':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 c_start = start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 c_end = end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 if c_start > c_end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 t = c_start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 c_start = c_end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 c_end = t
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 while c_start+3 <= c_end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 if strand:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 print_tab_sep(out_file, chrom, c_start, c_start+3, name, "0", strand )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 print_tab_sep(out_file, chrom, c_start, c_start+3)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 c_start += 3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 if strand:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 print_tab_sep(out_file, chrom, region_start, region_end, name, "0", strand )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 print_tab_sep(out_file, chrom, region_start, region_end )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 def print_tab_sep(out_file, *args ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 """Print items in `l` to stdout separated by tabs"""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 print >>out_file, string.join( [ str( f ) for f in args ], '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 if __name__ == "__main__": main()