Bio/SearchIO/Writer/ResultTableWriter.pm

filter: Statementstmt 69.0% 87 / 126 Branchbran 44.4% 16 / 36 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 12 / 16 Podpod 62.5% 5 / 8 totaltotal 62.8% 120 / 191 CC 24 SCAR 18.9
CC
24
Cov
62%
to_string
20.0
_set_labels
12.0
new
7.7
CRAP 6.6
« Bio/Annotation/Reference.pm ↑ Summary Bio/Matrix/PhylipDist.pm »
1  
2 =head1 NAME
3  
4 Bio::SearchIO::Writer::ResultTableWriter - Outputs tab-delimited data for each Bio::Search::Result::ResultI object.
5  
6 =head1 SYNOPSIS
7  
8 =head2 Example 1: Using the default columns
9  
10 use Bio::SearchIO;
11 use Bio::SearchIO::Writer::ResultTableWriter;
12  
13 my $in = Bio::SearchIO->new();
14  
15 my $writer = Bio::SearchIO::Writer::ResultTableWriter->new();
16  
17 my $out = Bio::SearchIO->new( -writer => $writer );
18  
19 while ( my $result = $in->next_result() ) {
20 $out->write_result($result, ($in->report_count - 1 ? 0 : 1) );
21 }
22  
23 =head2 Example 2: Specifying a subset of columns
24  
25 use Bio::SearchIO;
26 use Bio::SearchIO::Writer::ResultTableWriter;
27  
28 my $in = Bio::SearchIO->new();
29  
30 my $writer = Bio::SearchIO::Writer::ResultTableWriter->new(
31 -columns => [qw(
32 query_name
33 query_length
34 num_hits
35 )] );
36  
37 my $out = Bio::SearchIO->new( -writer => $writer,
38 -file => ">result.out" );
39  
40 while ( my $result = $in->next_result() ) {
41 $out->write_result($result, ($in->report_count - 1 ? 0 : 1) );
42 }
43  
44 =head2 Custom Labels
45  
46 You can also specify different column labels if you don't want to use
47 the defaults. Do this by specifying a C<-labels> hash reference
48 parameter when creating the ResultTableWriter object. The keys of the
49 hash should be the column number (left-most column = 1) for the label(s)
50 you want to specify. Here's an example:
51  
52 my $writer = Bio::SearchIO::Writer::ResultTableWriter->new(
53 -columns => [qw( query_name
54 query_length
55 query_description
56 num_hits)],
57 -labels => { 1 => 'QUERY_GI',
58 2 => 'QUERY_LENGTH' } );
59  
60  
61 =head1 DESCRIPTION
62  
63 Bio::SearchIO::Writer::ResultTableWriter outputs data in tab-delimited
64 format for each search result, one row per search result. This is a very
65 coarse-grain level of information since it only includes data
66 stored in the Bio::Search::Result::ResultI object itself and does not
67 include any information about hits or HSPs.
68  
69 You most likely will never use this object but instead will use one of
70 its subclasses: Bio::SearchIO::Writer::HitTableWriter or
71 Bio::SearchIO::Writer::HSPTableWriter.
72  
73 =head2 Available Columns
74  
75 Here are the columns that can be specified in the C<-columns>
76 parameter when creating a ResultTableWriter object. If a C<-columns> parameter
77 is not specified, this list, in this order, will be used as the default.
78  
79 query_name
80 query_length
81 query_description
82  
83 For more details about these columns, see the documentation for the
84 corresponding method in L<Bio::Search::Result::ResultI|Bio::Search::Result::ResultI>.
85  
86 =head1 FEEDBACK
87  
88 =head2 Mailing Lists
89  
90 User feedback is an integral part of the evolution of this and other
91 Bioperl modules. Send your comments and suggestions preferably to one
92 of the Bioperl mailing lists. Your participation is much appreciated.
93  
94 bioperl-l@bioperl.org - General discussion
95 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
96  
97 =head2 Support
98  
99 Please direct usage questions or support issues to the mailing list:
100  
101 I<bioperl-l@bioperl.org>
102  
103 rather than to the module maintainer directly. Many experienced and
104 reponsive experts will be able look at the problem and quickly
105 address it. Please include a thorough description of the problem
106 with code and data examples if at all possible.
107  
108 =head2 Reporting Bugs
109  
110 Report bugs to the Bioperl bug tracking system to help us keep track
111 the bugs and their resolution. Bug reports can be submitted via the
112 web:
113  
114 https://github.com/bioperl/bioperl-live/issues
115  
116 =head1 AUTHOR
117  
118 Steve Chervitz E<lt>sac@bioperl.orgE<gt>
119  
120 See L<the FEEDBACK section | FEEDBACK> for where to send bug reports
121 and comments.
122  
123 =head1 COPYRIGHT
124  
125 Copyright (c) 2001 Steve Chervitz. All Rights Reserved.
126  
127 This library is free software; you can redistribute it and/or modify
128 it under the same terms as Perl itself.
129  
130 =head1 DISCLAIMER
131  
132 This software is provided "as is" without warranty of any kind.
133  
134 =head1 SEE ALSO
135  
136 L<Bio::SearchIO::Writer::HitTableWriter>,
137 L<Bio::SearchIO::Writer::HSPTableWriter>
138  
139 =head1 METHODS
140  
141 =cut
142  
143  
144 package Bio::SearchIO::Writer::ResultTableWriter;
145 5 $Bio::SearchIO::Writer::ResultTableWriter::VERSION = '1.7.8';
146 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
147  
148 5 ▶use base qw(Bio::Root::Root Bio::SearchIO::SearchWriterI);
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 # Array fields: column, object, method[/argument], printf format, column label
151 # Methods are defined in Bio::Search::Result::ResultI.
152 # Tech note: If a bogus method is supplied, it will result in all values to be zero.
153 # Don't know why this is.
154 5 my %column_map = (
155 'query_name' => ['1', 'result', 'query_name', 's', 'QUERY' ],
156 'query_length' => ['2', 'result', 'query_length', 'd', 'LEN_Q'],
157 'query_description' => ['3', 'result', 'query_description', 's', 'DESC_Q'],
158 'num_hits' => ['4', 'result', 'num_hits', 'd', 'NUM_HITS'],
159 );
160  
161 0 ▶sub column_map { return %column_map }
Subroutine column_map: not called CC=1 SCAR=6.9
Pod undocumented
162  
163 2 ▶sub new {
Subroutine new: called CC=5 SCAR=20.5
Pod documented
164 2 my ($class, @args) = @_;
165 2 my $self = $class->SUPER::new(@args);
166  
167 2 my( $col_spec, $label_spec,
168 $filters ) = $self->_rearrange( [qw(COLUMNS
169 LABELS
170 FILTERS)], @args);
171
172 2 $self->_set_cols( $col_spec );
173 2 ▶ $self->_set_labels( $label_spec ) if $label_spec;
Branch: 1 branch
BranchTrueFalseTotal
if $label_spec 0 2 2
174 2 $self->_set_printf_fmt();
175 2 $self->_set_row_data_func();
176 2 $self->_set_column_labels();
177
178 2 ▶ if( defined $filters ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filters) 0 2 2
179 0 ▶ if( !ref($filters) =~ /HASH/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ref($filters) =~ /HASH/i) { } 0 0 0
180 0 $self->warn("Did not provide a hashref for the FILTERS option, ignoring.");
181 } else {
182 0 ▶ while( my ($type,$code) = each %{$filters} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$filters;}) 0 0 0
183 0 $self->filter($type,$code);
184 }
185 }
186 }
187  
188  
189 2 return $self;
190 }
191  
192  
193 # Purpose : Stores the column spec internally. Also performs QC on the
194 # user-supplied column specification.
195 #
196 2 ▶sub _set_cols {
Subroutine _set_cols: called CC=6 SCAR=20.3
197 2 my ($self, $col_spec_ref) = @_;
198 2 ▶ return if defined $self->{'_cols'}; # only set columns once
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_cols"} 0 2 2
199  
200 2 my %map = $self->column_map;
201  
202 2 ▶ if( not defined $col_spec_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $col_spec_ref) 0 2 2
203 0 print STDERR "\nUsing default column map.\n";
204 0 $col_spec_ref = [ map { $_ } sort { $map{$a}->[0] <=> $map{$b}->[0] } keys %map ];
205 }
206  
207 2 ▶ if( ref($col_spec_ref) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $col_spec_ref eq 'ARRAY') { } 2 0 2
208 # printf "%d columns to process\n", scalar(@$col_spec_ref);
209 2 my @col_spec = @{$col_spec_ref};
210 2 ▶ while( my $item = shift @col_spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @col_spec) 15 2 17
211 15 $item = lc($item);
212 15 ▶ if( not defined ($map{$item}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $map{$item}) 0 15 15
213 0 $self->throw(-class =>'Bio::Root::BadParameter',
214 -text => "Unknown column name: $item"
215 );
216 }
217 15 push @{$self->{'_cols'}}, $item;
218 #print "pushing on to col $col_num, $inner: $item\n";
219 }
220 }
221 else {
222 0 $self->throw(-class =>'Bio::Root::BadParameter',
223 -text => "Can't set columns: not a ARRAY ref",
224 -value => $col_spec_ref
225 );
226 }
227 }
228  
229 2 ▶sub _set_printf_fmt {
Subroutine _set_printf_fmt: called CC=2 SCAR=6.9
230 2 my ($self) = @_;
231  
232 2 my @cols = $self->columns();
233 2 my %map = $self->column_map;
234  
235 2 my $printf_fmt = '';
236  
237 2 foreach my $col ( @cols ) {
238 15 $printf_fmt .= "\%$map{$col}->[3]\t";
239 }
240  
241 2 $printf_fmt =~ s/\\t$//;
242  
243 2 $self->{'_printf_fmt'} = $printf_fmt;
244 }
245  
246 2 ▶sub printf_fmt { shift->{'_printf_fmt'} }
Subroutine printf_fmt: called CC=1 SCAR=0.0
Pod undocumented
247  
248 # Sets the data to be used for the labels.
249 0 ▶sub _set_labels {
Subroutine _set_labels: not called CC=3 SCAR=24.8
250 0 my ($self, $label_spec) = @_;
251 0 ▶ if( ref($label_spec) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $label_spec eq 'HASH') { } 0 0 0
252 0 foreach my $col ( sort { $a <=> $b } keys %$label_spec ) {
253 # print "LABEL: $col $label_spec->{$col}\n";
254 0 $self->{'_custom_labels'}->{$col} = $label_spec->{$col};
255 }
256 }
257 else {
258 0 $self->throw(-class =>'Bio::Root::BadParameter',
259 -text => "Can't set labels: not a HASH ref: $label_spec"
260 );
261 }
262 }
263  
264 2 ▶sub _set_column_labels {
Subroutine _set_column_labels: called CC=3 SCAR=11.0
265 2 my $self = shift;
266  
267 2 my @cols = $self->columns;
268 2 my %map = $self->column_map;
269 2 my $printf_fmt = '';
270 2 my (@data, $label, @underbars);
271  
272 2 my $i = 0;
273 2 foreach my $col( @cols ) {
274 15 $i++;
275 15 $printf_fmt .= "\%s\t";
276  
277 15 ▶ if(defined $self->{'_custom_labels'}->{$i}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_custom_labels'}{$i}) { } 0 15 15
278 0 $label = $self->{'_custom_labels'}->{$i};
279 }
280 else {
281 15 $label = $map{$col}->[4];
282 }
283 15 push @data, $label;
284 15 push @underbars, '-' x length($label);
285  
286 }
287 2 $printf_fmt =~ s/\\t$//;
288  
289 2 my $str = sprintf "$printf_fmt\n", @data;
290  
291 2 $str =~ s/\t\n/\n/;
292 2 $str .= sprintf "$printf_fmt\n", @underbars;
293  
294 2 $str =~ s/\t\n/\n/gs;
295 2 $self->{'_column_labels'} = $str;
296 }
297  
298 # Purpose : Generate a function that will call the appropriate
299 # methods on the result, hit, and hsp objects to retrieve the column data
300 # specified in the column spec.
301 #
302 # We should only have to go through the column spec once
303 # for a given ResultTableWriter. To permit this, we'll generate code
304 # for a method that returns an array of the data for a row of output
305 # given a result, hit, and hsp object as arguments.
306 #
307 2 ▶sub _set_row_data_func {
Subroutine _set_row_data_func: called CC=5 SCAR=16.7
308 2 my $self = shift;
309  
310 # Now we need to generate a string that can be eval'd to get the data.
311 2 my @cols = $self->columns();
312 2 my %map = $self->column_map;
313 2 my @data;
314 2 ▶ while( my $col = shift @cols ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $col = shift @cols) 15 2 17
315 15 my $object = $map{$col}->[1];
316 15 my $method = $map{$col}->[2];
317 15 my $arg = '';
318 15 ▶ if( $method =~ m!(\w+)/(\w+)! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ m[(\w+)/(\w+)]) 2 13 15
319 2 $method = $1;
320 2 $arg = "\"$2\"";
321 }
322 15 push @data, "\$$object->$method($arg)";
323 }
324 2 my $code = join( ",", @data);
325  
326 2 ▶ if( $self->verbose > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose > 0) 0 2 2
327 ## Begin Debugging
328 0 $self->debug( "Data to print:\n");
329 0 foreach( 0..$#data) { $self->debug( " [". ($_+ 1) . "] $data[$_]\n");}
330 0 $self->debug( "CODE:\n$code\n");
331 0 $self->debug("Printf format: ". $self->printf_fmt. "\n");
332 ## End Debugging
333 }
334  
335 41 ▶ my $func = sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
336 41 my ($result, $hit, $hsp) = @_;
337 41 my @r = eval $code;
338 # This should reduce the occurrence of those opaque "all zeros" bugs.
339 0 ▶ if( $@ ) { $self->throw("Trouble in ResultTableWriter::_set_row_data_func() eval: $@\n\n");
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 41 41
340 }
341 41 return @r;
342 2 };
343 2 $self->{'_row_data_func'} = $func;
344 }
345  
346 2 ▶sub row_data_func { shift->{'_row_data_func'} }
Subroutine row_data_func: called CC=1 SCAR=0.0
Pod undocumented
347  
348  
349 =head2 to_string()
350  
351 Note: this method is not intended for direct use. The
352 SearchIO::write_result() method calls it automatically if the writer
353 is hooked up to a SearchIO object as illustrated in L<the SYNOPSIS section | SYNOPSIS>.
354  
355 Title : to_string()
356 :
357 Usage : print $writer->to_string( $result_obj, [$include_labels] );
358 :
359 Argument : $result_obj = A Bio::Search::Result::ResultI object
360 : $include_labels = boolean, if true column labels are included (default: false)
361 :
362 Returns : String containing tab-delimited set of data for each hit
363 : in a ResultI object. Some data is summed across multiple HSPs.
364 :
365 Throws : n/a
366  
367 =cut
368  
369 #----------------
370 0 ▶sub to_string {
Subroutine to_string: not called CC=4 SCAR=30.0
Pod documented
371 #----------------
372 0 my ($self, $result, $include_labels) = @_;
373  
374 0 ▶ my $str = $include_labels ? $self->column_labels() : '';
Branch: 1 branch
BranchTrueFalseTotal
$include_labels ? : 0 0 0
375 0 my $resultfilter = $self->filter('RESULT');
376 0 ▶ if( ! defined $resultfilter ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $resultfilter or &{$resultfilter;}($result)) 0 0 0
Condition0%0/3
not defined $resultfilter or &{$resultfilter;}($result)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $resultfilter or &{$resultfilter;}($result)
not defined $resultfilter &{$resultfilter;}($result)
Truth table0%0/3
not(A) or B
A= not defined $resultfilterB= &{$resultfilter;}($result)
ABresult
1X1
011
000
377 0 &{$resultfilter}($result) ) {
378 0 my @row_data = &{$self->{'_row_data_func'}}( $result );
379 0 $str .= sprintf "$self->{'_printf_fmt'}\n", @row_data;
380 0 $str =~ s/\t\n/\n/gs;
381 }
382 0 return $str;
383 }
384  
385  
386  
387 6 ▶sub columns {
Subroutine columns: called CC=2 SCAR=7.8
Pod documented
388 6 my $self = shift;
389 6 my @cols;
390 6 ▶ if( ref $self->{'_cols'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'_cols'}) { } 6 0 6
391 6 @cols = @{$self->{'_cols'}};
392 }
393 else {
394 0 my %map = $self->column_map;
395 0 @cols = sort { $map{$a}->[0] <=> $map{$b}->[0] } keys %map;
396 }
397 6 return @cols;
398 }
399  
400  
401 =head2 column_labels
402  
403 Usage : print $result_obj->column_labels();
404 Purpose : Get column labels for to_string().
405 Returns : String containing column labels. Tab-delimited.
406 Argument : n/a
407 Throws : n/a
408  
409 =cut
410  
411 2 ▶sub column_labels { shift->{'_column_labels'} }
Subroutine column_labels: called CC=1 SCAR=0.0
Pod documented
412  
413 =head2 end_report
414  
415 Title : end_report
416 Usage : $self->end_report()
417 Function: The method to call when ending a report, this is
418 mostly for cleanup for formats which require you to
419 have something at the end of the document. Nothing for
420 a text message.
421 Returns : string
422 Args : none
423  
424 =cut
425  
426 0 ▶sub end_report {
Subroutine end_report: not called CC=1 SCAR=6.9
Pod documented
427 0 return '';
428 }
429  
430 =head2 filter
431  
432 Title : filter
433 Usage : $writer->filter('hsp', \&hsp_filter);
434 Function: Filter out either at HSP,Hit,or Result level
435 Returns : none
436 Args : string => data type,
437 CODE reference
438  
439  
440 =cut
441  
442  
443 # Is this really needed?
444 #=head2 signif_format
445 #
446 # Usage : $writer->signif_format( [FMT] );
447 # Purpose : Allows retrieval of the P/Expect exponent values only
448 # : or as a two-element list (mantissa, exponent).
449 # Usage : $writer->signif_format('exp');
450 # : $writer->signif_format('parts');
451 # Returns : String or '' if not set.
452 # Argument : String, FMT = 'exp' (return the exponent only)
453 # : = 'parts'(return exponent + mantissa in 2-elem list)
454 # : = undefined (return the raw value)
455 # Comments : P/Expect values are still stored internally as the full,
456 # : scientific notation value.
457 #
458 #=cut
459 #
460 ##-------------
461 #sub signif_format {
462 ##-------------
463 # my $self = shift;
464 # if(@_) { $self->{'_signif_format'} = shift; }
465 # return $self->{'_signif_format'};
466 #}
467  
468 5 1;
« Bio/Annotation/Reference.pm ↑ Summary Bio/Matrix/PhylipDist.pm »