如何在perl中创建矩阵格式

时间:2012-09-02 11:16:36

标签: perl

我有一个数组数组,其中每个数组都是:

clusterA gene1 1
clusterA gene2 0
clusterB gene1 1
clusterB gene2 0

我想生成一个像:

这样的文件
name       gene1    gene2 
clusterA    1         0
clusterB    1         0

目前的尝试:

if (condition) {

    @array = ($cluster, $genes, "1");
}
elsif (not condition) {

    @array = ($cluster, $genes, "0");
}
push @AoA, [ @array ];  

@ A0A是我的数组数组。

4 个答案:

答案 0 :(得分:0)

use warnings;
use strict;

my @AoA = ( ['a', 'b', 'c'], ['d', 'e', 'f'] );

open my $outf, ">", "output"
    or die();

print $outf join(" ", qw/name gene1 gene2/), "\n";
foreach my $arr (@AoA) {

    my @data = @{$arr};
    print $outf join(" ", @data), "\n";
}
close $outf;

答案 1 :(得分:0)

my %HoH;

foreach my $row (@AoA)
{
  $HoH{$row->[0]}{$row->[1]} = $row->[2];
}

print <<EOF;
name       gene1    gene2
clusterA    $HoH{clusterA}{gene1}         $HoH{clusterA}{gene2}
clusterB    $HoH{clusterB}{gene1}         $HoH{clusterB}{gene2}
EOF

答案 2 :(得分:0)

shaq想要总数吗?如果是这样,答案将更像(保留所有文件I / O以简化测试):

my @AOA = (
  [ qw(clusterA gene1 1) ],
  [ qw(clusterA gene2 0) ],
  [ qw(clusterB gene1 1) ],
  [ qw(clusterB gene2 0) ],
);

my %clusters;
my @genes = ( 0, 0 );

foreach my $array ( @AOA ) {

    my ( $cluster, $gene, $value ) = @$array;
    if ( $gene =~ /^gene(\d+)$/ ) {

        my $v = $1; 
        $clusters{$cluster} = [ 0, 0 ] 
                   unless ( exists $clusters{$cluster} );
        $clusters{$cluster}->[$v-1] += $value;
    }   
}

my $format = '%-12s %6s %6s'."\n";
printf( $format, qw( name gene1 gene2 ) );

foreach my $cluster ( sort keys %clusters ) {

    printf $format, $cluster, @{ $clusters{$cluster} };
}

答案 3 :(得分:0)

这种解决方案并不提前知道基因的名称。它将为所有看到的基因创建列。并且,它使用散列哈希。我解决了一个类似的问题,并且能够使用解决方案here的一些相同部分。

#!/usr/bin/perl
use strict;
use warnings;

my @array = map [split], split /\n/, <<EOF;
clusterA gene1 1
clusterA gene2 0
clusterB gene1 1
clusterB gene2 0
clusterC gene3 9
EOF

my %data;
for my $aref (@array) {
    $data{$aref->[0]}{$aref->[1]} = $aref->[2]; 
}

my %seen;
my @genes = sort
            grep ! $seen{$_}++,
            map keys %$_, values %data;

my $format = "%-15s" . "%-10s" x @genes . "\n";

printf $format, 'Cluster', @genes;

for my $name (sort keys %data) {
    my $href = $data{$name};
    printf $format, $name, map defined $_ ? $_ : 'none', @$href{ @genes };
}

这产生了下面的输出。

Cluster        gene1     gene2     gene3
clusterA       1         0         none
clusterB       1         0         none
clusterC       none      none      9

更新:首先创建哈希哈希值而不是数组数组可能会更容易。像:

my %data;
$data{$cluster}{$genes} = condition ? 1 : 0;