[Bioperl-l] Alternate hit sorting for Bio::Search::Result objects

Donald Jackson donald.jackson at bms.com
Tue Jan 6 10:05:43 EST 2004


Chris and Jason,

I've coded up a sort_hits method for Bio::Search::ResultI.pm (attached) 
which does what I was hoping.  I realized my last email was 
contradictory - one must either modify the hits list in the result 
object or modify the writers; I decided to go for the former.

I've held off on committing this because I'm still not sure we want to 
do this in ResultI instead of ResultWriter.  What are peoples' thoughts?

Thanks,

Don Jackson
BMS Bioinformatics

#############################################################
#
# POD and code for proposed sort_hits() method in 
Bio::Search::ResultI.pm follow          
#
########################################################################

=head2 sort_hits

 Title		: sort_hits
 Usage		: $result->sort_hits(\&sort_function)
 Function	: Sorts the available hit objects by a user-supplied function	
 Returns	: n/a
 Args		: A coderef for the sort function.  See the documentation on the Perl sort() 
                  function for guidelines on writing sort functions.  
 Note		: To access the special variables $a and $b used by the Perl sort() function 
                  the user function must access Bio::Search::Result::ResultI namespace. 
                  For example, use : 
                  $result->sort_hits( sub{$Bio::Search::Result::ResultI::a->length <=> 
					      $Bio::Search::Result::ResultI::b->length});
                   NOT $result->sort_hits($a->length <=>$b->length);

=cut

sub sort_hits {
    my ($self, $coderef) = @_;
    my @sorted_hits;

    unless ($coderef and ref($coderef) eq 'CODE') {
	$self->throw('next_hit requires a sort function passed as a subroutine reference');
    }

    my @hits = $self->hits();
    
    eval {@sorted_hits = sort $coderef @hits };

   if ($@) {
       $self->throw("Unable to sort hits: $@");
   }
   else {
       $self->{'_hits'} = \@sorted_hits;
       $self->{'_no_iterations'} = 1; # to bypass iteration checking in hits() method
       1;
   }
}




>  
>



More information about the Bioperl-l mailing list