-
Notifications
You must be signed in to change notification settings - Fork 0
/
sam2cfq.pl
executable file
·199 lines (162 loc) · 4.17 KB
/
sam2cfq.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env perl
=head1 Author
Dinghua Li <[email protected]>
=head1 Usage
sam2cfq.pl [options] <in.sam|bam> >out.{fq|lsam}
=head1 Options
-A <int> match score [1]
-B <int> mismatch penalty [2]
-O <int> gap open penalty [3]
-E <int> gap extension penalty [1]
-d <float> dropout ratio [0.95]
-l output lsam
=cut
use warnings;
use strict;
use Getopt::Long;
my ($A, $B, $O, $E) = (1, 2, 3, 1);
my $dropout = 0.95;
my $lsam;
GetOptions(
"A=i" => \$A,
"B=i" => \$B,
"O=i" => \$O,
"E=i" => \$E,
"d=f" => \$dropout,
"l" => \$lsam
);
unless (@ARGV == 1) {
die `pod2text $0`;
}
my $sam_fn = $ARGV[0];
open(SAM, "samtools view -F0x100 $sam_fn 2>/dev/null |") or die "Fail to open $sam_fn with samtools";
while (my $line = <SAM>) {
my ($query, $flag, $max_score, $seq, $qual, @aln) = getAln($line);
$line = <SAM>;
my ($query2, $flag2, $max_score2, $seq2, $qual2, @aln2) = getAln($line);
my %all_ref = collectRef(@aln);
my %all_ref2 = collectRef(@aln2);
my (@pe_aln, @pe_aln2);
$max_score = 0;
$max_score2 = 0;
while (@aln) {
my $s = shift @aln;
my $g = shift @aln;
if (exists $all_ref2{$g}) {
$s += $all_ref2{$g};
}
$max_score = $s if $s > $max_score;
push @pe_aln, $s, fetchTaxid($g);
}
while (@aln2) {
my $s = shift @aln2;
my $g = shift @aln2;
if (exists $all_ref{$g}) {
$s += $all_ref{$g};
}
$max_score2 = $s if $s > $max_score2;
push @pe_aln2, $s, fetchTaxid($g);
}
my (%final_aln, %final_aln2);
while (@pe_aln) {
my $s = shift @pe_aln;
my $g = shift @pe_aln;
$final_aln{$g} = $s if $s >= $max_score * $dropout;
}
while (@pe_aln2) {
my $s = shift @pe_aln2;
my $g = shift @pe_aln2;
$final_aln2{$g} = $s if $s >= $max_score2 * $dropout;
}
print join("\t", $query, $flag, $max_score, $seq, $qual, join(';', map{ "$final_aln{$_},$_" } (sort keys %final_aln)));
print "*" unless keys %final_aln;
print "\n";
print join("\t", $query2, $flag2, $max_score2, $seq2, $qual2, join(';', map{ "$final_aln2{$_},$_" } (sort keys %final_aln2)));
print "*" unless keys %final_aln2;
print "\n";
}
sub collectRef {
my @aln = @_;
my %ret;
while (@aln) {
my $s = shift @aln;
my $g = shift @aln;
$ret{$g} = $s if (!exists $ret{$g} or $ret{$g} < $s);
}
return %ret;
}
sub getAln {
my ($line) = @_;
chomp $line;
my ($query, $flag, $ref, undef, undef, $cigar, undef, undef, undef, $seq, $qual, @tags) = split "\t", $line;
if ($flag & 0x10) {
$seq = rev_comp($seq);
$qual = reverse($qual);
}
my @aln = ();
my $max_score = -99999999;
my $ed = -1;
my $mm = 0;
foreach my $tag (@tags) {
if ($tag =~ /^NM:i:(\d+)$/) {
$ed = $1;
} elsif ($tag =~ /^XA:Z:(.+)$/) {
my @xa = split ";", $1;
foreach my $a (@xa) {
my @rec = split ",", $a;
my @indel_rec = calc_indel($rec[2]);
my $score = $indel_rec[2] * $A - ($rec[3] - $indel_rec[0]) * ($A + $B) - $indel_rec[1];
push(@aln, $score, $rec[0]);
$max_score = $score if $score > $max_score;
}
} elsif ($tag =~ /^XC:Z:(.+)$/) {
my @xc = split ";", $1;
foreach my $c (@xc) {
my ($g, $s) = split ",", $c;
push(@aln, $s, $g);
$max_score = $s if $s > $max_score;
}
}
}
if (($flag & 0x4) == 0) {
my ($indel, $indel_p, $m_len) = calc_indel($cigar);
my $score = $m_len * $A - ($ed - $indel) * ($A + $B) - $indel_p;
if ($ed < 0) {
$score = $m_len * $A - $mm * ($A + $B) - $indel_p;
}
push(@aln, $score, $ref);
$max_score = $score if $score > $max_score;
}
return ($query, $flag, $max_score, $seq, $qual, @aln);
}
sub fetchTaxid {
my ($ref) = @_;
die "cannot fetch taxid" unless $ref =~ /kraken:taxid\|(\d+)$/;
return $1;
}
sub calc_indel {
my ($cigar) = @_;
my @cig_fields = split(/([A-Z=])/, $cigar);
# print $cigar, "\n";
my ($indel, $indel_p, $m_len) = (0, 0, 0);
while (@cig_fields) {
my $l = shift @cig_fields;
my $op = shift @cig_fields;
if ($op eq 'M' or $op eq 'X' or $op eq '=') {
$m_len += $l;
} elsif ($op eq 'I') {
$indel += $l;
$indel_p += $O + $E * $l;
} elsif ($op eq 'D') {
$indel += $l;
$indel_p += $O + $E * $l;
}
}
return ($indel, $indel_p, $m_len);
}
sub rev_comp {
my ($seq) = @_;
$seq =~ tr /atcgATCG/tagcTAGC/;
$seq = reverse($seq);
return $seq;
}