App-RL

 view release on metacpan or  search on metacpan

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

    }

    if ( $opt->{op} =~ /^overlap/i ) {
        $opt->{op} = 'overlap';
    }
    elsif ( $opt->{op} =~ /^non/i ) {
        $opt->{op} = 'non-overlap';
    }
    elsif ( $opt->{op} =~ /^superset/i ) {
        $opt->{op} = 'superset';
    }
    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;

    my $set_single
        = App::RL::Common::runlist2set( YAML::Syck::LoadFile( $args->[0] ), $opt->{remove} );
    $chrs->insert( keys %{$set_single} );

    #----------------------------#
    # Reading and Output
    #----------------------------#
    my $in_fh = IO::Zlib->new( $args->[1], "rb" );

    my $out_fh;
    if ( lc( $opt->{outfile} ) eq "stdout" ) {
        $out_fh = *STDOUT;
    }
    else {
        open $out_fh, ">", $opt->{outfile};
    }

    while ( !$in_fh->eof ) {
        my $line = $in_fh->getline;
        next if substr( $line, 0, 1 ) eq "#";
        chomp $line;

        my $info = App::RL::Common::decode_header($line);
        next unless App::RL::Common::info_is_valid($info);

        $info->{chr} =~ s/chr0?//i if $opt->{remove};
        my $cur_positions = App::RL::Common::new_set();
        $cur_positions->add_pair( $info->{start}, $info->{end} );

        if ( $opt->{op} eq "overlap" ) {
            if ( $chrs->has( $info->{chr} ) ) {
                my $chr_single = $set_single->{ $info->{chr} };
                if ( $chr_single->intersect($cur_positions)->is_not_empty ) {
                    printf {$out_fh} "%s\n", App::RL::Common::encode_header($info);
                }
            }
        }

        if ( $opt->{op} eq "non-overlap" ) {
            if ( $chrs->has( $info->{chr} ) ) {
                my $chr_single = $set_single->{ $info->{chr} };
                if ( $chr_single->intersect($cur_positions)->is_empty ) {
                    printf {$out_fh} "%s\n", App::RL::Common::encode_header($info);
                }
            }
            else {
                printf {$out_fh} "%s\n", App::RL::Common::encode_header($info);
            }
        }

        if ( $opt->{op} eq "superset" ) {
            if ( $chrs->has( $info->{chr} ) ) {
                my $chr_single = $set_single->{ $info->{chr} };
                if ( $chr_single->superset($cur_positions) ) {
                    printf {$out_fh} "%s\n", App::RL::Common::encode_header($info);
                }
            }
        }
    }

    $in_fh->close;
    close $out_fh;
}

1;



( run in 2.898 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )