Answers.Body: <p>In the simplest case, if you just want to stop after the first record was printed, you can just add <code>exit</code> (I also corrected the syntax errors you had and added <code>use strict</code> and <code>use warnings</code>; I suggest you get into the habit of using those two, they save you from a lot of grief in the long run):</p>
  
  <pre class="lang-perl prettyprint-override"><code>#!/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-&gt;new(-db =&gt; 'protein', 
                                            -query =&gt; $query );
  my $gb_obj = Bio::DB::GenBank-&gt;new;
  my $stream_obj = $gb_obj-&gt;get_Stream_by_query($query_obj);
  while (my $seq_obj = $stream_obj-&gt;next_seq) {
   print "&gt;$query",' ', $seq_obj-&gt;display_id, ' ', $seq_obj-&gt;desc,"\n", $seq_obj-&gt;seq, "\n";
      exit;
  }
  </code></pre>
  
  <p>If instead, you want to only process the first result for a list of multiple queries:</p>
  
  <pre><code>#!/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-&gt;new(-db =&gt; 'protein', 
                                            -query =&gt; $query );
   my $gb_obj = Bio::DB::GenBank-&gt;new;
   my $stream_obj = $gb_obj-&gt;get_Stream_by_query($query_obj);
   while (my $seq_obj = $stream_obj-&gt;next_seq) {
    print "&gt;$query",' ', $seq_obj-&gt;display_id, ' ', $seq_obj-&gt;desc,"\n", $seq_obj-&gt;seq, "\n";
          ## Move to the next query
          next query;
      }
  }
  </code></pre>
  
  <p>Or, to print only the first sequence if multiple quries have identical sequences:</p>
  
  <pre><code>#!/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-&gt;new(-db =&gt; 'protein', 
                                            -query =&gt; $query );
   my $gb_obj = Bio::DB::GenBank-&gt;new;
   my $stream_obj = $gb_obj-&gt;get_Stream_by_query($query_obj);
   while (my $seq_obj = $stream_obj-&gt;next_seq) {
    if ($seen{$seq_obj-&gt;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 "&gt;$query",' ', $seq_obj-&gt;display_id, ' ', $seq_obj-&gt;desc,"\n", $seq_obj-&gt;seq, "\n";
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-30T15:28:33.087Z
Answers.LastActivityDate: 2017-07-30T15:28:33.087Z
Answers.UserDisplayName: terdon
Body: <pre><code>use Bio::DB::GenBank;
  use Bio::DB::Query::GenBank;
  
  $query = "LEGK"; 
  $query_obj = Bio::DB::Query::GenBank-&gt;new(-db =&gt; 'protein', 
                                            -query =&gt; $query );
  
  $gb_obj = Bio::DB::GenBank-&gt;new;
  
  $stream_obj = $gb_obj-&gt;get_Stream_by_query($query_obj);
  
  while ($seq_obj = $stream_obj-&gt;next_seq) {
      # do something with the sequence object    
      print "&gt;$query",' ', $seq_obj-&gt;display_id, ' ', $seq_obj-&gt;desc,"\n", $seq_obj-&gt;seq[,'\n';
  </code></pre>
  
  <p>How can I print first occurrence of protein sequence?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-30T14:16:49.327Z
Comments.Text: 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?
Comments.UserDisplayName: terdon
Tags: <database><public-databases><perl>
Title: Bioperl - how can i print first result of search sequence per iteration?
ThreadText: # Bioperl - how can i print first result of search sequence per iteration?
  
  (Asked by: username_0 on 2017-07-28)
  
  ```
  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?
  
  **Tags:** database, public-databases, perl
  
  > **Comments:**
  > **username_1**: 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?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-07-30. Score: 4 | Accepted answer
  
  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):
  
  ```perl
  #!/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";
      }
  }
  
  ```
  
  
  ---
  
  