split_seqs_2.pl 650 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/perl -w
  2. #
  3. #
  4. use strict;
  5. my $cnt = shift or die("Usage: $0 <n_files> <fasta_file> [gzip:0]\n");
  6. my $inf = shift or die("Usage: $0 <n_files> <fasta_file> [gzip:0]\n");
  7. my $zip = shift || 0;
  8. $zip = 0 unless($zip eq '1' or $zip eq 'y');
  9. my $ouf = $inf;
  10. if($ouf eq '-'){
  11. $ouf = 'unnamed';
  12. } else {
  13. unshift @ARGV, $inf;
  14. }
  15. my @fhs = ();
  16. for(my $i=1;$i<=$cnt;$i++){
  17. my $fh;
  18. if($zip){
  19. open($fh, "| gzip -c >$ouf.shuffle$i.gz") or die;
  20. } else {
  21. open($fh, ">", "$ouf.shuffle$i") or die;
  22. }
  23. push(@fhs, $fh);
  24. }
  25. my $n = -1;
  26. while(<>){
  27. $n ++ if(/^>/);
  28. my $fh = $fhs[$n % $cnt];
  29. print $fh $_;
  30. }
  31. foreach my $fh (@fhs) {
  32. close $fh;
  33. }
  34. 1;