App-Dazz

 view release on metacpan or  search on metacpan

lib/App/Dazz/Command/layout.pm  view on Meta::CPAN

package App::Dazz::Command::layout;
use strict;
use warnings;
use autodie;

use App::Dazz -command;
use App::Dazz::Common;

sub abstract {
    return 'layout anchors within a group';
}

sub opt_spec {
    return (
        [ "outfile|o=s", "output filename", ],
        [ 'border=i', 'length of borders in anchors', { default => 500 }, ],
        [ "max=i",    "max distance",                 { default => 5000 }, ],
        [ 'pa=s',     'prefix of anchors',            { default => "anchor" }, ],
        [ 'oa=s',     'overlaps between anchors', ],
        [ "png",      "write a png file via graphviz", ],
        { show_defaults => 1, },
    );
}

sub usage_desc {
    return "dazz layout [options] <strand.fasta> <.ovlp.tsv> <.relation.tsv>";
}

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

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

    if ( @{$args} != 3 ) {
        my $message = "This command need three input files.\n\tIt found";
        $message .= sprintf " [%s]", $_ for @{$args};
        $message .= ".\n";
        $self->usage_error($message);
    }
    for ( @{$args} ) {
        if ( !Path::Tiny::path($_)->is_file ) {
            $self->usage_error("The input file [$_] doesn't exist.");
        }
    }

    if ( $opt->{oa} ) {
        if ( !Path::Tiny::path( $opt->{oa} )->is_file ) {
            $self->usage_error("The overlap file [$opt->{oa}] doesn't exist.\n");
        }
    }

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

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

    #----------------------------#
    # loading sequences
    #----------------------------#
    my $seq_of = App::Fasops::Common::read_fasta( $args->[0] );
    my %is_anchor;
    for my $id ( keys %{$seq_of} ) {
        if ( index( $id, $opt->{pa} . "/" ) == 0 ) {
            $is_anchor{$id} = 1;
        }
    }

    #----------------------------#
    # load overlaps and build graph
    #----------------------------#
    my $graph = Graph->new( directed => 1 );
    my $links_of = {};    # long_id => { anchor_id => overlap_on_long, }
    {
        open my $in_fh, "<", $args->[1];

        my %seen_pair;
        while ( my $line = <$in_fh> ) {
            my $info = App::Dazz::Common::parse_ovlp_line($line);

            # ignore self overlapping
            next if $info->{f_id} eq $info->{g_id};

            # we've orient all sequences to the same strand
            next if $info->{g_strand} == 1;

            # skip duplicated overlaps
            my $pair = join( "-", sort ( $info->{f_id}, $info->{g_id} ) );
            next if $seen_pair{$pair};



( run in 0.914 second using v1.01-cache-2.11-cpan-98e64b0badf )