allow specification of database UUID
[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 TryCatch;
8 use Text::Tradition;
9 use Text::Tradition::Directory;
10 use Text::Tradition::StemmaUtil qw/ character_input phylip_pars /;
11
12 binmode STDERR, ":utf8";
13 binmode STDOUT, ":utf8";
14 eval { no warnings; binmode $DB::OUT, ":utf8"; };
15
16 my( $informat, $inbase, $outformat, $help, $language, $name, $sep, $stemmafile, 
17         $dsn, $dbuser, $dbpass, $from, $to, $dbid ) 
18     = ( '', '', '', '', 'Default', 'Tradition', "\t", '',
19         "dbi:SQLite:dbname=stemmaweb/db/traditions.db", undef, undef, undef, undef, undef );
20
21 GetOptions( 'i|in=s'    => \$informat,
22             'b|base=s'  => \$inbase,
23             'o|out=s'   => \$outformat,
24             'l|language=s' => \$language,
25             'n|name=s'  => \$name,
26             'h|help'    => \$help,
27             's|stemma=s' => \$stemmafile,
28             'u|user=s'  => \$dbuser,
29             'p|pass=s'  => \$dbpass,
30             'f|from=s'  => \$from,
31             't|to=s'    => \$to,
32             'sep=s'             => \$sep,
33             'dsn=s'             => \$dsn,
34             'dbid=s'    => \$dbid,
35     );
36
37 if( $help ) {
38     help();
39 }
40
41 unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|stone$/i ) {
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;
49 $informat = 'Tabular' if $informat =~ /^tab$/i;
50 $informat = 'CollateText' if $informat =~ /^stone$/i;
51
52 unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) {
53     help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" );
54 }
55
56 if( $from || $to ) {
57         help( "Subgraphs only supported in GraphML format" ) 
58                 unless $outformat eq 'graphml';
59 }
60
61 # Do we have a base if we need it?
62 if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
63     help( "$informat input needs a base text" );
64 }
65 $sep = "\t" if $sep eq 'tab';
66
67 my $input = $ARGV[0];
68
69 # First: read the base. Make a graph, but also note which
70 # nodes represent line beginnings.
71 my %args = ( 'input' => $informat,
72              'file' => $input );
73 $args{'base'} = $inbase if $inbase;
74 $args{'language'} = $language if $language;
75 $args{'name'} = $name if $name;
76 $args{'sep_char'} = $sep if $informat eq 'Tabular';
77 ### Custom hacking for Stone
78 if( $informat eq 'CollateText' ) {
79     $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ];
80 }
81 my $tradition = Text::Tradition->new( %args );
82 if( $stemmafile ) {
83         my $stemma = $tradition->add_stemma( dotfile => $stemmafile );
84         print STDERR "Saved stemma at $stemmafile\n" if $stemma;
85 }
86
87 # Now output what we have been asked to.
88 if( $outformat eq 'stemma' ) {
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;
94     }
95 } elsif( $outformat eq 'db' ) {
96         my $extra_args = { 'create' => 1 };
97         $extra_args->{'user'} = $dbuser if $dbuser;
98         $extra_args->{'password'} = $dbpass if $dbpass;
99         my $dir = Text::Tradition::Directory->new( 'dsn' => $dsn, 
100                 'extra_args' => $extra_args );
101         my $scope = $dir->new_scope;
102         my $uuid;
103         if( $dbid ) {
104                 $uuid = $dir->store( $dbid => $tradition );
105         } else {
106                 $uuid = $dir->store( $tradition );
107         }
108         print STDERR "Saved tradition to database with ID $uuid\n";
109 } else {
110     my $output = "as_$outformat";
111     my $opts = {};
112     $opts->{'from'} = $from if $from;
113     $opts->{'to'} = $to if $to;
114     print $tradition->collation->$output( $opts );
115 }
116
117 sub help {
118     my( $msg ) = @_;
119     print STDERR << "EOF"
120 Usage: $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.
127 EOF
128     ;
129     if( $msg ) {
130         print STDERR "$msg\n";
131     }
132     exit ($msg ? 1 : 0 );
133 }