1 package Text::Tradition::Stemma;
3 use Encode qw( decode_utf8 );
6 use Graph::Reader::Dot;
7 use IPC::Run qw/ run binary /;
8 use Text::Tradition::Error;
13 Text::Tradition::Stemma - a representation of a I<stemma codicum> for a Text::Tradition
18 my $t = Text::Tradition->new(
19 'name' => 'this is a text',
21 'file' => '/path/to/tei_parallel_seg_file.xml' );
23 my $s = $tradition->add_stemma( dotfile => '/path/to/stemma.dot' );
27 Text::Tradition is a library for representation and analysis of collated
28 texts, particularly medieval ones. The Stemma is a representation of the
29 copying relationships between the witnesses in a Tradition, modelled with
30 a connected rooted directed acyclic graph (CRDAG).
34 The easiest way to define a stemma is to use a special form of the 'dot'
37 Each stemma opens with the line
39 digraph "Name of Stemma" {
41 and continues with a list of all manuscript witnesses in the stemma, whether
42 extant witnesses or missing archetypes or hyparchetypes. Each of these is
43 listed by its sigil on its own line, e.g.:
45 alpha [ class=hypothetical ]
46 1 [ class=hypothetical,label=* ]
49 Extant witnesses are listed with class=extant; missing or postulated witnesses
50 are listed with class=hypothetical. Anonymous hyparchetypes must be given a
51 unique name or number, but can be represented as anonymous with the addition
52 of 'label=*' to their lines. Greek letters or other special characters may be
53 used as names, but they must always be wrapped in double quotes.
55 Links between manuscripts are then listed with arrow notation, as below. These
56 lines show the direction of copying, one step at a time, for the entire stemma.
61 The final line in the definition should be the closing brace:
65 Thus for a set of extant manuscripts A, B, and C, where A and B were copied
66 from the archetype O and C was copied from B, the definition would be:
68 digraph "Test stemma 1" {
69 O [ class=hypothetical]
82 The constructor. This should generally be called from Text::Tradition, but
83 if called directly it takes the following options:
87 =item * dot - A filehandle open to a DOT representation of the stemma graph.
89 =item * graph - If no DOT specification is given, you can pass a Graph object
90 instead. The vertices of the graph should have an attribute 'class' set to
91 either of the values 'extant' or 'hypothetical'.
93 =item * is_undirected - If the graph specification (or graph object) is for an
94 undirected graph (e.g. a phylogenetic tree), this should be set.
102 use_ok( 'Text::Tradition::Stemma' );
104 # Try to create a bad graph
106 open( $baddotfh, 't/data/besoin_bad.dot' ) or die "Could not open test dotfile";
108 my $stemma = Text::Tradition::Stemma->new( dot => $baddotfh );
109 ok( 0, "Created broken stemma from dotfile with syntax error" );
110 } catch( Text::Tradition::Error $e ) {
111 like( $e->message, qr/^Error trying to parse/, "Syntax error in dot threw exception" );
114 # Create a good graph
116 open( $dotfh, 't/data/florilegium.dot' ) or die "Could not open test dotfile";
117 binmode( $dotfh, ':utf8' );
118 my $stemma = Text::Tradition::Stemma->new( dot => $dotfh );
119 is( ref( $stemma ), 'Text::Tradition::Stemma', "Created stemma from good dotfile" );
120 is( scalar $stemma->witnesses, 13, "Found correct number of extant witnesses" );
121 is( scalar $stemma->hypotheticals, 8, "Found correct number of extant hypotheticals" );
122 ok( $stemma->has_identifier, "Stemma identifier was found in dot" );
123 is( $stemma->identifier, 'Coislinianum lineage', "Correct stemma identifier was found in dot" );
124 my $found_unicode_sigil;
125 foreach my $h ( $stemma->hypotheticals ) {
126 $found_unicode_sigil = 1 if $h eq "\x{3b1}";
128 ok( $found_unicode_sigil, "Found a correctly encoded Unicode sigil" );
130 # Create an undirected graph
132 open( $undirdotfh, 't/data/besoin_undirected.dot' ) or die "Could not open test dotfile";
133 binmode( $undirdotfh, ':utf8' );
134 my $udstemma = Text::Tradition::Stemma->new( dot => $undirdotfh );
135 is( ref( $udstemma ), 'Text::Tradition::Stemma', "Created stemma from undirected dotfile" );
136 is( scalar $udstemma->witnesses, 13, "Found correct number of extant witnesses" );
137 is( scalar $udstemma->hypotheticals, 12, "Found correct number of hypotheticals" );
138 ok( $udstemma->is_undirected, "Stemma was recorded as undirected" );
146 isa => 'Text::Tradition::Collation',
147 clearer => 'clear_collation', # interim measure to remove refs in DB
154 predicate => 'has_graph',
160 writer => 'set_identifier',
161 predicate => 'has_identifier',
165 my( $self, $args ) = @_;
166 # If we have been handed a dotfile, initialize it into a graph.
167 if( exists $args->{'dot'} ) {
168 $self->_graph_from_dot( $args->{'dot'} );
172 before 'graph' => sub {
175 # Make sure all unclassed graph nodes are marked extant.
177 throw( "Cannot set graph to a non-Graph object" )
178 unless $g->isa( 'Graph' );
179 foreach my $v ( $g->vertices ) {
180 unless( $g->has_vertex_attribute( $v, 'class' ) ) {
181 $g->set_vertex_attribute( $v, 'class', 'extant' );
187 after 'graph' => sub {
190 ## HORRIBLE HACK but there is no API access to graph attributes!
191 my $graph_id = exists $_[0]->[4]->{'name'} ? $_[0]->[4]->{'name'} : '';
192 if( $graph_id && !( $self->has_identifier && $self->identifier eq $graph_id ) ) {
193 $self->set_identifier( $graph_id );
194 } elsif ( !$graph_id && $self->has_identifier ) {
195 $self->set_identifier( 'stemma' );
199 sub _graph_from_dot {
200 my( $self, $dotfh ) = @_;
201 my $reader = Graph::Reader::Dot->new();
202 # Redirect STDOUT in order to trap any error messages - syntax errors
203 # are evidently not fatal.
209 open( STDOUT, ">", \$reader_out );
211 open( STDERR, ">", \$reader_err );
212 $graph = $reader->read_graph( $dotfh );
216 if( $reader_out && $reader_out =~ /error/s ) {
217 throw( "Error trying to parse dot: $reader_out" );
219 throw( "Failed to create graph from dot" );
221 # Correct for implicit graph -> digraph quirk of reader
222 if( $reader_err && $reader_err =~ /graph will be treated as digraph/ ) {
223 my $udgraph = $graph->undirected_copy;
224 foreach my $v ( $graph->vertices ) {
225 $udgraph->set_vertex_attributes( $v, $graph->get_vertex_attributes( $v ) );
229 $self->graph( $graph );
234 return undef unless $self->has_graph;
235 return $self->graph->is_undirected;
240 =head2 as_dot( \%options )
242 Returns a normal dot representation of the stemma layout, suitable for rendering
243 with GraphViz. Options include:
247 =item * graph - A hashref of global graph options.
249 =item * node - A hashref of global node options.
251 =item * edge - A hashref of global edge options.
255 See the GraphViz documentation for the list of available options.
260 my( $self, $opts ) = @_;
262 ## See if we are including any a.c. witnesses in this graph.
263 my $graph = $self->graph;
264 if( exists $opts->{'layerwits'} ) {
266 map { $extant->{$_} = 1 } $self->witnesses;
267 $graph = $self->situation_graph( $extant, $opts->{'layerwits'} );
270 # Get default and specified options
273 'bgcolor' => 'transparent',
278 'fillcolor' => 'white',
280 'shape' => 'ellipse', # Shape for the extant nodes
283 'arrowhead' => 'none',
285 @graphopts{ keys %{$opts->{'graph'}} } = values %{$opts->{'graph'}}
287 @nodeopts{ keys %{$opts->{'node'}} } = values %{$opts->{'node'}}
289 @edgeopts{ keys %{$opts->{'edge'}} } = values %{$opts->{'edge'}}
292 my $gdecl = $graph->is_directed ? 'digraph' : 'graph';
293 my $gname = $self->has_identifier ? '"' . $self->identifier . '"'
296 push( @dotlines, "$gdecl $gname {" );
297 ## Print out the global attributes
298 push( @dotlines, _make_dotline( 'graph', %graphopts ) ) if keys %graphopts;
299 push( @dotlines, _make_dotline( 'edge', %edgeopts ) ) if keys %edgeopts;
300 push( @dotlines, _make_dotline( 'node', %nodeopts ) ) if keys %nodeopts;
302 # Add each of the nodes.
303 foreach my $n ( $graph->vertices ) {
304 my %vattr = ( 'id' => $n ); # Set the SVG element ID to the sigil itself
305 if( $graph->has_vertex_attribute( $n, 'label' ) ) {
306 $vattr{'label'} = $graph->get_vertex_attribute( $n, 'label' );
308 push( @dotlines, _make_dotline( $n, %vattr ) );
310 # Add each of our edges.
311 foreach my $e ( $graph->edges ) {
312 my( $from, $to ) = map { _dotquote( $_ ) } @$e;
313 my $connector = $graph->is_directed ? '->' : '--';
314 push( @dotlines, " $from $connector $to;" );
316 push( @dotlines, '}' );
318 return join( "\n", @dotlines );
321 =head2 alter_graph( $dotstring )
323 Alters the graph of this stemma according to the definition specified
329 my( $self, $dotstring ) = @_;
331 open $dotfh, '<', \$dotstring;
332 binmode $dotfh, ':utf8';
333 $self->_graph_from_dot( $dotfh );
336 =head2 editable( $opts )
338 =head2 editable_graph( $graph, $opts )
340 Returns a version of the graph rendered in our definition format. The
341 output separates statements with a newline; set $opts->{'linesep'} to the
342 empty string or to a space if the result is to be sent via JSON.
344 If a situational version of the stemma is required, the arguments for
345 situation_graph should be passed via $opts->{'extant'} and $opts->{'layerwits'}.
350 my( $self, $opts ) = @_;
351 my $graph = $self->graph;
352 if( $self->has_identifier ) {
353 $opts->{'name'} = $self->identifier;
355 ## See if we need an editable version of a situational graph.
356 if( exists $opts->{'layerwits'} || exists $opts->{'extant'} ) {
357 my $extant = delete $opts->{'extant'} || {};
358 my $layerwits = delete $opts->{'layerwits'} || [];
359 $graph = $self->situation_graph( $extant, $layerwits );
361 return editable_graph( $graph, $opts );
365 my( $graph, $opts ) = @_;
368 my $join = ( $opts && exists $opts->{'linesep'} ) ? $opts->{'linesep'} : "\n";
369 my $gdecl = $graph->is_undirected ? 'graph' : 'digraph';
370 my $gname = exists $opts->{'name'} ? '"' . $opts->{'name'} . '"'
373 push( @dotlines, "$gdecl $gname {" );
374 my @real; # A cheap sort
375 foreach my $n ( sort $graph->vertices ) {
376 my $c = $graph->get_vertex_attribute( $n, 'class' );
377 $c = 'extant' unless $c;
378 if( $c eq 'extant' ) {
381 push( @dotlines, _make_dotline( $n, 'class' => $c ) );
384 # Now do the real ones
385 foreach my $n ( @real ) {
386 push( @dotlines, _make_dotline( $n, 'class' => 'extant' ) );
388 foreach my $e ( sort _by_vertex $graph->edges ) {
389 my( $from, $to ) = map { _dotquote( $_ ) } @$e;
390 my $conn = $graph->is_undirected ? '--' : '->';
391 push( @dotlines, " $from $conn $to;" );
393 push( @dotlines, '}' );
394 return join( $join, @dotlines );
398 my( $obj, %attr ) = @_;
400 foreach my $k ( keys %attr ) {
401 my $v = _dotquote( $attr{$k} );
402 push( @pairs, "$k=$v" );
404 return sprintf( " %s [ %s ];", _dotquote( $obj ), join( ', ', @pairs ) );
409 return $str if $str =~ /^[A-Za-z0-9]+$/;
411 $str = '"' . $str . '"';
416 return $a->[0].$a->[1] cmp $b->[0].$b->[1];
419 =head2 situation_graph( $extant, $layered )
421 Returns a graph which is the original stemma graph with all witnesses not
422 in the %$extant hash marked as hypothetical, and witness layers added to
423 the graph according to the list in @$layered. A layered (a.c.) witness is
424 added as a parent of its main version, and additionally shares all other
425 parents and children with that version.
429 sub situation_graph {
430 my( $self, $extant, $layerwits, $layerlabel ) = @_;
432 my $graph = $self->graph->copy;
433 foreach my $vertex ( $graph->vertices ) {
434 # Set as extant any vertex that is extant in the stemma AND
435 # exists in the $extant hash.
436 my $class = 'hypothetical';
437 $class = 'extant' if exists $extant->{$vertex} && $extant->{$vertex} &&
438 $self->graph->get_vertex_attribute( $vertex, 'class' ) ne 'hypothetical';
439 $graph->set_vertex_attribute( $vertex, 'class', $class );
442 # For each 'layered' witness in the layerwits array, add it to the graph
443 # as an ancestor of the 'main' witness, and otherwise with the same parent/
444 # child links as its main analogue.
445 # TOOD Handle case where B is copied from A but corrected from C
446 $layerlabel = ' (a.c.)' unless $layerlabel;
447 foreach my $lw ( @$layerwits ) {
448 # Add the layered witness and set it with the same attributes as
449 # its 'main' analogue
450 throw( "Cannot add a layer to a hypothetical witness $lw" )
451 unless $graph->get_vertex_attribute( $lw, 'class' ) eq 'extant';
452 my $lwac = $lw . $layerlabel;
453 $graph->add_vertex( $lwac );
454 $graph->set_vertex_attributes( $lwac,
455 $graph->get_vertex_attributes( $lw ) );
457 # Set it as ancestor to the main witness
458 $graph->add_edge( $lwac, $lw );
460 # Give it the same ancestors and descendants as the main witness has,
461 # bearing in mind that those ancestors and descendants might also just
462 # have had a layered witness defined.
463 foreach my $v ( $graph->predecessors( $lw ) ) {
464 next if $v eq $lwac; # Don't add a loop
465 $graph->add_edge( $v, $lwac );
466 $graph->add_edge( $v.$layerlabel, $lwac )
467 if $graph->has_vertex( $v.$layerlabel );
469 foreach my $v ( $graph->successors( $lw ) ) {
470 next if $v eq $lwac; # but this shouldn't occur
471 $graph->add_edge( $lwac, $v );
472 $graph->add_edge( $lwac, $v.$layerlabel )
473 if $graph->has_vertex( $v.$layerlabel );
481 Returns an SVG representation of the graph, calling as_dot first.
486 my( $self, $opts ) = @_;
487 my $dot = $self->as_dot( $opts );
488 my @cmd = ( '-Tsvg' );
489 unshift( @cmd, $self->is_undirected ? 'neato' : 'dot' );
491 my $dotfile = File::Temp->new();
492 binmode $dotfile, ':utf8';
495 push( @cmd, $dotfile->filename );
496 run( \@cmd, ">", binary(), \$svg );
497 return decode_utf8( $svg );
502 Returns a list of the extant witnesses represented in the stemma.
508 my @wits = grep { $self->graph->get_vertex_attribute( $_, 'class' ) eq 'extant' }
509 $self->graph->vertices;
515 Returns a list of the hypothetical witnesses represented in the stemma.
522 { $self->graph->get_vertex_attribute( $_, 'class' ) eq 'hypothetical' }
523 $self->graph->vertices;
527 =head2 root_graph( $root_vertex )
529 If the stemma graph is undirected, make it directed with $root_vertex at the root.
530 If it is directed, re-root it.
535 my( $self, $rootvertex ) = @_;
537 my $ident = $self->identifier; # will have to restore this at the end
538 if( $self->is_undirected ) {
539 $graph = $self->graph;
541 # Make an undirected version of this graph.
542 $graph = $self->graph->undirected_copy();
544 # First, ensure that the requested root is actually a vertex in the graph.
545 unless( $graph->has_vertex( $rootvertex ) ) {
546 throw( "Cannot orient graph $graph on nonexistent vertex $rootvertex" );
549 # Now make a directed version of the graph.
550 my $rooted = Graph->new();
551 $rooted->add_vertex( $rootvertex );
552 my @next = ( $rootvertex );
555 foreach my $v ( @next ) {
556 # Place its not-placed neighbors (ergo children) in the tree
558 foreach my $n ( grep { !$rooted->has_vertex( $_ ) }
559 $graph->neighbors( $v ) ) {
560 $rooted->add_vertex( $n );
561 $rooted->add_edge( $v, $n );
562 push( @children, $n );
567 # Set the vertex classes
568 map { $rooted->set_vertex_attribute( $_, 'class', 'hypothetical' ) }
569 $self->hypotheticals;
570 map { $rooted->set_vertex_attribute( $_, 'class', 'extant' ) }
572 $self->graph( $rooted );
573 $self->set_identifier( $ident );
578 Text::Tradition::Error->throw(
579 'ident' => 'Stemma error',
586 __PACKAGE__->meta->make_immutable;
592 This package is free software and is provided "as is" without express
593 or implied warranty. You can redistribute it and/or modify it under
594 the same terms as Perl itself.
598 Tara L Andrews E<lt>aurum@cpan.orgE<gt>