ensure graph names come through for undirected graphs too (tla/stemmaweb#28)
[scpubgit/stemmatology.git] / analysis / lib / Text / Tradition / HasStemma.pm
CommitLineData
951ddfe8 1package Text::Tradition::HasStemma;
2
3use strict;
4use warnings;
5use Moose::Role;
a47b32d9 6use Date::Parse;
951ddfe8 7use Text::Tradition::Stemma;
98f22390 8use Text::Tradition::StemmaUtil qw/ parse_newick /;
951ddfe8 9
10=head1 NAME
11
a445ce40 12Text::Tradition::HasStemma - add-on to associate stemma hypotheses to
13Text::Tradition objects
951ddfe8 14
15=head1 DESCRIPTION
16
a445ce40 17It is often the case that, for a given text tradition, the order of copying
18of the witnesses can or should be reconstructed (or at least the attempt
19should be made.) This class is a role that can be applied to
20Text::Tradition objects to record stemma hypotheses. See the documentation
21for L<Text::Tradition::Stemma> for more information.
951ddfe8 22
23=head1 METHODS
24
25=head2 stemmata
26
27Return a list of all stemmata associated with the tradition.
28
29=head2 stemma_count
30
31Return the number of stemma hypotheses defined for this tradition.
32
33=head2 stemma( $idx )
34
35Return the L<Text::Tradition::Stemma> object identified by the given index.
36
37=head2 clear_stemmata
38
39Delete all stemma hypotheses associated with this tradition.
40
98f22390 41=head2 has_stemweb_jobid
42
43Returns true if there is currently a Stemweb job ID, indicating that a
44stemma tree calculation from the Stemweb service is in process.
45
46=head2 stemweb_jobid
47
48Return the currently-running job ID (if any) for calculation of Stemweb
49trees.
50
51=head2 set_stemweb_jobid( $jobid )
52
53Record a job ID for a Stemweb calculation.
54
951ddfe8 55=cut
56
57has 'stemmata' => (
58 traits => ['Array'],
59 isa => 'ArrayRef[Text::Tradition::Stemma]',
60 handles => {
61 stemmata => 'elements',
62 _add_stemma => 'push',
63 stemma => 'get',
64 stemma_count => 'count',
65 clear_stemmata => 'clear',
66 },
67 default => sub { [] },
68 );
69
98f22390 70has 'stemweb_jobid' => (
71 is => 'ro',
72 isa => 'Str',
73 writer => 'set_stemweb_jobid',
74 predicate => 'has_stemweb_jobid',
75 clearer => '_clear_stemweb_jobid',
76 );
77
78before 'set_stemweb_jobid' => sub {
79 my( $self ) = shift;
80 if( $self->has_stemweb_jobid ) {
81 $self->throw( "Tradition already has a Stemweb jobid: "
82 . $self->stemweb_jobid );
83 }
84};
951ddfe8 85
86=head2 add_stemma( $dotfile )
87
88Initializes a Text::Tradition::Stemma object from the given dotfile,
89and associates it with the tradition.
90
91=begin testing
92
93use Text::Tradition;
94
95my $t = Text::Tradition->new(
96 'name' => 'simple test',
97 'input' => 'Tabular',
98 'file' => 't/data/simple.txt',
99 );
951ddfe8 100is( $t->stemma_count, 0, "No stemmas added yet" );
101my $s;
102ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
103is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
104is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
105is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
106
107=end testing
108
109=cut
110
111sub add_stemma {
112 my $self = shift;
113 my %opts = @_;
114 my $stemma_fh;
115 if( $opts{'dotfile'} ) {
116 open $stemma_fh, '<', $opts{'dotfile'}
117 or warn "Could not open file " . $opts{'dotfile'};
118 } elsif( $opts{'dot'} ) {
119 my $str = $opts{'dot'};
120 open $stemma_fh, '<', \$str;
121 }
122 # Assume utf-8
123 binmode $stemma_fh, ':utf8';
124 my $stemma = Text::Tradition::Stemma->new(
125 'dot' => $stemma_fh );
126 $self->_add_stemma( $stemma ) if $stemma;
127 return $stemma;
128}
129
98f22390 130=head2 record_stemweb_result( $format, $data )
131
132Records the result returned by a Stemweb calculation, and clears any
133existing job ID.
134
a47b32d9 135=begin testing
136
137use Text::Tradition;
138use JSON qw/ from_json /;
139
140my $t = Text::Tradition->new(
141 'name' => 'Stemweb test',
142 'input' => 'Self',
143 'file' => 't/data/besoin.xml',
144 'stemweb_jobid' => '4',
145 );
146
147is( $t->stemma_count, 0, "No stemmas added yet" );
148
149my $answer = from_json( '{"status": 0, "job_id": "4", "algorithm": "RHM", "format": "newick", "start_time": "2013-10-26 10:44:14.050263", "result": "((((((((((((F,U),V),S),T1),T2),A),J),B),L),D),M),C);\n", "end_time": "2013-10-26 10:45:55.398944"}' );
150$t->record_stemweb_result( $answer );
151ok( !$t->has_stemweb_jobid, "Job ID was removed from tradition" );
152is( $t->stemma_count, 1, "Tradition has new stemma" );
153ok( $t->stemma(0)->is_undirected, "New stemma is undirected as it should be" );
154is( $t->stemma(0)->identifier, "RHM 1382777054_0", "Stemma has correct identifier" );
155
156
157=end testing
98f22390 158
159=cut
160
161sub record_stemweb_result {
a47b32d9 162 my( $self, $answer ) = @_;
163 if( $answer->{format} eq 'dot' ) {
164 $self->add_stemma( dot => $answer->{result} );
165 } elsif( $answer->{format} eq 'newick' ) {
166 my $stemmata = parse_newick( $answer->{result} );
167 my $title = sprintf( "%s %d", $answer->{algorithm},
168 str2time( $answer->{start_time} ) );
169 my $i = 0;
98f22390 170 foreach my $stemma ( @$stemmata ) {
a47b32d9 171 my $ititle = $title . "_$i"; $i++;
172 $stemma->set_identifier( $ititle );
98f22390 173 $self->_add_stemma( $stemma );
174 }
98f22390 175 } else {
a47b32d9 176 $self->throw( "Cannot parse tree results with format " . $answer->{format} );
98f22390 177 }
a47b32d9 178 $self->_clear_stemweb_jobid();
98f22390 179}
180
951ddfe8 1811;
182
183=head1 LICENSE
184
185This package is free software and is provided "as is" without express
186or implied warranty. You can redistribute it and/or modify it under
187the same terms as Perl itself.
188
189=head1 AUTHOR
190
191Tara L Andrews E<lt>aurum@cpan.orgE<gt>