| 1 | =encoding utf8 | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Bio::BPWrapper::TreeManipulations - Functions for biotree | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Bio::BPWrapper::TreeManipulations; | ||||||||||||||||||||||||||||
| 10 | # Set options hash ... | ||||||||||||||||||||||||||||
| 11 | initialize(\%opts); | ||||||||||||||||||||||||||||
| 12 | write_out(\%opts); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Package global variables | ||||||||||||||||||||||||||||
| 17 | 16 | my ($in, $out, $aln, %opts, $file, $in_format, $out_format, @nodes, | |||||||||||||||||||||||||||
| 18 | $tree, $print_tree, $rootnode); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | ###################### subroutine ###################### | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | package Bio::BPWrapper::TreeManipulations; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 16 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 16 | ▶ | use Bio::BPWrapper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 16 | ▶ | use Bio::TreeIO; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 16 | ▶ | use Bio::Tree::Tree; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 16 | ▶ | use Bio::Tree::Node; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 16 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 16 | ▶ | if ($ENV{'DEBUG'}) { use Data::Dumper } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 16 | ▶ | use vars qw(@ISA @EXPORT @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 16 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 16 | @EXPORT = qw(print_tree_shape edge_length_abundance swap_otus getdistance | |||||||||||||||||||||||||||
| 40 | sister_pairs countOTU reroot clean_tree delete_otus initialize | ||||||||||||||||||||||||||||
| 41 | write_out); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head2 initialize() | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Sets up most of the actions to be performed on an alignment. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Call this right after setting up an options hash. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Sets package variables: C<$in_format>, C<@nodes>, C<$tree>, C<$out_format>, and C<$out>. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub initialize { | ||||||||||||||||||||||||||||
| 57 | 16 | ▶ | my $opts_ref = shift; | ||||||||||||||||||||||||||
|
Subroutine
initialize: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 16 | Bio::BPWrapper::common_opts($opts_ref); | |||||||||||||||||||||||||||
| 59 | 16 | %opts = %{$opts_ref}; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 16 | ▶ | $in_format = $opts{"input"} // 'newick'; # This doesn't work...or does it? | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"input"} // "newick"
MC/DC0%0/1
$opts{"input"} // "newick"
$opts{"input"}
Truth table50%1/2
A
A= $opts{"input"}
| |||||||||||||||||||||||||||||
| 62 | 16 | ▶ | $out_format = $opts{"output"} // "newick"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"output"} // "newick"
MC/DC100%1/1
$opts{"output"} // "newick"
$opts{"output"}
Truth table100%2/2
A
A= $opts{"output"}
| |||||||||||||||||||||||||||||
| 63 | 16 | $print_tree = 0; # Trigger printing the tree. | |||||||||||||||||||||||||||
| 64 | 16 | ▶ | my $file = shift || "STDIN"; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'STDIN'
MC/DC0%0/1
shift() || 'STDIN'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 16 | ▶ | $in = Bio::TreeIO->new(-format => $in_format, ($file eq "STDIN") ? (-fh => \*STDIN) : (-file => $file)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 16 | $tree = $in->next_tree(); # get the first tree (and ignore the rest) | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 16 | $out = Bio::TreeIO->new(-format => $out_format); | |||||||||||||||||||||||||||
| 70 | 16 | @nodes = $tree->get_nodes; | |||||||||||||||||||||||||||
| 71 | 16 | $rootnode = $tree->get_root_node; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub print_tree_shape { | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my @matrix; | ||||||||||||||||||||||||||
|
Subroutine
print_tree_shape: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 0 | my (%leaf, %inode); | |||||||||||||||||||||||||||
| 78 | 0 | my $ct_leaf = 0; | |||||||||||||||||||||||||||
| 79 | 0 | my $ct_inode = 0; | |||||||||||||||||||||||||||
| 80 | 0 | for my $nd (@nodes) { | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($nd->is_Leaf()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $leaf{$nd->id} = ++$ct_leaf; | |||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||
| 84 | 0 | $inode{$nd->internal_id} = ++$ct_inode; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | for my $nd (@nodes) { | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | next if $nd->is_Leaf(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | my @dscs = $nd->each_Descendent; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | die unless @dscs == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | my $id1 = $dscs[0]->is_Leaf | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | ? -1 * $leaf{$dscs[0]->id} : $inode{$dscs[0]->internal_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | my $id2 = $dscs[1]->is_Leaf | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | ? -1 * $leaf{$dscs[1]->id} : $inode{$dscs[1]->internal_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ($nd eq $rootnode) { # root at first | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | unshift @matrix, [ ($id1, $id2) ]; | |||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||
| 99 | 0 | push @matrix, [ ($id1, $id2) ] | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | for (my $i = $#matrix; $i >=0; $i--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | print $matrix[$i]->[0], "\t", $matrix[$i]->[1], "\n"; | |||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||
| 105 | # print Dumper(\%leaf, \%inode); | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub edge_length_abundance { | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | my @inodes; | ||||||||||||||||||||||||||
|
Subroutine
edge_length_abundance: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | my @brs; | ||||||||||||||||||||||||||||
| 111 | 0 | push (@inodes, $_) for _walk_up($rootnode); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | for my $nd (@inodes) { | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | next if $nd eq $rootnode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | my $id = $nd->internal_id; | |||||||||||||||||||||||||||
| 116 | 0 | my $ct_otus = 0; | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($nd->is_Leaf) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | $ct_otus = 1; | |||||||||||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||||||||||
| 120 | 0 | foreach (&_each_leaf($nd)) { | |||||||||||||||||||||||||||
| 121 | 0 | $ct_otus++; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 0 | push @brs, { 'id' => $id, 'num_tips' => $ct_otus, 'br_len' => $nd->branch_length() } | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | my $ct_tips = 0; | |||||||||||||||||||||||||||
| 128 | 0 | foreach my $nd (@nodes) { | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | $ct_tips ++ if $nd->is_Leaf(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | for (my $k=1; $k<=$ct_tips; $k++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | my $total = 0; | |||||||||||||||||||||||||||
| 134 | 0 | my @nds = grep { $_->{num_tips} == $k } @brs; | |||||||||||||||||||||||||||
| 135 | 0 | ▶ | if (@nds) { $total += $_->{br_len} for @nds } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | printf "%d\t%.6f\n", $k, $total/$tree->total_branch_length(); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub swap_otus { | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my @otus; | ||||||||||||||||||||||||||
|
Subroutine
swap_otus: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 0 | my $otu_ct = 0; | |||||||||||||||||||||||||||
| 143 | 0 | foreach (@nodes) { | |||||||||||||||||||||||||||
| 144 | 0 | ▶ | next unless $_->is_Leaf(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | push @otus, $_; | |||||||||||||||||||||||||||
| 146 | 0 | $otu_ct++; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | @otus = sort {$a->id() cmp $b->id() } @otus; | |||||||||||||||||||||||||||
| 149 | 0 | my $ref_otu; | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if ($opts{'swap-otus'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | $ref_otu = $tree->find_node($opts{'swap-otus'}) || die "node not found\n"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$tree->find_node($opts{'swap-otus'}) || die("node not found\n")
MC/DC0%0/1
$tree->find_node($opts{'swap-otus'}) || die("node not found\n")
$tree->find_node($opts{'swap-otus'})
Truth table0%0/2
A
A= $tree->find_node($opts{'swap-otus'})
| |||||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||
| 153 | 0 | $ref_otu = $otus[0]; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | foreach my $nd (@otus) { | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | next if $nd eq $ref_otu; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | my $nd_id = $nd->id(); | |||||||||||||||||||||||||||
| 159 | 0 | my $ref_id = $ref_otu->id(); | |||||||||||||||||||||||||||
| 160 | 0 | $nd->id("new_".$ref_id); | |||||||||||||||||||||||||||
| 161 | 0 | $ref_otu->id("new_".$nd_id); | |||||||||||||||||||||||||||
| 162 | 0 | say $tree->as_text($out_format); | |||||||||||||||||||||||||||
| 163 | 0 | $nd->id($nd_id); | |||||||||||||||||||||||||||
| 164 | 0 | $ref_otu->id($ref_id); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Get the distance between nodes | ||||||||||||||||||||||||||||
| 169 | sub getdistance { | ||||||||||||||||||||||||||||
| 170 | 1 | ▶ | my @dnodes = _name2node($opts{'distance'}); | ||||||||||||||||||||||||||
|
Subroutine
getdistance: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | 1 | ▶ | if (scalar(@dnodes) != 2) { say "Error: Provide exactly two nodes/leaves to use with --distance" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 1 | else { say $tree->distance(-nodes => \@dnodes) } | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | sub sister_pairs { | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | my @otus; | ||||||||||||||||||||||||||
|
Subroutine
sister_pairs: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 177 | 0 | my $otu_ct = 0; | |||||||||||||||||||||||||||
| 178 | 0 | foreach (@nodes) { | |||||||||||||||||||||||||||
| 179 | 0 | ▶ | next unless $_->is_Leaf(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | push @otus, $_; | |||||||||||||||||||||||||||
| 181 | 0 | $otu_ct++; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | @otus = sort {$a->id() cmp $b->id() } @otus; | |||||||||||||||||||||||||||
| 185 | 0 | ▶ | for (my $i = 0; $i < $otu_ct; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 0 | my $pa_i = $otus[$i]->ancestor(); | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | for (my $j = $i+1; $j < $otu_ct; $j++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | my $pa_j = $otus[$j]->ancestor(); | |||||||||||||||||||||||||||
| 189 | 0 | print $otus[$i]->id, "\t", $otus[$j]->id, "\t"; | |||||||||||||||||||||||||||
| 190 | 0 | ▶ | print $pa_i eq $pa_j ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head2 countOTU() | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Print total number of OTUs (leaves). | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | sub countOTU { | ||||||||||||||||||||||||||||
| 203 | 1 | ▶ | my $otu_ct = 0; | ||||||||||||||||||||||||||
|
Subroutine
countOTU: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 204 | 1 | ▶ | foreach (@nodes) { $otu_ct++ if $_->is_Leaf() } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 1 | say $otu_ct | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head2 reroot() | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | Reroot tree to node in C<$opts{'reroot'}> by creating new branch. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =cut | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | sub reroot { | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | my $outgroup_id = $opts{'reroot'}; | ||||||||||||||||||||||||||
|
Subroutine
reroot: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | 0 | my $outgroup = $tree->find_node($outgroup_id); | |||||||||||||||||||||||||||
| 217 | 0 | my $newroot = $outgroup->create_node_on_branch(-FRACTION => 0.5, -ANNOT => {id => 'newroot'}); | |||||||||||||||||||||||||||
| 218 | 0 | $tree->reroot($newroot); | |||||||||||||||||||||||||||
| 219 | 0 | $print_tree = 1; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | sub clean_tree { | ||||||||||||||||||||||||||||
| 223 | 0 | ▶ | foreach my $nd (@nodes) { | ||||||||||||||||||||||||||
|
Subroutine
clean_tree: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 224 | 0 | ▶ | $nd->branch_length(0) if $opts{'cleanbr'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 0 | ▶ | if ($opts{'cleanboot'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 0 | $nd->bootstrap(0); | |||||||||||||||||||||||||||
| 227 | 0 | ▶ | $nd->id('') unless $nd->is_Leaf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | 0 | $print_tree = 1; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | sub delete_otus { | ||||||||||||||||||||||||||||
| 234 | 0 | ▶ | my $ref_otus = &_get_otus(); | ||||||||||||||||||||||||||
|
Subroutine
delete_otus: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 235 | 0 | my @otus_to_retain = &_remove_otus($ref_otus, $opts{'delete-otus'}); | |||||||||||||||||||||||||||
| 236 | # print Dumper(\@otus_to_retain); | ||||||||||||||||||||||||||||
| 237 | 0 | $opts{'subset'} = join ",", @otus_to_retain; | |||||||||||||||||||||||||||
| 238 | 0 | &subset(); | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | sub _get_otus { | ||||||||||||||||||||||||||||
| 242 | 0 | ▶ | my @list; | ||||||||||||||||||||||||||
|
Subroutine
_get_otus: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 243 | 0 | ▶ | foreach my $nd (@nodes) { push @list, $nd if $nd->is_Leaf } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 0 | return \@list; | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | sub _remove_otus { | ||||||||||||||||||||||||||||
| 248 | 0 | ▶ | my $ref = shift; | ||||||||||||||||||||||||||
|
Subroutine
_remove_otus: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 249 | 0 | my $str = shift; | |||||||||||||||||||||||||||
| 250 | 0 | my @list; | |||||||||||||||||||||||||||
| 251 | 0 | my @otus_to_remove = split /\s*,\s*/, $str; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 0 | foreach my $nd (@$ref) { | |||||||||||||||||||||||||||
| 254 | 0 | foreach my $otu (@otus_to_remove) { | |||||||||||||||||||||||||||
| 255 | 0 | ▶ | push @list, $nd->id() unless $otu eq $nd->id(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | 0 | return @list; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | sub multi2bi { | ||||||||||||||||||||||||||||
| 262 | 0 | ▶ | foreach my $nd (@nodes) { | ||||||||||||||||||||||||||
|
Subroutine
multi2bi: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 263 | # next if $nd eq $rootnode; | ||||||||||||||||||||||||||||
| 264 | 0 | &_add_node($nd); | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | 0 | $print_tree = 1; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | sub _add_node { | ||||||||||||||||||||||||||||
| 270 | 0 | ▶ | my $node = shift; | ||||||||||||||||||||||||||
|
Subroutine
_add_node: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 271 | # warn "processing\t", $node->internal_id, "\n"; | ||||||||||||||||||||||||||||
| 272 | 0 | my @desc = $node->each_Descendent; | |||||||||||||||||||||||||||
| 273 | 0 | ▶ | return if scalar(@desc) <= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | # warn "multifurcating node:\t", $node->internal_id, " ... add a new node\n"; | ||||||||||||||||||||||||||||
| 275 | 0 | shift @desc; # retain the first descent | |||||||||||||||||||||||||||
| 276 | # my $new_node = $node->create_node_on_branch(-FRACTION => 0.5, -FORCE => 1, -ANNOT=>{ -id => "new_id" }); | ||||||||||||||||||||||||||||
| 277 | 0 | my $new_node = Bio::Tree::Node->new(-id => "new", -branch_length => 0); | |||||||||||||||||||||||||||
| 278 | 0 | $node->add_Descendent($new_node); | |||||||||||||||||||||||||||
| 279 | # warn "\ta new node created:\t", $new_node->id, "\n"; | ||||||||||||||||||||||||||||
| 280 | 0 | foreach (@desc) { | |||||||||||||||||||||||||||
| 281 | 0 | $node->remove_Descendent($_); # remove from grand-parent | |||||||||||||||||||||||||||
| 282 | # warn "\t\tremove descendant:\t", $_->internal_id, "\n"; | ||||||||||||||||||||||||||||
| 283 | 0 | $new_node->add_Descendent($_); # re-attarch to parent | |||||||||||||||||||||||||||
| 284 | # warn "\t\tadd descendant to the new node:\t", $_->internal_id, "\n"; | ||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | 0 | &_add_node($new_node); | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | # Subset a tree | ||||||||||||||||||||||||||||
| 290 | sub subset { | ||||||||||||||||||||||||||||
| 291 | # Collect the subset of nodes from STDIN or from $_ | ||||||||||||||||||||||||||||
| 292 | 2 | ▶ | my @keep_nodes; | ||||||||||||||||||||||||||
|
Subroutine
subset: called CC=17 SCAR=28.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 293 | 2 | ▶ | if ($opts{'subset'}) { @keep_nodes = _name2node($opts{'subset'}) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 1 | else { my $ar = $_[0]; @keep_nodes = @$ar } | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # Collect list of descendents | ||||||||||||||||||||||||||||
| 297 | 2 | my @descendents; | |||||||||||||||||||||||||||
| 298 | 2 | for my $nd (@keep_nodes) { push @descendents, $_ for $nd->get_all_Descendents } | |||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # Collect list of ancestors | ||||||||||||||||||||||||||||
| 301 | 2 | my @ancestors; | |||||||||||||||||||||||||||
| 302 | my $tmp; | ||||||||||||||||||||||||||||
| 303 | 2 | for (@keep_nodes) { | |||||||||||||||||||||||||||
| 304 | 10 | $tmp = $_; | |||||||||||||||||||||||||||
| 305 | 10 | ▶ | while ($tmp->ancestor) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 35 | push @ancestors, $tmp->ancestor; | |||||||||||||||||||||||||||
| 307 | 35 | $tmp = $tmp->ancestor | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | # Make a hash of nodes to keep | ||||||||||||||||||||||||||||
| 312 | 2 | my %keep = map { $_->internal_id => $_ } @keep_nodes; | |||||||||||||||||||||||||||
| 313 | 2 | $keep{$_->internal_id} = $_ for @descendents; | |||||||||||||||||||||||||||
| 314 | 2 | $keep{$_->internal_id} = $_ for @ancestors; | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # Remove all nodes but those in %keep | ||||||||||||||||||||||||||||
| 317 | 2 | ▶ | for (@nodes) { $tree->remove_Node($_) unless exists($keep{$_->internal_id}) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | # Clean up internal single-descendent nodes | ||||||||||||||||||||||||||||
| 320 | 2 | my @desc; | |||||||||||||||||||||||||||
| 321 | my $nd_len; | ||||||||||||||||||||||||||||
| 322 | 2 | my $desc_len; | |||||||||||||||||||||||||||
| 323 | 2 | for my $nd ($tree->get_nodes) { | |||||||||||||||||||||||||||
| 324 | 27 | ▶ | next if $nd == $rootnode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 25 | @desc = $nd->each_Descendent; | |||||||||||||||||||||||||||
| 326 | 25 | ▶ | next unless scalar(@desc) == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 9 | ▶ | $nd_len = $nd->branch_length() || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$nd->branch_length || 0
MC/DC0%0/1
$nd->branch_length || 0
$nd->branch_length
Truth table50%1/2
A
A= $nd->branch_length
| |||||||||||||||||||||||||||||
| 328 | 9 | ▶ | $desc_len = $desc[0]->branch_length() || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$desc[0]->branch_length || 0
MC/DC0%0/1
$desc[0]->branch_length || 0
$desc[0]->branch_length
Truth table50%1/2
A
A= $desc[0]->branch_length
| |||||||||||||||||||||||||||||
| 329 | 9 | $desc[0]->branch_length($nd_len + $desc_len); | |||||||||||||||||||||||||||
| 330 | 9 | $nd->ancestor->add_Descendent($desc[0]); | |||||||||||||||||||||||||||
| 331 | 9 | $tree->remove_Node($nd) | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | # Take care of the a single-descendent root node | ||||||||||||||||||||||||||||
| 335 | 2 | @desc = $rootnode->each_Descendent; | |||||||||||||||||||||||||||
| 336 | 2 | ▶ | if (scalar(@desc) == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | 0 | $rootnode->add_Descendent($_) for $desc[0]->each_Descendent; | |||||||||||||||||||||||||||
| 338 | 0 | $tree->remove_Node($desc[0]) | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | 2 | $print_tree = 1 | |||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | # Print OTU names and lengths | ||||||||||||||||||||||||||||
| 344 | sub print_leaves_lengths { | ||||||||||||||||||||||||||||
| 345 | 1 | ▶ | foreach (@nodes) { say $_->id(), "\t", $_->branch_length() if $_->is_Leaf() } | ||||||||||||||||||||||||||
|
Subroutine
print_leaves_lengths: called CC=3 SCAR=11.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # Get LCA | ||||||||||||||||||||||||||||
| 349 | sub getlca { | ||||||||||||||||||||||||||||
| 350 | 1 | ▶ | my @lca_nodes; | ||||||||||||||||||||||||||
|
Subroutine
getlca: called CC=7 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 351 | 1 | ▶ | if (_name2node($opts{'lca'})) { @lca_nodes = _name2node($opts{'lca'}) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | 0 | else { my $ar = $_[0]; @lca_nodes = @$ar } | |||||||||||||||||||||||||||
| 353 | 1 | my @nd_pair; | |||||||||||||||||||||||||||
| 354 | my $lca; | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 1 | $nd_pair[0] = $lca_nodes[0]; | |||||||||||||||||||||||||||
| 357 | 1 | ▶ | if (@lca_nodes > 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 358 | 1 | ▶ | for (my $index = 1; $index < @lca_nodes; $index++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | 2 | $nd_pair[1] = $lca_nodes[$index]; | |||||||||||||||||||||||||||
| 360 | 2 | $lca = $tree->get_lca(-nodes => \@nd_pair); | |||||||||||||||||||||||||||
| 361 | 2 | $nd_pair[0] = $lca | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | 1 | ▶ | if (_name2node($opts{'lca'})) { say $lca->internal_id } else { return $lca } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | } elsif (@lca_nodes == 1) { | ||||||||||||||||||||||||||||
| 365 | 0 | ▶ | if (_name2node($opts{'lca'})) { say $lca_nodes[0]->ancestor->internal_id } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 0 | else { return $lca_nodes[0]->ancestor->internal_id } | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | # Label nodes with their internal ID's | ||||||||||||||||||||||||||||
| 371 | sub label_nodes { | ||||||||||||||||||||||||||||
| 372 | 1 | ▶ | for (@nodes) { | ||||||||||||||||||||||||||
|
Subroutine
label_nodes: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 373 | 28 | ▶ | next if $_ == $rootnode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 374 | 27 | ▶ | my $suffix = defined($_->id) ? "_" . $_->id : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 27 | $_->id($_->internal_id . $suffix) | |||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | 1 | $print_tree = 1 | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | # Print half-tree id distances between all pairs of nodes | ||||||||||||||||||||||||||||
| 381 | sub listdistance { | ||||||||||||||||||||||||||||
| 382 | 1 | ▶ | my (@leaves, @sortedleaf_names, @leafnames); | ||||||||||||||||||||||||||
|
Subroutine
listdistance: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 383 | 1 | ▶ | foreach (@nodes) { push(@leaves, $_) if $_->is_Leaf() } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | # Make an alphabetical list of OTU names | ||||||||||||||||||||||||||||
| 386 | 1 | push @sortedleaf_names, $_->id foreach sort {lc($a->id) cmp lc($b->id)} @leaves; | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 1 | @leaves = (); | |||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | #Rebuld leaf array with new alphabetical order | ||||||||||||||||||||||||||||
| 391 | 1 | push @leaves, $tree->find_node(-id => $_) foreach @sortedleaf_names; | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | # Prints a half-matrix of distance values | ||||||||||||||||||||||||||||
| 394 | 1 | my $i = 1; | |||||||||||||||||||||||||||
| 395 | 1 | for my $firstleaf (@leaves) { | |||||||||||||||||||||||||||
| 396 | 15 | my @dnodes; | |||||||||||||||||||||||||||
| 397 | 15 | ▶ | for (my $x = $i; $x < scalar(@leaves); $x++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 105 | @dnodes = ($firstleaf, $leaves[$x]); | |||||||||||||||||||||||||||
| 399 | 105 | say join "\t", ($firstleaf->id(), $leaves[$x]->id(), $tree->distance(-nodes => \@dnodes)) | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | 15 | $i++ | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =head2 bin() | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | Divides tree into number of specified segments and counts branches up | ||||||||||||||||||||||||||||
| 408 | to height the segment. Prints: bin_number, branch_count, bin_floor, | ||||||||||||||||||||||||||||
| 409 | bin_ceiling. | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =cut | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | sub bin { | ||||||||||||||||||||||||||||
| 414 | 1 | ▶ | my $treeheight = _treeheight(\$tree); | ||||||||||||||||||||||||||
|
Subroutine
bin: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 415 | 1 | my $bincount = $opts{'ltt'}; | |||||||||||||||||||||||||||
| 416 | 1 | my $binsize = $treeheight/$bincount; | |||||||||||||||||||||||||||
| 417 | 1 | my @bins; | |||||||||||||||||||||||||||
| 418 | 1 | ▶ | while ($treeheight > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | 11 | unshift @bins, $treeheight; | |||||||||||||||||||||||||||
| 420 | 11 | $treeheight -= $binsize | |||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | # Handle imperfect division. When approaching 0, if a tiny number is found, such as 2e-17, assign it as 0 and ignore negatives that may follow. | ||||||||||||||||||||||||||||
| 423 | 1 | ▶ | for (@bins) { shift @bins if $_ < 1e-10 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | 1 | unshift @bins, 0; | |||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 1 | ▶ | for (my $i=0; $i+1<@bins; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 427 | 10 | my $branchcount = 1; # branch from root | |||||||||||||||||||||||||||
| 428 | # Starting from the root, add a branch for each found descendent | ||||||||||||||||||||||||||||
| 429 | 10 | $branchcount += _binrecursive(\$rootnode, $bins[$i+1]); | |||||||||||||||||||||||||||
| 430 | 10 | printf "%3d\t%3d\t%.4f\t%.4f\n", $i+1, $branchcount, $bins[$i], $bins[$i+1]; | |||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | sub print_all_lengths{ | ||||||||||||||||||||||||||||
| 435 | 1 | ▶ | for (@nodes) { | ||||||||||||||||||||||||||
|
Subroutine
print_all_lengths: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 436 | 28 | ▶ | next if $_ == $rootnode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 437 | 27 | my $p_node = $_->ancestor(); | |||||||||||||||||||||||||||
| 438 | 27 | ▶ | my $p_id = $p_node->id ? $p_node->id : $p_node->internal_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 439 | 27 | ▶ | my $c_id = $_->id ? $_->id() : $_->internal_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 440 | 27 | say $p_id, "\t", $c_id, "\t", $_->branch_length; | |||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | sub random_tree{ | ||||||||||||||||||||||||||||
| 445 | 1 | ▶ | my @otus = _each_leaf($rootnode); | ||||||||||||||||||||||||||
|
Subroutine
random_tree: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 446 | 1 | my @sample; | |||||||||||||||||||||||||||
| 447 | 1 | ▶ | my $sample_size = $opts{"random"} == 0 ? int(scalar(@otus) / 2) : $opts{"random"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | 1 | ▶ | die "Error: sample size ($sample_size) exceeds number of OTUs (", scalar(@otus), ")" if $sample_size > scalar(@otus); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | # Use Reservoir Sampling to pick random otus. | ||||||||||||||||||||||||||||
| 452 | 1 | my @sampled = (1 .. $sample_size); | |||||||||||||||||||||||||||
| 453 | 1 | for ($sample_size + 1 .. scalar(@otus)) { | |||||||||||||||||||||||||||
| 454 | 8 | ▶ | $sampled[rand(@sampled)] = $_ if rand() < $sample_size/$_ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||
| 456 | 1 | push @sample, $otus[--$_] for @sampled; | |||||||||||||||||||||||||||
| 457 | 1 | &subset(\@sample) | |||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | # Depth to the root for a node | ||||||||||||||||||||||||||||
| 461 | sub depth_to_root { | ||||||||||||||||||||||||||||
| 462 | 1 | ▶ | say $_->depth for _name2node($opts{'depth'}) | ||||||||||||||||||||||||||
|
Subroutine
depth_to_root: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | # Remove Branch Lenghts | ||||||||||||||||||||||||||||
| 466 | sub remove_brlengths { | ||||||||||||||||||||||||||||
| 467 | 1 | ▶ | foreach (@nodes) { $_->branch_length(0) if defined $_->branch_length } | ||||||||||||||||||||||||||
|
Subroutine
remove_brlengths: called CC=3 SCAR=11.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 468 | 1 | $print_tree = 1 | |||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | sub alldesc { | ||||||||||||||||||||||||||||
| 472 | 1 | ▶ | my @inodes; | ||||||||||||||||||||||||||
|
Subroutine
alldesc: called CC=6 SCAR=19.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 473 | 1 | my $inode_id = $opts{'allchildOTU'}; | |||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 1 | ▶ | if ($inode_id eq 'all') { push (@inodes, $_) for _walk_up($rootnode) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 476 | 1 | else { push @inodes, $tree->find_node(-internal_id => $inode_id) } | |||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 1 | for my $nd (@inodes) { | |||||||||||||||||||||||||||
| 479 | 1 | print $nd->internal_id, " "; | |||||||||||||||||||||||||||
| 480 | 1 | ▶ | if ($nd->is_Leaf) { print $nd->id } else { print $_->id, " " for _each_leaf($nd) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 481 | 1 | print "\n" | |||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | # Walks from starting OTU | ||||||||||||||||||||||||||||
| 486 | sub walk { | ||||||||||||||||||||||||||||
| 487 | 1 | ▶ | my $startleaf = $tree->find_node($opts{'walk'}); | ||||||||||||||||||||||||||
|
Subroutine
walk: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 488 | 1 | my $curnode = $startleaf->ancestor; | |||||||||||||||||||||||||||
| 489 | 1 | my $last_curnode = $startleaf; | |||||||||||||||||||||||||||
| 490 | 1 | my @decs; | |||||||||||||||||||||||||||
| 491 | my %visited; | ||||||||||||||||||||||||||||
| 492 | 1 | my $totlen = 0; | |||||||||||||||||||||||||||
| 493 | 1 | my @dpair; | |||||||||||||||||||||||||||
| 494 | 1 | my $vcount = 0; | |||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | 1 | $visited{$startleaf} = 1; | |||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | 1 | ▶ | while ($curnode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 499 | 4 | $visited{$curnode} = 1; | |||||||||||||||||||||||||||
| 500 | 4 | @dpair = ($last_curnode, $curnode); | |||||||||||||||||||||||||||
| 501 | 4 | $totlen += $tree->distance(-nodes => \@dpair); | |||||||||||||||||||||||||||
| 502 | 4 | _desclen($curnode, \%visited, \$totlen, \$vcount); | |||||||||||||||||||||||||||
| 503 | 4 | $last_curnode = $curnode; | |||||||||||||||||||||||||||
| 504 | 4 | $curnode = $curnode->ancestor | |||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | =head2 write_out() | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | Performs the bulk of the actions actions set via | ||||||||||||||||||||||||||||
| 512 | L<C<initialize(\%opts)>|/initialize>. | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | Call this after calling C<#initialize(\%opts)>. | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | =cut | ||||||||||||||||||||||||||||
| 517 | sub write_out { | ||||||||||||||||||||||||||||
| 518 | 16 | ▶ | my $opts = shift; | ||||||||||||||||||||||||||
|
Subroutine
write_out: called CC=27 SCAR=33.5
Pod
documented
| |||||||||||||||||||||||||||||
| 519 | 16 | ▶ | getdistance() if $opts->{'distance'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 520 | 16 | ▶ | say $tree->total_branch_length() if $opts->{'length'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 521 | 16 | ▶ | countOTU() if $opts->{'numOTU'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 522 | 16 | ▶ | $print_tree = 1 if defined($opts->{'output'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 523 | 16 | ▶ | reroot() if $opts->{'reroot'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 524 | 16 | ▶ | subset() if $opts->{'subset'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 525 | 16 | ▶ | print_leaves_lengths() if $opts->{'otu'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 526 | 16 | ▶ | getlca() if $opts->{'lca'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | 16 | ▶ | label_nodes() if $opts->{'labelnodes'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 529 | 16 | ▶ | listdistance() if $opts->{'distanceall'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 530 | 16 | ▶ | bin() if $opts->{'ltt'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 531 | 16 | ▶ | print_all_lengths() if $opts->{'lengthall'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 532 | 16 | ▶ | random_tree() if defined($opts->{'random'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 533 | 16 | ▶ | depth_to_root() if $opts->{'depth'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 534 | 16 | ▶ | remove_brlengths() if $opts->{'rmbl'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 535 | 16 | ▶ | alldesc() if $opts->{'allchildOTU'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 536 | 16 | ▶ | walk() if $opts->{'walk'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 537 | 16 | ▶ | multi2bi() if $opts->{'multi2bi'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 538 | 16 | ▶ | clean_tree() if $opts->{'cleanbr'} || $opts->{'cleanboot'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts->{"cleanbr"} or $opts->{"cleanboot"}
MC/DC0%0/2
$opts->{"cleanbr"} or $opts->{"cleanboot"}
$opts->{"cleanbr"}
$opts->{"cleanboot"}
Truth table33%1/3
A or B
A= $opts->{"cleanbr"}B= $opts->{"cleanboot"}
| |||||||||||||||||||||||||||||
| 539 | 16 | ▶ | delete_otus() if $opts->{'delete-otus'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 540 | 16 | ▶ | sister_pairs() if $opts->{'sis-pairs'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 541 | 16 | ▶ | swap_otus() if $opts->{'swap-otus'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 542 | 16 | ▶ | edge_length_abundance() if $opts->{'ead'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 543 | 16 | ▶ | print_tree_shape() if $opts->{'tree-shape'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 544 | 16 | ▶ | say $tree->as_text($out_format) if $print_tree; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | ################# internal subroutines ############## | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | sub _name2node { | ||||||||||||||||||||||||||||
| 550 | 6 | ▶ | my $str = shift; | ||||||||||||||||||||||||||
|
Subroutine
_name2node: called CC=4 SCAR=14.8
| |||||||||||||||||||||||||||||
| 551 | 6 | my @node_names = split /,/, $str; | |||||||||||||||||||||||||||
| 552 | 6 | my $nd; | |||||||||||||||||||||||||||
| 553 | my @node_objects; | ||||||||||||||||||||||||||||
| 554 | 6 | for my $node_name (@node_names) { | |||||||||||||||||||||||||||
| 555 | 17 | ▶ | $nd = $tree->find_node(-id => $node_name) || $tree->find_node(-internal_id => $node_name); | ||||||||||||||||||||||||||
|
Condition33%1/3
$tree->find_node('-id', $node_name) || $tree->find_node('-internal_id', $node_name)
MC/DC0%0/2
$tree->find_node('-id', $node_name) || $tree->find_node('-internal_id', $node_name)
$tree->find_node('-id', $node_name)
$tree->find_node('-internal_id', $node_name)
Truth table33%1/3
A || B
A= $tree->find_node('-id', $node_name)B= $tree->find_node('-internal_id', $node_name)
| |||||||||||||||||||||||||||||
| 556 | 17 | ▶ | if ($nd) { push @node_objects, $nd } else { say "Node/leaf '$node_name' not found. Ignoring..." } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||
| 558 | return @node_objects | ||||||||||||||||||||||||||||
| 559 | 6 | } | |||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | # _each_leaf ($node): returns a list of all OTU's descended from this node, if any | ||||||||||||||||||||||||||||
| 562 | sub _each_leaf { | ||||||||||||||||||||||||||||
| 563 | 1 | ▶ | my @leaves; | ||||||||||||||||||||||||||
|
Subroutine
_each_leaf: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 564 | 1 | ▶ | for ($_[0]->get_all_Descendents) { push (@leaves, $_) if $_->is_Leaf } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 565 | return @leaves | ||||||||||||||||||||||||||||
| 566 | 1 | } | |||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | # main routine to walk up from root | ||||||||||||||||||||||||||||
| 569 | sub _wu { | ||||||||||||||||||||||||||||
| 570 | 0 | ▶ | my (@lf, @nd); | ||||||||||||||||||||||||||
|
Subroutine
_wu: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 571 | 0 | my $curnode = $_[0]; | |||||||||||||||||||||||||||
| 572 | 0 | my @decs = $_[0]->each_Descendent; | |||||||||||||||||||||||||||
| 573 | 0 | my $visitref = $_[1]; | |||||||||||||||||||||||||||
| 574 | 0 | my %visited = %$visitref; | |||||||||||||||||||||||||||
| 575 | 0 | my $node_list_ref = $_[2]; | |||||||||||||||||||||||||||
| 576 | # my $ref_ct_otu = $_[3]; | ||||||||||||||||||||||||||||
| 577 | # my $ref_tatal_br_len = $_[4]; | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | 0 | for (@decs) { | |||||||||||||||||||||||||||
| 580 | # $ref_total_br_len += $_->branch_length; | ||||||||||||||||||||||||||||
| 581 | 0 | ▶ | if ($_->is_Leaf) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 582 | 0 | push @lf, $_; | |||||||||||||||||||||||||||
| 583 | # $$ref_ct_otu++; | ||||||||||||||||||||||||||||
| 584 | } else { | ||||||||||||||||||||||||||||
| 585 | 0 | push @nd, $_ | |||||||||||||||||||||||||||
| 586 | } | ||||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | 0 | ▶ | for (@lf) { if (!exists($visited{$_})) { $visited{$_} = 1; push @$node_list_ref, $_ } } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 590 | 0 | for (@nd) { | |||||||||||||||||||||||||||
| 591 | 0 | ▶ | next if exists($visited{$_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 592 | 0 | $visited{$_} = 1; | |||||||||||||||||||||||||||
| 593 | 0 | push @$node_list_ref, $_; | |||||||||||||||||||||||||||
| 594 | 0 | _wu($_, \%visited, $node_list_ref) | |||||||||||||||||||||||||||
| 595 | } | ||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | # Walk Up: "Walks" up from a given node and returned an order array representing the order that each node descended from the given node was visited. | ||||||||||||||||||||||||||||
| 599 | sub _walk_up { | ||||||||||||||||||||||||||||
| 600 | 0 | ▶ | my %visited; | ||||||||||||||||||||||||||
|
Subroutine
_walk_up: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 601 | 0 | my @node_list = $_[0]; | |||||||||||||||||||||||||||
| 602 | 0 | _wu($_[0], \%visited, \@node_list); | |||||||||||||||||||||||||||
| 603 | return @node_list | ||||||||||||||||||||||||||||
| 604 | 0 | } | |||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | sub _treeheight { | ||||||||||||||||||||||||||||
| 607 | 1 | ▶ | my $height = 0; | ||||||||||||||||||||||||||
|
Subroutine
_treeheight: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 608 | 1 | my $tree = $_[0]; | |||||||||||||||||||||||||||
| 609 | 1 | ▶ | for ($$tree->get_nodes) { $height = $_->depth if $_->depth > $height } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 610 | 1 | return $height | |||||||||||||||||||||||||||
| 611 | } | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | sub _binrecursive { | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | 213 | ▶ | my $branchcount = 0; | ||||||||||||||||||||||||||
|
Subroutine
_binrecursive: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 616 | 213 | my $noderef = $_[0]; | |||||||||||||||||||||||||||
| 617 | 213 | my $upper = $_[1]; | |||||||||||||||||||||||||||
| 618 | 213 | my @desc = $$noderef->each_Descendent; | |||||||||||||||||||||||||||
| 619 | 213 | ▶ | $branchcount-- unless $$noderef->is_Leaf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | 213 | for (@desc) { | |||||||||||||||||||||||||||
| 622 | 224 | $branchcount++; | |||||||||||||||||||||||||||
| 623 | 224 | ▶ | $branchcount += _binrecursive(\$_, $upper) if $_->depth <= $upper | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 624 | } | ||||||||||||||||||||||||||||
| 625 | 213 | return $branchcount | |||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | # Starting at a node that has 2 descendents, print the distance from start to desc if it's a leaf or call itself passing the internal-node descendent | ||||||||||||||||||||||||||||
| 629 | # Input: basenode, internal node | ||||||||||||||||||||||||||||
| 630 | sub _desclen { | ||||||||||||||||||||||||||||
| 631 | # startlear, curnode | ||||||||||||||||||||||||||||
| 632 | 13 | ▶ | my (@dpair, @lf, @nd); | ||||||||||||||||||||||||||
|
Subroutine
_desclen: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 633 | 13 | my $curnode = $_[0]; | |||||||||||||||||||||||||||
| 634 | 13 | my @decs = $_[0]->each_Descendent; | |||||||||||||||||||||||||||
| 635 | 13 | my $visitref = $_[1]; | |||||||||||||||||||||||||||
| 636 | 13 | my $totlen = $_[2]; | |||||||||||||||||||||||||||
| 637 | 13 | my $vcountref = $_[3]; | |||||||||||||||||||||||||||
| 638 | 13 | my %visited = %$visitref; | |||||||||||||||||||||||||||
| 639 | 13 | my $dist; | |||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | 13 | ▶ | for (@decs) { if ($_->is_Leaf) { push @lf, $_ } else { push @nd, $_ } } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 642 | 13 | for (@lf) { | |||||||||||||||||||||||||||
| 643 | 15 | ▶ | next if exists($visited{$_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 644 | 14 | $visited{$_} = 1; | |||||||||||||||||||||||||||
| 645 | 14 | $dpair[0] = $curnode; | |||||||||||||||||||||||||||
| 646 | 14 | $dpair[1] = $_; | |||||||||||||||||||||||||||
| 647 | 14 | $dist = $tree->distance(-nodes => \@dpair); | |||||||||||||||||||||||||||
| 648 | 14 | $$totlen += $dist; | |||||||||||||||||||||||||||
| 649 | 14 | $$vcountref++; | |||||||||||||||||||||||||||
| 650 | 14 | say $_->id, "\t$$totlen\t$$vcountref" | |||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | 13 | for (@nd) { | |||||||||||||||||||||||||||
| 654 | 12 | ▶ | next if exists($visited{$_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 655 | 9 | $visited{$_} = 1; | |||||||||||||||||||||||||||
| 656 | 9 | $dpair[0] = $curnode; | |||||||||||||||||||||||||||
| 657 | 9 | $dpair[1] = $_; | |||||||||||||||||||||||||||
| 658 | 9 | $dist = $tree->distance(-nodes => \@dpair); | |||||||||||||||||||||||||||
| 659 | 9 | $$totlen += $dist; | |||||||||||||||||||||||||||
| 660 | 9 | _desclen($_, \%visited, $totlen, $vcountref) | |||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | 16 | 1; | |||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | __END__ | ||||||||||||||||||||||||||||