App-Fasops

 view release on metacpan or  search on metacpan

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

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

use MCE;
use MCE::Flow Sereal => 1;
use MCE::Candy;

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

sub abstract {
    return 'realign blocked fasta file with external programs';
}

sub opt_spec {
    return (
        [ "outfile|o=s", "Output filename. [stdout] for screen" ],
        [ "outgroup",    "Has outgroup at the end of blocks", ],
        [ "parallel|p=i", "run in parallel mode", { default => 1 }, ],
        [ "msa=s",        "Aligning program",     { default => "mafft" }, ],
        [   "quick",
            "Quick mode, only aligning indel adjacent regions. Suitable for multiz outputs",
        ],
        [ "pad=i",  "In quick mode, enlarge indel regions", { default => 50 }, ],
        [ "fill=i", "In quick mode, join indel regions",    { default => 50 }, ],
        [ "chop=i", "Chop head and tail indels",            { default => 0 }, ],
        { show_defaults => 1, }
    );
}

sub usage_desc {
    return "fasops refine [options] <infile>";
}

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

* List of msa:
    * mafft
    * muscle
    * clustalw
    * none: means skip realigning
* <infile> are paths to blocked fasta files, .fas.gz is supported
* infile == stdin means reading from STDIN

MARKDOWN

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

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

    my $in_fh;
    if ( lc $args->[0] eq "stdin" ) {
        $in_fh = *STDIN{IO};
    }
    else {
        $in_fh = IO::Zlib->new( $args->[0], "rb" );
    }

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

    my @infos;    # collect blocks for parallelly refining
    my $content = '';    # content of one block
    while (1) {
        last if $in_fh->eof and $content eq '';
        my $line = '';
        if ( !$in_fh->eof ) {
            $line = $in_fh->getline;
        }
        if ( ( $line eq '' or $line =~ /^\s+$/ ) and $content ne '' ) {
            my $info_ary = App::Fasops::Common::parse_block_array( $content );
            $content = '';

            if ( $opt->{parallel} >= 2 ) {
                push @infos, $info_ary;
            }
            else {
                my $out_string = proc_block( $info_ary, $opt );
                print {$out_fh} $out_string;
            }
        }
        else {



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