text: user12664: I am trying to use an external file with a list of gene names to query the ENSEMBL IDs using the perl API. This script works:
  #!/usr/local/bin/perl
  
  use strict;
  use warnings;
  use Bio::Ensembl::Registry;
  
  my $registry = 'Bio::EnsEMBL::Registry';
  
  $registry->load_registry_from_db(
      -host => 'useastdb.ensembl.org',
      -port => 5306,
      -user => 'anonymous'
  );
  
  my $gene_adaptor = $registry->get_adaptor( 'Human', 'Core', 'Gene' );
  
  my @genes;
  @genes = ('CCL2', 'CCL5');
  
  foreach $name(@genes){
      my $gene = $gene_adaptor -> fetch_by_display_label($name);
      print "GENE ", $gene->stable_id(), ",";
  }
  
  However I can't figure out how to get this to work with an external file that contains a list of genes like this:
  genes.txt
  CCL2
  CCL5
  
  Here's what I tried:
  #!/usr/local/bin/perl
  
  use strict;
  use warnings;
  use Bio::Ensembl::Registry;
  
  my $registry = 'Bio::EnsEMBL::Registry';
  
  $registry->load_registry_from_db(
      -host => 'useastdb.ensembl.org',
      -port => 5306,
      -user => 'anonymous'
      );
  
  my $gene_adaptor = $registry->get_adaptor( 'Human', 'Core', 'Gene' );
  
  open (IDs, '<', "genes.txt") or die "can't read input file";
  while (<IDs>){
      my $gene = $gene_adaptor -> fetch_by_display_label($_);
      print "GENE ", $gene->stable_id(), ",";
  }
  
  But I'm getting this error:
  Can't call method "stable_id" on an undefined value at ./get_ensembl_ids.pl line 22, <IDs> line 1.
  I was trying to follow this tutorial for reading txt files in perl, but what am I missing for getting perl to actually fetch the ENSEMBL ids with each of the .txt file lines?
  
  user6628: Similar to reading files in other languages, it is necessary to trim the newlines from each line. If you add chomp; to the first line of the while loop, the code runs just fine (note that I also print a newline at the end of the loop).
  open (IDs, '<', "genes.txt") or die "can't read input file";
  while (<IDs>){ 
      chomp;
      my $gene = $gene_adaptor -> fetch_by_display_label($_);
      print "GENE ", $gene->stable_id(), ",";
  }
  print '\n';
  
  The new loop produces the following result:
  >$ perl query.pl 
  GENE ENSG00000108691,GENE ENSG00000271503,
  
  Additionally, you may need to catch any exceptions/errors thrown if you have an invalid gene in your input file:
  open (IDs, '<', "genes.txt") or die "can't read input file";
  while (<IDs>){ 
      chomp;
      eval {
          my $gene = $gene_adaptor -> fetch_by_display_label($_);
          print "GENE ", $gene->stable_id(), ",";
      };
      if ($@) {
          warn "Error:\t$@\n"
      }
  }
  print '\n';
  
  
original_text: 12664: <p>I am trying to use an external file with a list of gene names to query the ENSEMBL IDs using the perl API. This script works:</p>
  <pre><code>#!/usr/local/bin/perl
  
  use strict;
  use warnings;
  use Bio::Ensembl::Registry;
  
  my $registry = 'Bio::EnsEMBL::Registry';
  
  $registry-&gt;load_registry_from_db(
      -host =&gt; 'useastdb.ensembl.org',
      -port =&gt; 5306,
      -user =&gt; 'anonymous'
  );
  
  my <span class="math-container">$gene_adaptor = $</span>registry-&gt;get_adaptor( 'Human', 'Core', 'Gene' );
  
  my @genes;
  @genes = ('CCL2', 'CCL5');
  
  foreach <span class="math-container">$name(@genes){
      my $gene = $gene_adaptor -&gt; fetch_by_display_label($name);
      print "GENE ", $</span>gene-&gt;stable_id(), &quot;,&quot;;
  }
  </code></pre>
  <p>However I can't figure out how to get this to work with an external file that contains a list of genes like this:</p>
  <p><code>genes.txt</code></p>
  <pre><code>CCL2
  CCL5
  </code></pre>
  <p>Here's what I tried:</p>
  <pre><code>#!/usr/local/bin/perl
  
  use strict;
  use warnings;
  use Bio::Ensembl::Registry;
  
  my $registry = 'Bio::EnsEMBL::Registry';
  
  $registry-&gt;load_registry_from_db(
      -host =&gt; 'useastdb.ensembl.org',
      -port =&gt; 5306,
      -user =&gt; 'anonymous'
      );
  
  my <span class="math-container">$gene_adaptor = $</span>registry-&gt;get_adaptor( 'Human', 'Core', 'Gene' );
  
  open (IDs, '&lt;', &quot;genes.txt&quot;) or die &quot;can't read input file&quot;;
  while (&lt;IDs&gt;){
      my <span class="math-container">$gene = $</span>gene_adaptor -&gt; fetch_by_display_label(<span class="math-container">$_);
      print "GENE ", $</span>gene-&gt;stable_id(), &quot;,&quot;;
  }
  </code></pre>
  <p>But I'm getting this error:
  <code>Can't call method &quot;stable_id&quot; on an undefined value at ./get_ensembl_ids.pl line 22, &lt;IDs&gt; line 1.</code></p>
  <p>I was trying to follow this <a href="https://www.perltutorial.org/perl-read-file/" rel="nofollow noreferrer">tutorial</a> for reading txt files in perl, but what am I missing for getting perl to actually fetch the ENSEMBL ids with each of the .txt file lines?</p>
   6628: <p>Similar to reading files in other languages, it is necessary to trim the newlines from each line. If you add <a href="https://perldoc.perl.org/functions/chomp" rel="nofollow noreferrer"><code>chomp;</code></a> to the first line of the <code>while</code> loop, the code runs just fine (note that I also print a newline at the end of the loop).</p>
  <pre class="lang-perl prettyprint-override"><code>open (IDs, '&lt;', &quot;genes.txt&quot;) or die &quot;can't read input file&quot;;
  while (&lt;IDs&gt;){ 
      chomp;
      my $gene = $gene_adaptor -&gt; fetch_by_display_label($_);
      print &quot;GENE &quot;, $gene-&gt;stable_id(), &quot;,&quot;;
  }
  print '\n';
  </code></pre>
  <p>The new loop produces the following result:</p>
  <pre><code>&gt;$ perl query.pl 
  GENE ENSG00000108691,GENE ENSG00000271503,
  </code></pre>
  <p>Additionally, you may need to <a href="https://stackoverflow.com/a/4006339/7092484">catch any exceptions/errors</a> thrown if you have an invalid gene in your input file:</p>
  <pre class="lang-perl prettyprint-override"><code>open (IDs, '&lt;', &quot;genes.txt&quot;) or die &quot;can't read input file&quot;;
  while (&lt;IDs&gt;){ 
      chomp;
      eval {
          my $gene = $gene_adaptor -&gt; fetch_by_display_label($_);
          print &quot;GENE &quot;, $gene-&gt;stable_id(), &quot;,&quot;;
      };
      if ($@) {
          warn &quot;Error:\t$@\n&quot;
      }
  }
  print '\n';
  </code></pre>
  