forked from samyk/samytools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathde_bruijn
executable file
·56 lines (51 loc) · 846 Bytes
/
de_bruijn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/perl
# de bruijn sequence generator
# by samy kamkar
use strict;
my $nums = shift || 3;
my $alpha = shift || 2;
print(de_bruijn($alpha, $nums));
print "\n";
sub de_bruijn
{
my ($k, $n) = @_;
my @sequence;
my @alphabet;
if ($k =~ /^\d+$/)
{
@alphabet = 0 .. $k;
}
else
{
@alphabet = split //, $k;
$k = @alphabet;
}
# my @alphabet = ($k =~ /^\d+$/ ? 0 .. $k : split //, $k);
my @a = (0) x ($k * $n);
sub db
{
my ($t, $p) = @_;
if ($t > $n)
{
if ($n % $p == 0)
{
foreach my $j (1 .. $p)
{
push @sequence, $alphabet[$a[$j]];
}
}
}
else
{
$a[$t] = $a[$t-$p];
db($t+1, $p);
foreach my $j ($a[$t-$p]+1 .. $k-1)
{
$a[$t] = $j;
db($t+1, $t);
}
}
}
db(1, 1);
return join("", @sequence) . ($alphabet[0] x ($n-1));
}