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?