lib/Bio/Tradis/Parser/Bam.pm

filter: Statementstmt 33.7% 26 / 77 Branchbran 16.6% 2 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 54.5% 6 / 11 Podpod 0.0% 0 / 6 totaltotal 32.0% 34 / 106 CC 10 SCAR 19.8
CC
10
Cov
31%
_parse_read
20.0
seq_info
7.7
is_mapped
6.0
CRAP 7.2
« blib/lib/Bio/Tradis/CommandLine/CheckTags.pm ↑ Summary lib/Bio/Tradis/CombinePlots.pm »
1 package Bio::Tradis::Parser::Bam;
2 10 $Bio::Tradis::Parser::Bam::VERSION = '1.4.5';
3 # ABSTRACT: Very basic BAM parser. Limited functionality.
4  
5  
6 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 has 'samtools_exec' => ( is => 'rw', isa => 'Str', default => 'samtools' );
9 10 has 'file' => ( is => 'rw', isa => 'Str', required => 1 );
10 10 has '_bam_handle' => (
11 is => 'ro',
12 isa => 'FileHandle',
13 required => 0,
14 lazy => 1,
15 builder => '_build__bam_handle'
16 );
17 10 has '_currentread' => (
18 is => 'rw',
19 isa => 'HashRef',
20 required => 0,
21 writer => '_set_currentread'
22 );
23  
24 ### Private methods ###
25  
26 3 ▶sub _build__bam_handle {
Subroutine _build__bam_handle: called CC=2 SCAR=7.6
27 3 my ($self) = @_;
28 3 my $bamfile = $self->file;
29  
30 3 ▶ open( my $bamh, "-|", $self->samtools_exec." view $bamfile" )
Branch: 1 branch
BranchTrueFalseTotal
open my $bamh, "-|", $self->samtools_exec . " view $bamfile" or die "Cannot open $bamfile" 0 3 3
31 or die "Cannot open $bamfile";
32 0 return $bamh;
33 }
34  
35 0 ▶sub _binary_flag {
Subroutine _binary_flag: not called CC=1 SCAR=6.9
36 0 my ( $self, $flag ) = @_;
37 0 my $bin_flag = sprintf( "%b", int($flag) );
38 0 return $bin_flag;
39 }
40  
41 0 ▶sub _parse_read {
Subroutine _parse_read: not called CC=4 SCAR=30.0
42 0 my ( $self, $line ) = @_;
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
BranchTrueFalseTotal
if ($i < scalar @fields) { } 0 0 0
52 0 $read{ $fields[$i] } = $cols[$i];
53 0 ▶ if ( $fields[$i] eq 'FLAG' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields[$i] eq "FLAG") 0 0 0
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 4 ▶sub seq_info {
Subroutine seq_info: called CC=4 SCAR=20.3
Pod undocumented
73 4 my ($self) = @_;
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
BranchTrueFalseTotal
if (defined(my $line = readline SINFO)) 0 4 4
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 3 ▶sub next_read {
Subroutine next_read: called CC=2 SCAR=14.3
Pod undocumented
98 3 my ($self) = @_;
99 3 my $bh = $self->_bam_handle;
100 0 my $line = <$bh>;
101 0 ▶ if ( defined($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) { } 0 0 0
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 0 ▶sub close_file_handle{
Subroutine close_file_handle: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my ($self) = @_;
114 0 close $self->_bam_handle;
115 }
116  
117  
118 2 ▶sub read_info {
Subroutine read_info: called CC=1 SCAR=0.0
Pod undocumented
119 2 my ($self) = @_;
120 2 return $self->_currentread;
121 }
122  
123  
124 0 ▶sub is_mapped {
Subroutine is_mapped: not called CC=2 SCAR=17.9
Pod undocumented
125 0 my ($self) = @_;
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
BranchTrueFalseTotal
if ($flag_array[-3]) { } 0 0 0
130 0 else { $resl = 1; }
131 0 return $resl;
132 }
133  
134  
135 0 ▶sub is_reverse {
Subroutine is_reverse: not called CC=1 SCAR=6.9
Pod undocumented
136 0 my ($self) = @_;
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 10 __PACKAGE__->meta->make_immutable;
145 10 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 10 1;
147  
148 __END__
« blib/lib/Bio/Tradis/CommandLine/CheckTags.pm ↑ Summary lib/Bio/Tradis/CombinePlots.pm »