seq_n50.pl 932 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/perl -w
  2. #
  3. # Author: Ruan Jue
  4. #
  5. use strict;
  6. use Getopt::Std;
  7. our ($opt_h, $opt_s);
  8. getopts("hs");
  9. my $total = 0;
  10. my @nums = ();
  11. my $len = 0;
  12. while(<>){
  13. if(/^>(\S+)/){
  14. if($opt_s){
  15. print "\t$len\n" if($len);
  16. print $1;
  17. }
  18. push(@nums, $len),$total+=$len if($len);
  19. $len = 0;
  20. } else {
  21. $len += length($_) - 1;
  22. }
  23. }
  24. print "\t$len\n" if($opt_s and $len);
  25. push(@nums, $len),$total+=$len if($len);
  26. my $n_seq = @nums;
  27. my $avg = sprintf("%0.2f", $total / $n_seq);
  28. print "Total: $total\n";
  29. print "Count: $n_seq\n";
  30. print "Average: $avg\n";
  31. my @nxxs = ();
  32. for(my $i=0;$i<=10;$i++){
  33. push(@nxxs, int($total*$i*0.1));
  34. }
  35. push(@nxxs, $total + 1);
  36. my $i = 0;
  37. my $j = 0;
  38. @nums = sort {$b <=> $a} @nums;
  39. my $median = $nums[int($n_seq / 2)];
  40. print "Median: $median\n";
  41. $len = 0;
  42. for(;$i<@nums;$i++){
  43. $len += $nums[$i];
  44. while($nxxs[$j] <= $len){
  45. print "N".$j."0: $nums[$i]\t". ($i + 1) . "\n";
  46. $j ++;
  47. }
  48. }
  49. 1;