1 package stemmaweb::Controller::Microservice;
3 use namespace::autoclean;
6 use Text::Tradition::Stemma;
7 use Text::Tradition::StemmaUtil qw/ character_input phylip_pars newick_to_svg /;
9 BEGIN { extends 'Catalyst::Controller' }
13 stemmaweb::Controller::Microservice - Controller for stemmaweb standalone
18 This package contains the pieces of web functionality relating to text tradition
19 objects that are useful outside the framework of this application.
21 =head1 COLLATION PARSING INPUT
23 Each URL call which operates on a provided collation is called by POST with the
24 following form parameters in the body:
28 =item * type - Can be one of CollateX, CSV, JSON, nCritic, TEI, Tabular.
30 =item * data - The collation data itself.
38 POST microservice/renderSVG
40 Parse the passed collation data and return an SVG of the collated text.
44 # Utility function to render SVG from a collation in some recognized format.
45 sub renderSVG :Local :Args(0) {
47 my $tradition = _parse_to_tradition( $c->request );
48 $c->stash->{'result'} = $tradition->collation->as_svg;
49 $c->forward('View::SVG');
52 =head1 STEMMA / DISTANCE TREE URLs
56 POST microservice/stemma_svg
58 Parameter: dot => a string containing the dot description of the stemma.
62 sub stemma_svg :Local :Args(0) {
64 my $t = Text::Tradition->new();
65 my $stemma = $t->add_stemma( 'dot' => $c->req->param('dot') );
66 $c->stash->{'result'} = $stemma->as_svg;
67 $c->forward('View::SVG');
70 =head2 character_matrix
72 POST microservice/character_matrix
74 Given an alignment table in JSON form, in the parameter 'alignment', returns a
75 character matrix suitable for input to Phylip PARS.
79 sub character_matrix :Local :Args(0) {
81 my $json = $c->request->params->{'alignment'};
82 $c->log->debug( $json );
83 my $table = from_json( $json );
84 my $matrix = character_input( $table );
85 $c->stash->{'result'} = { 'matrix' => $matrix };
86 $c->forward( 'View::JSON' );
91 POST microservice/run_pars
93 Runs Phylip PARS on the provided alignment, and returns the result. Parameters include:
97 =item * alignment - A JSON alignment table, as produced by CollateX
99 =item * matrix - A character matrix suitable for Phylip.
101 =item * format - The format in which to return the results. Default is 'newick'; also allowed is 'svg'.
105 Exactly one of 'alignment' or 'matrix' must be specified.
109 sub run_pars :Local :Args(0) {
110 my( $self, $c ) = @_;
112 my $view = 'View::JSON';
114 if( $c->request->param('matrix') ) {
115 $matrix = $c->request->param('matrix');
116 } elsif( $c->request->param('alignment') ) {
117 # Make the matrix from the alignment
118 my $table = from_json( $c->request->param('alignment') );
119 $matrix = character_input( $table );
121 $error = "Must pass either an alignment or a matrix";
124 # Got the matrix, so try to run pars.
125 my( $result, $output );
127 ( $result, $output ) = phylip_pars( $matrix );
128 $error = $output unless( $result );
131 # Did we want newick or a graph?
133 my $format = 'newick';
134 $format = $c->request->param('format') if $c->request->param('format');
135 if( $format eq 'svg' ) {
137 $c->stash->{'result'} = newick_to_svg( $output );
139 } elsif( $format ne 'newick' ) {
140 $error = "Requested output format $format unknown";
142 $c->stash->{'result'} = { 'tree' => $output };
147 $c->stash->{'error'} = $error;
148 } # else the stash is populated.
149 $c->forward( $view );
153 =head1 OPENSOCIAL URLs
157 Simple gadget to return the analysis table for the stexaminer
161 sub view_table :Local :Args(0) {
162 my( $self, $c ) = @_;
163 my $m = $c->model('Directory');
164 my $id = $c->request->params->{'textid'};
165 my $t = run_analysis( $m->tradition( $id ), $m->stemma( $id ) );
166 $c->stash->{variants} = $t->{'variants'};
167 $c->stash->{template} = 'table_gadget.tt';
170 =head2 view_stemma_svg
172 Simple gadget to return the SVG for a given stemma
176 sub view_svg :Local :Args(0) {
177 my( $self, $c ) = @_;
178 my $m = $c->model('Directory');
179 my $stemma = $m->tradition( $c->request->params->{'textid'} )->stemma;
181 $c->stash->{svg} = $stemma->as_svg;
183 $c->stash->{template} = 'stemma_gadget.tt';
188 Standard 404 error page
193 my ( $self, $c ) = @_;
194 $c->response->body( 'Page not found' );
195 $c->response->status(404);
198 ## Internal utility function
200 sub _parse_to_tradition {
202 my $type = $req->body_params->{'type'};
203 my $name = $req->param('name') || 'Collation graph';
204 my $data = $req->body_params->{'data'};
210 $opts->{'sep_char'} = ',' if $type eq 'CSV';
211 $opts->{'sep_char'} = "\t" if $type eq 'TabSep';
212 return Text::Tradition->new( $opts );
217 Attempt to render a view, if needed.
221 sub end : ActionClass('RenderView') {}
229 This library is free software. You can redistribute it and/or modify
230 it under the same terms as Perl itself.
234 __PACKAGE__->meta->make_immutable;