-1
$\begingroup$
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';

How can I print first occurrence of protein sequence?

$\endgroup$
  • 1
    $\begingroup$ Please edit your question and clarify. Do you mean print just the first sequence? Or do you mean that if multiple identifiers have the same sequence, you only want to print the first of them? Or is it that you might have different identifiers with the same sequence and you only want the first of those? $\endgroup$ – terdon Jul 30 '17 at 14:16
4
$\begingroup$

In the simplest case, if you just want to stop after the first record was printed, you can just add exit (I also corrected the syntax errors you had and added use strict and use warnings; I suggest you get into the habit of using those two, they save you from a lot of grief in the long run):

#!/usr/bin/env perl
use Bio::DB::GenBank;
use Bio::DB::Query::GenBank;
use strict;
use warnings; 
my $query = "LEGK"; 
my $query_obj = Bio::DB::Query::GenBank->new(-db => 'protein', 
                                          -query => $query );
my $gb_obj = Bio::DB::GenBank->new;
my $stream_obj = $gb_obj->get_Stream_by_query($query_obj);
while (my $seq_obj = $stream_obj->next_seq) {
	print ">$query",' ', $seq_obj->display_id, ' ', $seq_obj->desc,"\n", $seq_obj->seq, "\n";
    exit;
}

If instead, you want to only process the first result for a list of multiple queries:

#!/usr/bin/env perl
use Bio::DB::GenBank;
use Bio::DB::Query::GenBank;
use strict;
use warnings; 
my @queries = qw(LEGK TP53 ACT1); 
query:foreach my $query (@queries) {
	my $query_obj = Bio::DB::Query::GenBank->new(-db => 'protein', 
                                          -query => $query );
	my $gb_obj = Bio::DB::GenBank->new;
	my $stream_obj = $gb_obj->get_Stream_by_query($query_obj);
	while (my $seq_obj = $stream_obj->next_seq) {
		print ">$query",' ', $seq_obj->display_id, ' ', $seq_obj->desc,"\n", $seq_obj->seq, "\n";
        ## Move to the next query
        next query;
    }
}

Or, to print only the first sequence if multiple quries have identical sequences:

#!/usr/bin/env perl
use Bio::DB::GenBank;
use Bio::DB::Query::GenBank;
use strict;
use warnings; 
my @queries = qw(LEGK TP53 ACT1); 
foreach my $query (@queries) {
	my %seen;
	my $query_obj = Bio::DB::Query::GenBank->new(-db => 'protein', 
                                          -query => $query );
	my $gb_obj = Bio::DB::GenBank->new;
	my $stream_obj = $gb_obj->get_Stream_by_query($query_obj);
	while (my $seq_obj = $stream_obj->next_seq) {
		if ($seen{$seq_obj->seq}) {
			## Here, since we're not using an argument (next query), this will move
			## to the next iteration of the current loop and not the `foreach`. 
			next;
		}
		print ">$query",' ', $seq_obj->display_id, ' ', $seq_obj->desc,"\n", $seq_obj->seq, "\n";
    }
}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.