only use applicable a.c. witnesses in stemma for analysis
[scpubgit/stemmatology.git] / script / make_tradition.pl
CommitLineData
910a0a6d 1#!/usr/bin/env perl
2
3use lib 'lib';
4use strict;
5use warnings;
6use Getopt::Long;
7use Text::Tradition;
861c3e27 8use Text::Tradition::Directory;
4e5a7b2c 9use Text::Tradition::StemmaUtil;
910a0a6d 10
11binmode STDERR, ":utf8";
12binmode STDOUT, ":utf8";
13eval { no warnings; binmode $DB::OUT, ":utf8"; };
14
652e0b6e 15my( $informat, $inbase, $outformat, $help, $language, $name, $sep, $stemmafile,
7d99d254 16 $dsn, $dbuser, $dbpass )
652e0b6e 17 = ( '', '', '', '', 'Default', 'Tradition', "\t", '',
7d99d254 18 "dbi:SQLite:dbname=stemmaweb/db/traditions.db", undef, undef );
910a0a6d 19
408449b7 20GetOptions( 'i|in=s' => \$informat,
21 'b|base=s' => \$inbase,
22 'o|out=s' => \$outformat,
6a1c434d 23 'l|language=s' => \$language,
861c3e27 24 'n|name=s' => \$name,
408449b7 25 'h|help' => \$help,
861c3e27 26 's|stemma=s' => \$stemmafile,
7d99d254 27 'u|user=s' => \$dbuser,
28 'p|pass=s' => \$dbpass,
a7fb3133 29 'sep=s' => \$sep,
861c3e27 30 'dsn=s' => \$dsn,
910a0a6d 31 );
32
33if( $help ) {
34 help();
35}
36
fa954f4c 37unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|stone$/i ) {
910a0a6d 38 help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" );
39}
40$informat = 'CollateX' if $informat =~ /^c(ollate)?x$/i;
41$informat = 'KUL' if $informat =~ /^kul$/i;
42$informat = 'CTE' if $informat =~ /^cte$/i;
43$informat = 'Self' if $informat =~ /^self$/i;
44$informat = 'TEI' if $informat =~ /^tei$/i;
d9e873d0 45$informat = 'Tabular' if $informat =~ /^tab$/i;
fa954f4c 46$informat = 'CollateText' if $informat =~ /^stone$/i;
910a0a6d 47
861c3e27 48unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) {
49 help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" );
910a0a6d 50}
51
52# Do we have a base if we need it?
fa954f4c 53if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
910a0a6d 54 help( "$informat input needs a base text" );
55}
aa71409f 56$sep = "\t" if $sep eq 'tab';
fa954f4c 57
910a0a6d 58my $input = $ARGV[0];
910a0a6d 59
60# First: read the base. Make a graph, but also note which
61# nodes represent line beginnings.
dfc37e38 62my %args = ( 'input' => $informat,
6a1c434d 63 'file' => $input );
910a0a6d 64$args{'base'} = $inbase if $inbase;
6a1c434d 65$args{'language'} = $language if $language;
408449b7 66$args{'name'} = $name if $name;
a7fb3133 67$args{'sep_char'} = $sep if $informat eq 'Tabular';
fa954f4c 68### Custom hacking for Stone
69if( $informat eq 'CollateText' ) {
70 $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ];
71}
910a0a6d 72my $tradition = Text::Tradition->new( %args );
861c3e27 73if( $stemmafile ) {
173ecc07 74 my $stemma = $tradition->add_stemma( dotfile => $stemmafile );
861c3e27 75 print STDERR "Saved stemma at $stemmafile\n" if $stemma;
76}
910a0a6d 77
910a0a6d 78# Now output what we have been asked to.
79if( $outformat eq 'stemma' ) {
4e5a7b2c 80 my $cdata = character_input( $tradition->collation->make_alignment_table );
81 my( $result, $tree ) = phylip_pars( $cdata );
910a0a6d 82 if( $result ) {
83 print $tree;
84 } else {
85 print STDERR "Bad result: $tree";
86 }
861c3e27 87} elsif( $outformat eq 'db' ) {
7d99d254 88 my $extra_args = { 'create' => 1 };
89 $extra_args->{'user'} = $dbuser if $dbuser;
90 $extra_args->{'password'} = $dbpass if $dbpass;
861c3e27 91 my $dir = Text::Tradition::Directory->new( 'dsn' => $dsn,
7d99d254 92 'extra_args' => $extra_args );
861c3e27 93 my $scope = $dir->new_scope;
94 my $uuid = $dir->store( $tradition );
95 print STDERR "Saved tradition to database with ID $uuid\n";
910a0a6d 96} else {
97 my $output = "as_$outformat";
98 print $tradition->collation->$output();
99}
100
101sub help {
102 my( $msg ) = @_;
103 print STDERR << "EOF"
104Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile]
105 i, input: Format of the input file. Must be one of CollateX, CSV, CTE, Self, TEI.
106 o, output: Format of the output. Must be one of svg, dot, graphml, csv, stemma.
107 b, base: Filename that contains a base text. Needed for CSV input.
108 l, linear: Treat transposed readings separately, producing a linear graph.
109 If nolinear, treat transposed readings as the same node.
110 h, help: Print this message.
111EOF
112 ;
113 if( $msg ) {
114 print STDERR "$msg\n";
115 }
116 exit ($msg ? 1 : 0 );
117}