10 open my $in, "-|", "samtools view -f 64 -F 1796 $file" or die "Error opening $file: $!\n";
12 my @align = split /\t/;
13 my @match = $align[5] =~ /(\d+)M/g;
15 $sum_match += $_ foreach @match;
16 next if $sum_match < 64;
17 @match = sort {$b <=> $a} @match;
18 next if $match[0] < 30;
19 if ($align[1] & 0x10) {
20 next if $align[5] =~ /[NHP=X]/;
21 next unless $align[5] =~ /(\d+)M$/;
23 my @del = $align[5] =~ /(\d+)D/g;
25 $sum_del += $_ foreach @del;
26 my $c = $align[3] + $sum_match + $sum_del - 1;
27 $count{$align[2]}{$c}[0] = 1;
29 next unless $align[5] =~ /^(\d+)M/;
31 $count{$align[2]}{$align[3]+3}[1] = 1;
36 print STDERR
"$t\tCount file $file complete!\n";
41 my %thread; #Creat a hash of threads.
44 $thread{$_} = threads
->new (\
&SiteCover
, $_);
47 print STDERR
"$t\tDestribute threads completed!\n";
50 my %all; #Threads return a hash of all individual's coverage.
51 foreach (keys %thread) {
52 $all{$_} = $thread{$_}->join;
55 print STDERR
"$t\tRead all file completed!\n";
58 my (%stat_site, %stat_indiv); #Count sites' coverage and individuals' coverage.
59 foreach my $i (keys %all) {
62 foreach my $s (keys %{$all{$i}}) {
63 foreach my $c (keys %{$all{$i}{$s}}) {
64 $stat_site{$s}{$c}[0] = 0 unless defined $stat_site{$s}{$c}[0];
65 $stat_site{$s}{$c}[1] = 0 unless defined $stat_site{$s}{$c}[1];
66 if (defined $all{$i}{$s}{$c}[0]) {
68 $stat_site{$s}{$c}[0] = $stat_site{$s}{$c}[0] | ($all{$i}{$s}{$c}[0] <<= $bit);
70 $all{$i}{$s}{$c}[0] = 0;
72 if (defined $all{$i}{$s}{$c}[1]) {
74 $stat_site{$s}{$c}[1] = $stat_site{$s}{$c}[1] | ($all{$i}{$s}{$c}[1] <<= $bit);
76 $all{$i}{$s}{$c}[1] = 0;
78 $all{$i}{$s}{$c}[2] = $all{$i}{$s}{$c}[0] | $all{$i}{$s}{$c}[1];
79 $all{$i}{$s}{$c}[3] = $all{$i}{$s}{$c}[0] & $all{$i}{$s}{$c}[1];
80 ++$stat_indiv{$i}[2] if $all{$i}{$s}{$c}[2];
81 ++$stat_indiv{$i}[3] if $all{$i}{$s}{$c}[3];
86 print STDERR
"$t\tDo stastistics completed!\n";
89 my @stat_time; #Print site coverage and count coverage of times.
90 open Osite
, ">", "PstIcoverage.lst";
91 print Osite
"#SCAFFOLD\tCOORDINATE\tREVERSE\tFORWARD\tUNION\tINTERSECTION\n";
92 foreach my $s (sort keys %stat_site) {
93 foreach my $c (sort {$a <=> $b} keys %{$stat_site{$s}}) {
94 $stat_site{$s}{$c}[2] = $stat_site{$s}{$c}[1] | $stat_site{$s}{$c}[0];
95 $stat_site{$s}{$c}[3] = $stat_site{$s}{$c}[1] & $stat_site{$s}{$c}[0];
96 printf Osite
"%s\t%d\t%018b\t%018b\t%018b\t%018b\n", $s, $c, @
{$stat_site{$s}{$c}};
97 foreach my $r (0..3) {
98 my @n = split //, sprintf("%b", $stat_site{$s}{$c}[$r]);
101 ++$stat_time[$n][$r];
107 print STDERR
"$t\tPrint to PstIcoverage.lst completed!\n";
110 open Oindiv
, ">", "PstIindivCoverage.lst"; #Print individual and time coverage.
111 print Oindiv
"#INDIV.\tREVERSE\tFORWARD\tUNION\tINTERSECTION\n";
112 foreach my $i (sort keys %stat_indiv) {
113 printf Oindiv
"%s\t%d\t%d\t%d\t%d\n", $i, @
{$stat_indiv{$i}};
115 print Oindiv
"#TIME\tREVERSE\tFORWARD\tUNION\tINTERSECTION\tTIME\tREVERSE\tFORWARD\tUNION\tINTERSECTION\n";
117 foreach my $n1 (1..18) {
118 foreach my $n2 (0..3) {
119 foreach my $n3 ($n1..18) {
120 $accum[$n1][$n2] += $stat_time[$n3][$n2];
123 printf Oindiv
"%s\t%d\t%d\t%d\t%d\t%s\t%d\t%d\t%d\t%d\n", ">=$n1", @
{$accum[$n1]}, "=$n1", @
{$stat_time[$n1]};
127 print STDERR
"$t\tPrint to PstIindivCoverage.lst completed!\n";
131 print STDERR
"$t\tDone!\n";