Search code examples
databaseperlbioinformaticsbioperl

How can I print the first result of a search sequence for each iteration?


How can I print the first occurrence of a protein sequence? For this query I get four results and I want only the first.

use Bio::DB::GenBank;
use Bio::DB::Query::GenBank;

$query     = "LEGK";
$query_obj = Bio::DB::Query::GenBank->new(
    -db    => 'protein',
    -query => $query
);

$gb_obj = Bio::DB::GenBank->new;

$stream_obj = $gb_obj->get_Stream_by_query( $query_obj );

while ( $seq_obj = $stream_obj->next_seq ) {

    # do something with the sequence object
    print
        ">$query", ' ',
        $seq_obj->display_id, ' ',
        $seq_obj->desc, "\n",
        $seq_obj->seq[, '\n';

That while loop should look like this

while ( $seq_obj = $stream_obj->next_seq ) {

    # do something with the sequence object
    print $seq_obj->display_id, "\t", $seq_obj->length, "\n";
}

Solution

  • The main problem I see with your snippet is that it does not compile. Put use strict; use warnings; at the beginning of all your perl programs. This will alert you to syntax errors.

    I do not know much about biology, however, you are iterating over sequence objects, but then making a rather strange call with $seq_obj->seq[,'\n'

    First of all, to call a function, use (), not [], [] indicates a reference to an array. Secondly, seq seems to be used to set or get a sequence value, and I do not see how '\n' would be a valid value.

    So

    while ($seq_obj = $stream_obj->next_seq) {
        print join(' ', $seq_obj->display_id, $seq_obj->desc)."\n"; # or use 'say'
        print $seq_obj->seq() . "\n";
    }
    

    should print all sequences. To get just the first, simply don't iterate through all results ( That's how I understood your question ):

    replace the while (){} with:

    my $first_seq_obj = $stream_obj->next_seq;
    print join(' ', $first_seq_obj->display_id, $first_seq_obj->desc)."\n"; # or use 'say'
    print $first_seq_obj->seq() . "\n";