line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bio::Tradis::Parser::Bam; |
2
|
|
|
|
|
|
|
$Bio::Tradis::Parser::Bam::VERSION = '1.3.3'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Very basic BAM parser. Limited functionality. |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
7
|
|
|
7
|
|
103582
|
use Moose; |
|
7
|
|
|
|
|
410134
|
|
|
7
|
|
|
|
|
42
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has 'samtools_exec' => ( is => 'rw', isa => 'Str', default => 'samtools' ); |
9
|
|
|
|
|
|
|
has 'file' => ( is => 'rw', isa => 'Str', required => 1 ); |
10
|
|
|
|
|
|
|
has '_bam_handle' => ( |
11
|
|
|
|
|
|
|
is => 'ro', |
12
|
|
|
|
|
|
|
isa => 'FileHandle', |
13
|
|
|
|
|
|
|
required => 0, |
14
|
|
|
|
|
|
|
lazy => 1, |
15
|
|
|
|
|
|
|
builder => '_build__bam_handle' |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
has '_currentread' => ( |
18
|
|
|
|
|
|
|
is => 'rw', |
19
|
|
|
|
|
|
|
isa => 'HashRef', |
20
|
|
|
|
|
|
|
required => 0, |
21
|
|
|
|
|
|
|
writer => '_set_currentread' |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
### Private methods ### |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub _build__bam_handle { |
27
|
3
|
|
|
3
|
|
8
|
my ($self) = @_; |
28
|
3
|
|
|
|
|
65
|
my $bamfile = $self->file; |
29
|
|
|
|
|
|
|
|
30
|
3
|
50
|
|
|
|
73
|
open( my $bamh, "-|", $self->samtools_exec." view $bamfile" ) |
31
|
|
|
|
|
|
|
or die "Cannot open $bamfile"; |
32
|
0
|
|
|
|
|
0
|
return $bamh; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _binary_flag { |
36
|
0
|
|
|
0
|
|
0
|
my ( $self, $flag ) = @_; |
37
|
0
|
|
|
|
|
0
|
my $bin_flag = sprintf( "%b", int($flag) ); |
38
|
0
|
|
|
|
|
0
|
return $bin_flag; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _parse_read { |
42
|
0
|
|
|
0
|
|
0
|
my ( $self, $line ) = @_; |
43
|
0
|
|
|
|
|
0
|
chomp($line); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Parse and return as a hash ref |
46
|
0
|
|
|
|
|
0
|
my @fields = qw(QNAME FLAG RNAME POS MAPQ CIGAR RNEXT PNEXT TLEN SEQ QUAL); |
47
|
0
|
|
|
|
|
0
|
my @cols = split( '\t', $line ); |
48
|
0
|
|
|
|
|
0
|
my %read; |
49
|
0
|
|
|
|
|
0
|
$read{'READ'} = $line; |
50
|
0
|
|
|
|
|
0
|
foreach my $i ( 0 .. ( scalar(@cols) - 1 ) ) { |
51
|
0
|
0
|
|
|
|
0
|
if ( $i < scalar(@fields) ) { |
52
|
0
|
|
|
|
|
0
|
$read{ $fields[$i] } = $cols[$i]; |
53
|
0
|
0
|
|
|
|
0
|
if ( $fields[$i] eq 'FLAG' ) { |
54
|
0
|
|
|
|
|
0
|
$read{'BINARY_FLAG'} = $self->_binary_flag( int( $cols[$i] ) ); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else { |
58
|
0
|
|
|
|
|
0
|
$cols[$i] =~ /^([^:]+):[AifZHB]:(.+)/; |
59
|
|
|
|
|
|
|
#my @tagged = split( ':', $cols[$i] ); |
60
|
|
|
|
|
|
|
#my $tag_key = shift @tagged; |
61
|
|
|
|
|
|
|
#shift @tagged; |
62
|
|
|
|
|
|
|
#$read{ $tag_key } = join(':', @tagged); |
63
|
0
|
|
|
|
|
0
|
$read{ $1 } = $2; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
0
|
|
|
|
|
0
|
return \%read; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
### Public methods ### |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub seq_info { |
73
|
4
|
|
|
4
|
0
|
15
|
my ($self) = @_; |
74
|
4
|
|
|
|
|
94
|
my $bamfile = $self->file; |
75
|
|
|
|
|
|
|
|
76
|
4
|
|
|
|
|
16
|
my ( %all_seq_info, $seq_name, %this_seq_info ); |
77
|
4
|
|
|
|
|
91
|
open( SINFO, "-|", $self->samtools_exec." view -H $bamfile | grep ^\@SQ | cut -f 2-" ); |
78
|
4
|
|
|
|
|
8930
|
while ( my $line = <SINFO> ) { |
79
|
0
|
|
|
|
|
0
|
chomp($line); |
80
|
0
|
|
|
|
|
0
|
my @fields = split( '\t', $line ); |
81
|
0
|
|
|
|
|
0
|
$seq_name = shift(@fields); |
82
|
0
|
|
|
|
|
0
|
$seq_name =~ s/SN://; |
83
|
0
|
|
|
|
|
0
|
foreach my $item (@fields) { |
84
|
0
|
|
|
|
|
0
|
my @parts = split( ':', $item ); |
85
|
0
|
|
|
|
|
0
|
my $tag = shift(@parts); |
86
|
0
|
|
|
|
|
0
|
$this_seq_info{$tag} = join( ':', @parts ); |
87
|
0
|
|
|
|
|
0
|
foreach my $k (keys %this_seq_info){ |
88
|
0
|
|
|
|
|
0
|
$all_seq_info{$seq_name}->{$k} = $this_seq_info{$k}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
} |
93
|
4
|
|
|
|
|
77
|
return %all_seq_info; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub next_read { |
98
|
3
|
|
|
3
|
0
|
849
|
my ($self) = @_; |
99
|
3
|
|
|
|
|
77
|
my $bh = $self->_bam_handle; |
100
|
0
|
|
|
|
|
0
|
my $line = <$bh>; |
101
|
0
|
0
|
|
|
|
0
|
if ( defined($line) ) { |
102
|
0
|
|
|
|
|
0
|
chomp($line); |
103
|
0
|
|
|
|
|
0
|
my $read = $self->_parse_read($line); |
104
|
0
|
|
|
|
|
0
|
$self->_set_currentread($read); |
105
|
0
|
|
|
|
|
0
|
return 1; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
else { |
108
|
0
|
|
|
|
|
0
|
return 0; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub close_file_handle{ |
113
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
114
|
0
|
|
|
|
|
0
|
close $self->_bam_handle; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub read_info { |
119
|
2
|
|
|
2
|
0
|
34
|
my ($self) = @_; |
120
|
2
|
|
|
|
|
53
|
return $self->_currentread; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub is_mapped { |
125
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
126
|
0
|
|
|
|
|
|
my $flag = ${ $self->_currentread }{BINARY_FLAG}; |
|
0
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
my @flag_array = split( '', $flag ); |
128
|
0
|
|
|
|
|
|
my $resl; |
129
|
0
|
0
|
|
|
|
|
if ( $flag_array[-3] ) { $resl = 0; } |
|
0
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
else { $resl = 1; } |
131
|
0
|
|
|
|
|
|
return $resl; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub is_reverse { |
136
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
137
|
0
|
|
|
|
|
|
my $flag = ${ $self->_currentread }{BINARY_FLAG}; |
|
0
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
my @flag_array = split( '', $flag ); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
#print @flag_array; |
141
|
0
|
|
|
|
|
|
return $flag_array[-5]; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
145
|
7
|
|
|
7
|
|
48984
|
no Moose; |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
49
|
|
146
|
|
|
|
|
|
|
1; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
__END__ |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=pod |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=encoding UTF-8 |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 NAME |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Bio::Tradis::Parser::Bam - Very basic BAM parser. Limited functionality. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 VERSION |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
version 1.3.3 |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 SYNOPSIS |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Parses BAM files and gives access to basic info in them. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
use Bio::Tradis::Parser::Bam; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
my $pipeline = Bio::Tradis::Parser::Bam->new(file => 'abc'); |
169
|
|
|
|
|
|
|
$pipeline->read_info; |
170
|
|
|
|
|
|
|
$pipeline->next_read; |
171
|
|
|
|
|
|
|
$pipeline->seq_info; |
172
|
|
|
|
|
|
|
$pipeline->is_mapped; |
173
|
|
|
|
|
|
|
$pipeline->is_reverse; |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=seq_info |
176
|
|
|
|
|
|
|
Reads BAM header and returns a hash (keys are sequence ids, values are hash |
177
|
|
|
|
|
|
|
refs with keys as tags (like LN and M5)) |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=next_read |
180
|
|
|
|
|
|
|
Moves _currentread to the next entry in the BAM. Returns 0 if EOF. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=read_info |
183
|
|
|
|
|
|
|
Returns info from _currentread = hash reference with field name as key. |
184
|
|
|
|
|
|
|
Standard fields are named as per the SAM format specification: |
185
|
|
|
|
|
|
|
1 : QNAME |
186
|
|
|
|
|
|
|
2 : FLAG |
187
|
|
|
|
|
|
|
3 : RNAME |
188
|
|
|
|
|
|
|
4 : POS |
189
|
|
|
|
|
|
|
5 : MAPQ |
190
|
|
|
|
|
|
|
6 : CIGAR |
191
|
|
|
|
|
|
|
7 : RNEXT |
192
|
|
|
|
|
|
|
8 : PNEXT |
193
|
|
|
|
|
|
|
9 : TLEN |
194
|
|
|
|
|
|
|
10 : SEQ |
195
|
|
|
|
|
|
|
11 : QUAL |
196
|
|
|
|
|
|
|
Additional fields will use their tag names. |
197
|
|
|
|
|
|
|
Complete line is returned with key READ |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=is_mapped |
200
|
|
|
|
|
|
|
Parses the flag for the current read and determines if mapped. |
201
|
|
|
|
|
|
|
Returns 0 or 1. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=is_reverse |
204
|
|
|
|
|
|
|
Parses the flag for the current read and determines if reverse |
205
|
|
|
|
|
|
|
complemented. Returns 0 or 1. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 AUTHOR |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Carla Cummins <path-help@sanger.ac.uk> |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
This is free software, licensed under: |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
The GNU General Public License, Version 3, June 2007 |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=cut |