App-Fasops

 view release on metacpan or  search on metacpan

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

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

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

sub abstract {
    return 'scan blocked fasta files and output covers on chromosomes';
}

sub opt_spec {
    return (
        [ "outfile|o=s", "Output filename. [stdout] for screen" ],
        [ "name|n=s",    "Only output this species" ],
        [ "length|l=i", "the threshold of alignment length", { default => 1 } ],
        [   "trim|t=i",
            "Trim align borders to avoid some overlaps in lastz results",
            { default => 0 }
        ],
        { show_defaults => 1, }
    );
}

sub usage_desc {
    return "fasops covers [options] <infile> [more infiles]";
}

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

* <infiles> are paths to axt files, .axt.gz is supported
* infile == stdin means reading from STDIN

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

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

    my %count_of;    # YAML::Sync can't Dump tied hashes
    for my $infile ( @{$args} ) {
        my $in_fh;
        if ( lc $infile eq "stdin" ) {
            $in_fh = *STDIN{IO};
        }
        else {
            $in_fh = IO::Zlib->new( $infile, "rb" );
        }

        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;
            }
            next if substr( $line, 0, 1 ) eq "#";

            if ( ( $line eq '' or $line =~ /^\s+$/ ) and $content ne '' ) {
                my $info_of = App::Fasops::Common::parse_block($content);
                $content = '';

                my @names = keys %{$info_of};
                if ( $opt->{name} ) {
                    if ( exists $info_of->{ $opt->{name} } ) {
                        @names = ( $opt->{name} );
                    }
                    else {
                        warn "$opt->{name} doesn't exist in this alignment\n";
                        next;
                    }
                }

                if ( $opt->{length} ) {
                    next
                        if length $info_of->{ $names[0] }{seq} < $opt->{length};



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