| 1 | package Bio::Tradis::Analysis::InsertSite; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Take in a bam file and plot the start position of each read | ||||||||||||||||||||||||||||
| 3 | 7 | $Bio::Tradis::Analysis::InsertSite::VERSION = '1.4.5'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Bio::Tradis::Parser::Bam; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Bio::Tradis::Parser::Cigar; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 7 | has 'filename' => ( is => 'rw', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 11 | 7 | has 'output_base_filename' => ( is => 'rw', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 12 | 7 | has 'mapping_score' => ( is => 'ro', isa => 'Int', required => 1 ); | |||||||||||||||||||||||||||
| 13 | 7 | has 'samtools_exec' => ( is => 'ro', isa => 'Str', default => 'samtools' ); | |||||||||||||||||||||||||||
| 14 | 7 | has '_output_file_handles' => ( is => 'rw', isa => 'HashRef', lazy_build => 1 ); | |||||||||||||||||||||||||||
| 15 | 7 | has '_sequence_names' => ( is => 'rw', isa => 'ArrayRef', lazy_build => 1 ); | |||||||||||||||||||||||||||
| 16 | 7 | has '_sequence_base_counters' => | |||||||||||||||||||||||||||
| 17 | ( is => 'rw', isa => 'HashRef', lazy_build => 1 ); | ||||||||||||||||||||||||||||
| 18 | 7 | has '_sequence_information' => | |||||||||||||||||||||||||||
| 19 | ( is => 'rw', isa => 'HashRef', lazy_build => 1 ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 7 | has '_frequency_of_read_start' => ( | |||||||||||||||||||||||||||
| 22 | is => 'rw', | ||||||||||||||||||||||||||||
| 23 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 24 | lazy => 1, | ||||||||||||||||||||||||||||
| 25 | builder => '_build__frequency_of_read_start' | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 3 | ▶ | sub _build__sequence_information { | ||||||||||||||||||||||||||
|
Subroutine
_build__sequence_information: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 30 | 3 | my %all_sequences_info = | |||||||||||||||||||||||||||
| 31 | Bio::Tradis::Parser::Bam->new( file => $self->filename, samtools_exec => $self->samtools_exec )->seq_info; | ||||||||||||||||||||||||||||
| 32 | 3 | return \%all_sequences_info; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | ▶ | sub _build__sequence_names { | ||||||||||||||||||||||||||
|
Subroutine
_build__sequence_names: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 37 | 3 | my @sequence_names = keys %{ $self->_sequence_information }; | |||||||||||||||||||||||||||
| 38 | 3 | return \@sequence_names; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub _build__sequence_base_counters { | ||||||||||||||||||||||||||
|
Subroutine
_build__sequence_base_counters: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 42 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 43 | 0 | my %sequence_base_counters; | |||||||||||||||||||||||||||
| 44 | 0 | for my $sequence_name ( @{ $self->_sequence_names } ) { | |||||||||||||||||||||||||||
| 45 | 0 | $sequence_base_counters{$sequence_name} = 0; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 0 | return \%sequence_base_counters; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 3 | ▶ | sub _build__output_file_handles { | ||||||||||||||||||||||||||
|
Subroutine
_build__output_file_handles: called CC=3 SCAR=13.6
| |||||||||||||||||||||||||||||
| 51 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 52 | 3 | my $out = $self->output_base_filename; | |||||||||||||||||||||||||||
| 53 | 3 | chomp $out; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 3 | my %output_file_handles; | |||||||||||||||||||||||||||
| 56 | 3 | for my $sequence_name ( @{ $self->_sequence_names } ) { | |||||||||||||||||||||||||||
| 57 | 0 | my $file_sequence_name = $sequence_name; | |||||||||||||||||||||||||||
| 58 | 0 | $file_sequence_name =~ s/[^\w\d\.]/_/g; | |||||||||||||||||||||||||||
| 59 | 0 | my $cmd = "gzip > $out.$file_sequence_name.insert_site_plot.gz"; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | open( $output_file_handles{$sequence_name}, '|-', $cmd ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | || Bio::Tradis::Analysis::Exceptions::FailedToCreateOutputFileHandle | ||||||||||||||||||||||||||||
| 62 | ->throw( error => | ||||||||||||||||||||||||||||
| 63 | "Couldnt create output file handle for saving insertsite plot results for " | ||||||||||||||||||||||||||||
| 64 | . $sequence_name . " in " | ||||||||||||||||||||||||||||
| 65 | . $self->filename | ||||||||||||||||||||||||||||
| 66 | . " and output base " | ||||||||||||||||||||||||||||
| 67 | . $self->output_base_filename ); | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 3 | return \%output_file_handles; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub _number_of_forward_reads { | ||||||||||||||||||||||||||
|
Subroutine
_number_of_forward_reads: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 74 | 0 | my ( $self, $sequence_name, $read_coord ) = @_; | |||||||||||||||||||||||||||
| 75 | 0 | return $self->_number_of_reads( $sequence_name, $read_coord, 1 ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub _number_of_reverse_reads { | ||||||||||||||||||||||||||
|
Subroutine
_number_of_reverse_reads: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 79 | 0 | my ( $self, $sequence_name, $read_coord ) = @_; | |||||||||||||||||||||||||||
| 80 | 0 | return $self->_number_of_reads( $sequence_name, $read_coord, -1 ); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub _number_of_reads { | ||||||||||||||||||||||||||
|
Subroutine
_number_of_reads: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 84 | 0 | my ( $self, $sequence_name, $read_coord, $direction ) = @_; | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->_frequency_of_read_start->{$sequence_name}{$read_coord} and defined $self->_frequency_of_read_start->{$sequence_name}{$read_coord}{$direction}
MC/DC0%0/2
defined $self->_frequency_of_read_start->{$sequence_name}{$read_coord} and defined $self->_frequency_of_read_start->{$sequence_name}{$read_coord}{$direction}
defined $self->_frequency_of_read_start->{$sequence_name}{$read_coord}
defined $self->_frequency_of_read_start->{$sequence_name}{$read_coord}{$direction}
Truth table0%0/3
A and B
A= defined $self->_frequency_of_read_start->{$sequence_name}{$read_coord}B= defined $self->_frequency_of_read_start->{$sequence_name}{$read_coord}{$direction}
| |||||||||||||||||||||||||||||
| 86 | defined( | ||||||||||||||||||||||||||||
| 87 | $self->_frequency_of_read_start->{$sequence_name}{$read_coord} | ||||||||||||||||||||||||||||
| 88 | ) | ||||||||||||||||||||||||||||
| 89 | && defined( | ||||||||||||||||||||||||||||
| 90 | $self->_frequency_of_read_start->{$sequence_name}{$read_coord} | ||||||||||||||||||||||||||||
| 91 | {$direction} | ||||||||||||||||||||||||||||
| 92 | ) | ||||||||||||||||||||||||||||
| 93 | ) | ||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||
| 95 | return $self->_frequency_of_read_start->{$sequence_name}{$read_coord} | ||||||||||||||||||||||||||||
| 96 | 0 | {$direction}; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 0 | return 0; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # work out if padding is needed and return it as a formatted string | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub _create_padding_string { | ||||||||||||||||||||||||||
|
Subroutine
_create_padding_string: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 103 | 0 | my ( $self, $previous_counter, $current_counter ) = @_; | |||||||||||||||||||||||||||
| 104 | 0 | my $padding_string = ""; | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | for ( my $i = $previous_counter + 1 ; $i < $current_counter ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | $padding_string .= "0 0\n"; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | return $padding_string; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 3 | ▶ | sub _print_padding_at_end_of_sequence { | ||||||||||||||||||||||||||
|
Subroutine
_print_padding_at_end_of_sequence: called CC=3 SCAR=19.2
| |||||||||||||||||||||||||||||
| 112 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 113 | 3 | for my $sequence_name ( @{ $self->_sequence_names } ) { | |||||||||||||||||||||||||||
| 114 | my $sequence_length = | ||||||||||||||||||||||||||||
| 115 | 0 | $self->_sequence_information->{$sequence_name}->{'LN'}; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | next unless ( $sequence_length =~ /^[\d]+$/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | $sequence_length++; | |||||||||||||||||||||||||||
| 118 | my $padding_string = | ||||||||||||||||||||||||||||
| 119 | $self->_create_padding_string( | ||||||||||||||||||||||||||||
| 120 | 0 | $self->_sequence_base_counters->{$sequence_name}, | |||||||||||||||||||||||||||
| 121 | $sequence_length ); | ||||||||||||||||||||||||||||
| 122 | 0 | $self->_sequence_base_counters->{$sequence_name} = $sequence_length; | |||||||||||||||||||||||||||
| 123 | 0 | print { $self->_output_file_handles->{$sequence_name} } $padding_string; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 3 | ▶ | sub _close_output_file_handles { | ||||||||||||||||||||||||||
|
Subroutine
_close_output_file_handles: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 128 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 129 | 3 | for my $output_file_handle ( values %{ $self->_output_file_handles } ) { | |||||||||||||||||||||||||||
| 130 | 0 | close($output_file_handle); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 3 | return; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 3 | ▶ | sub _build__frequency_of_read_start { | ||||||||||||||||||||||||||
|
Subroutine
_build__frequency_of_read_start: called CC=4 SCAR=21.7
| |||||||||||||||||||||||||||||
| 136 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 137 | 3 | my %frequency_of_read_start; | |||||||||||||||||||||||||||
| 138 | 3 | my $samtools_command = join(' ', ($self->samtools_exec, 'view', '-F', 4, '-q', $self->mapping_score, $self->filename)); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 3 | open(my $samtools_view_fh,"-|" ,$samtools_command); | |||||||||||||||||||||||||||
| 141 | 3 | ▶ | while(<$samtools_view_fh>) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | { | ||||||||||||||||||||||||||||
| 143 | 0 | my $sam_line = $_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | my @read_details = split("\t", $sam_line); | |||||||||||||||||||||||||||
| 146 | 0 | my $seqid = $read_details[2]; | |||||||||||||||||||||||||||
| 147 | 0 | my $cigar_parser = Bio::Tradis::Parser::Cigar->new(cigar => $read_details[5], coordinate => $read_details[3]); | |||||||||||||||||||||||||||
| 148 | 0 | my $strand = 1; | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | $strand = -1 if(($read_details[1] & 0x10) == 0x10); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ( $strand == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | $frequency_of_read_start{$seqid}{ $cigar_parser->start } | ||||||||||||||||||||||||||||
| 153 | 0 | { $strand }++; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | else { | ||||||||||||||||||||||||||||
| 156 | $frequency_of_read_start{$seqid}{ $cigar_parser->end } | ||||||||||||||||||||||||||||
| 157 | 0 | { $strand }++; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 3 | return \%frequency_of_read_start; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 3 | ▶ | sub create_plots { | ||||||||||||||||||||||||||
|
Subroutine
create_plots: called CC=3 SCAR=15.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 164 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 165 | 3 | my %read_starts = %{ $self->_frequency_of_read_start }; | |||||||||||||||||||||||||||
| 166 | 3 | for my $sequence_name ( keys %read_starts ) { | |||||||||||||||||||||||||||
| 167 | 0 | my %sequence_read_coords = %{ $read_starts{$sequence_name} }; | |||||||||||||||||||||||||||
| 168 | 0 | for my $read_coord ( sort { $a <=> $b } ( keys %sequence_read_coords ) ) | |||||||||||||||||||||||||||
| 169 | { | ||||||||||||||||||||||||||||
| 170 | my $padding_string = | ||||||||||||||||||||||||||||
| 171 | $self->_create_padding_string( | ||||||||||||||||||||||||||||
| 172 | 0 | $self->_sequence_base_counters->{$sequence_name}, $read_coord ); | |||||||||||||||||||||||||||
| 173 | 0 | $self->_sequence_base_counters->{$sequence_name} = $read_coord; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | my $forward_reads = | |||||||||||||||||||||||||||
| 176 | $self->_number_of_forward_reads( $sequence_name, $read_coord ); | ||||||||||||||||||||||||||||
| 177 | 0 | my $reverse_reads = | |||||||||||||||||||||||||||
| 178 | $self->_number_of_reverse_reads( $sequence_name, $read_coord ); | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | print { $self->_output_file_handles->{$sequence_name} } | |||||||||||||||||||||||||||
| 181 | $padding_string . $forward_reads . " " . $reverse_reads . "\n"; | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 3 | $self->_print_padding_at_end_of_sequence; | |||||||||||||||||||||||||||
| 186 | 3 | $self->_close_output_file_handles; | |||||||||||||||||||||||||||
| 187 | 3 | return 1; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 7 | 1; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||