Bio-Tradis
view release on metacpan or search on metacpan
lib/Bio/Tradis/CombinePlots.pm view on Meta::CPAN
my @file_hs;
foreach my $f (@files){
open(my $fh, "<", $f);
push(@file_hs, $fh);
}
return ( id => $id, len => $len, files => \@file_hs );
}
sub _get_file_len {
my ( $self, $files ) = @_;
#check all files are of equal lens and return len if true
#wc misses last line - return $l++
my @lens;
for my $f ( @{$files} ) {
my $wc = `wc $f | awk '{print \$1}'`;
chomp $wc;
push( @lens, $wc );
}
my $l = shift @lens;
for my $x (@lens) {
return 0 if ( $x != $l );
}
return $l+1;
}
sub _combine_lines {
my ( $self, $lines ) = @_;
my @totals = ( 0, 0 );
foreach my $l ( @{$lines} ) {
if(!defined($l)){
return "";
next;
}
my @cols = split( /\s+/, $l );
$totals[0] += $cols[0];
$totals[1] += $cols[1];
}
return join( " ", @totals );
}
sub _write_stats_header {
my ($self) = @_;
my @fields =
( "ID", "Sequence Length", "Unique Insertion Sites", "Seq Len/UIS" );
print { $self->_stats_handle } join( ",", @fields ) . "\n";
return 1;
}
sub _write_stats {
my ( $self, $id, $seq_len ) = @_;
my $combined_dir = $self->combined_dir;
my $comb_plot = "$combined_dir/$id.insert_site_plot";
#my $seq_len = `wc $comb_plot | awk '{print \$1}'`;
#chomp($seq_len);
my $uis = `grep -c -v "0 0" $comb_plot`;
chomp($uis);
my $sl_per_uis = "NaN";
$sl_per_uis = $seq_len / $uis if($uis > 0);
my $stats = "$id,$seq_len,$uis,$sl_per_uis\n";
print { $self->_stats_handle } $stats;
return 1;
}
sub _abs_path_list {
my ( $self, $files ) = @_;
my $plot_path = $self->_get_plotfile_path;
my @pathlist;
foreach my $f ( @{$files} ) {
if ( $f =~ /^\// ) { push( @pathlist, $f ); }
else { push( @pathlist, $plot_path . $f ); }
}
return \@pathlist;
}
sub _get_plotfile_path {
my ($self) = @_;
my $plotfile = $self->plotfile;
my @dirs = split( '/', $plotfile );
pop(@dirs);
my $path2plot = join( '/', @dirs );
return "$path2plot/";
}
sub _is_gz {
my ( $self, $plotname ) = @_;
if ( $plotname =~ /\.gz$/ ) {
return 1;
}
else {
return 0;
}
}
sub _unzip_plots {
my ( $self, $files ) = @_;
my $destination_directory = $self->_destination;
my @filelist = @{ $self->_abs_path_list($files) };
my @unz_plots;
foreach my $plotname ( @filelist ) {
Bio::Tradis::Analysis::Exceptions::FileNotFound->throw("Cannot find $plotname\n") unless ( -e $plotname );
if ( $self->_is_gz($plotname) ) {
$plotname =~ /([^\/]+$)/;
my $unz = $1;
$unz =~ s/\.gz//;
my $unzip_cmd = "gunzip -c $plotname > $destination_directory/$unz";
system($unzip_cmd);
push(@unz_plots, "$destination_directory/$unz");
}
else {
push(@unz_plots, $plotname);
}
( run in 1.241 second using v1.01-cache-2.11-cpan-5735350b133 )