Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,
At a certain point in my code the Bio::SimpleAlign add_seq method is called. This method will give a warning if (chunk of the add_seq code):
if( $self->{'_seq'}->{$name} ) { $self->warn("Replacing one sequence [$name]\n") unless $self->verbose < 0; }
What happens here? I cannot find the _seq method anywhere in the BioPerl documentation and so cannot find out what is wrong with my sequence. Anyone here who could help me?

Cheers,
Wil

Replies are listed 'Best First'.
Re: [BioPerl] a warning I do not understand omits a sequence
by ikegami (Patriarch) on Jan 11, 2010 at 17:15 UTC

    _seq is not a method. It's a string, used as a hash key. The hash field appears to hold one of the object's attributes: The sequences keyed by name. The code you posted appears to be a check to see if two sequences have the same name.

Re: [BioPerl] a warning I do not understand omits a sequence
by BioLion (Curate) on Jan 11, 2010 at 17:18 UTC

    I am not massively familiar with Bio::SimpleAlign, but a few things might help you work out what is going on: _seq is not a method, it is part of the structure of the object (a blessed hash), which from the looks of the source code, holds the sequence objects used in the multiple alignment.

    So i would hazard a guess this code is doing first checking to see if a sequence of the given name already exists in the lookup hash ( ->{_seq} ) and then warns you conditional on the 'verbose' setting.

    Usually 'verbose' setting control how much output a program gives, so this is probably a 'suppress warnings' construct. If the author used exists this would stop autovivification, and allow you to check for a sequence twice!

    I couldn't find the verbose method, but the Bio::SimpleAlign inherits from :

    use base qw(Bio::Root::Root Bio::Align::AlignI Bio::AnnotatableI Bio::FeatureHolderI);
    So I suspect that one of these will hold the answer on that...

    So your error is probably coming from overwriting a named code... You should be able to check beforehand, to make sure you don't do this.

    Hope this helps ( and if i am wrong, it will be pointed out pretty quickly... ), and if you have more questions, we'll need to see more code and examples.

    Just a something something...
      If the author used exists this would stop autovivification, and allow you to check for a sequence twice!

      Just a side note...

      In a boolean context such as that of the OPed code fragment, hash keys are not autovivified at their lowest level, but any 'intervening' keys are. This is also true when exists is used. So with or without  exists, as long as autovivification of the  '_seq' key does not cause any side effects, the code can check for the pre-existence of  $name as often as desired!
      Update: However, there is a (possibly very important) difference between the two tests: A key that really and truly does exist in a hash may yet have a value like undef, the empty string, the string  '0' or numeric 0 that will evaluate as boolean false.

      >perl -wMstrict -MData::Dumper -le "my $hr; die 'huh?' if $hr->{foo}{bar}; print Dumper $hr; undef $hr; print 'hash ref de-init'; die 'huh?' if exists $hr->{foo}{bar}; print Dumper $hr; " $VAR1 = { 'foo' => {} }; hash ref de-init $VAR1 = { 'foo' => {} };
Re: [BioPerl] a warning I do not understand omits a sequence
by Khen1950fx (Canon) on Jan 11, 2010 at 20:00 UTC
    I found a _seq method here. Here's some info:

    Title : _seq
    Usage : get/set seq reference
    Function:
    Example : $self->_seq($new_seq)
    Returns : reference to underlying contained seq object
    Args : (optional) new-value

    sub _seq { my ($self,$value) = @_; if( defined $value) { $self->{'_seqobj'} = $value; } return $self->{'_seqobj'}; }
    It's probably not what you were looking for. Looking at the source, there's $self->{'_seq'} = {}; which sets up internal hashes.
Re: [BioPerl] a warning I do not understand omits a sequence
by biohisham (Priest) on Jan 11, 2010 at 22:06 UTC
    Can you show more code and the modules associated then clearly describe the warnings that are returned?

    the add_seq method of Bio::SimpleALign would return nothing and it adds just another sequence to the alignment.

    what is "$name" intended to hold and how was it initialized?

    if you really intended "_seq" as a method that's not how you do it. because "_seq" would be a sub that waits for appropriate argument to be passed

    $self->_seq($new_seq_name)

    Check Bio::CorbaServer::PrimarySeq for an example.

    Confusion sets in because _seq can be a hash key too as the OP showed and as is observed in other modules from BioPerl, so seeing more of the code can give clearer clues to what's going on...


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
      Dear Monks,

      Thanks for all your explanations. I think the answer is as simple as Ikegami and BioLion state it: it is likely that there are two sequences with the same name. I shall check this before I try any more complicated solutions.

      @ BioLion: verbose lives in Bio::Root::Root. I am not using it, the default level works for me.
      @ Khen1950fx: I did find the PrimarySeq _seq method, but that only lives in the Corba version and I only use the bio-perl-live packages.

      You have all been very helpful!
      Wil