Wednesday, November 19, 2008

Perl recursion for oligo creation


#!/usr/bin/perl -w
use strict;

=head1 NAME

oligoGenerator.pl

=head1 DESCRIPTION

Perl script to generate all possible combinations of size k
using an alphabet @a, we use function recursion.

My intention is to create all possible oligonucleotides (DNA
alphabet or ACGT) but can be extended to any other field
using a different alphabet.

Output also can be printed in other forms, you can put other
delimiters in the push function or in the final array printed.

=cut

my ($k) = 4; # definition of the word size
my @a = qw/A C G T/; # definition of the alphabet
my @words = createWords($k, @a);# main function
print join("\n", @words), "\n"; # print output

sub createWords {
my $k = shift @_; $k--;
my @old = @_;
my @new = ();
if ($k < 1) {
return @old;
}
else {
foreach my $e (@old) {
foreach my $n (@a) {
push @new, "$e$n"; # add new element
}
}
createWords($k, @new); # recursion call
}
}

=head1 AUTHOR

Juan Caballero @ 2008

=head1 CONTACT

linxe __a__ glib.org.mx

=head1 LICENSE

Perl Artistic License v2.0
http://www.perlfoundation.org/artistic_license_2_0

=cut

No comments:

Post a Comment