lib/Bio/Tradis/CombinePlots.pm

filter: Statementstmt 98.4% 186 / 189 Branchbran 80.5% 29 / 36 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 24 / 24 Podpod 0.0% 0 / 1 totaltotal 92.8% 246 / 265 CC 31 SCAR 17.7
CC
31
Cov
94%
combine
15.1
_get_file_len
4.0
_unzip_plots
4.0
CRAP 5.8
« lib/Bio/Tradis/Parser/Bam.pm ↑ Summary blib/lib/Bio/Tradis/CommandLine/PlotCombine.pm »
1 package Bio::Tradis::CombinePlots;
2 1 $Bio::Tradis::CombinePlots::VERSION = '1.4.1';
3 # ABSTRACT: Combine multiple plotfiles and generate updated statistics for the combined files
4  
5  
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Path qw( remove_tree );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Bio::Tradis::Analysis::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 has 'plotfile' => ( is => 'rw', isa => 'Str', required => 1 );
16 1 has 'combined_dir' => ( is => 'rw', isa => 'Str', default => 'combined' );
17 1 has '_plot_handle' => (
18 is => 'ro',
19 isa => 'FileHandle',
20 required => 0,
21 lazy => 1,
22 builder => '_build__plot_handle'
23 );
24 1 has '_stats_handle' => (
25 is => 'ro',
26 isa => 'FileHandle',
27 required => 0,
28 lazy => 1,
29 builder => '_build__stats_handle'
30 );
31 1 has '_ordered_plot_ids' => (
32 is => 'rw',
33 isa => 'ArrayRef',
34 required => 0,
35 lazy => 1,
36 builder => '_build__ordered_plot_ids'
37 );
38 1 has '_destination' => (
39 is => 'rw',
40 isa => 'Str',
41 required => 0,
42 lazy => 1,
43 builder => '_build__destination'
44 );
45  
46 sub _build__destination {
47 3 ▶ my $tmp_dir = File::Temp->newdir( DIR=> getcwd, CLEANUP => 0 );
Subroutine _build__destination: called CC=1 SCAR=0.0
48 3 return $tmp_dir->dirname;
49 }
50  
51 sub _build__stats_handle {
52 3 ▶ my ($self) = @_;
Subroutine _build__stats_handle: called CC=1 SCAR=0.0
53 3 my $filelist = $self->plotfile;
54 3 $filelist =~ s/([^\/]+$)//;
55 3 my $filename = $1;
56 3 $filename =~ s/[^\.]+$/stats/;
57 3 open( my $stats, ">", $filename );
58 3 return $stats;
59 }
60  
61 sub _build__plot_handle {
62 3 ▶ my ($self) = @_;
Subroutine _build__plot_handle: called CC=1 SCAR=0.0
63 3 my $plot = $self->plotfile;
64 3 open( my $plot_h, "<", $plot );
65 3 return $plot_h;
66 }
67  
68 sub _build__ordered_plot_ids {
69 3 ▶ my ($self) = @_;
Subroutine _build__ordered_plot_ids: called CC=2 SCAR=6.9
70 3 my $filelist = $self->plotfile;
71  
72 3 my @id_order = `awk '{print \$1}' $filelist`;
73 3 foreach my $id (@id_order) {
74 5 chomp($id);
75 }
76 3 return \@id_order;
77 }
78  
79 sub combine {
80 3 ▶ my ($self) = @_;
Subroutine combine: called CC=15 SCAR=27.1
Pod undocumented
81 3 my $ordered_keys = $self->_ordered_plot_ids;
82 3 my $plot_handle = $self->_plot_handle;
83 3 my $combined_dir = $self->combined_dir;
84  
85 3 $self->_write_stats_header;
86  
87  
88 3 ▶ system("mkdir $combined_dir") unless ( -d "$combined_dir" );
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$combined_dir" 2 1 3
89 3 my @tabix_plot;
90  
91 3 ▶ while ( my $line = <$plot_handle> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $plot_handle)) 5 3 8
92 #parse line into hash. keys = id, len, files. unzips files if needed.
93 5 my %plothash = $self->_parse_line($line);
94 5 my $id = $plothash{'id'};
95  
96 #create output plot file
97 5 my $comb_plot_name = "$combined_dir/$id.insert_site_plot";
98 5 my $filelen = $plothash{'len'};
99 5 my ( @currentlines, $this_line );
100  
101 5 my @full_plot;
102 5 foreach my $i ( 0 .. $filelen ) {
103 195 @currentlines = ();
104  
105 195 foreach my $curr_fh ( @{ $plothash{'files'} } ) {
106 390 $this_line = <$curr_fh>;
107 390 ▶ push( @currentlines, $this_line ) if( defined $line && $line ne "");
Branch: 1 branch
BranchTrueFalseTotal
if defined $line and $line ne "" 390 0 390
Condition33%1/3
defined $line and $line ne ""
!ll&&!rl&&r
00390
MC/DC0%0/2
defined $line and $line ne ""
defined $line $line ne ""
Truth table33%1/3
A and B
A= defined $lineB= $line ne ""
ABresult
0X0
100
111
108 }
109  
110 195 my $comb_line = $self->_combine_lines( \@currentlines );
111  
112 195 my $plot_values_tabix = $comb_line;
113 195 ▶ $plot_values_tabix =~ s/\s/\t/ if(defined $plot_values_tabix && $plot_values_tabix ne "");
Branch: 1 branch
BranchTrueFalseTotal
if defined $plot_values_tabix and $plot_values_tabix ne "" 190 5 195
Condition66%2/3
defined $plot_values_tabix and $plot_values_tabix ne ""
!ll&&!rl&&r
05190
MC/DC50%1/2
defined $plot_values_tabix and $plot_values_tabix ne ""
defined $plot_values_tabix $plot_values_tabix ne ""
Truth table66%2/3
A and B
A= defined $plot_values_tabixB= $plot_values_tabix ne ""
ABresult
0X0
100
111
114  
115 195 my $tabix_line;
116 195 ▶ if ($id !~ m/^zip_combined/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id =~ /^zip_combined/) 94 101 195
117 94 ▶ my $tabix_line = "$id\t$i\t" . $plot_values_tabix if( defined $plot_values_tabix && $plot_values_tabix ne "");
Branch: 1 branch
BranchTrueFalseTotal
if defined $plot_values_tabix and $plot_values_tabix ne "" 90 4 94
Condition66%2/3
defined $plot_values_tabix and $plot_values_tabix ne ""
!ll&&!rl&&r
0490
MC/DC50%1/2
defined $plot_values_tabix and $plot_values_tabix ne ""
defined $plot_values_tabix $plot_values_tabix ne ""
Truth table66%2/3
A and B
A= defined $plot_values_tabixB= $plot_values_tabix ne ""
ABresult
0X0
100
111
118 94 ▶ push( @tabix_plot, $tabix_line ) if( $comb_line ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $comb_line ne "" 90 4 94
119 }
120  
121 195 ▶ push(@full_plot, $comb_line) if ( $comb_line ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $comb_line ne "" 190 5 195
122 }
123  
124 5 open( CPLOT, '>', $comb_plot_name );
125 5 print CPLOT join("\n", @full_plot);
126 5 close(CPLOT);
127  
128 5 $self->_write_stats($id, $filelen);
129 5 system("gzip -f $comb_plot_name");
130 }
131  
132  
133 3 ▶ if (@tabix_plot) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tabix_plot) 2 1 3
134 2 $self->_prepare_and_create_tabix_for_combined_plots(\@tabix_plot);
135 }
136  
137 3 File::Temp::cleanup();
138 # double check tmp dir is deleted. cleanup not working properly
139 3 remove_tree($self->_destination);
140 3 return 1;
141 }
142  
143 sub _prepare_and_create_tabix_for_combined_plots {
144  
145 2 ▶ my ($self, $tabix_plot) = @_;
Subroutine _prepare_and_create_tabix_for_combined_plots: called CC=2 SCAR=6.9
146  
147 2 my $tabix_plot_name = "combined/tabix.insert_site_plot.gz";
148 2 my $sorted_tabix_plot_name = "combined/tabix_sorted.insert_site_plot.gz";
149  
150 2 ▶ open(my $tabix_plot_fh , '|-', " gzip >". $tabix_plot_name) or warn "Couldn't create the initial plot file for tabix";
Branch: 1 branch
BranchTrueFalseTotal
open my $tabix_plot_fh, "|-", ' gzip >' . $tabix_plot_name or warn "Couldn't create the initial plot file for tabix" 0 2 2
151 2 print $tabix_plot_fh join( "\n", @{ $tabix_plot } );
152 2 close($tabix_plot_fh);
153  
154 2 `cat $tabix_plot_name | gunzip - | sort -k1,1 -k2,2n | bgzip > $sorted_tabix_plot_name && tabix -b 2 -e 2 $sorted_tabix_plot_name`;
155 2 unlink($tabix_plot_name);
156  
157 }
158  
159  
160 sub _parse_line {
161 5 ▶ my ( $self, $line ) = @_;
Subroutine _parse_line: called CC=3 SCAR=11.0
162 5 chomp $line;
163 5 my @fields = split( /\s+/, $line );
164 5 my $id = shift @fields;
165 5 my @files = @{ $self->_unzip_plots(\@fields) };
166 5 my $len = $self->_get_file_len( \@files );
167 5 ▶ if ( $len == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 0) 0 5 5
168 0 die "\nPlots with ID $id not of equal length.\n";
169 }
170 #build file handles for each file
171 5 my @file_hs;
172 5 foreach my $f (@files){
173 10 open(my $fh, "<", $f);
174 10 push(@file_hs, $fh);
175 }
176 5 return ( id => $id, len => $len, files => \@file_hs );
177 }
178  
179 sub _get_file_len {
180 5 ▶ my ( $self, $files ) = @_;
Subroutine _get_file_len: called CC=4 SCAR=13.9
181  
182 #check all files are of equal lens and return len if true
183 #wc misses last line - return $l++
184 5 my @lens;
185 5 for my $f ( @{$files} ) {
186 10 my $wc = `wc $f | awk '{print \$1}'`;
187 10 chomp $wc;
188 10 push( @lens, $wc );
189 }
190  
191 5 my $l = shift @lens;
192 5 for my $x (@lens) {
193 5 ▶ return 0 if ( $x != $l );
Branch: 1 branch
BranchTrueFalseTotal
if $x != $l 0 5 5
194 }
195 5 return $l+1;
196 }
197  
198 sub _combine_lines {
199 195 ▶ my ( $self, $lines ) = @_;
Subroutine _combine_lines: called CC=3 SCAR=11.0
200  
201 195 my @totals = ( 0, 0 );
202 195 foreach my $l ( @{$lines} ) {
203 385 ▶ if(!defined($l)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $l) 5 380 385
204 5 return "";
205 0 next;
206 }
207 380 my @cols = split( /\s+/, $l );
208 380 $totals[0] += $cols[0];
209 380 $totals[1] += $cols[1];
210 }
211 190 return join( " ", @totals );
212 }
213  
214 sub _write_stats_header {
215 3 ▶ my ($self) = @_;
Subroutine _write_stats_header: called CC=1 SCAR=0.0
216 3 my @fields =
217 ( "ID", "Sequence Length", "Unique Insertion Sites", "Seq Len/UIS" );
218 3 print { $self->_stats_handle } join( ",", @fields ) . "\n";
219 3 return 1;
220 }
221  
222 sub _write_stats {
223 5 ▶ my ( $self, $id, $seq_len ) = @_;
Subroutine _write_stats: called CC=2 SCAR=6.9
224 5 my $combined_dir = $self->combined_dir;
225 5 my $comb_plot = "$combined_dir/$id.insert_site_plot";
226  
227 #my $seq_len = `wc $comb_plot | awk '{print \$1}'`;
228 #chomp($seq_len);
229 5 my $uis = `grep -c -v "0 0" $comb_plot`;
230 5 chomp($uis);
231 5 my $sl_per_uis = "NaN";
232 5 ▶ $sl_per_uis = $seq_len / $uis if($uis > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $uis > 0 5 0 5
233  
234 5 my $stats = "$id,$seq_len,$uis,$sl_per_uis\n";
235 5 print { $self->_stats_handle } $stats;
236  
237 5 return 1;
238 }
239  
240 sub _abs_path_list {
241 5 ▶ my ( $self, $files ) = @_;
Subroutine _abs_path_list: called CC=3 SCAR=11.2
242 5 my $plot_path = $self->_get_plotfile_path;
243  
244 5 my @pathlist;
245 5 foreach my $f ( @{$files} ) {
246 10 ▶ if ( $f =~ /^\// ) { push( @pathlist, $f ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ m[^/]) { } 0 10 10
247 10 else { push( @pathlist, $plot_path . $f ); }
248 }
249 5 return \@pathlist;
250 }
251  
252 sub _get_plotfile_path {
253 5 ▶ my ($self) = @_;
Subroutine _get_plotfile_path: called CC=1 SCAR=0.0
254 5 my $plotfile = $self->plotfile;
255  
256 5 my @dirs = split( '/', $plotfile );
257 5 pop(@dirs);
258 5 my $path2plot = join( '/', @dirs );
259 5 return "$path2plot/";
260 }
261  
262 sub _is_gz {
263 10 ▶ my ( $self, $plotname ) = @_;
Subroutine _is_gz: called CC=2 SCAR=6.9
264  
265 10 ▶ if ( $plotname =~ /\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plotname =~ /\.gz$/) { } 2 8 10
266 2 return 1;
267 }
268 else {
269 8 return 0;
270 }
271 }
272  
273 sub _unzip_plots {
274 5 ▶ my ( $self, $files ) = @_;
Subroutine _unzip_plots: called CC=4 SCAR=13.9
275 5 my $destination_directory = $self->_destination;
276  
277 5 my @filelist = @{ $self->_abs_path_list($files) };
278 5 my @unz_plots;
279 5 foreach my $plotname ( @filelist ) {
280 10 ▶ Bio::Tradis::Analysis::Exceptions::FileNotFound->throw("Cannot find $plotname\n") unless ( -e $plotname );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $plotname 0 10 10
281 10 ▶ if ( $self->_is_gz($plotname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_gz($plotname)) { } 2 8 10
282 2 $plotname =~ /([^\/]+$)/;
283 2 my $unz = $1;
284 2 $unz =~ s/\.gz//;
285 2 my $unzip_cmd = "gunzip -c $plotname > $destination_directory/$unz";
286 2 system($unzip_cmd);
287 2 push(@unz_plots, "$destination_directory/$unz");
288 }
289 else {
290 8 push(@unz_plots, $plotname);
291 }
292 }
293 5 return \@unz_plots;
294 }
295  
296 1 __PACKAGE__->meta->make_immutable;
297 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
298 1 1;
299  
300 __END__
301  
302 =pod
303  
304 =encoding UTF-8
305  
306 =head1 NAME
307  
308 Bio::Tradis::CombinePlots - Combine multiple plotfiles and generate updated statistics for the combined files
309  
310 =head1 VERSION
311  
312 version 1.4.1
313  
314 =head1 SYNOPSIS
315  
316 Takes a tab-delimited file with an ID as the first column followed by
317 a list of plotfiles to combine per row. The ID will be used to name the new
318 plotfile and as an identifier in the stats file, so ensure these are unique.
319  
320 For example, an input file named plots_to_combine.txt:
321  
322 tradis1 plot1.1.gz plot1.2.gz plot1.3.gz
323 tradis2 plot2.1.gz plot2.2.gz
324 tradis3 plot3.1.gz plot3.2.gz plot3.3.gz plot3.4.gz
325  
326 will produce
327  
328 =over
329  
330 1. a directory named combined with 3 files - tradis1.insertion_site_plot.gz,
331 tradis2.insertion_site_plot.gz, tradis3.insertion_site_plot.gz
332 2. a stats file named plots_to_combine.stats
333  
334 =back
335  
336 =head1 USAGE
337  
338 use Bio::Tradis::CombinePlots;
339
340 my $pipeline = Bio::Tradis::CombinePlots->new(plotfile => 'abc');
341 $pipeline->combine;
342  
343 =head1 AUTHOR
344  
345 Carla Cummins <path-help@sanger.ac.uk>
346  
347 =head1 COPYRIGHT AND LICENSE
348  
349 This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
350  
351 This is free software, licensed under:
352  
353 The GNU General Public License, Version 3, June 2007
354  
355 =cut
« lib/Bio/Tradis/Parser/Bam.pm ↑ Summary blib/lib/Bio/Tradis/CommandLine/PlotCombine.pm »