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