annotate utils/gff_util.py @ 2:9bf4f3968e07

Added tool images.
author devteam <devteam@galaxyproject.org>
date Mon, 14 Apr 2014 09:19:55 -0400
parents d4d33e26deef
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
1 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
2 Provides utilities for working with GFF files.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
3 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
4
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
5 import copy
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
6 from bx.intervals.io import *
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
7 from bx.tabular.io import Header, Comment
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
8 from utils.odict import odict
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
9
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
10 class GFFInterval( GenomicInterval ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
11 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
12 A GFF interval, including attributes. If file is strictly a GFF file,
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
13 only attribute is 'group.'
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
14 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
15 def __init__( self, reader, fields, chrom_col=0, feature_col=2, start_col=3, end_col=4, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
16 strand_col=6, score_col=5, default_strand='.', fix_strand=False ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
17 # HACK: GFF format allows '.' for strand but GenomicInterval does not. To get around this,
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
18 # temporarily set strand and then unset after initing GenomicInterval.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
19 unknown_strand = False
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
20 if not fix_strand and fields[ strand_col ] == '.':
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
21 unknown_strand = True
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
22 fields[ strand_col ] = '+'
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
23 GenomicInterval.__init__( self, reader, fields, chrom_col, start_col, end_col, strand_col, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
24 default_strand, fix_strand=fix_strand )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
25 if unknown_strand:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
26 self.strand = '.'
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
27 self.fields[ strand_col ] = '.'
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
28
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
29 # Handle feature, score column.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
30 self.feature_col = feature_col
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
31 if self.feature_col >= self.nfields:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
32 raise MissingFieldError( "No field for feature_col (%d)" % feature_col )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
33 self.feature = self.fields[ self.feature_col ]
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
34 self.score_col = score_col
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
35 if self.score_col >= self.nfields:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
36 raise MissingFieldError( "No field for score_col (%d)" % score_col )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
37 self.score = self.fields[ self.score_col ]
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
38
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
39 # GFF attributes.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
40 self.attributes = parse_gff_attributes( fields[8] )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
41
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
42 def copy( self ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
43 return GFFInterval(self.reader, list( self.fields ), self.chrom_col, self.feature_col, self.start_col,
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
44 self.end_col, self.strand_col, self.score_col, self.strand)
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
45
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
46 class GFFFeature( GFFInterval ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
47 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
48 A GFF feature, which can include multiple intervals.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
49 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
50 def __init__( self, reader, chrom_col=0, feature_col=2, start_col=3, end_col=4, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
51 strand_col=6, score_col=5, default_strand='.', fix_strand=False, intervals=[], \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
52 raw_size=0 ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
53 # Use copy so that first interval and feature do not share fields.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
54 GFFInterval.__init__( self, reader, copy.deepcopy( intervals[0].fields ), chrom_col, feature_col, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
55 start_col, end_col, strand_col, score_col, default_strand, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
56 fix_strand=fix_strand )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
57 self.intervals = intervals
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
58 self.raw_size = raw_size
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
59 # Use intervals to set feature attributes.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
60 for interval in self.intervals:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
61 # Error checking. NOTE: intervals need not share the same strand.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
62 if interval.chrom != self.chrom:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
63 raise ValueError( "interval chrom does not match self chrom: %s != %s" % \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
64 ( interval.chrom, self.chrom ) )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
65 # Set start, end of interval.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
66 if interval.start < self.start:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
67 self.start = interval.start
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
68 if interval.end > self.end:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
69 self.end = interval.end
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
70
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
71 def name( self ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
72 """ Returns feature's name. """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
73 name = None
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
74 # Preference for name: GTF, GFF3, GFF.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
75 for attr_name in [
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
76 # GTF:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
77 'gene_id', 'transcript_id',
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
78 # GFF3:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
79 'ID', 'id',
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
80 # GFF (TODO):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
81 'group' ]:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
82 name = self.attributes.get( attr_name, None )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
83 if name is not None:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
84 break
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
85 return name
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
86
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
87 def copy( self ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
88 intervals_copy = []
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
89 for interval in self.intervals:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
90 intervals_copy.append( interval.copy() )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
91 return GFFFeature(self.reader, self.chrom_col, self.feature_col, self.start_col, self.end_col, self.strand_col,
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
92 self.score_col, self.strand, intervals=intervals_copy )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
93
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
94 def lines( self ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
95 lines = []
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
96 for interval in self.intervals:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
97 lines.append( '\t'.join( interval.fields ) )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
98 return lines
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
99
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
100
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
101 class GFFIntervalToBEDReaderWrapper( NiceReaderWrapper ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
102 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
103 Reader wrapper that reads GFF intervals/lines and automatically converts
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
104 them to BED format.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
105 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
106
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
107 def parse_row( self, line ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
108 # HACK: this should return a GFF interval, but bx-python operations
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
109 # require GenomicInterval objects and subclasses will not work.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
110 interval = GenomicInterval( self, line.split( "\t" ), self.chrom_col, self.start_col, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
111 self.end_col, self.strand_col, self.default_strand, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
112 fix_strand=self.fix_strand )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
113 interval = convert_gff_coords_to_bed( interval )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
114 return interval
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
115
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
116 class GFFReaderWrapper( NiceReaderWrapper ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
117 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
118 Reader wrapper for GFF files.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
119
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
120 Wrapper has two major functions:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
121
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
122 1. group entries for GFF file (via group column), GFF3 (via id attribute),
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
123 or GTF (via gene_id/transcript id);
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
124 2. convert coordinates from GFF format--starting and ending coordinates
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
125 are 1-based, closed--to the 'traditional'/BED interval format--0 based,
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
126 half-open. This is useful when using GFF files as inputs to tools that
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
127 expect traditional interval format.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
128 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
129
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
130 def __init__( self, reader, chrom_col=0, feature_col=2, start_col=3, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
131 end_col=4, strand_col=6, score_col=5, fix_strand=False, convert_to_bed_coord=False, **kwargs ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
132 NiceReaderWrapper.__init__( self, reader, chrom_col=chrom_col, start_col=start_col, end_col=end_col, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
133 strand_col=strand_col, fix_strand=fix_strand, **kwargs )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
134 self.feature_col = feature_col
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
135 self.score_col = score_col
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
136 self.convert_to_bed_coord = convert_to_bed_coord
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
137 self.last_line = None
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
138 self.cur_offset = 0
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
139 self.seed_interval = None
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
140 self.seed_interval_line_len = 0
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
141
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
142 def parse_row( self, line ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
143 interval = GFFInterval( self, line.split( "\t" ), self.chrom_col, self.feature_col, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
144 self.start_col, self.end_col, self.strand_col, self.score_col, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
145 self.default_strand, fix_strand=self.fix_strand )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
146 return interval
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
147
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
148 def next( self ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
149 """ Returns next GFFFeature. """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
150
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
151 #
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
152 # Helper function.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
153 #
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
154
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
155 def handle_parse_error( parse_error ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
156 """ Actions to take when ParseError found. """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
157 if self.outstream:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
158 if self.print_delegate and hasattr(self.print_delegate,"__call__"):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
159 self.print_delegate( self.outstream, e, self )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
160 self.skipped += 1
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
161 # no reason to stuff an entire bad file into memmory
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
162 if self.skipped < 10:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
163 self.skipped_lines.append( ( self.linenum, self.current_line, str( e ) ) )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
164
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
165 # For debugging, uncomment this to propogate parsing exceptions up.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
166 # I.e. the underlying reason for an unexpected StopIteration exception
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
167 # can be found by uncommenting this.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
168 # raise e
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
169
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
170 #
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
171 # Get next GFFFeature
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
172 #
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
173 raw_size = self.seed_interval_line_len
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
174
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
175 # If there is no seed interval, set one. Also, if there are no more
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
176 # intervals to read, this is where iterator dies.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
177 if not self.seed_interval:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
178 while not self.seed_interval:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
179 try:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
180 self.seed_interval = GenomicIntervalReader.next( self )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
181 except ParseError, e:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
182 handle_parse_error( e )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
183 # TODO: When no longer supporting python 2.4 use finally:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
184 #finally:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
185 raw_size += len( self.current_line )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
186
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
187 # If header or comment, clear seed interval and return it with its size.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
188 if isinstance( self.seed_interval, ( Header, Comment ) ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
189 return_val = self.seed_interval
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
190 return_val.raw_size = len( self.current_line )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
191 self.seed_interval = None
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
192 self.seed_interval_line_len = 0
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
193 return return_val
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
194
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
195 # Initialize feature identifier from seed.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
196 feature_group = self.seed_interval.attributes.get( 'group', None ) # For GFF
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
197 # For GFF3
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
198 feature_id = self.seed_interval.attributes.get( 'ID', None )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
199 feature_parent_id = self.seed_interval.attributes.get( 'Parent', None )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
200 # For GTF.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
201 feature_gene_id = self.seed_interval.attributes.get( 'gene_id', None )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
202 feature_transcript_id = self.seed_interval.attributes.get( 'transcript_id', None )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
203
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
204 # Read all intervals associated with seed.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
205 feature_intervals = []
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
206 feature_intervals.append( self.seed_interval )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
207 while True:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
208 try:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
209 interval = GenomicIntervalReader.next( self )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
210 raw_size += len( self.current_line )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
211 except StopIteration, e:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
212 # No more intervals to read, but last feature needs to be
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
213 # returned.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
214 interval = None
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
215 raw_size += len( self.current_line )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
216 break
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
217 except ParseError, e:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
218 handle_parse_error( e )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
219 raw_size += len( self.current_line )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
220 continue
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
221 # TODO: When no longer supporting python 2.4 use finally:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
222 #finally:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
223 #raw_size += len( self.current_line )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
224
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
225 # Ignore comments.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
226 if isinstance( interval, Comment ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
227 continue
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
228
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
229 # Determine if interval is part of feature.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
230 part_of = False
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
231 group = interval.attributes.get( 'group', None )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
232 # GFF test:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
233 if group and feature_group == group:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
234 part_of = True
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
235 # GFF3 test:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
236 parent_id = interval.attributes.get( 'Parent', None )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
237 cur_id = interval.attributes.get( 'ID', None )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
238 if ( cur_id and cur_id == feature_id ) or ( parent_id and parent_id == feature_id ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
239 part_of = True
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
240 # GTF test:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
241 transcript_id = interval.attributes.get( 'transcript_id', None )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
242 if transcript_id and transcript_id == feature_transcript_id:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
243 part_of = True
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
244
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
245 # If interval is not part of feature, clean up and break.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
246 if not part_of:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
247 # Adjust raw size because current line is not part of feature.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
248 raw_size -= len( self.current_line )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
249 break
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
250
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
251 # Interval associated with feature.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
252 feature_intervals.append( interval )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
253
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
254 # Last interval read is the seed for the next interval.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
255 self.seed_interval = interval
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
256 self.seed_interval_line_len = len( self.current_line )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
257
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
258 # Return feature.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
259 feature = GFFFeature( self, self.chrom_col, self.feature_col, self.start_col, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
260 self.end_col, self.strand_col, self.score_col, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
261 self.default_strand, fix_strand=self.fix_strand, \
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
262 intervals=feature_intervals, raw_size=raw_size )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
263
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
264 # Convert to BED coords?
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
265 if self.convert_to_bed_coord:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
266 convert_gff_coords_to_bed( feature )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
267
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
268 return feature
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
269
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
270 def convert_bed_coords_to_gff( interval ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
271 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
272 Converts an interval object's coordinates from BED format to GFF format.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
273 Accepted object types include GenomicInterval and list (where the first
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
274 element in the list is the interval's start, and the second element is
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
275 the interval's end).
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
276 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
277 if isinstance( interval, GenomicInterval ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
278 interval.start += 1
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
279 if isinstance( interval, GFFFeature ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
280 for subinterval in interval.intervals:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
281 convert_bed_coords_to_gff( subinterval )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
282 elif type ( interval ) is list:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
283 interval[ 0 ] += 1
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
284 return interval
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
285
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
286 def convert_gff_coords_to_bed( interval ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
287 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
288 Converts an interval object's coordinates from GFF format to BED format.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
289 Accepted object types include GFFFeature, GenomicInterval, and list (where
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
290 the first element in the list is the interval's start, and the second
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
291 element is the interval's end).
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
292 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
293 if isinstance( interval, GenomicInterval ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
294 interval.start -= 1
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
295 if isinstance( interval, GFFFeature ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
296 for subinterval in interval.intervals:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
297 convert_gff_coords_to_bed( subinterval )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
298 elif type ( interval ) is list:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
299 interval[ 0 ] -= 1
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
300 return interval
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
301
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
302 def parse_gff_attributes( attr_str ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
303 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
304 Parses a GFF/GTF attribute string and returns a dictionary of name-value
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
305 pairs. The general format for a GFF3 attributes string is
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
306
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
307 name1=value1;name2=value2
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
308
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
309 The general format for a GTF attribute string is
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
310
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
311 name1 "value1" ; name2 "value2"
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
312
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
313 The general format for a GFF attribute string is a single string that
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
314 denotes the interval's group; in this case, method returns a dictionary
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
315 with a single key-value pair, and key name is 'group'
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
316 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
317 attributes_list = attr_str.split(";")
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
318 attributes = {}
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
319 for name_value_pair in attributes_list:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
320 # Try splitting by '=' (GFF3) first because spaces are allowed in GFF3
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
321 # attribute; next, try double quotes for GTF.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
322 pair = name_value_pair.strip().split("=")
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
323 if len( pair ) == 1:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
324 pair = name_value_pair.strip().split("\"")
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
325 if len( pair ) == 1:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
326 # Could not split for some reason -- raise exception?
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
327 continue
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
328 if pair == '':
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
329 continue
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
330 name = pair[0].strip()
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
331 if name == '':
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
332 continue
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
333 # Need to strip double quote from values
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
334 value = pair[1].strip(" \"")
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
335 attributes[ name ] = value
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
336
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
337 if len( attributes ) == 0:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
338 # Could not split attributes string, so entire string must be
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
339 # 'group' attribute. This is the case for strictly GFF files.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
340 attributes['group'] = attr_str
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
341 return attributes
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
342
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
343 def gff_attributes_to_str( attrs, gff_format ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
344 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
345 Convert GFF attributes to string. Supported formats are GFF3, GTF.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
346 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
347 if gff_format == 'GTF':
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
348 format_string = '%s "%s"'
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
349 # Convert group (GFF) and ID, parent (GFF3) attributes to transcript_id, gene_id
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
350 id_attr = None
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
351 if 'group' in attrs:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
352 id_attr = 'group'
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
353 elif 'ID' in attrs:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
354 id_attr = 'ID'
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
355 elif 'Parent' in attrs:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
356 id_attr = 'Parent'
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
357 if id_attr:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
358 attrs['transcript_id'] = attrs['gene_id'] = attrs[id_attr]
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
359 elif gff_format == 'GFF3':
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
360 format_string = '%s=%s'
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
361 attrs_strs = []
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
362 for name, value in attrs.items():
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
363 attrs_strs.append( format_string % ( name, value ) )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
364 return " ; ".join( attrs_strs )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
365
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
366 def read_unordered_gtf( iterator, strict=False ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
367 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
368 Returns GTF features found in an iterator. GTF lines need not be ordered
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
369 or clustered for reader to work. Reader returns GFFFeature objects sorted
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
370 by transcript_id, chrom, and start position.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
371 """
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
372
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
373 # -- Get function that generates line/feature key. --
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
374
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
375 get_transcript_id = lambda fields: parse_gff_attributes( fields[8] )[ 'transcript_id' ]
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
376 if strict:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
377 # Strict GTF parsing uses transcript_id only to group lines into feature.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
378 key_fn = get_transcript_id
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
379 else:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
380 # Use lenient parsing where chromosome + transcript_id is the key. This allows
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
381 # transcripts with same ID on different chromosomes; this occurs in some popular
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
382 # datasources, such as RefGenes in UCSC.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
383 key_fn = lambda fields: fields[0] + '_' + get_transcript_id( fields )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
384
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
385
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
386 # Aggregate intervals by transcript_id and collect comments.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
387 feature_intervals = odict()
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
388 comments = []
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
389 for count, line in enumerate( iterator ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
390 if line.startswith( '#' ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
391 comments.append( Comment( line ) )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
392 continue
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
393
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
394 line_key = key_fn( line.split('\t') )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
395 if line_key in feature_intervals:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
396 feature = feature_intervals[ line_key ]
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
397 else:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
398 feature = []
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
399 feature_intervals[ line_key ] = feature
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
400 feature.append( GFFInterval( None, line.split( '\t' ) ) )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
401
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
402 # Create features.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
403 chroms_features = {}
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
404 for count, intervals in enumerate( feature_intervals.values() ):
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
405 # Sort intervals by start position.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
406 intervals.sort( lambda a,b: cmp( a.start, b.start ) )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
407 feature = GFFFeature( None, intervals=intervals )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
408 if feature.chrom not in chroms_features:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
409 chroms_features[ feature.chrom ] = []
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
410 chroms_features[ feature.chrom ].append( feature )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
411
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
412 # Sort features by chrom, start position.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
413 chroms_features_sorted = []
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
414 for chrom_features in chroms_features.values():
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
415 chroms_features_sorted.append( chrom_features )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
416 chroms_features_sorted.sort( lambda a,b: cmp( a[0].chrom, b[0].chrom ) )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
417 for features in chroms_features_sorted:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
418 features.sort( lambda a,b: cmp( a.start, b.start ) )
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
419
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
420 # Yield comments first, then features.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
421 # FIXME: comments can appear anywhere in file, not just the beginning.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
422 # Ideally, then comments would be associated with features and output
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
423 # just before feature/line.
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
424 for comment in comments:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
425 yield comment
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
426
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
427 for chrom_features in chroms_features_sorted:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
428 for feature in chrom_features:
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
429 yield feature
d4d33e26deef Imported from capsule None
devteam
parents:
diff changeset
430