CollateX format for GraphML output changed; parser update
[scpubgit/stemmatology.git] / lib / Text / Tradition / Analysis.pm
index bf4c938..fda5137 100644 (file)
@@ -56,6 +56,8 @@ is 0 (i.e. the first).
 =item * merge_types - Specify a list of relationship types, where related readings 
 should be treated as identical for the purposes of analysis.
 
+=item * exclude_type1 - Exclude those ranks whose groupings have only type-1 variants.
+
 =back
 
 =begin testing
@@ -150,7 +152,13 @@ sub run_analysis {
        my %lacunae;
        foreach my $rank ( @ranks ) {
                my $missing = [ @lacunose ];
-               push( @groups, group_variants( $tradition, $rank, $missing, \@collapse ) );
+               my $rankgroup = group_variants( $tradition, $rank, $missing, \@collapse );
+               if( $opts{'exclude_type1'} ) {
+                       # Check to see whether this is a "useful" group.
+                       my( $rdgs, $grps ) = _useful_variant( $rankgroup );
+                       next unless @$rdgs;
+               }
+               push( @groups, $rankgroup );
                $lacunae{$rank} = $missing;
        }
        $DB::single = 1;
@@ -187,8 +195,8 @@ relationships in @merge_relationship_types as equivalent.  $lacunose should
 be a reference to an array, to which the sigla of lacunose witnesses at this 
 rank will be appended.
 
-Returns two ordered lists $readings, $groups, where $readings->[$n] is attested
-by the witnesses listed in $groups->[$n].
+Returns a hash $group_readings where $rdg is attested by the witnesses listed 
+in $group_readings->{$rdg}.
 
 =cut
 
@@ -262,7 +270,7 @@ sub solve_variants {
        my $groupings = [];
        foreach my $ghash ( @groups ) {
                my @grouping;
-               foreach my $k ( sort keys %$ghash ) {
+               foreach my $k ( keys %$ghash ) {
                        push( @grouping, $ghash->{$k} );
                }
                push( @$groupings, \@grouping );
@@ -278,8 +286,10 @@ sub solve_variants {
                                                  'Content' => $json );
                                                  
        my $answer;
+       my $used_idp;
        if( $resp->is_success ) {
                $answer = _desanitize_names( decode_json( $resp->content ), $witness_map );
+               $used_idp = 1;
        } else {
                # Fall back to the old method.
                warn "IDP solver returned " . $resp->status_line . " / " . $resp->content
@@ -292,7 +302,18 @@ sub solve_variants {
        my $genealogical = 0;
        foreach my $idx ( 0 .. $#groups ) {
                my( $calc_groups, $result ) = @{$answer->[$idx]};
-               $genealogical++ if $result;
+               if( $result ) {
+                       $genealogical++;
+                       # Prune the calculated groups, in case the IDP solver failed to.
+                       if( $used_idp ) {
+                               my @pruned_groups;
+                               foreach my $cg ( @$calc_groups ) {
+                                       my @pg = _prune_group( $cg, $stemma );
+                                       push( @pruned_groups, \@pg );
+                               }
+                               $calc_groups = \@pruned_groups;
+                       }
+               }
                my $input_group = $groups[$idx];
                foreach my $k ( sort keys %$input_group ) {
                        my $cg = shift @$calc_groups;
@@ -688,6 +709,24 @@ sub _solve_variant_location {
     return $variant_row;
 }
 
+sub _prune_group {
+       my( $group, $stemma ) = @_;
+       # Get these into a form prune_subtree will recognize. Make a "contighash"
+       my $hypohash = {};
+       map { $hypohash->{$_} = 1 } @$group;
+       # ...with reference values for hypotheticals.
+       map { $hypohash->{$_} = [] } $stemma->hypotheticals;
+       # Make our subgraph
+       my $subgraph = $stemma->graph->copy;
+       map { $subgraph->delete_vertex( $_ ) unless exists $hypohash->{$_} }
+               $subgraph->vertices;
+       # ...and find the root.
+       my( $root ) = $subgraph->predecessorless_vertices;
+       # Now prune and return the remaining vertices.
+       _prune_subtree( $subgraph, $root, $hypohash );
+       return $subgraph->vertices;
+}
+
 sub _prune_subtree {
     my( $tree, $root, $contighash ) = @_;
     # First, delete hypothetical leaves / orphans until there are none left.