File Coverage

Bio/SearchIO/Writer/GbrowseGFF.pm
Criterion Covered Total %
statement 94 153 61.4
branch 33 88 37.5
condition 9 31 29.0
subroutine 8 10 80.0
pod 4 5 80.0
total 148 287 51.5


line stmt bran cond sub pod time code
1             #-----------------------------------------------------------------
2             #
3             # BioPerl module Bio::SearchIO::Writer::GbrowseGFF.pm
4             #
5             # Please direct questions and support issues to
6             #
7             # Cared for by Mark Wilkinson
8             #
9             # You may distribute this module under the same terms as perl itself
10             #-----------------------------------------------------------------
11              
12             =head1 NAME
13              
14             Bio::SearchIO::Writer::GbrowseGFF - Interface for outputting parsed search results in Gbrowse GFF format
15              
16             =head1 SYNOPSIS
17              
18             use Bio::SearchIO;
19             my $in = Bio::SearchIO->new(-file => 'result.blast',
20             -format => 'blast');
21             my $out = Bio::SearchIO->new(-output_format => 'GbrowseGFF',
22             -output_cigar => 1,
23             -output_signif => 1,
24             -file => ">result.gff");
25             while( my $r = $in->next_result ) {
26             $out->write_result($r);
27             }
28              
29             =head1 DESCRIPTION
30              
31             This writer produces Gbrowse flavour GFF from a Search::Result object.
32              
33             =head1 AUTHOR Mark Wilkinson
34              
35             Email markw-at-illuminae-dot-com
36              
37             =head1 CONTRIBUTORS
38              
39             Susan Miller sjmiller at email-DOT-arizon-DOT-edu
40             Jason Stajich jason at bioperl-dot-org
41              
42             =head1 FEEDBACK
43              
44             =head2 Mailing Lists
45              
46             User feedback is an integral part of the evolution of this and other
47             Bioperl modules. Send your comments and suggestions preferably to
48             the Bioperl mailing list. Your participation is much appreciated.
49              
50             bioperl-l@bioperl.org - General discussion
51             http://bioperl.org/wiki/Mailing_lists - About the mailing lists
52              
53             =head2 Support
54              
55             Please direct usage questions or support issues to the mailing list:
56              
57             I
58              
59             rather than to the module maintainer directly. Many experienced and
60             reponsive experts will be able look at the problem and quickly
61             address it. Please include a thorough description of the problem
62             with code and data examples if at all possible.
63              
64             =head2 Reporting Bugs
65              
66             Report bugs to the Bioperl bug tracking system to help us keep track
67             of the bugs and their resolution. Bug reports can be submitted via
68             the web:
69              
70             https://github.com/bioperl/bioperl-live/issues
71              
72             =head1 APPENDIX
73              
74             The rest of the documentation details each of the object methods.
75             Internal methods are usually preceded with a _
76              
77             =cut
78              
79              
80             package Bio::SearchIO::Writer::GbrowseGFF;
81 1     1   5 use vars qw(%Defaults);
  1         2  
  1         39  
82 1     1   4 use strict;
  1         2  
  1         35  
83              
84             $Defaults{'Prefix'} = 'EST';
85             $Defaults{'HSPTag'} = 'HSP';
86             $Defaults{'MatchTag'} = 'match';
87              
88 1     1   4 use base qw(Bio::Root::Root Bio::SearchIO::SearchWriterI);
  1         2  
  1         264  
89              
90              
91             =head2 new
92              
93             Title : new
94             Usage : my $obj = Bio::SearchIO::Writer::GbrowseGFF->new(@args);
95             Function: Builds a new Bio::SearchIO::Writer::GbrowseGFF object
96             Returns : an instance of Bio::SearchIO::Writer::GbrowseGFF
97             Args : -e_value => 10 : set e_value parsing cutoff (default undef)
98             (note the -e_value flag is deprecated.)
99              
100             =cut
101              
102             sub new {
103 1     1 1 4 my($class,@args) = @_;
104              
105 1         6 my $self = $class->SUPER::new(@args);
106             ($self->{'_evalue'},
107             $self->{'_cigar'},
108             $self->{'_prefix'},
109 1         7 $self->{'_signif'} ) = $self->_rearrange([qw(E_VALUE OUTPUT_CIGAR PREFIX
110             OUTPUT_SIGNIF)], @args);
111 1 50       3 $self->{'_evalue'} && warn( "Use of the -e_value argument is deprecated.\nIn future, use \$writer->filter(\"type\", \&code) instead.\n\tparsing will proceed correctly with this e_value\n");
112 1         2 $self->{Gbrowse_HSPID} = 0;
113 1         2 $self->{Gbrowse_HITID} = 0;
114 1   33     2 $self->{'_prefix'} ||= $Defaults{'Prefix'};
115 1         3 return $self;
116             }
117              
118             sub _incrementHSP {
119 5     5   7 my ($self) = @_;
120 5         7 return ++$self->{Gbrowse_HSPID};
121             }
122              
123             sub _incrementHIT {
124 2     2   3 my ($self) = @_;
125             return ++$self->{Gbrowse_HITID}
126 2         4 }
127             # according to the GFF3 spec:
128             #"match". In addition to the generic "match"
129             #type, there are the subclasses "cDNA_match," "EST_match,"
130             #"translated_nucleotide_match," "nucleotide_to_protein_match," and
131             #"nucleotide_motif."
132              
133             =head2 to_string
134              
135             Purpose : Produce the Gbrowse format GFF lines for a Result
136             Usage : print $writer->to_string( $result_obj, @args);
137             Argument : $result_obj = A Bio::Search::Result::ResultI object
138             -version => 1|2|2.5|3 ; the GFF format you want to output (default 3)
139             -match_tag => match|cDNA_match|EST_match|translated_nucleotide_match
140             nucleotide_to_protein_match|nucleotide_motif
141             This is the SO term to be placed in GFF column 3.
142             -prefix => String to prefix the group by, default is EST
143             (see %Defaults class variable) A default can also
144             be set on object init
145             Returns : String containing data for each search Result or any of its
146             : sub-objects (Hits and HSPs).
147             Throws : n/a
148              
149             =cut
150              
151             #-reference => 'hit'|'query' ; whether the hit sequence name or the
152             # query sequence name is used as the
153             # 'reference' sequence (GFF column 1)
154              
155             sub to_string {
156 1     1 1 2 my ($self, $result, @args) = @_;
157 1         4 my ($format, $reference,
158             $match_tag,$hsp_tag,
159             $prefix) = $self->_rearrange([qw
160             (VERSION
161             REFERENCE
162             MATCH_TAG HSP_TAG
163             PREFIX)], @args);
164 1 50       3 $self->warn($reference) if $reference;
165 1   50     14 $reference ||='hit'; # default is that the hit sequence (db sequence) becomes the reference sequence. I think this is fairly typical...
166 1   33     9 $match_tag ||= $Defaults{'MatchTag'}; # default is the generic 'match' tag.
167 1   33     3 $hsp_tag ||= $Defaults{'HSPTag'}; # default is the generic 'hsp' tag.
168 1   33     4 $prefix ||= $self->{'_prefix'};
169 1 50       2 $self->throw("$reference must be one of 'query', or 'hit'\n") unless $reference;
170            
171             #************* THIS IS WHERE I STOPPED ****************
172             # *****************************************************
173             #*************************************************
174            
175 1   50     4 $format ||='3';
176 1         8 my $gffio = Bio::Tools::GFF->new(-gff_version => $format); # try to set it
177            
178             # just in case that behaviour changes (at the moment, an invalid format throws an exception, but it might return undef in the future
179 1 50       3 return "" unless defined $gffio; # be kind and don't return undef in case the person is putting the output directly into a print statement without testing it
180             # now $gffio is either false, or a valid GFF formatter
181              
182 1         1 my ($GFF,$cigar,$score);
183 1         6 my ($resultfilter,$hitfilter,$hspfilter) = (
184             $self->filter('RESULT'),
185             $self->filter('HIT'),
186             $self->filter('HSP'));
187 1 50       12 $result->can('rewind') && $result->rewind(); # ensure we're at the beginning
188 1 50 33     3 next if (defined $resultfilter && ! (&{$resultfilter}($result)) );
  0         0  
189              
190 1         2 while( my $hit = $result->next_hit ) {
191            
192 2 50       5 if (defined $self->{_evalue}){
193 0 0       0 next unless ($hit->significance < $self->{_evalue});
194             }
195 2 50 33     4 next if( defined $hitfilter && ! &{$hitfilter}($hit) ); # test against filter code
  0         0  
196              
197 2 50       7 my $refseq = $reference eq 'hit' ? $hit->name : $result->query_name;
198 2 50       7 my $seqname = $reference eq 'hit' ? $result->query_name : $hit->name; # hopefully this will be a simple identifier without a full description line!!
199 2 50       8 if ($self->{_signif}) {
200 2         5 $score = $hit->significance;
201             } else {
202 0         0 $score = $hit->raw_score;
203             }
204 2 50       3 $self->throw("No reference sequence name found in hit; required for GFF (this may not be your fault if your report type does not include reference sequence names)\n") unless $refseq;
205 2         4 my $source = $hit->algorithm;
206 2 50       4 $self->throw("No algorithm name found in hit; required for GFF (this may not be your fault if your report type does not include algorithm names)\n") unless $refseq;
207 2 50       4 $self->throw("This module only works on BLASTN reports at this time. Sorry.\n") unless $source eq "BLASTN";
208            
209 2         4 my @plus_hsps;
210             my @minus_hsps;
211            
212             # pre-process the HSP's because we later need to know
213             # the extents of the plus and munus strand
214             # on both the subject and query strands individually
215 2         0 my ($qpmin, $qpmax, $qmmin, $qmmax, $spmin, $spmax, $smmin, $smmax); # variables for the plus/minus strand min start and max end to know the full extents of the hit
216 2         7 while( my $hsp = $hit->next_hsp ) {
217 5 50       9 if ( defined $self->{_evalue} ) {
218             # for backward compatibility only
219 0 0       0 next unless ($hsp->significance < $self->{_evalue});
220             }
221 5 50 33     10 next if( defined $hspfilter && ! &{$hspfilter}($hsp) ); # test against HSP filter
  0         0  
222 5 50       9 if ($hsp->hit->strand >= 0 ){
223 5         6 push @plus_hsps, $hsp;
224 5 100       9 if (defined $qpmin){ # set or reset the minimum and maximum extent of the plus-strand hit
225 3 50       5 $qpmin = $hsp->query->start if $hsp->query->start < $qpmin;
226 3 100       6 $qpmax = $hsp->query->end if $hsp->query->end > $qpmax;
227 3 50       11 $spmin = $hsp->hit->start if $hsp->hit->start < $spmin;
228 3 100       7 $spmax = $hsp->hit->end if $hsp->hit->end > $spmax;
229             } else {
230 2         4 $qpmin = $hsp->query->start;
231 2         4 $qpmax = $hsp->query->end;
232 2         4 $spmin = $hsp->hit->start;
233 2         5 $spmax = $hsp->hit->end;
234             }
235             }
236 5 50       10 if ($hsp->hit->strand < 0 ){
237 0         0 push @minus_hsps, $hsp;
238 0 0       0 if (defined $qmmin){ # set or reset the minimum and maximum extent of the minus-strand hit
239 0 0       0 $qmmin = $hsp->query->start if $hsp->query->start < $qmmin;
240 0 0       0 $qmmax = $hsp->query->end if $hsp->query->end > $qmmax;
241 0 0       0 $smmin = $hsp->hit->start if $hsp->hit->start < $smmin;
242 0 0       0 $smmax = $hsp->hit->end if $hsp->hit->end > $smmax;
243             } else {
244 0         0 $qmmin = $hsp->query->start;
245 0         0 $qmmax = $hsp->query->end;
246 0         0 $smmin = $hsp->hit->start;
247 0         0 $smmax = $hsp->hit->end;
248             }
249             }
250             #else next if there is no strand, but that makes no sense..??
251             }
252 2 50       6 next unless (scalar(@plus_hsps) + scalar(@minus_hsps)); # next if no hsps (??)
253 2         5 my $ID = $self->_incrementHIT();
254             # okay, write out the index line for the entire hit before
255             # processing HSP's
256             # unfortunately (or not??), HitI objects do not implement
257             # SeqFeatureI, so we can't just call ->gff_string
258             # as a result, this module is quite brittle to changes
259             # in the GFF format since we are hard-coding the GFF output here :-(
260            
261 2 50       4 if (scalar(@plus_hsps)){
262 2         12 my %tags = ( 'ID' => "match_sequence$ID");
263              
264 2 50       7 if ($format==2.5) {
265 0         0 $tags{'Target'} = "$prefix:$seqname";
266 0         0 $tags{'tstart'} = $qmmin;
267 0         0 $tags{'tend'} = $qmmax;
268             } else {
269 2         6 $tags{'Target'} = "$prefix:$seqname $qpmin $qpmax";
270             }
271 2 50       3 if ( $self->{'_cigar'} ) {
272 2         3 $tags{'Gap'} = $cigar;
273             }
274 2         10 my $feat = Bio::SeqFeature::Generic->new(
275             -seq_id => $refseq,
276             -source_tag => $source,
277             -primary_tag => $match_tag,
278             -start => $spmin,
279             -end => $spmax,
280             -score => $score,
281             -strand => '+',
282             -frame => '.',
283             -tag => \%tags
284             );
285              
286              
287 2         8 my $formatter = Bio::Tools::GFF->new(-gff_version => $format);
288 2         6 $GFF .= $feat->gff_string($formatter)."\n";
289             }
290 2 50       5 if (scalar(@minus_hsps)){
291 0         0 my %tags = ( 'ID' => "match_sequence$ID");
292              
293 0 0       0 if ($format==2.5) {
294 0         0 $tags{'Target'} = "$prefix:$seqname";
295 0         0 $tags{'tstart'} = $qpmax;
296 0         0 $tags{'tend'} = $qpmin;
297             }
298             else {
299 0         0 $tags{'Target'} = "$prefix:$seqname $qpmax $qpmin";
300             }
301 0         0 my $feat = Bio::SeqFeature::Generic->new(
302             -seq_id => $refseq,
303             -source_tag => $source,
304             -primary_tag => $match_tag,
305             -start => $smmin,
306             -end => $smmax,
307             -score => $score,
308             -strand => '-',
309             -frame => '.',
310             -tag => \%tags
311             );
312              
313 0         0 my $formatter = Bio::Tools::GFF->new(-gff_version => $format);
314 0         0 $GFF .= $feat->gff_string($formatter)."\n";
315             }
316            
317             # process + strand hsps
318 2         3 foreach my $hsp (@plus_hsps){
319 5         10 my $hspID = $self->_incrementHSP();
320 5         13 my $qstart = $hsp->query->start;
321 5         9 my $qend = $hsp->query->end;
322 5         8 my $sstart = $hsp->hit->start;
323 5         9 my $send = $hsp->hit->end;
324 5         13 my $score = $hsp->score;
325            
326 5         25 my %tags = ( 'ID' => "match_hsp$hspID",
327             'Parent' => "match_sequence$ID" );
328            
329 5 50       8 if ($format==2.5) {
330 0         0 $tags{'Target'} = "$prefix:$seqname";
331 0         0 $tags{'tstart'} = $qstart;
332 0         0 $tags{'tend'} = $qend;
333             }
334             else {
335 5         17 $tags{'Target'} = "$prefix:$seqname $qstart $qend";
336             }
337 5 50       8 if ( $self->{'_cigar'} ) {
338 5         11 $tags{'Gap'} = $hsp->cigar_string;
339             }
340              
341 5         22 my $feat = Bio::SeqFeature::Generic->new(
342             -seq_id => $refseq,
343             -source_tag => $source,
344             -primary_tag => $hsp_tag,
345             -start => $sstart,
346             -end => $send,
347             -score => $score,
348             -strand => '+',
349             -frame => '.',
350             -tag => \%tags
351             );
352              
353 5         16 my $formatter = Bio::Tools::GFF->new(-gff_version => $format);
354 5         13 $GFF .= $feat->gff_string($formatter)."\n";
355             }
356              
357 2         9 foreach my $hsp (@minus_hsps) {
358 0         0 my $hspID = $self->_incrementHSP();
359 0         0 my $qstart = $hsp->query->start;
360 0         0 my $qend = $hsp->query->end;
361 0         0 my $sstart = $hsp->hit->start;
362 0         0 my $send = $hsp->hit->end;
363 0         0 my $score = $hsp->score;
364              
365 0         0 my %tags = ( 'ID' => "match_hsp$hspID",
366             'Parent' => "match_sequence$ID" );
367              
368 0 0       0 if ($format==2.5) {
369 0         0 $tags{'Target'} = "$prefix:$seqname";
370 0         0 $tags{'tstart'} = $qend;
371 0         0 $tags{'tend'} = $qstart;
372             }
373             else {
374 0         0 $tags{'Target'} = "$prefix:$seqname $qend $qstart";
375             }
376 0 0       0 if ( $self->{'_cigar'} ) {
377 0         0 $tags{'Gap'} = $hsp->cigar_string;
378             }
379              
380 0         0 my $feat = Bio::SeqFeature::Generic->new(
381             -seq_id => $refseq,
382             -source_tag => $source,
383             -primary_tag => $hsp_tag,
384             -start => $sstart,
385             -end => $send,
386             -score => $score,
387             -strand => '-',
388             -frame => '.',
389             -tag => \%tags
390             );
391              
392 0         0 my $formatter = Bio::Tools::GFF->new(-gff_version => $format);
393 0         0 $GFF .= $feat->gff_string($formatter) ."\n";
394             }
395             }
396 1         12 return $GFF;
397             }
398              
399             sub significance_filter {
400 0     0 0 0 my ($self,$method,$code) = @_;
401 0 0       0 return unless $method;
402 0         0 $method = uc($method);
403 0 0 0     0 if( $method ne 'HSP' &&
      0        
404             $method ne 'HIT' &&
405             $method ne 'RESULT' ) {
406 0         0 $self->warn("Unknown method $method");
407 0         0 return;
408             }
409 0 0       0 if( $code ) {
410 0 0       0 $self->throw("Must provide a valid code reference") unless ref($code) =~ /CODE/;
411 0         0 $self->{$method} = $code;
412             }
413 0         0 return $self->{$method};
414             }
415              
416             =head2 start_report
417              
418             Title : start_report
419             Usage : $self->start_report()
420             Function: has no function, returns nothing
421             Returns : empty string
422             Args : none
423              
424             =cut
425              
426 0     0 1 0 sub start_report { return '' }
427              
428             =head2 end_report
429              
430             Title : end_report
431             Usage : $self->end_report()
432             Function: has no function, returns nothing
433             Returns : empty string
434             Args : none
435              
436              
437             =cut
438              
439 1     1 1 3 sub end_report { return '' }
440              
441             =head2 filter
442              
443             Title : filter
444             Usage : $writer->filter('hsp', \&hsp_filter);
445             Function: Filter out either at HSP,Hit,or Result level
446             Returns : none
447             Args : string => data type,
448             CODE reference
449             Note : GbrowseGFF.pm makes no changes to the default filter code
450              
451              
452             =cut
453              
454             1;
455              
456