Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Random shuffling

by johngg (Canon)
on Jun 20, 2015 at 23:11 UTC ( [id://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.

Cheers,

JohnGG

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found