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