Commit | Line | Data |
910a0a6d |
1 | #!/usr/bin/env perl |
2 | |
3 | use lib 'lib'; |
4 | use strict; |
5 | use warnings; |
6 | use Getopt::Long; |
56c56f0d |
7 | use TryCatch; |
910a0a6d |
8 | use Text::Tradition; |
861c3e27 |
9 | use Text::Tradition::Directory; |
56c56f0d |
10 | use Text::Tradition::StemmaUtil qw/ character_input phylip_pars /; |
910a0a6d |
11 | |
12 | binmode STDERR, ":utf8"; |
13 | binmode STDOUT, ":utf8"; |
14 | eval { no warnings; binmode $DB::OUT, ":utf8"; }; |
15 | |
652e0b6e |
16 | my( $informat, $inbase, $outformat, $help, $language, $name, $sep, $stemmafile, |
fd7014c4 |
17 | $dsn, $dbuser, $dbpass, $from, $to ) |
652e0b6e |
18 | = ( '', '', '', '', 'Default', 'Tradition', "\t", '', |
fd7014c4 |
19 | "dbi:SQLite:dbname=stemmaweb/db/traditions.db", undef, undef, undef, undef ); |
910a0a6d |
20 | |
408449b7 |
21 | GetOptions( 'i|in=s' => \$informat, |
22 | 'b|base=s' => \$inbase, |
23 | 'o|out=s' => \$outformat, |
6a1c434d |
24 | 'l|language=s' => \$language, |
861c3e27 |
25 | 'n|name=s' => \$name, |
408449b7 |
26 | 'h|help' => \$help, |
861c3e27 |
27 | 's|stemma=s' => \$stemmafile, |
7d99d254 |
28 | 'u|user=s' => \$dbuser, |
29 | 'p|pass=s' => \$dbpass, |
fd7014c4 |
30 | 'f|from=s' => \$from, |
31 | 't|to=s' => \$to, |
a7fb3133 |
32 | 'sep=s' => \$sep, |
861c3e27 |
33 | 'dsn=s' => \$dsn, |
910a0a6d |
34 | ); |
35 | |
36 | if( $help ) { |
37 | help(); |
38 | } |
39 | |
fa954f4c |
40 | unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|stone$/i ) { |
910a0a6d |
41 | help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" ); |
42 | } |
43 | $informat = 'CollateX' if $informat =~ /^c(ollate)?x$/i; |
44 | $informat = 'KUL' if $informat =~ /^kul$/i; |
45 | $informat = 'CTE' if $informat =~ /^cte$/i; |
46 | $informat = 'Self' if $informat =~ /^self$/i; |
47 | $informat = 'TEI' if $informat =~ /^tei$/i; |
d9e873d0 |
48 | $informat = 'Tabular' if $informat =~ /^tab$/i; |
fa954f4c |
49 | $informat = 'CollateText' if $informat =~ /^stone$/i; |
910a0a6d |
50 | |
861c3e27 |
51 | unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) { |
52 | help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" ); |
910a0a6d |
53 | } |
54 | |
fd7014c4 |
55 | if( $from || $to ) { |
56 | help( "Subgraphs only supported in GraphML format" ) |
57 | unless $outformat eq 'graphml'; |
58 | } |
59 | |
910a0a6d |
60 | # Do we have a base if we need it? |
fa954f4c |
61 | if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) { |
910a0a6d |
62 | help( "$informat input needs a base text" ); |
63 | } |
aa71409f |
64 | $sep = "\t" if $sep eq 'tab'; |
fa954f4c |
65 | |
910a0a6d |
66 | my $input = $ARGV[0]; |
910a0a6d |
67 | |
68 | # First: read the base. Make a graph, but also note which |
69 | # nodes represent line beginnings. |
dfc37e38 |
70 | my %args = ( 'input' => $informat, |
6a1c434d |
71 | 'file' => $input ); |
910a0a6d |
72 | $args{'base'} = $inbase if $inbase; |
6a1c434d |
73 | $args{'language'} = $language if $language; |
408449b7 |
74 | $args{'name'} = $name if $name; |
a7fb3133 |
75 | $args{'sep_char'} = $sep if $informat eq 'Tabular'; |
fa954f4c |
76 | ### Custom hacking for Stone |
77 | if( $informat eq 'CollateText' ) { |
78 | $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ]; |
79 | } |
910a0a6d |
80 | my $tradition = Text::Tradition->new( %args ); |
861c3e27 |
81 | if( $stemmafile ) { |
173ecc07 |
82 | my $stemma = $tradition->add_stemma( dotfile => $stemmafile ); |
861c3e27 |
83 | print STDERR "Saved stemma at $stemmafile\n" if $stemma; |
84 | } |
910a0a6d |
85 | |
910a0a6d |
86 | # Now output what we have been asked to. |
87 | if( $outformat eq 'stemma' ) { |
56c56f0d |
88 | my $cdata = character_input( $tradition->collation->alignment_table ); |
89 | try { |
90 | print phylip_pars( $cdata ); |
91 | } catch( Text::Tradition::Error $e ) { |
92 | print STDERR "Bad result: " . $e->message; |
910a0a6d |
93 | } |
861c3e27 |
94 | } elsif( $outformat eq 'db' ) { |
7d99d254 |
95 | my $extra_args = { 'create' => 1 }; |
96 | $extra_args->{'user'} = $dbuser if $dbuser; |
97 | $extra_args->{'password'} = $dbpass if $dbpass; |
861c3e27 |
98 | my $dir = Text::Tradition::Directory->new( 'dsn' => $dsn, |
7d99d254 |
99 | 'extra_args' => $extra_args ); |
861c3e27 |
100 | my $scope = $dir->new_scope; |
101 | my $uuid = $dir->store( $tradition ); |
102 | print STDERR "Saved tradition to database with ID $uuid\n"; |
910a0a6d |
103 | } else { |
104 | my $output = "as_$outformat"; |
fd7014c4 |
105 | my $opts = {}; |
106 | $opts->{'from'} = $from if $from; |
107 | $opts->{'to'} = $to if $to; |
108 | print $tradition->collation->$output( $opts ); |
910a0a6d |
109 | } |
110 | |
111 | sub help { |
112 | my( $msg ) = @_; |
113 | print STDERR << "EOF" |
114 | Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile] |
115 | i, input: Format of the input file. Must be one of CollateX, CSV, CTE, Self, TEI. |
116 | o, output: Format of the output. Must be one of svg, dot, graphml, csv, stemma. |
117 | b, base: Filename that contains a base text. Needed for CSV input. |
118 | l, linear: Treat transposed readings separately, producing a linear graph. |
119 | If nolinear, treat transposed readings as the same node. |
120 | h, help: Print this message. |
121 | EOF |
122 | ; |
123 | if( $msg ) { |
124 | print STDERR "$msg\n"; |
125 | } |
126 | exit ($msg ? 1 : 0 ); |
127 | } |