| 1 | package Bio::Tradis::Parser::Bam; | ||||||||||
| 2 | 7 | $Bio::Tradis::Parser::Bam::VERSION = '1.4.1'; | |||||||||
| 3 | # ABSTRACT: Very basic BAM parser. Limited functionality. | ||||||||||
| 4 | |||||||||||
| 5 | |||||||||||
| 6 | 7 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 7 | has 'samtools_exec' => ( is => 'rw', isa => 'Str', default => 'samtools' ); | |||||||||
| 9 | 7 | has 'file' => ( is => 'rw', isa => 'Str', required => 1 ); | |||||||||
| 10 | 7 | has '_bam_handle' => ( | |||||||||
| 11 | is => 'ro', | ||||||||||
| 12 | isa => 'FileHandle', | ||||||||||
| 13 | required => 0, | ||||||||||
| 14 | lazy => 1, | ||||||||||
| 15 | builder => '_build__bam_handle' | ||||||||||
| 16 | ); | ||||||||||
| 17 | 7 | 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 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_build__bam_handle: called CC=2 SCAR=7.6
| |||||||||||
| 28 | 3 | my $bamfile = $self->file; | |||||||||
| 29 | |||||||||||
| 30 | 3 | ▶ | open( my $bamh, "-|", $self->samtools_exec." view $bamfile" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | or die "Cannot open $bamfile"; | ||||||||||
| 32 | 0 | return $bamh; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | sub _binary_flag { | ||||||||||
| 36 | 0 | ▶ | my ( $self, $flag ) = @_; | ||||||||
|
Subroutine
_binary_flag: not called CC=1 SCAR=6.9
| |||||||||||
| 37 | 0 | my $bin_flag = sprintf( "%b", int($flag) ); | |||||||||
| 38 | 0 | return $bin_flag; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | sub _parse_read { | ||||||||||
| 42 | 0 | ▶ | my ( $self, $line ) = @_; | ||||||||
|
Subroutine
_parse_read: not called CC=4 SCAR=30.0
| |||||||||||
| 43 | 0 | chomp($line); | |||||||||
| 44 | |||||||||||
| 45 | # Parse and return as a hash ref | ||||||||||
| 46 | 0 | my @fields = qw(QNAME FLAG RNAME POS MAPQ CIGAR RNEXT PNEXT TLEN SEQ QUAL); | |||||||||
| 47 | 0 | my @cols = split( '\t', $line ); | |||||||||
| 48 | 0 | my %read; | |||||||||
| 49 | 0 | $read{'READ'} = $line; | |||||||||
| 50 | 0 | foreach my $i ( 0 .. ( scalar(@cols) - 1 ) ) { | |||||||||
| 51 | 0 | ▶ | if ( $i < scalar(@fields) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | $read{ $fields[$i] } = $cols[$i]; | |||||||||
| 53 | 0 | ▶ | if ( $fields[$i] eq 'FLAG' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | $read{'BINARY_FLAG'} = $self->_binary_flag( int( $cols[$i] ) ); | |||||||||
| 55 | } | ||||||||||
| 56 | } | ||||||||||
| 57 | else { | ||||||||||
| 58 | 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 | $read{ $1 } = $2; | |||||||||
| 64 | } | ||||||||||
| 65 | } | ||||||||||
| 66 | 0 | return \%read; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | ### Public methods ### | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | sub seq_info { | ||||||||||
| 73 | 4 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
seq_info: called CC=4 SCAR=20.3
Pod
undocumented
| |||||||||||
| 74 | 4 | my $bamfile = $self->file; | |||||||||
| 75 | |||||||||||
| 76 | 4 | my ( %all_seq_info, $seq_name, %this_seq_info ); | |||||||||
| 77 | 4 | open( SINFO, "-|", $self->samtools_exec." view -H $bamfile | grep ^\@SQ | cut -f 2-" ); | |||||||||
| 78 | 4 | ▶ | while ( my $line = <SINFO> ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | chomp($line); | |||||||||
| 80 | 0 | my @fields = split( '\t', $line ); | |||||||||
| 81 | 0 | $seq_name = shift(@fields); | |||||||||
| 82 | 0 | $seq_name =~ s/SN://; | |||||||||
| 83 | 0 | foreach my $item (@fields) { | |||||||||
| 84 | 0 | my @parts = split( ':', $item ); | |||||||||
| 85 | 0 | my $tag = shift(@parts); | |||||||||
| 86 | 0 | $this_seq_info{$tag} = join( ':', @parts ); | |||||||||
| 87 | 0 | foreach my $k (keys %this_seq_info){ | |||||||||
| 88 | 0 | $all_seq_info{$seq_name}->{$k} = $this_seq_info{$k}; | |||||||||
| 89 | } | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | } | ||||||||||
| 93 | 4 | return %all_seq_info; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | |||||||||||
| 97 | sub next_read { | ||||||||||
| 98 | 3 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
next_read: called CC=2 SCAR=14.3
Pod
undocumented
| |||||||||||
| 99 | 3 | my $bh = $self->_bam_handle; | |||||||||
| 100 | 0 | my $line = <$bh>; | |||||||||
| 101 | 0 | ▶ | if ( defined($line) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 0 | chomp($line); | |||||||||
| 103 | 0 | my $read = $self->_parse_read($line); | |||||||||
| 104 | 0 | $self->_set_currentread($read); | |||||||||
| 105 | 0 | return 1; | |||||||||
| 106 | } | ||||||||||
| 107 | else { | ||||||||||
| 108 | 0 | return 0; | |||||||||
| 109 | } | ||||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | sub close_file_handle{ | ||||||||||
| 113 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
close_file_handle: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 114 | 0 | close $self->_bam_handle; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | |||||||||||
| 118 | sub read_info { | ||||||||||
| 119 | 2 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
read_info: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 120 | 2 | return $self->_currentread; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | |||||||||||
| 124 | sub is_mapped { | ||||||||||
| 125 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
is_mapped: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 126 | 0 | my $flag = ${ $self->_currentread }{BINARY_FLAG}; | |||||||||
| 127 | 0 | my @flag_array = split( '', $flag ); | |||||||||
| 128 | 0 | my $resl; | |||||||||
| 129 | 0 | ▶ | if ( $flag_array[-3] ) { $resl = 0; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 0 | else { $resl = 1; } | |||||||||
| 131 | 0 | return $resl; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | |||||||||||
| 135 | sub is_reverse { | ||||||||||
| 136 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
is_reverse: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 137 | 0 | my $flag = ${ $self->_currentread }{BINARY_FLAG}; | |||||||||
| 138 | 0 | my @flag_array = split( '', $flag ); | |||||||||
| 139 | |||||||||||
| 140 | #print @flag_array; | ||||||||||
| 141 | 0 | return $flag_array[-5]; | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | 7 | __PACKAGE__->meta->make_immutable; | |||||||||
| 145 | 7 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 146 | 7 | 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.4.1 | ||||||||||
| 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 | ||||||||||