From: Tara L Andrews Date: Sat, 1 Oct 2011 18:39:02 +0000 (+0200) Subject: provide for stemma graphic display X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e79c23c720dcef073275b5847371b832cac75dfe;p=scpubgit%2Fstemmatology.git provide for stemma graphic display --- diff --git a/group_vars.pl b/group_vars.pl index 5ffeefd..3302b1a 100644 --- a/group_vars.pl +++ b/group_vars.pl @@ -27,7 +27,7 @@ $input = join( '', @lines ); my %args = ( $informat => $input, 'linear' => $linear ); $args{'base'} = $inbase if $inbase; - my $tradition = Text::Tradition->new( %args ); +my $tradition = Text::Tradition->new( %args ); # Parse the stemma hypothesis my $stemma = Text::Tradition::Stemma->new( @@ -95,6 +95,11 @@ foreach my $i ( 0 .. $#$all_wits_table ) { } print "Found $used_vars useful variants in this analysis\n"; +# Save the stemma picture +open( STEMMA, ">stemma_graph.svg" ) or die "Could not open stemma graph to write"; +binmode STEMMA, ":utf8"; +print STEMMA $stemma->as_svg; +close STEMMA; sub analyze_variant_location { my( $group_readings, $groups, $apsp ) = @_; diff --git a/lib/Text/Tradition/Stemma.pm b/lib/Text/Tradition/Stemma.pm index a90d863..e04811c 100644 --- a/lib/Text/Tradition/Stemma.pm +++ b/lib/Text/Tradition/Stemma.pm @@ -1,11 +1,13 @@ package Text::Tradition::Stemma; use Bio::Phylo::IO; +use Encode qw( decode_utf8 ); use File::chdir; use File::Temp; use Graph; +use Graph::Convert; use Graph::Reader::Dot; -use IPC::Run qw/ run /; +use IPC::Run qw/ run binary /; use Moose; use Text::Balanced qw/ extract_bracketed /; @@ -44,8 +46,12 @@ sub BUILD { my( $self, $args ) = @_; # If we have been handed a dotfile, initialize it into a graph. if( exists $args->{'dot'} ) { + # Open the file, assume UTF-8 + open( my $dot, $args->{'dot'} ) or warn "Failed to read dot file"; + # TODO don't bother if we haven't opened + binmode $dot, ":utf8"; my $reader = Graph::Reader::Dot->new(); - my $graph = $reader->read_graph( $args->{'dot'} ); + my $graph = $reader->read_graph( $dot ); $graph ? $self->graph( $graph ) : warn "Failed to parse dot file " . $args->{'dot'}; @@ -71,6 +77,38 @@ sub BUILD { } } +# Render the stemma as SVG. +sub as_svg { + my $self = shift; + # TODO add options for display, someday + my $dgraph = Graph::Convert->as_graph_easy( $self->graph ); + # Set some class display attributes for 'hypothetical' and 'extant' nodes + $dgraph->set_attribute( 'flow', 'south' ); + foreach my $n ( $dgraph->nodes ) { + if( $n->attribute( 'class' ) eq 'hypothetical' ) { + $n->set_attribute( 'shape', 'point' ); + $n->set_attribute( 'pointshape', 'diamond' ); + } else { + $n->set_attribute( 'shape', 'ellipse' ); + } + } + + # Render to svg via graphviz + my @cmd = qw/dot -Tsvg/; + my( $svg, $err ); + my $dotfile = File::Temp->new(); + ## TODO REMOVE + # $dotfile->unlink_on_destroy(0); + binmode $dotfile, ':utf8'; + print $dotfile $dgraph->as_graphviz(); + push( @cmd, $dotfile->filename ); + run( \@cmd, ">", binary(), \$svg ); + $svg = decode_utf8( $svg ); + return $svg; +} + +#### Methods for calculating phylogenetic trees #### + before 'distance_trees' => sub { my $self = shift; my %args = @_;