<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>

CC BY-SA 4.0
2021-07-13T15:45:57.400Z
Thanks @Throckmorton for the response. Your code is still throwing the error at me, but manages to retrieve the Ensembl ID for CCL5: `Error:  Can't call method "stable_id" on an undefined value at ./get_ensembl_ids.pl line 20, <IDs> line 1.

GENE ENSG00000271503,\n% ` Any idea why that might be? Can I ask what version of perl you're using?
user45678789
CC BY-SA 4.0
2021-07-14T21:57:18.387Z
I am running 5.26
Throckmorton
CC BY-SA 4.0
2021-07-15T01:30:38.950Z
@user45678789 I will say it is a bit suspicious that you have `use Bio::Ensembl::Registry;` as well as `my $registry = 'Bio::EnsEMBL::Registry';`, I would assume the case should be the same in both lines.
Throckmorton
CC BY-SA 4.0
2021-07-01T03:52:18.457Z
2021-07-01T19:03:23.877Z
2021-07-01T19:03:23.877Z
Throckmorton
<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>

<ensembl><perl>
Using txt file to query Ensembl API
# Using txt file to query Ensembl API

(Asked by: username_0 on 2021-06-30)

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 (){
    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,  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?

**Tags:** ensembl, perl

---

## 1 answers

### Answer by username_1 on 2021-07-01. Score: 2

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 (){ 
    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 (){ 
    chomp;
    eval {
        my $gene = $gene_adaptor -> fetch_by_display_label($_);
        print "GENE ", $gene->stable_id(), ",";
    };
    if ($@) {
        warn "Error:\t$@\n"
    }
}
print '\n';

> **Comments:**
> **username_0**: Thanks @Throckmorton for the response. Your code is still throwing the error at me, but manages to retrieve the Ensembl ID for CCL5: `Error:  Can't call method "stable_id" on an undefined value at ./get_ensembl_ids.pl line 20, <IDs> line 1.  GENE ENSG00000271503,\n% ` Any idea why that might be? Can I ask what version of perl you're using?  
> **username_1**: I am running 5.26  
> **username_1**: @user45678789 I will say it is a bit suspicious that you have `use Bio::Ensembl::Registry;` as well as `my $registry = 'Bio::EnsEMBL::Registry';`, I would assume the case should be the same in both lines.  

---

