5 die "Usage: $0 <sorted_bam> <outprefix>\n" if @ARGV != 2;
8 open( HEAD
,"-|","samtools view -H $in") or die "Error opening $in: $!\n";
9 my ($LenT,@Heads,@ChrID,%ChrLen,%ChrFlag,%ChrStat);
12 if (/\@SQ\tSN:(\S+)\tLN:(\d+)/) {
23 $ChrLen{'*'} = -$LenT;
26 open LOG
,'>',"$out.log" or die "Error opening $out.log: $!\n";
29 open( OUT
,"|-","samtools view -bS - > $out.$outChr.bam") or die "Error opening $out.$outChr.bam: $!\n";
30 print OUT
"\@SQ\tSN:$outChr\tLN:$ChrLen{$outChr}\n",join("\n",@Heads),"\n";
33 my $outChr = $ChrID[0];
34 $ChrFlag{$outChr} = 1;
37 open( IN
,"-|","samtools view $in") or die "Error opening $in: $!\n";
39 my $chr=(split /\t/)[2];
43 next; # unmap are skipped outputing
45 if ($chr eq $outChr) {
48 $ChrFlag{$outChr} = 1;
50 die "File not sorted:[$chr] was before [$outChr].\n" if $ChrFlag{$chr};
59 print LOG
"ChrID\tLen\tLenRatio\tCount\tRatio\tReletiveRatio\n";
60 for my $chr (@ChrID,'*') {
61 print LOG
join("\t",$chr,$ChrLen{$chr},$ChrLen{$chr}/$LenT,$ChrStat{$chr},$ChrStat{$chr}/$ChrStat{"\t"},($ChrStat{$chr}/$ChrStat{"\t"})/($ChrLen{$chr}/$LenT)),"\n";