From: Tara L Andrews Date: Fri, 21 Oct 2011 00:05:38 +0000 (+0200) Subject: change analysis graph calculation - closer but not correct yet. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=08e0fb850e4e867ac0be6711ec77b69718cc3958;p=scpubgit%2Fstemmatology.git change analysis graph calculation - closer but not correct yet. --- diff --git a/lib/Text/Tradition/Analysis.pm b/lib/Text/Tradition/Analysis.pm index f4605ed..90a82d8 100644 --- a/lib/Text/Tradition/Analysis.pm +++ b/lib/Text/Tradition/Analysis.pm @@ -41,8 +41,9 @@ sub run_analysis { # We have the collation, so get the alignment table with witnesses in rows. # Also return the reading objects in the table, rather than just the words. - - my $all_wits_table = $tradition->collation->make_alignment_table( 'refs' ); + my $wits = {}; + map { $wits->{$_} = 1 } $stemma->witnesses; + my $all_wits_table = $tradition->collation->make_alignment_table( 'refs', $wits ); # For each column in the alignment table, we want to see if the existing # groupings of witnesses match our stemma hypothesis. We also want, at the @@ -104,7 +105,7 @@ sub run_analysis { # TODO: deal with a.c. reading logic $DB::single = 1 if $rank == 25; my $variant_row = analyze_variant_location( $group_readings, $groups, - $stemma->apsp, $lacunose ); + $stemma->graph, $lacunose ); $variant_row->{'id'} = $rank; $genealogical++ if $variant_row->{'genealogical'}; $conflicts += grep { $_->{'conflict'} } @{$variant_row->{'readings'}}; @@ -113,8 +114,7 @@ sub run_analysis { # my @trees = @{$stemma->distance_trees}; # if( @trees ) { # foreach my $tree ( 0 .. $#trees ) { -# my $dc = analyze_variant_location( $group_readings, $groups, -# $stemma->distance_apsps->[$tree] ); +# my $dc = analyze_variant_location( $group_readings, $groups, $tree ); # foreach my $rdg ( keys %$dc ) { # my $var = $dc->{$rdg}; # # TODO Do something with this @@ -145,7 +145,7 @@ sub run_analysis { # -> readings [ { text, group, conflict, missing } ] sub analyze_variant_location { - my( $group_readings, $groups, $apsp, $lacunose ) = @_; + my( $group_readings, $groups, $graph, $lacunose ) = @_; my %contig; my $conflict = {}; my %missing; @@ -157,20 +157,32 @@ sub analyze_variant_location { map { $contig{$_} = $gst } @$g; } foreach my $g ( sort { scalar @$b <=> scalar @$a } @$groups ) { - my @members = @$g; - my $gst = wit_stringify( $g ); # $gst is now the name of this group. - while( @members ) { - # Gather the list of vertices that are needed to join all members. - my $curr = pop @members; - foreach my $m ( @members ) { - foreach my $v ( $apsp->path_vertices( $curr, $m ) ) { - $contig{$v} = $gst unless exists $contig{$v}; - next if $contig{$v} eq $gst; - # Record what is conflicting. TODO do we use this? - $conflict->{$group_readings->{$gst}} = $group_readings->{$contig{$v}}; - } + my $gst = wit_stringify( $g ); + # Copy the graph, and delete all non-members from the new graph. + my $part = $graph->undirected_copy; + map { $part->delete_vertex( $_ ) + if $contig{$_} && $contig{$_} ne $gst } $graph->vertices; + # Now all the members of the group should still be reachable + # from the first member. + my %reachable = ( $g->[0] => 1 ); + map { $reachable{$_} = 1 } $part->all_reachable( $g->[0] ); + + # ...and none of these nodes should be marked as being in another + # group. + foreach ( keys %reachable ) { + if( $contig{$_} && $contig{$_} ne $gst ) { + $conflict->{$group_readings->{$gst}} = $group_readings->{$contig{$_}}; + } else { + $contig{$_} = $gst; } } + # None of the unreachable nodes should be in our group either. + foreach ( $part->vertices ) { + next if $reachable{$_}; + $conflict->{$group_readings->{$gst}} = $group_readings->{$gst} + if $contig{$_} && $contig{$_} eq $gst; + } + # Write the reading. my $reading = { 'text' => $group_readings->{$gst}, 'missing' => wit_stringify( $lacunose ), @@ -178,12 +190,12 @@ sub analyze_variant_location { if( $reading->{'conflict'} ) { $reading->{'group'} = $gst; } else { - my @all_vertices = grep { $contig{$_} eq $gst && !$missing{$_} } keys %contig; + my @all_vertices = grep { !$missing{$_} } keys %reachable; $reading->{'group'} = wit_stringify( \@all_vertices ); } push( @{$variant_row->{'readings'}}, $reading ); } - $variant_row->{'genealogical'} = keys %$conflict ? undef : 1; + $variant_row->{'genealogical'} = !( keys %$conflict ); return $variant_row; } diff --git a/lib/Text/Tradition/Collation.pm b/lib/Text/Tradition/Collation.pm index 1f4e00f..d28d7d7 100644 --- a/lib/Text/Tradition/Collation.pm +++ b/lib/Text/Tradition/Collation.pm @@ -528,7 +528,7 @@ sub as_csv { # in the table are the nodes or simply their readings. sub make_alignment_table { - my( $self, $noderefs ) = @_; + my( $self, $noderefs, $include ) = @_; unless( $self->linear ) { warn "Need a linear graph in order to make an alignment table"; return; @@ -547,8 +547,19 @@ sub make_alignment_table { } } + if( $include ) { + my $winnowed = []; + # Winnow out the rows for any witness not included. + foreach my $row ( @$table ) { + next unless $include->{$row->[0]}; + push( @$winnowed, $row ); + } + $table = $winnowed; + } + # Return a table where the witnesses read in columns rather than rows. my $turned = _turn_table( $table ); + # TODO We should really go through and delete empty rows. return $turned; } diff --git a/lib/Text/Tradition/Stemma.pm b/lib/Text/Tradition/Stemma.pm index 23fbbcc..e241d56 100644 --- a/lib/Text/Tradition/Stemma.pm +++ b/lib/Text/Tradition/Stemma.pm @@ -17,19 +17,12 @@ has collation => ( required => 1, ); -# TODO Think about making a new class for the graphs, which has apsp as a property. has graph => ( is => 'rw', isa => 'Graph', predicate => 'has_graph', ); -has apsp => ( - is => 'ro', - isa => 'Graph', - writer => '_save_apsp', - ); - has distance_trees => ( is => 'ro', isa => 'ArrayRef[Graph]', @@ -37,12 +30,6 @@ has distance_trees => ( predicate => 'has_distance_trees', ); -has distance_apsps => ( - is => 'ro', - isa => 'ArrayRef[Graph]', - writer => '_save_distance_apsps', - ); - sub BUILD { my( $self, $args ) = @_; # If we have been handed a dotfile, initialize it into a graph. @@ -59,28 +46,6 @@ sub BUILD { } } -# If we are saving a new graph, calculate its apsp values. -after 'graph' => sub { - my( $self, $args ) = @_; - if( $args ) { - # We had a new graph. - my $undirected; - if( $self->graph->is_directed ) { - # Make an undirected version. - $undirected = Graph->new( 'undirected' => 1 ); - foreach my $v ( $self->graph->vertices ) { - $undirected->add_vertex( $v ); - } - foreach my $e ( $self->graph->edges ) { - $undirected->add_edge( @$e ); - } - } else { - $undirected = $self->graph; - } - $self->_save_apsp( $undirected->APSP_Floyd_Warshall() ); - } -}; - # Render the stemma as SVG. sub as_svg { my( $self, $opts ) = @_; @@ -117,6 +82,13 @@ sub as_svg { return $svg; } +sub witnesses { + my $self = shift; + my @wits = grep { $self->graph->get_vertex_attribute( $_, 'class' ) eq 'extant' } + $self->graph->vertices; + return @wits; +} + #### Methods for calculating phylogenetic trees #### before 'distance_trees' => sub { @@ -130,12 +102,6 @@ before 'distance_trees' => sub { # Save the resulting trees my $trees = _parse_newick( $result ); $self->_save_distance_trees( $trees ); - # and calculate their APSP values. - my @apsps; - foreach my $t ( @$trees ) { - push( @apsps, $t->APSP_Floyd_Warshall() ); - } - $self->_save_distance_apsps( \@apsps ); } else { warn "Failed to calculate distance trees: $result"; }