我有一个看起来像这样的文件:
LOCUS POS ALIAS VAR TEST P I DESC
CCL23|disruptive chr17:34340329..34340854 . 2 BURDEN 1 0.43 0/1
CCL23|disruptive chr17:34340329..34340854 . 2 BURDEN 1 0.295 0/1
CCL23|disruptive chr17:34340329..34340854 . 2 BURDEN 1 0.005 1/1
CCL23|disruptive chr17:34340329..34340854 . 2 BURDEN 0.676617 0.005 1/0
我想用“/”分割最后一个字段,然后对这些数字求和,然后用总和推送另一列。例如,我希望输出看起来像:
CCL23|disruptive chr17:34340329..34340854 . 2 BURDEN 1 0.43 0/1 1
CCL23|disruptive chr17:34340329..34340854 . 2 BURDEN 1 0.295 0/1 1
CCL23|disruptive chr17:34340329..34340854 . 2 BURDEN 1 0.005 1/1 2
CCL23|disruptive chr17:34340329..34340854 . 2 BURDEN 0.676617 0.005 1/0 1
我有这段代码,但它不起作用:
#! perl -w
my $file1 = shift@ARGV;
my $NVAR=0;
my @vars;
open (IN, $file1) or die "couldn't read file one";
while(<IN>){
my@L=split;
next if ($L[0] =~ m/LOCUS/);
my@counts=split /\//, $L[7];
foreach (@counts){
$NVAR=${$_}[0] + ${$_}[1];
}
push @vars,[$L[0],$L[1],$L[2],$L[3],$L[4],$L[5],$L[6],$L[7],$NVAR];
}
close IN;
print "LOCUS POS ALIAS NVAR TEST P I DESC SUM\n";
foreach(@vars){
print "@{$_}\n";
}
感谢任何帮助。
答案 0 :(得分:1)
始终在每个脚本的顶部加入use strict;
和use warnings;
。
将变量限制在尽可能小的范围内,因为在$NVAR
循环之外声明while
会引入错误。您的总和可以通过以下方式确定:
my $NVAR = 0;
foreach (@counts){
#$NVAR=${$_}[0] + ${$_}[1]; <-- this was bad.
$NVAR += $_;
}
然而,这可以使用perl oneliner来解决
perl -MList::Util=sum -lane 'push @F, sum split "/", $F[-1]; print "@F"' file.txt
或者如果您有标题行:
perl -MList::Util=sum -lane '
push @F, $. == 1 ? "SUM" : sum split "/", $F[-1];
print "@F"
' file.txt
注意,您也可以在脚本中使用List::Util sum
。