| 1 | package Bio::Coordinate::Graph; | ||||||||||||||||||||||||||||
| 2 | 4 | our $AUTHORITY = 'cpan:BIOPERLML'; | |||||||||||||||||||||||||||
| 3 | 4 | $Bio::Coordinate::Graph::VERSION = '1.007001'; | |||||||||||||||||||||||||||
| 4 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use parent qw(Bio::Root::Root); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Finds shortest path between nodes in a graph. | ||||||||||||||||||||||||||||
| 10 | # AUTHOR: Heikki Lehvaslaiho <heikki@bioperl.org> | ||||||||||||||||||||||||||||
| 11 | # OWNER: Heikki Lehvaslaiho | ||||||||||||||||||||||||||||
| 12 | # LICENSE: Perl_5 | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 8 | my($class,@args) = @_; | |||||||||||||||||||||||||||
| 18 | 8 | my $self = $class->SUPER::new(@args); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 8 | my($graph, $hasharray) = | |||||||||||||||||||||||||||
| 21 | $self->_rearrange([qw( | ||||||||||||||||||||||||||||
| 22 | GRAPH | ||||||||||||||||||||||||||||
| 23 | HASHARRAY | ||||||||||||||||||||||||||||
| 24 | )], | ||||||||||||||||||||||||||||
| 25 | @args); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 8 | ▶ | $graph && $self->graph($graph); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 8 | ▶ | $hasharray && $self->hasharray($hasharray); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 8 | $self->{'_root'} = undef; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 8 | return $self; # success - we hope! | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub graph { | ||||||||||||||||||||||||||
|
Subroutine
graph: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | my ($self,$value) = @_; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $self->throw("Need a hash of hashes") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | unless ref($value) eq 'HASH' ; | ||||||||||||||||||||||||||||
| 43 | 0 | $self->{'_dag'} = $value; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # empty the cache | ||||||||||||||||||||||||||||
| 46 | 0 | $self->{'_root'} = undef; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | return $self->{'_dag'}; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 8 | ▶ | sub hash_of_arrays { | ||||||||||||||||||||||||||
|
Subroutine
hash_of_arrays: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 8 | my ($self,$value) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # empty the cache | ||||||||||||||||||||||||||||
| 60 | 8 | $self->{'_root'} = undef; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 8 | ▶ | if ($value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 8 | ▶ | $self->throw("Need a hash of hashes") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | unless ref($value) eq 'HASH' ; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #copy the hash of arrays into a hash of hashes; | ||||||||||||||||||||||||||||
| 68 | 8 | my %hash; | |||||||||||||||||||||||||||
| 69 | 8 | foreach my $start ( keys %{$value}){ | |||||||||||||||||||||||||||
| 70 | 79 | $hash{$start} = undef; | |||||||||||||||||||||||||||
| 71 | 79 | map { $hash{$start}{$_} = 1 } @{$value->{$start}}; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 8 | $self->{'_dag'} = \%hash; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 8 | return $self->{'_dag'}; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 21 | ▶ | sub shortest_path { | ||||||||||||||||||||||||||
|
Subroutine
shortest_path: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 21 | my ($self, $root, $end) = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 21 | ▶ | $self->throw("Two arguments needed") unless @_ == 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | $self->throw("No node name [$root]") | ||||||||||||||||||||||||||||
| 87 | 21 | ▶ | unless exists $self->{'_dag'}->{$root}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | $self->throw("No node name [$end]") | ||||||||||||||||||||||||||||
| 89 | 21 | ▶ | unless exists $self->{'_dag'}->{$end}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 21 | my @res; # results | |||||||||||||||||||||||||||
| 92 | my $reverse; | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 21 | ▶ | if ($root > $end) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1 | ($root, $end) = ($end, $root ); | |||||||||||||||||||||||||||
| 96 | 1 | $reverse++; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # try to use cached paths | ||||||||||||||||||||||||||||
| 100 | $self->dijkstra($root) unless | ||||||||||||||||||||||||||||
| 101 | 21 | ▶ | defined $self->{'_root'} and $self->{'_root'} eq $root; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"_root"} and $self->{"_root"} eq $root
MC/DC100%2/2
defined $self->{"_root"} and $self->{"_root"} eq $root
defined $self->{"_root"}
$self->{"_root"} eq $root
Truth table100%3/3
A and B
A= defined $self->{"_root"}B= $self->{"_root"} eq $root
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 21 | ▶ | return @res unless $self->{'_paths'} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # create the list | ||||||||||||||||||||||||||||
| 106 | 21 | my $node = $end; | |||||||||||||||||||||||||||
| 107 | 21 | my $prev = $self->{'_paths'}->{$end}{'prev'}; | |||||||||||||||||||||||||||
| 108 | 21 | ▶ | while ($prev) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 29 | unshift @res, $node; | |||||||||||||||||||||||||||
| 110 | 29 | $node = $self->{'_paths'}->{$node}{'prev'}; | |||||||||||||||||||||||||||
| 111 | 29 | $prev = $self->{'_paths'}->{$node}{'prev'}; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 21 | unshift @res, $node; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 21 | ▶ | $reverse ? return reverse @res : return @res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 14 | ▶ | sub dijkstra { | ||||||||||||||||||||||||||
|
Subroutine
dijkstra: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 14 | my ($self,$root) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 14 | ▶ | $self->throw("I need the name of the root node input") unless $root; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | $self->throw("No node name [$root]") | ||||||||||||||||||||||||||||
| 124 | 14 | ▶ | unless exists $self->{'_dag'}->{$root}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 14 | my %est = (); # estimate hash | |||||||||||||||||||||||||||
| 127 | 14 | my %res = (); # result hash | |||||||||||||||||||||||||||
| 128 | 14 | my $nodes = keys %{$self->{'_dag'}}; | |||||||||||||||||||||||||||
| 129 | 14 | my $maxdist = 1000000; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # cache the root value | ||||||||||||||||||||||||||||
| 132 | 14 | $self->{'_root'} = $root; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 14 | foreach my $node ( keys %{$self->{'_dag'}} ){ | |||||||||||||||||||||||||||
| 135 | 136 | ▶ | if ($node eq $root) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 14 | $est{$node}{'prev'} = undef; | |||||||||||||||||||||||||||
| 137 | 14 | $est{$node}{'dist'} = 0; | |||||||||||||||||||||||||||
| 138 | } else { | ||||||||||||||||||||||||||||
| 139 | 122 | $est{$node}{'prev'} = undef; | |||||||||||||||||||||||||||
| 140 | 122 | $est{$node}{'dist'} = $maxdist; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # remove nodes from %est until it is empty | ||||||||||||||||||||||||||||
| 145 | 14 | ▶ | while (keys %est) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | #select the node closest to current one, or root node | ||||||||||||||||||||||||||||
| 148 | 111 | my $min_node; | |||||||||||||||||||||||||||
| 149 | 111 | my $min = $maxdist; | |||||||||||||||||||||||||||
| 150 | 111 | foreach my $node (reverse sort keys %est) { | |||||||||||||||||||||||||||
| 151 | 638 | ▶ | if ( $est{$node}{'dist'} < $min ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 135 | $min = $est{$node}{'dist'}; | |||||||||||||||||||||||||||
| 153 | 135 | $min_node = $node; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # no more links between nodes | ||||||||||||||||||||||||||||
| 158 | 111 | ▶ | last unless ($min_node); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # move the node from %est into %res; | ||||||||||||||||||||||||||||
| 161 | 103 | $res{$min_node} = delete $est{$min_node}; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # recompute distances to the neighbours | ||||||||||||||||||||||||||||
| 164 | 103 | my $dist = $res{$min_node}{'dist'}; | |||||||||||||||||||||||||||
| 165 | 103 | foreach my $neighbour ( keys %{$self->{'_dag'}->{$min_node}} ){ | |||||||||||||||||||||||||||
| 166 | 115 | ▶ | next unless $est{$neighbour}; # might not be there any more | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 89 | $est{$neighbour}{'prev'} = $min_node; | |||||||||||||||||||||||||||
| 168 | $est{$neighbour}{'dist'} = | ||||||||||||||||||||||||||||
| 169 | $dist + $self->{'_dag'}{$min_node}{$neighbour} | ||||||||||||||||||||||||||||
| 170 | 89 | ▶ | if $est{$neighbour}{'dist'} > $dist + 1 ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 14 | return $self->{'_paths'} = \%res; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 4 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||