[Bioperl-l] Sort for get_all_tags method

Jason Stajich jason at bioperl.org
Tue May 27 21:24:47 UTC 2008


Again, please ask these questions on the list.

yes it is possible, but I don't specifically have any code for doing  
the sorting myself, but have hacked up some untested code at the end  
of this msg. The main problem for bioperl is the Feature object needs  
a new method to accept setting a sort function, something we can  
easily do, but someone just needs to put in a little time to test it  
out and make sure it all works.

It would boil down to updating the function that is implemented in  
Bio::SeqFeature::Generic :

sub get_all_tags {
    my ($self, @args) = @_;
    return sort keys %{ $self->{'_gsf_tag_hash'}};
}

If you are looking for more example code, you might try the Perl  
Cookbook from O'Reilly.  I would implement a specific sort function  
by probably mapping the keys to a number based on your pre-determined  
preference (ie 'note' would map to 0, 'gene' to 1, etc) and then all  
tags without a mapping would get ordered alphabetically.
Something like this


The solution of updating the module to accept a general sort function  
would be a more general solution, but you can always override this  
all_tags function in your local script by including this code in the  
beginning of your script:


use Bio::SeqFeature::Generic;
my %lookup = ( 'note' => 0, 'gene' => 1, 'locus' => 2 );
sub Bio::SeqFeature::Generic::get_all_tags {
    my ($self, @args) = @_;
    return sort { my ($amap,$bmap) = map { $lookup{$_} } ($a,$b);
                  my $rc = undef;
		 if( defined $amap && ! defined $bmap ) {
	          $rc = -1; # only $a is in the lookup, it should come first
                  } elsif( ! defined $amap && defined $bmap ) {
                   $rc = 1;  # only $b is in the lookup, it should  
come first
                  } elsif( defined $amap && defined $bmap ) {
                   $rc = $amap <=> $bmap; # numeric compare, these  
are both in the lookup
                  } else {
                   $rc = $amap cmp $bmap; # alpha compare, neither  
are in the lookup;
                  }
               $rc; # return code from the function passed to sort
             } keys %{ $self->{'_gsf_tag_hash'}};
} # end routine

-jason

On May 22, 2008, at 12:24 PM, Mgavi Brathwaite wrote:

> Hi Jason,
>
> I looked at your comment and I agree that there should be a sort  
> method to
> return the tags in a predictable order. Would you be willing to  
> share you
> code for sorting tags with the get_all_tags method ?
>
> M
>




More information about the Bioperl-l mailing list