1 package Text::Tradition::Stemma;
4 use Encode qw( decode_utf8 );
7 use Graph::Reader::Dot;
8 use IPC::Run qw/ run binary /;
9 use Text::Tradition::Error;
10 use Text::Tradition::StemmaUtil qw/ character_input phylip_pars parse_newick /;
15 isa => 'Text::Tradition::Collation',
23 predicate => 'has_graph',
26 has distance_trees => (
28 isa => 'ArrayRef[Graph]',
29 writer => '_save_distance_trees',
30 predicate => 'has_distance_trees',
33 has distance_program => (
40 my( $self, $args ) = @_;
41 # If we have been handed a dotfile, initialize it into a graph.
42 if( exists $args->{'dot'} ) {
43 $self->graph_from_dot( $args->{'dot'} );
48 my( $self, $dotfh ) = @_;
49 my $reader = Graph::Reader::Dot->new();
50 my $graph = $reader->read_graph( $dotfh );
52 $self->graph( $graph );
53 # Go through the nodes and set any non-hypothetical node to extant.
54 foreach my $v ( $self->graph->vertices ) {
55 $self->graph->set_vertex_attribute( $v, 'class', 'extant' )
56 unless $self->graph->has_vertex_attribute( $v, 'class' );
59 throw( "Failed to parse dot in $dotfh" );
64 my( $self, $opts ) = @_;
66 # Get default and specified options
73 'fillcolor' => 'white',
75 'shape' => 'ellipse', # Shape for the extant nodes
78 'arrowhead' => 'none',
80 @graphopts{ keys %{$opts->{'graph'}} } = values %{$opts->{'graph'}}
82 @nodeopts{ keys %{$opts->{'node'}} } = values %{$opts->{'node'}}
84 @edgeopts{ keys %{$opts->{'edge'}} } = values %{$opts->{'edge'}}
88 push( @dotlines, 'digraph stemma {' );
89 ## Print out the global attributes
90 push( @dotlines, _make_dotline( 'graph', %graphopts ) ) if keys %graphopts;
91 push( @dotlines, _make_dotline( 'edge', %edgeopts ) ) if keys %edgeopts;
92 push( @dotlines, _make_dotline( 'node', %nodeopts ) ) if keys %nodeopts;
94 # Add each of the nodes.
95 foreach my $n ( $self->graph->vertices ) {
96 if( $self->graph->has_vertex_attribute( $n, 'label' ) ) {
97 my $ltext = $self->graph->get_vertex_attribute( $n, 'label' );
98 push( @dotlines, _make_dotline( $n, 'label' => $ltext ) );
100 # Use the default display settings.
101 push( @dotlines, " $n;" );
104 # Add each of our edges.
105 foreach my $e ( $self->graph->edges ) {
106 my( $from, $to ) = @$e;
107 push( @dotlines, " $from -> $to;" );
109 push( @dotlines, '}' );
111 return join( "\n", @dotlines );
115 # Another version of dot output meant for graph editing, thus
120 push( @dotlines, 'digraph stemma {' );
121 my @real; # A cheap sort
122 foreach my $n ( sort $self->graph->vertices ) {
123 my $c = $self->graph->get_vertex_attribute( $n, 'class' );
124 $c = 'extant' unless $c;
125 if( $c eq 'extant' ) {
128 push( @dotlines, _make_dotline( $n, 'class' => $c ) );
131 # Now do the real ones
132 foreach my $n ( @real ) {
133 push( @dotlines, _make_dotline( $n, 'class' => 'extant' ) );
135 foreach my $e ( sort _by_vertex $self->graph->edges ) {
136 my( $from, $to ) = @$e;
137 push( @dotlines, " $from -> $to;" );
139 push( @dotlines, '}' );
140 return join( "\n", @dotlines );
144 my( $obj, %attr ) = @_;
146 foreach my $k ( keys %attr ) {
149 push( @pairs, "$k=\"$v\"" );
151 return sprintf( " %s [ %s ];", $obj, join( ', ', @pairs ) );
155 return $a->[0].$a->[1] cmp $b->[0].$b->[1];
158 # Render the stemma as SVG.
160 my( $self, $opts ) = @_;
161 my $dot = $self->as_dot( $opts );
162 my @cmd = qw/dot -Tsvg/;
164 my $dotfile = File::Temp->new();
166 # $dotfile->unlink_on_destroy(0);
167 binmode $dotfile, ':utf8';
169 push( @cmd, $dotfile->filename );
170 run( \@cmd, ">", binary(), \$svg );
171 $svg = decode_utf8( $svg );
177 my @wits = grep { $self->graph->get_vertex_attribute( $_, 'class' ) eq 'extant' }
178 $self->graph->vertices;
182 #### Methods for calculating phylogenetic trees ####
184 before 'distance_trees' => sub {
187 'program' => 'phylip_pars',
189 # TODO allow specification of method for calculating distance tree
190 if( !$self->has_distance_trees
191 || $args{'program'} ne $self->distance_program ) {
192 # We need to make a tree before we can return it.
193 my $dsub = 'run_' . $args{'program'};
194 my( $ok, $result ) = $self->$dsub();
196 # Save the resulting trees
197 my $trees = parse_newick( $result );
198 $self->_save_distance_trees( $trees );
199 $self->distance_program( $args{'program'} );
201 throw( "Failed to calculate distance trees: $result" );
206 sub run_phylip_pars {
208 my $cdata = character_input( $self->collation->make_alignment_table() );
209 return phylip_pars( $cdata );
213 Text::Tradition::Error->throw(
214 'ident' => 'Stemma error',
221 __PACKAGE__->meta->make_immutable;