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::StemmaUtil qw/ character_input phylip_pars parse_newick /;
14 isa => 'Text::Tradition::Collation',
22 predicate => 'has_graph',
25 has distance_trees => (
27 isa => 'ArrayRef[Graph]',
28 writer => '_save_distance_trees',
29 predicate => 'has_distance_trees',
32 has distance_program => (
39 my( $self, $args ) = @_;
40 # If we have been handed a dotfile, initialize it into a graph.
41 if( exists $args->{'dot'} ) {
42 $self->graph_from_dot( $args->{'dot'} );
47 my( $self, $dotfh ) = @_;
49 binmode( $dotfh, ':utf8' );
50 my $reader = Graph::Reader::Dot->new();
51 my $graph = $reader->read_graph( $dotfh );
53 $self->graph( $graph );
54 # Go through the nodes and set any non-hypothetical node to extant.
55 foreach my $v ( $self->graph->vertices ) {
56 $self->graph->set_vertex_attribute( $v, 'class', 'extant' )
57 unless $self->graph->has_vertex_attribute( $v, 'class' );
60 warn "Failed to parse dot in $dotfh";
65 my( $self, $opts ) = @_;
67 # Get default and specified options
71 'hshape' => 'plaintext', # Shape for the hypothetical nodes
74 'fillcolor' => 'white',
75 'shape' => 'ellipse', # Shape for the extant nodes
78 'arrowhead' => 'open',
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 ## Delete our special attributes from the node set before continuing
93 my $hshape = delete $nodeopts{'hshape'};
94 my $htext = delete $nodeopts{'htext'};
95 push( @dotlines, _make_dotline( 'node', %nodeopts ) ) if keys %nodeopts;
97 # Add each of the nodes.
98 foreach my $n ( $self->graph->vertices ) {
99 if( $self->graph->get_vertex_attribute( $n, 'class' ) eq 'hypothetical' ) {
100 # Apply our display settings for hypothetical nodes.
101 push( @dotlines, _make_dotline( $n, 'shape' => $hshape, 'label' => $htext ) );
103 # Use the default display settings.
104 push( @dotlines, " $n;" );
107 # Add each of our edges.
108 foreach my $e ( $self->graph->edges ) {
109 my( $from, $to ) = @$e;
110 push( @dotlines, " $from -> $to;" );
112 push( @dotlines, '}' );
114 return join( "\n", @dotlines );
118 # Another version of dot output meant for graph editing, thus
123 push( @dotlines, 'digraph stemma {' );
124 my @real; # A cheap sort
125 foreach my $n ( sort $self->graph->vertices ) {
126 my $c = $self->graph->get_vertex_attribute( $n, 'class' );
127 $c = 'extant' unless $c;
128 if( $c eq 'extant' ) {
131 push( @dotlines, _make_dotline( $n, 'class' => $c ) );
134 # Now do the real ones
135 foreach my $n ( @real ) {
136 push( @dotlines, _make_dotline( $n, 'class' => 'extant' ) );
138 foreach my $e ( sort _by_vertex $self->graph->edges ) {
139 my( $from, $to ) = @$e;
140 push( @dotlines, " $from -> $to;" );
142 push( @dotlines, '}' );
143 return join( "\n", @dotlines );
147 my( $obj, %attr ) = @_;
149 foreach my $k ( keys %attr ) {
152 push( @pairs, "$k=\"$v\"" );
154 return sprintf( " %s [ %s ];", $obj, join( ', ', @pairs ) );
158 return $a->[0].$a->[1] cmp $b->[0].$b->[1];
161 # Render the stemma as SVG.
163 my( $self, $opts ) = @_;
164 my $dot = $self->as_dot( $opts );
165 my @cmd = qw/dot -Tsvg/;
167 my $dotfile = File::Temp->new();
169 # $dotfile->unlink_on_destroy(0);
170 binmode $dotfile, ':utf8';
172 push( @cmd, $dotfile->filename );
173 run( \@cmd, ">", binary(), \$svg );
174 $svg = decode_utf8( $svg );
180 my @wits = grep { $self->graph->get_vertex_attribute( $_, 'class' ) eq 'extant' }
181 $self->graph->vertices;
185 #### Methods for calculating phylogenetic trees ####
187 before 'distance_trees' => sub {
190 'program' => 'phylip_pars',
192 # TODO allow specification of method for calculating distance tree
193 if( !$self->has_distance_trees
194 || $args{'program'} ne $self->distance_program ) {
195 # We need to make a tree before we can return it.
196 my $dsub = 'run_' . $args{'program'};
197 my( $ok, $result ) = $self->$dsub();
199 # Save the resulting trees
200 my $trees = parse_newick( $result );
201 $self->_save_distance_trees( $trees );
202 $self->distance_program( $args{'program'} );
204 warn "Failed to calculate distance trees: $result";
209 sub run_phylip_pars {
211 my $cdata = character_input( $self->collation->make_alignment_table() );
212 return phylip_pars( $cdata );
216 __PACKAGE__->meta->make_immutable;