3 # Copyright (C) 2013 Genome Research Ltd.
5 # Author: James Bonfield <jkb@sanger.ac.uk>
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 # DEALINGS IN THE SOFTWARE.
25 # Compares two SAM files to report differences.
26 # Optionally can skip header or ignore specific types of diff.
32 GetOptions
(\
%opts, 'noqual', 'noaux', 'notemplate', 'unknownrg', 'nomd', 'template-1', 'noflag', 'Baux');
34 my ($fn1, $fn2) = @ARGV;
35 open(my $fd1, "<", $fn1) || die $!;
36 open(my $fd2, "<", $fn2) || die $!;
40 my (@hd1, @hd2, $ln1, $ln2);
66 while ($ln1 && $ln2) {
70 # Java CRAM adds RG:Z:UNKNOWN when the read-group is absent
71 if (exists $opts{unknownrg
}) {
72 $ln1 =~ s/\tRG:Z:UNKNOWN//;
73 $ln2 =~ s/\tRG:Z:UNKNOWN//;
76 if (exists $opts{nomd
}) {
77 $ln1 =~ s/\tMD:Z:[A-Z0-9^]*//;
78 $ln2 =~ s/\tMD:Z:[A-Z0-9^]*//;
79 $ln1 =~ s/\tNM:i:\d+//;
80 $ln2 =~ s/\tNM:i:\d+//;
83 my @ln1 = split("\t", $ln1);
84 my @ln2 = split("\t", $ln2);
86 # Fix BWA bug: unmapped data should have no alignments
87 if ($ln1[1] & 4) { $ln1[4] = 0; $ln1[5] = "*"; }
88 if ($ln2[1] & 4) { $ln2[4] = 0; $ln2[5] = "*"; }
90 # Canonicalise floating point numbers
91 map {s/^(..):f:(.*)/{"$1:f:".($2+0)}/e} @ln1[11..$#ln1];
92 map {s/^(..):f:(.*)/{"$1:f:".($2+0)}/e} @ln2[11..$#ln2];
95 if (exists $opts{Baux
}) {
96 # Turn ??:H:<hex> into ??:B:c,<vals> so we can compare
97 # Cramtools.jar vs htslib encodings. Probably doable with (un)pack
98 map {s/^(..):H:(.*)/{join(",", "$1:B:C", map {hex $_} $2=~m:..:g)}/e} @ln1[11..$#ln1];
99 map {s/^(..):H:(.*)/{join(",", "$1:B:C", map {hex $_} $2=~m:..:g)}/e} @ln2[11..$#ln2];
101 # Canonicalise ??:B:? data series to be unsigned
102 map {s/^(..):B:c(,?)(.*)/{"$1:B:C$2".join(",",map {($_+256)&255} split(",",$3))}/e} @ln1[11..$#ln1];
103 map {s/^(..):B:c(,?)(.*)/{"$1:B:C$2".join(",",map {($_+256)&255} split(",",$3))}/e} @ln2[11..$#ln2];
105 map {s/^(..):B:s(,?)(.*)/{"$1:B:S$2".join(",",map {($_+65536)&65535} split(",",$3))}/e} @ln1[11..$#ln1];
106 map {s/^(..):B:s(,?)(.*)/{"$1:B:S$2".join(",",map {($_+65536)&65535} split(",",$3))}/e} @ln2[11..$#ln2];
108 map {s/^(..):B:i(,?)(.*)/{"$1:B:I$2".join(",",map {$_<0? ($_+4294967296) : $_} split(",",$3))}/e} @ln1[11..$#ln1];
109 map {s/^(..):B:i(,?)(.*)/{"$1:B:I$2".join(",",map {$_<0? ($_+4294967296) : $_} split(",",$3))}/e} @ln2[11..$#ln2];
112 # Rationalise order of auxiliary fields
113 if (exists $opts{noaux
}) {
117 #my @a=@ln1[11..$#ln1];print "<<<@a>>>\n";
118 @ln1[11..$#ln1] = sort @ln1[11..$#ln1];
119 @ln2[11..$#ln2] = sort @ln2[11..$#ln2];
122 if (exists $opts{noqual
}) {
127 if (exists $opts{notemplate
}) {
128 @ln1[6..8] = qw
/* 0 0/;
129 @ln2[6..8] = qw
/* 0 0/;
132 if (exists $opts{noflag
}) {
133 $ln1[1] = 0; $ln2[1] = 0;
136 if (exists $opts{'template-1'}) {
137 if (abs($ln1[8] - $ln2[8]) == 1) {
142 # Cram doesn't uppercase the reference
143 $ln1[9] = uc($ln1[9]);
144 $ln2[9] = uc($ln2[9]);
146 # Cram will populate a sequence string that starts as "*"
147 $ln2[9] = "*" if ($ln1[9] eq "*");
149 # Fix 0<op> cigar fields
150 $ln1[5] =~ s/(\D|^)0\D/$1/g;
152 $ln2[5] =~ s/(\D|^)0\D/$1/g;
155 # Fix 10M10M cigar to 20M
156 $ln1[5] =~ s/(\d+)(\D)(\d+)(\2)/$1+$3.$2/e;
157 $ln2[5] =~ s/(\d+)(\D)(\d+)(\2)/$1+$3.$2/e;
159 if ("@ln1" ne "@ln2") {
160 print "Diff at lines $fn1:$c1, $fn2:$c2\n";
161 my @s1 = split("","@ln1");
162 my @s2 = split("","@ln2");
164 for (my $i=0; $i < $#s1; $i++) {
165 if ($s1[$i] eq $s2[$i]) {
171 print "1\t@ln1\n2\t@ln2\n\t$ptr^\n\n";
182 print "EOF on $fn1\n";
187 print "EOF on $fn2\n";