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