Bio-FastParsers

 view release on metacpan or  search on metacpan

t/blast_xml.t  view on Meta::CPAN

        1, -1,
        17538, 17582,
        17538, 17582,
        1, 17538, 17582,
        -1, 17582, 17538,
        22897,22897,
        0.2,0.2,
        95.7,95.7

    ];
    
    my @hsp_data = map {
        $_->query_strand, $_->hit_strand,
        $_->query_start, $_->query_end,
        $_->hit_start, $_->hit_end,
        $_->query_frame, $_->query_from, $_->query_to,
        $_->hit_frame, $_->hit_from, $_->hit_to,
        $_->query_len, $_->hit_len,
        $_->qcov, $_->subject_coverage,
        $_->pident, $_->percentage_positive
    } map { $_->get_hsp(27) } $bo->get_iteration(0)->get_hit(0);

    cmp_deeply \@hsp_data, $hsp_data_ref,
        'got expected coordinates for hsp 27';
}


sub check_file {
	my %args = @_;

	my (
	    $infile, $program, $db, $expect, $matrix,
	    $collect_mode, $iteration_count, $hit_count,
	    $scores_ref, $identities_ref, $hsp_data_ref
	) = @args{ qw(
	    infile program db expect matrix
	    collect_mode iteration_count hit_count
	    scores_ref identities_ref hsp_data_ref
	) };

    # open and parse BLAST report in XML format
    ok my $report = $class->new( file => $infile ), 'Blast::XML constructor';
    isa_ok $report, $class, $infile;

    # get main container
    my $bo = $report->blast_output;

    # examine report content
    cmp_deeply [ $bo->program, $bo->db ], [ $program, $db ],
        'got expected blast_output attributes';

    # get evalue threshold...
    cmp_ok $bo->parameters->expect, '==', $expect,
        'got expected evalue threshold';

    # ...or equivalently
    my $param = $bo->parameters;
    cmp_deeply [ $param->expect, $param->matrix ], [ $expect, $matrix ],
        'got expected blast_output attributes';

    # get the number of iterations (= queries)
    cmp_ok $bo->count_iterations, '==', $iteration_count,
        'got expected number of iterations';

    # get some 1st hsp attributes for all hits of first iteration
    my @hsp_data
        = $collect_mode ? map { [ $_->num, $_->score, $_->midline ] } map {
            $_->get_hsp(0)
        } $bo->get_iteration(0)->all_hits
        :                 map { [ $_->num, $_->score ] } map {
            $_->all_hsps
        } $bo->get_iteration(0)->get_hit(1)
    ;
    cmp_deeply \@hsp_data, $hsp_data_ref,
        'got expected attrs for 1st/all hsp(s) of 2nd/all hit(s) of 1st iter';

    # loop through iterations (or queries), hits and hsps
    # this is extremely fast because no data is moved around
    my @scores;
    my @queries;
    my @hits;
    for my $iter ($bo->all_iterations) {
        cmp_ok $iter->count_hits, '==', $hit_count, 'got expected hit count';
        for my $hit ($iter->all_hits) {
            for my $hsp ($hit->all_hsps) {
                push @scores, ($collect_mode ? $hsp->score : $hsp->positive);
                push @queries, [ $hsp->query_start, $hsp->query_end ];
                push @hits,    [ $hsp->hit_start,   $hsp->hit_end   ];
            }
        }
    }
    cmp_deeply \@scores, $scores_ref,
        'got expected scores/positives for all hits';
    ok((List::AllUtils::all { $_->[0] < $_->[1] } @queries),
        'got expected coordinates for queries');
    ok((List::AllUtils::all { $_->[0] < $_->[1] } @hits),
        'got expected coordinates for hits');

    # ...or nearly equivalently
    # here the container is altered by each iterator call
    my @identities;
    while (my $iter = $bo->next_iteration) {
        cmp_ok $iter->count_hits, '==', $hit_count, 'got expected hit count';
        while (my $hit = $iter->next_hit) {
            while (my $hsp = $hit->next_hsp) {
                push @identities,
                    ($collect_mode ? $hsp->identity : $hsp->hit_from);
            }
        }
        cmp_ok $iter->count_hits, '==', 0, 'rightly exhausted all hits';
    }
    cmp_deeply \@identities, $identities_ref,
        'got expected identities/hit_from for all hits';
    
    return;
}

done_testing;



( run in 0.497 second using v1.01-cache-2.11-cpan-96521ef73a4 )