7 use Text::Tradition::Stemma;
9 binmode STDERR, ":utf8";
10 binmode STDOUT, ":utf8";
11 eval { no warnings; binmode $DB::OUT, ":utf8"; };
17 # Parse the tradition data
21 open( INFILE, "$input" ) or die "Could not read $input";
22 binmode INFILE, ':utf8';
25 $input = join( '', @lines );
27 my %args = ( $informat => $input,
28 'linear' => $linear );
29 $args{'base'} = $inbase if $inbase;
30 my $tradition = Text::Tradition->new( %args );
32 # Parse the stemma hypothesis
33 my $stemma = Text::Tradition::Stemma->new(
34 'collation' => $tradition->collation,
38 # We have the collation, so get the alignment table with witnesses in rows.
39 # Also return the reading objects in the table, rather than just the words.
41 my $all_wits_table = $tradition->collation->make_alignment_table( 'refs' );
43 # For each column in the alignment table, we want to see if the existing
44 # groupings of witnesses match our stemma hypothesis. We also want, at the
45 # end, to produce an HTML table with all the variants.
48 my $total = 0; # Keep track of the total number of variant locations
50 # Strip the list of sigla and save it for correlation to the readings.
51 my $col_wits = shift @$all_wits_table;
53 foreach my $i ( 0 .. $#$all_wits_table ) {
54 # For each column in the table, group the readings by witness.
56 my $col_rdgs = shift @$all_wits_table;
58 foreach my $j ( 0 .. $#{$col_rdgs} ) {
59 my $rdg = $col_rdgs->[$j];
60 $rank = $rdg->rank if $rdg; # Save the rank for later display
61 my $rdg_text = '(omitted)'; # Initialize in case of empty reading
63 $rdg_text = $rdg->is_lacuna ? undef : $rdg->text; # Don't count lacunae
65 if( defined $rdg_text ) {
66 # Initialize the witness array if we haven't got one yet
67 $rdg_wits->{$rdg_text} = [] unless $rdg_wits->{$rdg_text};
68 # Add the relevant witness, subject to a.c. logic
69 add_variant_wit( $rdg_wits->{$rdg_text}, $col_wits->[$j] );
73 # See if this column has any potentially genealogical variants.
74 # If not, skip to the next.
75 $total++ unless scalar keys %$rdg_wits == 1;
76 my( $groups, $readings ) = useful_variant( $rdg_wits );
77 next unless $groups && $readings;
78 $html_columns = scalar @$groups if scalar @$groups > $html_columns;
80 # We can already look up witnesses for a reading; we also want to look
81 # up readings for a given witness.
82 my $group_readings = {};
83 foreach my $x ( 0 .. $#$groups ) {
84 $group_readings->{wit_stringify( $groups->[$x] )} = $readings->[$x];
87 # For all the groups with more than one member, collect the list of all
88 # contiguous vertices needed to connect them.
89 # TODO: deal with a.c. reading logic
90 my $sc = analyze_variant_location( $group_readings, $groups, $stemma->apsp );
91 print wit_stringify( $groups ) . ' - ' . join( " / ", @$readings ) . "\n";
92 foreach my $rdg ( keys %$sc ) {
93 my $var = $sc->{$rdg};
94 print "\tReadings '$rdg' and '$var' are not genealogical\n";
97 # Now run the same analysis given the calculated distance tree(s).
98 foreach my $tree ( 0 .. $#{$stemma->distance_trees} ) {
99 my $dc = analyze_variant_location( $group_readings, $groups,
100 $stemma->distance_apsps->[$tree] );
101 foreach my $rdg ( keys %$dc ) {
102 my $var = $dc->{$rdg};
103 print "\tReadings '$rdg' and '$var' disregarded by parsimony on tree $tree\n";
107 # Record that we used this variant in an analysis
108 push( @$html_data, [ $rank, $readings, $sc ] );
111 # Save the stemma picture
112 open( STEMMA, ">stemma_graph.svg" ) or die "Could not open stemma graph to write";
113 binmode STEMMA, ":utf8";
114 print STEMMA $stemma->as_svg;
117 # Save the used variants as an HTML table
118 open( TABLE, ">variant_table.html" ) or die "Could not save variant table";
119 binmode TABLE, ":utf8";
120 print TABLE "<table>\n";
121 foreach my $row ( @$html_data ) {
122 my( $rank, $readings, $sc ) = @$row;
123 # Do we have a stemma conflict or a distance-tree conflict?
124 my $class = scalar keys %$sc ? 'coincidental' : 'genealogical';
125 print TABLE sprintf( "\t<tr id=\"%s\" class=\"%s\">\n", "variant-$rank", $class );
126 # Table row header should be the graph rank.
127 print TABLE "\t\t<th>$rank</th>\n";
129 foreach my $rdg ( @$readings ) {
130 print TABLE sprintf( "\t\t<td id=\"%s\">%s</td>\n", "item-$rank-$ctr", $rdg );
133 # Pad out the table - is this necessary I wonder?
134 while( $ctr++ < $html_columns ) {
135 print TABLE "\t\t<td/>\n";
137 print TABLE "\t</tr>\n";
139 print TABLE "</table>\n";
141 printf( "Ran analysis on %d / %d variant locations\n", scalar @$html_data, $total );
143 sub analyze_variant_location {
144 my( $group_readings, $groups, $apsp ) = @_;
147 foreach my $g ( sort { scalar @$b <=> scalar @$a } @$groups ) {
149 my $gst = wit_stringify( $g );
150 map { $contig{$_} = $gst } @members; # The witnesses need themselves to be
151 # in their collection.
152 next unless @members > 1;
153 my $curr = pop @members;
154 foreach my $m ( @members ) {
155 foreach my $v ( $apsp->path_vertices( $curr, $m ) ) {
156 $contig{$v} = $gst unless exists $contig{$v};
157 next if $contig{$v} eq $gst;
158 # print STDERR "Conflict at $v between group $gst and group "
159 # . $contig{$v} . "\n";
160 # Record what is conflicting.
161 $conflict->{$group_readings->{$gst}} = $group_readings->{$contig{$v}};
168 # Add the variant, subject to a.c. representation logic.
169 # This assumes that we will see the 'main' version before the a.c. version.
170 sub add_variant_wit {
171 my( $arr, $wit ) = @_;
172 my $acstr = $tradition->collation->ac_label;
174 if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
176 $skip = grep { $_ =~ /^\Q$real\E$/ } @$arr;
178 push( @$arr, $wit ) unless $skip;
181 # Return an answer if the variant is useful, i.e. if there are at least 2 variants
182 # with at least 2 witnesses each.
184 my( $readings ) = @_;
185 my $total = keys %$readings;
186 foreach my $var ( keys %$readings ) {
187 $total-- if @{$readings->{$var}} == 1;
189 return( undef, undef ) if $total <= 1;
190 my( $groups, $text );
191 foreach my $var ( keys %$readings ) {
192 push( @$groups, $readings->{$var} );
193 push( @$text, $var );
195 return( $groups, $text );
198 # Take an array of witness groupings and produce a string like
204 # If we were passed an array of witnesses instead of an array of
205 # groupings, then "group" the witnesses first.
206 unless( ref( $groups->[0] ) ) {
207 my $mkgrp = [ $groups ];
210 foreach my $g ( @$groups ) {
211 push( @gst, join( ',', @$g ) );
213 return join( ' / ', @gst );