1
$\begingroup$

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?

$\endgroup$

1 Answer 1

2
$\begingroup$

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';
$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$ Jul 13, 2021 at 15:45
  • $\begingroup$ I am running 5.26 $\endgroup$ Jul 14, 2021 at 21:57
  • $\begingroup$ @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. $\endgroup$ Jul 15, 2021 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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