in reply to Re: Shuffling codons
in thread Shuffling CODONS

Thanks for your reply. I know it is an FAQ, That's where I got the Fisher Yates option from, But unable to correctly implement it in my code..

If I try the list util it doesnt work... My code looks like this

print "enter sequence and signal end with enter followed by ctrl d\n"; $sequence = <STDIN>; chomp $sequence; print "sequence inserted : $sequence\n"; @trips = unpack("a3" x (length($sequence)-2), $sequence); @trips = join(" ", @trips); use List::Util 'shuffle'; @shuffled = shuffle(@trips); print "@shuffled\n";

What is wrong/missing here?

I am incapable of installing the list::utils, I wouldn't know where to unpack them and how to properly install and run them. Do they need to be situated somewhere. I am on perl v5.18.2, so the list utils should already be there you mention?

How would I implement the fisher Yates algorithm correctly?

Replies are listed 'Best First'.
Re^3: Shuffling codons
by pryrt (Abbot) on Jun 07, 2018 at 14:12 UTC

    perl v5.18.2 came after perl v5.7.3, so yes, it should be installed for you. However, the name is List::Util, not list::utils. The following shows that list::utils doesn't exist, but that List::Util exists and has been in core since v5.7.3:

    pryrt@debianvm:~$ perl -Mlist::utils -le 'print "OK"' Can't locate list/utils.pm in @INC (you may need to install the list:: +utils module) (@INC contains: ./blib/lib /home/pryrt/perl5/perlbrew/p +erls/perl-5.20.3/lib/site_perl/5.20.3/i686-linux /home/pryrt/perl5/pe +rlbrew/perls/perl-5.20.3/lib/site_perl/5.20.3 /home/pryrt/perl5/perlb +rew/perls/perl-5.20.3/lib/5.20.3/i686-linux /home/pryrt/perl5/perlbre +w/perls/perl-5.20.3/lib/5.20.3 .). BEGIN failed--compilation aborted. pryrt@debianvm:~$ perl -MList::Util -le 'print "OK"' OK pryrt@debianvm:~$ corelist List::Util Data for 2015-09-12 List::Util was first released with perl v5.7.3

    ... Besides, if your example code, which included use List::Util 'shuffle'; compiled at all, then you already knew you had it installed properly.

    Oh, there, that's what you're doing wrong: @trips = join(" ", @trips);. You just replaced the contents of the @trips array with a single value, which is a string which joins all the old elements of @trips with a space. I think what you want is more akin to:

    #!/usr/bin/env perl use warnings; use strict; print "enter sequence and signal end with enter followed by ctrl d\n"; my $sequence = <STDIN>; chomp $sequence; print "sequence inserted : $sequence\n"; my @trips = unpack("a3" x (length($sequence)-2), $sequence); local $" = ", "; print "unshuffled: (@trips)\n"; use List::Util 'shuffle'; my @shuffled = shuffle(@trips); print "shuffled: (@shuffled)\n"; __END__
    __RESULTS__ enter sequence and signal end with enter followed by ctrl d GATTACCAT sequence inserted : GATTACCAT unshuffled: (GAT, TAC, CAT, , , , ) shuffled: (, , GAT, TAC, CAT, , )
Re^3: Shuffling codons
by hippo (Archbishop) on Jun 07, 2018 at 14:13 UTC
    If I try the list util it doesnt work

    Not enough information there. Error message? Segfault? Compilation error?

    What is wrong/missing here?

    This line:

    @trips = join(" ", @trips);

    ruins it. What do you understand this line to be doing? Without it I get at least some shuffling which may or may not be what you want:

    #!/usr/bin/env perl use strict; use warnings; use List::Util 'shuffle'; print "enter sequence and signal end with enter followed by ctrl d\n"; my $sequence = <STDIN>; chomp $sequence; print "sequence inserted : $sequence\n"; my @trips = unpack("a3" x (length($sequence)-2), $sequence); my @shuffled = shuffle(@trips); print "@shuffled\n";

    which gives:

    $ perl /tmp/shuf.pl enter sequence and signal end with enter followed by ctrl d aaabbbcccddd sequence inserted : aaabbbcccddd ccc aaa ddd bbb
    I am on perl v5.18.2, so the list utils should already be there you mention?

    Yes.