Re: How to expand a string
by BrowserUk (Patriarch) on Nov 29, 2007 at 16:34 UTC
|
$s = 'RAGGTS';
$s =~ s[R][{A,G}]g;
$s =~ s[S][{C,G}]g;
print while glob $s;
AAGGTC
AAGGTG
GAGGTC
GAGGTG
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [Watch: Dir/Any] [d/l] |
|
++ nice trick :) didn't know that...
| [reply] [Watch: Dir/Any] |
Re: How to expand a string
by sundialsvc4 (Abbot) on Nov 29, 2007 at 19:08 UTC
|
A search for “DNA” at http://search.cpan.org produced 282 hits...
Maybe you actually don't have to write a program at all...
| [reply] [Watch: Dir/Any] |
Re: How to expand a string
by roboticus (Chancellor) on Nov 29, 2007 at 16:35 UTC
|
Becky:
If you're interested in doing this a *lot*, you might consider doing a "Super Search" for containing the keyword "continuation" or "permutation". If I recall correctly there were a few interesting threads that he participated in that centered around generating various combinations & permutations of strings.
...roboticus | [reply] [Watch: Dir/Any] |
Re: How to expand a string
by dsheroh (Monsignor) on Nov 29, 2007 at 16:25 UTC
|
Probably not a stable enough solution to be desirable, but I can't have been the only one to immediately think of Quantum::Superpositions... | [reply] [Watch: Dir/Any] |
Re: How to expand a string
by jagh (Monk) on Nov 29, 2007 at 15:29 UTC
|
In a particular chapter of Higher Order Perl, there is an implementation of this very problem. I don't have the book on me at the moment unfortunately, but it's in there. | [reply] [Watch: Dir/Any] |
Re: How to expand a string
by b4swine (Pilgrim) on Nov 29, 2007 at 16:02 UTC
|
A simple (but inefficient) way is:
$_ = ' RAGGTS ';
while (/[RS]/)
{ s/ ([A-Z]*)R([A-Z]*) / $1A$2 $1G$2 /g;
s/ ([A-Z]*)S([A-Z]*) / $1C$2 $1G$2 /g;
}
print;
| [reply] [Watch: Dir/Any] [d/l] |
Re: How to expand a string
by hangon (Deacon) on Nov 30, 2007 at 10:39 UTC
|
I have been working on a module that should do what you're looking for. Usage for your case follows, and the module is in the readmore tag below. Just put it in the same directory as your script and name it Permute.pm. Sorry no POD yet, but you should be able to get the idea.
use strict;
use Permute;
my $string = 'RAGGTS';
$string =~ s/S/\[C,G\]/g;
$string =~ s/R/\[A,G\]/g;
$string =~ s/K/\[G,T\]/g;
# $string now becomes: [A,G]AGGT[C,G]
my $seq = Permute->New($string);
# get one at a time
while (my $out = $seq->Next){
print "$out \n";
}
# or all at once
my @allofthem = $seq->All;
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: How to expand a string
by tcf03 (Deacon) on Nov 29, 2007 at 15:46 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $string = 'RAGGTS';
my @temp;
my @wanted;
if ( grep /R/, $string )
{
my $orig = $string;
$string =~ s/R/A/g;
push @temp, $string;
$string = $orig;
$string =~ s/R/G/g;
push @temp, $string;
}
for my $string_ (@temp)
{
if ( grep /S/, $string_ )
{
my $orig = $string_;
$string_ =~ s/S/C/g;
push @wanted, $string_;
$string_ = $orig;
$string_ =~ s/S/G/g;
push @wanted, $string_;
}
}
print Dumper @itemb;
a recursive sub might make that a little cleaner though.
UPDATE
output is
$VAR1 = 'AAGGTC';
$VAR2 = 'AAGGTG';
$VAR3 = 'GAGGTC';
$VAR4 = 'GAGGTG';
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
--Ralph Waldo Emerson
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: How to expand a string
by lima1 (Curate) on Nov 29, 2007 at 15:10 UTC
|
This sounds like homework. People will be glad to help you when you show that you have thought a little about this problem. | [reply] [Watch: Dir/Any] |
|
really it's not - I'm a postdoc trying to write a quick program to analyse DNA sequences! There's a heck of a lot of them to do and this was just a simple example - in some cases in DNA, 3 bases can be represented by 1 letter so it's not as simple as it first looks (at least to me!)
| [reply] [Watch: Dir/Any] |
|
use strict;
use warnings;
use Data::Dumper;
my %replace = (
R => '{A,G}',
S => '{C,G}',
K => '{G,T}',
' ' => '_',
);
my $s = 'KAG GTR CAG CTG AAG SAG TCA GG';
my @results;
for my $base (keys %replace) {
$s =~ s/$base/$replace{$base}/g;
}
push @results, $_ while glob $s;
@results = map { s/_/ /g; $_ } @results;
Update: BrowserUK's solution. Credits to him. | [reply] [Watch: Dir/Any] [d/l] |
|
|
This isn't a complete solution, but I think it will get you started on the right path. If you need more detailed help, I'll need a more detailed description of the problem (namely, expected inputs and outputs). What you want to do is iterate over the string and replace the current character with possible replacement characters. You could use recursion as mentioned above, but that's probably overkill for what you're looking for.
use strict;
#the substitution possibilities
my @a = ('a');
my @b = ('d','e');
my @c = ('f','g','h');
my $string = pop(@_); #this is an argument passed to the script on th
+e command line
#my $string = "abc"; #if you want it hardcoded
my $expanded_string = "";
my @strings = ();
foreach my $first (@a){
foreach my $second (@b){
foreach my $third (@c){
$string = "$first$second$third";
push(@strings, $string); #to store them all
print "$string\n"; #to print this particular one
}
}
}
| [reply] [Watch: Dir/Any] [d/l] |
|
|
|
Re: How to expand a string
by sh1tn (Priest) on Nov 29, 2007 at 15:21 UTC
|
| [reply] [Watch: Dir/Any] |
|
Thanks, but I don't just want to substitute, what I need to get and store is all the possible sequences from such a string. The real strings are DNA sequences 24 letters long and any of these letters may actually represent 2 or more possibilities.
| [reply] [Watch: Dir/Any] |