【问题标题】:Eliminating unitialized values in my Perl hash of arrays消除我的 Perl 数组散列中的未初始化值
【发布时间】:2019-08-15 00:39:33
【问题描述】:

我成功地创建了一个数组哈希,并使用它来计算文件中每个 DNA 序列的对数赔率分数(Creating a hash of arrays for DNA sequences, Perl 具有输入文件格式)。我得到每个序列的分数,但我得到每个计算的警告。自然,我想清除警告。警告是:Use of uninitialized value in string eq at line 148

这里是代码的摘要版本(如果需要,我可以发布完整的代码):

use strict;
use warnings;
use Data::Dumper;

#USER SPECIFICATIONS
print "Please enter the filename of the fasta sequence data: ";
my $filename1 = <STDIN>;

#Remove newline from file
chomp $filename1;

#Open the file and store each dna seq in hash
my %id2seq = ();
my %HoA = ();
my %loscore = ();
my $id = '';
open (FILE, '<', $filename1) or die "Cannot open $filename1.",$!;
my $dna;
while (<FILE>)
{
    if($_ =~ /^>(.+)/)
    {
         $id = $1; #Stores 'Sequence 1' as the first $id, for example
    }
    else
    {
        $HoA{$id} = [ split(//) ]; #Splits the contents to allow for position reference later
        $id2seq{$id} .= $_; #Creates a hash with each seq associated to an id number, used for calculating tables that have been omitted for space
        $loscore{$id} .= 0; #Creates a hash with each id number to have a log-odds score
    }
}
close FILE;

#User specifies motif width
print "Please enter the motif width:\n";
my $width = <STDIN>;

#Remove newline from file
chomp $width;

#Default width is 3 (arbitrary number chosen)
if ($width eq '')
{
    $width = 3;
}

#Omitting code about $width<=0, creation of log-odds score hash to save space

foreach $id (keys %HoA, %loscore)
{
    for my $pos (0..($width-1))
    {
        for my $base (qw( A C G T))
        {
            if ($HoA{$id}[$pos] eq $base) #ERROR OCCURS HERE
            {
                $loscore{$id} += $logodds{$base}[$pos];
            }
            elsif ( ! defined $HoA{$id}[$pos]) 
            {
                print "$pos\n"; 
            }
        }
    }
}
print Dumper(\%loscore);

我得到的输出是:

Use of uninitialized value in string eq at line 148, <STDIN> line 2.
2
(This error repeats 4 times for each position - most likely due to matching to each $base?)

$VAR1 = {
         'Sequence 15' => '-1.27764697876093',
         'Sequence 4' => '0.437512962981119',
         (continues for 29 sequences)
        }

总而言之,我想计算每个序列的对数赔率得分。我有一个 log-odds 分数哈希%loscore,其中包含一个基序内每个位置的碱基分数。通过对参考值求和来计算对数赔率分数。例如,如果 log-odds 表是

A 4 3 2
C 7 2 1
G 6 9 2
T 1 0 3

序列CAG 的log-odds 分数将是7+3+2=12

目前,我认为错误的发生是因为我将 DNA 字符串拆分为数组哈希的方式。正如我之前所说,如果您想要所有代码以便复制粘贴,我可以提供。我认为解决方案非常简单,我只需要有人指出我正确的方向。感谢您提供任何和所有帮助,我可以在出现问题时进行澄清。此外,感谢任何可以帮助我发布更简洁问题的提示(我知道这个很长,我只想提供足够的背景信息)。

【问题讨论】:

  • 这句话看起来有点可疑:foreach $id (keys %HoA, %loscore)。为什么只取第一个哈希的键?
  • 应该是foreach $id (keys %HoA)
  • 我的格式显然有误。我改成foreach $id (keys %HoA, keys %loscore),现在可以正常使用了!感谢您的快速帮助!
  • foreach $id (keys %HoA, keys %loscore)foreach $id (keys %HoA, keys %HoA) 相同。确定要循环每个键两次吗?
  • Nit: $loscore{$id} .= 0; 应该是 $loscore{$id} = 0;,它应该在 if 的“then”部分,而不是“else”解析中。

标签: arrays string perl hash


【解决方案1】:

这是我用来遍历 `%HoA.它计算每个序列的对数赔率分数,然后遍历每个序列以找到每个序列的最大分数。非常感谢大家的帮助!

foreach $id (keys %HoA)
{
    for my $pos1 (0..length($HoA{$id})-1)
    {
        for my $pos2 ($pos1..$pos1+($width-1))
        {
            for my $base (qw( A C G T))
            {
                if ($HoA{$id}[$pos2] eq $base)
                {
                    for my $pos3 (0..$width-1)
                    {
                        $loscore{$id} += $logodds{$base}[$pos3];

                        if ($loscore{$id} > $maxscore{$id})
                        {
                            $maxscore{$id} = $loscore{$id};
                        }
                    }
                }
                elsif ( ! defined $HoA{$id}[$pos2])
                {
                    print "$pos2\n";
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 2011-12-13
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    相关资源
    最近更新 更多