split off stemma analysis modules from base Tradition layer
[scpubgit/stemmatology.git] / analysis / t / stemma.t
CommitLineData
6f4946fb 1#!/usr/bin/perl
2
3use strict; use warnings;
457b1620 4use File::Which;
6f4946fb 5use Test::More;
6use lib 'lib';
7use Text::Tradition;
9457207b 8use Text::Tradition::StemmaUtil qw/ character_input phylip_pars parse_newick /;
951ddfe8 9use TryCatch;
6f4946fb 10
11my $datafile = 't/data/Collatex-16.xml'; #TODO need other test data
12
7035e3a6 13my $tradition = Text::Tradition->new(
14 'name' => 'inline',
15 'input' => 'CollateX',
16 'file' => $datafile,
17 );
7a7c249c 18# Set up some relationships
19my $c = $tradition->collation;
679f17e1 20$c->add_relationship( 'n23', 'n24', { 'type' => 'spelling' } );
21$c->add_relationship( 'n9', 'n10', { 'type' => 'spelling' } );
22$c->add_relationship( 'n12', 'n13', { 'type' => 'spelling' } );
7a7c249c 23$c->calculate_ranks();
24
951ddfe8 25$tradition->enable_stemmata;
9ba651b9 26my $stemma = $tradition->add_stemma( dotfile => 't/data/simple.dot' );
6f4946fb 27
28# Test for object creation
29ok( $stemma->isa( 'Text::Tradition::Stemma' ), 'Got the right sort of object' );
7a7c249c 30is( $stemma->graph, '1-2,1-A,2-B,2-C', "Got the correct graph" );
6f4946fb 31
32# Test for character matrix creation
a0acdb4b 33my $mstr = character_input( $tradition );
6f4946fb 34 ## check number of rows
9457207b 35my @mlines = split( "\n", $mstr );
36my $msig = shift @mlines;
37my( $rows, $chars ) = $msig =~ /(\d+)\s+(\d+)/;
38is( $rows, 3, "Found three witnesses in char matrix" );
b02332ca 39 ## check number of columns
9457207b 40is( $chars, 18, "Found 18 rows plus sigla in char matrix" );
b02332ca 41 ## check matrix
42my %expected = (
43 'A' => 'AAAAAAAXAAAAAAAAAA',
44 'B' => 'AXXXAAAAAABABAABAA',
45 'C' => 'AXXXAAAAABAAAAAXBB',
46 );
9457207b 47foreach my $ml ( @mlines ) {
48 my( $wit, $chars ) = split( /\s+/, $ml );
49 is( $chars, $expected{$wit}, "Row for witness $wit is correct" );
b02332ca 50}
6f4946fb 51
52# Test that pars runs
457b1620 53SKIP: {
951ddfe8 54 skip "pars not in path", 3 unless File::Which::which('pars');
55 my $newick = phylip_pars( $mstr );
56 ok( $newick, "pars ran successfully" );
9457207b 57
58 my $trees = parse_newick( $newick );
951ddfe8 59 # Test that we get a tree
60 is( scalar @$trees, 1, "Got a single tree" );
61 # Test that the tree has all our witnesses
62 my $tree = $trees->[0];
63 is( scalar $tree->witnesses, 3, "All witnesses in the tree" );
457b1620 64}
7a7c249c 65
66# Test our dot output
67my $display = $stemma->as_dot();
68ok( $display =~ /digraph/, "Got a dot display graph" );
69ok( $display !~ /hypothetical/, "Graph is display rather than edit" );
70# Test our editable output
71my $editable = $stemma->editable();
72ok( $editable =~ /digraph/, "Got a dot edit graph" );
73ok( $editable =~ /hypothetical/, "Graph contains an edit class" );
457b1620 74done_testing();