#!/usr/bin/perl use strict; use warnings; my (@samples,%data,%total,@chrs); my $outf=shift; for ('X','Y',1..22){ push @chrs, $_; } my @files=<*.idxstats>; for my $file (@files){ open my $fh, '<', $file or die "Cannot open file: $file $!"; my $sample=$file; $sample=~s/.idxstats//; push @samples,$sample; while(<$fh>){ my ($chr,$map)=(split /\t/)[0,2]; $chr=~s/^chr//; next if $chr =~/_/; next if $chr eq '*'; $chr='MT' if $chr eq 'M'; $total{$sample}+=$map unless $chr eq 'MT' or $chr eq 'M'; $data{$sample}{$chr}=$map; } close($fh); } open my $wfh, '>' , $outf or die "Cannot write file:$!"; print $wfh join("\t",'#Sample',@chrs,'MT (%)','Total'),"\n"; for my $sample(@samples){ print $wfh $sample; my $tot=$total{$sample}; my $nchrm=$data{$sample}{'MT'}; for my $chr(@chrs){ print $wfh "\t",int($data{$sample}{$chr}/$tot*10000+0.5); } print $wfh "\t",sprintf("%.2f",$nchrm/($nchrm+$tot)),"\t",$tot+$nchrm,"\n"; }