Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Random shuffling

by johngg (Canon)
on Jun 20, 2015 at 23:11 UTC ( #1131307=note: print w/replies, xml ) Need Help??

in reply to Random shuffling

As hexcoder says, you only need to read two lines at a time. Rather than using C-style loops to move along the sequence shuffling as you go you could use unpack and map; as BrowserUk has observed, there seems to be no point in shuffling more than once.

use strict; use warnings; use List::Util qw{ shuffle }; my $inFile = shift; open my $inFH, q{<}, $inFile or die qq{open: < $inFile: $!\n}; my $outFile = $inFile . q{_shuffled.fasta}; open my $outFH, q{>}, $outFile or die qq{open: > $outFile: $!\n}; my $window = 10; while ( not eof $inFH ) { my( $id, $seq ) = map { chomp; $_ } map { scalar <$inFH> } 1 .. 2; my $shuffled = join q{}, map { join q{}, shuffle split m{} } unpack qq{(a$window)*}, $seq; print $outFH qq{$id\n$shuffled\n}; } close $inFH or die $!; close $outFH or die $!;

I hope this is of interest.

Update: Corrected cut'n'paste error in script, I was erroneously unpacking $shuffled instead of $seq.



Replies are listed 'Best First'.
Re^2: Random shuffling
by onlyIDleft (Scribe) on Jun 21, 2015 at 00:15 UTC

    Hi JohnGG, please see my UPDATE 1 to the original post, perhaps your answer may change or need modification in light of the additional info I have provided?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1131307]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2020-11-24 20:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found