add clear_witness functionality, use it, and update Collation pod
[scpubgit/stemmatology.git] / script / make_tradition.pl
1 #!/usr/bin/env perl
2
3 use lib 'lib';
4 use strict;
5 use warnings;
6 use Getopt::Long;
7 use Text::Tradition;
8 use Text::Tradition::StemmaUtil;
9
10 binmode STDERR, ":utf8";
11 binmode STDOUT, ":utf8";
12 eval { no warnings; binmode $DB::OUT, ":utf8"; };
13
14 my( $informat, $inbase, $outformat, $help, $linear, $name, $HACK, $sep ) 
15     = ( '', '', '', '', 1, 'Tradition', 0, "\t" );
16
17 GetOptions( 'i|in=s'    => \$informat,
18             'b|base=s'  => \$inbase,
19             'o|out=s'   => \$outformat,
20             'l|linear!' => \$linear,
21             'n|name=s'    => \$name,
22             'h|help'    => \$help,
23             'sep=s'             => \$sep,
24             'hack'      => \$HACK,
25     );
26
27 if( $help ) {
28     help();
29 }
30
31 unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|stone$/i ) {
32     help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" );
33 }
34 $informat = 'CollateX' if $informat =~ /^c(ollate)?x$/i;
35 $informat = 'KUL' if $informat =~ /^kul$/i;
36 $informat = 'CTE' if $informat =~ /^cte$/i;
37 $informat = 'Self' if $informat =~ /^self$/i;
38 $informat = 'TEI' if $informat =~ /^tei$/i;
39 $informat = 'Tabular' if $informat =~ /^tab$/i;
40 $informat = 'CollateText' if $informat =~ /^stone$/i;
41
42 unless( $outformat =~ /^(graphml|svg|dot|stemma|csv)$/ ) {
43     help( "Output format must be one of graphml, svg, csv, stemma, or dot" );
44 }
45
46 # Do we have a base if we need it?
47 if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
48     help( "$informat input needs a base text" );
49 }
50 $sep = "\t" if $sep eq 'tab';
51
52 my $input = $ARGV[0];
53
54 # First: read the base. Make a graph, but also note which
55 # nodes represent line beginnings.
56 my %args = ( 'input' => $informat,
57              'file' => $input,
58              'linear' => $linear );
59 $args{'base'} = $inbase if $inbase;
60 $args{'name'} = $name if $name;
61 $args{'sep_char'} = $sep if $informat eq 'Tabular';
62 ### Custom hacking for Stone
63 if( $informat eq 'CollateText' ) {
64     $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ];
65 }
66 my $tradition = Text::Tradition->new( %args );
67
68 ### Custom hacking
69 # Remove witnesses C, E, G in the Matthew text
70 if( $HACK ) {
71         my @togo = qw/ C E G /;
72         $tradition->collation->clear_witness( @togo );
73         $tradition->del_witness( @togo );
74 }
75
76 # Now output what we have been asked to.
77 if( $outformat eq 'stemma' ) {
78     my $cdata = character_input( $tradition->collation->make_alignment_table );
79     my( $result, $tree ) = phylip_pars( $cdata );
80     if( $result ) {
81         print $tree;
82     } else {
83         print STDERR "Bad result: $tree";
84     }
85 } else {
86     my $output = "as_$outformat";
87     print $tradition->collation->$output();
88 }
89
90 sub help {
91     my( $msg ) = @_;
92     print STDERR << "EOF"
93 Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile]
94     i, input: Format of the input file.  Must be one of CollateX, CSV, CTE, Self, TEI.
95     o, output: Format of the output.  Must be one of svg, dot, graphml, csv, stemma.
96     b, base: Filename that contains a base text.  Needed for CSV input.
97     l, linear: Treat transposed readings separately, producing a linear graph.  
98         If nolinear, treat transposed readings as the same node.
99     h, help: Print this message.
100 EOF
101     ;
102     if( $msg ) {
103         print STDERR "$msg\n";
104     }
105     exit ($msg ? 1 : 0 );
106 }