2

I have a tab-delimited file: abc.txt. which has data like:

Pytul_T015270   Protein of unknown function
Pytul_T015269   Protein of unknown function
Pytul_T015255   Protein of unknown function
Pytul_T015297   Protein of unknown function

I am creating a parser which takes this abc.txt and 2 other files as input and parses the files by calling different subroutines from a package: utility.pm

The subroutine to parse abc.txt is defined in my package, utility.pm goes as follows:

use strict;

sub readblast{

my $fileName = shift;
my %hash;
my %geneNameHash;

open PRED, $fileName or die "Can't open file $!\n";
while (my $line=<PRED>) {

    chomp $line;
    #print $line,"\n";
    (my $gene,my $desc) =  split /\t/, $line;

    $hash{$gene} = $desc;
}

close(PRED);

return %hash;
}

And my parser.pl script, which uses the hash is as follows:

my %blast=&utility::readblast($ARGV[2]);
for my $mRNA(keys %{ $featureHash{$scaffold}{$gene}}){
my $desc = $blast{$mRNA};
}

Here $featurehash is another hash I made from another file. And $mRNA has the key values of the file abc.txt.

But output of $desc is blank and I am getting error:

Use of uninitialized value $desc in concatenation (.) or string at parser.pl

What is wrong with my $desc = $blast{$mRNA}; And why won't it store the 2nd column of abc.txt?

aki2all
  • 429
  • 1
  • 8
  • 24
  • 1
    Are there actual tabs in the file? Use `Data::Dumper` to dump the structure as you read it and post the output. – Sinan Ünür Aug 23 '13 at 14:39
  • @SinanÜnür I have checked my `abc.txt` file. That is a tab-delimited file. first column is the mRNA and 2nd column is the description. – aki2all Aug 23 '13 at 14:43
  • 1
    Have you printed the value of `$mRNA`? Have you printed the keys (and values) in `%blast`? That's the first step. On the surface, the message means that the value in `$mRNA` is not present as a key in `%blast`. – Jonathan Leffler Aug 23 '13 at 14:45

1 Answers1

4

The following guards against trailing blank lines and possible non-tab separators (by using split with a limit):

#!/usr/bin/env perl

package My::Utility;

use strict;
use warnings;

sub read_blast {
    my $fh = shift;

    my %hash;

    while (my $line = <$fh>) {
        chomp $line;
        last unless $line =~ /\S/;
        my ($key, $value) = split ' ', $line, 2;
        $hash{ $key } = $value;
    }

    return \%hash;
}

package main;

my $blast = My::Utility::read_blast(\*DATA);
while (my ($k, $v) = each %$blast) {
    print "'$k' => '$v'\n";
}

__DATA__
Pytul_T015270   Protein of unknown function
Pytul_T015269   Protein of unknown function
Pytul_T015255   Protein of unknown function
Pytul_T015297   Protein of unknown function
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • I have to link the key of the has `%blast` with $mRNA inside the loop: `for my $mRNA(keys %{ $featureHash{$scaffold}{$gene}}){ }` which has all the mRNA values that are same in the key values of `abc.txt` file. So, how would I print the description inside my loop? – aki2all Aug 23 '13 at 15:09
  • `print $blast->{$mRNA};` ... I can't comment on data you haven't shown. – Sinan Ünür Aug 23 '13 at 15:34