Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to expand a string

by Anonymous Monk
on Nov 29, 2007 at 14:53 UTC ( [id://653862]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a string of letters where certain letters can represent 2 or more options, e.g. if there is an R it means 'substitute either A or G in this position' and an S means 'subsitute either C or G'. So, a string like RAGGTS can be expanded to be:

1.AAGGTC
2.AAGGTG
3.GAGGTC
4.GAGGTG

How can a write some code that will expand the string RAGGTS into these 4 separate sequences and do something like store them in an array?

Thanks! Becky

Replies are listed 'Best First'.
Re: How to expand a string
by BrowserUk (Patriarch) on Nov 29, 2007 at 16:34 UTC
      ++ nice trick :) didn't know that...
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...

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

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...
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.
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;
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;
Re: How to expand a string
by tcf03 (Deacon) on Nov 29, 2007 at 15:46 UTC
    Simplistically:
    #!/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
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.
      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!)
        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.
        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 } } }
Re: How to expand a string
by sh1tn (Priest) on Nov 29, 2007 at 15:21 UTC
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://653862]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-19 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found