This file is indexed.

/usr/share/perl5/Bio/SearchIO/sim4.pm is in libbio-perl-perl 1.6.924-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#
# BioPerl module for Bio::SearchIO::sim4
#
# Please direct questions and support issues to <bioperl-l@bioperl.org> 
#
# Cared for by Jason Stajich <jason-at-bioperl-dot-org>
#
# Copyright Jason Stajich
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::SearchIO::sim4 - parser for Sim4 alignments

=head1 SYNOPSIS

  # do not use this module directly, it is a driver for SearchIO
  use Bio::SearchIO;
  my $searchio = Bio::SearchIO->new(-file => 'results.sim4',
                                   -format => 'sim4');

  while ( my $result = $searchio->next_result ) {
      while ( my $hit = $result->next_hit ) {
	  while ( my $hsp = $hit->next_hsp ) {
              # ...
	  }
      }
  }

=head1 DESCRIPTION

This is a driver for the SearchIO system for parsing Sim4.
http://globin.cse.psu.edu/html/docs/sim4.html

Cannot parse LAV or 'exon file' formats (A=2 or A=5)

=head1 FEEDBACK

=head2 Mailing Lists

User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list.  Your participation is much appreciated.

  bioperl-l@bioperl.org                  - General discussion
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists

=head2 Support 

Please direct usage questions or support issues to the mailing list:

I<bioperl-l@bioperl.org>

rather than to the module maintainer directly. Many experienced and 
reponsive experts will be able look at the problem and quickly 
address it. Please include a thorough description of the problem 
with code and data examples if at all possible.

=head2 Reporting Bugs

Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via the
web:

  https://github.com/bioperl/bioperl-live/issues

=head1 AUTHOR - Jason Stajich

Email jason-at-bioperl-dot-org

=head1 CONTRIBUTORS

Luc Gauthier (lgauthie@hotmail.com)

=head1 APPENDIX

The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _

=cut


# Let the code begin...


package Bio::SearchIO::sim4;

use strict;
use vars qw($DEFAULTFORMAT %ALIGN_TYPES
            %MAPPING %MODEMAP $DEFAULT_WRITER_CLASS);

use POSIX;
use Bio::SearchIO::SearchResultEventBuilder;

use base qw(Bio::SearchIO);

$DEFAULTFORMAT = 'SIM4';
$DEFAULT_WRITER_CLASS = 'Bio::SearchIO::Writer::HitTableWriter';

%ALIGN_TYPES = (
    0 => 'Ruler',
    1 => 'Query', 
    2 => 'Mid', 
    3 => 'Sbjct'
);

%MODEMAP = (
    'Sim4Output' => 'result',
    'Hit'        => 'hit',
    'Hsp'        => 'hsp'
);

%MAPPING = (
    'Hsp_query-from'=>  'HSP-query_start',
    'Hsp_query-to'  =>  'HSP-query_end',
    'Hsp_qseq'      =>  'HSP-query_seq',
    'Hsp_qlength'   =>  'HSP-query_length',
    'Hsp_querygaps'  => 'HSP-query_gaps',
    'Hsp_hit-from'  =>  'HSP-hit_start',
    'Hsp_hit-to'    =>  'HSP-hit_end',
    'Hsp_hseq'      =>  'HSP-hit_seq',
    'Hsp_hlength'   =>  'HSP-hit_length',
    'Hsp_hitgaps'    => 'HSP-hit_gaps',
    'Hsp_midline'   =>  'HSP-homology_seq',
    'Hsp_score'     =>  'HSP-score',
    'Hsp_align-len' =>  'HSP-hsp_length',
    'Hsp_identity'  =>  'HSP-identical',

    'Hit_id'        => 'HIT-name',
    'Hit_desc'      => 'HIT-description',
    'Hit_len'       => 'HIT-length',

    'Sim4Output_program'   => 'RESULT-algorithm_name',
    'Sim4Output_query-def' => 'RESULT-query_name',
    'Sim4Output_query-desc'=> 'RESULT-query_description',
    'Sim4Output_query-len' => 'RESULT-query_length',
);



=head2 new

 Title   : new
 Usage   : my $obj = Bio::SearchIO::sim4->new();
 Function: Builds a new Bio::SearchIO::sim4 object
 Returns : an instance of Bio::SearchIO::sim4
 Args    :


=cut


=head2 next_result

 Title   : next_result
 Usage   : my $result = $searchio->next_result;
 Function: Returns the next Result from a search
 Returns : Bio::Search::Result::ResultI object
 Args    : none

=cut

sub next_result {
    my ($self) = @_;
    local $/ = "\n";
    local $_;

    # Declare/adjust needed variables
    $self->{'_last_data'} = '';
    my ($seentop, $qfull, @hsps, %alignment, $format);
    my $hit_direction = 1;

    # Start document and main element
    $self->start_document();
    $self->start_element({'Name' => 'Sim4Output'});
    my $lastquery = '';
    # Read output report until EOF
    while( defined($_ = $self->_readline) ) {       
        # Skip empty lines, chomp filled ones
	next if( /^\s+$/); chomp;

        # Make sure sim4 output format is not 2 or 5
        if (!$seentop) {
	    if ( /^\#:lav/ ) { $format = 2; }
            elsif ( /^<|>/ ) { $format = 5; }
            $self->throw("Bio::SearchIO::sim4 module cannot parse 'type $format' outputs.") if $format;
	}

        # This line indicates the start of a new hit
	if( /^seq1\s*=\s*(\S+),\s+(\d+)/ ) {
	    my ($nm,$desc) = ($1,$2);
            # First hit? Adjust some parameters if so
	    if ( ! $seentop ) {
	        $self->element( {'Name' => 'Sim4Output_query-def', 
				 'Data' => $nm} );
	        $self->element( {'Name' => 'Sim4Output_query-len', 
				 'Data' => $desc} );
                $seentop = 1;
	    } elsif( $nm ne $lastquery ) {
		$self->_pushback($_);
		last;
	    }
	    $lastquery = $nm;
            # A previous HSP may need to be ended
            $self->end_element({'Name' => 'Hsp'}) if ( $self->in_element('hsp') );
            # A previous hit exists? End it and reset needed variables
            if ( $self->in_element('hit') ) {
	        foreach (@hsps) {
                    $self->start_element({'Name' => 'Hsp'});
                    while (my ($name, $data) = each %$_) {
                        $self->{'_currentHSP'}{$name} = $data;
		    }
		    $self->end_element({'Name' => 'Hsp'});
                    $self->{'_currentHSP'} = {};
	        }
                $format = 0 if @hsps;
                @hsps = ();
                %alignment = ();
                $qfull = 0;
                $hit_direction = 1;
                $self->end_element({'Name' => 'Hit'});
	    }

        # This line describes the current hit... so let's start it
	} elsif( /^seq2\s*=\s*(\S+)\s+\(>?(\S+)\s*\),\s*(\d+)/ ) {
            $self->start_element({'Name' => 'Hit'});
	    $self->element( {'Name' => 'Hit_id', 'Data' => $2} );
	    $self->element( {'Name' => 'Hit_desc', 'Data' => $1} );
	    $self->element( {'Name' => 'Hit_len', 'Data' => $3} );

        # This line may give additional details about query or subject
	} elsif( /^>(\S+)\s*(.*)?/ ) {
            # Previous line was query details... this time subject details
	    if( $qfull )  {
                $format = 4 if !$format;
		$self->element({'Name' => 'Hit_desc', 'Data' => $2});
            # First line of this type is always query details for a given hit
	    } else { 
		$self->element({'Name' => 'Sim4Output_query-desc', 'Data' => $2});
		$qfull = 1;
	    }

        # This line indicates that subject is on reverse strand
	} elsif( /^\(complement\)/ ) {
	    $hit_direction = -1;

        # This line describes the current HSP... so add it to @hsps array
	} elsif( /^\(?(\d+)\-(\d+)\)?\s+\(?(\d+)\-(\d+)\)?\s+(\d+)/ ) {
		my ($qs,$qe,$hs,$he,$pid) = ($1,$2,$3,$4,$5);
                push @hsps, {
                    'Hsp_query-from' => $qs,
                    'Hsp_query-to' => $qe,
                    'Hsp_hit-from' => $hit_direction >= 0 ? $hs : $he,
                    'Hsp_hit-to' => $hit_direction >= 0 ? $he : $hs,
                    'Hsp_identity' => 0, #can't determine correctly from raw pct
                    'Hsp_qlength' => abs($qe - $qs) + 1,
                    'Hsp_hlength' => abs($he - $hs) + 1,
                    'Hsp_align-len' => abs($qe - $qs) + 1,
	        };

        # This line indicates the start of an alignment block
        } elsif( /^\s+(\d+)\s/ ) {
            # Store the current alignment block in a hash
	    for( my $i = 0; defined($_) && $i < 4; $i++ ) {
                my ($start, $string) = /^\s+(\d*)\s(.*)/;
                $alignment{$ALIGN_TYPES{$i}} = { start => $start, string => $i != 2
                    ? $string
                    : (' ' x (length($alignment{$ALIGN_TYPES{$i-1}}{string}) - length($string))) . $string
                };
                $_ = $self->_readline();
	    }

            # 'Ruler' line indicates the start of a new HSP
            if ($alignment{Ruler}{start} == 0) {
                $format = @hsps ? 3 : 1 if !$format;
                # A previous HSP may need to be ended
                $self->end_element({'Name' => 'Hsp'}) if ( $self->in_element('hsp') );
                # Start the new HSP and fill the '_currentHSP' property with available details
     	        $self->start_element({'Name' => 'Hsp'});
                $self->{'_currentHSP'} = @hsps ? shift @hsps : {
                    'Hsp_query-from' => $alignment{Query}{start},
                    'Hsp_hit-from' => $alignment{Sbjct}{start},
     	        }
	    }

            # Midline indicates a boundary between two HSPs
	    if ( $alignment{Mid}{string} =~ /<|>/g ) {
                my ($hsp_start, $hsp_end);
                # Are we currently in an open HSP?
    	        if ( $self->in_element('hsp') ) {
                    # Find end pos, adjust 'gaps', 'seq' and 'midline' properties... then close HSP
                    $hsp_end = (pos $alignment{Mid}{string}) - 1;
                    $self->{'_currentHSP'}{'Hsp_querygaps'} +=
                        ($self->{'_currentHSP'}{'Hsp_qseq'} .= substr($alignment{Query}{string}, 0, $hsp_end)) =~ s/ /-/g;
                    $self->{'_currentHSP'}{'Hsp_hitgaps'} +=
                        ($self->{'_currentHSP'}{'Hsp_hseq'} .= substr($alignment{Sbjct}{string}, 0, $hsp_end)) =~ s/ /-/g;
                    ($self->{'_currentHSP'}{'Hsp_midline'} .= substr($alignment{Mid}{string}, 0, $hsp_end)) =~ s/-/ /g;
                    $self->end_element({'Name' => 'Hsp'});

                    # Does a new HSP start in the current alignment block?
                    if ( $alignment{Mid}{string} =~ /\|/g ) {
                        # Find start pos, start new HSP and fill it with available details
                        $hsp_start = (pos $alignment{Mid}{string}) - 1;
                        $self->start_element({'Name' => 'Hsp'});
                        $self->{'_currentHSP'} = @hsps ? shift @hsps : {};
                        $self->{'_currentHSP'}{'Hsp_querygaps'} +=
                            ($self->{'_currentHSP'}{'Hsp_qseq'} = substr($alignment{Query}{string}, $hsp_start)) =~ s/ /-/g;
                        $self->{'_currentHSP'}{'Hsp_hitgaps'} +=
                            ($self->{'_currentHSP'}{'Hsp_hseq'} = substr($alignment{Sbjct}{string}, $hsp_start)) =~ s/ /-/g;
                        ($self->{'_currentHSP'}{'Hsp_midline'} = substr($alignment{Mid}{string}, $hsp_start)) =~ s/-/ /g;
		    }
		}
                # No HSP is currently open...
                else {
                    # Find start pos, start new HSP and fill it with available
                    # details then skip to next alignment block
		    $hsp_start = index($alignment{Mid}{string}, '|');
	            $self->start_element({'Name' => 'Hsp'});
                    $self->{'_currentHSP'} = @hsps ? shift @hsps : {
                        'Hsp_query-from' => $alignment{Query}{start},
    	            };
                    $self->{'_currentHSP'}{'Hsp_querygaps'} +=
                        ($self->{'_currentHSP'}{'Hsp_qseq'} = substr($alignment{Query}{string}, $hsp_start)) =~ s/ /-/g;
                    $self->{'_currentHSP'}{'Hsp_hitgaps'} +=
                        ($self->{'_currentHSP'}{'Hsp_hseq'} = substr($alignment{Sbjct}{string}, $hsp_start)) =~ s/ /-/g;
                    ($self->{'_currentHSP'}{'Hsp_midline'} = substr($alignment{Mid}{string}, $hsp_start)) =~ s/-/ /g;
                    next;
		}
	    }
            # Current alignment block does not contain HSPs boundary
            else {
                # Start a new HSP if none is currently open
	        # (Happens if last boundary finished at the very end of previous block)
	        if ( !$self->in_element('hsp') ) {
     	            $self->start_element({'Name' => 'Hsp'});
                    $self->{'_currentHSP'} = @hsps ? shift @hsps : {
                        'Hsp_query-from' => $alignment{Query}{start},
                        'Hsp_hit-from' => $alignment{Sbjct}{start},
     	            }
		}
                # Adjust details of the current HSP
                $self->{'_currentHSP'}{'Hsp_query-from'} ||= 
		    $alignment{Query}{start} - 
		    length($self->{'_currentHSP'}{'Hsp_qseq'} || '');
                $self->{'_currentHSP'}{'Hsp_hit-from'} ||= 
		    $alignment{Sbjct}{start} - 
		    length($self->{'_currentHSP'}{'Hsp_hseq'} || '');
                $self->{'_currentHSP'}{'Hsp_querygaps'} +=
                    ($self->{'_currentHSP'}{'Hsp_qseq'} .= 
		     $alignment{Query}{string}) =~ s/ /-/g;
                $self->{'_currentHSP'}{'Hsp_hitgaps'} +=
                    ($self->{'_currentHSP'}{'Hsp_hseq'} .= 
		     $alignment{Sbjct}{string}) =~ s/ /-/g;
                ($self->{'_currentHSP'}{'Hsp_midline'} .= 
		 $alignment{Mid}{string}) =~ s/-/ /g;
	    }
	}
    }

    # We are done reading the sim4 report, end everything and return
    if( $seentop ) {
        # end HSP if needed
        $self->end_element({'Name' => 'Hsp'}) if ( $self->in_element('hsp') );
        # end Hit if needed
        if ( $self->in_element('hit') ) {
            foreach (@hsps) {
                $self->start_element({'Name' => 'Hsp'});
                while (my ($name, $data) = each %$_) {
                    $self->{'_currentHSP'}{$name} = $data;
    	        }
    	        $self->end_element({'Name' => 'Hsp'});
            }
            $self->end_element({'Name' => 'Hit'});
	}
        # adjust result's algorithm name, end output and return
        $self->element({'Name' => 'Sim4Output_program',
                        'Data' => $DEFAULTFORMAT . ' (A=' . (defined $format ? $format : '?') . ')'});
	$self->end_element({'Name' => 'Sim4Output'});
	return $self->end_document();
    } 
    return;
}

=head2 start_element

 Title   : start_element
 Usage   : $eventgenerator->start_element
 Function: Handles a start element event
 Returns : none
 Args    : hashref with at least 2 keys 'Data' and 'Name'


=cut

sub start_element{
   my ($self,$data) = @_;
   # we currently don't care about attributes
   my $nm = $data->{'Name'};
   my $type = $MODEMAP{$nm};

   if( $type ) {
       if( $self->_will_handle($type) ) {
	   my $func = sprintf("start_%s",lc $type);
	   $self->_eventHandler->$func($data->{'Attributes'});
       }
       unshift @{$self->{'_elements'}}, $type;

       if($type eq 'result') {
	   $self->{'_values'} = {};
	   $self->{'_result'}= undef;
       }
   }

}

=head2 end_element

 Title   : start_element
 Usage   : $eventgenerator->end_element
 Function: Handles an end element event
 Returns : none
 Args    : hashref with at least 2 keys 'Data' and 'Name'


=cut

sub end_element {
    my ($self,$data) = @_;
    my $nm = $data->{'Name'};
    my $type = $MODEMAP{$nm};
    my $rc;
    
    if( $nm eq 'Hsp' ) {
        $self->{'_currentHSP'}{'Hsp_midline'} ||= '';
	$self->{'_currentHSP'}{'Hsp_query-to'} ||=
            $self->{'_currentHSP'}{'Hsp_query-from'} + length($self->{'_currentHSP'}{'Hsp_qseq'}) - 1 - $self->{'_currentHSP'}{'Hsp_querygaps'};
        $self->{'_currentHSP'}{'Hsp_hit-to'} ||=
            $self->{'_currentHSP'}{'Hsp_hit-from'} + length($self->{'_currentHSP'}{'Hsp_hseq'}) - 1 - $self->{'_currentHSP'}{'Hsp_hitgaps'};
        $self->{'_currentHSP'}{'Hsp_identity'} ||= 
	    ($self->{'_currentHSP'}{'Hsp_midline'} =~ tr/\|//);
        $self->{'_currentHSP'}{'Hsp_qlength'} ||= abs($self->{'_currentHSP'}{'Hsp_query-to'} - $self->{'_currentHSP'}{'Hsp_query-from'}) + 1;
        $self->{'_currentHSP'}{'Hsp_hlength'} ||= abs($self->{'_currentHSP'}{'Hsp_hit-to'} - $self->{'_currentHSP'}{'Hsp_hit-from'}) + 1;
        $self->{'_currentHSP'}{'Hsp_align-len'} ||= abs($self->{'_currentHSP'}{'Hsp_query-to'} - $self->{'_currentHSP'}{'Hsp_query-from'}) + 1;
        $self->{'_currentHSP'}{'Hsp_score'} ||= int(100 * ($self->{'_currentHSP'}{'Hsp_identity'} / $self->{'_currentHSP'}{'Hsp_align-len'}));
        foreach (keys %{$self->{'_currentHSP'}}) {
            $self->element({'Name' => $_, 'Data' => delete ${$self->{'_currentHSP'}}{$_}});
	}
    }

    if( $type = $MODEMAP{$nm} ) {
	if( $self->_will_handle($type) ) {
	    my $func = sprintf("end_%s",lc $type);
	    $rc = $self->_eventHandler->$func($self->{'_reporttype'},
					      $self->{'_values'});
	}
	shift @{$self->{'_elements'}};

    } elsif( $MAPPING{$nm} ) {

	if ( ref($MAPPING{$nm}) =~ /hash/i ) {
	    my $key = (keys %{$MAPPING{$nm}})[0];
	    $self->{'_values'}->{$key}->{$MAPPING{$nm}->{$key}} = $self->{'_last_data'};
	} else {
	    $self->{'_values'}->{$MAPPING{$nm}} = $self->{'_last_data'};
	}
    } else {
	$self->debug( "unknown nm $nm, ignoring\n");
    }
    $self->{'_last_data'} = ''; # remove read data if we are at
				# end of an element
    $self->{'_result'} = $rc if( defined $type && $type eq 'result' );
    return $rc;
}

=head2 element

 Title   : element
 Usage   : $eventhandler->element({'Name' => $name, 'Data' => $str});
 Function: Convience method that calls start_element, characters, end_element
 Returns : none
 Args    : Hash ref with the keys 'Name' and 'Data'


=cut

sub element{
   my ($self,$data) = @_;
   $self->start_element($data);
   $self->characters($data);
   $self->end_element($data);
}

=head2 characters

 Title   : characters
 Usage   : $eventgenerator->characters($str)
 Function: Send a character events
 Returns : none
 Args    : string


=cut

sub characters{
   my ($self,$data) = @_;
   return unless ( defined $data->{'Data'} && $data->{'Data'} !~ /^\s+$/ );
   
   if( $self->in_element('hsp') && 
       $data->{'Name'} =~ /Hsp\_(qseq|hseq|midline)/ ) {
       $self->{'_last_hspdata'}->{$data->{'Name'}} .= $data->{'Data'};
   }  

   $self->{'_last_data'} = $data->{'Data'};
}

=head2 within_element

 Title   : within_element
 Usage   : if( $eventgenerator->within_element($element) ) {}
 Function: Test if we are within a particular element
           This is different than 'in' because within can be tested
           for a whole block.
 Returns : boolean
 Args    : string element name


=cut

sub within_element{
   my ($self,$name) = @_;
   return 0 if ( ! defined $name &&
		 ! defined  $self->{'_elements'} ||
		 scalar @{$self->{'_elements'}} == 0) ;
   foreach (  @{$self->{'_elements'}} ) {
       if( $_ eq $name  ) {
	   return 1;
       }
   }
   return 0;
}


=head2 in_element

 Title   : in_element
 Usage   : if( $eventgenerator->in_element($element) ) {}
 Function: Test if we are in a particular element
           This is different than 'in' because within can be tested
           for a whole block.
 Returns : boolean
 Args    : string element name


=cut

sub in_element{
   my ($self,$name) = @_;
   return 0 if ! defined $self->{'_elements'}->[0];
   return ( $self->{'_elements'}->[0] eq $name)
}

=head2 start_document

 Title   : start_document
 Usage   : $eventgenerator->start_document
 Function: Handle a start document event
 Returns : none
 Args    : none


=cut

sub start_document{
    my ($self) = @_;
    $self->{'_lasttype'} = '';
    $self->{'_values'} = {};
    $self->{'_result'}= undef;
    $self->{'_elements'} = [];
    $self->{'_reporttype'} = $DEFAULTFORMAT;
}


=head2 end_document

 Title   : end_document
 Usage   : $eventgenerator->end_document
 Function: Handles an end document event
 Returns : Bio::Search::Result::ResultI object
 Args    : none


=cut

sub end_document{
   my ($self,@args) = @_;
   return $self->{'_result'};
}


sub write_result {
   my ($self, $blast, @args) = @_;

   if( not defined($self->writer) ) {
       $self->warn("Writer not defined. Using a $DEFAULT_WRITER_CLASS");
       $self->writer( $DEFAULT_WRITER_CLASS->new() );
   }
   $self->SUPER::write_result( $blast, @args );
}

sub result_count {
    return 1; # can a sim4 report contain more than one result?
}

sub report_count { shift->result_count }

=head2 _will_handle

 Title   : _will_handle
 Usage   : Private method. For internal use only.
              if( $self->_will_handle($type) ) { ... }
 Function: Provides an optimized way to check whether or not an element of a 
           given type is to be handled.
 Returns : Reference to EventHandler object if the element type is to be handled.
           undef if the element type is not to be handled.
 Args    : string containing type of element.

Optimizations:

  1. Using the cached pointer to the EventHandler to minimize repeated lookups.
  2. Caching the will_handle status for each type that is encountered
     so that it only need be checked by calling handler->will_handle($type) once.

This does not lead to a major savings by itself (only 5-10%).
In combination with other optimizations, or for large parse jobs, the
savings good be significant.

To test against the unoptimized version, remove the parentheses from
around the third term in the ternary " ? : " operator and add two
calls to $self-E<gt>_eventHandler().

=cut

sub _will_handle {
    my ($self,$type) = @_;
    my $handler = $self->{'_handler_cache'} ||= $self->_eventHandler;

    my $will_handle = defined($self->{'_will_handle_cache'}->{$type})
                             ? $self->{'_will_handle_cache'}->{$type}
                             : ($self->{'_will_handle_cache'}->{$type} =
                               $handler->will_handle($type));

    return $will_handle ? $handler : undef;
}

1;