let make_tradition parse excel files too
[scpubgit/stemmatology.git] / base / 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
10943ab0 16# Variables with defaults
17my( $informat, $outformat, $language, $name, $sep, $dsn ) = ( '', '', 'Default',
d8dd6236 18 'Tradition', "\t", "dbi:SQLite:dbname=db/traditions.db" );
10943ab0 19# Variables with no default
b63589d0 20my( $inbase, $help, $stemmafile, $dbuser, $dbpass, $from, $to, $dbid, $debug, $nonlinear );
910a0a6d 21
408449b7 22GetOptions( 'i|in=s' => \$informat,
23 'b|base=s' => \$inbase,
24 'o|out=s' => \$outformat,
6a1c434d 25 'l|language=s' => \$language,
861c3e27 26 'n|name=s' => \$name,
408449b7 27 'h|help' => \$help,
861c3e27 28 's|stemma=s' => \$stemmafile,
7d99d254 29 'u|user=s' => \$dbuser,
30 'p|pass=s' => \$dbpass,
fd7014c4 31 'f|from=s' => \$from,
32 't|to=s' => \$to,
b63589d0 33 'nl|nonlinear' => \$nonlinear,
a7fb3133 34 'sep=s' => \$sep,
861c3e27 35 'dsn=s' => \$dsn,
28333e88 36 'dbid=s' => \$dbid,
10943ab0 37 'debug' => \$debug
910a0a6d 38 );
39
40if( $help ) {
41 help();
42}
43
9a36afc0 44unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|xlsx?|db$/i ) {
910a0a6d 45 help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" );
46}
9a36afc0 47my $excel = $informat =~ /^xls/i ? lc( $informat ) : undef;
910a0a6d 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;
d9e873d0 53$informat = 'Tabular' if $informat =~ /^tab$/i;
fa954f4c 54$informat = 'CollateText' if $informat =~ /^stone$/i;
9a36afc0 55$informat = 'Tabular' if $informat =~ /^xls/i;
910a0a6d 56
861c3e27 57unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) {
58 help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" );
910a0a6d 59}
60
fd7014c4 61if( $from || $to ) {
68c59fcd 62 help( "Subgraphs only supported in GraphML, dot, or SVG format" )
63 unless $outformat =~ /^(graphml|dot|svg)$/;
fd7014c4 64}
65
910a0a6d 66# Do we have a base if we need it?
fa954f4c 67if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
910a0a6d 68 help( "$informat input needs a base text" );
69}
aa71409f 70$sep = "\t" if $sep eq 'tab';
fa954f4c 71
910a0a6d 72my $input = $ARGV[0];
00e822da 73my $tradition;
74my $dir;
75if( $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 );
b63589d0 87 $args{'linear'} = 0 if $nonlinear;
00e822da 88 $args{'base'} = $inbase if $inbase;
89 $args{'language'} = $language if $language;
90 $args{'name'} = $name if $name;
9a36afc0 91 if( $informat eq 'Tabular' ) {
92 if( $excel ) {
93 $args{'excel'} = $excel;
94 } else {
95 $args{'sep_char'} = $sep;
96 }
97 }
00e822da 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 );
fa954f4c 103}
861c3e27 104if( $stemmafile ) {
173ecc07 105 my $stemma = $tradition->add_stemma( dotfile => $stemmafile );
861c3e27 106 print STDERR "Saved stemma at $stemmafile\n" if $stemma;
107}
910a0a6d 108
910a0a6d 109# Now output what we have been asked to.
110if( $outformat eq 'stemma' ) {
56c56f0d 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;
910a0a6d 116 }
861c3e27 117} elsif( $outformat eq 'db' ) {
00e822da 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 }
861c3e27 125 my $scope = $dir->new_scope;
28333e88 126 my $uuid;
127 if( $dbid ) {
128 $uuid = $dir->store( $dbid => $tradition );
129 } else {
130 $uuid = $dir->store( $tradition );
131 }
861c3e27 132 print STDERR "Saved tradition to database with ID $uuid\n";
910a0a6d 133} else {
134 my $output = "as_$outformat";
fd7014c4 135 my $opts = {};
136 $opts->{'from'} = $from if $from;
137 $opts->{'to'} = $to if $to;
10943ab0 138 $opts->{'nocalc'} = 1 if $debug;
fd7014c4 139 print $tradition->collation->$output( $opts );
910a0a6d 140}
141
142sub help {
143 my( $msg ) = @_;
144 print STDERR << "EOF"
145Usage: $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.
152EOF
153 ;
154 if( $msg ) {
155 print STDERR "$msg\n";
156 }
157 exit ($msg ? 1 : 0 );
158}