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