将值推入数组的哈希时出错



我正在分析psiblast的输出报告。我使用COG比对,并在基因数据库中搜索匹配(同源物)。我想做的一件事是找出哪些基因与多个COG匹配。我的部分脚本如下。

我在创建一个数组时遇到了问题,该数组包含分配给多个COG的基因的所有COG。

我收到以下错误"在parse_POG_reports.pl第26行第67行使用"strict refs"时,不能将字符串("COG0003")用作ARRAY ref。"。

我看过其他关于将元素推入数组散列的帖子。但我认为,当一个基因与同一COG有2个匹配时,可能会发生错误,并且它试图将相同的COG推入数组(即样本输入的最后2行)。这有道理吗?如果是,我该如何避免这个问题?

use strict;
use warnings;
my %maxBits;my %COGhit_count;
my $Hohits={};my %COGhits;
my $COG_psi_report=$ARGV[0];
open (IN, $COG_psi_report) or die "cannot open $COG_psi_reportn";
while (my $line=<IN>){
    next if ($line =~/^#/);
    chomp $line;
    my @columns = split(/t/,$line);
    my $bits=$columns[11];
    my $COG=$columns[0];
    my $hit=$columns[1];
    my $Eval=$columns[10];
    next if ($Eval > 0.00001); # threshold for significant hits set by DK
    $COGhit_count{$hit}++; # count how many COGs each gene is homologous to
    $COGhits{$hit}=$COG;
    if ($COGhit_count{$hit}>1) {
            push @{$COGhits{$hit}}, $COG; #
    }
    ## for those that there are multiple hits we need to select top hit ##
    if (!exists $maxBits{$hit}){
            $maxBits{$hit}=$bits;
    }
    elsif (exists $maxBits{$hit} && $bits > $maxBits{$hit}){
            $maxBits{$hit}=$bits;
    }
    $Hohits->{$hit}->{$bits}=$COG;
}
close (IN);

示例输入:

POG0002 764184357-stool1_revised_scaffold22981_1_gene47608      23.90   159     112     3       1       156     1       153     2e-06   54.2
POG0002 764062976-stool2_revised_C999233_1_gene54902    23.63   182     121     5       3       169     2       180     2e-06   53.9
POG0002 763901136-stool1_revised_scaffold39447_1_gene145241     26.45   155     89      3       3       137     5       154     3e-06   53.9
POG0002 765701615-stool1_revised_C1349270_1_gene168522  23.53   187     115     5       3       169     2       180     5e-06   53.1
POG0002 158802708-stool2_revised_C1077267_1_gene26470   22.69   216     158     5       3       213     5       216     5e-06   52.7
POG0003 160502038-stool1_revised_scaffold47906_2_gene161164     33.00   297     154     6       169     424     334     626     6e-40    157
POG0003 160502038-stool1_revised_scaffold47906_2_gene161164     16.28   172     128     4       23      192     46      203     1e-06   56.6
POG0003 158337416-stool1_revised_C1254444_1_gene13533   30.06   346     184     7       133     424     57      398     6e-40    155
POG0003 158337416-stool1_revised_scaffold29713_1_gene153054     28.61   332     194     8       132     424     272     599     2e-38    152
POG0003 158337416-stool1_revised_scaffold29713_1_gene153054     24.00   200     131     5       1       193     5       190     9e-11   69.3

您需要去掉第24行(向后计数):

$COGhits{$hit}=$COG;

在其中,您将$COGhits{$hit}设置为标量值($COG的值)。稍后,在第26行中,您试图将$COGhits{$hit}取消引用为一个数组以推入其中。这不起作用,因为其中有一个标量。

只需移除if并将这些行更改为此即可。这应该很有用,因为现在所有的$hit都存储在数组引用中。

$COGhit_count{$hit}++; # count how many COGs each gene is homologous to
push @{$COGhits{$hit}}, $COG;

$COGhits:输出

$VAR4 = {
      '158802708-stool2_revised_C1077267_1_gene26470' => [
                                                           'POG0002'
                                                         ],
      '764062976-stool2_revised_C999233_1_gene54902' => [
                                                          'POG0002'
                                                        ],
      '764184357-stool1_revised_scaffold22981_1_gene47608' => [
                                                                'POG0002'
                                                              ],
      '765701615-stool1_revised_C1349270_1_gene168522' => [
                                                            'POG0002'
                                                          ],
      '763901136-stool1_revised_scaffold39447_1_gene145241' => [
                                                                 'POG0002'
                                                               ],
      '160502038-stool1_revised_scaffold47906_2_gene161164' => [
                                                                 'POG0003',
                                                                 'POG0003'
                                                               ]
    };

但是,如果同时需要标量和数组ref,请尝试以下代码不过我不建议这样做

$COGhit_count{$hit}++; # count how many COGs each gene is homologous to
if ($COGhit_count{$hit} == 1) {
  $COGhits{$hit}=$COG;             # Save as scalar
}
elsif ($COGhit_count{$hit} == 2) { # If we've just found the second hit,
  my $temp = $COGhits{$hit};       # save the first and convert $COGhits{$hit}
  $COGhits{$hit} = [];             # to an array ref, then push both the old and
  push @{$COGhits{$hit}}, $temp, $COG; # the new value in it.
} elsif ($COGhit_count{$hit} > 2) {
  push @{$COGhits{$hit}}, $COG;    # Just push the new value in
}

想法:您可能先有$COGhits{$hit}=$COG,但后来注意到有时可能有多个值,所以添加了push行,但您没有意识到实际上必须替换旧行。

它准确地告诉你你做错了什么。

$COGhits{$hit}=$COG;  # <--- scalar
if ($COGhit_count{$hit}>1) {
        push @{$COGhits{$hit}}, $COG; # <--- array
}

不能将值指定为非引用类型,然后尝试将其自动激活为引用类型。Perl将执行后者,但如果您已经在该位置存储了冲突的数据类型,则不会执行。

此外,如果这是第一次奇迹般地起作用(不会),并且你运行了不止一次,那么你可能已经被推送自动激活的任何数组都会被标量非引用赋值所破坏。

我不确定你在追求什么,但第一行应该可能被删除。


与该构造不同,您需要决定是否会有一个以上的值为$hit$COG规范。如果可以的话,简单地用push替换这4条线路是可行的。

我以前做过多用途结构槽,它们在很大程度上很难维护。但如果你想做这样的事情,你可以这样做:

my $ref = $hashref->{ $key }; # autovivifies slot as simple scalar.
                               # it starts out as undefined.
if ( ref $$ref ) {             # ref $ref will always be true
    push @$$ref, $value;
}
else { 
    $$ref = defined( $$ref ) ? [ $$ref, $value ] : $value;
}

但是,每次您想以某种不同的方式访问混合树时,都必须编写分叉逻辑。使用标量所节省的性能在一定程度上被测试和分支所消耗。

所以我不再做太多了。我事先决定关系是1-1还是1-n。像下面这样的例程可以在一定程度上使处理这类表更加简单。

sub get_list_from_hash { 
    my ( $hash, $key ) = @_;
    my $ref = $hash->{ $key };
    return unless defined( $$ref );
    return ref( $$ref ) ? @$$ref : $$ref;
}
sub store_in_hash { 
    $_[0] = {} unless ref $_[0];
    my ( $hash, $key, @values ) = @_;
    my @defined = grep {; defined } @values;
    unless ( @defined ) { 
        delete $hash->{ $key };
        return;
    }
    my $ref = $hash->{ $key };
    if ( ref $$ref ) { 
        push @$$ref, @defined;
    }
    elsif ( defined $$ref ) { 
        $$ref = [ $$ref, @defined ];
    }
    elsif ( @values > 1 ) { 
        @$$ref = @defined;
    }
    else { 
        ( $$ref ) = @defined;
    }
}

最新更新