Bio/Annotation/StructuredValue.pm

filter: Statementstmt 76.3% 55 / 72 Branchbran 65.7% 25 / 38 Conditioncond 55.5% 10 / 18 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 61.5% 8 / 13 Podpod 100% 9 / 9 totaltotal 69.5% 112 / 161 CC 27 SCAR 20.2
CC
27
Cov
70%
add_value
14.4
display_text
12.0
value
8.0
CRAP 7.5
« Bio/Root/Exception.pm ↑ Summary Bio/AlignIO/psi.pm »
1 #
2 # BioPerl module for Bio::Annotation::StructuredValue
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Hilmar Lapp <hlapp at gmx.net>
7 #
8 # (c) Hilmar Lapp, hlapp at gmx.net, 2002.
9 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
10 #
11 # You may distribute this module under the same terms as perl itself.
12 # Refer to the Perl Artistic License (see the license accompanying this
13 # software package, or see http://www.perl.com/language/misc/Artistic.html)
14 # for the terms under which you may use, modify, and redistribute this module.
15 #
16 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
17 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
18 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 #
20 # POD documentation - main docs before the code
21  
22 =head1 NAME
23  
24 Bio::Annotation::StructuredValue - A scalar with embedded structured
25 information
26  
27 =head1 SYNOPSIS
28  
29 use Bio::Annotation::StructuredValue;
30 use Bio::Annotation::Collection;
31  
32 my $col = Bio::Annotation::Collection->new();
33 my $sv = Bio::Annotation::StructuredValue->new(-value => 'someval');
34 $col->add_Annotation('tagname', $sv);
35  
36 =head1 DESCRIPTION
37  
38 Scalar value annotation object.
39  
40 =head1 FEEDBACK
41  
42 =head2 Mailing Lists
43  
44 User feedback is an integral part of the evolution of this and other
45 Bioperl modules. Send your comments and suggestions preferably to one
46 of the Bioperl mailing lists. Your participation is much appreciated.
47  
48 bioperl-l@bioperl.org - General discussion
49 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
50  
51 =head2 Support
52  
53 Please direct usage questions or support issues to the mailing list:
54  
55 I<bioperl-l@bioperl.org>
56  
57 rather than to the module maintainer directly. Many experienced and
58 reponsive experts will be able look at the problem and quickly
59 address it. Please include a thorough description of the problem
60 with code and data examples if at all possible.
61  
62 =head2 Reporting Bugs
63  
64 Report bugs to the Bioperl bug tracking system to help us keep track
65 the bugs and their resolution. Bug reports can be submitted via
66 or the web:
67  
68 https://github.com/bioperl/bioperl-live/issues
69  
70 =head1 AUTHOR - Hilmar Lapp
71  
72 Email hlapp-at-gmx.net
73  
74 =head1 APPENDIX
75  
76 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
77  
78 =cut
79  
80  
81 # Let the code begin...
82  
83  
84 package Bio::Annotation::StructuredValue;
85 2 $Bio::Annotation::StructuredValue::VERSION = '1.7.8';
86 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 # Object preamble - inherits from Bio::Root::Root
89  
90 2 ▶use base qw(Bio::Annotation::SimpleValue);
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 =head2 new
93  
94 Title : new
95 Usage : my $sv = Bio::Annotation::StructuredValue->new();
96 Function: Instantiate a new StructuredValue object
97 Returns : Bio::Annotation::StructuredValue object
98 Args : -value => $value to initialize the object data field [optional]
99 -tagname => $tag to initialize the tagname [optional]
100  
101 =cut
102  
103 1 ▶sub new{
Subroutine new: called CC=3 SCAR=11.2
Pod documented
104 1 my ($class,@args) = @_;
105  
106 1 my $self = $class->SUPER::new(@args);
107  
108 1 my ($value,$tag) = $self->_rearrange([qw(VALUE TAGNAME)], @args);
109 1 $self->{'values'} = [];
110 1 ▶ defined $value && $self->value($value);
Branch: 1 branch
BranchTrueFalseTotal
defined $value and $self->value($value) 0 1 1
111 1 ▶ defined $tag && $self->tagname($tag);
Branch: 1 branch
BranchTrueFalseTotal
defined $tag and $self->tagname($tag) 0 1 1
112  
113 1 return $self;
114 }
115  
116  
117 =head1 AnnotationI implementing functions
118  
119 =cut
120  
121 =head2 as_text
122  
123 Title : as_text
124 Usage : my $text = $obj->as_text
125 Function: return the string "Value: $v" where $v is the value
126 Returns : string
127 Args : none
128  
129  
130 =cut
131  
132 0 ▶sub as_text{
Subroutine as_text: not called CC=1 SCAR=6.9
Pod documented
133 0 my ($self) = @_;
134  
135 0 return "Value: ".$self->value;
136 }
137  
138 =head2 display_text
139  
140 Title : display_text
141 Usage : my $str = $ann->display_text();
142 Function: returns a string. Unlike as_text(), this method returns a string
143 formatted as would be expected for te specific implementation.
144  
145 One can pass a callback as an argument which allows custom text
146 generation; the callback is passed the current instance and any text
147 returned
148 Example :
149 Returns : a string
150 Args : [optional] callback
151  
152 =cut
153  
154 {
155 0 ▶ my $DEFAULT_CB = sub { $_[0]->value || ''};
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->value or "" 0 0 0
156  
157 0 ▶ sub display_text {
Subroutine display_text: not called CC=3 SCAR=24.8
Pod documented
158 0 my ($self, $cb) = @_;
159 0 ▶ $cb ||= $DEFAULT_CB;
Condition0%0/3
$cb ||= $DEFAULT_CB
l!l&&r!l&&!r
000
MC/DC0%0/2
$cb ||= $DEFAULT_CB
$cb $DEFAULT_CB
Truth table0%0/3
A ||= B
A= $cbB= $DEFAULT_CB
ABresult
1X1
011
000
160 0 ▶ $self->throw("Callback must be a code reference") if ref $cb ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $cb ne "CODE" 0 0 0
161 0 return $cb->($self);
162 }
163  
164 }
165  
166 =head2 hash_tree
167  
168 Title : hash_tree
169 Usage : my $hashtree = $value->hash_tree
170 Function: For supporting the AnnotationI interface just returns the value
171 as a hashref with the key 'value' pointing to the value
172 Returns : hashrf
173 Args : none
174  
175  
176 =cut
177  
178 2 ▶sub hash_tree{
Subroutine hash_tree: not called CC=1 SCAR=3.5
Pod documented
179 0 my ($self) = @_;
180  
181 0 my $h = {};
182 0 $h->{'value'} = $self->value;
183 }
184  
185 =head2 tagname
186  
187 Title : tagname
188 Usage : $obj->tagname($newval)
189 Function: Get/set the tagname for this annotation value.
190  
191 Setting this is optional. If set, it obviates the need to provide
192 a tag to AnnotationCollection when adding this object.
193 Example :
194 Returns : value of tagname (a scalar)
195 Args : new value (a scalar, optional)
196  
197  
198 =cut
199  
200 9 ▶sub tagname{
Subroutine tagname: called CC=2 SCAR=6.9
Pod documented
201 9 my ($self,$value) = @_;
202 9 ▶ if( defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 8 9
203 1 $self->{'tagname'} = $value;
204 }
205 9 return $self->{'tagname'};
206 }
207  
208  
209 =head1 Specific accessors for StructuredValue
210  
211 =cut
212  
213 =head2 value
214  
215 Title : value
216 Usage : $obj->value($newval)
217 Function: Get/set the value for this annotation.
218  
219 Set mode is here only to retain compatibility with
220 SimpleValue. It is equivalent to calling
221 add_value([0], $newval).
222  
223 In get mode, this implementation allows one to pass additional
224 parameters that control how the structured annotation
225 components will be joined together to form a
226 string. Recognized are presently
227 -joins a reference to an array of join strings, the
228 elements at index i applying to joining
229 annotations at dimension i. The last element
230 will be re-used for dimensions higher than i.
231 Defaults to ['; '].
232 -brackets a reference to an array of two strings
233 denoting the opening and closing brackets for
234 the elements of one dimension, if there is
235 more than one element in the dimension.
236 Defaults to ['(',')'].
237  
238 Returns : value of value
239 Args : newvalue (optional)
240  
241  
242 =cut
243  
244 8 ▶sub value{
Subroutine value: called CC=8 SCAR=20.8
Pod documented
245 8 my ($self,$value,@args) = @_;
246  
247 # set mode?
248 8 ▶ return $self->add_value([0], $value) if defined($value) && (@args == 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and @args == 0 1 7 8
Condition100%3/3
defined $value and @args == 0
!ll&&!rl&&r
251
MC/DC100%2/2
defined $value and @args == 0
defined $value @args == 0
Truth table100%3/3
A and B
A= defined $valueB= @args == 0
ABresult
0X0
100
111
249 # no, get mode
250 # determine joins and brackets
251 7 unshift(@args, $value);
252 7 my ($joins, $brackets) =
253 $self->_rearrange([qw(JOINS BRACKETS)], @args);
254 7 ▶ $joins = ['; '] unless $joins;
Branch: 1 branch
BranchTrueFalseTotal
unless $joins 2 5 7
255 7 ▶ $brackets = ['(', ')'] unless $brackets;
Branch: 1 branch
BranchTrueFalseTotal
unless $brackets 6 1 7
256 7 my $txt = &_to_text($self->{'values'}, $joins, $brackets);
257 # if there's only brackets at the start and end, remove them
258 7 ▶ if((@{$self->{'values'}} == 1) &&
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"values"};} == 1 and length $brackets->[0] == 1 and length $brackets->[1] == 1) 2 5 7
Condition66%4/6
@{$self->{"values"};} == 1 and length $brackets->[0] == 1 and length $brackets->[1] == 1
!ll&&!rl&&r
502
@{$self->{"values"};} == 1 and length $brackets->[0] == 1
!ll&&!rl&&r
502
MC/DC33%1/3
@{$self->{"values"};} == 1 and length $brackets->[0] == 1 and length $brackets->[1] == 1
@{$self->{"values"};} == 1 length $brackets->[0] == 1 length $brackets->[1] == 1
Truth table50%2/4
A and B and C
A= @{$self->{"values"};} == 1B= length $brackets->[0] == 1C= length $brackets->[1] == 1
ABCresult
0XX0
10X0
1100
1111
259 (length($brackets->[0]) == 1) && (length($brackets->[1]) == 1)) {
260 2 my $re = '\\'.$brackets->[0].
261 '([^\\'.$brackets->[1].']*)\\'.$brackets->[1];
262 2 $txt =~ s/^$re$/$1/;
263 }
264 7 return $txt;
265 }
266  
267 16 ▶sub _to_text{
Subroutine _to_text: called CC=7 SCAR=19.5
268 16 my ($arr, $joins, $brackets, $rec_n) = @_;
269  
270 16 ▶ $rec_n = 0 unless defined($rec_n);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec_n 7 9 16
271 16 ▶ my $i = $rec_n >= @$joins ? @$joins-1 : $rec_n;
Branch: 1 branch
BranchTrueFalseTotal
$rec_n >= @$joins ? : 6 10 16
272 my $txt = join($joins->[$i],
273 map {
274 16 ▶ ref($_) ?
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 9 27 36
ref $_ eq 'ARRAY' ? : 9 0 9
275 (ref($_) eq "ARRAY" ?
276 &_to_text($_, $joins, $brackets, $rec_n+1) :
277 $_->value()) :
278 $_;
279 } @$arr);
280 16 ▶ if($rec_n && (@$arr > 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec_n and @$arr > 1) 6 10 16
Condition100%3/3
$rec_n and @$arr > 1
!ll&&!rl&&r
736
MC/DC100%2/2
$rec_n and @$arr > 1
$rec_n @$arr > 1
Truth table100%3/3
A and B
A= $rec_nB= @$arr > 1
ABresult
0X0
100
111
281 6 $txt = $brackets->[0] . $txt . $brackets->[1];
282 }
283 16 return $txt;
284 }
285  
286 =head2 get_values
287  
288 Title : get_values
289 Usage :
290 Function: Get the top-level array of values. Each of the elements will
291 recursively be a reference to an array or a scalar, depending
292 on the depth of this structured value annotation.
293 Example :
294 Returns : an array
295 Args : none
296  
297  
298 =cut
299  
300 0 ▶sub get_values{
Subroutine get_values: not called CC=1 SCAR=6.9
Pod documented
301 0 my $self = shift;
302  
303 0 return @{$self->{'values'}};
304 }
305  
306 =head2 get_all_values
307  
308 Title : get_all_values
309 Usage :
310 Function: Flattens all values in this structured annotation and
311 returns them as an array.
312 Example :
313 Returns : the (flat) array of values
314 Args : none
315  
316  
317 =cut
318  
319 1 ▶sub get_all_values{
Subroutine get_all_values: called CC=1 SCAR=0.0
Pod documented
320 1 my ($self) = @_;
321 # we code lazy here and just take advantage of value()
322 1 my $txt = $self->value(-joins => ['@!@'], -brackets => ['','']);
323 1 return split(/\@!\@/, $txt);
324 }
325  
326 =head2 add_value
327  
328 Title : add_value
329 Usage :
330 Function: Adds the given value to the structured annotation at the
331 given index.
332  
333 The index is multi-dimensional, with the first dimension
334 applying to the first level, and so forth. If a particular
335 dimension or a particular index does not exist yet, it will
336 be created. If it does exist and adding the value would
337 mean replacing a scalar with an array reference, we throw
338 an exception to prevent unintended damage. An index of -1
339 at any dimension means append.
340  
341 If an array of values is to be added, it will create an
342 additional dimension at the index specified, unless the
343 last index value is -1, in which case they will all be
344 appended to the last dimension.
345  
346 Example :
347 Returns : none
348 Args : the index at which to add (a reference to an array)
349 the value(s) to add
350  
351  
352 =cut
353  
354 7 ▶sub add_value{
Subroutine add_value: called CC=8 SCAR=26.7
Pod documented
355 7 my ($self,$index,@values) = @_;
356  
357 7 my $tree = $self->{'values'};
358 7 my $lastidx = pop(@$index);
359 7 foreach my $i (@$index) {
360 3 ▶ if($i < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < 0) { } 3 0 3
elsif (not $tree->[$i] or ref $tree->[$i] eq 'ARRAY') { } 0 0 0
361 3 my $subtree = [];
362 3 push(@$tree, $subtree);
363 3 $tree = $subtree;
364 ▶ } elsif((! $tree->[$i]) || (ref($tree->[$i]) eq "ARRAY")) {
Condition0%0/3
not $tree->[$i] or ref $tree->[$i] eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $tree->[$i] or ref $tree->[$i] eq "ARRAY"
not $tree->[$i] ref $tree->[$i] eq "ARRAY"
Truth table0%0/3
not(A) or B
A= not $tree->[$i]B= ref $tree->[$i] eq "ARRAY"
ABresult
1X1
011
000
365 0 ▶ $tree->[$i] = [] unless ref($tree->[$i]) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree->[$i] eq "ARRAY" 0 0 0
366 0 $tree = $tree->[$i];
367 } else {
368 0 $self->throw("element $i is a scalar but not in last dimension");
369 }
370 }
371 7 ▶ if($lastidx < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lastidx < 0) { } 5 2 7
elsif (@values < 2) { } 2 0 2
372 5 push(@$tree, @values);
373 } elsif(@values < 2) {
374 2 $tree->[$lastidx] = shift(@values);
375 } else {
376 0 $tree->[$lastidx] = [@values];
377 }
378  
379 }
380  
381 2 1;
« Bio/Root/Exception.pm ↑ Summary Bio/AlignIO/psi.pm »