Phylo/Matrices/MatrixRole.pm

filter: Statementstmt 30.7% 385 / 1254 Branchbran 18.9% 79 / 418 Conditioncond 10.1% 13 / 128 MC/DCmcdc 5.5% 4 / 72 Subroutinesub 37.2% 41 / 110 Podpod 41.2% 33 / 80 totaltotal 26.9% 555 / 2062 CC 367 SCAR 52.7
CC
367
Cov
26%
match_line
743.3
to_nexus
649.5
overall_percentage_identity
542.8
CRAP 194.6
« Phylo/Unparsers/Nexus.pm ↑ Summary Phylo/NeXML/Entities.pm »
1 package Bio::Phylo::Matrices::MatrixRole;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use base qw'Bio::Phylo::Matrices::TypeSafeData Bio::Phylo::Taxa::TaxaLinker';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Bio::Phylo::Models::Substitution::Binary;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Bio::Phylo::Models::Substitution::Dna;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Bio::Phylo::Util::OptionalInterface 'Bio::Align::AlignI';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Bio::Phylo::Util::CONSTANT qw':objecttypes /looks_like/';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Bio::Phylo::Util::Exceptions qw'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Bio::Phylo::NeXML::Writable;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Bio::Phylo::Matrices::Datum;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Bio::Phylo::IO qw(parse unparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Bio::Phylo::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 my $LOADED_WRAPPERS = 0;
16 {
17 13 my $CONSTANT_TYPE = _MATRIX_;
18 13 my $CONSTANT_CONTAINER = _MATRICES_;
19 13 my $logger = __PACKAGE__->get_logger;
20 13 my $factory = Bio::Phylo::Factory->new;
21  
22 =head1 NAME
23  
24 Bio::Phylo::Matrices::MatrixRole - Extra behaviours for a character state matrix
25  
26 =head1 SYNOPSIS
27  
28 use Bio::Phylo::Factory;
29 my $fac = Bio::Phylo::Factory->new;
30  
31 # instantiate taxa object
32 my $taxa = $fac->create_taxa;
33 for ( 'Homo sapiens', 'Pan paniscus', 'Pan troglodytes' ) {
34 $taxa->insert( $fac->create_taxon( '-name' => $_ ) );
35 }
36  
37 # instantiate matrix object, 'standard' data type. All categorical
38 # data types follow semantics like this, though with different
39 # symbols in lookup table and matrix
40 my $standard_matrix = $fac->create_matrix(
41 '-type' => 'STANDARD',
42 '-taxa' => $taxa,
43 '-lookup' => {
44 '-' => [],
45 '0' => [ '0' ],
46 '1' => [ '1' ],
47 '?' => [ '0', '1' ],
48 },
49 '-labels' => [ 'Opposable big toes', 'Opposable thumbs', 'Not a pygmy' ],
50 '-matrix' => [
51 [ 'Homo sapiens' => '0', '1', '1' ],
52 [ 'Pan paniscus' => '1', '1', '0' ],
53 [ 'Pan troglodytes' => '1', '1', '1' ],
54 ],
55 );
56  
57 # note: complicated constructor for mixed data!
58 my $mixed_matrix = Bio::Phylo::Matrices::Matrix->new(
59  
60 # if you want to create 'mixed', value for '-type' is array ref...
61 '-type' => [
62  
63 # ...with first field 'mixed'...
64 'mixed',
65  
66 # ...second field is an array ref...
67 [
68  
69 # ...with _ordered_ key/value pairs...
70 'dna' => 10, # value is length of type range
71 'standard' => 10, # value is length of type range
72  
73 # ... or, more complicated, value is a hash ref...
74 'rna' => {
75 '-length' => 10, # value is length of type range
76  
77 # ...value for '-args' is an array ref with args
78 # as can be passed to 'unmixed' datatype constructors,
79 # for example, here we modify the lookup table for
80 # rna to allow both 'U' (default) and 'T'
81 '-args' => [
82 '-lookup' => {
83 'A' => [ 'A' ],
84 'C' => [ 'C' ],
85 'G' => [ 'G' ],
86 'U' => [ 'U' ],
87 'T' => [ 'T' ],
88 'M' => [ 'A', 'C' ],
89 'R' => [ 'A', 'G' ],
90 'S' => [ 'C', 'G' ],
91 'W' => [ 'A', 'U', 'T' ],
92 'Y' => [ 'C', 'U', 'T' ],
93 'K' => [ 'G', 'U', 'T' ],
94 'V' => [ 'A', 'C', 'G' ],
95 'H' => [ 'A', 'C', 'U', 'T' ],
96 'D' => [ 'A', 'G', 'U', 'T' ],
97 'B' => [ 'C', 'G', 'U', 'T' ],
98 'X' => [ 'G', 'A', 'U', 'T', 'C' ],
99 'N' => [ 'G', 'A', 'U', 'T', 'C' ],
100 },
101 ],
102 },
103 ],
104 ],
105 );
106  
107 # prints 'mixed(Dna:1-10, Standard:11-20, Rna:21-30)'
108 print $mixed_matrix->get_type;
109  
110 =head1 DESCRIPTION
111  
112 This module defines a container object that holds
113 L<Bio::Phylo::Matrices::Datum> objects. The matrix
114 object inherits from L<Bio::Phylo::Listable>, so the
115 methods defined there apply here.
116  
117 =head1 METHODS
118  
119 =head2 CONSTRUCTOR
120  
121 =over
122  
123 =item new()
124  
125 Matrix constructor.
126  
127 Type : Constructor
128 Title : new
129 Usage : my $matrix = Bio::Phylo::Matrices::Matrix->new;
130 Function: Instantiates a Bio::Phylo::Matrices::Matrix
131 object.
132 Returns : A Bio::Phylo::Matrices::Matrix object.
133 Args : -type => optional, but if used must be FIRST argument,
134 defines datatype, one of dna|rna|protein|
135 continuous|standard|restriction|[ mixed => [] ]
136  
137 -taxa => optional, link to taxa object
138 -lookup => character state lookup hash ref
139 -labels => array ref of character labels
140 -matrix => two-dimensional array, first element of every
141 row is label, subsequent are characters
142  
143 =cut
144  
145 38 ▶ sub new : Constructor {
Subroutine new: called CC=3 SCAR=24.3
Pod documented
146  
147 # could be child class
148 38 my $class = shift;
149  
150 # notify user
151 38 $logger->info("constructor called for '$class'");
152 38 ▶ if ( not $LOADED_WRAPPERS ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LOADED_WRAPPERS) 11 27 38
153 0 ▶ eval do { local $/; <DATA> };
Subroutine __ANON__: not called CC=2 SCAR=17.8
Subroutine accession: not called CC=2 SCAR=17.8
Subroutine add_seq: not called CC=1 SCAR=6.8
Subroutine annotation: not called CC=2 SCAR=17.8
Subroutine average_percentage_identity: not called CC=11 SCAR=48.7
Subroutine column_from_residue_number: not called CC=5 SCAR=33.9
Subroutine consensus_iupac: not called CC=1 SCAR=6.8
Subroutine consensus_meta: not called CC=2 SCAR=17.8
Subroutine consensus_string: not called CC=3 SCAR=24.7
Subroutine datatype: not called CC=1 SCAR=6.8
Subroutine description: not called CC=2 SCAR=17.8
Subroutine displayname: not called CC=6 SCAR=37.2
Subroutine each_alphabetically: not called CC=1 SCAR=6.8
Subroutine each_seq: not called CC=1 SCAR=6.8
Subroutine each_seq_with_id: not called CC=1 SCAR=6.8
Subroutine gap_char: not called CC=2 SCAR=17.8
Subroutine get_SeqFeatures: not called CC=1 SCAR=6.8
Subroutine get_seq_by_pos: not called CC=1 SCAR=6.8
Subroutine id: not called CC=2 SCAR=17.8
Subroutine is_flush: not called CC=1 SCAR=6.8
Subroutine length: not called CC=1 SCAR=6.8
Subroutine map_chars: not called CC=3 SCAR=24.7
Subroutine match: not called CC=5 SCAR=33.9
Subroutine match_char: not called CC=2 SCAR=17.8
Subroutine match_line: not called CC=27 SCAR=66.1
Subroutine max_metaname_length: not called CC=9 SCAR=44.8
Subroutine maxdisplayname_length: not called CC=3 SCAR=24.7
Subroutine maxname_length: not called CC=1 SCAR=6.8
Subroutine missing_char: not called CC=2 SCAR=17.8
Subroutine no_residues: not called CC=1 SCAR=6.8
Subroutine no_sequences: not called CC=1 SCAR=6.8
Subroutine num_sequences: not called CC=1 SCAR=6.8
Subroutine overall_percentage_identity: not called CC=23 SCAR=63.0
Subroutine percentage_identity: not called CC=1 SCAR=6.8
Subroutine purge: not called CC=1 SCAR=6.8
Subroutine remove_seq: not called CC=1 SCAR=6.8
Subroutine score: not called CC=2 SCAR=17.8
Subroutine select: not called CC=5 SCAR=33.9
Subroutine select_noncont: not called CC=4 SCAR=29.8
Subroutine set_displayname_count: not called CC=1 SCAR=6.8
Subroutine set_displayname_flat: not called CC=2 SCAR=17.8
Subroutine set_displayname_normal: not called CC=1 SCAR=6.8
Subroutine slice: not called CC=4 SCAR=29.8
Subroutine sort_alphabetically: not called CC=1 SCAR=6.8
Subroutine source: not called CC=2 SCAR=17.8
Subroutine symbol_chars: not called CC=4 SCAR=29.8
Subroutine unmatch: not called CC=5 SCAR=33.9
Subroutine uppercase: not called CC=2 SCAR=17.8
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 65 branches
BranchTrueFalseTotal
if $_[0]->get_nse eq $name 0 0 0
if (defined $acc) 0 0 0
if (defined $anno) 0 0 0
unless ($self->is_flush) 0 0 0
if ($index < $len) 0 0 0
unless $countHashes[$index] 0 0 0
if ($column < @seqChars) 0 0 0
if (exists $countHashes[$column]{$char}) 0 0 0
if ($column < $len) 0 0 0
$divisor > 0 ? : 0 0 0
if (my $seq = $self->get_by_name($seqname)) 0 0 0
if $char[$i] ne $gap 0 0 0
if ($col + 1 == $resnumber) 0 0 0
if (defined $meta) 0 0 0
if (defined $desc) 0 0 0
unless $seq 0 0 0
if ($disname and $name) { } 0 0 0
elsif (defined $disnames->{$name}) { } 0 0 0
if (defined $gap) 0 0 0
if (defined $name) 0 0 0
if (defined $match) { } 0 0 0
if ($char[$j] eq $firstseq[$j]) 0 0 0
if (defined $match) 0 0 0
unless defined $alphabet 0 0 0
unless (defined $refchar) 0 0 0
if $pos == $self->length 0 0 0
if $pos >= scalar @$seq 0 0 0
if $seq->[$pos] eq "-" or $seq->[$pos] eq "." or $seq->[$pos] eq " " 0 0 0
if defined $seq->[$pos] 0 0 0
if ($dash) { } 0 0 0
elsif (@colresidues == 1) { } 0 0 0
elsif ($alphabet eq 'protein') { } 0 0 0
if scalar @$cols != scalar @colresidues 0 0 0
if ($_ < scalar @$cols) 0 0 0
if $cols->[$_] ne $colresidues[$_] 0 0 0
unless $seq->isa("Bio::Seq::MetaI") 0 0 0
if ($len > $maxname) 0 0 0
unless $meta 0 0 0
if ($len > $maxname) 0 0 0
if ($len > $maxname) 0 0 0
if (defined $missing) 0 0 0
if $length_measure and not $enum{$length_measure} 0 0 0
unless ($self->is_flush) 0 0 0
if ($index < $len) 0 0 0
unless $countHashes[$index] 0 0 0
if ($column < @seqChars) 0 0 0
if (exists $countHashes[$column]{$char}) 0 0 0
if ($column < $len) 0 0 0
if $_ == 0 0 0 0
if $_ == scalar @seqs 0 0 0
if ($length_measure eq 'short') { } 0 0 0
elsif ($length_measure eq 'long') { } 0 0 0
if ($len) { } 0 0 0
if $count < $len 0 0 0
if ($len) { } 0 0 0
if $count > $len 0 0 0
if (defined $score) 0 0 0
if ($i < $start - 1 or $i > $end - 1) 0 0 0
unless (exists $keep{$i}) 0 0 0
unless ($include_gapped) 0 0 0
unless (grep {not $_ =~ /^\Q$gap\E$/;} @slice) 0 0 0
if (defined $source) 0 0 0
if $includeextra 0 0 0
if (defined $match) { } 0 0 0
if ($char[$j] eq $match) 0 0 0
Condition0%0/31
$self->get_generic('displaynames') || {}
l!l
00
$disname and $name
!ll&&!rl&&r
000
$matchlinechar || '*'
l!l
00
$weak || '.'
l!l
00
$strong || ':'
l!l
00
$refchar eq '-' || $refchar eq '.' || $refchar eq ' '
l!l&&r!l&&!r
000
$refchar eq "-" or $refchar eq "."
l!l&&r!l&&!r
000
$seq->[$pos] eq "-" or $seq->[$pos] eq "." or $seq->[$pos] eq " "
l!l&&r!l&&!r
000
$seq->[$pos] eq "-" or $seq->[$pos] eq "."
l!l&&r!l&&!r
000
$length_measure and not $enum{$length_measure}
!ll&&!rl&&r
000
$length_measure ||= "align"
l!l
00
$i < $start - 1 or $i > $end - 1
l!l&&r!l&&!r
000
MC/DC0%0/1
$self->get_generic('displaynames') || {}
$self->get_generic('displaynames')
MC/DC0%0/2
$disname and $name
$disname $name
MC/DC0%0/1
$matchlinechar || '*'
$matchlinechar
MC/DC0%0/1
$weak || '.'
$weak
MC/DC0%0/1
$strong || ':'
$strong
MC/DC0%0/3
$refchar eq '-' || $refchar eq '.' || $refchar eq ' '
$refchar eq "-" $refchar eq "." $refchar eq ' '
MC/DC0%0/3
$seq->[$pos] eq "-" or $seq->[$pos] eq "." or $seq->[$pos] eq " "
$seq->[$pos] eq "-" $seq->[$pos] eq "." $seq->[$pos] eq " "
MC/DC0%0/2
$length_measure and not $enum{$length_measure}
$length_measure not $enum{$length_measure}
MC/DC0%0/1
$length_measure ||= "align"
$length_measure
MC/DC0%0/2
$i < $start - 1 or $i > $end - 1
$i < $start - 1 $i > $end - 1
Truth table0%0/2
A
A= $self->get_generic('displaynames')
Aresult
11
00
Truth table0%0/3
A and B
A= $disnameB= $name
ABresult
0X0
100
111
Truth table0%0/2
A
A= $matchlinechar
Aresult
11
00
Truth table0%0/2
A
A= $weak
Aresult
11
00
Truth table0%0/2
A
A= $strong
Aresult
11
00
Truth table0%0/4
A or B || C
A= $refchar eq "-"B= $refchar eq "."C= $refchar eq ' '
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A or B or C
A= $seq->[$pos] eq "-"B= $seq->[$pos] eq "."C= $seq->[$pos] eq " "
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/3
A and not(B)
A= $length_measureB= not $enum{$length_measure}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $length_measure
Aresult
11
00
Truth table0%0/3
A or B
A= $i < $start - 1B= $i > $end - 1
ABresult
1X1
011
000
154 11 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
155 11 $LOADED_WRAPPERS++;
156 }
157  
158 # go up inheritance tree, eventually get an ID
159 38 my $self = $class->SUPER::new(
160 '-characters' => $factory->create_characters,
161 '-listener' => \&_update_characters,
162 @_
163 );
164 38 return $self;
165 13 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 =item new_from_bioperl()
168  
169 Matrix constructor from Bio::Align::AlignI argument.
170  
171 Type : Constructor
172 Title : new_from_bioperl
173 Usage : my $matrix =
174 Bio::Phylo::Matrices::Matrix->new_from_bioperl(
175 $aln
176 );
177 Function: Instantiates a
178 Bio::Phylo::Matrices::Matrix object.
179 Returns : A Bio::Phylo::Matrices::Matrix object.
180 Args : An alignment that implements Bio::Align::AlignI
181  
182 =cut
183  
184 0 ▶ sub new_from_bioperl {
Subroutine new_from_bioperl: not called CC=10 SCAR=47.0
Pod documented
185 0 my ( $class, $aln, @args ) = @_;
186 0 ▶ if ( looks_like_instance( $aln, 'Bio::Align::AlignI' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_instance($aln, 'Bio::Align::AlignI')) { } 0 0 0
187 0 $aln->unmatch;
188 0 $aln->map_chars( '\.', '-' );
189 0 my @seqs = $aln->each_seq;
190 0 my ( $type, $missing, $gap, $matchchar );
191 0 ▶ if ( $seqs[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seqs[0]) { } 0 0 0
192 0 ▶ $type =
Condition0%0/5
$seqs[0]->alphabet || $seqs[0]->_guess_alphabet || 'dna'
l!l
00
$seqs[0]->alphabet or $seqs[0]->_guess_alphabet
l!l&&r!l&&!r
000
MC/DC0%0/2
$seqs[0]->alphabet || $seqs[0]->_guess_alphabet || 'dna'
$seqs[0]->alphabet $seqs[0]->_guess_alphabet
Truth table0%0/3
A or B
A= $seqs[0]->alphabetB= $seqs[0]->_guess_alphabet
ABresult
1X1
011
000
193 $seqs[0]->alphabet
194 || $seqs[0]->_guess_alphabet
195 || 'dna';
196 }
197 else {
198 0 $type = 'dna';
199 }
200 0 ▶ my $self = $factory->create_matrix(
Condition0%0/6
$aln->missing_char || '?'
l!l
00
$aln->match_char || '.'
l!l
00
$aln->gap_char || '-'
l!l
00
MC/DC0%0/1
$aln->missing_char || '?'
$aln->missing_char
MC/DC0%0/1
$aln->match_char || '.'
$aln->match_char
MC/DC0%0/1
$aln->gap_char || '-'
$aln->gap_char
Truth table0%0/2
A
A= $aln->missing_char
Aresult
11
00
Truth table0%0/2
A
A= $aln->match_char
Aresult
11
00
Truth table0%0/2
A
A= $aln->gap_char
Aresult
11
00
201 '-type' => $type,
202 '-special_symbols' => {
203 '-missing' => $aln->missing_char || '?',
204 '-matchchar' => $aln->match_char || '.',
205 '-gap' => $aln->gap_char || '-',
206 },
207 @args
208 );
209  
210 # XXX create raw getter/setter pairs for annotation,
211 # accession, consensus_meta source
212 0 for my $field ( qw(description accession id annotation consensus_meta score source) ) {
213 0 $self->$field( $aln->$field );
214 }
215 0 my $to = $self->get_type_object;
216 0 for my $seq (@seqs) {
217 0 my $datum = Bio::Phylo::Matrices::Datum->new_from_bioperl( $seq,
218 '-type_object' => $to );
219 0 $self->insert($datum);
220 }
221 0 return $self;
222 }
223 else {
224 0 throw 'ObjectMismatch' => 'Not a bioperl alignment!';
225 }
226 }
227  
228 =back
229  
230 =head2 MUTATORS
231  
232 =over
233  
234 =item set_special_symbols
235  
236 Sets three special symbols in one call
237  
238 Type : Mutator
239 Title : set_special_symbols
240 Usage : $matrix->set_special_symbols(
241 -missing => '?',
242 -gap => '-',
243 -matchchar => '.'
244 );
245 Function: Assigns state labels.
246 Returns : $self
247 Args : Three args (with distinct $x, $y and $z):
248 -missing => $x,
249 -gap => $y,
250 -matchchar => $z
251 Notes : This method is here to ensure
252 you don't accidentally use the
253 same symbol for missing AND gap
254  
255 =cut
256  
257 0 ▶ sub set_special_symbols {
Subroutine set_special_symbols: not called CC=20 SCAR=60.4
Pod documented
258 0 my $self = shift;
259 0 my %args;
260 0 ▶ if ( ( @_ == 1 && ref( $_[0] ) eq 'HASH' && ( %args = %{ $_[0] } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq "HASH" and %args = %{$_[0];} or %args = &looks_like_hash(@_)) 0 0 0
Condition0%0/9
@_ == 1 and ref $_[0] eq "HASH" and %args = %{$_[0];} or %args = &looks_like_hash(@_)
l!l&&r!l&&!r
000
@_ == 1 and ref $_[0] eq "HASH" and %args = %{$_[0];}
!ll&&!rl&&r
000
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/4
@_ == 1 and ref $_[0] eq "HASH" and %args = %{$_[0];} or %args = &looks_like_hash(@_)
@_ == 1 ref $_[0] eq "HASH" %args = %{$_[0];} %args = &looks_like_hash(@_)
Truth table0%0/7
A and B and C or D
A= @_ == 1B= ref $_[0] eq "HASH"C= %args = %{$_[0];}D= %args = &looks_like_hash(@_)
ABCDresult
111X1
0XX11
10X11
11011
0XX00
10X00
11000
261 || ( %args = looks_like_hash @_ ) )
262 {
263 0 ▶ if ( !defined $args{'-missing'}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"-missing"} and defined $args{"-gap"} and defined $args{"-matchchar"}) 0 0 0
Condition0%0/6
defined $args{"-missing"} and defined $args{"-gap"} and defined $args{"-matchchar"}
!ll&&!rl&&r
000
defined $args{"-missing"} and defined $args{"-gap"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $args{"-missing"} and defined $args{"-gap"} and defined $args{"-matchchar"}
defined $args{"-missing"} defined $args{"-gap"} defined $args{"-matchchar"}
Truth table0%0/4
A and B and C
A= defined $args{"-missing"}B= defined $args{"-gap"}C= defined $args{"-matchchar"}
ABCresult
0XX0
10X0
1100
1111
264 || !defined $args{'-gap'}
265 || !defined $args{'-matchchar'} )
266 {
267 0 throw 'BadArgs' =>
268 'Need -missing => $x, -gap => $y, -matchchar => $z arguments, not '
269 . "@_";
270 }
271 0 my %values = map { $_ => 1 } values %args;
272 0 my @values = keys %values;
273 0 ▶ if ( scalar @values < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @values < 3) 0 0 0
274 0 throw 'BadArgs' => 'Symbols must be distinct, not '
275 . join( ', ', values %args );
276 }
277 0 ▶ my %old_special_symbols = (
Condition0%0/6
$self->get_missing || '?'
l!l
00
$self->get_gap || '-'
l!l
00
$self->get_matchchar || '.'
l!l
00
MC/DC0%0/1
$self->get_missing || '?'
$self->get_missing
MC/DC0%0/1
$self->get_gap || '-'
$self->get_gap
MC/DC0%0/1
$self->get_matchchar || '.'
$self->get_matchchar
Truth table0%0/2
A
A= $self->get_missing
Aresult
11
00
Truth table0%0/2
A
A= $self->get_gap
Aresult
11
00
Truth table0%0/2
A
A= $self->get_matchchar
Aresult
11
00
278 ( $self->get_missing || '?' ) => 'set_missing',
279 ( $self->get_gap || '-' ) => 'set_gap',
280 ( $self->get_matchchar || '.' ) => 'set_matchchar',
281 );
282 my %new_special_symbols = (
283 $args{'-missing'} => 'set_missing',
284 $args{'-gap'} => 'set_gap',
285 0 $args{'-matchchar'} => 'set_matchchar',
286 );
287 0 my %dummies;
288 0 ▶ while (%new_special_symbols) {
Branch: 1 branch
BranchTrueFalseTotal
if (%new_special_symbols) 0 0 0
289 0 for my $sym ( keys %new_special_symbols ) {
290 0 ▶ if ( not $old_special_symbols{$sym} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $old_special_symbols{$sym}) { } 0 0 0
elsif ($old_special_symbols{$sym} eq $new_special_symbols{$sym}) { } 0 0 0
291 0 my $method = $new_special_symbols{$sym};
292 0 $self->$method($sym);
293 0 delete $new_special_symbols{$sym};
294 }
295 elsif ( $old_special_symbols{$sym} eq
296 $new_special_symbols{$sym} )
297 {
298 0 delete $new_special_symbols{$sym};
299 }
300 else {
301 0 DUMMY: for my $dummy (qw(! @ $ % ^ & *)) {
302 0 ▶ if ( !$new_special_symbols{$dummy}
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_special_symbols{$dummy} or $old_special_symbols{$dummy} or $dummies{$dummy}) 0 0 0
Condition0%0/6
$new_special_symbols{$dummy} or $old_special_symbols{$dummy} or $dummies{$dummy}
l!l&&r!l&&!r
000
$new_special_symbols{$dummy} or $old_special_symbols{$dummy}
l!l&&r!l&&!r
000
MC/DC0%0/3
$new_special_symbols{$dummy} or $old_special_symbols{$dummy} or $dummies{$dummy}
$new_special_symbols{$dummy} $old_special_symbols{$dummy} $dummies{$dummy}
Truth table0%0/4
A or B or C
A= $new_special_symbols{$dummy}B= $old_special_symbols{$dummy}C= $dummies{$dummy}
ABCresult
1XX1
01X1
0011
0000
303 && !$old_special_symbols{$dummy}
304 && !$dummies{$dummy} )
305 {
306 0 my $method = $old_special_symbols{$sym};
307 0 $self->$method($dummy);
308 0 $dummies{$dummy} = 1;
309 0 delete $old_special_symbols{$sym};
310 0 $old_special_symbols{$dummy} = $method;
311 0 last DUMMY;
312 }
313 }
314 }
315 }
316 }
317 }
318 0 return $self;
319 }
320  
321 =item set_charlabels()
322  
323 Sets argument character labels.
324  
325 Type : Mutator
326 Title : set_charlabels
327 Usage : $matrix->set_charlabels( [ 'char1', 'char2', 'char3' ] );
328 Function: Assigns character labels.
329 Returns : $self
330 Args : ARRAY, or nothing (to reset);
331  
332 =cut
333  
334 4 ▶ sub set_charlabels {
Subroutine set_charlabels: called CC=8 SCAR=23.2
Pod documented
335 4 my ( $self, $charlabels ) = @_;
336  
337 # it's an array ref, but what about its contents?
338 4 ▶ if ( looks_like_instance( $charlabels, 'ARRAY' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&looks_like_instance($charlabels, 'ARRAY')) { } 4 0 4
elsif (defined $charlabels and not &looks_like_instance($charlabels, 'ARRAY')) { } 0 0 0
339 4 for my $label ( @{$charlabels} ) {
340 15 ▶ if ( ref $label ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $label) 0 15 15
341 0 throw 'BadArgs' =>
342 "charlabels must be an array ref of scalars";
343 }
344 }
345 }
346  
347 # it's defined but not an array ref
348 ▶ elsif ( defined $charlabels
Condition0%0/3
defined $charlabels and not &looks_like_instance($charlabels, "ARRAY")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $charlabels and not &looks_like_instance($charlabels, "ARRAY")
defined $charlabels not &looks_like_instance($charlabels, "ARRAY")
Truth table0%0/3
A and not(B)
A= defined $charlabelsB= not &looks_like_instance($charlabels, "ARRAY")
ABresult
0X0
100
111
349 && !looks_like_instance( $charlabels, 'ARRAY' ) )
350 {
351 0 throw 'BadArgs' => "charlabels must be an array ref of scalars";
352 }
353  
354 # there might be more labels than currently existing characters.
355 # here we add however more are needed
356 4 my $characters = $self->get_characters;
357 4 my $missing = scalar(@{$charlabels}) - scalar(@{$characters->get_entities});
358 4 for my $i ( 1 .. $missing ) {
359 4 $characters->insert($factory->create_character);
360 }
361  
362 # it's either a valid array ref, or nothing, i.e. a reset
363 4 my @characters = @{ $characters->get_entities };
364 4 for my $i ( 0 .. $#{$charlabels} ) {
365 15 $characters[$i]->set_name( $charlabels->[$i] );
366 }
367 4 return $self;
368 }
369  
370 =item set_raw()
371  
372 Set contents using two-dimensional array argument.
373  
374 Type : Mutator
375 Title : set_raw
376 Usage : $matrix->set_raw( [ [ 'taxon1' => 'acgt' ], [ 'taxon2' => 'acgt' ] ] );
377 Function: Syntax sugar to define $matrix data contents.
378 Returns : $self
379 Args : A two-dimensional array; first dimension contains matrix rows,
380 second dimension contains taxon name / character string pair.
381  
382 =cut
383  
384 12 ▶ sub set_raw {
Subroutine set_raw: called CC=7 SCAR=20.5
Pod documented
385 12 my ( $self, $raw ) = @_;
386 12 ▶ if ( defined $raw ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw) 12 0 12
387 12 ▶ if ( looks_like_instance( $raw, 'ARRAY' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_instance($raw, 'ARRAY')) { } 12 0 12
388 12 my @rows;
389 12 for my $row ( @{$raw} ) {
390 46 ▶ if ( defined $row ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row) 46 0 46
391 46 ▶ if ( looks_like_instance( $row, 'ARRAY' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_instance($row, 'ARRAY')) { } 46 0 46
392 my $matrixrow = $factory->create_datum(
393 '-type_object' => $self->get_type_object,
394 '-name' => $row->[0],
395 46 '-char' => join( ' ', @$row[ 1 .. $#{$row} ] ),
396 );
397 46 push @rows, $matrixrow;
398 }
399 else {
400 0 throw 'BadArgs' =>
401 "Raw matrix row must be an array reference";
402 }
403 }
404 }
405 12 $self->clear;
406 12 $self->insert($_) for @rows;
407 }
408 else {
409 0 throw 'BadArgs' => "Raw matrix must be an array reference";
410 }
411 }
412 12 return $self;
413 }
414  
415 =back
416  
417 =head2 ACCESSORS
418  
419 =over
420  
421 =item get_special_symbols()
422  
423 Retrieves hash ref for missing, gap and matchchar symbols
424  
425 Type : Accessor
426 Title : get_special_symbols
427 Usage : my %syms = %{ $matrix->get_special_symbols };
428 Function: Retrieves special symbols
429 Returns : HASH ref, e.g. { -missing => '?', -gap => '-', -matchchar => '.' }
430 Args : None.
431  
432 =cut
433  
434 0 ▶ sub get_special_symbols {
Subroutine get_special_symbols: not called CC=1 SCAR=6.9
Pod documented
435 0 my $self = shift;
436 return {
437 0 '-missing' => $self->get_missing,
438 '-matchchar' => $self->get_matchchar,
439 '-gap' => $self->get_gap
440 };
441 }
442  
443 =item get_charlabels()
444  
445 Retrieves character labels.
446  
447 Type : Accessor
448 Title : get_charlabels
449 Usage : my @charlabels = @{ $matrix->get_charlabels };
450 Function: Retrieves character labels.
451 Returns : ARRAY
452 Args : None.
453  
454 =cut
455  
456 4 ▶ sub get_charlabels {
Subroutine get_charlabels: called CC=1 SCAR=0.0
Pod documented
457 4 my $self = shift;
458 my @labels =
459 4 map { $_->get_name } @{ $self->get_characters->get_entities };
460 4 return \@labels;
461 }
462  
463 =item get_nchar()
464  
465 Calculates number of characters.
466  
467 Type : Accessor
468 Title : get_nchar
469 Usage : my $nchar = $matrix->get_nchar;
470 Function: Calculates number of characters (columns) in matrix (if the matrix
471 is non-rectangular, returns the length of the longest row).
472 Returns : INT
473 Args : none
474  
475 =cut
476  
477 214 ▶ sub get_nchar {
Subroutine get_nchar: called CC=4 SCAR=13.9
Pod documented
478 214 my $self = shift;
479 214 my $nchar = 0;
480 214 for my $row ( @{ $self->get_entities } ) {
481 639 ▶ my $offset = ( $row->get_position || 1 ) - 1;
Condition50%1/2
$row->get_position || 1
l!l
0639
MC/DC0%0/1
$row->get_position || 1
$row->get_position
Truth table50%1/2
A
A= $row->get_position
Aresult
11
00
482 639 my $rowlength = scalar @{ $row->get_entities };
483 639 $rowlength += $offset;
484 639 ▶ $nchar = $rowlength if $rowlength > $nchar;
Branch: 1 branch
BranchTrueFalseTotal
if $rowlength > $nchar 199 440 639
485 }
486 214 return $nchar;
487 }
488  
489 =item get_ntax()
490  
491 Calculates number of taxa (rows) in matrix.
492  
493 Type : Accessor
494 Title : get_ntax
495 Usage : my $ntax = $matrix->get_ntax;
496 Function: Calculates number of taxa (rows) in matrix
497 Returns : INT
498 Args : none
499  
500 =cut
501  
502 16 ▶ sub get_ntax { scalar @{ shift->get_entities } }
Subroutine get_ntax: called CC=1 SCAR=0.0
Pod documented
503  
504 =item get_raw()
505  
506 Retrieves a 'raw' (two-dimensional array) representation of the matrix's contents.
507  
508 Type : Accessor
509 Title : get_raw
510 Usage : my $rawmatrix = $matrix->get_raw;
511 Function: Retrieves a 'raw' (two-dimensional array) representation
512 of the matrix's contents.
513 Returns : A two-dimensional array; first dimension contains matrix rows,
514 second dimension contains taxon name and characters.
515 Args : NONE
516  
517 =cut
518  
519 5 ▶ sub get_raw {
Subroutine get_raw: called CC=2 SCAR=6.9
Pod documented
520 5 my $self = shift;
521 5 my @raw;
522 5 for my $row ( @{ $self->get_entities } ) {
523 22 my @row;
524 22 push @row, $row->get_name;
525 22 my @char = $row->get_char;
526 22 push @row, @char;
527 22 push @raw, \@row;
528 }
529 5 return \@raw;
530 }
531  
532 =item get_ungapped_columns()
533  
534 Type : Accessor
535 Title : get_ungapped_columns
536 Usage : my @ungapped = @{ $matrix->get_ungapped_columns };
537 Function: Retrieves the zero-based column indices of columns without gaps
538 Returns : An array reference with zero or more indices (i.e. integers)
539 Args : NONE
540  
541 =cut
542  
543 1 ▶ sub get_ungapped_columns {
Subroutine get_ungapped_columns: called CC=4 SCAR=13.9
Pod documented
544 1 my $self = shift;
545 1 my $raw = $self->get_raw;
546 1 my $nchar = $self->get_nchar;
547 1 my @ungapped;
548 1 my $gap = $self->get_gap;
549 1 for my $i ( 1 .. $nchar ) {
550 29 my %seen;
551 29 for my $row ( @$raw ) {
552 116 my $c = $row->[$i];
553 116 $seen{$c}++;
554 }
555 29 ▶ push @ungapped, $i - 1 unless $seen{$gap};
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$gap} 19 10 29
556 }
557 1 return \@ungapped;
558 }
559  
560 =item get_invariant_columns()
561  
562 Type : Accessor
563 Title : get_invariant_columns
564 Usage : my @invariant = @{ $matrix->get_invariant_columns };
565 Function: Retrieves the zero-based column indices of invariant columns
566 Returns : An array reference with zero or more indices (i.e. integers)
567 Args : Optional:
568 -gap => if true, counts the gap symbol (probably '-') as a variant
569 -missing => if true, counts the missing symbol (probably '?') as a variant
570  
571 =cut
572  
573 2 ▶ sub get_invariant_columns {
Subroutine get_invariant_columns: called CC=7 SCAR=19.5
Pod documented
574 2 my ( $self, %args ) = @_;
575 2 my $raw = $self->get_raw;
576 2 my $nchar = $self->get_nchar;
577 2 my $gap = $self->get_gap;
578 2 my $miss = $self->get_missing;
579 2 my @invariant;
580 2 for my $i ( 1 .. $nchar ) {
581 58 my %seen;
582 58 for my $row ( @$raw ) {
583 232 my $c = uc $row->[$i];
584 232 $seen{$c}++;
585 }
586 58 my @states = keys %seen;
587 58 ▶ @states = grep { $_ ne $gap } @states unless $args{'-gap'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"-gap"} 29 29 58
588 58 ▶ @states = grep { $_ ne $miss } @states unless $args{'-missing'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"-missing"} 58 0 58
589 58 ▶ push @invariant, $i - 1 if @states and @states == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @states and @states == 1 35 23 58
Condition100%3/3
@states and @states == 1
!ll&&!rl&&r
12235
MC/DC100%2/2
@states and @states == 1
@states @states == 1
Truth table100%3/3
A and B
A= @statesB= @states == 1
ABresult
0X0
100
111
590 }
591 2 return \@invariant;
592 }
593  
594  
595 =back
596  
597 =head2 CALCULATIONS
598  
599 =over
600  
601 =item calc_indel_sizes()
602  
603 Calculates size distribution of insertions or deletions
604  
605 Type : Calculation
606 Title : calc_indel_sizes
607 Usage : my %sizes = %{ $matrix->calc_indel_sizes };
608 Function: Calculates the size distribution of indels.
609 Returns : HASH
610 Args : Optional:
611 -trim => if true, disregards indels at start and end
612 -insertions => if true, counts insertions, if false, counts deletions
613  
614 =cut
615  
616 0 ▶ sub calc_indel_sizes {
Subroutine calc_indel_sizes: not called CC=16 SCAR=56.1
Pod documented
617 0 my ($self,%args) = @_;
618 0 my $ntax = $self->get_ntax;
619 0 my $nchar = $self->get_nchar;
620 0 my $raw = $self->get_raw;
621 0 my $gap = $self->get_gap;
622 0 my @indels;
623  
624 # iterate over rows
625 0 for my $row ( @$raw ) {
626 0 my $previous;
627 my @row_indels;
628  
629 # iterate over columns
630 0 for my $i ( 1 .. $nchar ) {
631  
632 # focal cell is a gap
633 0 ▶ if ( $row->[$i] eq $gap ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->[$i] eq $gap) { } 0 0 0
634 0 ▶ if ( $previous ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous) { } 0 0 0
635  
636 # we're extending an indel
637 0 ▶ if ( $previous eq $gap ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous eq $gap) { } 0 0 0
638 0 $row_indels[-1]->{'end'} = $i;
639 }
640  
641 # we're starting an indel
642 else {
643 0 push @row_indels, { 'start' => $i };
644 }
645 }
646  
647 # sequence starts with an indel
648 else {
649 0 push @row_indels, { 'start' => $i };
650 }
651 }
652 else {
653 # gap of length 1 is closed: start==end
654 0 ▶ if ( $previous and $previous eq $gap ){
Branch: 1 branch
BranchTrueFalseTotal
if ($previous and $previous eq $gap) 0 0 0
Condition0%0/3
$previous and $previous eq $gap
!ll&&!rl&&r
000
MC/DC0%0/2
$previous and $previous eq $gap
$previous $previous eq $gap
Truth table0%0/3
A and B
A= $previousB= $previous eq $gap
ABresult
0X0
100
111
655 0 $row_indels[-1]->{'end'} = $i;
656 }
657 }
658 0 $previous = $row->[$i];
659 }
660  
661 # flatten the lists
662 0 push @indels, @row_indels;
663 }
664  
665 # remove tops and tails
666 0 ▶ if ( $args{'-trim'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-trim"}) 0 0 0
667 0 @indels = grep { $_->{'start'} > 1 } @indels;
668 0 @indels = grep { $_->{'end'} < $nchar } @indels;
669 }
670  
671 # count sizes
672 0 my %sizes;
673 0 for my $i ( 1 .. $nchar ) {
674 0 my @starting_here = grep { $_->{'start'} == $i } @indels;
675 0 ▶ if ( @starting_here ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@starting_here) 0 0 0
676 0 for my $j ( $i + 1 .. $nchar ) {
677 0 my @ending_here = grep { $_->{'end'} == $j } @starting_here;
678 0 ▶ if ( @ending_here ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ending_here) 0 0 0
679 0 my $size = $j - $i;
680  
681 # more taxa have an indel here than don't. crudely, we
682 # say this is probably an insertion
683 0 ▶ if ( scalar(@ending_here) > ( $ntax / 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ending_here > $ntax / 2) { } 0 0 0
684 0 ▶ $sizes{$size}++ if $args{'-insertions'};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"-insertions"} 0 0 0
685 }
686 # it's a deletion
687 else {
688 0 ▶ $sizes{$size}++ if not $args{'-insertions'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"-insertions"} 0 0 0
689 }
690 }
691 }
692 }
693 }
694 0 return \%sizes;
695 }
696  
697  
698 =item calc_prop_invar()
699  
700 Calculates proportion of invariant sites.
701  
702 Type : Calculation
703 Title : calc_prop_invar
704 Usage : my $pinvar = $matrix->calc_prop_invar;
705 Function: Calculates proportion of invariant sites.
706 Returns : Scalar: a number
707 Args : Optional:
708 # if true, counts missing (usually the '?' symbol) as a state
709 # in the final tallies. Otherwise, missing states are ignored
710 -missing => 1
711 # if true, counts gaps (usually the '-' symbol) as a state
712 # in the final tallies. Otherwise, gap states are ignored
713 -gap => 1
714  
715 =cut
716  
717 1 ▶ sub calc_prop_invar {
Subroutine calc_prop_invar: called CC=7 SCAR=19.5
Pod documented
718 1 my $self = shift;
719 1 my $raw = $self->get_raw;
720 1 my $ntax = $self->get_ntax;
721 1 ▶ my $nchar = $self->get_nchar || 1;
Condition50%1/2
$self->get_nchar || 1
l!l
10
MC/DC0%0/1
$self->get_nchar || 1
$self->get_nchar
Truth table50%1/2
A
A= $self->get_nchar
Aresult
11
00
722 1 my %args = looks_like_hash @_;
723 1 my @symbols_to_ignore;
724 1 for my $sym (qw(missing gap)) {
725 2 ▶ if ( not exists $args{"-${sym}"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"-$sym"}) 2 0 2
726 2 my $method = "get_${sym}";
727 2 push @symbols_to_ignore, $self->$method;
728 }
729 }
730 1 my $invar_count;
731 1 for my $i ( 1 .. $nchar ) {
732 4 my %seen;
733 4 for my $j ( 0 .. ( $ntax - 1 ) ) {
734 12 $seen{ $raw->[$j]->[$i] }++;
735 }
736 4 delete @seen{@symbols_to_ignore};
737 4 my @symbols_in_column = keys %seen;
738 4 ▶ $invar_count++ if scalar(@symbols_in_column) <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @symbols_in_column <= 1 2 2 4
739 }
740 1 return $invar_count / $nchar;
741 }
742  
743 =item calc_state_counts()
744  
745 Calculates occurrences of states.
746  
747 Type : Calculation
748 Title : calc_state_counts
749 Usage : my %counts = %{ $matrix->calc_state_counts };
750 Function: Calculates occurrences of states.
751 Returns : Hashref: keys are states, values are counts
752 Args : Optional - one or more states to focus on
753  
754 =cut
755  
756 4 ▶ sub calc_state_counts {
Subroutine calc_state_counts: called CC=3 SCAR=11.0
Pod documented
757 4 my $self = shift;
758 4 my %totals;
759 4 for my $row ( @{ $self->get_entities } ) {
760 18 my $counts = $row->calc_state_counts(@_);
761 18 for my $state ( keys %{$counts} ) {
762 60 $totals{$state} += $counts->{$state};
763 }
764 }
765 4 return \%totals;
766 }
767  
768 =item calc_state_frequencies()
769  
770 Calculates the frequencies of the states observed in the matrix.
771  
772 Type : Calculation
773 Title : calc_state_frequencies
774 Usage : my %freq = %{ $object->calc_state_frequencies() };
775 Function: Calculates state frequencies
776 Returns : A hash, keys are state symbols, values are frequencies
777 Args : Optional:
778 # if true, counts missing (usually the '?' symbol) as a state
779 # in the final tallies. Otherwise, missing states are ignored
780 -missing => 1
781 # if true, counts gaps (usually the '-' symbol) as a state
782 # in the final tallies. Otherwise, gap states are ignored
783 -gap => 1
784 Comments: Throws exception if matrix holds continuous values
785  
786 =cut
787  
788 2 ▶ sub calc_state_frequencies {
Subroutine calc_state_frequencies: called CC=6 SCAR=17.9
Pod documented
789 2 my $self = shift;
790 2 my %result;
791 2 for my $row ( @{ $self->get_entities } ) {
792 5 my $freqs = $row->calc_state_frequencies(@_);
793 5 for my $state ( keys %{$freqs} ) {
794 20 $result{$state} += $freqs->{$state};
795 }
796 }
797 2 my $total = 0;
798 2 $total += $_ for values %result;
799 2 ▶ if ( $total > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total > 0) 2 0 2
800 2 for my $state ( keys %result ) {
801 8 $result{$state} /= $total;
802 }
803 }
804 2 return \%result;
805 }
806  
807 =item calc_distinct_site_patterns()
808  
809 Identifies the distinct distributions of states for all characters and
810 counts their occurrences. Returns an array-of-arrays, where the first cell
811 of each inner array holds the occurrence count, the second cell holds the
812 pattern, i.e. an array of states. For example, for a matrix like this:
813  
814 taxon1 GTGTGTGTGTGTGTGTGTGTGTG
815 taxon2 AGAGAGAGAGAGAGAGAGAGAGA
816 taxon3 TCTCTCTCTCTCTCTCTCTCTCT
817 taxon4 TCTCTCTCTCTCTCTCTCTCTCT
818 taxon5 AAAAAAAAAAAAAAAAAAAAAAA
819 taxon6 CGCGCGCGCGCGCGCGCGCGCGC
820 taxon7 AAAAAAAAAAAAAAAAAAAAAAA
821  
822 The following data structure will be returned:
823  
824 [
825 [ 12, [ 'G', 'A', 'T', 'T', 'A', 'C', 'A' ] ],
826 [ 11, [ 'T', 'G', 'C', 'C', 'A', 'G', 'A' ] ]
827 ]
828  
829 The patterns are sorted from most to least frequently occurring, the states
830 for each pattern are in the order of the rows in the matrix. (In other words,
831 the original matrix can more or less be reconstructed by inverting the patterns,
832 and multiplying them by their occurrence, although the order of the columns
833 will be lost.)
834  
835 Type : Calculation
836 Title : calc_distinct_site_patterns
837 Usage : my $patterns = $object->calc_distinct_site_patterns;
838 Function: Calculates distinct site patterns.
839 Returns : A multidimensional array, see above.
840 Args : NONE
841 Comments:
842  
843 =cut
844  
845 1 ▶ sub calc_distinct_site_patterns {
Subroutine calc_distinct_site_patterns: called CC=6 SCAR=17.9
Pod documented
846 1 my ( $self, $indices ) = @_;
847 1 my $raw = $self->get_raw;
848 1 my $nchar = $self->get_nchar;
849 1 my $ntax = $self->get_ntax;
850 1 my ( %pattern, %index );
851 1 for my $i ( 1 .. $nchar ) {
852 23 my @column;
853 23 for my $j ( 0 .. ( $ntax - 1 ) ) {
854 161 push @column, $raw->[$j]->[$i];
855 }
856 23 my $col_pattern = join ' ', @column;
857 23 $pattern{$col_pattern}++;
858 23 ▶ $index{$col_pattern} = [] if not $index{$col_pattern};
Branch: 1 branch
BranchTrueFalseTotal
unless $index{$col_pattern} 2 21 23
859 23 push @{ $index{$col_pattern} }, $i - 1;
860 }
861 1 my @pattern_array;
862 1 for my $key ( keys %pattern ) {
863 2 my @column = split / /, $key;
864 2 ▶ if ( $indices ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indices) { } 0 2 2
865 0 push @pattern_array, [ $pattern{$key}, \@column, $index{$key} ];
866 }
867 else {
868 2 push @pattern_array, [ $pattern{$key}, \@column ];
869 }
870 }
871 1 return [ sort { $b->[0] <=> $a->[0] } @pattern_array ];
872 }
873  
874 =item calc_gc_content()
875  
876 Calculates the G+C content as a fraction on the total
877  
878 Type : Calculation
879 Title : calc_gc_content
880 Usage : my $fraction = $obj->calc_gc_content;
881 Function: Calculates G+C content
882 Returns : A number between 0 and 1 (inclusive)
883 Args : Optional:
884 # if true, counts missing (usually the '?' symbol) as a state
885 # in the final tallies. Otherwise, missing states are ignored
886 -missing => 1
887 # if true, counts gaps (usually the '-' symbol) as a state
888 # in the final tallies. Otherwise, gap states are ignored
889 -gap => 1
890 Comments: Throws 'BadArgs' exception if matrix holds anything other than DNA
891 or RNA. The calculation also takes the IUPAC symbol S (which is C|G)
892 into account, but no other symbols (such as V, for A|C|G);
893  
894 =cut
895  
896 1 ▶ sub calc_gc_content {
Subroutine calc_gc_content: called CC=4 SCAR=14.0
Pod documented
897 1 my $self = shift;
898 1 my $type = $self->get_type;
899 1 ▶ if ( $type !~ /^(?:d|r)na/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /^(?:d|r)na/i) 0 1 1
900 0 throw 'BadArgs' => "Matrix doesn't contain nucleotides";
901 }
902 1 my $freq = $self->calc_state_frequencies;
903 1 my $total = 0;
904 1 for (qw(c C g G s S)) {
905 6 ▶ $total += $freq->{$_} if exists $freq->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $freq->{$_} 2 4 6
906 }
907 1 return $total;
908 }
909  
910 =item calc_median_sequence()
911  
912 Calculates the median character sequence of the matrix
913  
914 Type : Calculation
915 Title : calc_median_sequence
916 Usage : my $seq = $obj->calc_median_sequence;
917 Function: Calculates median sequence
918 Returns : Array in list context, string in scalar context
919 Args : Optional:
920 -ambig => if true, uses ambiguity codes to summarize equally frequent
921 states for a given character. Otherwise picks a random one.
922 -missing => if true, keeps the missing symbol (probably '?') if this
923 is the most frequent for a given character. Otherwise strips it.
924 -gaps => if true, keeps the gap symbol (probably '-') if this is the most
925 frequent for a given character. Otherwise strips it.
926 Comments: The intent of this method is to provide a crude approximation of the most
927 commonly occurring sequences in an alignment, for example as a starting
928 sequence for a sequence simulator. This gives you something to work with if
929 ancestral sequence calculation is too computationally intensive and/or not
930 really necessary.
931  
932 =cut
933  
934 0 ▶ sub calc_median_sequence {
Subroutine calc_median_sequence: not called CC=11 SCAR=48.8
Pod documented
935 0 my ( $self, %args ) = @_;
936 0 my $to = $self->get_type_object;
937 0 my $type = $self->get_type;
938 0 ▶ if ( $type =~ /continuous/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /continuous/i) { } 0 0 0
939 0 throw 'BadArgs' => 'No median sequence calculation for continuous data (yet)';
940 }
941 else {
942 0 my $raw = $self->get_raw;
943 0 my $ntax = $self->get_ntax;
944 0 my $nchar = $self->get_nchar;
945 0 my $gap = $self->get_gap;
946 0 my $miss = $self->get_missing;
947 0 my @seq;
948 0 for my $i ( 1 .. $nchar ) {
949 0 my %seen;
950 0 for my $row ( @$raw ) {
951 0 my $c = uc $row->[$i];
952 0 $seen{$c}++;
953 }
954 0 my @sorted = sort { $seen{$b} <=> $seen{$a} } keys %seen;
955 0 my $max = $seen{$sorted[0]};
956 0 my @top;
957 0 my $j = 0;
958 0 ▶ while ( $sorted[$j] and $seen{$sorted[$j]} == $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sorted[$j] and $seen{$sorted[$j]} == $max) 0 0 0
Condition0%0/3
$sorted[$j] and $seen{$sorted[$j]} == $max
!ll&&!rl&&r
000
MC/DC0%0/2
$sorted[$j] and $seen{$sorted[$j]} == $max
$sorted[$j] $seen{$sorted[$j]} == $max
Truth table0%0/3
A and B
A= $sorted[$j]B= $seen{$sorted[$j]} == $max
ABresult
0X0
100
111
959 0 push @top, $sorted[$j];
960 0 $j++;
961 }
962 0 ▶ if ( @top == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@top == 1) { } 0 0 0
963 0 push @seq, @top;
964 }
965 else {
966 0 ▶ if ( $args{'-ambig'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'-ambig'}) { } 0 0 0
967 0 push @seq, $to->get_symbol_for_states(@top);
968 }
969 else {
970 0 push @seq, shift @top;
971 }
972 }
973 }
974 0 ▶ if ( not $args{'-gaps'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"-gaps"}) 0 0 0
975 0 @seq = grep { $_ ne $gap } @seq;
976 }
977 0 ▶ if ( not $args{'-missing'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"-missing"}) 0 0 0
978 0 @seq = grep { $_ ne $miss } @seq;
979 }
980 0 ▶ return wantarray ? @seq : join '', @seq;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
981 }
982 }
983  
984 =back
985  
986 =head2 METHODS
987  
988 =over
989  
990 =item keep_chars()
991  
992 Creates a cloned matrix that only keeps the characters at
993 the supplied (zero-based) indices.
994  
995 Type : Utility method
996 Title : keep_chars
997 Usage : my $clone = $object->keep_chars([6,3,4,1]);
998 Function: Creates spliced clone.
999 Returns : A spliced clone of the invocant.
1000 Args : Required, an array ref of integers
1001 Comments: The columns are retained in the order in
1002 which they were supplied.
1003  
1004 =cut
1005  
1006 4 ▶ sub keep_chars {
Subroutine keep_chars: called CC=3 SCAR=11.2
Pod documented
1007 4 my ( $self, $indices_array_ref ) = @_;
1008 4 my @indices = @{$indices_array_ref};
1009 4 my $clone = $self->clone;
1010 4 for my $seq ( @{ $clone->get_entities } ) {
1011 16 $seq->keep_entities( \@indices );
1012 16 my @anno = @{ $seq->get_annotations };
1013 16 ▶ if (@anno) {
Branch: 1 branch
BranchTrueFalseTotal
if (@anno) 0 16 16
1014 0 my @re_anno = @anno[@indices];
1015 0 $seq->set_annotations(@re_anno);
1016 }
1017 }
1018 4 $clone->get_characters->keep_entities(\@indices);
1019 4 return $clone;
1020 }
1021  
1022 =item prune_chars()
1023  
1024 Creates a cloned matrix that omits the characters at
1025 the supplied (zero-based) indices.
1026  
1027 Type : Utility method
1028 Title : prune_chars
1029 Usage : my $clone = $object->prune_chars([6,3,4,1]);
1030 Function: Creates spliced clone.
1031 Returns : A spliced clone of the invocant.
1032 Args : Required, an array ref of integers
1033 Comments: The columns are retained in the order in
1034 which they were supplied.
1035  
1036 =cut
1037  
1038 3 ▶ sub prune_chars {
Subroutine prune_chars: called CC=3 SCAR=11.0
Pod documented
1039 3 my ( $self, $indices ) = @_;
1040 3 my $nchar = $self->get_nchar;
1041 3 my %indices = map { $_ => 1 } @{$indices};
1042 3 my @keep;
1043 3 for my $i ( 0 .. ( $nchar - 1 ) ) {
1044 13 ▶ push @keep, $i if not exists $indices{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $indices{$i} 6 7 13
1045 }
1046 3 return $self->keep_chars( \@keep );
1047 }
1048  
1049 =item prune_invariant()
1050  
1051 Creates a cloned matrix that omits the characters for which all taxa
1052 have the same state (or missing);
1053  
1054 Type : Utility method
1055 Title : prune_invariant
1056 Usage : my $clone = $object->prune_invariant;
1057 Function: Creates spliced clone.
1058 Returns : A spliced clone of the invocant.
1059 Args : None
1060 Comments: The columns are retained in the order in
1061 which they were supplied.
1062  
1063 =cut
1064  
1065 1 ▶ sub prune_invariant {
Subroutine prune_invariant: called CC=5 SCAR=16.1
Pod documented
1066 1 my $self = shift;
1067 1 my $nchar = $self->get_nchar;
1068 1 my $missing = $self->get_missing;
1069 1 my @indices;
1070 1 for my $i ( 0 .. ( $nchar - 1 ) ) {
1071 5 my %seen;
1072 5 ROW: for my $row ( @{ $self->get_entities } ) {
1073 25 my $state = $row->get_by_index($i);
1074 25 ▶ next ROW if $state eq $missing;
Branch: 1 branch
BranchTrueFalseTotal
if $state eq $missing 11 14 25
1075 14 $seen{ $state } = 1;
1076 }
1077 5 my @states = keys %seen;
1078 5 ▶ push @indices, $i if scalar(@states) <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @states <= 1 2 3 5
1079 }
1080 1 return $self->prune_chars(\@indices);
1081 }
1082  
1083 =item prune_uninformative()
1084  
1085 Creates a cloned matrix that omits all uninformative characters. Uninformative
1086 are considered characters where all non-missing values are either invariant
1087 or autapomorphies.
1088  
1089 Type : Utility method
1090 Title : prune_uninformative
1091 Usage : my $clone = $object->prune_uninformative;
1092 Function: Creates spliced clone.
1093 Returns : A spliced clone of the invocant.
1094 Args : None
1095 Comments: The columns are retained in the order in
1096 which they were supplied.
1097  
1098 =cut
1099  
1100 1 ▶ sub prune_uninformative {
Subroutine prune_uninformative: called CC=11 SCAR=24.0
Pod documented
1101 1 my $self = shift;
1102 1 my $nchar = $self->get_nchar;
1103 1 my $ntax = $self->get_ntax;
1104 1 my $missing = $self->get_missing;
1105 1 my @indices;
1106 1 for my $i ( 0 .. ( $nchar - 1 ) ) {
1107 5 my %seen;
1108 5 for my $row ( @{ $self->get_entities } ) {
1109 25 my $state = $row->get_by_index($i);
1110 25 $seen{ $state }++;
1111 }
1112 5 my @states = keys %seen;
1113 5 ▶ push @indices, $i if scalar(@states) <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @states <= 1 1 4 5
1114 5 ▶ if ( scalar(@states) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @states > 1) 4 1 5
1115 4 my $seen_informative;
1116 4 ▶ my $non_missing = $ntax - ( $seen{$missing} || 0 );
Condition100%2/2
$seen{$missing} || 0
l!l
31
MC/DC100%1/1
$seen{$missing} || 0
$seen{$missing}
Truth table100%2/2
A
A= $seen{$missing}
Aresult
11
00
1117 4 my @informative_maybe;
1118 4 for my $state ( @states ) {
1119 10 ▶ if ( $seen{$state} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$state} == 1) { } 4 6 10
1120 4 $non_missing--;
1121 }
1122 else {
1123 6 push @informative_maybe, $state;
1124 }
1125 }
1126 4 for my $state ( @informative_maybe ) {
1127 6 ▶ $seen_informative++ if $seen{$state} < $non_missing;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$state} < $non_missing 4 2 6
1128 }
1129 4 ▶ push @indices, $i unless $seen_informative;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_informative 2 2 4
1130 }
1131 }
1132 1 return $self->prune_chars(\@indices);
1133 }
1134  
1135 =item prune_missing_and_gaps()
1136  
1137 Creates a cloned matrix that omits all characters for which the invocant only
1138 has missing and/or gap states.
1139  
1140 Type : Utility method
1141 Title : prune_missing_and_gaps
1142 Usage : my $clone = $object->prune_missing_and_gaps;
1143 Function: Creates spliced clone.
1144 Returns : A spliced clone of the invocant.
1145 Args : None
1146 Comments: The columns are retained in the order in
1147 which they were supplied.
1148  
1149 =cut
1150  
1151 0 ▶ sub prune_missing_and_gaps {
Subroutine prune_missing_and_gaps: not called CC=2 SCAR=17.9
Pod documented
1152 0 my $self = shift;
1153 0 my $nchar = $self->get_nchar;
1154 0 my $to = $self->get_type_object;
1155 0 my $m = $to->get_missing;
1156 0 my $g = $to->get_gap;
1157 0 my @indices;
1158 0 for my $i ( 0 .. ( $nchar - 1 ) ) {
1159 0 my %col;
1160 0 ▶ $self->visit(sub{
Subroutine __ANON__: not called CC=4 SCAR=30.0
1161 0 my $row = shift;
1162 0 my $state = $row->get_by_index($i);
1163 0 ▶ if ( $state ne $m and $state ne $g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state ne $m and $state ne $g) 0 0 0
Condition0%0/3
$state ne $m and $state ne $g
!ll&&!rl&&r
000
MC/DC0%0/2
$state ne $m and $state ne $g
$state ne $m $state ne $g
Truth table0%0/3
A and B
A= $state ne $mB= $state ne $g
ABresult
0X0
100
111
1164 0 $col{$state} = 1;
1165 }
1166 0 ▶ push @indices, $i if not keys %col;
Branch: 1 branch
BranchTrueFalseTotal
if not keys %col 0 0 0
1167 0 });
1168 }
1169 0 return $self->prune_chars(\@indices);
1170 }
1171  
1172 =item bootstrap()
1173  
1174 Creates bootstrapped clone.
1175  
1176 Type : Utility method
1177 Title : bootstrap
1178 Usage : my $bootstrap = $object->bootstrap;
1179 Function: Creates bootstrapped clone.
1180 Returns : A bootstrapped clone of the invocant.
1181 Args : Optional, a subroutine reference that returns a random
1182 integer between 0 (inclusive) and the argument provided
1183 to it (exclusive). The default implementation is to use
1184 sub { int( rand( shift ) ) }, a user might override this
1185 by providing an implementation with a better random number
1186 generator.
1187 Comments: The bootstrapping algorithm uses perl's random number
1188 generator to create a new series of indices (without
1189 replacement) of the same length as the original matrix.
1190 These indices are first sorted, then applied to the
1191 cloned sequences. Annotations (if present) stay connected
1192 to the resampled cells.
1193  
1194 =cut
1195  
1196 0 ▶ sub bootstrap {
Subroutine bootstrap: not called CC=3 SCAR=24.8
1197 0 my $self = shift;
1198 0 ▶ my $gen = shift || sub { int( rand(shift) ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
shift() || sub { int rand shift(); }
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || sub { int rand shift(); }
shift() sub { int rand shift(); }
Truth table0%0/3
A || B
A= shift()B= sub { int rand shift(); }
ABresult
1X1
011
000
1199 0 my $nchar = $self->get_nchar;
1200 0 my @indices;
1201 0 push @indices, $gen->($nchar) for ( 1 .. $nchar );
1202 0 @indices = sort { $a <=> $b } @indices;
1203 0 return $self->keep_chars( \@indices );
1204 }
1205  
1206 =item jackknife()
1207  
1208 Creates jackknifed clone.
1209  
1210 Type : Utility method
1211 Title : jackknife
1212 Usage : my $bootstrap = $object->jackknife(0.5);
1213 Function: Creates jackknifed clone.
1214 Returns : A jackknifed clone of the invocant.
1215 Args : * Required, a number between 0 and 1, representing the
1216 fraction of characters to jackknife.
1217 * Optional, a subroutine reference that returns a random
1218 integer between 0 (inclusive) and the argument provided
1219 to it (exclusive). The default implementation is to use
1220 sub { int( rand( shift ) ) }, a user might override this
1221 by providing an implementation with a better random number
1222 generator.
1223 Comments: The jackknife algorithm uses perl's random number
1224 generator to create a new series of indices of cells to keep.
1225 These indices are first sorted, then applied to the
1226 cloned sequences. Annotations (if present) stay connected
1227 to the resampled cells.
1228  
1229 =cut
1230  
1231 0 ▶ sub jackknife {
Subroutine jackknife: not called CC=6 SCAR=37.4
Pod documented
1232 0 my ( $self, $prop ) = @_;
1233 0 ▶ if ( not looks_like_number $prop or $prop >= 1 or $prop < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not &looks_like_number($prop) or $prop >= 1 or $prop < 0) 0 0 0
Condition0%0/6
not &looks_like_number($prop) or $prop >= 1 or $prop < 0
l!l&&r!l&&!r
000
not &looks_like_number($prop) or $prop >= 1
l!l&&r!l&&!r
000
MC/DC0%0/3
not &looks_like_number($prop) or $prop >= 1 or $prop < 0
not &looks_like_number($prop) $prop >= 1 $prop < 0
Truth table0%0/4
not(A) or B or C
A= not &looks_like_number($prop)B= $prop >= 1C= $prop < 0
ABCresult
1XX1
01X1
0011
0000
1234 0 throw 'BadNumber' =>
1235 "Jackknifing proportion must be a number between 0 and 1";
1236 }
1237 0 ▶ my $gen = $_[2] || sub { int( rand(shift) ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$_[2] || sub { int rand shift(); }
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[2] || sub { int rand shift(); }
$_[2] sub { int rand shift(); }
Truth table0%0/3
A || B
A= $_[2]B= sub { int rand shift(); }
ABresult
1X1
011
000
1238 0 my $nchar = $self->get_nchar;
1239 0 my ( %indices, @indices );
1240 0 ▶ while ( scalar keys %indices < ( $nchar - int( $nchar * $prop ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %indices < $nchar - int($nchar * $prop)) 0 0 0
1241 0 $indices{ $gen->($nchar) } = 1;
1242 }
1243 0 @indices = sort { $a <=> $b } keys %indices;
1244 0 return $self->keep_chars( \@indices );
1245 }
1246  
1247 =item replicate()
1248  
1249 Creates simulated replicate.
1250  
1251 Type : Utility method
1252 Title : replicate
1253 Usage : my $replicate = $matrix->replicate($tree);
1254 Function: Creates simulated replicate.
1255 Returns : A simulated replicate of the invocant.
1256 Args : Tree to simulate the characters on.
1257 Optional:
1258 -seed => a random integer seed
1259 -model => an object of class Bio::Phylo::Models::Substitution::Dna or
1260 Bio::Phylo::Models::Substitution::Binary
1261 -random_rootseq => start DNA sequence simulation from random ancestral sequence
1262 instead of the median sequence in the alignment.
1263  
1264 Comments: Requires Statistics::R, with 'ape', 'phylosim', 'phangorn' and 'phytools'.
1265 If model is not given as argument, it will be estimated.
1266  
1267 =cut
1268  
1269 0 ▶ sub replicate {
Subroutine replicate: not called CC=4 SCAR=30.0
Pod documented
1270 0 my ($self,%args) = @_;
1271  
1272 0 my $tree = $args{'-tree'};
1273 0 ▶ if ( not looks_like_object $tree, _TREE_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_object($tree, 3)) 0 0 0
1274 0 throw 'BadArgs' => "Need tree as argument";
1275 }
1276  
1277 0 my $type = $self->get_type;
1278 0 ▶ if ( $type =~ /dna/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /dna/i) { } 0 0 0
elsif ($type =~ /standard/i) { } 0 0 0
1279 return $self->_replicate_dna(
1280 '-tree' => $tree,
1281 '-model' => $args{'-model'},
1282 '-seed' => $args{'-seed'},
1283 0 '-random_rootseq' => $args{'-random_rootseq'}
1284 );
1285 }
1286 elsif ( $type =~ /standard/i ) {
1287 return $self->_replicate_binary(
1288 '-tree' => $tree,
1289 '-model' => $args{'-model'},
1290 0 '-seed' => $args{'-seed'}
1291 );
1292 }
1293 else {
1294 0 throw 'BadArgs' => "Can't replicate $type matrices (yet?)";
1295 }
1296 }
1297
1298 0 ▶ sub _replicate_dna {
Subroutine _replicate_dna: not called CC=20 SCAR=60.4
1299 0 my ($self,%args) = @_;
1300
1301 0 my $seed = $args{'-seed'};
1302 0 my $tree = $args{'-tree'};
1303 0 my $model = $args{'-model'};
1304 0 my $random_rootseq = $args{'-random_rootseq'};
1305
1306 0 ▶ if ( scalar @{ $self->get_entities } < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->get_entities;} < 3) 0 0 0
1307 0 $logger->warn("Cannot replicate a matrix with less than three elements.");
1308 0 return;
1309 }
1310  
1311 # we will need 'ape', 'phylosim' (and 'phangorn' for model testing)
1312 0 ▶ if ( looks_like_class 'Statistics::R' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_class("Statistics::R")) 0 0 0
1313  
1314 # instantiate R
1315 0 my $R = Statistics::R->new;
1316 0 ▶ $R->run(qq[set.seed($seed)]) if $seed;
Branch: 1 branch
BranchTrueFalseTotal
if $seed 0 0 0
1317 0 $R->run(q[options(device=NULL)]);
1318 0 $R->run(q[require('ape')]);
1319 0 $R->run(q[phylosim <- require('phylosim')]);
1320 0 $R->run(q[PSIM_FAST<-TRUE]);
1321 # check if phylosim (and therefore ape) is installed
1322 0 ▶ if ( $R->get(q[phylosim]) eq 'FALSE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($R->get("phylosim") eq "FALSE") 0 0 0
1323 0 $logger->warn('R package phylosim must be installed to replicate alignment.');
1324 0 return;
1325 }
1326  
1327 # pass in the tree, scale it so that its length sums to 1.
1328 # in combination with a gamma function and/or invariant sites
1329 # this should give us sequences that are reasonably realistic:
1330 # not overly divergent.
1331 0 my $newick = $tree->to_newick;
1332 0 $R->run(qq[phylo <- read.tree(text="$newick")]);
1333 0 $R->run(q[tree <- PhyloSim(phylo)]);
1334 0 $R->run(q[scaleTree(tree,1/tree$treeLength)]);
1335 0 $R->run(q[t <- tree$phylo]);
1336  
1337 # run the model test if model not given as argument
1338 0 ▶ if ( ! $model ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($model) 0 0 0
1339 0 $logger->info("no model given as argument, determining model with phangorn's modelTest");
1340 0 $model = 'Bio::Phylo::Models::Substitution::Dna'->modeltest(
1341 '-matrix' => $self,
1342 '-tree' => $tree
1343 );
1344 }
1345 # prepare data for processes
1346 0 my @ungapped = @{ $self->get_ungapped_columns };
1347 0 my @invariant = @{ $self->get_invariant_columns };
1348 0 my %deletions = %{ $self->calc_indel_sizes( '-trim' => 0 ) };
1349 0 my %insertions = %{ $self->calc_indel_sizes( '-trim' => 0, '-insertions' => 1 ) };
1350  
1351 # set ancestral (root) sequence
1352 0 my $ancestral;
1353 0 my @alphabet = ('A', 'T', 'C', 'G');
1354 0 ▶ if ( $random_rootseq ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($random_rootseq) { } 0 0 0
1355 # start from random sequence if requested
1356 0 $logger->debug('simulating from random ancestral sequence');
1357 0 $ancestral .= $alphabet[ rand @alphabet ] for 1..$self->length;
1358 }
1359 else {
1360 0 $logger->debug('simulating from median ancestral sequence');
1361 # start from median sequence
1362 0 $ancestral = $self->calc_median_sequence;
1363 # phylosim does not accept ambiguity characters, replace with random nucleotides
1364 0 $ancestral =~ s/[^ATCG]/$alphabet[rand @alphabet]/g;
1365 }
1366 0 $logger->debug("ancestral sequence for simulation : $ancestral");
1367 0 $R->run(qq[root.seq=NucleotideSequence(string='$ancestral')]);
1368  
1369 0 my $m = ref($model);
1370 0 ▶ if ( $m=~/([^\:\:]+$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m =~ /([^\:\:]+$)/) 0 0 0
1371 0 $m = $1;
1372 }
1373 # mapping between model names (can differ between Bio::Phylo and phylosim)
1374 0 my %models = ('JC69'=>'JC69', 'GTR'=>'GTR', 'F81'=>'F81', 'HKY85'=>'HKY', 'K80'=>'K80');
1375 0 ▶ my $type = $models{$m} || 'GTR';
Condition0%0/2
$models{$m} || 'GTR'
l!l
00
MC/DC0%0/1
$models{$m} || 'GTR'
$models{$m}
Truth table0%0/2
A
A= $models{$m}
Aresult
11
00
1376  
1377 # collect model specific parameters in string passed to R
1378 0 my $model_params = '';
1379 0 ▶ if ( $type =~ /(?:F81|GTR|K80|HKY)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /(?:F81|GTR|K80|HKY)/) 0 0 0
1380 0 $logger->debug("setting base frequencies for substitution model");
1381 0 $model_params .= 'base.freqs=c(' . join(',', @{$model->get_pi}) . ')';
1382 }
1383 0 ▶ if ( $type =~ /GTR/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /GTR/) 0 0 0
1384 0 $logger->debug("setting rate params for GTR model");
1385 # (watch out for different column order in phangorn's and phylosim's Q matrix!)
1386 0 my $a = $model->get_rate('C', 'T');
1387 0 my $b = $model->get_rate('A', 'T');
1388 0 my $c = $model->get_rate('G', 'T');
1389 0 my $d = $model->get_rate('A', 'C');
1390 0 my $e = $model->get_rate('G', 'C');
1391 0 my $f = $model->get_rate('G', 'A');
1392 0 $model_params .= ", rate.params=list(a=$a, b=$b, c=$c, d=$d, e=$e, f=$f)";
1393 }
1394 0 ▶ if ( $type =~ /(?:K80|HKY)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /(?:K80|HKY)/) 0 0 0
1395 0 $logger->debug("setting kappa parameter for substitution model");
1396 0 my $kappa = $model->get_kappa;
1397 # get transition and transversion rates from kappa,
1398 # scale with number of nucleotides to obtain similar Q matrices
1399 0 my $alpha = $kappa * 4;
1400 0 my $beta = 4;
1401 0 $model_params .= ", rate.params=list(Alpha=$alpha, Beta=$beta)";
1402 }
1403 # create model for phylosim
1404 0 $R->run(qq[model <- $type($model_params)]);
1405 # set parameters for indels
1406 0 ▶ if ( keys %deletions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %deletions) 0 0 0
1407 # deletions
1408 0 my @del_sizes = keys %deletions;
1409 0 my $del_total = 0;
1410 0 $del_total += $_ for values (%deletions);
1411 0 my @del_probs = map {$_/$del_total} values %deletions;
1412 0 my $size_str = 'c(' . join(',', @del_sizes) . ')';
1413 0 my $prob_str = 'c(' . join(',', @del_probs) . ')';
1414 0 my $rate = scalar(keys %deletions) / $self->get_nchar;
1415 0 $logger->debug("Setting deletion rate to $rate");
1416 0 $R->run(qq[attachProcess(root.seq, DiscreteDeletor(rate=$rate, sizes=$size_str, probs=$prob_str))]);
1417 }
1418 0 ▶ if ( keys %insertions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %insertions) 0 0 0
1419 # insertions
1420 0 my @ins_sizes = keys %insertions;
1421 0 my $ins_total = 0;
1422 0 $ins_total += $_ for values (%insertions);
1423 0 my @ins_probs = map {$_/$ins_total} values %insertions;
1424 0 my $size_str = 'c(' . join(',', @ins_sizes) . ')';
1425 0 my $prob_str = 'c(' . join(',', @ins_probs) . ')';
1426 0 my $maxsize = (sort { $b <=> $a } keys(%insertions))[0];
1427 0 my $rate = scalar(keys %insertions) / $self->get_nchar;
1428 0 $logger->debug("Setting insertion rate to $rate");
1429 0 $R->run(qq[i <- DiscreteInsertor(rate=$rate, sizes=$size_str, probs=$prob_str)]);
1430 0 $R->run(qq[template <- NucleotideSequence(length=$maxsize,processes=list(list(model)))]);
1431 0 $R->run(q[i$templateSeq <- template]);
1432 0 $R->run(qq[attachProcess(root.seq, i)]);
1433 }
1434  
1435 # specify model that evolves root sequence
1436 0 $R->run(q[attachProcess(root.seq, model)]);
1437  
1438 # set invariant sites
1439 0 ▶ if ( scalar @invariant ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @invariant) 0 0 0
1440 0 ▶ my $pinvar = $model->get_pinvar || scalar(@invariant)/$self->get_nchar;
Condition0%0/3
$model->get_pinvar || scalar(@invariant) / $self->get_nchar
l!l&&r!l&&!r
000
MC/DC0%0/2
$model->get_pinvar || scalar(@invariant) / $self->get_nchar
$model->get_pinvar scalar(@invariant) / $self->get_nchar
Truth table0%0/3
A || B
A= $model->get_pinvarB= scalar(@invariant) / $self->get_nchar
ABresult
1X1
011
000
1441 0 ▶ if ( $pinvar == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($pinvar == 1) 0 0 0
1442 0 my $epsilon = 0.01;
1443 0 $pinvar -= $epsilon;
1444 }
1445 # set a high value for gamma, then we approximate the empirical number of invariant sites
1446 0 $R->run(qq[plusInvGamma(root.seq,model,pinv=$pinvar,shape=1e10)]);
1447 }
1448  
1449 # run the simulation
1450 0 $R->run(q[sim <- PhyloSim(root.seq=root.seq, phylo=t)]);
1451 0 $R->run(q[Simulate(sim)]);
1452  
1453 # get alignment as Fasta string
1454 0 my $aln = $R->get(q[paste('>', t$tip.label, '\n', apply(sim$alignment, 1, paste, collapse='')[t$tip.label], '\n', collapse='', sep='')]);
1455 0 $aln =~ s/\\n/\n/g;
1456  
1457 # create matrix
1458 0 my $project = parse(
1459 '-string' => $aln,
1460 '-format' => 'fasta',
1461 '-type' => 'dna',
1462 '-as_project' => 1,
1463 );
1464  
1465 0 my ($matrix) = @{ $project->get_items(_MATRIX_) };
1466  
1467 0 return $matrix;
1468 }
1469 }
1470  
1471 0 ▶ sub _replicate_binary {
Subroutine _replicate_binary: not called CC=11 SCAR=48.8
1472 0 my ($self,%args) = @_;
1473  
1474 0 my $seed = $args{'-seed'};
1475 0 my $tree = $args{'-tree'};
1476  
1477 # we will need both 'ape' and 'phytools'
1478 0 ▶ if ( looks_like_class 'Statistics::R' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_class("Statistics::R")) 0 0 0
1479 0 my $pattern = $self->calc_distinct_site_patterns('with_indices_of_patterns');
1480 0 my $characters = $self->get_characters;
1481 0 $logger->info("matrix has ".scalar(@$pattern)." distinct patterns");
1482  
1483 # instantiate R
1484 0 my $newick = $tree->to_newick;
1485 0 my $R = Statistics::R->new;
1486 0 ▶ $R->run(qq[set.seed($seed)]) if $seed;
Branch: 1 branch
BranchTrueFalseTotal
if $seed 0 0 0
1487 0 $R->run(q[library("ape")]);
1488 0 $R->run(q[library("phytools")]);
1489 0 $R->run(qq[phylo <- read.tree(text="$newick")]);
1490  
1491 # start the matrix
1492 0 my @matrix;
1493 0 push @matrix, [] for 1 .. $self->get_ntax;
1494  
1495 # iterate over distinct site patterns
1496 0 for my $p ( @$pattern ) {
1497 0 my $i = $p->[2]->[0];
1498 0 my %pat = map { $_ => 1 } @{ $p->[1] };
1499 0 my $states;
1500  
1501 # if this column is completely invariant, the model
1502 # estimator is unhappy, so we just copy it over
1503 0 ▶ if ( keys(%pat) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %pat == 1) { } 0 0 0
1504 0 $logger->info("column is invariant, copying verbatim");
1505 0 $states = $p->[1];
1506 }
1507 else {
1508 0 my $model = $args{'-model'};
1509 0 ▶ if ( ! $model ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($model) 0 0 0
1510 0 $logger->info("going to test model for column(s) $i..*");
1511 0 $model = Bio::Phylo::Models::Substitution::Binary->modeltest(
1512 '-tree' => $tree,
1513 '-matrix' => $self,
1514 '-char' => $characters->get_by_index($i),
1515 );
1516 }
1517 # pass model to R
1518 0 my ( $fw, $rev ) = ( $model->get_forward, $model->get_reverse );
1519  
1520 # add epsilon if one of the rates is zero, otherwise sim.history chokes
1521 0 ▶ $fw = 1e-6 if $fw == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $fw == 0 0 0 0
1522 0 ▶ $rev = 1e-6 if $rev == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rev == 0 0 0 0
1523 0 $logger->info("0 -> 1 ($fw), 1 -> 0 ($rev)");
1524  
1525 0 my $rates = [ 0, $fw, $rev, 0 ];
1526 0 $R->set( 'rates' => $rates );
1527 0 $R->run(qq[Q<-matrix(rates,2,2,byrow=TRUE)]);
1528 0 $R->run(qq[rownames(Q)<-colnames(Q)<-c("0","1")]);
1529 0 $R->run(qq[diag(Q)<--rowSums(Q)]);
1530  
1531 # simulate character on tree, get states
1532 0 $R->run(qq[tt<-sim.history(phylo,Q,message=FALSE)]);
1533 0 $R->run(qq[states<-as.double(getStates(tt,"tips"))]);
1534 0 $states = $R->get(q[states]);
1535 }
1536  
1537 # add states to matrix
1538 0 my @indices = @{ $p->[2] };
1539 0 for my $row ( 0 .. $#{ $states } ) {
1540 0 my $value = $states->[$row];
1541 0 for my $col ( @indices ) {
1542 0 $matrix[$row]->[$col] = $value;
1543 }
1544 }
1545 }
1546  
1547 # create matrix
1548 0 my $i = 0;
1549 $tree->visit_depth_first(
1550 0 ▶ '-pre' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1551 0 my $node = shift;
1552 0 ▶ if ( $node->is_terminal ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->is_terminal) 0 0 0
1553 0 unshift @{ $matrix[$i++] }, $node->get_name
1554 }
1555 }
1556 0 );
1557 0 $logger->info(Dumper(\@matrix));
1558 0 return $factory->create_matrix(
1559 '-type' => $self->get_type,
1560 '-raw' => \@matrix,
1561 );
1562 }
1563 }
1564  
1565 =item insert()
1566  
1567 Insert argument in invocant.
1568  
1569 Type : Listable method
1570 Title : insert
1571 Usage : $matrix->insert($datum);
1572 Function: Inserts $datum in $matrix.
1573 Returns : Modified object
1574 Args : A datum object
1575 Comments: This method re-implements the method by the same
1576 name in Bio::Phylo::Listable
1577  
1578 =cut
1579  
1580 120 ▶ sub insert {
Subroutine insert: called CC=10 SCAR=24.7
Pod documented
1581 120 my ( $self, $obj ) = @_;
1582 120 my $obj_container;
1583 120 eval { $obj_container = $obj->_container };
1584 120 ▶ if ( $@ || $obj_container != $self->_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or $obj_container != $self->_type) 1 119 120
Condition66%2/3
$@ or $obj_container != $self->_type
l!l&&r!l&&!r
10119
MC/DC50%1/2
$@ or $obj_container != $self->_type
$@ $obj_container != $self->_type
Truth table66%2/3
A or B
A= $@B= $obj_container != $self->_type
ABresult
1X1
011
000
1585 1 throw 'ObjectMismatch' => 'object not a datum object!';
1586 }
1587 119 $logger->info("inserting '$obj' in '$self'");
1588 119 ▶ if ( !$self->get_type_object->is_same( $obj->get_type_object ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_type_object->is_same($obj->get_type_object)) 0 119 119
1589 0 throw 'ObjectMismatch' => 'object is of wrong data type';
1590 }
1591 119 my $taxon1 = $obj->get_taxon;
1592 119 ▶ my $mname = $self->get_name || $self->get_internal_name;
Condition66%2/3
$self->get_name || $self->get_internal_name
l!l&&r!l&&!r
24950
MC/DC0%0/2
$self->get_name || $self->get_internal_name
$self->get_name $self->get_internal_name
Truth table66%2/3
A || B
A= $self->get_nameB= $self->get_internal_name
ABresult
1X1
011
000
1593 119 for my $ents ( @{ $self->get_entities } ) {
1594 224 ▶ if ( $obj->get_id == $ents->get_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->get_id == $ents->get_id) 0 224 224
1595 0 throw 'ObjectMismatch' => 'row already inserted';
1596 }
1597 224 ▶ if ($taxon1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxon1) 1 223 224
1598 1 my $tname = $taxon1->get_name;
1599 1 my $taxon2 = $ents->get_taxon;
1600 1 ▶ if ( $taxon2 && $taxon1->get_id == $taxon2->get_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxon2 and $taxon1->get_id == $taxon2->get_id) 0 1 1
Condition33%1/3
$taxon2 and $taxon1->get_id == $taxon2->get_id
!ll&&!rl&&r
010
MC/DC0%0/2
$taxon2 and $taxon1->get_id == $taxon2->get_id
$taxon2 $taxon1->get_id == $taxon2->get_id
Truth table33%1/3
A and B
A= $taxon2B= $taxon1->get_id == $taxon2->get_id
ABresult
0X0
100
111
1601 0 my $tmpl = 'Note: a row linking to %s already exists in matrix %s';
1602 0 $logger->info(sprintf $tmpl,$tname,$mname);
1603 }
1604 }
1605 }
1606 119 $self->SUPER::insert($obj);
1607 119 return $self;
1608 }
1609  
1610 =begin comment
1611  
1612 Validates the object's contents.
1613  
1614 Type : Method
1615 Title : validate
1616 Usage : $obj->validate
1617 Function: Validates the object's contents
1618 Returns : True or throws Bio::Phylo::Util::Exceptions::InvalidData
1619 Args : None
1620 Comments: This method implements the interface method by the same
1621 name in Bio::Phylo::Matrices::TypeSafeData
1622  
1623 =end comment
1624  
1625 =cut
1626  
1627 73 ▶ sub _validate {
Subroutine _validate: called CC=1 SCAR=1.2
1628 0 ▶ shift->visit( sub { shift->validate } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
1629 }
1630  
1631 =item compress_lookup()
1632  
1633 Removes unused states from lookup table
1634  
1635 Type : Method
1636 Title : validate
1637 Usage : $obj->compress_lookup
1638 Function: Removes unused states from lookup table
1639 Returns : $self
1640 Args : None
1641  
1642 =cut
1643  
1644 0 ▶ sub compress_lookup {
Subroutine compress_lookup: not called CC=5 SCAR=34.0
Pod documented
1645 0 my $self = shift;
1646 0 my $to = $self->get_type_object;
1647 0 my $lookup = $to->get_lookup;
1648 0 my %seen;
1649 0 for my $row ( @{ $self->get_entities } ) {
1650 0 my @char = $row->get_char;
1651 0 $seen{$_}++ for (@char);
1652 }
1653 0 for my $state ( keys %{$lookup} ) {
1654 0 ▶ if ( not exists $seen{$state} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $seen{$state}) 0 0 0
1655 0 delete $lookup->{$state};
1656 }
1657 }
1658 0 $to->set_lookup($lookup);
1659 0 return $self;
1660 }
1661  
1662 =item check_taxa()
1663  
1664 Validates taxa associations.
1665  
1666 Type : Method
1667 Title : check_taxa
1668 Usage : $obj->check_taxa
1669 Function: Validates relation between matrix and taxa block
1670 Returns : Modified object
1671 Args : None
1672 Comments: This method implements the interface method by the same
1673 name in Bio::Phylo::Taxa::TaxaLinker
1674  
1675 =cut
1676  
1677 48 ▶ sub check_taxa {
Subroutine check_taxa: called CC=7 SCAR=20.1
Pod documented
1678 48 my $self = shift;
1679  
1680 # is linked to taxa
1681 48 ▶ if ( my $taxa = $self->get_taxa ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $taxa = $self->get_taxa) { } 44 4 48
1682 my %taxa =
1683 44 map { $_->get_internal_name => $_ } @{ $taxa->get_entities };
1684 44 ROW_CHECK: for my $row ( @{ $self->get_entities } ) {
1685 116 ▶ if ( my $taxon = $row->get_taxon ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $taxon = $row->get_taxon) 105 11 116
1686 105 ▶ next ROW_CHECK if exists $taxa{ $taxon->get_name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $taxa{$taxon->get_name} 105 0 105
1687 }
1688 11 my $name = $row->get_name;
1689 11 ▶ if ( exists $taxa{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $taxa{$name}) { } 11 0 11
1690 11 $row->set_taxon( $taxa{$name} );
1691 }
1692 else {
1693 0 my $taxon = $factory->create_taxon( -name => $name );
1694 0 $taxa{$name} = $taxon;
1695 0 $taxa->insert($taxon);
1696 0 $row->set_taxon($taxon);
1697 }
1698 }
1699 }
1700  
1701 # not linked
1702 else {
1703 4 for my $row ( @{ $self->get_entities } ) {
1704 16 $row->set_taxon();
1705 }
1706 }
1707 48 return $self;
1708 }
1709  
1710 =item make_taxa()
1711  
1712 Creates a taxa block from the objects contents if none exists yet.
1713  
1714 Type : Method
1715 Title : make_taxa
1716 Usage : my $taxa = $obj->make_taxa
1717 Function: Creates a taxa block from the objects contents if none exists yet.
1718 Returns : $taxa
1719 Args : NONE
1720  
1721 =cut
1722  
1723 1 ▶ sub make_taxa {
Subroutine make_taxa: called CC=5 SCAR=16.4
Pod documented
1724 1 my $self = shift;
1725 1 ▶ if ( my $taxa = $self->get_taxa ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $taxa = $self->get_taxa) { } 0 1 1
1726 0 return $taxa;
1727 }
1728 else {
1729 1 my %taxa;
1730 1 my $taxa = $factory->create_taxa;
1731 1 for my $row ( @{ $self->get_entities } ) {
1732 2 my $name = $row->get_internal_name;
1733 2 ▶ if ( not $taxa{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($taxa{$name}) 2 0 2
1734 2 $taxa{$name} = $factory->create_taxon( '-name' => $name );
1735 }
1736 }
1737 1 ▶ if ( keys %taxa ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %taxa) 1 0 1
1738 1 $taxa->insert( map { $taxa{$_} } sort { $a cmp $b } keys %taxa );
1739 }
1740 1 $self->set_taxa($taxa);
1741 1 return $taxa;
1742 }
1743 }
1744  
1745 =back
1746  
1747 =head2 SERIALIZERS
1748  
1749 =over
1750  
1751 =item to_xml()
1752  
1753 Serializes matrix to nexml format.
1754  
1755 Type : Format convertor
1756 Title : to_xml
1757 Usage : my $data_block = $matrix->to_xml;
1758 Function: Converts matrix object into a nexml element structure.
1759 Returns : Nexml block (SCALAR).
1760 Args : Optional:
1761 -compact => 1 (for compact representation of matrix)
1762  
1763 =cut
1764  
1765 0 ▶ sub to_xml {
Subroutine to_xml: not called CC=4 SCAR=30.0
Pod documented
1766 0 my $self = shift;
1767 0 $logger->debug("writing $self to xml");
1768 0 my ( %args, $ids_for_states );
1769 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
1770 0 %args = @_;
1771 }
1772  
1773 # creating opening tag
1774 0 my $type = $self->get_type;
1775 0 ▶ my $verbosity = $args{'-compact'} ? 'Seqs' : 'Cells';
Branch: 1 branch
BranchTrueFalseTotal
$args{'-compact'} ? : 0 0 0
1776 0 my $xsi_type = 'nex:' . ucfirst($type) . $verbosity;
1777 0 $self->set_attributes( 'xsi:type' => $xsi_type );
1778 0 my $xml = $self->get_xml_tag;
1779 0 $logger->debug("created opening tag $xml");
1780  
1781 # normalizing symbol table
1782 0 my $normalized = $self->_normalize_symbols;
1783 0 $logger->debug("normalized symbols");
1784  
1785 # the format block
1786 0 $xml .= '<format>';
1787 0 $logger->debug($xml);
1788 0 my $to = $self->get_type_object;
1789 0 $ids_for_states = $to->get_ids_for_states(1);
1790  
1791 # write state definitions
1792 # this calls Datatype::to_xml method
1793 #
1794 0 $xml .= $to->to_xml( $normalized, $self->get_polymorphism );
1795 0 $logger->debug($xml);
1796 0 $xml .= $self->get_characters->to_xml;
1797 0 $xml .= "\n</format>";
1798  
1799 # the matrix block
1800 0 $xml .= "\n<matrix>";
1801 my @char_ids =
1802 0 map { $_->get_xml_id } @{ $self->get_characters->get_entities };
1803  
1804 # write rows
1805 0 my $special = $self->get_type_object->get_ids_for_special_symbols(1);
1806 0 for my $row ( @{ $self->get_entities } ) {
1807 0 $xml .= "\n"
1808 . $row->to_xml(
1809 '-states' => $ids_for_states,
1810 '-chars' => \@char_ids,
1811 '-symbols' => $normalized,
1812 '-special' => $special,
1813 %args,
1814 );
1815 }
1816 0 $xml .= "\n</matrix>";
1817 0 $xml .= "\n" . sprintf( '</%s>', $self->get_tag );
1818 0 return $xml;
1819 }
1820  
1821 # what this does:
1822 # numerical states are their own keys; also want numerical
1823 # representations for non-numerical states..."normalization"
1824 # provides this mapping....
1825 0 ▶ sub _normalize_symbols {
Subroutine _normalize_symbols: not called CC=3 SCAR=24.8
1826 0 my $self = shift;
1827 0 $logger->debug("normalizing symbols");
1828 0 ▶ if ( $self->get_type =~ /^standard$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_type =~ /^standard$/i) { } 0 0 0
1829 0 my $to = $self->get_type_object;
1830 0 my $lookup = $self->get_lookup;
1831 0 my @states = keys %{$lookup};
1832 0 ▶ if ( my @letters = sort { $a cmp $b } grep { /[a-z]/i } @states ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@letters) = (sort {$a cmp $b;} grep({/[a-z]/i;} @states))) { } 0 0 0
1833 0 my @numbers = sort { $a <=> $b } grep { /^\d+$/ } @states;
1834 0 my $i = $numbers[-1];
1835 0 my %map = map { $_ => ++$i } @letters;
1836 0 return \%map;
1837 }
1838 else {
1839 0 return {};
1840 }
1841 }
1842 else {
1843 0 return {};
1844 }
1845 }
1846  
1847 =item to_nexus()
1848  
1849 Serializes matrix to nexus format.
1850  
1851 Type : Format convertor
1852 Title : to_nexus
1853 Usage : my $data_block = $matrix->to_nexus;
1854 Function: Converts matrix object into a nexus data block.
1855 Returns : Nexus data block (SCALAR).
1856 Args : The following options are available:
1857  
1858 # if set, writes TITLE & LINK tokens
1859 '-links' => 1
1860  
1861 # if set, writes block as a "data" block (deprecated, but used by mrbayes),
1862 # otherwise writes "characters" block (default)
1863 -data_block => 1
1864  
1865 # if set, writes "RESPECTCASE" token
1866 -respectcase => 1
1867  
1868 # if set, writes "GAPMODE=(NEWSTATE or MISSING)" token
1869 -gapmode => 1
1870  
1871 # if set, writes "MSTAXA=(POLYMORPH or UNCERTAIN)" token
1872 -polymorphism => 1
1873  
1874 # if set, writes character labels
1875 -charlabels => 1
1876  
1877 # if set, writes state labels
1878 -statelabels => 1
1879  
1880 # if set, writes mesquite-style charstatelabels
1881 -charstatelabels => 1
1882  
1883 # by default, names for sequences are derived from $datum->get_name, if
1884 # 'internal' is specified, uses $datum->get_internal_name, if 'taxon'
1885 # uses $datum->get_taxon->get_name, if 'taxon_internal' uses
1886 # $datum->get_taxon->get_internal_name, if $key, uses $datum->get_generic($key)
1887 -seqnames => one of (internal|taxon|taxon_internal|$key)
1888  
1889 =cut
1890  
1891 1 ▶ sub to_nexus {
Subroutine to_nexus: called CC=44 SCAR=64.8
Pod documented
1892 1 my $self = shift;
1893 1 $logger->info("writing to nexus: $self");
1894 1 my %args = @_;
1895 1 my $nchar = $self->get_nchar;
1896 my $string = sprintf "BEGIN %s;\n",
1897 1 ▶ $args{'-data_block'} ? 'DATA' : 'CHARACTERS';
Branch: 1 branch
BranchTrueFalseTotal
$args{'-data_block'} ? : 0 1 1
1898 1 $string .=
1899 "[! Characters block written by "
1900 . ref($self) . " "
1901 . $self->VERSION . " on "
1902 . localtime() . " ]\n";
1903  
1904 # write links
1905 1 ▶ if ( $args{'-links'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-links"}) 0 1 1
1906 0 $string .= sprintf "\tTITLE %s;\n", $self->get_internal_name;
1907 0 ▶ $string .= sprintf "\tLINK TAXA=%s;\n",
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_taxa 0 0 0
1908 $self->get_taxa->get_internal_name
1909 if $self->get_taxa;
1910 }
1911  
1912 # dimensions token line - data block defines NTAX, characters block doesn't
1913 1 ▶ if ( $args{'-data_block'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'-data_block'}) { } 0 1 1
1914 0 $string .= "\tDIMENSIONS NTAX=" . $self->get_ntax() . ' ';
1915 0 $string .= 'NCHAR=' . $nchar . ";\n";
1916 }
1917 else {
1918 1 $string .= "\tDIMENSIONS NCHAR=" . $nchar . ";\n";
1919 }
1920  
1921 # format token line
1922 1 $string .= "\tFORMAT DATATYPE=" . $self->get_type();
1923 $string .= ( $self->get_respectcase ? " RESPECTCASE" : "" )
1924 1 ▶ if $args{'-respectcase'}; # mrbayes no like
Branch: 2 branches
BranchTrueFalseTotal
if $args{"-respectcase"} 0 1 1
$self->get_respectcase ? : 0 0 0
1925 1 ▶ $string .= " MATCHCHAR=" . $self->get_matchchar if $self->get_matchchar;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_matchchar 0 1 1
1926 1 $string .= " MISSING=" . $self->get_missing();
1927 1 ▶ $string .= " GAP=" . $self->get_gap() if $self->get_gap();
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_gap 1 0 1
1928 1 $string .= ";\n";
1929  
1930 # options token line (mrbayes no like)
1931 1 ▶ if ( $args{'-gapmode'} or $args{'-polymorphism'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-gapmode"} or $args{"-polymorphism"}) 0 1 1
Condition33%1/3
$args{"-gapmode"} or $args{"-polymorphism"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$args{"-gapmode"} or $args{"-polymorphism"}
$args{"-gapmode"} $args{"-polymorphism"}
Truth table33%1/3
A or B
A= $args{"-gapmode"}B= $args{"-polymorphism"}
ABresult
1X1
011
000
1932 0 $string .= "\tOPTIONS ";
1933 $string .=
1934 "GAPMODE=" . ( $self->get_gapmode ? "NEWSTATE " : "MISSING " )
1935 0 ▶ if $args{'-gapmode'};
Branch: 2 branches
BranchTrueFalseTotal
if $args{"-gapmode"} 0 0 0
$self->get_gapmode ? : 0 0 0
1936 $string .=
1937 "MSTAXA="
1938 . ( $self->get_polymorphism ? "POLYMORPH " : "UNCERTAIN " )
1939 0 ▶ if $args{'-polymorphism'};
Branch: 2 branches
BranchTrueFalseTotal
if $args{"-polymorphism"} 0 0 0
$self->get_polymorphism ? : 0 0 0
1940 0 $string .= ";\n";
1941 }
1942  
1943 # charlabels token line
1944 1 ▶ if ( $args{'-charlabels'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-charlabels"}) 0 1 1
1945 0 my $charlabels;
1946 0 ▶ if ( my @labels = @{ $self->get_charlabels } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@labels) = @{$self->get_charlabels;}) 0 0 0
1947 0 my $i = 1;
1948 0 for my $label (@labels) {
1949 0 ▶ $charlabels .=
Branch: 1 branch
BranchTrueFalseTotal
$label =~ /(\s|-)/ ? : 0 0 0
1950 $label =~ /(\s|-)/
1951 ? "\n\t\t [$i] '$label'"
1952 : "\n\t\t [$i] $label";
1953 0 $i++;
1954 }
1955 0 $string .= "\tCHARLABELS$charlabels\n\t;\n";
1956 }
1957 }
1958  
1959 # statelabels token line
1960 1 ▶ if ( $args{'-statelabels'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-statelabels"}) 0 1 1
1961 0 my $statelabels;
1962 0 ▶ if ( my @labels = @{ $self->get_statelabels } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@labels) = @{$self->get_statelabels;}) 0 0 0
1963 0 my $i = 1;
1964 0 for my $labelset (@labels) {
1965 0 $statelabels .= "\n\t\t $i";
1966 0 for my $label ( @{$labelset} ) {
1967 0 ▶ $statelabels .=
Branch: 1 branch
BranchTrueFalseTotal
$label =~ /\s/ ? : 0 0 0
1968 $label =~ /\s/
1969 ? "\n\t\t\t'$label'"
1970 : "\n\t\t\t$label";
1971 0 $i++;
1972 }
1973 0 $statelabels .= ',';
1974 }
1975 0 $string .= "\tSTATELABELS$statelabels\n\t;\n";
1976 }
1977 }
1978  
1979 # charstatelabels token line
1980 1 ▶ if ( $args{'-charstatelabels'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-charstatelabels"}) 0 1 1
1981 0 my @charlabels = @{ $self->get_charlabels };
1982 0 my @statelabels = @{ $self->get_statelabels };
1983 0 ▶ if ( @charlabels and @statelabels ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@charlabels and @statelabels) 0 0 0
Condition0%0/3
@charlabels and @statelabels
!ll&&!rl&&r
000
MC/DC0%0/2
@charlabels and @statelabels
@charlabels @statelabels
Truth table0%0/3
A and B
A= @charlabelsB= @statelabels
ABresult
0X0
100
111
1984 0 my $charstatelabels;
1985 0 my $nlabels = $self->get_nchar - 1;
1986 0 for my $i ( 0 .. $nlabels ) {
1987 0 $charstatelabels .= "\n\t\t" . ( $i + 1 );
1988 0 ▶ if ( my $label = $charlabels[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $label = $charlabels[$i]) { } 0 0 0
1989 0 ▶ $charstatelabels .=
Branch: 1 branch
BranchTrueFalseTotal
$label =~ /\s/ ? : 0 0 0
1990 $label =~ /\s/ ? " '$label' /" : " $label /";
1991 }
1992 else {
1993 0 $charstatelabels .= " ' ' /";
1994 }
1995 0 ▶ if ( my $labelset = $statelabels[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $labelset = $statelabels[$i]) { } 0 0 0
1996 0 for my $label ( @{$labelset} ) {
1997 0 ▶ $charstatelabels .=
Branch: 1 branch
BranchTrueFalseTotal
$label =~ /\s/ ? : 0 0 0
1998 $label =~ /\s/ ? " '$label'" : " $label";
1999 }
2000 }
2001 else {
2002 0 $charstatelabels .= " ' '";
2003 }
2004 0 ▶ $charstatelabels .= ',' if $i < $nlabels;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $nlabels 0 0 0
2005 }
2006 0 $string .= "\tCHARSTATELABELS$charstatelabels\n\t;\n";
2007 }
2008 }
2009  
2010 # ...and write matrix!
2011 1 $string .= "\tMATRIX\n";
2012 1 my $length = 0;
2013 1 foreach my $datum ( @{ $self->get_entities } ) {
2014 3 ▶ $length = length( $datum->get_nexus_name )
Branch: 1 branch
BranchTrueFalseTotal
if CORE::length $datum->get_nexus_name > $length 1 2 3
2015 if length( $datum->get_nexus_name ) > $length;
2016 }
2017 1 $length += 4;
2018 1 my $sp = ' ';
2019 1 foreach my $datum ( @{ $self->get_entities } ) {
2020 3 $string .= "\t\t";
2021  
2022 # construct name
2023 3 my $name;
2024 3 ▶ if ( not $args{'-seqnames'} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $args{'-seqnames'}) { } 3 0 3
elsif ($args{'-seqnames'} =~ /^internal$/i) { } 0 0 0
elsif ($args{'-seqnames'} =~ /^taxon/i and $datum->get_taxon) { } 0 0 0
2025 3 $name = $datum->get_nexus_name;
2026 }
2027 elsif ( $args{'-seqnames'} =~ /^internal$/i ) {
2028 0 $name = $datum->get_nexus_name;
2029 }
2030 ▶ elsif ( $args{'-seqnames'} =~ /^taxon/i and $datum->get_taxon ) {
Condition0%0/3
$args{"-seqnames"} =~ /^taxon/i and $datum->get_taxon
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"-seqnames"} =~ /^taxon/i and $datum->get_taxon
$args{"-seqnames"} =~ /^taxon/i $datum->get_taxon
Truth table0%0/3
A and B
A= $args{"-seqnames"} =~ /^taxon/iB= $datum->get_taxon
ABresult
0X0
100
111
2031 0 ▶ if ( $args{'-seqnames'} =~ /^taxon_internal$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'-seqnames'} =~ /^taxon_internal$/i) { } 0 0 0
elsif ($args{'-seqnames'} =~ /^taxon$/i) { } 0 0 0
2032 0 $name = $datum->get_taxon->get_nexus_name;
2033 }
2034 elsif ( $args{'-seqnames'} =~ /^taxon$/i ) {
2035 0 $name = $datum->get_taxon->get_nexus_name;
2036 }
2037 }
2038 else {
2039 0 $name = $datum->get_generic( $args{'-seqnames'} );
2040 }
2041 3 ▶ $name = $datum->get_nexus_name if not $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 3 3
2042 3 $string .= $name . ( $sp x ( $length - length($name) ) );
2043 3 my $char = $datum->get_char;
2044 3 $string .= $char . "\n";
2045 }
2046 1 $string .= "\t;\nEND;\n";
2047 1 return $string;
2048 }
2049  
2050 =item to_dom()
2051  
2052 Analog to to_xml.
2053  
2054 Type : Serializer
2055 Title : to_dom
2056 Usage : $matrix->to_dom
2057 Function: Generates a DOM subtree from the invocant
2058 and its contained objects
2059 Returns : an Element object
2060 Args : Optional:
2061 -compact => 1 : renders characters as sequences,
2062 not individual cells
2063  
2064 =cut
2065  
2066 0 ▶ sub to_dom {
Subroutine to_dom: not called CC=10 SCAR=47.0
Pod documented
2067 0 my $self = shift;
2068 0 my $dom = $_[0];
2069 0 my @args = @_;
2070  
2071 # handle dom factory object...
2072 0 ▶ if ( looks_like_instance( $dom, 'SCALAR' )
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_instance($dom, 'SCALAR') and $dom->_type == 18) { } 0 0 0
Condition0%0/3
&looks_like_instance($dom, "SCALAR") and $dom->_type == 18
!ll&&!rl&&r
000
MC/DC0%0/2
&looks_like_instance($dom, "SCALAR") and $dom->_type == 18
&looks_like_instance($dom, "SCALAR") $dom->_type == 18
Truth table0%0/3
A and B
A= &looks_like_instance($dom, "SCALAR")B= $dom->_type == 18
ABresult
0X0
100
111
2073 && $dom->_type == _DOMCREATOR_ )
2074 {
2075 0 splice( @args, 0, 1 );
2076 }
2077 else {
2078 0 $dom = $Bio::Phylo::NeXML::DOM::DOM;
2079 0 ▶ unless ($dom) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dom) 0 0 0
2080 0 throw 'BadArgs' => 'DOM factory object not provided';
2081 }
2082 }
2083 #### make sure argument handling works here...
2084 0 my ( %args, $ids_for_states );
2085 0 ▶ %args = @args if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 0 0
2086 0 my $type = $self->get_type;
2087 0 ▶ my $verbosity = $args{'-compact'} ? 'Seqs' : 'Cells';
Branch: 1 branch
BranchTrueFalseTotal
$args{'-compact'} ? : 0 0 0
2088 0 my $xsi_type = 'nex:' . ucfirst($type) . $verbosity;
2089 0 $self->set_attributes( 'xsi:type' => $xsi_type );
2090 0 my $elt = $self->get_dom_elt($dom);
2091 0 my $normalized = $self->_normalize_symbols;
2092  
2093 # the format block
2094 0 my $format_elt = $dom->create_element( '-tag' => 'format' );
2095 0 my $to = $self->get_type_object;
2096 0 $ids_for_states = $to->get_ids_for_states(1);
2097  
2098 # write state definitions
2099 0 $format_elt->set_child(
2100 $to->to_dom( $dom, $normalized, $self->get_polymorphism ) );
2101  
2102 # write column definitions
2103 $format_elt->set_child($_)
2104 0 ▶ for $self->_package_char_labels( $dom,
Branch: 1 branch
BranchTrueFalseTotal
%{$ids_for_states;} ? : 0 0 0
2105 0 %{$ids_for_states} ? $to->get_xml_id : undef );
2106 0 $elt->set_child($format_elt);
2107  
2108 # the matrix block
2109 0 my $mx_elt = $dom->create_element( '-tag' => 'matrix' );
2110 0 my @char_ids;
2111 0 for ( 0 .. $self->get_nchar ) {
2112 0 push @char_ids, 'c' . ( $_ + 1 );
2113 }
2114  
2115 # write rows
2116 0 my $special = $self->get_type_object->get_ids_for_special_symbols(1);
2117 0 for my $row ( @{ $self->get_entities } ) {
2118  
2119 # $row->to_dom is calling ...::Datum::to_dom...
2120 0 $mx_elt->set_child(
2121 $row->to_dom(
2122 $dom,
2123 '-states' => $ids_for_states,
2124 '-chars' => \@char_ids,
2125 '-symbols' => $normalized,
2126 '-special' => $special,
2127 %args,
2128 )
2129 );
2130 }
2131 0 $elt->set_child($mx_elt);
2132 0 return $elt;
2133 }
2134  
2135 # returns an array of elements
2136 0 ▶ sub _package_char_labels {
Subroutine _package_char_labels: not called CC=4 SCAR=30.0
2137 0 my ( $self, $dom, $states_id ) = @_;
2138 0 my @elts;
2139 0 my $labels = $self->get_charlabels;
2140 0 for my $i ( 1 .. $self->get_nchar ) {
2141 0 my $char_id = 'c' . $i;
2142 0 my $label = $labels->[ $i - 1 ];
2143 0 my $elt = $dom->create_element( '-tag' => 'char' );
2144 0 $elt->set_attributes( 'id' => $char_id );
2145 0 ▶ $elt->set_attributes( 'label' => $label ) if $label;
Branch: 1 branch
BranchTrueFalseTotal
if $label 0 0 0
2146 0 ▶ $elt->set_attributes( 'states' => $states_id ) if $states_id;
Branch: 1 branch
BranchTrueFalseTotal
if $states_id 0 0 0
2147 0 push @elts, $elt;
2148 }
2149 0 return @elts;
2150 }
2151 10 ▶ sub _tag { 'characters' }
Subroutine _tag: called CC=1 SCAR=0.0
2152 634 ▶ sub _type { $CONSTANT_TYPE }
Subroutine _type: called CC=1 SCAR=0.0
2153 18 ▶ sub _container { $CONSTANT_CONTAINER }
Subroutine _container: called CC=1 SCAR=0.0
2154  
2155 137 ▶ sub _update_characters {
Subroutine _update_characters: called CC=5 SCAR=16.2
2156 137 my $self = shift;
2157 137 my $nchar = $self->get_nchar;
2158 137 my $characters = $self->get_characters;
2159 137 my $type_object = $self->get_type_object;
2160 137 my @chars = @{ $characters->get_entities };
2161 137 my @defined = grep { defined $_ } @chars;
2162 137 ▶ if ( scalar @defined != $nchar ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @defined != $nchar) 26 111 137
2163 26 for my $i ( 0 .. ( $nchar - 1 ) ) {
2164 157 ▶ if ( not $chars[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chars[$i]) 157 0 157
2165 157 $characters->insert_at_index(
2166 $factory->create_character(
2167 '-type_object' => $type_object
2168 ),
2169 $i,
2170 );
2171 }
2172 }
2173 }
2174 137 @chars = @{ $characters->get_entities };
2175 137 ▶ if ( scalar(@chars) > $nchar ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @chars > $nchar) 0 137 137
2176 0 $characters->prune_entities( [ $nchar .. $#chars ] );
2177 }
2178 }
2179  
2180 =back
2181  
2182 =cut
2183  
2184 # podinherit_insert_token
2185  
2186 =head1 SEE ALSO
2187  
2188 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
2189 for any user or developer questions and discussions.
2190  
2191 =over
2192  
2193 =item L<Bio::Phylo::Taxa::TaxaLinker>
2194  
2195 This object inherits from L<Bio::Phylo::Taxa::TaxaLinker>, so the
2196 methods defined therein are also applicable to L<Bio::Phylo::Matrices::Matrix>
2197 objects.
2198  
2199 =item L<Bio::Phylo::Matrices::TypeSafeData>
2200  
2201 This object inherits from L<Bio::Phylo::Matrices::TypeSafeData>, so the
2202 methods defined therein are also applicable to L<Bio::Phylo::Matrices::Matrix>
2203 objects.
2204  
2205 =item L<Bio::Phylo::Manual>
2206  
2207 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
2208  
2209 =back
2210  
2211 =head1 CITATION
2212  
2213 If you use Bio::Phylo in published research, please cite it:
2214  
2215 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
2216 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
2217 I<BMC Bioinformatics> B<12>:63.
2218 L<http://dx.doi.org/10.1186/1471-2105-12-63>
2219  
2220 =cut
2221  
2222 }
2223 13 1;
2224 __DATA__
« Phylo/Unparsers/Nexus.pm ↑ Summary Phylo/NeXML/Entities.pm »