send ASCII-only requests to Stemweb. Issue #46
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Stemweb.pm
index c808d9d..262f235 100644 (file)
@@ -105,28 +105,28 @@ sub available :Local :Args(0) {
        my( $self, $c ) = @_;
        my $ua = LWP::UserAgent->new();
        my $resp = $ua->get( $self->stemweb_url . '/algorithms/available' );
+       my $parameters = [];
        if( $resp->is_success ) {
-               my $parameters = decode_json( $resp->content );
-               # Temporary hack: add Pars
-               if( $self->_has_pars ) {
-                       # Use the highest passed primary key + 1
-                       my $parspk = max( map { $_->{pk} } 
-                               grep { $_->{model} eq 'algorithms.algorithm' } @$parameters ) + 1;
-                       # Add Pars as an algorithm
-                       $self->pars_pk( $parspk );
-                       push( @$parameters, {
-                               pk => $parspk,
-                               model => 'algorithms.algorithm',
-                               fields => {
-                                       args => [],
-                                       name => 'Pars'
-                               }
-                       });
-               }
-               $c->stash->{'result'} = $parameters;
-       } else {
-               $c->stash->{'result'} = {};
+               $parameters = decode_json( $resp->content );
+       } # otherwise we have no available algorithms.
+       ## Temporary HACK: run Pars too
+       if( $self->_has_pars ) {
+               # Use the highest passed primary key + 1
+               my $parspk = max( map { $_->{pk} } 
+                       grep { $_->{model} eq 'algorithms.algorithm' } @$parameters ) + 1;
+               # Add Pars as an algorithm
+               $self->pars_pk( $parspk );
+               push( @$parameters, {
+                       pk => $parspk,
+                       model => 'algorithms.algorithm',
+                       fields => {
+                               args => [],
+                               name => 'Pars',
+                               desc => 'The program "pars", from the Phylip bio-statistical software package, produces a maximum-parsimony distance tree of the witnesses. More information on maximum parsimony can be found <a href="https://wiki.hiit.fi/display/stemmatology/Maximum+parsimony">here</a>. Please note that Phylip "pars" only supports a maximum of eight variants readings in any one variant location in the text. If your text displays more divergence than this at any point, please consider disregarding orthographic and spelling variation below, or use one of the other algorithms.'
+                       }
+               });
        }
+       $c->stash->{result} = $parameters;
        $c->forward('View::JSON');
 }
 
@@ -214,11 +214,13 @@ sub _process_stemweb_result {
                        }
                        $c->stash->{'result'} = { status => 'notfound' };
                }
-       } elsif( $answer->{status} == -1 ) {
+       } elsif( $answer->{status} == 1 ) {
                $c->stash->{'result'} = { 'status' => 'running' };
        } else {
-               return _json_error( $c, 500,
-                       "Stemweb failure not handled: " . $answer->{result} );
+               # Failure. Clear the job ID so that the user can try again.
+               $tradition->_clear_stemweb_jobid;
+               $m->save( $tradition );
+               $c->stash->{'result'} = { 'status' => 'failed', 'message' => $answer->{result} };
        }
        $c->forward('View::JSON');
 }
@@ -274,7 +276,7 @@ sub request :Local :Args(0) {
        } else {
                # Form the request for Stemweb.
                my $return_uri = URI->new( $c->uri_for( '/stemweb/result' ) );
-               my $tsv_options = { noac => 1 };
+               my $tsv_options = { noac => 1, ascii => 1 };
                if( $mergetypes && @$mergetypes ) {
                        $tsv_options->{mergetypes} = $mergetypes;
                }
@@ -288,7 +290,7 @@ sub request :Local :Args(0) {
                
                # Call to the appropriate URL with the request parameters.
                my $ua = LWP::UserAgent->new();
-               $c->log->debug( 'Sending request to Stemweb: ' . to_json( $stemweb_request ) ); 
+               # $c->log->debug( 'Sending request to Stemweb: ' . to_json( $stemweb_request ) ); 
                my $resp = $ua->post( $self->stemweb_url . "/algorithms/process/$algorithm/",
                        'Content-Type' => 'application/json; charset=utf-8', 
                        'Content' => encode_json( $stemweb_request ) );