Bio/SeqIO/table.pm

filter: Statementstmt 97.0% 196 / 202 Branchbran 87.7% 100 / 114 Conditioncond 62.5% 35 / 56 MC/DCmcdc 41.1% 14 / 34 Subroutinesub 95.6% 22 / 23 Podpod 100% 10 / 10 totaltotal 85.8% 377 / 439 CC 86 SCAR 28.3
CC
86
Cov
89%
_initialize
35.3
_parse_header
19.2
next_seq
15.0
CRAP 16.9
« Bio/Search/HSP/GenericHSP.pm ↑ Summary Bio/Matrix/IO/scoring.pm »
1 #
2 # BioPerl module for Bio::SeqIO::table
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Hilmar Lapp
7 #
8 # You may distribute this module under the same terms as perl itself.
9 # Refer to the Perl Artistic License (see the license accompanying this
10 # software package, or see http://www.perl.com/language/misc/Artistic.html)
11 # for the terms under which you may use, modify, and redistribute this module.
12 #
13 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
14 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
15 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 #
17  
18 # POD documentation - main docs before the code
19  
20 =head1 NAME
21  
22 Bio::SeqIO::table - sequence input/output stream from a delimited table
23  
24 =head1 SYNOPSIS
25  
26 # Do not to use this object directly, use Bio::SeqIO, for example:
27  
28 $in = Bio::SeqIO->new(-file => $filename, -format => 'table');
29  
30 while ( my $seq = $in->next_seq() ) {
31 # do something with $seq
32 }
33  
34 =head1 DESCRIPTION
35  
36 This class transforms records in a table-formatted text file into
37 Bio::Seq objects.
38  
39 A table-formatted text file of sequence records for the purposes of
40 this module is defined as a text file with each row corresponding to a
41 sequence, and the attributes of the sequence being in different
42 columns. Columns are delimited by a common delimiter, for instance tab
43 or comma.
44  
45 The module permits specifying which columns hold which type of
46 annotation. The semantics of certain attributes, if present, are
47 pre-defined, e.g., accession number and sequence. Additional
48 attributes may be added to the annotation bundle.
49  
50 =head1 FEEDBACK
51  
52 =head2 Mailing Lists
53  
54 User feedback is an integral part of the evolution of this and other
55 Bioperl modules. Send your comments and suggestions preferably to one
56 of the Bioperl mailing lists. Your participation is much appreciated.
57  
58 bioperl-l@bioperl.org - General discussion
59 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
60  
61 =head2 Support
62  
63 Please direct usage questions or support issues to the mailing list:
64  
65 I<bioperl-l@bioperl.org>
66  
67 rather than to the module maintainer directly. Many experienced and
68 reponsive experts will be able look at the problem and quickly
69 address it. Please include a thorough description of the problem
70 with code and data examples if at all possible.
71  
72 =head2 Reporting Bugs
73  
74 Report bugs to the Bioperl bug tracking system to help us keep track
75 the bugs and their resolution.
76  
77 Bug reports can be submitted via email or the web:
78  
79 https://github.com/bioperl/bioperl-live/issues
80  
81 =head1 AUTHOR - Hilmar Lapp
82  
83 Email hlapp at gmx.net
84  
85 =head1 APPENDIX
86  
87 The rest of the documentation details each of the object
88 methods. Internal methods are usually preceded with a _
89  
90 =cut
91  
92 # Let the code begin...
93  
94 package Bio::SeqIO::table;
95 2 $Bio::SeqIO::table::VERSION = '1.7.8';
96 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 2 ▶use Bio::Species;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 2 ▶use Bio::Seq::SeqFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 2 ▶use Bio::Annotation::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 2 ▶use Bio::Annotation::SimpleValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 2 ▶use base qw(Bio::SeqIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 =head2 new
106  
107 Title : new
108 Usage : $stream = Bio::SeqIO->new(-file => $filename, -format => 'table')
109 Function: Returns a new seqstream
110 Returns : A Bio::SeqIO stream for a table format
111 Args : Named parameters:
112  
113 -file Name of file to read
114 -fh Filehandle to attach to
115 -comment Leading character(s) introducing a comment line
116 -header the number of header lines to skip; the first
117 non-comment header line will be used to obtain
118 column names; column names will be used as the
119 default tags for attaching annotation.
120 -delim The delimiter for columns as a regular expression;
121 consecutive occurrences of the delimiter will
122 not be collapsed.
123 -display_id The one-based index of the column containing
124 the display ID of the sequence
125 -accession_number The one-based index of the column
126 containing the accession number of the sequence
127 -seq The one-based index of the column containing
128 the sequence string of the sequence
129 -desc The one-based index of the column containing
130 the description of the sequence
131 -species The one-based index of the column containing the
132 species for the sequence record; if not a
133 number, will be used as the static species
134 common to all records
135 -annotation If provided and a scalar (but see below), a
136 flag whether or not all additional columns are
137 to be preserved as annotation, the tags used
138 will either be 'colX' if there is no column
139 header and where X is the one-based column
140 index, and otherwise the column headers will be
141 used as tags;
142  
143 If a reference to an array, or a square
144 bracket-enclosed string of comma-delimited
145 values, only those columns (one-based index)
146 will be preserved as annotation, tags as before;
147  
148 If a reference to a hash, or a curly
149 braces-enclosed string of comma-delimited key
150 and value pairs in alternating order, the keys
151 are one-based column indexes to be preserved,
152 and the values are the tags under which the
153 annotation is to be attached; if not provided or
154 supplied as undef, no additional annotation will
155 be preserved.
156 -colnames A reference to an array of column labels, or a
157 string of comma-delimited labels, denoting the
158 columns to be converted into annotation; this is
159 an alternative to -annotation and will be
160 ignored if -annotation is also supplied with a
161 valid value.
162 -trim Flag determining whether or not all values should
163 be trimmed of leading and trailing white space
164 and double quotes
165  
166 Additional arguments may be used to e.g. set factories and
167 builders involved in the sequence object creation (see the
168 POD of Bio::SeqIO).
169  
170 =cut
171  
172 5 ▶sub _initialize {
Subroutine _initialize: called CC=29 SCAR=35.6
173 5 my($self,@args) = @_;
174  
175 # chained initialization
176 5 $self->SUPER::_initialize(@args);
177  
178 # our own parameters
179 5 my ($cmtchars,
180 $header,
181 $delim,
182 $display_id,
183 $desc,
184 $accnr,
185 $seq,
186 $taxon,
187 $useann,
188 $colnames,
189 $trim) =
190 $self->_rearrange([qw(COMMENT
191 HEADER
192 DELIM
193 DISPLAY_ID
194 DESC
195 ACCESSION_NUMBER
196 SEQ
197 SPECIES
198 ANNOTATION
199 COLNAMES
200 TRIM)
201 ], @args);
202  
203 # store options and apply defaults
204 5 ▶ $self->comment_char(defined($cmtchars) ? $cmtchars : "#")
Branch: 2 branches
BranchTrueFalseTotal
if not defined $self->comment_char or defined $cmtchars 5 0 5
defined $cmtchars ? : 0 5 5
Condition33%1/3
not defined $self->comment_char or defined $cmtchars
l!l&&r!l&&!r
500
MC/DC0%0/2
not defined $self->comment_char or defined $cmtchars
not defined $self->comment_char defined $cmtchars
Truth table33%1/3
not(A) or B
A= not defined $self->comment_charB= defined $cmtchars
ABresult
1X1
011
000
205 if (!defined($self->comment_char)) || defined($cmtchars);
206 5 ▶ $self->delimiter(defined($delim) ? $delim : "\t")
Branch: 2 branches
BranchTrueFalseTotal
if not defined $self->delimiter or defined $delim 5 0 5
defined $delim ? : 3 2 5
Condition33%1/3
not defined $self->delimiter or defined $delim
l!l&&r!l&&!r
500
MC/DC0%0/2
not defined $self->delimiter or defined $delim
not defined $self->delimiter defined $delim
Truth table33%1/3
not(A) or B
A= not defined $self->delimiterB= defined $delim
ABresult
1X1
011
000
207 if (!defined($self->delimiter)) || defined($delim);
208 5 ▶ $self->header($header) if defined($header);
Branch: 1 branch
BranchTrueFalseTotal
if defined $header 4 1 5
209 5 ▶ $self->trim_values($trim) if defined($trim);
Branch: 1 branch
BranchTrueFalseTotal
if defined $trim 3 2 5
210  
211 # attribute columns
212 5 my $attrs = {};
213 5 ▶ $attrs->{-display_id} = $display_id if defined($display_id);
Branch: 1 branch
BranchTrueFalseTotal
if defined $display_id 4 1 5
214 5 ▶ $attrs->{-accession_number} = $accnr if defined($accnr);
Branch: 1 branch
BranchTrueFalseTotal
if defined $accnr 4 1 5
215 5 ▶ $attrs->{-seq} = $seq if defined($seq);
Branch: 1 branch
BranchTrueFalseTotal
if defined $seq 4 1 5
216 5 ▶ $attrs->{-desc} = $desc if defined($desc);
Branch: 1 branch
BranchTrueFalseTotal
if defined $desc 1 4 5
217 5 ▶ if (defined($taxon)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $taxon) 3 2 5
218 3 ▶ if (ref($taxon) || ($taxon =~ /^\d+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $taxon or $taxon =~ /^\d+$/) { } 0 3 3
Condition33%1/3
ref $taxon or $taxon =~ /^\d+$/
l!l&&r!l&&!r
003
MC/DC0%0/2
ref $taxon or $taxon =~ /^\d+$/
ref $taxon $taxon =~ /^\d+$/
Truth table33%1/3
A or B
A= ref $taxonB= $taxon =~ /^\d+$/
ABresult
1X1
011
000
219 # either a static object, or a column reference
220 0 $attrs->{-species} = $taxon;
221 } else {
222 # static species as a string
223 3 $attrs->{-species} = Bio::Species->new(
224 -classification => [reverse(split(' ',$taxon))]);
225 }
226 }
227 5 $self->attribute_map($attrs);
228  
229 # annotation columns, if any
230 5 ▶ if ($useann && !ref($useann)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($useann and not ref $useann) 2 3 5
Condition66%2/3
$useann and not ref $useann
!ll&&!rl&&r
302
MC/DC50%1/2
$useann and not ref $useann
$useann not ref $useann
Truth table66%2/3
A and not(B)
A= $useannB= not ref $useann
ABresult
0X0
100
111
231 # it's a scalar; check whether this is in fact an array or
232 # hash as a string rather than just a flag
233 2 ▶ if ($useann =~ /^\[(.*)\]$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($useann =~ /^\[(.*)\]$/) { } 1 1 2
elsif ($useann =~ /^{(.*)}$/) { } 0 1 1
234 1 $useann = [split(/[,;]/,$1)];
235 } elsif ($useann =~ /^{(.*)}$/) {
236 0 $useann = {split(/[,;]/,$1)};
237 } # else it is probably indeed just a flag
238 }
239 5 ▶ if (ref($useann)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $useann) { } 1 4 5
240 1 my $ann_map;
241 1 ▶ if (ref($useann) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $useann eq 'ARRAY') { } 1 0 1
242 1 my $has_header = ($self->header > 0);
243 1 $ann_map = {};
244 1 foreach my $i (@$useann) {
245 5 ▶ $ann_map->{$i} = $has_header ? undef : "col$i";
Branch: 1 branch
BranchTrueFalseTotal
$has_header ? : 5 0 5
246 }
247 } else {
248 # no special handling necessary
249 0 $ann_map = $useann;
250 }
251 1 $self->annotation_map($ann_map);
252 } else {
253 4 ▶ $self->keep_annotation($useann || $colnames);
Condition100%3/3
$useann || $colnames
l!l&&r!l&&!r
112
MC/DC100%2/2
$useann || $colnames
$useann $colnames
Truth table100%3/3
A || B
A= $useannB= $colnames
ABresult
1X1
011
000
254 # annotation columns, if any
255 4 ▶ if ($colnames && !ref($colnames)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colnames and not ref $colnames) 1 3 4
Condition66%2/3
$colnames and not ref $colnames
!ll&&!rl&&r
301
MC/DC50%1/2
$colnames and not ref $colnames
$colnames not ref $colnames
Truth table66%2/3
A and not(B)
A= $colnamesB= not ref $colnames
ABresult
0X0
100
111
256 # an array as a string
257 1 $colnames =~ s/^\[(.*)\]$/$1/;
258 1 $colnames = [split(/[,;]/,$colnames)];
259 }
260 4 ▶ $self->annotation_columns($colnames) if ref($colnames);
Branch: 1 branch
BranchTrueFalseTotal
if ref $colnames 1 3 4
261 }
262  
263 # make sure we have a factory defined
264 5 ▶ if(!defined($self->sequence_factory)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->sequence_factory) 5 0 5
265 5 $self->sequence_factory(
266 Bio::Seq::SeqFactory->new(-verbose => $self->verbose(),
267 -type => 'Bio::Seq::RichSeq'));
268 }
269 }
270  
271 =head2 next_seq
272  
273 Title : next_seq
274 Usage : $seq = $stream->next_seq()
275 Function: returns the next sequence in the stream
276 Returns : Bio::Seq::RichSeq object
277 Args :
278  
279 =cut
280  
281 35 ▶sub next_seq {
Subroutine next_seq: called CC=15 SCAR=27.1
Pod documented
282 35 my $self = shift;
283  
284 # skip until not a comment and not an empty line
285 35 my $line_ok = $self->_next_record();
286  
287 # if there is a header but we haven't read past it yet then do so now
288 35 ▶ if ($line_ok && (! $self->_header_skipped) && $self->header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_ok and not $self->_header_skipped and $self->header) 4 31 35
Condition83%5/6
$line_ok and not $self->_header_skipped and $self->header
!ll&&!rl&&r
3104
$line_ok and not $self->_header_skipped
!ll&&!rl&&r
3284
MC/DC66%2/3
$line_ok and not $self->_header_skipped and $self->header
$line_ok not $self->_header_skipped $self->header
Truth table75%3/4
A and not(B) and C
A= $line_okB= not $self->_header_skippedC= $self->header
ABCresult
0XX0
10X0
1100
1111
289 4 $line_ok = $self->_parse_header();
290 4 $self->_header_skipped(1);
291 }
292  
293 # return if we reached end-of-file
294 35 ▶ return unless $line_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $line_ok 3 32 35
295  
296 # otherwise, parse the record
297  
298 # split into columns
299 32 my @cols = $self->_get_row_values();
300 # trim leading and trailing whitespace and quotes if desired
301 32 ▶ if ($self->trim_values) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trim_values) 30 2 32
302 30 ▶ for(my $i = 0; $i < scalar(@cols); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @cols) 300 30 330
303 300 ▶ if ($cols[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols[$i]) 285 15 300
304 # trim off whitespace
305 285 $cols[$i] =~ s/^\s+//;
306 285 $cols[$i] =~ s/\s+$//;
307 # trim off double quotes
308 285 $cols[$i] =~ s/^"//;
309 285 $cols[$i] =~ s/"$//;
310 }
311 }
312 }
313  
314 # assign values for columns in the attribute map
315 32 my $attrmap = $self->_attribute_map;
316 32 my %params = ();
317 32 foreach my $attr (keys %$attrmap) {
318 128 ▶ if ((!ref($attrmap->{$attr})) && ($attrmap->{$attr} =~ /^\d+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $attrmap->{$attr} and $attrmap->{$attr} =~ /^\d+$/) { } 98 30 128
Condition66%2/3
not ref $attrmap->{$attr} and $attrmap->{$attr} =~ /^\d+$/
!ll&&!rl&&r
30098
MC/DC50%1/2
not ref $attrmap->{$attr} and $attrmap->{$attr} =~ /^\d+$/
not ref $attrmap->{$attr} $attrmap->{$attr} =~ /^\d+$/
Truth table66%2/3
not(A) and B
A= not ref $attrmap->{$attr}B= $attrmap->{$attr} =~ /^\d+$/
ABresult
0X0
100
111
319 # this is a column index, add to instantiation parameters
320 98 $params{$attr} = $cols[$attrmap->{$attr}];
321 } else {
322 # not a column index; we assume it's a static value
323 30 $params{$attr} = $attrmap->{$attr};
324 }
325 }
326  
327 # add annotation columns to the annotation bundle
328 32 my $annmap = $self->_annotation_map;
329 32 ▶ if ($annmap && %$annmap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($annmap and %$annmap) 30 2 32
Condition66%2/3
$annmap and %$annmap
!ll&&!rl&&r
2030
MC/DC50%1/2
$annmap and %$annmap
$annmap %$annmap
Truth table66%2/3
A and B
A= $annmapB= %$annmap
ABresult
0X0
100
111
330 30 my $anncoll = Bio::Annotation::Collection->new();
331 30 foreach my $col (keys %$annmap) {
332 170 ▶ next unless $cols[$col]; # skip empty columns!
Branch: 1 branch
BranchTrueFalseTotal
unless $cols[$col] 19 151 170
333 $anncoll->add_Annotation(
334 Bio::Annotation::SimpleValue->new(-value => $cols[$col],
335 151 -tagname=> $annmap->{$col}));
336 }
337 30 $params{'-annotation'} = $anncoll;
338 }
339  
340 # ask the object builder to add the slots that we've gathered
341 32 my $builder = $self->sequence_builder();
342 32 $builder->add_slot_value(%params);
343 # and instantiate the object
344 32 my $seq = $builder->make_object();
345  
346 # done!
347 32 return $seq;
348 }
349  
350 =head2 comment_char
351  
352 Title : comment_char
353 Usage : $obj->comment_char($newval)
354 Function: Get/set the leading character(s) designating a line as
355 a comment-line.
356 Example :
357 Returns : value of comment_char (a scalar)
358 Args : on set, new value (a scalar or undef, optional)
359  
360  
361 =cut
362  
363 49 ▶sub comment_char{
Subroutine comment_char: called CC=2 SCAR=6.9
Pod documented
364 49 my $self = shift;
365  
366 49 ▶ return $self->{'comment_char'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 44 49
367 44 return $self->{'comment_char'};
368 }
369  
370 =head2 header
371  
372 Title : header
373 Usage : $obj->header($newval)
374 Function: Get/set the number of header lines to skip before the
375 rows containing actual sequence records.
376  
377 If set to zero or undef, means that there is no header and
378 therefore also no column headers.
379  
380 Example :
381 Returns : value of header (a scalar)
382 Args : on set, new value (a scalar or undef, optional)
383  
384  
385 =cut
386  
387 13 ▶sub header{
Subroutine header: called CC=2 SCAR=6.9
Pod documented
388 13 my $self = shift;
389  
390 13 ▶ return $self->{'header'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 9 13
391 9 return $self->{'header'};
392 }
393  
394 =head2 delimiter
395  
396 Title : delimiter
397 Usage : $obj->delimiter($newval)
398 Function: Get/set the column delimiter. This will in fact be
399 treated as a regular expression. Consecutive occurrences
400 will not be collapsed to a single one.
401  
402 Example :
403 Returns : value of delimiter (a scalar)
404 Args : on set, new value (a scalar or undef, optional)
405  
406  
407 =cut
408  
409 45 ▶sub delimiter{
Subroutine delimiter: called CC=2 SCAR=6.9
Pod documented
410 45 my $self = shift;
411  
412 45 ▶ return $self->{'delimiter'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 40 45
413 40 return $self->{'delimiter'};
414 }
415  
416 =head2 attribute_map
417  
418 Title : attribute_map
419 Usage : $obj->attribute_map($newval)
420 Function: Get/set the map of sequence object initialization
421 attributes (keys) to one-based column index.
422  
423 Attributes will usually need to be prefixed by a dash, just
424 as if they were passed to the new() method of the sequence
425 class.
426  
427 Example :
428 Returns : value of attribute_map (a reference to a hash)
429 Args : on set, new value (a reference to a hash or undef, optional)
430  
431  
432 =cut
433  
434 6 ▶sub attribute_map{
Subroutine attribute_map: called CC=10 SCAR=23.2
Pod documented
435 6 my $self = shift;
436  
437 # internally we store zero-based maps - so we need to convert back
438 # and forth here
439 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 1 6
440 5 my $arg = shift;
441 # allow for and protect against undef
442 5 ▶ return delete $self->{'_attribute_map'} unless defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 5 5
443 # copy to avoid side-effects
444 5 my $attr_map = {%$arg};
445 5 foreach my $key (keys %$attr_map) {
446 16 ▶ if ((!ref($attr_map->{$key})) && ($attr_map->{$key} =~ /^\d+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $attr_map->{$key} and $attr_map->{$key} =~ /^\d+$/) 13 3 16
Condition66%2/3
not ref $attr_map->{$key} and $attr_map->{$key} =~ /^\d+$/
!ll&&!rl&&r
3013
MC/DC50%1/2
not ref $attr_map->{$key} and $attr_map->{$key} =~ /^\d+$/
not ref $attr_map->{$key} $attr_map->{$key} =~ /^\d+$/
Truth table66%2/3
not(A) and B
A= not ref $attr_map->{$key}B= $attr_map->{$key} =~ /^\d+$/
ABresult
0X0
100
111
447 13 $attr_map->{$key}--;
448 }
449 }
450 5 $self->{'_attribute_map'} = $attr_map;
451 }
452 # there may not be a map
453 6 ▶ return unless exists($self->{'_attribute_map'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_attribute_map"} 0 6 6
454 # we need to copy in order not to override the stored map!
455 6 my %attr_map = %{$self->{'_attribute_map'}};
456 6 foreach my $key (keys %attr_map) {
457 20 ▶ if ((!ref($attr_map{$key})) && ($attr_map{$key} =~ /^\d+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $attr_map{$key} and $attr_map{$key} =~ /^\d+$/) 16 4 20
Condition66%2/3
not ref $attr_map{$key} and $attr_map{$key} =~ /^\d+$/
!ll&&!rl&&r
4016
MC/DC50%1/2
not ref $attr_map{$key} and $attr_map{$key} =~ /^\d+$/
not ref $attr_map{$key} $attr_map{$key} =~ /^\d+$/
Truth table66%2/3
not(A) and B
A= not ref $attr_map{$key}B= $attr_map{$key} =~ /^\d+$/
ABresult
0X0
100
111
458 16 $attr_map{$key}++;
459 }
460 }
461 6 return \%attr_map;
462 }
463  
464 =head2 annotation_map
465  
466 Title : annotation_map
467 Usage : $obj->annotation_map($newval)
468 Function: Get/set the mapping between one-based column indexes
469 (keys) and annotation tags (values).
470  
471 Note that the map returned by this method may change after
472 the first next_seq() call if the file contains a column
473 header and no annotation keys have been predefined in the
474 map, because upon reading the column header line the tag
475 names will be set automatically.
476  
477 Note also that the map may reference columns that are used
478 as well in the sequence attribute map.
479  
480 Example :
481 Returns : value of annotation_map (a reference to a hash)
482 Args : on set, new value (a reference to a hash or undef, optional)
483  
484  
485 =cut
486  
487 7 ▶sub annotation_map{
Subroutine annotation_map: called CC=6 SCAR=17.9
Pod documented
488 7 my $self = shift;
489  
490 # internally we store zero-based maps - so we need to convert back
491 # and forth here
492 7 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 3 7
493 4 my $arg = shift;
494 # allow for and protect against undef
495 4 ▶ return delete $self->{'_annotation_map'} unless defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 4 4
496 # copy to avoid side-effects
497 4 my $ann_map = {%$arg};
498 # make sure we sort the keys numerically or otherwise we may
499 # clobber a key with a higher index
500 4 foreach my $key (sort { $a <=> $b } keys(%$ann_map)) {
501 22 $ann_map->{$key-1} = $ann_map->{$key};
502 22 delete $ann_map->{$key};
503 }
504 4 $self->{'_annotation_map'} = $ann_map;
505 # also make a note that we want to keep annotation
506 4 $self->keep_annotation(1);
507 }
508 # there may not be a map
509 7 ▶ return unless exists($self->{'_annotation_map'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_annotation_map"} 2 5 7
510 # we need to copy in order not to override the stored map!
511 5 my %ann_map = %{$self->{'_annotation_map'}};
512 # here we need to sort numerically in reverse order ...
513 5 foreach my $key (sort { $b <=> $a } keys(%ann_map)) {
514 27 $ann_map{$key+1} = $ann_map{$key};
515 27 delete $ann_map{$key};
516 }
517 5 return \%ann_map;
518 }
519  
520 =head2 keep_annotation
521  
522 Title : keep_annotation
523 Usage : $obj->keep_annotation($newval)
524 Function: Get/set flag whether or not to keep values from
525 additional columns as annotation.
526  
527 Additional columns are all those columns in the input file
528 that aren't referenced in the attribute map.
529  
530 Example :
531 Returns : value of keep_annotation (a scalar)
532 Args : on set, new value (a scalar or undef, optional)
533  
534  
535 =cut
536  
537 12 ▶sub keep_annotation{
Subroutine keep_annotation: called CC=2 SCAR=6.9
Pod documented
538 12 my $self = shift;
539  
540 12 ▶ return $self->{'keep_annotation'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 8 4 12
541 4 return $self->{'keep_annotation'};
542 }
543  
544 =head2 annotation_columns
545  
546 Title : annotation_columns
547 Usage : $obj->annotation_columns($newval)
548 Function: Get/set the names (labels) of the columns to be used for
549 annotation.
550  
551 This is an alternative to using annotation_map. In order to
552 have any effect, it must be set before the first call of
553 next_seq(), and obviously there must be a header line (or
554 row) too giving the column labels.
555  
556 Example :
557 Returns : value of annotation_columns (a reference to an array)
558 Args : on set, new value (a reference to an array of undef, optional)
559  
560  
561 =cut
562  
563 3 ▶sub annotation_columns{
Subroutine annotation_columns: called CC=2 SCAR=6.9
Pod documented
564 3 my $self = shift;
565  
566 3 ▶ return $self->{'annotation_columns'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 2 3
567 2 return $self->{'annotation_columns'};
568 }
569  
570 =head2 trim_values
571  
572 Title : trim_values
573 Usage : $obj->trim_values($newval)
574 Function: Get/set whether or not to trim leading and trailing
575 whitespace off all column values.
576 Example :
577 Returns : value of trim_values (a scalar)
578 Args : on set, new value (a scalar or undef, optional)
579  
580  
581 =cut
582  
583 38 ▶sub trim_values{
Subroutine trim_values: called CC=2 SCAR=6.9
Pod documented
584 38 my $self = shift;
585  
586 38 ▶ return $self->{'trim_values'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 35 38
587 35 return $self->{'trim_values'};
588 }
589  
590 =head2 write_seq
591  
592 Title: write_seq
593 Usage: write_seq() is not implemented for table format output.
594  
595 =cut
596  
597 0 ▶sub write_seq {
Subroutine write_seq: not called CC=1 SCAR=6.9
Pod documented
598 0 shift->throw("write_seq() not implemented for 'table' format");
599 }
600  
601 =head1 Internal methods
602  
603 All methods with a leading underscore are not meant to be part of the
604 'official' API. They are for use by this module only, consider them
605 private unless you are a developer trying to modify this module.
606  
607 =cut
608  
609 =head2 _attribute_map
610  
611 Title : _attribute_map
612 Usage : $obj->_attribute_map($newval)
613 Function: Get only. Same as attribute_map, but zero-based indexes.
614  
615 Note that any changes made to the returned map will change
616 the map used by this instance. You should know what you are
617 doing if you modify the returned value (or if you call this
618 method in the first place).
619  
620 Example :
621 Returns : value of _attribute_map (a reference to a hash)
622 Args : none
623  
624  
625 =cut
626  
627 32 ▶sub _attribute_map{
Subroutine _attribute_map: called CC=1 SCAR=0.0
628 32 my $self = shift;
629  
630 32 return $self->{'_attribute_map'};
631 }
632  
633 =head2 _annotation_map
634  
635 Title : _annotation_map
636 Usage : $obj->_annotation_map($newval)
637 Function: Get only. Same as annotation_map, but with zero-based indexes.
638  
639 Note that any changes made to the returned map will change
640 the map used by this instance. You should know what you are
641 doing if you modify the returned value (or if you call this
642 method in the first place).
643  
644 Example :
645 Returns : value of _annotation_map (a reference to a hash)
646 Args : none
647  
648  
649 =cut
650  
651 32 ▶sub _annotation_map{
Subroutine _annotation_map: called CC=1 SCAR=0.0
652 32 my $self = shift;
653  
654 32 return $self->{'_annotation_map'};
655 }
656  
657 =head2 _header_skipped
658  
659 Title : _header_skipped
660 Usage : $obj->_header_skipped($newval)
661 Function: Get/set the flag whether the header was already
662 read (and skipped) or not.
663 Example :
664 Returns : value of _header_skipped (a scalar)
665 Args : on set, new value (a scalar or undef, optional)
666  
667  
668 =cut
669  
670 36 ▶sub _header_skipped{
Subroutine _header_skipped: called CC=2 SCAR=6.9
671 36 my $self = shift;
672  
673 36 ▶ return $self->{'_header_skipped'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 32 36
674 32 return $self->{'_header_skipped'};
675 }
676  
677 =head2 _next_record
678  
679 Title : _next_record
680 Usage :
681 Function: Navigates the underlying file to the next record.
682  
683 For row-based records in delimited text files, this will
684 skip all empty lines and lines with a leading comment
685 character.
686  
687 This method is here is to serve as a hook for other formats
688 that conceptually also represent tables but aren't
689 formatted as row-based text files.
690  
691 Example :
692 Returns : TRUE if the navigation was successful and FALSE
693 otherwise. Unsuccessful navigation will usually be treated
694 as an end-of-file condition.
695 Args :
696  
697  
698 =cut
699  
700 39 ▶sub _next_record{
Subroutine _next_record: called CC=5 SCAR=19.1
701 39 my $self = shift;
702  
703 39 my $cmtcc = $self->comment_char;
704 39 my $line = $self->_readline();
705  
706 # skip until not a comment and not an empty line
707 39 ▶ while (defined($line)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and $cmtcc && $line =~ /^\s*$cmtcc/ || $line =~ /^\s*$/) 0 39 39
Condition44%4/9
defined $line and $cmtcc && $line =~ /^\s*$cmtcc/ || $line =~ /^\s*$/
!ll&&!rl&&r
3360
$cmtcc and $line =~ /^\s*$cmtcc/ or $line =~ /^\s*$/
l!l&&r!l&&!r
0036
$cmtcc and $line =~ /^\s*$cmtcc/
!ll&&!rl&&r
0360
MC/DC0%0/4
defined $line and $cmtcc && $line =~ /^\s*$cmtcc/ || $line =~ /^\s*$/
defined $line $cmtcc $line =~ /^\s*$cmtcc/ $line =~ /^\s*$/
Truth table33%2/6
A and B and C or D
A= defined $lineB= $cmtccC= $line =~ /^\s*$cmtcc/D= $line =~ /^\s*$/
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
708 && (($cmtcc && ($line =~ /^\s*$cmtcc/))
709 || ($line =~ /^\s*$/))) {
710 0 $line = $self->_readline();
711 }
712  
713 39 return $self->{'_line'} = $line;
714 }
715  
716 =head2 _parse_header
717  
718 Title : _parse_header
719 Usage :
720 Function: Parse the table header and navigate past it.
721  
722 This method is called if the number of header rows has been
723 specified equal to or greater than one, and positioned at
724 the first header line (row). By default the first header
725 line (row) is used for setting column names, but additional
726 lines (rows) may be skipped too. Empty lines and comment
727 lines do not count as header lines (rows).
728  
729 This method will call _next_record() to navigate to the
730 next header line (row), if there is more than one header
731 line (row). Upon return, the file is presumed to be
732 positioned at the first record after the header.
733  
734 This method is here is to serve as a hook for other formats
735 that conceptually also represent tables but aren't
736 formatted as row-based text files.
737  
738 Note however that the only methods used to access file
739 content or navigate the position are _get_row_values() and
740 _next_record(), so it should usually suffice to override
741 those.
742  
743 Example :
744 Returns : TRUE if navigation past the header was successful and FALSE
745 otherwise. Unsuccessful navigation will usually be treated
746 as an end-of-file condition.
747 Args :
748  
749  
750 =cut
751  
752 4 ▶sub _parse_header{
Subroutine _parse_header: called CC=19 SCAR=29.5
753 4 my $self = shift;
754  
755 # the first header line contains the column headers, see whether
756 # we need them
757 4 ▶ if ($self->keep_annotation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keep_annotation) 3 1 4
758 3 my @colnames = $self->_get_row_values();
759 # trim leading and trailing whitespace if desired
760 3 ▶ if ($self->trim_values) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trim_values) 3 0 3
761 # trim off whitespace
762 3 @colnames = map { $_ =~ s/^\s+//; $_ =~ s/\s+$//; $_; } @colnames;
763 # trim off double quotes
764 3 @colnames = map { $_ =~ s/^"//; $_ =~ s/"$//; $_; } @colnames;
765 }
766 # build or complete annotation column map
767 3 ▶ my $annmap = $self->annotation_map || {};
Condition100%2/2
$self->annotation_map || {}
l!l
12
MC/DC100%1/1
$self->annotation_map || {}
$self->annotation_map
Truth table100%2/2
A
A= $self->annotation_map
Aresult
11
00
768 3 ▶ if (! %$annmap) {
Branch: 1 branch
BranchTrueFalseTotal
if (not %$annmap) { } 2 1 3
769 # check whether columns have been defined by name rather than index
770 2 ▶ if (my $anncols = $self->annotation_columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $anncols = $self->annotation_columns) { } 1 1 2
771 # first sanity check: all column names must map
772 1 my %colmap = map { ($_,1); } @colnames;
773 1 foreach my $col (@$anncols) {
774 5 ▶ if (!exists($colmap{$col})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $colmap{$col}) 0 5 5
775 0 $self->throw("no such column labeled '$col'");
776 }
777 }
778 # now map to the column indexes
779 1 %colmap = map { ($_,1); } @$anncols;
780 1 ▶ for (my $i = 0; $i < scalar(@colnames); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @colnames) 10 1 11
781 10 ▶ if (exists($colmap{$colnames[$i]})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $colmap{$colnames[$i]}) 5 5 10
782 5 $annmap->{$i+1} = $colnames[$i];
783 }
784 }
785 } else {
786 # no columns specified, default to all non-attribute columns
787 1 ▶ for (my $i = 0; $i < scalar(@colnames); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @colnames) 10 1 11
788 10 $annmap->{$i+1} = $colnames[$i];
789 }
790 # subtract all attribute-referenced columns
791 1 foreach my $attrcol (values %{$self->attribute_map}) {
792 4 ▶ if ((!ref($attrcol)) && ($attrcol =~ /^\d+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $attrcol and $attrcol =~ /^\d+$/) 3 1 4
Condition66%2/3
not ref $attrcol and $attrcol =~ /^\d+$/
!ll&&!rl&&r
103
MC/DC50%1/2
not ref $attrcol and $attrcol =~ /^\d+$/
not ref $attrcol $attrcol =~ /^\d+$/
Truth table66%2/3
not(A) and B
A= not ref $attrcolB= $attrcol =~ /^\d+$/
ABresult
0X0
100
111
793 3 delete $annmap->{$attrcol};
794 }
795 }
796 }
797 } else {
798 # fill in where the tag names weren't pre-defined
799 1 ▶ for (my $i = 0; $i < scalar(@colnames); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @colnames) 10 1 11
800 10 ▶ if (exists($annmap->{$i+1}) && ! defined($annmap->{$i+1})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$annmap{$i + 1} and not defined $$annmap{$i + 1}) 5 5 10
Condition66%2/3
exists $$annmap{$i + 1} and not defined $$annmap{$i + 1}
!ll&&!rl&&r
505
MC/DC50%1/2
exists $$annmap{$i + 1} and not defined $$annmap{$i + 1}
exists $$annmap{$i + 1} not defined $$annmap{$i + 1}
Truth table66%2/3
A and not(B)
A= exists $$annmap{$i + 1}B= not defined $$annmap{$i + 1}
ABresult
0X0
100
111
801 5 $annmap->{$i+1} = $colnames[$i];
802 }
803 }
804 }
805 3 $self->annotation_map($annmap);
806 }
807  
808 # now read past the header
809 4 my $header_lines = $self->header;
810 4 my $line_ok = 1;
811 4 ▶ while (defined($line_ok) && ($header_lines > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line_ok and $header_lines > 0) 4 4 8
Condition66%2/3
defined $line_ok and $header_lines > 0
!ll&&!rl&&r
044
MC/DC50%1/2
defined $line_ok and $header_lines > 0
defined $line_ok $header_lines > 0
Truth table66%2/3
A and B
A= defined $line_okB= $header_lines > 0
ABresult
0X0
100
111
812 4 $line_ok = $self->_next_record();
813 4 $header_lines--;
814 }
815  
816 4 return $line_ok;
817 }
818  
819 =head2 _get_row_values
820  
821 Title : _get_row_values
822 Usage :
823 Function: Get the values for the current line (or row) as an array in
824 the order of columns.
825  
826 This method is here is to serve as a hook for other formats
827 that conceptually also represent tables but aren't
828 formatted as row-based text files.
829  
830 Example :
831 Returns : An array of column values for the current row.
832 Args :
833  
834  
835 =cut
836  
837 35 ▶sub _get_row_values{
Subroutine _get_row_values: called CC=1 SCAR=0.0
838 35 my $self = shift;
839 35 my $delim = $self->delimiter;
840 35 my $line = $self->{'_line'};
841 35 chomp($line);
842 35 my @cols = split(/$delim/,$line);
843 35 return @cols;
844 }
845  
846 2 1;
« Bio/Search/HSP/GenericHSP.pm ↑ Summary Bio/Matrix/IO/scoring.pm »