comparison ezBAMQC/src/htslib/test/compare_sam.pl @ 0:dfa3745e5fd8

Uploaded
author youngkim
date Thu, 24 Mar 2016 17:12:52 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dfa3745e5fd8
1 #!/usr/bin/perl -w
2 #
3 # Copyright (C) 2013 Genome Research Ltd.
4 #
5 # Author: James Bonfield <jkb@sanger.ac.uk>
6 #
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:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
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.
24
25 # Compares two SAM files to report differences.
26 # Optionally can skip header or ignore specific types of diff.
27
28 use strict;
29 use Getopt::Long;
30
31 my %opts;
32 GetOptions(\%opts, 'noqual', 'noaux', 'notemplate', 'unknownrg', 'nomd', 'template-1', 'noflag');
33
34 my ($fn1, $fn2) = @ARGV;
35 open(my $fd1, "<", $fn1) || die $!;
36 open(my $fd2, "<", $fn2) || die $!;
37
38 # Headers
39 my ($c1,$c2)=(1,1);
40 my (@hd1, @hd2, $ln1, $ln2);
41 while (<$fd1>) {
42 if (/^@/) {
43 push(@hd1, $_);
44 } else {
45 $ln1 = $_;
46 last;
47 }
48 $c1++;
49 }
50
51 while (<$fd2>) {
52 if (/^@/) {
53 push(@hd2, $_);
54 } else {
55 $ln2 = $_;
56 last;
57 }
58 $c2++;
59 }
60
61 # FIXME: to do
62 #print "@hd1\n";
63 #print "@hd2\n";
64
65 # Compare lines
66 while ($ln1 && $ln2) {
67 chomp($ln1);
68 chomp($ln2);
69
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//;
74 }
75
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+//;
81 }
82
83 my @ln1 = split("\t", $ln1);
84 my @ln2 = split("\t", $ln2);
85
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] = "*"; }
89
90 # Rationalise order of auxiliary fields
91 if (exists $opts{noaux}) {
92 @ln1 = @ln1[0..10];
93 @ln2 = @ln2[0..10];
94 } else {
95 #my @a=@ln1[11..$#ln1];print "<<<@a>>>\n";
96 @ln1[11..$#ln1] = sort @ln1[11..$#ln1];
97 @ln2[11..$#ln2] = sort @ln2[11..$#ln2];
98 }
99
100 if (exists $opts{noqual}) {
101 $ln1[10] = "*";
102 $ln2[10] = "*";
103 }
104
105 if (exists $opts{notemplate}) {
106 @ln1[6..8] = qw/* 0 0/;
107 @ln2[6..8] = qw/* 0 0/;
108 }
109
110 if (exists $opts{noflag}) {
111 $ln1[1] = 0; $ln2[1] = 0;
112 }
113
114 if (exists $opts{'template-1'}) {
115 if (abs($ln1[8] - $ln2[8]) == 1) {
116 $ln1[8] = $ln2[8];
117 }
118 }
119
120 # Cram doesn't uppercase the reference
121 $ln1[9] = uc($ln1[9]);
122 $ln2[9] = uc($ln2[9]);
123
124 # Cram will populate a sequence string that starts as "*"
125 $ln2[9] = "*" if ($ln1[9] eq "*");
126
127 # Fix 0<op> cigar fields
128 $ln1[5] =~ s/(\D|^)0\D/$1/g;
129 $ln1[5] =~ s/^$/*/g;
130 $ln2[5] =~ s/(\D|^)0\D/$1/g;
131 $ln2[5] =~ s/^$/*/g;
132
133 # Fix 10M10M cigar to 20M
134 $ln1[5] =~ s/(\d+)(\D)(\d+)(\2)/$1+$3.$2/e;
135 $ln2[5] =~ s/(\d+)(\D)(\d+)(\2)/$1+$3.$2/e;
136
137 if ("@ln1" ne "@ln2") {
138 print "Diff at lines $fn1:$c1, $fn2:$c2\n";
139 my @s1 = split("","@ln1");
140 my @s2 = split("","@ln2");
141 my $ptr = "";
142 for (my $i=0; $i < $#s1; $i++) {
143 if ($s1[$i] eq $s2[$i]) {
144 $ptr .= "-";
145 } else {
146 last;
147 }
148 }
149 print "1\t@ln1\n2\t@ln2\n\t$ptr^\n\n";
150 exit(1);
151 }
152
153 $ln1 = <$fd1>;
154 $ln2 = <$fd2>;
155
156 $c1++; $c2++;
157 }
158
159 if (defined($ln1)) {
160 print "EOF on $fn1\n";
161 exit(1);
162 }
163
164 if (defined($ln2)) {
165 print "EOF on $fn2\n";
166 exit(1);
167 }
168
169 close($fd1);
170 close($fd2);
171
172 exit(0);