App-RL

 view release on metacpan or  search on metacpan

lib/App/RL/Command/compare.pm  view on Meta::CPAN

package App::RL::Command::compare;
use strict;
use warnings;
use autodie;

use App::RL -command;
use App::RL::Common;

sub abstract {
    return 'compare 2 chromosome runlists';
}

sub opt_spec {
    return (
        [ "outfile|o=s", "output filename. [stdout] for screen" ],
        [ "op=s",     "operations: intersect, union, diff or xor", { default => "intersect" } ],
        [ "remove|r", "remove 'chr0' from chromosome names" ],
        [ "mk",       "*first* YAML file contains multiple sets of runlists" ],
        { show_defaults => 1, }
    );
}

sub usage_desc {
    return "runlist compare [options] <infile1> <infile2> [more infiles]";
}

sub description {
    my $desc;
    $desc .= ucfirst(abstract) . ".\n";
    return $desc;
}

sub validate_args {
    my ( $self, $opt, $args ) = @_;

    if ( @{$args} < 2 ) {
        my $message = "This command need two 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 ( $opt->{op} =~ /^dif/i ) {
        $opt->{op} = 'diff';
    }
    elsif ( $opt->{op} =~ /^uni/i ) {
        $opt->{op} = 'union';
    }
    elsif ( $opt->{op} =~ /^int/i ) {
        $opt->{op} = 'intersect';
    }
    elsif ( $opt->{op} =~ /^xor/i ) {
        $opt->{op} = 'xor';
    }
    else {
        Carp::confess "[@{[$opt->{op}]}] invalid\n";
    }

    if ( !exists $opt->{outfile} ) {
        $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . "." . $opt->{op} . ".yml";
    }
}

sub execute {
    my ( $self, $opt, $args ) = @_;

    #----------------------------#
    # Loading
    #----------------------------#
    my $chrs = Set::Scalar->new;

    # file1
    my $set_of = {};
    my @names;
    if ( $opt->{mk} ) {
        my $yml = YAML::Syck::LoadFile( $args->[0] );
        @names = sort keys %{$yml};

        for my $name (@names) {
            $set_of->{$name} = App::RL::Common::runlist2set( $yml->{$name}, $opt->{remove} );
            $chrs->insert( keys %{ $set_of->{$name} } );
        }
    }
    else {
        @names = ("__single");
        $set_of->{__single}
            = App::RL::Common::runlist2set( YAML::Syck::LoadFile( $args->[0] ), $opt->{remove} );



( run in 1.361 second using v1.01-cache-2.11-cpan-39bf76dae61 )