Code for perl to generate random sequences with specific base ratios

Source: Internet
Author: User

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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.