]> git.donarmstrong.com Git - samtools.git/blob - misc/wgsim_eval.pl
fixed a bug in generating tag AM
[samtools.git] / misc / wgsim_eval.pl
1 #!/usr/bin/perl -w
2
3 # Contact: lh3
4 # Version: 0.1.1
5
6 use strict;
7 use warnings;
8 use Getopt::Std;
9
10 &wgsim_eval;
11 exit;
12
13 sub wgsim_eval {
14   my %opts;
15   getopts('p', \%opts);
16   die("Usage: wgsim_eval.pl [-p] <in.sam>\n") if (@ARGV == 0 && -t STDIN);
17   my (@c0, @c1);
18   my $max_q = 0;
19   while (<>) {
20         my @t = split;
21         my $line = $_;
22         my ($q, $is_correct, $chr, $left, $rght) = (int($t[4]/10), 1, $t[2], $t[3], $t[3]);
23         $max_q = $q if ($q > $max_q);
24         # right coordinate
25         $_ = $t[5]; s/(\d+)[MDN]/$rght+=$1,'x'/eg;
26         --$rght;
27         # correct for soft clipping
28         $left -= $1 if (/^(\d+)S/);
29         $rght += $1 if (/(\d+)S$/);
30         # skip unmapped reads
31         next if (($t[1]&0x4) || $chr eq '*');
32         # parse read name and check
33         ++$c0[$q];
34         if ($t[0] =~ /^(\S+)_(\d+)_(\d+)_[0-9a-f]+(\/[12])?$/) {
35           if ($1 ne $chr) { # different chr
36                 $is_correct = 0;
37           } elsif ($2 < $3) { # should always be true if reads are generated from wgsim
38                 if ($t[1] & 0x10) { # reverse
39                   $is_correct = 0 if (abs($3 - $rght) > 5); # in case of indels that are close to the end of a reads
40                 } else {
41                   $is_correct = 0 if (abs($2 - $left) > 5);
42                 }
43           } else {
44                 die("[wgsim_eval] bug in wgsim?\n");
45           }
46         } else {
47           die("[wgsim_eval] read '$t[0]' was not generated by wgsim?\n");
48         }
49         ++$c1[$q] unless ($is_correct);
50         print $line if (defined($opts{p}) && !$is_correct);
51   }
52   # print
53   my ($cc0, $cc1) = (0, 0);
54   for (my $i = $max_q; $i >= 0; --$i) {
55         $c0[$i] = 0 unless (defined $c0[$i]);
56         $c1[$i] = 0 unless (defined $c1[$i]);
57         $cc0 += $c0[$i]; $cc1 += $c1[$i];
58         printf("%.2dx %12d / %-12d  %12d  %.3e\n", $i, $c1[$i], $c0[$i], $cc0, $cc1/$cc0);
59   }
60 }