#!/usr/local/bin/perl # # $Id: fasta-shuffle-letters.pl.in 5026 2010-10-20 07:00:35Z james_johnson $ # $Log$ # Revision 1.1 2005/07/28 23:54:37 nadya # Initial revision # # # AUTHOR: Timothy L. Bailey # CREATE DATE: 12/4/98 $pgm = $0; # name of program $pgm =~ s#.*/##; # remove part up to last slash @args = @ARGV; # arguments to program $| = 1; # flush after all prints $SIG{'INT'} = 'cleanup'; # interrupt handler # Note: so that interrupts work, always use for system calls: # if ($status = system($command)) {&cleanup($status)} # requires push(@INC, split(":", $ENV{'PATH'})); # look in entire path # defaults $usage = <= 0) { $_ = shift; if ($_ eq "-h") { die($usage); } elsif ($_ eq "-tod") { $tod = 1; } else { die($usage); } } # # intialize the random number generator if -tod # if ($tod) { srand(); } else { srand(0); } while () { chop; # remove newline if (/^>/) { # new sequence if ($label) { print &shuffle($label, $seq); } $label = $_; # save label $seq = ""; # initialize sequence } else { # sequence $seq .= $_; # save line of sequence } } # shuffle and print last sequence print &shuffle($label, $seq); ################################################################################ # Subroutines # ################################################################################ sub shuffle { my($label, $seq) = @_; my($out, @chars, $n, $i, %hash, @keys, $out, $x); $out = $label; # put label @chars = split(//, $seq); # split into characters $n = @chars; # Give each position in the sequence a random key and sort the keys for ($i=0; $i<$n; $i++) { do {$x = rand()/(1+rand());} while (defined $hash{"$x"}); $x = rand()/(1+rand()); # only 32768 values for rand! $hash{"$x"} = $chars[$i]; } @keys = sort {$a <=> $b} keys %hash; for ($i=0; $i<$n; $i++) { # shuffle sequence $out .= "\n" if ($i%70 == 0); # split at 70 columns $out .= $hash{$keys[$i]}; # add to shuffled seq } undef %hash; $out .= "\n"; }