File Coverage

blib/lib/Bio/Phylo/Matrices/Matrix.pm
Criterion Covered Total %
statement 70 80 87.5
branch 10 22 45.4
condition 3 8 37.5
subroutine 24 24 100.0
pod 12 12 100.0
total 119 146 81.5


line stmt bran cond sub pod time code
1             package Bio::Phylo::Matrices::Matrix;
2 13     13   128052 use strict;
  13         55  
  13         357  
3 13     13   57 use base 'Bio::Phylo::Matrices::MatrixRole';
  13         27  
  13         6248  
4 13     13   96 use Bio::Phylo::Util::CONSTANT qw':objecttypes /looks_like/';
  13         26  
  13         3312  
5 13     13   86 use Bio::Phylo::Util::Exceptions qw'throw';
  13         31  
  13         2285  
6             {
7              
8             my $logger = __PACKAGE__->get_logger;
9             my @inside_out_arrays = \(
10             my (
11             %type, %charlabels, %statelabels,
12             %gapmode, %matchchar, %polymorphism,
13             %case_sensitivity, %characters,
14             )
15             );
16              
17             =head1 NAME
18              
19             Bio::Phylo::Matrices::Matrix - Character state matrix
20              
21             =head1 SYNOPSIS
22              
23             use Bio::Phylo::Factory;
24             my $fac = Bio::Phylo::Factory->new;
25              
26             # instantiate taxa object
27             my $taxa = $fac->create_taxa;
28             for ( 'Homo sapiens', 'Pan paniscus', 'Pan troglodytes' ) {
29             $taxa->insert( $fac->create_taxon( '-name' => $_ ) );
30             }
31              
32             # instantiate matrix object, 'standard' data type. All categorical
33             # data types follow semantics like this, though with different
34             # symbols in lookup table and matrix
35             my $standard_matrix = $fac->create_matrix(
36             '-type' => 'STANDARD',
37             '-taxa' => $taxa,
38             '-lookup' => {
39             '-' => [],
40             '0' => [ '0' ],
41             '1' => [ '1' ],
42             '?' => [ '0', '1' ],
43             },
44             '-charlabels' => [ 'Opposable big toes', 'Opposable thumbs', 'Not a pygmy' ],
45             '-matrix' => [
46             [ 'Homo sapiens' => '0', '1', '1' ],
47             [ 'Pan paniscus' => '1', '1', '0' ],
48             [ 'Pan troglodytes' => '1', '1', '1' ],
49             ],
50             );
51            
52             # note: complicated constructor for mixed data!
53             my $mixed_matrix = Bio::Phylo::Matrices::Matrix->new(
54            
55             # if you want to create 'mixed', value for '-type' is array ref...
56             '-type' => [
57            
58             # ...with first field 'mixed'...
59             'mixed',
60            
61             # ...second field is an array ref...
62             [
63            
64             # ...with _ordered_ key/value pairs...
65             'dna' => 10, # value is length of type range
66             'standard' => 10, # value is length of type range
67            
68             # ... or, more complicated, value is a hash ref...
69             'rna' => {
70             '-length' => 10, # value is length of type range
71            
72             # ...value for '-args' is an array ref with args
73             # as can be passed to 'unmixed' datatype constructors,
74             # for example, here we modify the lookup table for
75             # rna to allow both 'U' (default) and 'T'
76             '-args' => [
77             '-lookup' => {
78             'A' => [ 'A' ],
79             'C' => [ 'C' ],
80             'G' => [ 'G' ],
81             'U' => [ 'U' ],
82             'T' => [ 'T' ],
83             'M' => [ 'A', 'C' ],
84             'R' => [ 'A', 'G' ],
85             'S' => [ 'C', 'G' ],
86             'W' => [ 'A', 'U', 'T' ],
87             'Y' => [ 'C', 'U', 'T' ],
88             'K' => [ 'G', 'U', 'T' ],
89             'V' => [ 'A', 'C', 'G' ],
90             'H' => [ 'A', 'C', 'U', 'T' ],
91             'D' => [ 'A', 'G', 'U', 'T' ],
92             'B' => [ 'C', 'G', 'U', 'T' ],
93             'X' => [ 'G', 'A', 'U', 'T', 'C' ],
94             'N' => [ 'G', 'A', 'U', 'T', 'C' ],
95             },
96             ],
97             },
98             ],
99             ],
100             );
101            
102             # prints 'mixed(Dna:1-10, Standard:11-20, Rna:21-30)'
103             print $mixed_matrix->get_type;
104              
105             =head1 DESCRIPTION
106              
107             This module defines a container object that holds
108             L<Bio::Phylo::Matrices::Datum> objects. The matrix
109             object inherits from L<Bio::Phylo::MatrixRole>, so the
110             methods defined there apply here.
111              
112             =head1 METHODS
113              
114             =head2 MUTATORS
115              
116             =over
117              
118             =item set_statelabels()
119              
120             Sets argument state labels.
121              
122             Type : Mutator
123             Title : set_statelabels
124             Usage : $matrix->set_statelabels( [ [ 'state1', 'state2' ] ] );
125             Function: Assigns state labels.
126             Returns : $self
127             Args : ARRAY, or nothing (to reset);
128             The array is two-dimensional,
129             the first index is to indicate
130             the column the labels apply to,
131             the second dimension the states
132             (sorted numerically or alphabetically,
133             depending on what's appropriate)
134              
135             =cut
136              
137             sub set_statelabels : Clonable {
138 7     7 1 19 my ( $self, $statelabels ) = @_;
139              
140             # it's an array ref, but what about its contents?
141 7 50 0     29 if ( looks_like_instance( $statelabels, 'ARRAY' ) ) {
    0          
142 7         16 for my $col ( @{$statelabels} ) {
  7         19  
143 2 50       3 if ( not looks_like_instance( $col, 'ARRAY' ) ) {
144 0         0 throw 'BadArgs' =>
145             "statelabels must be a two dimensional array ref";
146             }
147             }
148             }
149              
150             # it's defined but not an array ref
151             elsif ( defined $statelabels
152             && !looks_like_instance( $statelabels, 'ARRAY' ) )
153             {
154 0         0 throw 'BadArgs' =>
155             "statelabels must be a two dimensional array ref";
156             }
157              
158             # it's either a valid array ref, or nothing, i.e. a reset
159 7   50     30 $statelabels{ $self->get_id } = $statelabels || [];
160 7         16 return $self;
161 13     13   91 }
  13         27  
  13         70  
162              
163             =item set_characters()
164              
165             Sets the character set manager object Bio::Phylo::Matrices::Characters.
166             Normally you never have to use this.
167              
168             Type : Mutator
169             Title : set_characters
170             Usage : $matrix->set_characters( $characters );
171             Function: Assigns Bio::Phylo::Matrices::Characters object
172             Returns : $self
173             Args : Bio::Phylo::Matrices::Characters
174              
175             =cut
176              
177             sub set_characters : Clonable DeepClonable {
178 44     44 1 122 my ( $self, $characters ) = @_;
179 44 50       152 if ( looks_like_object $characters, _CHARACTERS_ ) {
180 44         149 $characters{ $self->get_id } = $characters;
181             }
182 44         119 return $self;
183 13     13   3711 }
  13         26  
  13         53  
184              
185             =item set_gapmode()
186              
187             Defines matrix gapmode.
188              
189             Type : Mutator
190             Title : set_gapmode
191             Usage : $matrix->set_gapmode( 1 );
192             Function: Defines matrix gapmode ( false = missing, true = fifth state )
193             Returns : $self
194             Args : boolean
195              
196             =cut
197              
198             sub set_gapmode : Clonable {
199 6     6 1 18 my ( $self, $gapmode ) = @_;
200 6         20 $gapmode{ $self->get_id } = $gapmode;
201 6         16 return $self;
202 13     13   3338 }
  13         27  
  13         48  
203              
204             =item set_matchchar()
205              
206             Assigns match symbol.
207              
208             Type : Mutator
209             Title : set_matchchar
210             Usage : $matrix->set_matchchar( $match );
211             Function: Assigns match symbol (default is '.').
212             Returns : $self
213             Args : ARRAY
214              
215             =cut
216              
217             sub set_matchchar : Clonable {
218 6     6 1 15 my ( $self, $match ) = @_;
219 6 50       16 if ( $match ) {
220 0         0 my $missing = $self->get_missing;
221 0         0 my $gap = $self->get_gap;
222 0 0       0 if ( $match eq $missing ) {
    0          
223 0         0 throw 'BadArgs' =>
224             "Match character '$match' already in use as missing character";
225             }
226             elsif ( $match eq $gap ) {
227 0         0 throw 'BadArgs' =>
228             "Match character '$match' already in use as gap character";
229             }
230             else {
231 0         0 $matchchar{ $self->get_id } = $match;
232             }
233             }
234             else {
235 6         19 $matchchar{ $self->get_id } = undef;
236             }
237 6         16 return $self;
238 13     13   3777 }
  13         42  
  13         52  
239              
240             =item set_polymorphism()
241              
242             Defines matrix 'polymorphism' interpretation.
243              
244             Type : Mutator
245             Title : set_polymorphism
246             Usage : $matrix->set_polymorphism( 1 );
247             Function: Defines matrix 'polymorphism' interpretation
248             ( false = uncertainty, true = polymorphism )
249             Returns : $self
250             Args : boolean
251              
252             =cut
253              
254             sub set_polymorphism : Clonable {
255 6     6 1 17 my ( $self, $poly ) = @_;
256 6 50       16 if ( defined $poly ) {
257 0         0 $polymorphism{ $self->get_id } = $poly;
258             }
259             else {
260 6         16 delete $polymorphism{ $self->get_id };
261             }
262 6         16 return $self;
263 13     13   3097 }
  13         27  
  13         48  
264              
265             =item set_respectcase()
266              
267             Defines matrix case sensitivity interpretation.
268              
269             Type : Mutator
270             Title : set_respectcase
271             Usage : $matrix->set_respectcase( 1 );
272             Function: Defines matrix case sensitivity interpretation
273             ( false = disregarded, true = "respectcase" )
274             Returns : $self
275             Args : boolean
276              
277             =cut
278              
279             sub set_respectcase : Clonable {
280 6     6 1 17 my ( $self, $case_sensitivity ) = @_;
281 6 50       16 if ( defined $case_sensitivity ) {
282 0         0 $case_sensitivity{ $self->get_id } = $case_sensitivity;
283             }
284             else {
285 6         42 delete $case_sensitivity{ $self->get_id };
286             }
287 6         16 return $self;
288 13     13   3173 }
  13         32  
  13         52  
289              
290             =back
291              
292             =head2 ACCESSORS
293              
294             =over
295              
296             =item get_characters()
297              
298             Retrieves characters object.
299              
300             Type : Accessor
301             Title : get_characters
302             Usage : my $characters = $matrix->get_characters
303             Function: Retrieves characters object.
304             Returns : Bio::Phylo::Matrices::Characters
305             Args : None.
306              
307             =cut
308              
309             sub get_characters {
310 169     169 1 268 my $self = shift;
311 169         332 return $characters{ $self->get_id };
312             }
313              
314             =item get_statelabels()
315              
316             Retrieves state labels.
317              
318             Type : Accessor
319             Title : get_statelabels
320             Usage : my @statelabels = @{ $matrix->get_statelabels };
321             Function: Retrieves state labels.
322             Returns : ARRAY
323             Args : None.
324              
325             =cut
326              
327 7 100   7 1 27 sub get_statelabels { $statelabels{ $_[0]->get_id } || [] }
328              
329             =item get_gapmode()
330              
331             Returns matrix gapmode.
332              
333             Type : Accessor
334             Title : get_gapmode
335             Usage : do_something() if $matrix->get_gapmode;
336             Function: Returns matrix gapmode ( false = missing, true = fifth state )
337             Returns : boolean
338             Args : none
339              
340             =cut
341              
342 6     6 1 21 sub get_gapmode { $gapmode{ $_[0]->get_id } }
343              
344             =item get_matchchar()
345              
346             Returns matrix match character.
347              
348             Type : Accessor
349             Title : get_matchchar
350             Usage : my $char = $matrix->get_matchchar;
351             Function: Returns matrix match character (default is '.')
352             Returns : SCALAR
353             Args : none
354              
355             =cut
356              
357 25     25 1 80 sub get_matchchar { $matchchar{ $_[0]->get_id } }
358              
359             =item get_polymorphism()
360              
361             Returns matrix 'polymorphism' interpretation.
362              
363             Type : Accessor
364             Title : get_polymorphism
365             Usage : do_something() if $matrix->get_polymorphism;
366             Function: Returns matrix 'polymorphism' interpretation
367             ( false = uncertainty, true = polymorphism )
368             Returns : boolean
369             Args : none
370              
371             =cut
372              
373 6     6 1 20 sub get_polymorphism { $polymorphism{ shift->get_id } }
374              
375             =item get_respectcase()
376              
377             Returns matrix case sensitivity interpretation.
378              
379             Type : Accessor
380             Title : get_respectcase
381             Usage : do_something() if $matrix->get_respectcase;
382             Function: Returns matrix case sensitivity interpretation
383             ( false = disregarded, true = "respectcase" )
384             Returns : boolean
385             Args : none
386              
387             =cut
388              
389 6     6 1 20 sub get_respectcase { $case_sensitivity{ shift->get_id } }
390              
391             sub _cleanup : Destructor {
392 39     39   84 my $self = shift;
393 39         98 my $id = $self->get_id;
394 39         93 for (@inside_out_arrays) {
395 312 100 66     998 delete $_->{$id} if defined $id and exists $_->{$id};
396             }
397 13     13   4351 }
  13         25  
  13         50  
398              
399             =back
400              
401             =cut
402              
403             # podinherit_insert_token
404              
405             =head1 SEE ALSO
406              
407             There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
408             for any user or developer questions and discussions.
409              
410             =over
411              
412             =item L<Bio::Phylo::Taxa::TaxaLinker>
413              
414             This object inherits from L<Bio::Phylo::Taxa::TaxaLinker>, so the
415             methods defined therein are also applicable to L<Bio::Phylo::Matrices::Matrix>
416             objects.
417              
418             =item L<Bio::Phylo::Matrices::TypeSafeData>
419              
420             This object inherits from L<Bio::Phylo::Matrices::TypeSafeData>, so the
421             methods defined therein are also applicable to L<Bio::Phylo::Matrices::Matrix>
422             objects.
423              
424             =item L<Bio::Phylo::Manual>
425              
426             Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
427              
428             =back
429              
430             =head1 CITATION
431              
432             If you use Bio::Phylo in published research, please cite it:
433              
434             B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
435             and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
436             I<BMC Bioinformatics> B<12>:63.
437             L<http://dx.doi.org/10.1186/1471-2105-12-63>
438              
439             =cut
440              
441             }
442             1;