Method 1 (do not use modules, by agonyr)
Copy codeThe Code is as follows :#! /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 ";
Method 2 (Use module, by yixf)
Copy codeThe Code is as follows :#! /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 ";
Comparison of the two methods
Set the length to 10000 and the proportion of ACGT to 3: 3: 2: 2.
Copy codeThe Code is as follows: 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