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