C
Chris
Hi all,
I'm having difficulty trying to genericise this problem and
am looking for suggestions to point me in the right direction:
Take the following string:
LVFSGVLPLGTDTQNADI(SF)LWAKSFGAVIASKINM(KNA)TTHLVAGRNRT(DAGV)KVREATRYPKVKIVTTQWLLDCLTQWKRLAEEPYLLP
The letters in brackets are ambiguous positions where any of
the letters are allowed in that position. I want output all
the allowed combinations of the ambiguous positions e.g.
LVFSGVLPLGTDTQNADISLWAKSFGAVIASKINMKTTHLVAGRNRTDKVREATRYPKVKIVTTQWLLDCLTQWKRLAEEPYLLP
LVFSGVLPLGTDTQNADISLWAKSFGAVIASKINMNTTHLVAGRNRTAKVREATRYPKVKIVTTQWLLDCLTQWKRLAEEPYLLP
etc. ^ ^ ^
There can be any number of ambiguous positions with upto 4
allowed letters per position.
The code below correctly outputs all the combinations for
the above example, but I want to make this more generic for
more or less ambiguous positions. I would appreciate any
pointers as this has been bugging me for days now :-(
It only outputs the ambiguous letters - I can add in the
rest easily enough later.
TIA
Chris.
#!/usr/bin/perl
use strict;
use warnings;
my $string =
'LVFSGVLPLGTDTQNADI(SF)LWAKSFGAVIASKINM(KNA)TTHLVAGRNRT(DAGV)KVREATRYPKVKIVTTQWLLDCLTQWKRLAEEPYLLP';
my @ambis;
my $hit = 0;
while ($string =~ /\((\w+)\)/g) { # extract ambiguous regions
my @F = split //, $1;
foreach my $aa (@F) {
$ambis[$hit]{$aa}++;
}
++$hit;
}
if (scalar @ambis) { # output all combinations
# this bit doesn't work as desired :-(
# my $i = 0;
# while ($ambis[$i]) {
# foreach my $k (keys %{$ambis[$i]}) {
# print "$k\n";
# }
# ++$i;
# }
# this works, but only for when there are three ambiguous
regions. How can I change it?
foreach my $k1 (keys %{$ambis[0]}) {
foreach my $k2 (keys %{$ambis[1]}) {
foreach my $k3 (keys %{$ambis[2]}) {
print "$k1$k2$k3\n";
}
}
}
}
I'm having difficulty trying to genericise this problem and
am looking for suggestions to point me in the right direction:
Take the following string:
LVFSGVLPLGTDTQNADI(SF)LWAKSFGAVIASKINM(KNA)TTHLVAGRNRT(DAGV)KVREATRYPKVKIVTTQWLLDCLTQWKRLAEEPYLLP
The letters in brackets are ambiguous positions where any of
the letters are allowed in that position. I want output all
the allowed combinations of the ambiguous positions e.g.
LVFSGVLPLGTDTQNADISLWAKSFGAVIASKINMKTTHLVAGRNRTDKVREATRYPKVKIVTTQWLLDCLTQWKRLAEEPYLLP
LVFSGVLPLGTDTQNADISLWAKSFGAVIASKINMNTTHLVAGRNRTAKVREATRYPKVKIVTTQWLLDCLTQWKRLAEEPYLLP
etc. ^ ^ ^
There can be any number of ambiguous positions with upto 4
allowed letters per position.
The code below correctly outputs all the combinations for
the above example, but I want to make this more generic for
more or less ambiguous positions. I would appreciate any
pointers as this has been bugging me for days now :-(
It only outputs the ambiguous letters - I can add in the
rest easily enough later.
TIA
Chris.
#!/usr/bin/perl
use strict;
use warnings;
my $string =
'LVFSGVLPLGTDTQNADI(SF)LWAKSFGAVIASKINM(KNA)TTHLVAGRNRT(DAGV)KVREATRYPKVKIVTTQWLLDCLTQWKRLAEEPYLLP';
my @ambis;
my $hit = 0;
while ($string =~ /\((\w+)\)/g) { # extract ambiguous regions
my @F = split //, $1;
foreach my $aa (@F) {
$ambis[$hit]{$aa}++;
}
++$hit;
}
if (scalar @ambis) { # output all combinations
# this bit doesn't work as desired :-(
# my $i = 0;
# while ($ambis[$i]) {
# foreach my $k (keys %{$ambis[$i]}) {
# print "$k\n";
# }
# ++$i;
# }
# this works, but only for when there are three ambiguous
regions. How can I change it?
foreach my $k1 (keys %{$ambis[0]}) {
foreach my $k2 (keys %{$ambis[1]}) {
foreach my $k3 (keys %{$ambis[2]}) {
print "$k1$k2$k3\n";
}
}
}
}