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