6

鉴于这些输入:

my $init_seq = "AAAAAAAAAA" #length 10 bp 
my $sub_rate = 0.003;
my $nof_tags = 1000;
my @dna = qw( A C G T );

我想生成:

  1. 一千个长度 - 10 个标签

  2. 标签中每个位置的替换率是 0.003

产生输出如:

AAAAAAAAAA
AATAACAAAA
.....
AAGGAAAAGA # 1000th tags

在 Perl 中有一种紧凑的方法吗?

我坚持将此脚本的逻辑作为核心:

#!/usr/bin/perl

my $init_seq = "AAAAAAAAAA" #length 10 bp 
my $sub_rate = 0.003;
my $nof_tags = 1000;
my @dna = qw( A C G T );

    $i = 0;
    while ($i < length($init_seq)) {
        $roll = int(rand 4) + 1;       # $roll is now an integer between 1 and 4

        if ($roll == 1) {$base = A;}
        elsif ($roll == 2) {$base = T;}
        elsif ($roll == 3) {$base = C;}
        elsif ($roll == 4) {$base = G;};

        print $base;
    }
    continue {
        $i++;
    }
4

5 回答 5

5

作为一个小的优化,替换:

    $roll = int(rand 4) + 1;       # $roll is now an integer between 1 and 4

    if ($roll == 1) {$base = A;}
    elsif ($roll == 2) {$base = T;}
    elsif ($roll == 3) {$base = C;}
    elsif ($roll == 4) {$base = G;};

    $base = $dna[int(rand 4)];
于 2009-03-02T10:21:58.370 回答
3

编辑:假设替代率在 0.001 到 1.000 范围内:

以及$roll,在[1..1000]范围内生成另一个(伪)随机数,如果它小于或等于(1000 * $sub_rate)则执行替换,否则什么都不做(即输出'A') .

请注意,除非您的随机数生成器的属性已知,否则您可能会引入细微的偏差。

于 2009-03-02T09:32:01.923 回答
2

不完全是你要找的东西,但我建议你看看 BioPerl 的Bio::SeqEvolution::DNAPoint模块。它虽然没有将突变率作为参数。相反,它询问您想要的原始序列同一性的下限是多少。

use strict;
use warnings;
use Bio::Seq;
use Bio::SeqEvolution::Factory;

my $seq = Bio::Seq->new(-seq => 'AAAAAAAAAA', -alphabet => 'dna');

my $evolve = Bio::SeqEvolution::Factory->new (
   -rate     => 2,      # transition/transversion rate
   -seq      => $seq
   -identity => 50      # At least 50% identity with the original
);


my @mutated;
for (1..1000) { push @mutated, $evolve->next_seq }

所有 1000 个变异序列都将存储在 @mutated 数组中,它们的序列可以通过该seq方法访问。

于 2009-03-02T20:14:57.137 回答
1

如果发生替换,您希望从可能性中排除当前碱基

my @other_bases = grep { $_ ne substr($init_seq, $i, 1) } @dna;
$base = @other_bases[int(rand 3)];

另请参阅Mitch Wheat 的回答,了解如何实施替代率。

于 2009-03-03T12:31:18.147 回答
1

我不知道我是否理解正确,但我会做这样的事情(伪代码):

digits = 'ATCG'
base = 'AAAAAAAAAA'
MAX = 1000
for i = 1 to len(base)
  # check if we have to mutate
  mutate = 1+rand(MAX) <= rate*MAX
  if mutate then
    # find current A:0 T:1 C:2 G:3
    current = digits.find(base[i])
    # get a new position 
    # but ensure that it is not current
    new = (j+1+rand(3)) mod 4        
    base[i] = digits[new]
  end if
end for
于 2009-03-05T04:41:19.740 回答