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