« blib/lib/Bio/Tradis/CommandLine/TradisBam.pm
↑ Summary
blib/lib/Bio/Tradis/CommandLine/PlotTradis.pm »
| 1 | package Bio::Tradis::RunTradis; | ||||||||||
| 2 | 5 | $Bio::Tradis::RunTradis::VERSION = '1.4.5'; | |||||||||
| 3 | # ABSTRACT: Perform all steps required for a tradis analysis | ||||||||||
| 4 | |||||||||||
| 5 | |||||||||||
| 6 | 5 | ▶ | use Cwd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 5 | ▶ | use File::Temp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 5 | ▶ | use File::Path 'rmtree'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 5 | ▶ | use Bio::Tradis::FilterTags; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 5 | ▶ | use Bio::Tradis::RemoveTags; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 5 | ▶ | use Bio::Tradis::Map; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 5 | ▶ | use Bio::Tradis::TradisPlot; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 5 | ▶ | use Bio::Tradis::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 5 | ▶ | use Bio::Tradis::Samtools; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 5 | has 'verbose' => ( is => 'rw', isa => 'Bool', default => 0 ); | |||||||||
| 18 | 5 | has 'fastqfile' => ( is => 'rw', isa => 'Str', required => 1 ); | |||||||||
| 19 | 5 | has '_unzipped_fastq' => | |||||||||
| 20 | ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build__unzipped_fastq' ); | ||||||||||
| 21 | 5 | has 'tag' => ( is => 'ro', isa => 'Maybe[Str]', required => 0 ); | |||||||||
| 22 | 5 | has 'tagdirection' => | |||||||||
| 23 | ( is => 'ro', isa => 'Str', required => 1, default => '3' ); | ||||||||||
| 24 | 5 | has 'mismatch' => ( is => 'rw', isa => 'Int', required => 1, default => 0 ); | |||||||||
| 25 | 5 | has 'mapping_score' => | |||||||||
| 26 | ( is => 'ro', isa => 'Int', required => 1, default => 30 ); | ||||||||||
| 27 | 5 | has 'reference' => ( is => 'rw', isa => 'Str', required => 1 ); | |||||||||
| 28 | has 'outfile' => ( | ||||||||||
| 29 | is => 'rw', | ||||||||||
| 30 | isa => 'Str', | ||||||||||
| 31 | required => 0, | ||||||||||
| 32 | 1 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 1 | my ($self) = @_; | |||||||||
| 34 | 1 | my @dirs = split( '/', $self->fastqfile ); | |||||||||
| 35 | 1 | my $o = pop(@dirs); | |||||||||
| 36 | 1 | $o =~ s/fastq/out/; | |||||||||
| 37 | 1 | return $o; | |||||||||
| 38 | } | ||||||||||
| 39 | 5 | ); | |||||||||
| 40 | 5 | has 'min_seed_len' => ( is => 'rw', isa => 'Maybe[Int]', required => 0 ); | |||||||||
| 41 | 5 | has 'smalt' => ( is => 'rw', isa => 'Bool', default => 0 ); | |||||||||
| 42 | 5 | has 'smalt_k' => ( is => 'rw', isa => 'Maybe[Int]', required => 0 ); | |||||||||
| 43 | 5 | has 'smalt_s' => ( is => 'rw', isa => 'Maybe[Int]', required => 0 ); | |||||||||
| 44 | 5 | has 'smalt_y' => ( is => 'rw', isa => 'Maybe[Num]', required => 0, default => 0.96 ); | |||||||||
| 45 | 5 | has 'smalt_r' => ( is => 'rw', isa => 'Maybe[Int]', required => 0, default => -1); | |||||||||
| 46 | 5 | has 'smalt_n' => ( is => 'rw', isa => 'Maybe[Int]', required => 0, default => 1); | |||||||||
| 47 | 5 | has 'samtools_exec' => ( is => 'rw', isa => 'Str', default => 'samtools' ); | |||||||||
| 48 | |||||||||||
| 49 | 5 | has '_temp_directory' => ( | |||||||||
| 50 | is => 'rw', | ||||||||||
| 51 | isa => 'Str', | ||||||||||
| 52 | required => 0, | ||||||||||
| 53 | lazy => 1, | ||||||||||
| 54 | builder => '_build__temp_directory' | ||||||||||
| 55 | ); | ||||||||||
| 56 | 5 | has 'output_directory' => ( | |||||||||
| 57 | is => 'rw', | ||||||||||
| 58 | isa => 'Str', | ||||||||||
| 59 | required => 0, | ||||||||||
| 60 | lazy => 1, | ||||||||||
| 61 | builder => '_build_output_directory' | ||||||||||
| 62 | ); | ||||||||||
| 63 | 5 | has '_stats_handle' => ( is => 'ro', isa => 'FileHandle', required => 1 ); | |||||||||
| 64 | 5 | has '_sequence_info' => ( | |||||||||
| 65 | is => 'rw', | ||||||||||
| 66 | isa => 'HashRef', | ||||||||||
| 67 | required => 0, | ||||||||||
| 68 | lazy => 1, | ||||||||||
| 69 | builder => '_build__sequence_info' | ||||||||||
| 70 | ); | ||||||||||
| 71 | 5 | has '_current_directory' => ( | |||||||||
| 72 | is => 'rw', | ||||||||||
| 73 | isa => 'Str', | ||||||||||
| 74 | required => 0, | ||||||||||
| 75 | lazy => 1, | ||||||||||
| 76 | builder => '_build__current_directory' | ||||||||||
| 77 | ); | ||||||||||
| 78 | |||||||||||
| 79 | 5 | ▶ | sub _is_gz { | ||||||||
|
Subroutine
_is_gz: called CC=2 SCAR=7.4
| |||||||||||
| 80 | 5 | my ($self) = @_; | |||||||||
| 81 | 5 | my $fq = $self->fastqfile; | |||||||||
| 82 | |||||||||||
| 83 | 5 | ▶ | if ( $fq =~ /\.gz/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | return 1; | |||||||||
| 85 | } | ||||||||||
| 86 | else { | ||||||||||
| 87 | 5 | return 0; | |||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 5 | ▶ | sub _build__unzipped_fastq { | ||||||||
|
Subroutine
_build__unzipped_fastq: called CC=3 SCAR=16.0
| |||||||||||
| 92 | 5 | my ($self) = @_; | |||||||||
| 93 | 5 | my $fq = $self->fastqfile; | |||||||||
| 94 | 5 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 95 | |||||||||||
| 96 | 5 | ▶ | if ( $self->_is_gz ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | $fq =~ /([^\/]+)$/; | |||||||||
| 98 | 0 | my $newfq = $1; | |||||||||
| 99 | 0 | $newfq =~ s/\.gz//; | |||||||||
| 100 | 0 | ▶ | if ( !-e "$temporary_directory/$newfq" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | `gunzip -c $fq > $temporary_directory/$newfq`; | |||||||||
| 102 | } | ||||||||||
| 103 | 0 | return "$temporary_directory/$newfq"; | |||||||||
| 104 | } | ||||||||||
| 105 | else { | ||||||||||
| 106 | 5 | return $fq; | |||||||||
| 107 | } | ||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | sub _build__stats_handle { | ||||||||
|
Subroutine
_build__stats_handle: not called CC=1 SCAR=6.9
| |||||||||||
| 111 | 0 | my ($self) = @_; | |||||||||
| 112 | 0 | my $outfile = $self->outfile; | |||||||||
| 113 | |||||||||||
| 114 | 0 | open( my $stats, ">", "$outfile.stats" ); | |||||||||
| 115 | 0 | return $stats; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | sub _build__sequence_info { | ||||||||
|
Subroutine
_build__sequence_info: not called CC=2 SCAR=17.9
| |||||||||||
| 119 | 0 | my ($self) = @_; | |||||||||
| 120 | 0 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 121 | 0 | open( GREP, | |||||||||
| 122 | "grep \@SQ $temporary_directory/mapped.sam | awk '{print \$2, \$3}' |" | ||||||||||
| 123 | ); | ||||||||||
| 124 | 0 | my %sns = (); | |||||||||
| 125 | 0 | ▶ | while ( my $sn = <GREP> ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | chomp($sn); | |||||||||
| 127 | 0 | $sn =~ /SN:(\S+)\s+LN:(\d+)/; | |||||||||
| 128 | 0 | $sns{$1} = $2; | |||||||||
| 129 | } | ||||||||||
| 130 | 0 | return \%sns; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 1 | ▶ | sub _build__temp_directory { | ||||||||
|
Subroutine
_build__temp_directory: called CC=1 SCAR=0.0
| |||||||||||
| 134 | 1 | my ($self) = @_; | |||||||||
| 135 | 1 | my $tmp_dir = File::Temp->newdir( 'tmp_run_tradis_XXXXX', | |||||||||
| 136 | CLEANUP => 0, | ||||||||||
| 137 | DIR => $self->output_directory ); | ||||||||||
| 138 | 1 | return $tmp_dir->dirname; | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 0 | ▶ | sub _build_output_directory { | ||||||||
|
Subroutine
_build_output_directory: not called CC=1 SCAR=6.9
| |||||||||||
| 142 | 0 | return cwd(); | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | sub _build__current_directory { | ||||||||
|
Subroutine
_build__current_directory: not called CC=1 SCAR=6.9
| |||||||||||
| 146 | 0 | my ($self) = @_; | |||||||||
| 147 | 0 | my $fq = $self->fastqfile; | |||||||||
| 148 | |||||||||||
| 149 | 0 | my @dirs = split( '/', $fq ); | |||||||||
| 150 | 0 | pop(@dirs); | |||||||||
| 151 | 0 | return join( '/', @dirs ); | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | 1 | ▶ | sub run_tradis { | ||||||||
|
Subroutine
run_tradis: called CC=15 SCAR=39.8
Pod
undocumented
| |||||||||||
| 155 | 1 | my ($self) = @_; | |||||||||
| 156 | 1 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 157 | 1 | my $fq = $self->fastqfile; | |||||||||
| 158 | |||||||||||
| 159 | 1 | my $ref = $self->reference; | |||||||||
| 160 | 1 | ▶ | Bio::Tradis::Exception::RefNotFound->throw( error => "$ref not found\n" ) unless( -e $ref ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | |||||||||||
| 162 | 1 | ▶ | print STDERR "::::::::::::::::::\n$fq\n::::::::::::::::::\n\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | |||||||||||
| 164 | 1 | ▶ | if (defined($self->tag)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | # Step 1: Filter tags that match user input tag | ||||||||||
| 166 | 1 | ▶ | print STDERR "..........Step 1: Filter tags that match user input tag\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | 1 | $self->_filter; | |||||||||
| 168 | |||||||||||
| 169 | 1 | ▶ | print STDERR "..........Step 1.1: Check that at least one read started with the tag\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 1 | $self->_check_filter; | |||||||||
| 171 | |||||||||||
| 172 | # Step 2: Remove the tag from the sequence and quality strings | ||||||||||
| 173 | 1 | ▶ | print STDERR "..........Step 2: Remove the tag from the sequence and quality strings\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 1 | $self->_remove; | |||||||||
| 175 | } else { | ||||||||||
| 176 | 0 | ▶ | print STDERR "..........Tagless mode selected skipping steps 1 and 2\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | # Step 3: Map file to reference | ||||||||||
| 180 | 1 | ▶ | my $mapper = $self->smalt ? "smalt" : "bwa"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 181 | 1 | ▶ | print STDERR "..........Step 3: Map file to reference using $mapper\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 182 | 1 | $self->_map; | |||||||||
| 183 | |||||||||||
| 184 | # Step 4: Convert output from SAM to BAM, sort and index | ||||||||||
| 185 | 1 | ▶ | print STDERR | ||||||||
|
Branch: 1 branch
| |||||||||||
| 186 | "..........Step 3.5: Convert output from SAM to BAM and sort\n" if($self->verbose); | ||||||||||
| 187 | 1 | $self->_sam2bam; | |||||||||
| 188 | 1 | $self->_sort_bam; | |||||||||
| 189 | 0 | $self->_bamcheck; | |||||||||
| 190 | |||||||||||
| 191 | # Step 5: Generate plot | ||||||||||
| 192 | 0 | ▶ | print STDERR "..........Step 4: Generate plot\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 193 | 0 | $self->_make_plot; | |||||||||
| 194 | |||||||||||
| 195 | # Step 6: Generate statistics | ||||||||||
| 196 | 0 | ▶ | print STDERR "..........Step 5: Generate statistics\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 197 | 0 | $self->_stats; | |||||||||
| 198 | |||||||||||
| 199 | # Step 7: Move files to current directory | ||||||||||
| 200 | 0 | ▶ | print STDERR "..........Step 6: Move files to current directory\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 201 | 0 | my $outfile = $self->outfile; | |||||||||
| 202 | 0 | my $output_directory = $self->output_directory; | |||||||||
| 203 | 0 | system("mv $temporary_directory/$outfile* $output_directory"); | |||||||||
| 204 | 0 | system("mv $temporary_directory/mapped.sort.bam $output_directory/$outfile.mapped.bam"); | |||||||||
| 205 | 0 | system("mv $temporary_directory/mapped.sort.bam.bai $output_directory/$outfile.mapped.bam.bai"); | |||||||||
| 206 | 0 | system("mv $temporary_directory/mapped.bamcheck $output_directory/$outfile.mapped.bamcheck"); | |||||||||
| 207 | |||||||||||
| 208 | # Clean up | ||||||||||
| 209 | 0 | ▶ | print STDERR "..........Clean up\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 210 | |||||||||||
| 211 | 0 | rmtree($temporary_directory); | |||||||||
| 212 | |||||||||||
| 213 | 0 | return 1; | |||||||||
| 214 | } | ||||||||||
| 215 | |||||||||||
| 216 | 3 | ▶ | sub _filter { | ||||||||
|
Subroutine
_filter: called CC=1 SCAR=0.0
| |||||||||||
| 217 | 3 | my ($self) = @_; | |||||||||
| 218 | 3 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 219 | 3 | my $fqfile = $self->_unzipped_fastq; | |||||||||
| 220 | 3 | my $tag = $self->tag; | |||||||||
| 221 | 3 | my $mm = $self->mismatch; | |||||||||
| 222 | |||||||||||
| 223 | 3 | my $filter = Bio::Tradis::FilterTags->new( | |||||||||
| 224 | fastqfile => $fqfile, | ||||||||||
| 225 | tag => $tag, | ||||||||||
| 226 | mismatch => $mm, | ||||||||||
| 227 | outfile => "$temporary_directory/filter.fastq" | ||||||||||
| 228 | )->filter_tags; | ||||||||||
| 229 | } | ||||||||||
| 230 | |||||||||||
| 231 | 15 | ▶ | sub _check_filter { | ||||||||
|
Subroutine
_check_filter: called CC=6 SCAR=17.9
| |||||||||||
| 232 | 15 | my ($self) = @_; | |||||||||
| 233 | 15 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 234 | 15 | my $filtered_file_filename = "$temporary_directory/filter.fastq"; | |||||||||
| 235 | 15 | ▶ | open my $filtered_file, '<', $filtered_file_filename or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 236 | Bio::Tradis::Exception::TagFilterError->throw( error => "There was a problem filtering reads by the specified tag. Please check all input files are Fastq formatted and that at least one read in each starts with the specified tag\n" ); | ||||||||||
| 237 | 13 | my @first_read_data; | |||||||||
| 238 | 13 | ▶ | while( my $line = <$filtered_file> ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 239 | 39 | ▶ | last if $. > 4; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 240 | 36 | chomp($line); | |||||||||
| 241 | 36 | push @first_read_data, $line; | |||||||||
| 242 | } | ||||||||||
| 243 | 13 | my $number_of_read_lines = scalar @first_read_data; | |||||||||
| 244 | 13 | ▶ | if ( $number_of_read_lines ne 4) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 245 | # There wasn't enough data for a complete read | ||||||||||
| 246 | 6 | Bio::Tradis::Exception::TagFilterError->throw( error => "There was a problem filtering reads by the specified tag. Please check all input files are Fastq formatted and that at least one read in each starts with the specified tag\n" ); | |||||||||
| 247 | } | ||||||||||
| 248 | 7 | my $read_plus_sign = $first_read_data[2]; | |||||||||
| 249 | 7 | ▶ | if ( $read_plus_sign ne '+' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 250 | # The first 'read' didn't have a '+' on the third line, suspicious | ||||||||||
| 251 | 2 | Bio::Tradis::Exception::TagFilterError->throw( error => "There was a problem filtering reads by the specified tag. Please check all input files are Fastq formatted and that at least one read in each starts with the specified tag\n" ); | |||||||||
| 252 | } | ||||||||||
| 253 | # I'm not proposing further (more detailed) validation here | ||||||||||
| 254 | 5 | close $filtered_file; | |||||||||
| 255 | } | ||||||||||
| 256 | |||||||||||
| 257 | 3 | ▶ | sub _remove { | ||||||||
|
Subroutine
_remove: called CC=1 SCAR=0.0
| |||||||||||
| 258 | 3 | my ($self) = @_; | |||||||||
| 259 | 3 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 260 | 3 | my $tag = $self->tag; | |||||||||
| 261 | 3 | my $mm = $self->mismatch; | |||||||||
| 262 | |||||||||||
| 263 | 3 | my $rm_tags = Bio::Tradis::RemoveTags->new( | |||||||||
| 264 | fastqfile => "$temporary_directory/filter.fastq", | ||||||||||
| 265 | tag => $tag, | ||||||||||
| 266 | mismatch => $mm, | ||||||||||
| 267 | outfile => "$temporary_directory/tags_removed.fastq" | ||||||||||
| 268 | )->remove_tags; | ||||||||||
| 269 | } | ||||||||||
| 270 | |||||||||||
| 271 | 5 | ▶ | sub _map { | ||||||||
|
Subroutine
_map: called CC=2 SCAR=6.9
| |||||||||||
| 272 | 5 | my ($self) = @_; | |||||||||
| 273 | 5 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 274 | |||||||||||
| 275 | 5 | my $ref = $self->reference; | |||||||||
| 276 | 5 | ▶ | my $fqfile = !defined($self->tag) ? $self->_unzipped_fastq : "$temporary_directory/tags_removed.fastq"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 277 | |||||||||||
| 278 | 5 | my $mapping = Bio::Tradis::Map->new( | |||||||||
| 279 | fastqfile => $fqfile, | ||||||||||
| 280 | reference => "$ref", | ||||||||||
| 281 | refname => "$temporary_directory/ref.index", | ||||||||||
| 282 | outfile => "$temporary_directory/mapped.sam", | ||||||||||
| 283 | min_seed_len => $self->min_seed_len, | ||||||||||
| 284 | smalt_k => $self->smalt_k, | ||||||||||
| 285 | smalt_s => $self->smalt_s, | ||||||||||
| 286 | smalt_y => $self->smalt_y, | ||||||||||
| 287 | smalt_r => $self->smalt_r, | ||||||||||
| 288 | smalt_n => $self->smalt_n, | ||||||||||
| 289 | smalt => $self->smalt | ||||||||||
| 290 | ); | ||||||||||
| 291 | 5 | $mapping->index_ref; | |||||||||
| 292 | 5 | $mapping->do_mapping; | |||||||||
| 293 | } | ||||||||||
| 294 | |||||||||||
| 295 | 5 | ▶ | sub _sam2bam { | ||||||||
|
Subroutine
_sam2bam: called CC=1 SCAR=0.0
| |||||||||||
| 296 | 5 | my ($self) = @_; | |||||||||
| 297 | 5 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 298 | |||||||||||
| 299 | 5 | system( | |||||||||
| 300 | $self->samtools_exec." view -b -o $temporary_directory/mapped.bam -S $temporary_directory/mapped.sam" | ||||||||||
| 301 | ); | ||||||||||
| 302 | 5 | return 1; | |||||||||
| 303 | } | ||||||||||
| 304 | |||||||||||
| 305 | 5 | ▶ | sub _sort_bam { | ||||||||
|
Subroutine
_sort_bam: called CC=1 SCAR=0.4
| |||||||||||
| 306 | 5 | my ($self) = @_; | |||||||||
| 307 | 5 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 308 | |||||||||||
| 309 | 5 | my $samtools_obj = Bio::Tradis::Samtools->new(exec => $self->samtools_exec, threads => $self->smalt_n); | |||||||||
| 310 | 5 | $samtools_obj->run_sort("$temporary_directory/mapped.bam","$temporary_directory/mapped.sort.bam"); | |||||||||
| 311 | 0 | $samtools_obj->run_index("$temporary_directory/mapped.sort.bam"); | |||||||||
| 312 | 0 | return 1; | |||||||||
| 313 | } | ||||||||||
| 314 | |||||||||||
| 315 | 0 | ▶ | sub _bamcheck { | ||||||||
|
Subroutine
_bamcheck: not called CC=1 SCAR=6.9
| |||||||||||
| 316 | 0 | my ($self) = @_; | |||||||||
| 317 | 0 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 318 | |||||||||||
| 319 | 0 | system( | |||||||||
| 320 | $self->samtools_exec." stats $temporary_directory/mapped.sort.bam > $temporary_directory/mapped.bamcheck" | ||||||||||
| 321 | ); | ||||||||||
| 322 | 0 | return 1; | |||||||||
| 323 | } | ||||||||||
| 324 | |||||||||||
| 325 | 0 | ▶ | sub _make_plot { | ||||||||
|
Subroutine
_make_plot: not called CC=3 SCAR=24.8
| |||||||||||
| 326 | 0 | my ($self) = @_; | |||||||||
| 327 | 0 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 328 | 0 | my $ref = $self->reference; | |||||||||
| 329 | 0 | my $outfile = $self->outfile; | |||||||||
| 330 | 0 | my $tr_d = $self->tagdirection; | |||||||||
| 331 | |||||||||||
| 332 | 0 | my $plot = Bio::Tradis::TradisPlot->new( | |||||||||
| 333 | mappedfile => "$temporary_directory/mapped.sort.bam", | ||||||||||
| 334 | mapping_score => $self->mapping_score, | ||||||||||
| 335 | outfile => "$temporary_directory/$outfile" | ||||||||||
| 336 | )->plot; | ||||||||||
| 337 | |||||||||||
| 338 | # if tag direction is 5, reverse plot columns | ||||||||||
| 339 | 0 | ▶ | if ( $self->tagdirection eq '5' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 340 | 0 | ▶ | print STDERR "Tag direction = 5. Reversing plot..\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 341 | 0 | $self->_reverse_plots; | |||||||||
| 342 | } | ||||||||||
| 343 | 0 | return 1; | |||||||||
| 344 | } | ||||||||||
| 345 | |||||||||||
| 346 | 0 | ▶ | sub _reverse_plots { | ||||||||
|
Subroutine
_reverse_plots: not called CC=3 SCAR=21.1
| |||||||||||
| 347 | 0 | my ($self) = @_; | |||||||||
| 348 | 0 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 349 | 0 | my $outfile = $self->outfile; | |||||||||
| 350 | 0 | my @seqnames = keys %{ $self->_sequence_info }; | |||||||||
| 351 | |||||||||||
| 352 | 0 | my @current_plots = | |||||||||
| 353 | 5 | ▶ | glob("$temporary_directory/$outfile.*.insert_site_plot.gz"); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 354 | |||||||||||
| 355 | 0 | foreach my $plotname (@current_plots) { | |||||||||
| 356 | 0 | ▶ | print STDERR "Reversing $plotname\n" if($self->verbose); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 357 | |||||||||||
| 358 | #my $plotname = $self->_plotname($sn); | ||||||||||
| 359 | 0 | system("gunzip -c $plotname > $temporary_directory/tmp.plot"); | |||||||||
| 360 | 0 | system( | |||||||||
| 361 | "awk '{ t = \$1; \$1 = \$2; \$2 = t; print; }' $temporary_directory/tmp.plot > rv_plot" | ||||||||||
| 362 | ); | ||||||||||
| 363 | 0 | system("gzip -c rv_plot > $plotname"); | |||||||||
| 364 | } | ||||||||||
| 365 | 0 | unlink("$temporary_directory/tmp.plot"); | |||||||||
| 366 | 0 | unlink("rv_plot"); | |||||||||
| 367 | } | ||||||||||
| 368 | |||||||||||
| 369 | 0 | ▶ | sub _stats { | ||||||||
|
Subroutine
_stats: not called CC=5 SCAR=34.0
| |||||||||||
| 370 | 0 | my ($self) = @_; | |||||||||
| 371 | 0 | my $outfile = $self->outfile; | |||||||||
| 372 | 0 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 373 | 0 | my $fq = $self->_unzipped_fastq; | |||||||||
| 374 | 0 | my $seq_info = $self->_sequence_info; | |||||||||
| 375 | |||||||||||
| 376 | #write header to stats file | ||||||||||
| 377 | 0 | $self->_write_stats_header; | |||||||||
| 378 | |||||||||||
| 379 | # Add file name and number of reads in it | ||||||||||
| 380 | 0 | my @fql = split( "/", $fq ); | |||||||||
| 381 | 0 | my $stats = "$fql[-1],"; | |||||||||
| 382 | 0 | my $total_reads = `wc $fq | awk '{print \$1/4}'`; | |||||||||
| 383 | 0 | chomp($total_reads); | |||||||||
| 384 | 0 | $stats .= "$total_reads,"; | |||||||||
| 385 | |||||||||||
| 386 | # Matching reads | ||||||||||
| 387 | 0 | my $matching = $total_reads; | |||||||||
| 388 | 0 | ▶ | if (defined($self->tag)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 389 | 0 | $matching = `wc $temporary_directory/filter.fastq | awk '{print \$1/4}' `; | |||||||||
| 390 | 0 | chomp($matching); | |||||||||
| 391 | } | ||||||||||
| 392 | 0 | $stats .= "$matching,"; | |||||||||
| 393 | 0 | $stats .= ( $matching / $total_reads ) * 100 . ","; | |||||||||
| 394 | |||||||||||
| 395 | # Mapped reads | ||||||||||
| 396 | 0 | my $mapped = $self->_number_of_mapped_reads; | |||||||||
| 397 | 0 | $stats .= "$mapped,"; | |||||||||
| 398 | 0 | $stats .= ( $mapped / $matching ) * 100 . ","; | |||||||||
| 399 | |||||||||||
| 400 | # Unique insertion sites | ||||||||||
| 401 | 0 | my ( $total_uis, $total_seq_len ); | |||||||||
| 402 | 0 | foreach my $si ( keys %{$seq_info} ) { | |||||||||
| 403 | 0 | my $plotname = $self->_plotname($si); | |||||||||
| 404 | 0 | system( | |||||||||
| 405 | "gunzip -c $temporary_directory/$plotname > $temporary_directory/tmp.plot" | ||||||||||
| 406 | ); | ||||||||||
| 407 | 0 | my $uis = `grep -c -v "0 0" $temporary_directory/tmp.plot`; | |||||||||
| 408 | 0 | chomp($uis); | |||||||||
| 409 | 0 | $total_uis += $uis; | |||||||||
| 410 | 0 | $stats .= "$uis,"; | |||||||||
| 411 | 0 | my $seqlen = ${$seq_info}{$si}; | |||||||||
| 412 | 0 | $total_seq_len += $seqlen; | |||||||||
| 413 | 0 | my $uis_per_seqlen = "NaN"; | |||||||||
| 414 | 0 | ▶ | $uis_per_seqlen = $seqlen / $uis if ( $uis > 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 415 | 0 | chomp($uis_per_seqlen); | |||||||||
| 416 | 0 | $stats .= "$uis_per_seqlen,"; | |||||||||
| 417 | } | ||||||||||
| 418 | 0 | $stats .= "$total_uis,"; | |||||||||
| 419 | 0 | my $t_uis_p_l = "NaN"; | |||||||||
| 420 | 0 | ▶ | $t_uis_p_l = $total_seq_len / $total_uis if ( $total_uis > 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 421 | 0 | $stats .= "$t_uis_p_l\n"; | |||||||||
| 422 | 0 | print { $self->_stats_handle } $stats; | |||||||||
| 423 | } | ||||||||||
| 424 | |||||||||||
| 425 | 0 | ▶ | sub _write_stats_header { | ||||||||
|
Subroutine
_write_stats_header: not called CC=2 SCAR=17.9
| |||||||||||
| 426 | 0 | my ($self) = @_; | |||||||||
| 427 | 0 | my @seqnames = keys %{ $self->_sequence_info }; | |||||||||
| 428 | 0 | my @fields = ( | |||||||||
| 429 | "File", | ||||||||||
| 430 | "Total Reads", | ||||||||||
| 431 | "Reads Matched", | ||||||||||
| 432 | "\% Matched", | ||||||||||
| 433 | "Reads Mapped", | ||||||||||
| 434 | "\% Mapped" | ||||||||||
| 435 | ); | ||||||||||
| 436 | 0 | print { $self->_stats_handle } join( ",", @fields ) . ","; | |||||||||
| 437 | 0 | foreach my $sn (@seqnames) { | |||||||||
| 438 | 0 | print { $self->_stats_handle } "Unique Insertion Sites : $sn,"; | |||||||||
| 439 | 0 | print { $self->_stats_handle } "Seq Len/UIS : $sn,"; | |||||||||
| 440 | } | ||||||||||
| 441 | 0 | print { $self->_stats_handle } "Total Unique Insertion Sites,"; | |||||||||
| 442 | 0 | print { $self->_stats_handle } "Total Seq Len/Total UIS\n"; | |||||||||
| 443 | } | ||||||||||
| 444 | |||||||||||
| 445 | 0 | ▶ | sub _plotname { | ||||||||
|
Subroutine
_plotname: not called CC=1 SCAR=6.9
| |||||||||||
| 446 | 0 | my ( $self, $seq_name ) = @_; | |||||||||
| 447 | 0 | my $outfile = $self->outfile; | |||||||||
| 448 | |||||||||||
| 449 | 0 | $seq_name =~ s/[^\w\d\.]/_/g; | |||||||||
| 450 | 0 | my $plotfile_name = "$outfile.$seq_name.insert_site_plot.gz"; | |||||||||
| 451 | 0 | return $plotfile_name; | |||||||||
| 452 | } | ||||||||||
| 453 | |||||||||||
| 454 | 0 | ▶ | sub _number_of_mapped_reads { | ||||||||
|
Subroutine
_number_of_mapped_reads: not called CC=3 SCAR=24.8
| |||||||||||
| 455 | 0 | my ($self) = @_; | |||||||||
| 456 | 0 | my $temporary_directory = $self->_temp_directory; | |||||||||
| 457 | |||||||||||
| 458 | 0 | my $pars = | |||||||||
| 459 | Bio::Tradis::Parser::Bam->new( | ||||||||||
| 460 | file => "$temporary_directory/mapped.bam" ); | ||||||||||
| 461 | 0 | my $c = 0; | |||||||||
| 462 | 0 | ▶ | while ( $pars->next_read ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 463 | 0 | ▶ | if ( $pars->is_mapped ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 464 | 0 | $c++; | |||||||||
| 465 | } | ||||||||||
| 466 | } | ||||||||||
| 467 | 0 | return $c; | |||||||||
| 468 | } | ||||||||||
| 469 | |||||||||||
| 470 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||
| 471 | 5 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 472 | 5 | 1; | |||||||||
| 473 | |||||||||||
| 474 | __END__ | ||||||||||