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