stats_chrs.pl 964 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my (@samples,%data,%total,@chrs);
  5. my $outf=shift;
  6. for ('X','Y',1..22){
  7. push @chrs, $_;
  8. }
  9. my @files=<*.idxstats>;
  10. for my $file (@files){
  11. open my $fh, '<', $file or die "Cannot open file: $file $!";
  12. my $sample=$file;
  13. $sample=~s/.idxstats//;
  14. push @samples,$sample;
  15. while(<$fh>){
  16. my ($chr,$map)=(split /\t/)[0,2];
  17. $chr=~s/^chr//;
  18. next if $chr =~/_/;
  19. next if $chr eq '*';
  20. $chr='MT' if $chr eq 'M';
  21. $total{$sample}+=$map unless $chr eq 'MT' or $chr eq 'M';
  22. $data{$sample}{$chr}=$map;
  23. }
  24. close($fh);
  25. }
  26. open my $wfh, '>' , $outf or die "Cannot write file:$!";
  27. print $wfh join("\t",'#Sample',@chrs,'MT (%)','Total'),"\n";
  28. for my $sample(@samples){
  29. print $wfh $sample;
  30. my $tot=$total{$sample};
  31. my $nchrm=$data{$sample}{'MT'};
  32. for my $chr(@chrs){
  33. print $wfh "\t",int($data{$sample}{$chr}/$tot*10000+0.5);
  34. }
  35. print $wfh "\t",sprintf("%.2f",$nchrm/($nchrm+$tot)),"\t",$tot+$nchrm,"\n";
  36. }