App-RL
view release on metacpan or search on metacpan
lib/App/RL/Command/stat.pm view on Meta::CPAN
package App::RL::Command::stat;
use strict;
use warnings;
use autodie;
use App::RL -command;
use App::RL::Common;
sub abstract {
return 'coverage statistics on chromosomes for runlists';
}
sub opt_spec {
return (
[ "outfile|o=s", "output filename. [stdout] for screen" ],
[ "size|s=s", "chr.sizes", { required => 1 } ],
[ "remove|r", "remove 'chr0' from chromosome names" ],
[ "mk", "YAML file contains multiple sets of runlists" ],
[ "all", "only write whole genome stats" ],
{ show_defaults => 1, }
);
}
sub usage_desc {
return "runlist stat [options] <infile>";
}
sub description {
my $desc;
$desc .= ucfirst(abstract) . ".\n";
return $desc;
}
sub validate_args {
my ( $self, $opt, $args ) = @_;
if ( @{$args} != 1 ) {
my $message = "This command need one input file.\n\tIt found";
$message .= sprintf " [%s]", $_ for @{$args};
$message .= ".\n";
$self->usage_error($message);
}
for ( @{$args} ) {
next if lc $_ eq "stdin";
if ( !Path::Tiny::path($_)->is_file ) {
$self->usage_error("The input file [$_] doesn't exist.");
}
}
if ( !exists $opt->{outfile} ) {
$opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".csv";
}
}
sub execute {
my ( $self, $opt, $args ) = @_;
#----------------------------#
# Loading
#----------------------------#
my $infile; # YAML::Syck::LoadFile handles IO::*
if ( lc $args->[0] eq 'stdin' ) {
$infile = *STDIN;
}
else {
$infile = $args->[0];
}
my $length_of = App::RL::Common::read_sizes( $opt->{size}, $opt->{remove} );
my $s_of = {};
my @keys;
if ( $opt->{mk} ) {
my $yml = YAML::Syck::LoadFile($infile);
@keys = sort keys %{$yml};
for my $key (@keys) {
$s_of->{$key}
= App::RL::Common::runlist2set( $yml->{$key}, $opt->{remove} );
}
}
else {
@keys = ("__single");
$s_of->{__single}
= App::RL::Common::runlist2set( YAML::Syck::LoadFile($infile), $opt->{remove} );
}
#----------------------------#
# Calcing
#----------------------------#
my $out_fh;
if ( lc( $opt->{outfile} ) eq "stdout" ) {
$out_fh = *STDOUT;
}
( run in 0.620 second using v1.01-cache-2.11-cpan-39bf76dae61 )