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

Hi Monks,

I am trying to get to grips with bioperl and have some really stupid questions.

Basically all variables seem to be a reference to something rather than holding the actual value themselves, which is causing me problems when it comes to printing them out to see what they hold.

Here is my code:

my $seqio = $gb->get_Stream_by_id(['13474692']); + + while( my $seq = $seqio->next_seq ) { print "seq is ", $seq->display_id, "\n"; my @f=$seq->get_all_SeqFeatures; #This gives you the annotation +of the retrieved sequence object
How can I print the contents of @f ?

Nothing seems to be working past here.

foreach my $feat (@f) { my $ann=$feat->annotation; next unless ($ann->get_Annotations('coded_by')); my @coded=$ann->get_Annotations('coded_by'); foreach my $location (@coded) { print $location->value, " is the location that codes this pro +tein\n"; } } }

Replies are listed 'Best First'.
Re: Bioperl + perl modules
by srdst13 (Pilgrim) on Jun 27, 2005 at 10:21 UTC

    Bioperl is amazingly useful, but it isn't known for its ease-of-use, at least when getting started. I would highly recommend starting with one of the HOWTOs. I would suggest that the most useful are the "beginners", "SearchIO (blast parsing)", "SeqIO (Sequence reading/format manipulation)", and "Feature-Annotation".

    That said, you have "partially" working code. "How do I get further along?" you ask.

    I make heavy use of the perl debugger. Just run your script using perl -d script.pl Then, hit keep hitting n and RETURN to go to the next line. When you get to the line after the variable whose contents you want to know, just type: x @f to see the Data::Dumper version of what is in @f, including what class is associated with the various things in @f.

    Hope that helps,
    Sean
Re: Bioperl + perl modules
by Joost (Canon) on Jun 27, 2005 at 09:40 UTC
      Hi

      I have progressed slightly since my post. Here is my newer code below, it doesn't print any warnings or errors, I am having a problem in the loop starting  foreach $key ( $ac->get_all_annotation_keys() ) {

      . How can I tell whether $ac variable has a value? If I print it out, it looks like a hash.

      + my $seqio = $gb->get_Stream_by_id(['13474692']); #print $gb->get_Stream_by_id(['13474692']); + + while( my $seq = $seqio->next_seq ) { print "seq is ", $seq->display_id, "\n"; my @f=$seq->get_SeqFeatures; #This gives you the annotation of t +he retrieved sequence object + foreach my $feat (@f) { + print "Feature ",$feat->primary_tag," starts ",$feat->start," e +nds ", $feat->end," strand ",$feat->strand,"\n"; + # features retain link to underlying sequence object #print "Feature sequence is ",$feat->seq->seq(),"\n"; + my $ac=$feat->annotation(); + # HOW CAN I TELL IF $ac HAS A VALUE print "GETS TO HERE\n"; + foreach $key ( $ac->get_all_annotation_keys() ) { print "DOESN'T GET TO HERE\n"; @values = $ac->get_Annotations($key); foreach $value ( @values ) { # value is an Bio::AnnotationI, and defines a "as_text +" method print "Annotation ",$key," stringified value ",$value- +>as_text,"\n"; + # also defined hash_tree method, which allows data ori +entated # access into this object $hash = $value->hash_tree(); } } + + next unless ($ac->get_Annotations('coded_by')); my @coded=$ac->get_Annotations('coded_by'); foreach my $location (@coded) { print $location->value, " is the location that codes this pro +tein\n"; } } }
        foreach $key ( $ac->get_all_annotation_keys() ) {
        if $ac is not a valid object of the right type, that code would throw an error: note that you are not indexing a hash here, you're calling an object method. see perltoot for more information on the perl OO system.

        If you want to know what data is stored in a perl variable or object, an easy way to see that is to use the standard Data::Dumper module:

        use Data::Dumper; # ... print '$ac: ',Dumper($ac); foreach $key ( $ac->get_all_annotation_keys() ) print '$key: ',Dumper($key); }

        That should probably give you some indication of what these objects contain. Note that object can store data in such a way that Data::Dumper doesn't know how to get at it, but it's a start anyway.

Re: Bioperl + perl modules
by stajich (Chaplain) on Jul 03, 2005 at 20:36 UTC
    @f will hold an array of Bio::SeqFeatureI objects. This is one per feature in the feature table (so 'source', 'Protein', 'CDS' in this example). You have to further query the feature object to get the data out from it. A simple method $f->gff_string can be called to get a quick representation in GFF Format

    To help you with your problem specifics, if you are trying to get the cds sequence for the protein, see the FAQ Q5.4 which should get you most of the way there.