make phylo tree generation work with script
[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 ) 
18     = ( '', '', '', '', 'Default', 'Tradition', "\t", '',
19         "dbi:SQLite:dbname=stemmaweb/db/traditions.db", 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             'sep=s'             => \$sep,
31             'dsn=s'             => \$dsn,
32     );
33
34 if( $help ) {
35     help();
36 }
37
38 unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|stone$/i ) {
39     help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" );
40 }
41 $informat = 'CollateX' if $informat =~ /^c(ollate)?x$/i;
42 $informat = 'KUL' if $informat =~ /^kul$/i;
43 $informat = 'CTE' if $informat =~ /^cte$/i;
44 $informat = 'Self' if $informat =~ /^self$/i;
45 $informat = 'TEI' if $informat =~ /^tei$/i;
46 $informat = 'Tabular' if $informat =~ /^tab$/i;
47 $informat = 'CollateText' if $informat =~ /^stone$/i;
48
49 unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) {
50     help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" );
51 }
52
53 # Do we have a base if we need it?
54 if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
55     help( "$informat input needs a base text" );
56 }
57 $sep = "\t" if $sep eq 'tab';
58
59 my $input = $ARGV[0];
60
61 # First: read the base. Make a graph, but also note which
62 # nodes represent line beginnings.
63 my %args = ( 'input' => $informat,
64              'file' => $input );
65 $args{'base'} = $inbase if $inbase;
66 $args{'language'} = $language if $language;
67 $args{'name'} = $name if $name;
68 $args{'sep_char'} = $sep if $informat eq 'Tabular';
69 ### Custom hacking for Stone
70 if( $informat eq 'CollateText' ) {
71     $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ];
72 }
73 my $tradition = Text::Tradition->new( %args );
74 if( $stemmafile ) {
75         my $stemma = $tradition->add_stemma( dotfile => $stemmafile );
76         print STDERR "Saved stemma at $stemmafile\n" if $stemma;
77 }
78
79 # Now output what we have been asked to.
80 if( $outformat eq 'stemma' ) {
81     my $cdata = character_input( $tradition->collation->alignment_table );
82     try {
83         print phylip_pars( $cdata );
84     } catch( Text::Tradition::Error $e ) {
85         print STDERR "Bad result: " . $e->message;
86     }
87 } elsif( $outformat eq 'db' ) {
88         my $extra_args = { 'create' => 1 };
89         $extra_args->{'user'} = $dbuser if $dbuser;
90         $extra_args->{'password'} = $dbpass if $dbpass;
91         my $dir = Text::Tradition::Directory->new( 'dsn' => $dsn, 
92                 'extra_args' => $extra_args );
93         my $scope = $dir->new_scope;
94         my $uuid = $dir->store( $tradition );
95         print STDERR "Saved tradition to database with ID $uuid\n";
96 } else {
97     my $output = "as_$outformat";
98     print $tradition->collation->$output();
99 }
100
101 sub help {
102     my( $msg ) = @_;
103     print STDERR << "EOF"
104 Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile]
105     i, input: Format of the input file.  Must be one of CollateX, CSV, CTE, Self, TEI.
106     o, output: Format of the output.  Must be one of svg, dot, graphml, csv, stemma.
107     b, base: Filename that contains a base text.  Needed for CSV input.
108     l, linear: Treat transposed readings separately, producing a linear graph.  
109         If nolinear, treat transposed readings as the same node.
110     h, help: Print this message.
111 EOF
112     ;
113     if( $msg ) {
114         print STDERR "$msg\n";
115     }
116     exit ($msg ? 1 : 0 );
117 }