App-Rangeops
view release on metacpan or search on metacpan
lib/App/Rangeops/Command/clean.pm view on Meta::CPAN
package App::Rangeops::Command::clean;
use strict;
use warnings;
use autodie;
use App::Rangeops -command;
use App::Rangeops::Common;
sub abstract {
return 'replace ranges within links, incorporate hit strands and remove nested links';
}
sub opt_spec {
return (
[ "replace|r=s",
"Two-column tsv file, normally produced by command merge."
],
[ "bundle|b=i",
"Bundle overlapped links. This value is the overlapping size, default is [0]. Suggested value is [500].",
{ default => 0 },
],
[ "outfile|o=s", "Output filename. [stdout] for screen." ],
[ "verbose|v", "Verbose mode.", ],
);
}
sub usage_desc {
return "rangeops clean [options] <infiles>";
}
sub description {
my $desc;
$desc .= ucfirst(abstract) . ".\n";
$desc
.= "\t<infiles> are bilateral links files, with or without hit strands\n";
return $desc;
}
sub validate_args {
my ( $self, $opt, $args ) = @_;
if ( !@{$args} ) {
my $message = "This command need one or more input files.\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 . ".clean.tsv";
}
}
sub execute {
my ( $self, $opt, $args ) = @_;
#----------------------------#
# Load replaces
#----------------------------#
my $info_of = {}; # info of ranges
my $replace_of = {};
if ( $opt->{replace} ) {
print STDERR "==> Load replaces\n" if $opt->{verbose};
for my $line ( App::RL::Common::read_lines( $opt->{replace} ) ) {
$info_of = App::Rangeops::Common::build_info( [$line], $info_of );
my @parts = split /\t/, $line;
if ( @parts == 2 ) {
$replace_of->{ $parts[0] } = $parts[1];
}
}
}
#----------------------------#
# Replacing and incorporating
#----------------------------#
print STDERR "==> Incorporating strands\n" if $opt->{verbose};
my @lines;
for my $file ( @{$args} ) {
for my $line ( App::RL::Common::read_lines($file) ) {
$info_of = App::Rangeops::Common::build_info( [$line], $info_of );
my @new_parts;
# replacing
for my $part ( split /\t/, $line ) {
if ( exists $replace_of->{$part} ) {
my $original = $part;
my $replaced = $replace_of->{$part};
# create new info, don't touch anything of $info_of
( run in 0.427 second using v1.01-cache-2.11-cpan-39bf76dae61 )