-3

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";
}
Borodin
  • 126,100
  • 9
  • 70
  • 144
MTG
  • 191
  • 1
  • 22
  • 1
    This will not compile. Please post enough code that it can be run without fixing syntax errors, unless those are part of the question. This creates additional work for anyone trying to help you! Also, please do put `use strict;` and `use warnings;` at the beginning of the file. – bytepusher Jul 29 '17 at 11:13
  • 2
    Well I've posted an answer. If it's not useful, please try and explain what you're looking for in more detail. This is a perl forum, and I for one am not a geneticist ;) – bytepusher Jul 29 '17 at 11:34
  • 2
    @MTG: Please don't edit your question so as to make nonsense of any answers or comments that have already been posted. I have restored your malformed Perl code and added your new code as an update. – Borodin Jul 29 '17 at 14:26

1 Answers1

1

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";
bytepusher
  • 1,568
  • 10
  • 19