File Coverage

blib/lib/Bio/FastParsers/Hmmer/Standard/Iteration.pm
Criterion Covered Total %
statement 18 22 81.8
branch n/a
condition n/a
subroutine 6 10 60.0
pod 0 4 0.0
total 24 36 66.6


line stmt bran cond sub pod time code
1             package Bio::FastParsers::Hmmer::Standard::Iteration;
2             # ABSTRACT: Front-end class for standard HMMER parser
3             # CONTRIBUTOR: Arnaud DI FRANCO <arnaud.difranco@gmail.com>
4             $Bio::FastParsers::Hmmer::Standard::Iteration::VERSION = '0.201110';
5 7     7   4885 use Moose;
  7         18  
  7         43  
6 7     7   49788 use namespace::autoclean;
  7         20  
  7         54  
7              
8 7     7   616 use autodie;
  7         17  
  7         47  
9              
10 7     7   39171 use List::AllUtils qw(indexes firstidx mesh);
  7         19  
  7         474  
11              
12 7     7   47 use Bio::FastParsers::Constants qw(:files);
  7         25  
  7         812  
13 7     7   50 use aliased 'Bio::FastParsers::Hmmer::Standard::Target';
  7         16  
  7         59  
14              
15              
16             # public attributes
17              
18             has 'query' => (
19             is => 'ro',
20             isa => 'Str',
21             required => 1,
22             );
23              
24             has 'query_length' => (
25             is => 'ro',
26             isa => 'Int',
27             required => 1,
28             );
29              
30             has 'targets' => (
31             traits => ['Array'],
32             is => 'ro',
33             isa => 'ArrayRef[Bio::FastParsers::Hmmer::Standard::Target]',
34             required => 1,
35             handles => {
36             next_target => 'shift',
37             get_target => 'get',
38             all_targets => 'elements',
39             count_targets => 'count',
40             },
41             );
42              
43             ## no critic (ProhibitUnusedPrivateSubroutines)
44              
45             around BUILDARGS => sub {
46             my ($orig, $class, $iter_block) = @_;
47              
48             my %outargs;
49             my @hits;
50              
51             # Parse Iteration to separate header information (q, qlen), header hit
52             # table (Hit) and main output information per target (Target).
53             # Hit and Target are the same entity but Hit is close to what is retrieved
54             # from a Hmmer::Table output. For now, we chose to conserve Target as
55             # primary name since it is how it is called in the file.
56              
57             my @lines = split /\n/xms, $iter_block;
58              
59             LINE:
60             for my $line (@lines) {
61              
62             # parse header
63             if ($line =~ m/Query:/xms) {
64             my @fields = split /\s+/xms, $line;
65             $outargs{'query'} = $fields[1];
66             ( $outargs{'query_length'} = $fields[2] ) =~ s/\D//xmsg;
67             }
68              
69             # parse Hit
70             my @attrs = qw(
71             evalue score bias
72             best_dom_evalue best_dom_score best_dom_bias
73             exp dom query_name target_description
74             );
75              
76             if ($line =~ m/^\s+ (\d .*)/xms) {
77             my @fields = split /\s+/xms, $1;
78              
79             # Fields
80             # 0. full seq - evalue
81             # 1. full seq - score
82             # 2. full seq - bias
83             # 3. best domain - evalue
84             # 4. best domain - score
85             # 5. best domain - bias
86             # 6. #domain - exp
87             # 7. #domain - N
88             # 8. sequence
89             # 9. description
90              
91             # coerce numeric fields to numbers
92             @fields[0..7] = map { 0 + $_ } @fields[0..7];
93              
94             # fixing description
95             $fields[9] = join q{ }, @fields[9..$#fields];
96             # set missing/empty field values to undef
97             @fields[8..9] = map { $_ || undef } @fields[8..9];
98              
99             my @wanted = @fields[0..9];
100             push @hits, { mesh @attrs, @wanted };
101             }
102              
103             # process only header
104             last LINE if $line =~ m/^\>\>/xms;
105             }
106              
107             # split Targets (Hits)
108             my @target_indexes = indexes { m/^\>\>/xms } @lines;
109             my @targets;
110             for (my $i = 0; $i < @target_indexes; $i++) {
111             my @block = defined $target_indexes[$i+1]
112             ? @lines[ $target_indexes[$i] .. $target_indexes[$i+1] ]
113             : splice @lines, $target_indexes[$i]
114             ;
115             push @targets, Target->new( { raw => \@block, hit => $hits[$i] } );
116             }
117             $outargs{targets} = \@targets;
118              
119             # return expected constructor hash
120             return $class->$orig(%outargs);
121             };
122              
123              
124             # aliases for Target/Hit
125              
126             sub next_hit {
127 0     0 0   return shift->next_target;
128             }
129              
130             sub get_hit { ## no critic (RequireArgUnpacking)
131 0     0 0   return shift->get_target(@_);
132             }
133              
134             sub all_hits {
135 0     0 0   return shift->all_targets;
136             }
137              
138             sub count_hits {
139 0     0 0   return shift->count_targets;
140             }
141              
142             __PACKAGE__->meta->make_immutable;
143             1;
144              
145             __END__
146              
147             =pod
148              
149             =head1 NAME
150              
151             Bio::FastParsers::Hmmer::Standard::Iteration - Front-end class for standard HMMER parser
152              
153             =head1 VERSION
154              
155             version 0.201110
156              
157             =head1 SYNOPSIS
158              
159             use aliased 'Bio::FastParsers::Hmmer::Standard';
160              
161             # open and parse hmmsearch output
162             my $infile = 'test/hmmer.out';
163             my $parser = Standard->new(file => $infile);
164              
165             =head1 DESCRIPTION
166              
167             # TODO
168              
169             =head1 AUTHOR
170              
171             Denis BAURAIN <denis.baurain@uliege.be>
172              
173             =head1 CONTRIBUTOR
174              
175             =for stopwords Arnaud DI FRANCO
176              
177             Arnaud DI FRANCO <arnaud.difranco@gmail.com>
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             This software is copyright (c) 2013 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN.
182              
183             This is free software; you can redistribute it and/or modify it under
184             the same terms as the Perl 5 programming language system itself.
185              
186             =cut