3
# BioPerl module for Bio::SearchIO::sim4
5
# Cared for by Jason Stajich <jason-at-bioperl-dot-org>
7
# Copyright Jason Stajich
9
# You may distribute this module under the same terms as perl itself
11
# POD documentation - main docs before the code
15
Bio::SearchIO::sim4 - parser for Sim4 alignments
19
# do not use this module directly, it is a driver for SearchIO
21
my $searchio = new Bio::SearchIO(-file => 'results.sim4',
24
while ( my $result = $searchio->next_result ) {
25
while ( my $hit = $result->next_hit ) {
26
while ( my $hsp = $hit->next_hsp ) {
34
This is a driver for the SearchIO system for parsing Sim4.
35
http://globin.cse.psu.edu/html/docs/sim4.html
41
User feedback is an integral part of the evolution of this and other
42
Bioperl modules. Send your comments and suggestions preferably to
43
the Bioperl mailing list. Your participation is much appreciated.
45
bioperl-l@bioperl.org - General discussion
46
http://bioperl.org/MailList.shtml - About the mailing lists
50
Report bugs to the Bioperl bug tracking system to help us keep track
51
of the bugs and their resolution. Bug reports can be submitted via
54
bioperl-bugs@bioperl.org
55
http://bioperl.org/bioperl-bugs/
57
=head1 AUTHOR - Jason Stajich
59
Email jason-at-bioperl-dot-org
63
Luc Gauthier (lgauthie@hotmail.com)
67
The rest of the documentation details each of the object methods.
68
Internal methods are usually preceded with a _
73
# Let the code begin...
76
package Bio::SearchIO::sim4;
79
use vars qw(@ISA $DEFAULTFORMAT %ALIGN_TYPES
80
%MAPPING %MODEMAP $DEFAULT_WRITER_CLASS);
84
use Bio::SearchIO::SearchResultEventBuilder;
86
@ISA = qw(Bio::SearchIO );
88
$DEFAULTFORMAT = 'SIM4';
89
$DEFAULT_WRITER_CLASS = 'Bio::Search::Writer::HitTableWriter';
99
'Sim4Output' => 'result',
105
'Hsp_query-from'=> 'HSP-query_start',
106
'Hsp_query-to' => 'HSP-query_end',
107
'Hsp_qseq' => 'HSP-query_seq',
108
'Hsp_qlength' => 'HSP-query_length',
109
'Hsp_querygaps' => 'HSP-query_gaps',
110
'Hsp_hit-from' => 'HSP-hit_start',
111
'Hsp_hit-to' => 'HSP-hit_end',
112
'Hsp_hseq' => 'HSP-hit_seq',
113
'Hsp_hlength' => 'HSP-hit_length',
114
'Hsp_hitgaps' => 'HSP-hit_gaps',
115
'Hsp_midline' => 'HSP-homology_seq',
116
'Hsp_score' => 'HSP-score',
117
'Hsp_align-len' => 'HSP-hsp_length',
118
'Hsp_identity' => 'HSP-identical',
120
'Hit_id' => 'HIT-name',
121
'Hit_desc' => 'HIT-description',
122
'Hit_len' => 'HIT-length',
124
'Sim4Output_program' => 'RESULT-algorithm_name',
125
'Sim4Output_query-def' => 'RESULT-query_name',
126
'Sim4Output_query-desc'=> 'RESULT-query_description',
127
'Sim4Output_query-len' => 'RESULT-query_length',
135
Usage : my $obj = new Bio::SearchIO::sim4();
136
Function: Builds a new Bio::SearchIO::sim4 object
137
Returns : an instance of Bio::SearchIO::sim4
147
Usage : my $result = $searchio->next_result;
148
Function: Returns the next Result from a search
149
Returns : Bio::Search::Result::ResultI object
157
# Declare/adjust needed variables
158
$self->{'_last_data'} = '';
159
my ($seentop, $qfull, @hsps, %alignment, $format);
160
my $hit_direction = 1;
162
# Start document and main element
163
$self->start_document();
164
$self->start_element({'Name' => 'Sim4Output'});
166
# Read output report until EOF
167
while( defined($_ = $self->_readline) ) {
168
# Skip empty lines, chomp filled ones
169
next if( /^\s+$/); chomp;
171
# Make sure sim4 output format is not 2 or 5
173
if ( /^#:lav/ ) { $format = 2; }
174
elsif ( /^<|>/ ) { $format = 5; }
175
$self->throw("Bio::SearchIO::sim4 module cannot parse 'type $format' outputs.") if $format;
178
# This line indicates the start of a new hit
179
if( /^seq1\s*=\s*(\S+),\s+(\d+)/ ) {
180
# First hit? Adjust some parameters if so
182
$self->element( {'Name' => 'Sim4Output_query-def',
184
$self->element( {'Name' => 'Sim4Output_query-len',
188
# A previous HSP may need to be ended
189
$self->end_element({'Name' => 'Hsp'}) if ( $self->in_element('hsp') );
190
# A previous hit exists? End it and reset needed variables
191
if ( $self->in_element('hit') ) {
193
$self->start_element({'Name' => 'Hsp'});
194
while (my ($name, $data) = each %$_) {
195
$self->{'_currentHSP'}{$name} = $data;
197
$self->end_element({'Name' => 'Hsp'});
198
$self->{'_currentHSP'} = {};
200
$format = 0 if @hsps;
205
$self->end_element({'Name' => 'Hit'});
208
# This line describes the current hit... so let's start it
209
} elsif( /^seq2\s*=\s*(\S+)\s+\(>?(\S+)\s*\),\s*(\d+)/ ) {
210
$self->start_element({'Name' => 'Hit'});
211
$self->element( {'Name' => 'Hit_id', 'Data' => $2} );
212
$self->element( {'Name' => 'Hit_desc', 'Data' => $1} );
213
$self->element( {'Name' => 'Hit_len', 'Data' => $3} );
215
# This line may give additional details about query or subject
216
} elsif( /^>(\S+)\s*(.*)?/ ) {
217
# Previous line was query details... this time subject details
219
$format = 4 if !$format;
220
$self->element({'Name' => 'Hit_desc', 'Data' => $2});
221
# First line of this type is always query details for a given hit
223
$self->element({'Name' => 'Sim4Output_query-desc', 'Data' => $2});
227
# This line indicates that subject is on reverse strand
228
} elsif( /^\(complement\)/ ) {
231
# This line describes the current HSP... so add it to @hsps array
232
} elsif( /^\(?(\d+)\-(\d+)\)?\s+\(?(\d+)\-(\d+)\)?\s+(\d+)/ ) {
233
my ($qs,$qe,$hs,$he,$pid) = ($1,$2,$3,$4,$5);
235
'Hsp_query-from' => $qs,
236
'Hsp_query-to' => $qe,
237
'Hsp_hit-from' => $hit_direction >= 0 ? $hs : $he,
238
'Hsp_hit-to' => $hit_direction >= 0 ? $he : $hs,
239
'Hsp_identity' => 0, #can't determine correctly from raw pct
240
'Hsp_qlength' => abs($qe - $qs) + 1,
241
'Hsp_hlength' => abs($he - $hs) + 1,
242
'Hsp_align-len' => abs($qe - $qs) + 1,
245
# This line indicates the start of an alignment block
246
} elsif( /^\s+(\d+)\s/ ) {
247
# Store the current alignment block in a hash
248
for( my $i = 0; defined($_) && $i < 4; $i++ ) {
249
my ($start, $string) = /^\s+(\d*)\s(.*)/;
250
$alignment{$ALIGN_TYPES{$i}} = { start => $start, string => $i != 2
252
: (' ' x (length($alignment{$ALIGN_TYPES{$i-1}}{string}) - length($string))) . $string
254
$_ = $self->_readline();
257
# 'Ruler' line indicates the start of a new HSP
258
if ($alignment{Ruler}{start} == 0) {
259
$format = @hsps ? 3 : 1 if !$format;
260
# A previous HSP may need to be ended
261
$self->end_element({'Name' => 'Hsp'}) if ( $self->in_element('hsp') );
262
# Start the new HSP and fill the '_currentHSP' property with available details
263
$self->start_element({'Name' => 'Hsp'});
264
$self->{'_currentHSP'} = @hsps ? shift @hsps : {
265
'Hsp_query-from' => $alignment{Query}{start},
266
'Hsp_hit-from' => $alignment{Sbjct}{start},
270
# Midline indicates a boundary between two HSPs
271
if ( $alignment{Mid}{string} =~ /<|>/g ) {
272
my ($hsp_start, $hsp_end);
273
# Are we currently in an open HSP?
274
if ( $self->in_element('hsp') ) {
275
# Find end pos, adjust 'gaps', 'seq' and 'midline' properties... then close HSP
276
$hsp_end = (pos $alignment{Mid}{string}) - 1;
277
$self->{'_currentHSP'}{'Hsp_querygaps'} +=
278
($self->{'_currentHSP'}{'Hsp_qseq'} .= substr($alignment{Query}{string}, 0, $hsp_end)) =~ s/ /-/g;
279
$self->{'_currentHSP'}{'Hsp_hitgaps'} +=
280
($self->{'_currentHSP'}{'Hsp_hseq'} .= substr($alignment{Sbjct}{string}, 0, $hsp_end)) =~ s/ /-/g;
281
($self->{'_currentHSP'}{'Hsp_midline'} .= substr($alignment{Mid}{string}, 0, $hsp_end)) =~ s/-/ /g;
282
$self->end_element({'Name' => 'Hsp'});
284
# Does a new HSP start in the current alignment block?
285
if ( $alignment{Mid}{string} =~ /\|/g ) {
286
# Find start pos, start new HSP and fill it with available details
287
$hsp_start = (pos $alignment{Mid}{string}) - 1;
288
$self->start_element({'Name' => 'Hsp'});
289
$self->{'_currentHSP'} = @hsps ? shift @hsps : {};
290
$self->{'_currentHSP'}{'Hsp_querygaps'} +=
291
($self->{'_currentHSP'}{'Hsp_qseq'} = substr($alignment{Query}{string}, $hsp_start)) =~ s/ /-/g;
292
$self->{'_currentHSP'}{'Hsp_hitgaps'} +=
293
($self->{'_currentHSP'}{'Hsp_hseq'} = substr($alignment{Sbjct}{string}, $hsp_start)) =~ s/ /-/g;
294
($self->{'_currentHSP'}{'Hsp_midline'} = substr($alignment{Mid}{string}, $hsp_start)) =~ s/-/ /g;
297
# No HSP is currently open...
299
# Find start pos, start new HSP and fill it with available
300
# details then skip to next alignment block
301
$hsp_start = index($alignment{Mid}{string}, '|');
302
$self->start_element({'Name' => 'Hsp'});
303
$self->{'_currentHSP'} = @hsps ? shift @hsps : {
304
'Hsp_query-from' => $alignment{Query}{start},
306
$self->{'_currentHSP'}{'Hsp_querygaps'} +=
307
($self->{'_currentHSP'}{'Hsp_qseq'} = substr($alignment{Query}{string}, $hsp_start)) =~ s/ /-/g;
308
$self->{'_currentHSP'}{'Hsp_hitgaps'} +=
309
($self->{'_currentHSP'}{'Hsp_hseq'} = substr($alignment{Sbjct}{string}, $hsp_start)) =~ s/ /-/g;
310
($self->{'_currentHSP'}{'Hsp_midline'} = substr($alignment{Mid}{string}, $hsp_start)) =~ s/-/ /g;
314
# Current alignment block does not contain HSPs boundary
315
# We only need to adjust details of the current HSP
317
$self->{'_currentHSP'}{'Hsp_query-from'} ||=
318
$alignment{Query}{start} -
319
length($self->{'_currentHSP'}{'Hsp_qseq'} || '');
320
$self->{'_currentHSP'}{'Hsp_hit-from'} ||=
321
$alignment{Sbjct}{start} -
322
length($self->{'_currentHSP'}{'Hsp_hseq'} || '');
323
$self->{'_currentHSP'}{'Hsp_querygaps'} +=
324
($self->{'_currentHSP'}{'Hsp_qseq'} .=
325
$alignment{Query}{string}) =~ s/ /-/g;
326
$self->{'_currentHSP'}{'Hsp_hitgaps'} +=
327
($self->{'_currentHSP'}{'Hsp_hseq'} .=
328
$alignment{Sbjct}{string}) =~ s/ /-/g;
329
($self->{'_currentHSP'}{'Hsp_midline'} .=
330
$alignment{Mid}{string}) =~ s/-/ /g;
335
# We are done reading the sim4 report, end everything and return
338
$self->end_element({'Name' => 'Hsp'}) if ( $self->in_element('hsp') );
340
if ( $self->in_element('hit') ) {
342
$self->start_element({'Name' => 'Hsp'});
343
while (my ($name, $data) = each %$_) {
344
$self->{'_currentHSP'}{$name} = $data;
346
$self->end_element({'Name' => 'Hsp'});
348
$self->end_element({'Name' => 'Hit'});
350
# adjust result's algorithm name, end output and return
351
$self->element({'Name' => 'Sim4Output_program',
352
'Data' => $DEFAULTFORMAT . ' (A=' . (defined $format ? $format : '?') . ')'});
353
$self->end_element({'Name' => 'Sim4Output'});
354
return $self->end_document();
361
Title : start_element
362
Usage : $eventgenerator->start_element
363
Function: Handles a start element event
365
Args : hashref with at least 2 keys 'Data' and 'Name'
371
my ($self,$data) = @_;
372
# we currently don't care about attributes
373
my $nm = $data->{'Name'};
374
my $type = $MODEMAP{$nm};
377
if( $self->_will_handle($type) ) {
378
my $func = sprintf("start_%s",lc $type);
379
$self->_eventHandler->$func($data->{'Attributes'});
381
unshift @{$self->{'_elements'}}, $type;
383
if($type eq 'result') {
384
$self->{'_values'} = {};
385
$self->{'_result'}= undef;
393
Title : start_element
394
Usage : $eventgenerator->end_element
395
Function: Handles an end element event
397
Args : hashref with at least 2 keys 'Data' and 'Name'
403
my ($self,$data) = @_;
404
my $nm = $data->{'Name'};
405
my $type = $MODEMAP{$nm};
409
$self->{'_currentHSP'}{'Hsp_midline'} ||= '';
410
$self->{'_currentHSP'}{'Hsp_query-to'} ||=
411
$self->{'_currentHSP'}{'Hsp_query-from'} + length($self->{'_currentHSP'}{'Hsp_qseq'}) - 1 - $self->{'_currentHSP'}{'Hsp_querygaps'};
412
$self->{'_currentHSP'}{'Hsp_hit-to'} ||=
413
$self->{'_currentHSP'}{'Hsp_hit-from'} + length($self->{'_currentHSP'}{'Hsp_hseq'}) - 1 - $self->{'_currentHSP'}{'Hsp_hitgaps'};
414
$self->{'_currentHSP'}{'Hsp_identity'} ||=
415
($self->{'_currentHSP'}{'Hsp_midline'} =~ tr/\|//);
416
$self->{'_currentHSP'}{'Hsp_qlength'} ||= abs($self->{'_currentHSP'}{'Hsp_query-to'} - $self->{'_currentHSP'}{'Hsp_query-from'}) + 1;
417
$self->{'_currentHSP'}{'Hsp_hlength'} ||= abs($self->{'_currentHSP'}{'Hsp_hit-to'} - $self->{'_currentHSP'}{'Hsp_hit-from'}) + 1;
418
$self->{'_currentHSP'}{'Hsp_align-len'} ||= abs($self->{'_currentHSP'}{'Hsp_query-to'} - $self->{'_currentHSP'}{'Hsp_query-from'}) + 1;
419
$self->{'_currentHSP'}{'Hsp_score'} ||= int(100 * ($self->{'_currentHSP'}{'Hsp_identity'} / $self->{'_currentHSP'}{'Hsp_align-len'}));
420
foreach (keys %{$self->{'_currentHSP'}}) {
421
$self->element({'Name' => $_, 'Data' => delete ${$self->{'_currentHSP'}}{$_}});
425
if( $type = $MODEMAP{$nm} ) {
426
if( $self->_will_handle($type) ) {
427
my $func = sprintf("end_%s",lc $type);
428
$rc = $self->_eventHandler->$func($self->{'_reporttype'},
431
shift @{$self->{'_elements'}};
433
} elsif( $MAPPING{$nm} ) {
435
if ( ref($MAPPING{$nm}) =~ /hash/i ) {
436
my $key = (keys %{$MAPPING{$nm}})[0];
437
$self->{'_values'}->{$key}->{$MAPPING{$nm}->{$key}} = $self->{'_last_data'};
439
$self->{'_values'}->{$MAPPING{$nm}} = $self->{'_last_data'};
442
$self->debug( "unknown nm $nm, ignoring\n");
444
$self->{'_last_data'} = ''; # remove read data if we are at
446
$self->{'_result'} = $rc if( defined $type && $type eq 'result' );
453
Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
454
Function: Convience method that calls start_element, characters, end_element
456
Args : Hash ref with the keys 'Name' and 'Data'
462
my ($self,$data) = @_;
463
$self->start_element($data);
464
$self->characters($data);
465
$self->end_element($data);
471
Usage : $eventgenerator->characters($str)
472
Function: Send a character events
480
my ($self,$data) = @_;
481
return unless ( defined $data->{'Data'} && $data->{'Data'} !~ /^\s+$/ );
483
if( $self->in_element('hsp') &&
484
$data->{'Name'} =~ /Hsp\_(qseq|hseq|midline)/ ) {
485
$self->{'_last_hspdata'}->{$data->{'Name'}} .= $data->{'Data'};
488
$self->{'_last_data'} = $data->{'Data'};
491
=head2 within_element
493
Title : within_element
494
Usage : if( $eventgenerator->within_element($element) ) {}
495
Function: Test if we are within a particular element
496
This is different than 'in' because within can be tested
499
Args : string element name
505
my ($self,$name) = @_;
506
return 0 if ( ! defined $name &&
507
! defined $self->{'_elements'} ||
508
scalar @{$self->{'_elements'}} == 0) ;
509
foreach ( @{$self->{'_elements'}} ) {
521
Usage : if( $eventgenerator->in_element($element) ) {}
522
Function: Test if we are in a particular element
523
This is different than 'in' because within can be tested
526
Args : string element name
532
my ($self,$name) = @_;
533
return 0 if ! defined $self->{'_elements'}->[0];
534
return ( $self->{'_elements'}->[0] eq $name)
537
=head2 start_document
539
Title : start_document
540
Usage : $eventgenerator->start_document
541
Function: Handle a start document event
550
$self->{'_lasttype'} = '';
551
$self->{'_values'} = {};
552
$self->{'_result'}= undef;
553
$self->{'_elements'} = [];
554
$self->{'_reporttype'} = $DEFAULTFORMAT;
561
Usage : $eventgenerator->end_document
562
Function: Handles an end document event
563
Returns : Bio::Search::Result::ResultI object
570
my ($self,@args) = @_;
571
return $self->{'_result'};
576
my ($self, $blast, @args) = @_;
578
if( not defined($self->writer) ) {
579
$self->warn("Writer not defined. Using a $DEFAULT_WRITER_CLASS");
580
$self->writer( $DEFAULT_WRITER_CLASS->new() );
582
$self->SUPER::write_result( $blast, @args );
586
return 1; # can a sim4 report contain more than one result?
589
sub report_count { shift->result_count }
594
Usage : Private method. For internal use only.
595
if( $self->_will_handle($type) ) { ... }
596
Function: Provides an optimized way to check whether or not an element of a
597
given type is to be handled.
598
Returns : Reference to EventHandler object if the element type is to be handled.
599
undef if the element type is not to be handled.
600
Args : string containing type of element.
604
1. Using the cached pointer to the EventHandler to minimize repeated lookups.
605
2. Caching the will_handle status for each type that is encountered
606
so that it only need be checked by calling handler->will_handle($type) once.
608
This does not lead to a major savings by itself (only 5-10%).
609
In combination with other optimizations, or for large parse jobs, the
610
savings good be significant.
612
To test against the unoptimized version, remove the parentheses from
613
around the third term in the ternary " ? : " operator and add two
614
calls to $self-E<gt>_eventHandler().
619
my ($self,$type) = @_;
620
my $handler = $self->{'_handler_cache'} ||= $self->_eventHandler;
622
my $will_handle = defined($self->{'_will_handle_cache'}->{$type})
623
? $self->{'_will_handle_cache'}->{$type}
624
: ($self->{'_will_handle_cache'}->{$type} =
625
$handler->will_handle($type));
627
return $will_handle ? $handler : undef;