split stemma lib into util and object; make phylip_input microservice
[scpubgit/stemmatology.git] / 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;
68454b71 8use Text::Tradition::StemmaUtil qw/ phylip_pars_input /;
6f4946fb 9use XML::LibXML;
10use XML::LibXML::XPathContext;
11
12my $datafile = 't/data/Collatex-16.xml'; #TODO need other test data
13
7035e3a6 14my $tradition = Text::Tradition->new(
15 'name' => 'inline',
16 'input' => 'CollateX',
17 'file' => $datafile,
18 );
7a7c249c 19# Set up some relationships
20my $c = $tradition->collation;
21$c->add_relationship( 'n25', 'n26', { 'type' => 'spelling' } );
22$c->add_relationship( 'n9', 'n23', { 'type' => 'spelling' } );
23$c->add_relationship( 'n8', 'n13', { 'type' => 'spelling' } );
24$c->calculate_ranks();
25
26my $stemma = $tradition->add_stemma( '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
68454b71 33my $m = phylip_pars_input( $c->make_alignment_table() );
6f4946fb 34 ## check number of rows
68454b71 35my $expected = "\t3\t18\n";
36$expected .= 'A AAAAAAAXAAAAAAAAAA
37B AXXXAAAAAABABAABAA
38C AXXXAAAAABAAAAAXBB';
39$expected .= "\n";
40is( $m, $expected, "Got the right pars input" );
41# is( scalar @$m, 3, "Found three witnesses in char matrix" );
42# ## check number of columns
43# is( scalar( @{$m->[0]} ), 19, "Found 18 rows plus sigla in char matrix" );
44# ## check matrix
45# my %expected = (
46# 'A' => 'AAAAAAAXAAAAAAAAAA',
47# 'B' => 'AXXXAAAAAABABAABAA',
48# 'C' => 'AXXXAAAAABAAAAAXBB',
49# );
50# my @wits = map { shift @$_; } @$m;
51# map { s/\s+//g } @wits;
52# foreach my $i ( 0 .. $#wits ) {
53# my $w = $wits[$i];
54# is( join( '', @{$m->[$i]} ), $expected{$w}, "Row for witness $w is correct" );
55# }
6f4946fb 56
57# Test that pars runs
457b1620 58SKIP: {
0f5d05c6 59 skip "pars not in path", 3 unless File::Which::which('pars');
457b1620 60 my( $status, $tree ) = $stemma->run_phylip_pars();
61 ok( $status, "pars ran successfully" );
62 print STDERR "Error was $tree\n" unless $status;
63
64 # Test that we get a tree
65 is( scalar @{$stemma->distance_trees}, 1, "Got a single tree" );
66 # Test that the tree has all our witnesses
67 $tree = $stemma->distance_trees->[0];
68 my @leaves = grep { $tree->degree( $_ ) == 1 } $tree->vertices;
69 is( scalar @leaves, 3, "All witnesses in the tree" );
70}
7a7c249c 71
72# Test our dot output
73my $display = $stemma->as_dot();
74ok( $display =~ /digraph/, "Got a dot display graph" );
75ok( $display !~ /hypothetical/, "Graph is display rather than edit" );
76# Test our editable output
77my $editable = $stemma->editable();
78ok( $editable =~ /digraph/, "Got a dot edit graph" );
79ok( $editable =~ /hypothetical/, "Graph contains an edit class" );
7035e3a6 80
457b1620 81done_testing();