最简单的数据结构的应用,把相同id的行合并,涉及引用等知识:
#!/usr/bin/env perl
use warnings;
use strict;
my %hash;
open FA, $ARGV[0] || die $!;
while (<FA>)
{
chomp;
my @tmp = split;
my $string = "$tmp[1]\t$tmp[2]\t$tmp[3]\t$tmp[4]\t$tmp[5]";
push @{$hash{$tmp[0]}}, $string;
}
close FA;
foreach my $id (sort {$a cmp $b} keys %hash)
{
if (@{$hash{$id}} == 1)
{
print "$id\t$hash{$id}[0]\n";
}else{
my ($ref, $sum);
my (@colsum, @set);
for(my $i = 0; $i < @{$hash{$id}}; $i ++)
{
@set = split /\s/, $hash{$id}[$i];
for(my $j = 0; $j < @set; $j ++)
{
$ref->[$i][$j] = $set[$j];
}
}
for(my $j = 0; $j < @{$ref->[0]}; $j ++)
{
for(my $i = 0; $i < @{$ref}; $i ++)
{
$sum += $ref->[$i][$j];
}
push @colsum, $sum;
}
print join "\t", $id, @colsum, "\n";
}
}