forked from chaolinzhanglab/czplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSam.pm
294 lines (235 loc) · 5.29 KB
/
Sam.pm
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#
#===============================================================================
#
# FILE: Sam.pm
#
# DESCRIPTION: Package to handle sam files
# BUGS: ---
# NOTES:
# AUTHOR: Chaolin Zhang (cz), [email protected]
# COMPANY: Columbia University
# VERSION: 1.0
# CREATED: 12/24/2013
# REVISION: ---
#===============================================================================
package Sam;
require Exporter;
our $VERSION = 1.01;
@ISA = qw (Exporter);
@EXPORT = qw (
lineToSam
decodeSamFlag
readSamFile
samToLine
samToBed
writeSamFile
);
=head1 NAME
Sam - subroutines that deal with sequence alignment
subroutines starting with a hyphen should not be called outside
=cut
use strict;
use warnings;
use Data::Dumper;
use Carp;
use Common;
sub readSamFile
{
my ($inFile, $verbose) = @_;
my $fin;
open ($fin, "<$inFile") || Carp::croak "cannot open file $inFile to read\n";
my @ret;
my $iter = 0;
while (my $line = <$fin>)
{
chomp $line;
next if $line =~/^\s*$/;
next if $line =~/^\@/;
print "$iter ...\n" if $verbose && $iter % 100000 == 0;
$iter++;
my $s = lineToSam ($line);
push @ret, $s;
}
close ($fin);
return \@ret;
}
sub writeSamFile
{
my ($sam, $outFile) = @_;
my $fout;
open ($fout, ">$outFile") || Carp::croak "cannot open file $outFile to write\n";
foreach my $s (@$sam)
{
print $fout samToLine ($s), "\n";
}
close ($fout);
}
=head2 lineToSam
very light-weight function, with essentially no decoding
=cut
sub lineToSam
{
my $line = $_[0];
my ($QNAME, $FLAG, $RNAME, $POS, $MAPQ, $CIGAR, $MRNM, $MPOS, $ISIZE, $SEQ, $QUAL, $TAGS) = split (/\s+/, $line, 12);
return {
QNAME => $QNAME,
FLAG=> $FLAG,
RNAME=>$RNAME,
POS=>$POS,
MAPQ=>$MAPQ,
CIGAR=>$CIGAR,
MRNM=>$MRNM,
MPOS=>$MPOS,
ISIZE=>$ISIZE,
SEQ=>$SEQ,
QUAL=>$QUAL,
TAGS=>$TAGS
};
}
sub samToLine
{
my $sam = $_[0];
my $line = join ("\t",
$sam->{'QNAME'},
$sam->{'FLAG'},
$sam->{'RNAME'},
$sam->{'POS'},
$sam->{'MAPQ'},
$sam->{'CIGAR'},
$sam->{'MRNM'},
$sam->{'MPOS'},
$sam->{'ISIZE'},
$sam->{'SEQ'},
$sam->{'QUAL'},
$sam->{'TAGS'});
return $line;
}
=head2
my $bed = samToBed ($sam)
or
my $bed = samToBed ($sam, $useRNAStrand)
return 0 if no alignment
besides all columns for bed format, an additional key is flagInfo, which is decoded flags from SAM format
#TODO: deal with soft clipping
#NOTE: sam2bed.pl in OLego has a separate copy of this function. Any changes here need to be incoporated in that program
=cut
sub samToBed
{
my ($sam, $useRNAStrand) = @_;
$useRNAStrand = 0 unless $useRNAStrand;
return 0 if $sam->{"CIGAR"} eq '*'; #no alignment
my $flagInfo = decodeSamFlag ($sam->{"FLAG"});
my $strand = $flagInfo->{'query_strand'};
my $TAGS = "";
$TAGS = $sam->{"TAGS"} if $sam->{"TAGS"};
if ($useRNAStrand)
{
if ($TAGS=~/XS\:\S*\:([-+\.])/)
{
$strand = $1;
$strand = '+' if $strand eq '.';
}
}
my $read1_or_2 = $flagInfo->{'read_1_or_2'};
my $name = $sam->{"QNAME"};
my $chrom = $sam->{"RNAME"};
my $chromStart = $sam->{"POS"} - 1;
my $score = 0;
if ($TAGS=~/NM\:\S*\:(\d+)/)
{
$score = $1;
}
my $CIGAR = $sam->{"CIGAR"};
my $QNAME = $sam->{"QNAME"};
my $SEQ = $sam->{"SEQ"};
#remove soft cliped nucleotides
if ($CIGAR =~/^\d+S(.*?)$/)
{
$CIGAR = $1;
}
if ($CIGAR =~/^(.*?)\d+S$/)
{
$CIGAR = $1;
}
#deal with the rest
if ($sam->{"CIGAR"}=~/[^\d+|M|N|I|D]/g)
{
Carp::croak "unexpected CIGAR string: $CIGAR in $QNAME: $SEQ\n";
}
my (@blockSizes, @blockStarts);
my $currLen = 0;
my $extendBlock = 0;
while ($CIGAR =~/(\d+)([M|N|I|D])/g)
{
my ($size, $type) = ($1, $2);
if ($type eq 'I' || $type eq 'D')
{
#insertion in reads
$extendBlock = 1;
if ($type eq 'D')
{
my $n = @blockSizes;
if ($n < 1)
{
$chromStart += $size;
}
else
{
$blockSizes[$#blockSizes] += $size;
$currLen += $size;
}
}
next;
}
if ($type eq 'M')
{
if ($extendBlock && @blockSizes > 0)
{
#extend the previous block
my $n = @blockSizes;
$blockSizes[$n-1] += $size;
}
else
{
push @blockSizes, $size;
push @blockStarts, $currLen;
}
$extendBlock = 0;
}
$currLen += $size;
}
my $blockCount = @blockSizes;
my $chromEnd = $chromStart + $blockStarts[$blockCount-1] + $blockSizes[$blockCount-1] - 1;
my $bed = {
chrom=>$chrom,
chromStart=>$chromStart,
chromEnd=>$chromEnd,
name=>$name,
score=>$score,
strand=>$strand,
thickStart=>$chromStart,
thickEnd=>$chromEnd,
itemRgb=>0,
blockCount=>$blockCount,
blockSizes=>\@blockSizes,
blockStarts=>\@blockStarts,
flagInfo=>$flagInfo
};
}
sub decodeSamFlag
{
my $flag = $_[0];
$flag = sprintf ("%012b", $flag);
my @flags = split (//, $flag);
my $flagInfo = {
PE=>$flags[11], #1 means paired-end data
PE_map=>$flags[10], #1 means each end is properly aligned according to the aligner
query_nomap=>$flags[9], #1 means this read is unmapped
mate_nomap=>$flags[8], #1 means its mate is unmapped
query_strand=>$flags[7] == 0 ? '+' : '-', #1 means the strand of this read is on the negative strand
mate_strand=>$flags[6] == 0 ? '+' : '-', #1 means its mate is on the negative strand
read_1_or_2=> $flags[5] == 1 ? 1 : 2 #1 means this is read1
};
return $flagInfo;
}
1;