Commit | Line | Data |
9463b0bf |
1 | package Text::Tradition::Stemma; |
2 | |
40f19742 |
3 | use Bio::Phylo::IO; |
e79c23c7 |
4 | use Encode qw( decode_utf8 ); |
9463b0bf |
5 | use File::Temp; |
e05997e2 |
6 | use Graph; |
7 | use Graph::Reader::Dot; |
e79c23c7 |
8 | use IPC::Run qw/ run binary /; |
63778331 |
9 | use Text::Tradition::Error; |
b02332ca |
10 | use Text::Tradition::StemmaUtil qw/ character_input phylip_pars parse_newick /; |
40f19742 |
11 | use Moose; |
9463b0bf |
12 | |
13 | has collation => ( |
14 | is => 'ro', |
15 | isa => 'Text::Tradition::Collation', |
16 | required => 1, |
8d9a1cd8 |
17 | weak_ref => 1, |
9463b0bf |
18 | ); |
19 | |
e05997e2 |
20 | has graph => ( |
21 | is => 'rw', |
22 | isa => 'Graph', |
23 | predicate => 'has_graph', |
24 | ); |
25 | |
40f19742 |
26 | has distance_trees => ( |
27 | is => 'ro', |
28 | isa => 'ArrayRef[Graph]', |
29 | writer => '_save_distance_trees', |
30 | predicate => 'has_distance_trees', |
31 | ); |
c0ccdb62 |
32 | |
0f5d05c6 |
33 | has distance_program => ( |
34 | is => 'rw', |
35 | isa => 'Str', |
36 | default => '', |
37 | ); |
38 | |
e05997e2 |
39 | sub BUILD { |
40 | my( $self, $args ) = @_; |
41 | # If we have been handed a dotfile, initialize it into a graph. |
42 | if( exists $args->{'dot'} ) { |
8d9a1cd8 |
43 | $self->graph_from_dot( $args->{'dot'} ); |
e05997e2 |
44 | } |
c0ccdb62 |
45 | } |
46 | |
8d9a1cd8 |
47 | sub graph_from_dot { |
48 | my( $self, $dotfh ) = @_; |
8d9a1cd8 |
49 | my $reader = Graph::Reader::Dot->new(); |
50 | my $graph = $reader->read_graph( $dotfh ); |
7a7c249c |
51 | if( $graph ) { |
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' ); |
57 | } |
58 | } else { |
63778331 |
59 | throw( "Failed to parse dot in $dotfh" ); |
7a7c249c |
60 | } |
8d9a1cd8 |
61 | } |
62 | |
63 | sub as_dot { |
e367f5c0 |
64 | my( $self, $opts ) = @_; |
7a7c249c |
65 | |
66 | # Get default and specified options |
e02340f3 |
67 | my %graphopts = ( |
68 | # 'ratio' => 1, |
69 | ); |
7a7c249c |
70 | my %nodeopts = ( |
71 | 'fontsize' => 11, |
7a7c249c |
72 | 'style' => 'filled', |
73 | 'fillcolor' => 'white', |
e02340f3 |
74 | 'color' => 'white', |
7a7c249c |
75 | 'shape' => 'ellipse', # Shape for the extant nodes |
76 | ); |
77 | my %edgeopts = ( |
e02340f3 |
78 | 'arrowhead' => 'none', |
7a7c249c |
79 | ); |
80 | @graphopts{ keys %{$opts->{'graph'}} } = values %{$opts->{'graph'}} |
81 | if $opts->{'graph'}; |
82 | @nodeopts{ keys %{$opts->{'node'}} } = values %{$opts->{'node'}} |
83 | if $opts->{'node'}; |
84 | @edgeopts{ keys %{$opts->{'edge'}} } = values %{$opts->{'edge'}} |
85 | if $opts->{'edge'}; |
86 | |
87 | my @dotlines; |
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; |
7a7c249c |
92 | push( @dotlines, _make_dotline( 'node', %nodeopts ) ) if keys %nodeopts; |
93 | |
94 | # Add each of the nodes. |
95 | foreach my $n ( $self->graph->vertices ) { |
e02340f3 |
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 ) ); |
e79c23c7 |
99 | } else { |
7a7c249c |
100 | # Use the default display settings. |
101 | push( @dotlines, " $n;" ); |
e79c23c7 |
102 | } |
103 | } |
7a7c249c |
104 | # Add each of our edges. |
105 | foreach my $e ( $self->graph->edges ) { |
106 | my( $from, $to ) = @$e; |
107 | push( @dotlines, " $from -> $to;" ); |
108 | } |
109 | push( @dotlines, '}' ); |
e79c23c7 |
110 | |
7a7c249c |
111 | return join( "\n", @dotlines ); |
112 | } |
113 | |
114 | |
115 | # Another version of dot output meant for graph editing, thus |
116 | # much simpler. |
117 | sub editable { |
118 | my $self = shift; |
119 | my @dotlines; |
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' ) { |
126 | push( @real, $n ); |
127 | } else { |
128 | push( @dotlines, _make_dotline( $n, 'class' => $c ) ); |
129 | } |
e367f5c0 |
130 | } |
7a7c249c |
131 | # Now do the real ones |
132 | foreach my $n ( @real ) { |
133 | push( @dotlines, _make_dotline( $n, 'class' => 'extant' ) ); |
134 | } |
135 | foreach my $e ( sort _by_vertex $self->graph->edges ) { |
136 | my( $from, $to ) = @$e; |
137 | push( @dotlines, " $from -> $to;" ); |
138 | } |
139 | push( @dotlines, '}' ); |
140 | return join( "\n", @dotlines ); |
141 | } |
142 | |
143 | sub _make_dotline { |
144 | my( $obj, %attr ) = @_; |
145 | my @pairs; |
146 | foreach my $k ( keys %attr ) { |
147 | my $v = $attr{$k}; |
148 | $v =~ s/\"/\\\"/g; |
149 | push( @pairs, "$k=\"$v\"" ); |
150 | } |
151 | return sprintf( " %s [ %s ];", $obj, join( ', ', @pairs ) ); |
8d9a1cd8 |
152 | } |
153 | |
7a7c249c |
154 | sub _by_vertex { |
155 | return $a->[0].$a->[1] cmp $b->[0].$b->[1]; |
156 | } |
8d9a1cd8 |
157 | |
158 | # Render the stemma as SVG. |
159 | sub as_svg { |
160 | my( $self, $opts ) = @_; |
161 | my $dot = $self->as_dot( $opts ); |
e79c23c7 |
162 | my @cmd = qw/dot -Tsvg/; |
163 | my( $svg, $err ); |
164 | my $dotfile = File::Temp->new(); |
165 | ## TODO REMOVE |
166 | # $dotfile->unlink_on_destroy(0); |
167 | binmode $dotfile, ':utf8'; |
8d9a1cd8 |
168 | print $dotfile $dot; |
e79c23c7 |
169 | push( @cmd, $dotfile->filename ); |
170 | run( \@cmd, ">", binary(), \$svg ); |
171 | $svg = decode_utf8( $svg ); |
172 | return $svg; |
173 | } |
174 | |
08e0fb85 |
175 | sub witnesses { |
176 | my $self = shift; |
177 | my @wits = grep { $self->graph->get_vertex_attribute( $_, 'class' ) eq 'extant' } |
178 | $self->graph->vertices; |
179 | return @wits; |
180 | } |
181 | |
e79c23c7 |
182 | #### Methods for calculating phylogenetic trees #### |
183 | |
40f19742 |
184 | before 'distance_trees' => sub { |
185 | my $self = shift; |
0f5d05c6 |
186 | my %args = ( |
187 | 'program' => 'phylip_pars', |
188 | @_ ); |
40f19742 |
189 | # TODO allow specification of method for calculating distance tree |
0f5d05c6 |
190 | if( !$self->has_distance_trees |
191 | || $args{'program'} ne $self->distance_program ) { |
40f19742 |
192 | # We need to make a tree before we can return it. |
0f5d05c6 |
193 | my $dsub = 'run_' . $args{'program'}; |
69403daa |
194 | my $result = $self->$dsub(); # this might throw an error - catch it? |
195 | # Save the resulting trees |
196 | my $trees = parse_newick( $result ); |
197 | $self->_save_distance_trees( $trees ); |
198 | $self->distance_program( $args{'program'} ); |
40f19742 |
199 | } |
200 | }; |
f6066bac |
201 | |
40f19742 |
202 | sub run_phylip_pars { |
b02332ca |
203 | my $self = shift; |
204 | my $cdata = character_input( $self->collation->make_alignment_table() ); |
205 | return phylip_pars( $cdata ); |
40f19742 |
206 | } |
207 | |
63778331 |
208 | sub throw { |
209 | Text::Tradition::Error->throw( |
210 | 'ident' => 'Stemma error', |
211 | 'message' => $_[0], |
212 | ); |
213 | } |
214 | |
215 | |
9463b0bf |
216 | no Moose; |
217 | __PACKAGE__->meta->make_immutable; |
218 | |
219 | 1; |