App-Fasops

 view release on metacpan or  search on metacpan

lib/App/Fasops/Command/create.pm  view on Meta::CPAN

package App::Fasops::Command::create;
use strict;
use warnings;
use autodie;

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

sub abstract {
    return 'create blocked fasta files from links of ranges';
}

sub opt_spec {
    return (
        [ "outfile|o=s", "Output filename. [stdout] for screen" ],
        [ "genome|g=s",  "Reference genome file", { required => 1 }, ],
        [ "name|n=s",    "Default name for ranges", ],
        { show_defaults => 1, }
    );
}

sub usage_desc {
    return "fasops create [options] <infiles>";
}

sub description {
    my $desc;
    $desc .= ucfirst(abstract) . ".\n";
    $desc .= <<'MARKDOWN';

* Need `samtools` in $PATH

MARKDOWN

    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 . ".fas";
    }

}

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

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

    #----------------------------#
    # Loading
    #----------------------------#
    my $info_of = {};
    for my $file ( @{$args} ) {
        for my $line ( App::RL::Common::read_lines($file) ) {
            $info_of = App::Fasops::Common::build_info( [$line], $info_of );
            my @parts;
            for my $part ( split /\t/, $line ) {
                next unless exists $info_of->{$part};
                push @parts, $part;
            }
            next unless @parts >= 2;

            for my $range (@parts) {
                my $info     = $info_of->{$range};
                my $location = sprintf "%s:%d-%d", $info->{chr}, $info->{start}, $info->{end};
                my $seq      = App::Fasops::Common::get_seq_faidx( $opt->{genome}, $location );
                if ( defined $info->{strand} and $info->{strand} ne "+" ) {
                    $seq = App::Fasops::Common::revcom($seq);
                }
                if ( $opt->{name} ) {
                    $info->{name} = $opt->{name};
                    $range = App::RL::Common::encode_header($info);
                }



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