let make_tradition parse excel files too
[scpubgit/stemmatology.git] / base / 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 # Variables with defaults
17 my( $informat, $outformat, $language, $name, $sep, $dsn )  = ( '', '', 'Default', 
18         'Tradition', "\t", "dbi:SQLite:dbname=db/traditions.db" );
19 # Variables with no default
20 my( $inbase, $help, $stemmafile,  $dbuser, $dbpass, $from, $to, $dbid, $debug, $nonlinear );
21
22 GetOptions( 'i|in=s'    => \$informat,
23             'b|base=s'  => \$inbase,
24             'o|out=s'   => \$outformat,
25             'l|language=s' => \$language,
26             'n|name=s'  => \$name,
27             'h|help'    => \$help,
28             's|stemma=s' => \$stemmafile,
29             'u|user=s'  => \$dbuser,
30             'p|pass=s'  => \$dbpass,
31             'f|from=s'  => \$from,
32             't|to=s'    => \$to,
33             'nl|nonlinear' => \$nonlinear,
34             'sep=s'             => \$sep,
35             'dsn=s'             => \$dsn,
36             'dbid=s'    => \$dbid,
37                 'debug'     => \$debug
38     );
39
40 if( $help ) {
41     help();
42 }
43
44 unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|xlsx?|db$/i ) {
45     help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" );
46 }
47 my $excel = $informat =~ /^xls/i ? lc( $informat ) : undef;
48 $informat = 'CollateX' if $informat =~ /^c(ollate)?x$/i;
49 $informat = 'KUL' if $informat =~ /^kul$/i;
50 $informat = 'CTE' if $informat =~ /^cte$/i;
51 $informat = 'Self' if $informat =~ /^self$/i;
52 $informat = 'TEI' if $informat =~ /^tei$/i;
53 $informat = 'Tabular' if $informat =~ /^tab$/i;
54 $informat = 'CollateText' if $informat =~ /^stone$/i;
55 $informat = 'Tabular' if $informat =~ /^xls/i;
56
57 unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) {
58     help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" );
59 }
60
61 if( $from || $to ) {
62         help( "Subgraphs only supported in GraphML, dot, or SVG format" ) 
63                 unless $outformat =~ /^(graphml|dot|svg)$/;
64 }
65
66 # Do we have a base if we need it?
67 if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
68     help( "$informat input needs a base text" );
69 }
70 $sep = "\t" if $sep eq 'tab';
71
72 my $input = $ARGV[0];
73 my $tradition;
74 my $dir;
75 if( $informat eq 'db' ) {
76         my $dbargs = { dsn => $dsn };
77         $dbargs->{'extra_args'}->{'user'} = $dbuser if $dbuser;
78         $dbargs->{'extra_args'}->{'password'} = $dbpass if $dbpass;
79         $dir = Text::Tradition::Directory->new( $dbargs );
80         my $scope = $dir->new_scope();
81         $tradition = $dir->lookup( $input );
82 } else {
83         # First: read the base. Make a graph, but also note which
84         # nodes represent line beginnings.
85         my %args = ( 'input' => $informat,
86                                  'file' => $input );
87         $args{'linear'} = 0 if $nonlinear;
88         $args{'base'} = $inbase if $inbase;
89         $args{'language'} = $language if $language;
90         $args{'name'} = $name if $name;
91         if( $informat eq 'Tabular' ) {
92                 if( $excel ) {
93                         $args{'excel'} = $excel;
94                 } else {
95                         $args{'sep_char'} = $sep;
96                 }
97         }
98         ### Custom hacking for Stone
99         if( $informat eq 'CollateText' ) {
100                 $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ];
101         }
102         $tradition = Text::Tradition->new( %args );
103 }
104 if( $stemmafile ) {
105         my $stemma = $tradition->add_stemma( dotfile => $stemmafile );
106         print STDERR "Saved stemma at $stemmafile\n" if $stemma;
107 }
108
109 # Now output what we have been asked to.
110 if( $outformat eq 'stemma' ) {
111     my $cdata = character_input( $tradition->collation->alignment_table );
112     try {
113         print phylip_pars( $cdata );
114     } catch( Text::Tradition::Error $e ) {
115         print STDERR "Bad result: " . $e->message;
116     }
117 } elsif( $outformat eq 'db' ) {
118         unless( $dir ) {
119                 my $extra_args = { 'create' => 1 };
120                 $extra_args->{'user'} = $dbuser if $dbuser;
121                 $extra_args->{'password'} = $dbpass if $dbpass;
122                 $dir = Text::Tradition::Directory->new( 'dsn' => $dsn, 
123                         'extra_args' => $extra_args );
124         }
125         my $scope = $dir->new_scope;
126         my $uuid;
127         if( $dbid ) {
128                 $uuid = $dir->store( $dbid => $tradition );
129         } else {
130                 $uuid = $dir->store( $tradition );
131         }
132         print STDERR "Saved tradition to database with ID $uuid\n";
133 } else {
134     my $output = "as_$outformat";
135     my $opts = {};
136     $opts->{'from'} = $from if $from;
137     $opts->{'to'} = $to if $to;
138     $opts->{'nocalc'} = 1 if $debug;
139     print $tradition->collation->$output( $opts );
140 }
141
142 sub help {
143     my( $msg ) = @_;
144     print STDERR << "EOF"
145 Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile]
146     i, input: Format of the input file.  Must be one of CollateX, CSV, CTE, Self, TEI.
147     o, output: Format of the output.  Must be one of svg, dot, graphml, csv, stemma.
148     b, base: Filename that contains a base text.  Needed for CSV input.
149     l, linear: Treat transposed readings separately, producing a linear graph.  
150         If nolinear, treat transposed readings as the same node.
151     h, help: Print this message.
152 EOF
153     ;
154     if( $msg ) {
155         print STDERR "$msg\n";
156     }
157     exit ($msg ? 1 : 0 );
158 }