怎么用perl程序,随机生成一条序列,使ACGT四种碱基的含量分别为0.3,0.3,0.2,0.2!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| #!/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"; |
#!/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";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| #!/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"; |
#!/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。
1
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 |
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
相关
用模块之后会成整数麼?不随机?
你说的整数指的是全长吗?
保持例子中碱基比例不变,把长度l设成11,我简单测试了一下,每次多出来的那个碱基(11-10=1)不一定是哪一个,应该是随机的。
当然,最好还是阅读模块的源码,这样就知道是不是真的随机了。
Well done!