Bio-FastParsers

 view release on metacpan or  search on metacpan

lib/Bio/FastParsers/Blast/Table.pm  view on Meta::CPAN

package Bio::FastParsers::Blast::Table;
# ABSTRACT: Front-end class for tabular BLAST parser
$Bio::FastParsers::Blast::Table::VERSION = '0.221230';
use Moose;
use namespace::autoclean;

use autodie;

use List::AllUtils qw(mesh);

extends 'Bio::FastParsers::Base';

use Bio::FastParsers::Constants qw(:files);
use aliased 'Bio::FastParsers::Blast::Table::Hsp';

# TODO: recreate Table classes and internal classes through Templating
# TODO: check API consistency with Hmmer::Table and DomTable through synonyms
# TODO: document Hsp/Hit methods

# public attributes (inherited)


# private attributes

has '_line_iterator' => (
    traits   => ['Code'],
    is       => 'ro',
    isa      => 'CodeRef',
    init_arg => undef,
    lazy     => 1,
    builder  => '_build_line_iterator',
    handles  => {
        _next_line => 'execute',
    },
);

has '_last_' . $_ => (
    is       => 'ro',
    isa      => 'Str',
    init_arg => undef,
    default  => q{},
    writer   => '_set_last_' . $_,
) for qw(query hit);


## no critic (ProhibitUnusedPrivateSubroutines)

sub _build_line_iterator {
    my $self = shift;

    open my $fh, '<', $self->file;      # autodie
    return sub { <$fh> };               # return closure
}

## use critic

my @attrs = qw(
    query_id hit_id
    percent_identity hsp_length mismatches gaps
    query_from  query_to
      hit_from    hit_to
    evalue bit_score
    query_strand
      hit_strand
    query_start query_end
      hit_start   hit_end
);      # DID try to use MOP to get HSP attrs but order was not preserved


sub next_hsp {
    my $self = shift;

    # optional args for next_hit/next_query mode
    my $curr_query = shift;
    my $curr_hit   = shift;

    LINE:
    while (my $line = $self->_next_line) {

        # skip header/comments and empty lines
        chomp $line;
        next LINE if $line =~ $COMMENT_LINE
                  || $line =~ $EMPTY_LINE;

        # process HSP line
        my @fields = ( split(/\t/xms, $line), +1, +1 );

        # Fields for m8/m9 (now 6/7) format:
        #   0.  query id
        #   1.  subject id
        #   2.  % identity
        #   3.  alignment length
        #   4.  mismatches
        #   5.  gap opens
        #   6.  q. start  => query_from
        #   7.  q. end    => query_to
        #   8.  s. start  => hit_from
        #   9.  s. end    => hit_to
        #  10.  evalue
        #  11.  bit score
        # [12.] query_strand
        # [13.] hit_strand
        # [14.] query_start
        # [15.] query_end
        # [16.] hit_start
        # [17.] hit_end

        # optionally skip current line in next_hit/next_query mode
        if (defined $curr_query) {
            if (defined $curr_hit) {
                next LINE if $fields[0] eq $curr_query
                          && $fields[1] eq $curr_hit;
            }
            else {
                next LINE if $fields[0] eq $curr_query;
            }
        }

        # coerce numeric fields to numbers...



( run in 2.150 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )