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