[Bioperl-l] Using a hash reference in the SeqFeature tag system

Jason Stajich jason at cgt.duhs.duke.edu
Wed Jul 14 14:24:26 EDT 2004


On Wed, 14 Jul 2004, Leo, Paul (NIH/NHGRI) wrote:

>
> Hi,
> I want to add additional information of a $seq object using the tag system.
> The tag value I want to add is a reference to a hash of hashes (though I
> don't think it worked for just a hash either).
> I thought that so long as the tag value was a scalar (i.e. a reference) then
> all would be ok?
> Perhaps I have done something silly ....and this is a simple perl
> programming mistake... But have been staring at this for too long!
> Can someone tell me why the example below fails? If so, any ideas on how to
> add this hash structure to a $seq object ??

do you want them to be serializable (i.e. do you want to be able to write
out your hashref in genbank format?  Cause just adding a hashref won't
really work correctly.

>
>
>
> use Bio::DB::GenPept;
> use Bio::SeqFeature::Generic;
> use Bio::Seq;
> use Bio::SeqIO;
>
>
> ######## make a hash of hashes ###########
> $Data{cage1}{mouse1}{legs}=1;
> $Data{cage1}{mouse1}{tails}=10;
>
> $Data{cage1}{mouse2}{legs}=2;
> $Data{cage1}{mouse2}{tails}=20;
>
> $Data{cage2}{mouse1}{legs}=3;
> $Data{cage2}{mouse1}{tails}=30;
>
> $Data{cage2}{mouse2}{legs}=4;
> $Data{cage2}{mouse2}{tails}=40;
>
> print $Data{cage1}{mouse2}{legs}, "\n"; #just a check :gives "2"
> print map "$_ ",keys(%{$Data{cage2}{mouse2}});  #just a check gives "legs
> tails"
> print "\n";
> ########################################
>
> #### make a hash reference and put in into tag ###
> $href=\%{$Data{cage1}{mouse1}}; #make ref here to a hash since used in tests
> below
> $seqextra=new Bio::SeqFeature::Generic ( -tag => {
>                   experiment_id => 1,
>                   Cage =>  $href     });
>
> ###############Sanity Checks...
> print "Ref ok : \n";
> print map "$_ ",keys(%{$href}); #prints "legs tails" as expected

I think your problem is all about context.
get_tag_values returns an array even if there is only one tag value.  You
need to either tell perl you want to operate on the first value from that
array or more simply grab that first value and then do things like calling
keys.




my $feat = Bio::SeqFeature::Generic->new(-tag =>
                      { 'Cage' => { 'one' => 'tail'}});
my ($val) = $feat->get_tag_values('Cage');

print join(" ", keys %$val), "\n";
$val->{'two'} = 'nose';

my ($val2) = $feat->get_tag_values('Cage');
# val got updated because we were operating on a hashref before
print join(" ", keys %$val2), "\n";


n.b. you call add_tag_values and there is already an existing value for
the tag, now there will be two values stored.

> print "\n";
>
> print "Ref in a simple hash ok : \n";
> $hashtest{zero}=$href;
> print map "$_ ",keys(%{$hashtest{zero}}); #prints "legs tails" as expected
> ############ This fails....

> print "\n";
> print "Tag in not ? \n";
> print map "$_ ",keys(%{$seqextra->get_tag_values('Cage')}); #NOTHING!
>
# try this
my ($val) = $seqextra->get_tag_values('Cage');
print join(" ", keys %$val),"\n";

> Any suggestions?
> Thanks in advance
> Paul
>
>

--
Jason Stajich
Duke University
jason at cgt.mc.duke.edu


More information about the Bioperl-l mailing list