| 1 | package Text::Tradition::StemmaUtil; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use vars qw/ @EXPORT_OK /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Bio::Phylo::IO; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Encode qw( encode_utf8 decode_utf8 ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use File::chdir; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use File::Temp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use File::Which; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Graph; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Graph::Reader::Dot; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use IPC::Run qw/ run binary /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Text::Tradition::Error; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 5 | @EXPORT_OK = qw/ read_graph display_graph editable_graph | |||||||||||||||||||||||||||||||
| 17 | character_input phylip_pars parse_newick newick_to_svg /; | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | Text::Tradition::StemmaUtil - standalone utilities for stemma graph display and | ||||||||||||||||||||||||||||||||
| 22 | distance tree calculations | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | This package contains a set of utilities for displaying arbitrary stemmata and | ||||||||||||||||||||||||||||||||
| 27 | running phylogenetic analysis on text collations. | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | =head2 read_graph( $dotstr) { | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | Parses the graph specification on the filehandle in $dotstr and returns a Graph | ||||||||||||||||||||||||||||||||
| 34 | object. This subroutine works around some deficiencies in Graph::Reader::Dot. | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 8 | ▶ | sub read_graph { | ||||||||||||||||||||||||||||||
|
Subroutine
read_graph: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 39 | 8 | my $dotstr = shift; | |||||||||||||||||||||||||||||||
| 40 | # Graph::Reader::Dot does not handle bare non-ASCII Unicode word characters. | ||||||||||||||||||||||||||||||||
| 41 | # We get around this by wrapping all words in double quotes, as long as they | ||||||||||||||||||||||||||||||||
| 42 | # aren't already wrapped, and as long as they aren't the initial '(di)?graph'. | ||||||||||||||||||||||||||||||||
| 43 | # Also need to deal correctly with the graph title. | ||||||||||||||||||||||||||||||||
| 44 | 8 | ▶ | if( $dotstr =~ /^\s*((di)?graph)\s+(.*?)\s*\{(.*)$/s ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 45 | 8 | my( $decl, $ident, $rest ) = ( $1, $3, $4 ); | |||||||||||||||||||||||||||||||
| 46 | 8 | ▶ | unless( substr( $ident, 0, 1 ) eq '"' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | 4 | $ident = '"'.$ident.'"'; | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | 8 | $rest =~ s/(?<!")\b(\w+)\b(?!")/"$1"/g; | |||||||||||||||||||||||||||||||
| 50 | 8 | $dotstr = "$decl $ident { $rest"; | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | # Now open a filehandle onto the string and pass it to Graph::Reader::Dot. | ||||||||||||||||||||||||||||||||
| 54 | 8 | $dotstr = encode_utf8( $dotstr ); | |||||||||||||||||||||||||||||||
| 55 | 8 | my $dotfh; | |||||||||||||||||||||||||||||||
| 56 | 8 | open $dotfh, '<', \$dotstr; | |||||||||||||||||||||||||||||||
| 57 | 8 | binmode $dotfh, ':utf8'; | |||||||||||||||||||||||||||||||
| 58 | 8 | my $reader = Graph::Reader::Dot->new(); | |||||||||||||||||||||||||||||||
| 59 | # Redirect STDOUT in order to trap any error messages - syntax errors | ||||||||||||||||||||||||||||||||
| 60 | # are evidently not fatal. | ||||||||||||||||||||||||||||||||
| 61 | 8 | my $graph; | |||||||||||||||||||||||||||||||
| 62 | my $reader_out; | ||||||||||||||||||||||||||||||||
| 63 | 8 | my $reader_err; | |||||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||||||
| 65 | 8 | local(*STDOUT); | |||||||||||||||||||||||||||||||
| 66 | 8 | open( STDOUT, ">", \$reader_out ); | |||||||||||||||||||||||||||||||
| 67 | 8 | local(*STDERR); | |||||||||||||||||||||||||||||||
| 68 | 8 | open( STDERR, ">", \$reader_err ); | |||||||||||||||||||||||||||||||
| 69 | 8 | $graph = $reader->read_graph( $dotfh ); | |||||||||||||||||||||||||||||||
| 70 | 8 | close STDOUT; | |||||||||||||||||||||||||||||||
| 71 | 8 | close STDERR; | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | if( $reader_out && $reader_out =~ /error/s ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$reader_out and $reader_out =~ /error/s
MC/DC50%1/2
$reader_out and $reader_out =~ /error/s
$reader_out
$reader_out =~ /error/s
Truth table66%2/3
A and B
A= $reader_outB= $reader_out =~ /error/s
| |||||||||||||||||||||||||||||||||
| 74 | 1 | throw( "Error trying to parse dot: $reader_out" ); | |||||||||||||||||||||||||||||||
| 75 | } elsif( !$graph ) { | ||||||||||||||||||||||||||||||||
| 76 | 0 | throw( "Failed to create graph from dot" ); | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | # Wrench the graph identifier out of the graph | ||||||||||||||||||||||||||||||||
| 79 | ## HORRIBLE HACK but there is no API access to the graph identifier! | ||||||||||||||||||||||||||||||||
| 80 | 7 | $graph->set_graph_attribute( 'name', $graph->[4]->{'name'} ); | |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | # Correct for implicit graph -> digraph quirk of reader | ||||||||||||||||||||||||||||||||
| 83 | 7 | ▶ | if( $reader_err && $reader_err =~ /graph will be treated as digraph/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$reader_err and $reader_err =~ /graph will be treated as digraph/
MC/DC50%1/2
$reader_err and $reader_err =~ /graph will be treated as digraph/
$reader_err
$reader_err =~ /graph will be treated as digraph/
Truth table66%2/3
A and B
A= $reader_errB= $reader_err =~ /graph will be treated as digraph/
| |||||||||||||||||||||||||||||||||
| 84 | 2 | my $udgraph = $graph->undirected_copy; | |||||||||||||||||||||||||||||||
| 85 | 2 | $udgraph->set_graph_attributes( $graph->get_graph_attributes ); | |||||||||||||||||||||||||||||||
| 86 | 2 | foreach my $v ( $graph->vertices ) { | |||||||||||||||||||||||||||||||
| 87 | 50 | $udgraph->set_vertex_attributes( $v, $graph->get_vertex_attributes( $v ) ); | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | 2 | $graph = $udgraph; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 7 | return $graph; | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | =head2 display_graph( $graph, $opts ) | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | Returns a dot specification intended for display, according to the logical | ||||||||||||||||||||||||||||||||
| 98 | attributes of the witnesses. | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 1 | ▶ | sub display_graph { | ||||||||||||||||||||||||||||||
|
Subroutine
display_graph: called CC=13 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 103 | 1 | my( $graph, $opts ) = @_; | |||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | # Get default and specified options | ||||||||||||||||||||||||||||||||
| 106 | 1 | my %graphopts = ( | |||||||||||||||||||||||||||||||
| 107 | # 'ratio' => 1, | ||||||||||||||||||||||||||||||||
| 108 | 'bgcolor' => 'transparent', | ||||||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||||||
| 110 | 1 | my %nodeopts = ( | |||||||||||||||||||||||||||||||
| 111 | 'fontsize' => 11, | ||||||||||||||||||||||||||||||||
| 112 | 'style' => 'filled', | ||||||||||||||||||||||||||||||||
| 113 | 'fillcolor' => 'white', | ||||||||||||||||||||||||||||||||
| 114 | 'color' => 'white', | ||||||||||||||||||||||||||||||||
| 115 | 'shape' => 'ellipse', # Shape for the extant nodes | ||||||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||||||
| 117 | 1 | my %edgeopts = ( | |||||||||||||||||||||||||||||||
| 118 | 'arrowhead' => 'none', | ||||||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||||||
| 120 | 0 | @graphopts{ keys %{$opts->{'graph'}} } = values %{$opts->{'graph'}} | |||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | if $opts->{'graph'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | 0 | @nodeopts{ keys %{$opts->{'node'}} } = values %{$opts->{'node'}} | |||||||||||||||||||||||||||||||
| 123 | 1 | ▶ | if $opts->{'node'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 124 | 0 | @edgeopts{ keys %{$opts->{'edge'}} } = values %{$opts->{'edge'}} | |||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | if $opts->{'edge'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | my $gdecl = $graph->is_directed ? 'digraph' : 'graph'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | my $gname = $opts->{'name'} ? '"' . $opts->{'name'} . '"' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | : 'stemma'; | ||||||||||||||||||||||||||||||||
| 130 | 1 | my @dotlines; | |||||||||||||||||||||||||||||||
| 131 | 1 | push( @dotlines, "$gdecl $gname {" ); | |||||||||||||||||||||||||||||||
| 132 | ## Print out the global attributes | ||||||||||||||||||||||||||||||||
| 133 | 1 | ▶ | push( @dotlines, _make_dotline( 'graph', %graphopts ) ) if keys %graphopts; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | 1 | ▶ | push( @dotlines, _make_dotline( 'edge', %edgeopts ) ) if keys %edgeopts; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | 1 | ▶ | push( @dotlines, _make_dotline( 'node', %nodeopts ) ) if keys %nodeopts; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | # Add each of the nodes. | ||||||||||||||||||||||||||||||||
| 138 | 1 | foreach my $n ( $graph->vertices ) { | |||||||||||||||||||||||||||||||
| 139 | 5 | my %vattr = ( 'id' => $n ); # Set the SVG element ID to the sigil itself | |||||||||||||||||||||||||||||||
| 140 | 5 | ▶ | if( $graph->has_vertex_attribute( $n, 'label' ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | 0 | $vattr{'label'} = $graph->get_vertex_attribute( $n, 'label' ); | |||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | 5 | push( @dotlines, _make_dotline( $n, %vattr ) ); | |||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | # Add each of our edges. | ||||||||||||||||||||||||||||||||
| 146 | 1 | foreach my $e ( $graph->edges ) { | |||||||||||||||||||||||||||||||
| 147 | 4 | my( $from, $to ) = map { _dotquote( $_ ) } @$e; | |||||||||||||||||||||||||||||||
| 148 | 4 | ▶ | my $connector = $graph->is_directed ? '->' : '--'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | 4 | push( @dotlines, " $from $connector $to;" ); | |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | 1 | push( @dotlines, '}' ); | |||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 1 | return join( "\n", @dotlines ); | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | =head2 editable_graph( $graph, $opts ) | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | Returns a dot specification of a stemma graph with logical witness features, | ||||||||||||||||||||||||||||||||
| 160 | intended for editing the stemma definition. | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 250 | ▶ | sub editable_graph { | ||||||||||||||||||||||||||||||
|
Subroutine
editable_graph: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 165 | 250 | my( $graph, $opts ) = @_; | |||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | # Create the graph | ||||||||||||||||||||||||||||||||
| 168 | 250 | ▶ | my $join = ( $opts && exists $opts->{'linesep'} ) ? $opts->{'linesep'} : "\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts and exists $opts->{"linesep"}
MC/DC50%1/2
$opts and exists $opts->{"linesep"}
$opts
exists $opts->{"linesep"}
Truth table66%2/3
A and B
A= $optsB= exists $opts->{"linesep"}
| |||||||||||||||||||||||||||||||||
| 169 | 250 | ▶ | my $gdecl = $graph->is_undirected ? 'graph' : 'digraph'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | 250 | ▶ | my $gname = exists $opts->{'name'} ? '"' . $opts->{'name'} . '"' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 171 | : 'stemma'; | ||||||||||||||||||||||||||||||||
| 172 | 250 | my @dotlines; | |||||||||||||||||||||||||||||||
| 173 | 250 | push( @dotlines, "$gdecl $gname {" ); | |||||||||||||||||||||||||||||||
| 174 | 250 | my @real; # A cheap sort | |||||||||||||||||||||||||||||||
| 175 | 250 | foreach my $n ( sort $graph->vertices ) { | |||||||||||||||||||||||||||||||
| 176 | 3502 | my $c = $graph->get_vertex_attribute( $n, 'class' ); | |||||||||||||||||||||||||||||||
| 177 | 3502 | ▶ | $c = 'extant' unless $c; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | 3502 | ▶ | if( $c eq 'extant' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | 3114 | push( @real, $n ); | |||||||||||||||||||||||||||||||
| 180 | } else { | ||||||||||||||||||||||||||||||||
| 181 | 388 | push( @dotlines, _make_dotline( $n, 'class' => $c ) ); | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||
| 184 | # Now do the real ones | ||||||||||||||||||||||||||||||||
| 185 | 250 | foreach my $n ( @real ) { | |||||||||||||||||||||||||||||||
| 186 | 3114 | push( @dotlines, _make_dotline( $n, 'class' => 'extant' ) ); | |||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||
| 188 | 250 | foreach my $e ( sort _by_vertex $graph->edges ) { | |||||||||||||||||||||||||||||||
| 189 | 3500 | my( $from, $to ) = map { _dotquote( $_ ) } @$e; | |||||||||||||||||||||||||||||||
| 190 | 3500 | ▶ | my $conn = $graph->is_undirected ? '--' : '->'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 191 | 3500 | push( @dotlines, " $from $conn $to;" ); | |||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | 250 | push( @dotlines, '}' ); | |||||||||||||||||||||||||||||||
| 194 | 250 | return join( $join, @dotlines ); | |||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | 3510 | ▶ | sub _make_dotline { | ||||||||||||||||||||||||||||||
|
Subroutine
_make_dotline: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 198 | 3510 | my( $obj, %attr ) = @_; | |||||||||||||||||||||||||||||||
| 199 | 3510 | my @pairs; | |||||||||||||||||||||||||||||||
| 200 | 3510 | foreach my $k ( keys %attr ) { | |||||||||||||||||||||||||||||||
| 201 | 3514 | my $v = _dotquote( $attr{$k} ); | |||||||||||||||||||||||||||||||
| 202 | 3514 | push( @pairs, "$k=$v" ); | |||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||
| 204 | 3510 | return sprintf( " %s [ %s ];", _dotquote( $obj ), join( ', ', @pairs ) ); | |||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | 14032 | ▶ | sub _dotquote { | ||||||||||||||||||||||||||||||
|
Subroutine
_dotquote: called CC=2 SCAR=10.1
| |||||||||||||||||||||||||||||||||
| 208 | 14032 | my( $str ) = @_; | |||||||||||||||||||||||||||||||
| 209 | 14032 | ▶ | return $str if $str =~ /^[A-Za-z0-9_]+$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 210 | 0 | $str =~ s/\"/\\\"/g; | |||||||||||||||||||||||||||||||
| 211 | 0 | $str = '"' . $str . '"'; | |||||||||||||||||||||||||||||||
| 212 | 0 | return $str; | |||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||
| 215 | 8507 | ▶ | sub _by_vertex { | ||||||||||||||||||||||||||||||
|
Subroutine
_by_vertex: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 216 | 8507 | return $a->[0].$a->[1] cmp $b->[0].$b->[1]; | |||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | =head2 character_input( $tradition, $opts ) | ||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | Returns a character matrix string suitable for Phylip programs, which | ||||||||||||||||||||||||||||||||
| 222 | corresponds to the given alignment table. See Text::Tradition::Collation | ||||||||||||||||||||||||||||||||
| 223 | for a description of the alignment table format. Options include: | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | =over | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | =item * exclude_layer - Exclude layered witnesses from the character input, | ||||||||||||||||||||||||||||||||
| 228 | using only the 'main' text of the witnesses in the tradition. | ||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||
| 230 | =item * collapse - A reference to an array of relationship names that should | ||||||||||||||||||||||||||||||||
| 231 | be treated as equivalent for the purposes of generating the character matrix. | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | =back | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | =cut | ||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | 1 | ▶ | sub character_input { | ||||||||||||||||||||||||||||||
|
Subroutine
character_input: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 238 | 1 | my ( $tradition, $opts ) = @_; | |||||||||||||||||||||||||||||||
| 239 | 1 | my $table = $tradition->collation->alignment_table; | |||||||||||||||||||||||||||||||
| 240 | 1 | ▶ | if( $opts->{exclude_layer} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 241 | # Filter out all alignment table rows that do not correspond | ||||||||||||||||||||||||||||||||
| 242 | # to a named witness - these are the layered witnesses. | ||||||||||||||||||||||||||||||||
| 243 | 0 | my $newtable = { alignment => [], length => $table->{length} }; | |||||||||||||||||||||||||||||||
| 244 | 0 | foreach my $row ( @{$table->{alignment}} ) { | |||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | if( $tradition->has_witness( $row->{witness} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 246 | 0 | push( @{$newtable->{alignment}}, $row ); | |||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||
| 249 | 0 | $table = $newtable; | |||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||
| 251 | 1 | my $character_matrix = _make_character_matrix( $table, $opts ); | |||||||||||||||||||||||||||||||
| 252 | 1 | my $input = ''; | |||||||||||||||||||||||||||||||
| 253 | 1 | my $rows = scalar @{$character_matrix}; | |||||||||||||||||||||||||||||||
| 254 | 1 | my $columns = scalar @{$character_matrix->[0]} - 1; | |||||||||||||||||||||||||||||||
| 255 | 1 | $input .= "\t$rows\t$columns\n"; | |||||||||||||||||||||||||||||||
| 256 | 1 | foreach my $row ( @{$character_matrix} ) { | |||||||||||||||||||||||||||||||
| 257 | 3 | $input .= join( '', @$row ) . "\n"; | |||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||
| 259 | 1 | return $input; | |||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | 1 | ▶ | sub _make_character_matrix { | ||||||||||||||||||||||||||||||
|
Subroutine
_make_character_matrix: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 263 | 1 | my( $table, $opts ) = @_; | |||||||||||||||||||||||||||||||
| 264 | # Push the names of the witnesses to initialize the rows of the matrix. | ||||||||||||||||||||||||||||||||
| 265 | 3 | my @matrix = map { [ _normalize_witname( $_->{'witness'} ) ] } | |||||||||||||||||||||||||||||||
| 266 | 1 | @{$table->{'alignment'}}; | |||||||||||||||||||||||||||||||
| 267 | 1 | foreach my $token_index ( 0 .. $table->{'length'} - 1) { | |||||||||||||||||||||||||||||||
| 268 | 54 | my @pos_tokens = map { $_->{'tokens'}->[$token_index] } | |||||||||||||||||||||||||||||||
| 269 | 18 | @{$table->{'alignment'}}; | |||||||||||||||||||||||||||||||
| 270 | 18 | ▶ | my @pos_readings = map { $_ ? $_->{'t'} : $_ } @pos_tokens; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 271 | 18 | my @chars = _convert_characters( \@pos_readings, $opts ); | |||||||||||||||||||||||||||||||
| 272 | 18 | foreach my $idx ( 0 .. $#matrix ) { | |||||||||||||||||||||||||||||||
| 273 | 54 | push( @{$matrix[$idx]}, $chars[$idx] ); | |||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||
| 276 | 1 | return \@matrix; | |||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | # Helper function to make the witness name something legal for pars | ||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | 3 | ▶ | sub _normalize_witname { | ||||||||||||||||||||||||||||||
|
Subroutine
_normalize_witname: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 282 | 3 | my( $witname ) = @_; | |||||||||||||||||||||||||||||||
| 283 | 3 | $witname =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||
| 284 | 3 | $witname =~ s/[\[\]\(\)\:;,]//g; | |||||||||||||||||||||||||||||||
| 285 | 3 | $witname = substr( $witname, 0, 10 ); | |||||||||||||||||||||||||||||||
| 286 | 3 | return sprintf( "%-10s", $witname ); | |||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | 18 | ▶ | sub _convert_characters { | ||||||||||||||||||||||||||||||
|
Subroutine
_convert_characters: called CC=11 SCAR=31.3
| |||||||||||||||||||||||||||||||||
| 290 | 18 | my( $row, $opts ) = @_; | |||||||||||||||||||||||||||||||
| 291 | # This is a simple algorithm that treats every reading as different. | ||||||||||||||||||||||||||||||||
| 292 | # Eventually we will want to be able to specify how relationships | ||||||||||||||||||||||||||||||||
| 293 | # affect the character matrix. | ||||||||||||||||||||||||||||||||
| 294 | 18 | my %unique = ( '__UNDEF__' => 'X', | |||||||||||||||||||||||||||||||
| 295 | '#LACUNA#' => '?', | ||||||||||||||||||||||||||||||||
| 296 | ); | ||||||||||||||||||||||||||||||||
| 297 | 18 | my %equivalent; | |||||||||||||||||||||||||||||||
| 298 | my %count; | ||||||||||||||||||||||||||||||||
| 299 | 18 | my $ctr = 0; | |||||||||||||||||||||||||||||||
| 300 | 18 | foreach my $rdg ( @$row ) { | |||||||||||||||||||||||||||||||
| 301 | 54 | ▶ | next unless $rdg; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 302 | 46 | ▶ | next if $rdg->is_lacuna; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 303 | 46 | ▶ | next if exists $unique{$rdg->text}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 304 | 24 | ▶ | if( ref( $opts->{'collapse'} ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 305 | 0 | my @exclude_types = @{$opts->{'collapse'}}; | |||||||||||||||||||||||||||||||
| 306 | 0 | ▶ | my @set = $rdg->related_readings( sub { my $rel = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | $rel->colocated && grep { $rel->type eq $_ } @exclude_types } ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 308 | 0 | push( @set, $rdg ); | |||||||||||||||||||||||||||||||
| 309 | 0 | my $char = chr( 65 + $ctr++ ); | |||||||||||||||||||||||||||||||
| 310 | 0 | map { $unique{$_->text} = $char } @set; | |||||||||||||||||||||||||||||||
| 311 | 0 | $count{$rdg->text} += scalar @set; | |||||||||||||||||||||||||||||||
| 312 | } else { | ||||||||||||||||||||||||||||||||
| 313 | 24 | $unique{$rdg->text} = chr( 65 + $ctr++ ); | |||||||||||||||||||||||||||||||
| 314 | 24 | $count{$rdg->text}++; | |||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||
| 317 | # Try to keep variants under 8 by lacunizing any singletons. | ||||||||||||||||||||||||||||||||
| 318 | 18 | ▶ | if( scalar( keys %unique ) > 8 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 319 | 0 | foreach my $word ( keys %count ) { | |||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | if( $count{$word} == 1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 321 | 0 | $unique{$word} = '?'; | |||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||
| 325 | 18 | my %u = reverse %unique; | |||||||||||||||||||||||||||||||
| 326 | 18 | ▶ | if( scalar( keys %u ) > 8 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 327 | 0 | warn "Have more than 8 variants on this location; phylip will break"; | |||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||
| 329 | 18 | ▶ | my @chars = map { $_ ? $unique{$_->text} : $unique{'__UNDEF__' } } @$row; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 330 | 18 | return @chars; | |||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | =head2 phylip_pars( $character_matrix ) | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | Runs Phylip Pars on the given character matrix. Returns results in Newick format. | ||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||
| 337 | =cut | ||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | sub phylip_pars { | ||||||||||||||||||||||||||||||
|
Subroutine
phylip_pars: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 340 | 0 | my( $charmatrix, $opts ) = @_; | |||||||||||||||||||||||||||||||
| 341 | # Set up a temporary directory for all the default Phylip files. | ||||||||||||||||||||||||||||||||
| 342 | 0 | my $phylip_dir = File::Temp->newdir(); | |||||||||||||||||||||||||||||||
| 343 | # $phylip_dir->unlink_on_destroy(0); | ||||||||||||||||||||||||||||||||
| 344 | # We need an infile, and we need a command input file. | ||||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | open( MATRIX, ">$phylip_dir/infile" ) or die "Could not write $phylip_dir/infile"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 346 | 0 | print MATRIX $charmatrix; | |||||||||||||||||||||||||||||||
| 347 | 0 | close MATRIX; | |||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||
| 349 | 0 | ▶ | open( CMD, ">$phylip_dir/cmdfile" ) or die "Could not write $phylip_dir/cmdfile"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 350 | ## TODO any configuration parameters we want to set here | ||||||||||||||||||||||||||||||||
| 351 | # U Search for best tree? Yes | ||||||||||||||||||||||||||||||||
| 352 | # S Search option? More thorough search | ||||||||||||||||||||||||||||||||
| 353 | # V Number of trees to save? 100 | ||||||||||||||||||||||||||||||||
| 354 | # J Randomize input order of species? No. Use input order | ||||||||||||||||||||||||||||||||
| 355 | # O Outgroup root? No, use as outgroup species 1 | ||||||||||||||||||||||||||||||||
| 356 | # T Use Threshold parsimony? No, use ordinary parsimony | ||||||||||||||||||||||||||||||||
| 357 | # W Sites weighted? No | ||||||||||||||||||||||||||||||||
| 358 | # M Analyze multiple data sets? No | ||||||||||||||||||||||||||||||||
| 359 | # I Input species interleaved? Yes | ||||||||||||||||||||||||||||||||
| 360 | # 0 Terminal type (IBM PC, ANSI, none)? ANSI | ||||||||||||||||||||||||||||||||
| 361 | # 1 Print out the data at start of run No | ||||||||||||||||||||||||||||||||
| 362 | # 2 Print indications of progress of run Yes | ||||||||||||||||||||||||||||||||
| 363 | # 3 Print out tree Yes | ||||||||||||||||||||||||||||||||
| 364 | # 4 Print out steps in each site No | ||||||||||||||||||||||||||||||||
| 365 | # 5 Print character at all nodes of tree No | ||||||||||||||||||||||||||||||||
| 366 | # 6 Write out trees onto tree file? Yes | ||||||||||||||||||||||||||||||||
| 367 | 0 | print CMD "Y\n"; | |||||||||||||||||||||||||||||||
| 368 | 0 | close CMD; | |||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||
| 370 | # And then we run the program. | ||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | my $program = $opts->{parspath} || File::Which::which( 'pars' ); | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$opts->{'parspath'} || File::Which::which('pars')
MC/DC0%0/2
$opts->{'parspath'} || File::Which::which('pars')
$opts->{'parspath'}
File::Which::which('pars')
Truth table0%0/3
A || B
A= $opts->{'parspath'}B= File::Which::which('pars')
| |||||||||||||||||||||||||||||||||
| 372 | 0 | ▶ | unless( $program && -x $program ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$program and -x $program
MC/DC0%0/2
$program and -x $program
$program
-x $program
Truth table0%0/3
A and B
A= $programB= -x $program
| |||||||||||||||||||||||||||||||||
| 373 | 0 | throw( "Phylip pars $program not executable" ); | |||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||
| 376 | { | ||||||||||||||||||||||||||||||||
| 377 | # We need to run it in our temporary directory where we have created | ||||||||||||||||||||||||||||||||
| 378 | # all the expected files. | ||||||||||||||||||||||||||||||||
| 379 | 0 | local $CWD = $phylip_dir; | |||||||||||||||||||||||||||||||
| 380 | 0 | my @cmd = ( $program ); | |||||||||||||||||||||||||||||||
| 381 | 0 | run \@cmd, '<', 'cmdfile', '>', '/dev/null'; | |||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||
| 383 | # Now our output should be in 'outfile' and our tree in 'outtree', | ||||||||||||||||||||||||||||||||
| 384 | # both in the temp directory. | ||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||
| 386 | 0 | my @outtree; | |||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | if( -f "$phylip_dir/outtree" ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 388 | 0 | ▶ | open( TREE, "$phylip_dir/outtree" ) or die "Could not open outtree for read"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 389 | 0 | @outtree = <TREE>; | |||||||||||||||||||||||||||||||
| 390 | 0 | close TREE; | |||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||
| 392 | 0 | ▶ | return join( '', @outtree ) if @outtree; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||
| 394 | # If we got this far, we are about to throw an error. | ||||||||||||||||||||||||||||||||
| 395 | 0 | my @error; | |||||||||||||||||||||||||||||||
| 396 | 0 | ▶ | if( -f "$phylip_dir/outfile" ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 397 | 0 | ▶ | open( OUTPUT, "$phylip_dir/outfile" ) or die "Could not open output for read"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 398 | 0 | @error = <OUTPUT>; | |||||||||||||||||||||||||||||||
| 399 | 0 | close OUTPUT; | |||||||||||||||||||||||||||||||
| 400 | } else { | ||||||||||||||||||||||||||||||||
| 401 | 0 | push( @error, "Neither outtree nor output file was produced!" ); | |||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||
| 403 | 0 | throw( join( '', @error ) ); | |||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||
| 406 | =head2 parse_newick( $newick_string ) | ||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||
| 408 | Parses the given Newick tree(s) into one or more Stemma objects with | ||||||||||||||||||||||||||||||||
| 409 | undirected graphs. | ||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||
| 411 | =cut | ||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | 1 | ▶ | sub parse_newick { | ||||||||||||||||||||||||||||||
|
Subroutine
parse_newick: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 414 | 1 | my $newick = shift; | |||||||||||||||||||||||||||||||
| 415 | # Parse the result into a set of trees and return them. | ||||||||||||||||||||||||||||||||
| 416 | 1 | my $forest = Bio::Phylo::IO->parse( | |||||||||||||||||||||||||||||||
| 417 | -format => 'newick', | ||||||||||||||||||||||||||||||||
| 418 | -string => $newick, | ||||||||||||||||||||||||||||||||
| 419 | ); | ||||||||||||||||||||||||||||||||
| 420 | 1 | return map { _graph_from_bio( $_ ) } @{$forest->get_entities}; | |||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | 1 | ▶ | sub _graph_from_bio { | ||||||||||||||||||||||||||||||
|
Subroutine
_graph_from_bio: called CC=6 SCAR=18.5
| |||||||||||||||||||||||||||||||||
| 424 | 1 | my $tree = shift; | |||||||||||||||||||||||||||||||
| 425 | 1 | my $graph = Graph->new( 'undirected' => 1 ); | |||||||||||||||||||||||||||||||
| 426 | # Give all the intermediate anonymous nodes a name. | ||||||||||||||||||||||||||||||||
| 427 | 1 | my $i = 0; | |||||||||||||||||||||||||||||||
| 428 | 1 | my $classes = {}; | |||||||||||||||||||||||||||||||
| 429 | 1 | foreach my $n ( @{$tree->get_terminals} ) { | |||||||||||||||||||||||||||||||
| 430 | # The terminal nodes are our named witnesses. | ||||||||||||||||||||||||||||||||
| 431 | 13 | $classes->{$n->get_name} = 'extant'; | |||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||
| 433 | 1 | foreach my $n ( @{$tree->get_internals} ) { | |||||||||||||||||||||||||||||||
| 434 | 12 | ▶ | unless( defined $n->get_name && $n->get_name ne '' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $n->get_name and $n->get_name ne ""
MC/DC0%0/2
defined $n->get_name and $n->get_name ne ""
defined $n->get_name
$n->get_name ne ""
Truth table33%1/3
A and B
A= defined $n->get_nameB= $n->get_name ne ""
| |||||||||||||||||||||||||||||||||
| 435 | # Get an integer, make sure it's a unique name | ||||||||||||||||||||||||||||||||
| 436 | 12 | ▶ | while( exists $classes->{$i} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 437 | 0 | $i++; | |||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||
| 439 | 12 | $n->set_name( $i++ ); | |||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||
| 441 | 12 | $classes->{$n->get_name} = 'hypothetical'; | |||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||
| 443 | 1 | _add_tree_children( $graph, $classes, undef, [ $tree->get_root ]); | |||||||||||||||||||||||||||||||
| 444 | 1 | return $graph; | |||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||
| 447 | 26 | ▶ | sub _add_tree_children { | ||||||||||||||||||||||||||||||
|
Subroutine
_add_tree_children: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 448 | 26 | my( $graph, $classes, $parent, $tree_children ) = @_; | |||||||||||||||||||||||||||||||
| 449 | 26 | foreach my $c ( @$tree_children ) { | |||||||||||||||||||||||||||||||
| 450 | 25 | my $child = $c->get_name; | |||||||||||||||||||||||||||||||
| 451 | 25 | $graph->add_vertex( $child ); | |||||||||||||||||||||||||||||||
| 452 | 25 | $graph->set_vertex_attribute( $child, 'class', $classes->{$child} ); | |||||||||||||||||||||||||||||||
| 453 | 25 | ▶ | $graph->add_path( $parent, $child ) if defined $parent; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 454 | 25 | _add_tree_children( $graph, $classes, $child, $c->get_children() ); | |||||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||
| 458 | =head2 newick_to_svg( $newick_string ) | ||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||
| 460 | Uses the FigTree utility (if installed) to transform the given Newick tree(s) | ||||||||||||||||||||||||||||||||
| 461 | into a graph visualization. | ||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||
| 463 | =cut | ||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||
| 465 | 0 | ▶ | sub newick_to_svg { | ||||||||||||||||||||||||||||||
|
Subroutine
newick_to_svg: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 466 | 0 | my $newick = shift; | |||||||||||||||||||||||||||||||
| 467 | 0 | my $program = File::Which::which( 'figtree' ); | |||||||||||||||||||||||||||||||
| 468 | 0 | ▶ | unless( -x $program ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 469 | 0 | throw( "FigTree commandline utility not found in path" ); | |||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||
| 471 | 0 | my $svg; | |||||||||||||||||||||||||||||||
| 472 | 0 | my $nfile = File::Temp->new(); | |||||||||||||||||||||||||||||||
| 473 | 0 | print $nfile $newick; | |||||||||||||||||||||||||||||||
| 474 | 0 | close $nfile; | |||||||||||||||||||||||||||||||
| 475 | 0 | my @cmd = ( $program, '-graphic', 'SVG', $nfile ); | |||||||||||||||||||||||||||||||
| 476 | 0 | run( \@cmd, ">", binary(), \$svg ); | |||||||||||||||||||||||||||||||
| 477 | 0 | return decode_utf8( $svg ); | |||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||
| 480 | 1 | ▶ | sub throw { | ||||||||||||||||||||||||||||||
|
Subroutine
throw: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 481 | 1 | Text::Tradition::Error->throw( | |||||||||||||||||||||||||||||||
| 482 | 'ident' => 'StemmaUtil error', | ||||||||||||||||||||||||||||||||
| 483 | 'message' => $_[0], | ||||||||||||||||||||||||||||||||
| 484 | ); | ||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||
| 487 | 5 | 1; | |||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||
| 489 | =head1 LICENSE | ||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||
| 491 | This package is free software and is provided "as is" without express | ||||||||||||||||||||||||||||||||
| 492 | or implied warranty. You can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 493 | the same terms as Perl itself. | ||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||
| 495 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||
| 497 | Tara L Andrews E<lt>aurum@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||