perl生成特定堿基比例的隨機序列的代碼
更新時間:2013年03月01日 11:24:56 作者:
怎么用perl程序,隨機生成一條序列,使ACGT四種堿基的含量分別為0.3,0.3,0.2,0.2!
方法一(不使用模塊,by agonyr)
復制代碼 代碼如下:
#!/usr/bin/perl -w
use strict;
my @seq = ( "A", "T", "C", "G" );
my $length = 10000;
undef my %hash;
$hash{"A"} = int( $length * 0.3 );
$hash{"C"} = int( $length * 0.3 );
$hash{"G"} = int( $length * 0.2 );
$hash{"T"} = int( $length * 0.2 );
my $i = 0;
while ( $i 《 $length ) {
my $word = $seq[ rand(@seq) ];
if ( $hash{$word} ) {
print "$word";
$i++;
}
$hash{$word}--;
}
print "n";
方法二(使用模塊,by yixf)
復制代碼 代碼如下:
#!/usr/bin/perl
use strict;
use warnings;
use BioX::SeqUtils::RandomSequence;
my $randomizer = BioX::SeqUtils::RandomSequence-》new(
{
l =》 10000,
s =》 1,
y =》 "dna",
a =》 3,
c =》 3,
g =》 2,
t =》 2
}
);
print $randomizer-》rand_seq(), "n";
兩種方法比較
設定長度為10000,ACGT的比例為3:3:2:2。
復制代碼 代碼如下:
withoutModule Length=10000 GC=49.42% A=2558,C=2503,G=2439,T=2500,Others=0
withModule Length=10000 GC=50.00% A=3000,C=3000,G=2000,T=2000,Others=0
相關文章
perl中chomp的使用介紹(chop和chomp函數(shù)區(qū)別)
perl程序中,有時在輸入過程中使用chomp才會得到正確的結果2013-02-02