]> git.donarmstrong.com Git - samtools.git/blob - misc/wgsim_eval.pl
skip @header lines in SAM
[samtools.git] / misc / wgsim_eval.pl
1 #!/usr/bin/perl -w
2
3 # Contact: lh3
4 # Version: 0.1.4
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('pc', \%opts);
16   die("Usage: wgsim_eval.pl [-pc] <in.sam>\n") if (@ARGV == 0 && -t STDIN);
17   my (@c0, @c1);
18   my ($max_q, $flag) = (0, 0);
19   my $gap = 5;
20   $flag |= 1 if (defined $opts{p});
21   $flag |= 2 if (defined $opts{c});
22   while (<>) {
23         next if (/^\@/);
24         my @t = split;
25         my $line = $_;
26         my ($q, $is_correct, $chr, $left, $rght) = (int($t[4]/10), 1, $t[2], $t[3], $t[3]);
27         $max_q = $q if ($q > $max_q);
28         # right coordinate
29         $_ = $t[5]; s/(\d+)[MDN]/$rght+=$1,'x'/eg;
30         --$rght;
31         # correct for soft clipping
32         $left -= $1 if (/^(\d+)S/);
33         $rght += $1 if (/(\d+)S$/);
34         # skip unmapped reads
35         next if (($t[1]&0x4) || $chr eq '*');
36         # parse read name and check
37         if ($t[0] =~ /^(\S+)_(\d+)_(\d+)_/) {
38           if ($1 ne $chr) { # different chr
39                 $is_correct = 0;
40           } else {
41                 if ($flag & 2) {
42                   if (($t[1]&0x40) && !($t[1]&0x10)) { # F3, forward
43                         $is_correct = 0 if (abs($2 - $left) > $gap);
44                   } elsif (($t[1]&0x40) && ($t[1]&0x10)) { # F3, reverse
45                         $is_correct = 0 if (abs($3 - $rght) > $gap);
46                   } elsif (($t[1]&0x80) && !($t[1]&0x10)) { # R3, forward
47                         $is_correct = 0 if (abs($3 - $left) > $gap);
48                   } else { # R3, reverse
49                         $is_correct = 0 if (abs($2 - $rght) > $gap);
50                   }
51                 } else {
52                   if ($t[1] & 0x10) { # reverse
53                         $is_correct = 0 if (abs($3 - $rght) > $gap); # in case of indels that are close to the end of a reads
54                   } else {
55                         $is_correct = 0 if (abs($2 - $left) > $gap);
56                   }
57                 }
58           }
59         } else {
60           warn("[wgsim_eval] read '$t[0]' was not generated by wgsim?\n");
61           next;
62         }
63         ++$c0[$q];
64         ++$c1[$q] unless ($is_correct);
65         print STDERR $line if (($flag&1) && !$is_correct && $q > 0);
66   }
67   # print
68   my ($cc0, $cc1) = (0, 0);
69   for (my $i = $max_q; $i >= 0; --$i) {
70         $c0[$i] = 0 unless (defined $c0[$i]);
71         $c1[$i] = 0 unless (defined $c1[$i]);
72         $cc0 += $c0[$i]; $cc1 += $c1[$i];
73         printf("%.2dx %12d / %-12d  %12d  %.3e\n", $i, $c1[$i], $c0[$i], $cc0, $cc1/$cc0);
74   }
75 }