annotate PsiCLASS-1.0.2/samtools-0.1.19/misc/samtools.pl @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #!/usr/bin/perl -w
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 # Author: lh3
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 use strict;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 use warnings;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 use Getopt::Std;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 my $version = '0.3.3';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 &usage if (@ARGV < 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 my $command = shift(@ARGV);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter, plp2vcf=>\&plp2vcf,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 unique=>\&unique, uniqcmp=>\&uniqcmp, sra2hdr=>\&sra2hdr, sam2fq=>\&sam2fq);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 die("Unknown command \"$command\".\n") if (!defined($func{$command}));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 &{$func{$command}};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 exit(0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 # showALEN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 sub showALEN {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 die(qq/Usage: samtools.pl showALEN <in.sam>\n/) if (@ARGV == 0 && -t STDIN);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 next if (/^\@/ || @t < 11);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 my $l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 $_ = $t[5];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 s/(\d+)[MI]/$l+=$1/eg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 print join("\t", @t[0..5]), "\t$l\t", join("\t", @t[6..$#t]), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 # varFilter
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 # Filtration code:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 # d low depth
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 # D high depth
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 # W too many SNPs in a window (SNP only)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 # G close to a high-quality indel (SNP only)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 # Q low RMS mapping quality (SNP only)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 # g close to another indel with higher quality (indel only)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 # s low SNP quality (SNP only)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 # i low indel quality (indel only)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 sub varFilter {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 my %opts = (d=>3, D=>100, l=>30, Q=>25, q=>10, G=>25, s=>100, w=>10, W=>10, N=>2, p=>undef, S=>'', i=>'');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 getopts('pq:d:D:l:Q:w:W:N:G:S:i:', \%opts);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 die(qq/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 Usage: samtools.pl varFilter [options] <in.cns-pileup>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 Options: -Q INT minimum RMS mapping quality for SNPs [$opts{Q}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 -q INT minimum RMS mapping quality for gaps [$opts{q}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 -d INT minimum read depth [$opts{d}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 -D INT maximum read depth [$opts{D}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 -S INT minimum SNP quality [$opts{S}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 -i INT minimum indel quality [$opts{i}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 -G INT min indel score for nearby SNP filtering [$opts{G}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 -w INT SNP within INT bp around a gap to be filtered [$opts{w}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 -W INT window size for filtering dense SNPs [$opts{W}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 -N INT max number of SNPs in a window [$opts{N}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 -l INT window size for filtering adjacent gaps [$opts{l}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 -p print filtered variants
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 \n/) if (@ARGV == 0 && -t STDIN);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 # calculate the window size
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 my ($ol, $ow, $oW) = ($opts{l}, $opts{w}, $opts{W});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 my $max_dist = $ol > $ow? $ol : $ow;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 $max_dist = $oW if ($max_dist < $oW);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 # the core loop
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 my @staging; # (indel_filtering_score, flt_tag)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 next if (uc($t[2]) eq uc($t[3]) || $t[3] eq '*/*'); # skip non-var sites
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 # clear the out-of-range elements
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 while (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 # Still on the same chromosome and the first element's window still affects this position?
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 last if ($staging[0][3] eq $t[0] && $staging[0][4] + $staging[0][2] + $max_dist >= $t[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 my ($flt, $score) = (0, -1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 # first a simple filter
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 if ($t[7] < $opts{d}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 $flt = 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 } elsif ($t[7] > $opts{D}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 $flt = 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 if ($t[2] eq '*') { # an indel
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 if ($opts{i} && $opts{i}>$t[5]) { $flt = 8; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 elsif ($opts{S} && $opts{S}>$t[5]) { $flt = 7; } # SNP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 # site dependent filters
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 my $len=0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 if ($flt == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 if ($t[2] eq '*') { # an indel
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 # If deletion, remember the length of the deletion
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 my ($a,$b) = split(m{/},$t[3]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 my $alen = length($a) - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 my $blen = length($b) - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 if ( $alen>$blen )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 if ( substr($a,0,1) eq '-' ) { $len=$alen; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 elsif ( substr($b,0,1) eq '-' ) { $len=$blen; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 $flt = 1 if ($t[6] < $opts{q});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 # filtering SNPs
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 if ($t[5] >= $opts{G}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 for my $x (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 # Is it a SNP and is it outside the SNP filter window?
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 next if ($x->[0] >= 0 || $x->[4] + $x->[2] + $ow < $t[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 $x->[1] = 5 if ($x->[1] == 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 # calculate the filtering score (different from indel quality)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 $score = $t[5];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 $score += $opts{s} * $t[10] if ($t[8] ne '*');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 $score += $opts{s} * $t[11] if ($t[9] ne '*');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 # check the staging list for indel filtering
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 for my $x (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 # Is it a SNP and is it outside the gap filter window
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 next if ($x->[0] < 0 || $x->[4] + $x->[2] + $ol < $t[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 if ($x->[0] < $score) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 $x->[1] = 6;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 $flt = 6; last;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 } else { # a SNP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 $flt = 1 if ($t[6] < $opts{Q});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 # check adjacent SNPs
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 my $k = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 for my $x (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 ++$k if ($x->[0] < 0 && $x->[4] + $x->[2] + $oW >= $t[1] && ($x->[1] == 0 || $x->[1] == 4 || $x->[1] == 5));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 # filtering is necessary
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 if ($k > $opts{N}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 $flt = 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 for my $x (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 $x->[1] = 4 if ($x->[0] < 0 && $x->[4] + $x->[2] + $oW >= $t[1] && $x->[1] == 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 } else { # then check gap filter
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 for my $x (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 next if ($x->[0] < 0 || $x->[4] + $x->[2] + $ow < $t[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 if ($x->[0] >= $opts{G}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 $flt = 5; last;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 push(@staging, [$score, $flt, $len, @t]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 # output the last few elements in the staging list
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 while (@staging) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 varFilter_aux(shift @staging, $opts{p});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 sub varFilter_aux {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 my ($first, $is_print) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 if ($first->[1] == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 print join("\t", @$first[3 .. @$first-1]), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 } elsif ($is_print) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 print STDERR join("\t", substr("UQdDWGgsiX", $first->[1], 1), @$first[3 .. @$first-1]), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 # pileup2fq
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 sub pileup2fq {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 my %opts = (d=>3, D=>255, Q=>25, G=>25, l=>10);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 getopts('d:D:Q:G:l:', \%opts);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 die(qq/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 Usage: samtools.pl pileup2fq [options] <in.cns-pileup>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 Options: -d INT minimum depth [$opts{d}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 -D INT maximum depth [$opts{D}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 -Q INT min RMS mapQ [$opts{Q}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 -G INT minimum indel score [$opts{G}]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 -l INT indel filter winsize [$opts{l}]\n
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 /) if (@ARGV == 0 && -t STDIN);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 my ($last_chr, $seq, $qual, @gaps, $last_pos);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 my $_Q = $opts{Q};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 my $_d = $opts{d};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 my $_D = $opts{D};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 $last_chr = '';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 if ($last_chr ne $t[0]) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l}) if ($last_chr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 $last_chr = $t[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 $last_pos = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 $seq = ''; $qual = '';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 @gaps = ();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 if ($t[1] - $last_pos != 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 $seq .= 'n' x ($t[1] - $last_pos - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 $qual .= '!' x ($t[1] - $last_pos - 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 if ($t[2] eq '*') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 push(@gaps, $t[1]) if ($t[5] >= $opts{G});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 $seq .= ($t[6] >= $_Q && $t[7] >= $_d && $t[7] <= $_D)? uc($t[3]) : lc($t[3]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 my $q = $t[4] + 33;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 $q = 126 if ($q > 126);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 $qual .= chr($q);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 $last_pos = $t[1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 &p2q_post_process($last_chr, \$seq, \$qual, \@gaps, $opts{l});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 sub p2q_post_process {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 my ($chr, $seq, $qual, $gaps, $l) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 &p2q_filter_gaps($seq, $gaps, $l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 print "\@$chr\n"; &p2q_print_str($seq);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 print "+\n"; &p2q_print_str($qual);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 sub p2q_filter_gaps {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 my ($seq, $gaps, $l) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 for my $g (@$gaps) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 my $x = $g > $l? $g - $l : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 substr($$seq, $x, $l + $l) = lc(substr($$seq, $x, $l + $l));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 sub p2q_print_str {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 my ($s) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 my $l = length($$s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 for (my $i = 0; $i < $l; $i += 60) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 print substr($$s, $i, 60), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 # sam2fq
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 sub sam2fq {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 my %opts = (n=>20, p=>'');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 getopts('n:p:', \%opts);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 die("Usage: samtools.pl sam2fq [-n 20] [-p <prefix>] <inp.sam>\n") if (@ARGV == 0 && -t STDIN);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 if ($opts{p} && $opts{n} > 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 my $pre = $opts{p};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 my @fh;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 for (0 .. $opts{n}-1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 open($fh[$_], sprintf("| gzip > $pre.%.3d.fq.gz", $_)) || die;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 my $i = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 next if (/^@/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 chomp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 my @t = split("\t");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 next if ($t[9] eq '*');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 my ($name, $seq, $qual);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 if ($t[1] & 16) { # reverse strand
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 $seq = reverse($t[9]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 $qual = reverse($t[10]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 $seq =~ tr/ACGTacgt/TGCAtgca/;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 ($seq, $qual) = @t[9,10];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 $name = $t[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 $name .= "/1" if ($t[1] & 0x40);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 $name .= "/2" if ($t[1] & 0x80);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 print {$fh[$i]} "\@$name\n$seq\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 if ($qual ne '*') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 print {$fh[$i]} "+\n$qual\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 $i = 0 if (++$i == $opts{n});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 close($fh[$_]) for (0 .. $opts{n}-1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 die("To be implemented.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 # sra2hdr
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 # This subroutine does not use an XML parser. It requires that the SRA
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 # XML files are properly formated.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 sub sra2hdr {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 my %opts = ();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 getopts('', \%opts);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 die("Usage: samtools.pl sra2hdr <SRA.prefix>\n") if (@ARGV == 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 my $pre = $ARGV[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 my $fh;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 # read sample
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 my $sample = 'UNKNOWN';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 open($fh, "$pre.sample.xml") || die;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 while (<$fh>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 $sample = $1 if (/<SAMPLE.*alias="([^"]+)"/i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 close($fh);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 # read experiment
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 my (%exp2lib, $exp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 open($fh, "$pre.experiment.xml") || die;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 while (<$fh>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 if (/<EXPERIMENT.*accession="([^\s"]+)"/i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 $exp = $1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 } elsif (/<LIBRARY_NAME>\s*(\S+)\s*<\/LIBRARY_NAME>/i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 $exp2lib{$exp} = $1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 close($fh);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 # read run
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 my ($run, @fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 open($fh, "$pre.run.xml") || die;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 while (<$fh>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 if (/<RUN.*accession="([^\s"]+)"/i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 $run = $1; @fn = ();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 } elsif (/<EXPERIMENT_REF.*accession="([^\s"]+)"/i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 print "\@RG\tID:$run\tSM:$sample\tLB:$exp2lib{$1}\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 } elsif (/<FILE.*filename="([^\s"]+)"/i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 push(@fn, $1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 } elsif (/<\/RUN>/i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 if (@fn == 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 print STDERR "$fn[0]\t$run\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 for (0 .. $#fn) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 print STDERR "$fn[$_]\t$run", "_", $_+1, "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 close($fh);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 # unique
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 sub unique {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 my %opts = (f=>250.0, q=>5, r=>2, a=>1, b=>3);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 getopts('Qf:q:r:a:b:m', \%opts);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 die("Usage: samtools.pl unique [-f $opts{f}] <in.sam>\n") if (@ARGV == 0 && -t STDIN);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 my $last = '';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 my $recal_Q = !defined($opts{Q});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 my $multi_only = defined($opts{m});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 my @a;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 my $score = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 print $_ if (/^\@/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 $score = $1 if (/AS:i:(\d+)/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 my @t = split("\t");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 next if (@t < 11);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 if ($score < 0) { # AS tag is unavailable
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 my $cigar = $t[5];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 my ($mm, $go, $ge) = (0, 0, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 $cigar =~ s/(\d+)[ID]/++$go,$ge+=$1/eg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 $cigar = $t[5];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 $cigar =~ s/(\d+)M/$mm+=$1/eg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 $score = $mm * $opts{a} - $go * $opts{q} - $ge * $opts{r}; # no mismatches...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 $score = 1 if ($score < 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 if ($t[0] ne $last) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 &unique_aux(\@a, $opts{f}, $recal_Q, $multi_only) if (@a);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 $last = $t[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 push(@a, [$score, \@t]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 &unique_aux(\@a, $opts{f}, $recal_Q, $multi_only) if (@a);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 sub unique_aux {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 my ($a, $fac, $is_recal, $multi_only) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 my ($max, $max2, $max_i) = (0, 0, -1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 for (my $i = 0; $i < @$a; ++$i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 if ($a->[$i][0] > $max) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 $max2 = $max; $max = $a->[$i][0]; $max_i = $i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 } elsif ($a->[$i][0] > $max2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 $max2 = $a->[$i][0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 if ($is_recal) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 if (!$multi_only || @$a > 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 my $q = int($fac * ($max - $max2) / $max + .499);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 $q = 250 if ($q > 250);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 $a->[$max_i][1][4] = $q < 250? $q : 250;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 print join("\t", @{$a->[$max_i][1]});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 @$a = ();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 # uniqcmp: compare two SAM files
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 sub uniqcmp {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 my %opts = (q=>10, s=>100);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 getopts('pq:s:', \%opts);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 die("Usage: samtools.pl uniqcmp <in1.sam> <in2.sam>\n") if (@ARGV < 2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 my ($fh, %a);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 warn("[uniqcmp] read the first file...\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 &uniqcmp_aux($ARGV[0], \%a, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 warn("[uniqcmp] read the second file...\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 &uniqcmp_aux($ARGV[1], \%a, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 warn("[uniqcmp] stats...\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 my @cnt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 $cnt[$_] = 0 for (0..9);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 for my $x (keys %a) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 my $p = $a{$x};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 my $z;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 if (defined($p->[0]) && defined($p->[1])) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 $z = ($p->[0][0] == $p->[1][0] && $p->[0][1] eq $p->[1][1] && abs($p->[0][2] - $p->[1][2]) < $opts{s})? 0 : 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 if ($p->[0][3] >= $opts{q} && $p->[1][3] >= $opts{q}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 ++$cnt[$z*3+0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 } elsif ($p->[0][3] >= $opts{q}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 ++$cnt[$z*3+1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 } elsif ($p->[1][3] >= $opts{q}) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 ++$cnt[$z*3+2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 print STDERR "$x\t$p->[0][1]:$p->[0][2]\t$p->[0][3]\t$p->[0][4]\t$p->[1][1]:$p->[1][2]\t$p->[1][3]\t$p->[1][4]\t",
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 $p->[0][5]-$p->[1][5], "\n" if ($z && defined($opts{p}) && ($p->[0][3] >= $opts{q} || $p->[1][3] >= $opts{q}));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 } elsif (defined($p->[0])) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 ++$cnt[$p->[0][3]>=$opts{q}? 6 : 7];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 print STDERR "$x\t$p->[0][1]:$p->[0][2]\t$p->[0][3]\t$p->[0][4]\t*\t0\t*\t",
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 $p->[0][5], "\n" if (defined($opts{p}) && $p->[0][3] >= $opts{q});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 print STDERR "$x\t*\t0\t*\t$p->[1][1]:$p->[1][2]\t$p->[1][3]\t$p->[1][4]\t",
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 -$p->[1][5], "\n" if (defined($opts{p}) && $p->[1][3] >= $opts{q});
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 ++$cnt[$p->[1][3]>=$opts{q}? 8 : 9];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 print "Consistent (high, high): $cnt[0]\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446 print "Consistent (high, low ): $cnt[1]\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 print "Consistent (low , high): $cnt[2]\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 print "Inconsistent (high, high): $cnt[3]\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 print "Inconsistent (high, low ): $cnt[4]\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 print "Inconsistent (low , high): $cnt[5]\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 print "Second missing (high): $cnt[6]\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 print "Second missing (low ): $cnt[7]\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 print "First missing (high): $cnt[8]\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 print "First missing (low ): $cnt[9]\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 sub uniqcmp_aux {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 my ($fn, $a, $which) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 my $fh;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 $fn = "samtools view $fn |" if ($fn =~ /\.bam/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 open($fh, $fn) || die;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 while (<$fh>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 next if (@t < 11);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 # my $l = ($t[5] =~ /^(\d+)S/)? $1 : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 my $l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 my ($x, $nm) = (0, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468 $nm = $1 if (/NM:i:(\d+)/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 $_ = $t[5];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470 s/(\d+)[MI]/$x+=$1/eg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 @{$a->{$t[0]}[$which]} = (($t[1]&0x10)? 1 : 0, $t[2], $t[3]-$l, $t[4], "$x:$nm", $x - 4 * $nm);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 close($fh);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476 sub plp2vcf {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477 while (<>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 my @t = split;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479 next if ($t[3] eq '*/*');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480 if ($t[2] eq '*') { # indel
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481 my @s = split("/", $t[3]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482 my (@a, @b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 my ($ref, $alt);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 for (@s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485 next if ($_ eq '*');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486 if (/^-/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
487 push(@a, 'N'.substr($_, 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
488 push(@b, 'N');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
489 } elsif (/^\+/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
490 push(@a, 'N');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
491 push(@b, 'N'.substr($_, 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
492 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
493 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
494 if ($a[0] && $a[1]) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
495 if (length($a[0]) < length($a[1])) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
496 $ref = $a[1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
497 $alt = ($b[0] . ('N' x (length($a[1]) - length($a[0])))) . ",$b[1]";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
498 } elsif (length($a[0]) > length($a[1])) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
499 $ref = $a[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
500 $alt = ($b[1] . ('N' x (length($a[0]) - length($a[1])))) . ",$b[0]";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
501 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
502 $ref = $a[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
503 $alt = ($b[0] eq $b[1])? $b[0] : "$b[0],$b[1]";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
504 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
505 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
506 $ref = $a[0]; $alt = $b[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
507 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
508 print join("\t", @t[0,1], '.', $ref, $alt, $t[5], '.', '.'), "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
509 } else { # SNP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
510 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
511 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
512 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
513
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
514 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
515 # Usage
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
516 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
517
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
518 sub usage {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
519 die(qq/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
520 Program: samtools.pl (helper script for SAMtools)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
521 Version: $version
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
522 Contact: Heng Li <lh3\@sanger.ac.uk>\n
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
523 Usage: samtools.pl <command> [<arguments>]\n
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
524 Command: varFilter filtering SNPs and short indels
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
525 pileup2fq generate fastq from `pileup -c'
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
526 showALEN print alignment length (ALEN) following CIGAR
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
527 \n/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
528 }