2

我的数据如下所示:

    my @homopol = (
                   ["T","C","CC","G"],  # part1
                   ["T","TT","C","G","A"], #part2
                   ["C","CCC","G"], #part3 ...upto part K=~50
                  );


    my @prob = ([1.00,0.63,0.002,1.00,0.83],
                [0.72,0.03,1.00, 0.85,1.00],
                [1.00,0.97,0.02]);


   # Note also that the dimension of @homopol is always exactly the same with @prob.
   # Although number of elements can differ from 'part' to 'part'.

我想做的是

  1. 生成元素的所有part1组合partK
  2. 求 中对应元素的乘积@prob

因此,最后我们希望得到这个输出:

T-T-C  1 x 0.72 x 1 = 0.720
T-T-CCC     1 x 0.72 x 0.97 = 0.698
T-T-G  1 x 0.72 x 0.02 = 0.014
...
G-G-G  1 x 0.85 x 0.02 = 0.017
G-A-C  1 x 1 x 1 = 1.000
G-A-CCC     1 x 1 x 0.97 = 0.970
G-A-G  1 x 1 x 0.02 = 0.020

问题是我的以下代码通过对循环进行硬编码来做到这一点。由于 is 的部分数量@homopol可以变化且很大(例如~K=50),我们需要一种灵活且紧凑的方式来获得相同的结果。有没有?我正在考虑使用Algorithm::Loops,但不知道如何实现。

use strict;
use Data::Dumper;
use Carp;


my @homopol = (["T","C","CC","G"],
               ["T","TT","C","G","A"],
               ["C","CCC","G"]);


my @prob = ([1.00,0.63,0.002,1.00,0.83],
            [0.72,0.03,1.00, 0.85,1.00],
            [1.00,0.97,0.02]);



my $i_of_part1 = -1;
foreach my $base_part1 ( @{ $homopol[0] } ) {
    $i_of_part1++;
    my $probpart1 = $prob[0]->[$i_of_part1];

    my $i_of_part2 =-1;
    foreach my $base_part2 ( @{ $homopol[1] } ) {
        $i_of_part2++;
        my $probpart2 = $prob[1]->[$i_of_part2];

        my $i_of_part3 = -1;
        foreach my $base_part3 ( @{ $homopol[2] } ) {
            $i_of_part3++;
            my $probpart3 = $prob[2]->[$i_of_part3];

            my $nstr = $base_part1."".$base_part2."".$base_part3;
            my $prob_prod = sprintf("%.3f",$probpart1 * $probpart2 *$probpart3);

            print "$base_part1-$base_part2-$base_part3 \t";
            print "$probpart1 x $probpart2 x $probpart3 = $prob_prod\n";

        }
    }
}
4

5 回答 5

4

我会推荐Set::CrossProduct,它将创建一个迭代器来产生所有集合的叉积。因为它使用了迭代器,所以不需要预先生成每个组合;相反,它会按需生产每一个。

use strict;
use warnings;
use Set::CrossProduct;

my @homopol = (
    [qw(T C CC G)],
    [qw(T TT C G A)],
    [qw(C CCC G)], 
);

my @prob = (
    [1.00,0.63,0.002,1.00],
    [0.72,0.03,1.00, 0.85,1.00],
    [1.00,0.97,0.02],
);

# Prepare by storing the data in a list of lists of pairs.
my @combined;
for my $i (0 .. $#homopol){
    push @combined, [];
    push @{$combined[-1]}, [$homopol[$i][$_], $prob[$i][$_]]
        for 0 .. @{$homopol[$i]} - 1;
};

my $iterator = Set::CrossProduct->new([ @combined ]);
while( my $tuple = $iterator->get ){
    my @h = map { $_->[0] } @$tuple;
    my @p = map { $_->[1] } @$tuple;
    my $product = 1;
    $product *= $_ for @p;
    print join('-', @h), ' ', join(' x ', @p), ' = ', $product, "\n";
}
于 2009-09-18T13:14:03.237 回答
2

在不更改输入数据的情况下使用Algorithm::Loops的解决方案如下所示:

use Algorithm::Loops;

# Turns ([a, b, c], [d, e], ...) into ([0, 1, 2], [0, 1], ...)
my @lists_of_indices = map { [ 0 .. @$_ ] } @homopol;

NestedLoops( [ @lists_of_indices ], sub {
  my @indices = @_;
  my $prob_prod = 1; # Multiplicative identity
  my @base_string;
  my @prob_string;
  for my $n (0 .. $#indices) {
    push @base_string, $hompol[$n][ $indices[$n] ];
    push @prob_string, sprintf("%.3f", $prob[$n][ $indices[$n] ]);
    $prob_prod *= $prob[$n][ $indices[$n] ];
  }
  print join "-", @base_string; print "\t";
  print join "x", @prob_string; print " = ";
  printf "%.3f\n", $prob_prod;
});

但我认为您实际上可以通过将结构更改为类似的结构来使代码更清晰

[ 
  { T => 1.00, C => 0.63, CC => 0.002, G => 0.83 },
  { T => 0.72, TT => 0.03, ... },
  ...
]

因为没有并行数据结构,您可以简单地迭代可用的基本序列,而不是迭代索引然后在两个不同的地方查找这些索引。

于 2009-09-18T08:57:22.067 回答
0

为什么不使用递归?将深度作为参数传递,并让函数在循环内以 depth+1 调用自身。

于 2009-09-18T07:17:36.923 回答
-1

您可以通过创建一个与@homopol 数组(例如N)长度相同的索引数组来做到这一点,以跟踪您正在查看的组合。事实上,这个数组就像一个以 N 为底的数字,元素是数字。以与在基数 N 中写下连续数字相同的方式进行迭代,例如 (0 0 0 ... 0), (0 0 0 ... 1), ...,(0 0 0 ... N- 1), (0 0 0 ... 1 0), ....

于 2009-09-18T07:18:40.667 回答
-2

方法 1:从指数计算

计算 homopol (length1 * length2 * ... * lengthN) 中长度的乘积。然后,从零迭代 i 到产品。现在,您想要的索引是 i % length1, (i / length1)%length2, (i / length1 / length2) % length3, ...

方法 2:递归

我被打败了,看看nikie的回答。:-)

于 2009-09-18T07:25:38.140 回答