From: Tara L Andrews Date: Tue, 10 Jun 2014 09:44:03 +0000 (+0200) Subject: Add parsimony algorithm as quick hack; allow relationship merge in Stemweb. Fixes... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3cb9d9c0bc6a0c24d413b04900b7c567964af280;p=scpubgit%2Fstemmaweb.git Add parsimony algorithm as quick hack; allow relationship merge in Stemweb. Fixes #40 and #41 --- diff --git a/Makefile.PL b/Makefile.PL index 6dd80af..d74feb7 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -37,6 +37,8 @@ requires 'CatalystX::Controller::Auth' => '0.22'; requires 'Catalyst::TraitFor::Controller::reCAPTCHA'; requires 'LWP::Protocol::https'; ## +requires 'File::Which'; +requires 'List::Util'; requires 'Moose'; requires 'TryCatch'; requires 'namespace::autoclean'; diff --git a/lib/stemmaweb/Controller/Root.pm b/lib/stemmaweb/Controller/Root.pm index c772122..11e54af 100644 --- a/lib/stemmaweb/Controller/Root.pm +++ b/lib/stemmaweb/Controller/Root.pm @@ -317,6 +317,10 @@ sub textinfo :Local :Args(1) { public => $tradition->public || 0, owner => $tradition->user ? $tradition->user->email : undef, witnesses => [ map { $_->sigil } $tradition->witnesses ], + # TODO Send them all with appropriate parameters so that the + # client side can choose what to display. + reltypes => [ map { $_->name } grep { !$_->is_weak && $_->is_colocation } + $tradition->collation->relationship_types ] }; ## TODO Make these into callbacks in the other controllers maybe? if( $tradition->can('language') ) { diff --git a/lib/stemmaweb/Controller/Stemweb.pm b/lib/stemmaweb/Controller/Stemweb.pm index 3dc4ede..c37b600 100644 --- a/lib/stemmaweb/Controller/Stemweb.pm +++ b/lib/stemmaweb/Controller/Stemweb.pm @@ -2,10 +2,13 @@ package stemmaweb::Controller::Stemweb; use Moose; use namespace::autoclean; use Encode qw/ decode_utf8 /; +use File::Which; use JSON; +use List::Util qw/ max /; use LWP::UserAgent; use Safe::Isa; use Scalar::Util qw/ looks_like_number /; +use Text::Tradition::StemmaUtil qw/ character_input phylip_pars /; use TryCatch; use URI; @@ -16,6 +19,16 @@ has stemweb_url => ( isa => 'Str', default => 'http://slinkola.users.cs.helsinki.fi', ); + +has pars_path => ( + is => 'ro', + isa => 'Str', + ); + +has pars_pk => ( + is => 'rw', + isa => 'Int', + ); =head1 NAME @@ -93,7 +106,24 @@ sub available :Local :Args(0) { my $ua = LWP::UserAgent->new(); my $resp = $ua->get( $self->stemweb_url . '/algorithms/available' ); if( $resp->is_success ) { - $c->stash->{'result'} = decode_json( $resp->content ); + 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'} = {}; } @@ -217,43 +247,72 @@ sub request :Local :Args(0) { 'You do not have permission to update stemmata for this tradition' ) ) unless $ok eq 'full'; - # Form the request for Stemweb. my $algorithm = delete $reqparams->{algorithm}; - my $return_uri = URI->new( $c->uri_for( '/stemweb/result' ) ); - my $stemweb_request = { - return_path => $return_uri->path, - return_host => $return_uri->host_port, - data => $t->collation->as_tsv({noac => 1}), - userid => $c->user->get_object->email, - textid => $tid, - parameters => _cast_nonstrings( $reqparams ) }; - - # 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 ) ); - my $resp = $ua->post( $self->stemweb_url . "/algorithms/process/$algorithm/", - 'Content-Type' => 'application/json; charset=utf-8', - 'Content' => encode_json( $stemweb_request ) ); - if( $resp->is_success ) { - # Process it - $c->log->debug( 'Got a response from the server: ' - . decode_utf8( $resp->content ) ); - my $stemweb_response = decode_json( $resp->content ); + my $mergetypes = delete $reqparams->{merge_reltypes}; + if( $self->_has_pars && $algorithm == $self->pars_pk ) { + my $start_time = scalar( gmtime( time() ) ); + $t->set_stemweb_jobid( 'local' ); + my $cdata = character_input( $t, { collapse => $mergetypes } ); + my $newick; try { - $t->set_stemweb_jobid( $stemweb_response->{jobid} ); - } catch( Text::Tradition::Error $e ) { - return _json_error( $c, 429, $e->message ); + $newick = phylip_pars( $cdata, { parspath => $self->_has_pars } ); + } catch ( Text::Tradition::Error $e ) { + return _json_error( $c, 503, "Parsimony tree generation failed: " + . $e->message ); } - $c->model('Directory')->save( $t ); - $c->stash->{'result'} = $stemweb_response; - $c->forward('View::JSON'); - } elsif( $resp->code == 500 && $resp->header('Client-Warning') - && $resp->header('Client-Warning') eq 'Internal response' ) { - # The server was unavailable. - return _json_error( $c, 503, "The Stemweb server is currently unreachable." ); + # We have a result, so form an answer to process. + my $answer = { + status => 0, + algorithm => 'pars', + 'format' => 'newick', + textid => $tid, + jobid => 'local', + result => $newick, + start_time => $start_time + }; + return _process_stemweb_result( $c, $answer ); } else { - return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / " - . $resp->content ); + # Form the request for Stemweb. + my $return_uri = URI->new( $c->uri_for( '/stemweb/result' ) ); + my $tsv_options = { noac => 1 }; + if( $mergetypes && @$mergetypes ) { + $tsv_options->{mergetypes} = $mergetypes; + } + my $stemweb_request = { + return_path => $return_uri->path, + return_host => $return_uri->host_port, + data => $t->collation->as_tsv( $tsv_options ), + userid => $c->user->get_object->email, + textid => $tid, + parameters => _cast_nonstrings( $reqparams ) }; + + # 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 ) ); + my $resp = $ua->post( $self->stemweb_url . "/algorithms/process/$algorithm/", + 'Content-Type' => 'application/json; charset=utf-8', + 'Content' => encode_json( $stemweb_request ) ); + if( $resp->is_success ) { + # Process it + $c->log->debug( 'Got a response from the server: ' + . decode_utf8( $resp->content ) ); + my $stemweb_response = decode_json( $resp->content ); + try { + $t->set_stemweb_jobid( $stemweb_response->{jobid} ); + } catch( Text::Tradition::Error $e ) { + return _json_error( $c, 429, $e->message ); + } + $c->model('Directory')->save( $t ); + $c->stash->{'result'} = $stemweb_response; + $c->forward('View::JSON'); + } elsif( $resp->code == 500 && $resp->header('Client-Warning') + && $resp->header('Client-Warning') eq 'Internal response' ) { + # The server was unavailable. + return _json_error( $c, 503, "The Stemweb server is currently unreachable." ); + } else { + return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / " + . $resp->content ); + } } } @@ -300,5 +359,10 @@ sub _json_bool { return $_[0] ? JSON::true : JSON::false; } +sub _has_pars { + my $self = shift; + return $self->pars_path || which('pars'); +} + 1; diff --git a/root/js/componentload.js b/root/js/componentload.js index 888bf69..06a2991 100644 --- a/root/js/componentload.js +++ b/root/js/componentload.js @@ -166,32 +166,36 @@ function load_stemma( idx ) { function query_stemweb_progress() { var requrl = _get_url([ "stemweb", "query", selectedTextInfo.stemweb_jobid ]); $.getJSON( requrl, function (data) { - // Look for a status message, either success, running, or notfound. - if( data.status === 'success' ) { - // Add the new stemmata to the textinfo and tell the user. - selectedTextInfo.stemweb_jobid = 0; - if( data.stemmata.length > 0 ) { - stemmata = stemmata.concat( data.stemmata ); - if( selectedStemmaID == -1 ) { - // We have a stemma for the first time; load the first one. - load_stemma( 0, true ); - } else { - // Move to the index of the first added stemma. - var newIdx = stemmata.length - data.stemmata.length; - load_stemma( newIdx, true ); - } - alert( 'You have one or more new stemmata!' ); + process_stemweb_result( data ); + }); +} + +function process_stemweb_result(data) { + // Look for a status message, either success, running, or notfound. + if( data.status === 'success' ) { + // Add the new stemmata to the textinfo and tell the user. + selectedTextInfo.stemweb_jobid = 0; + if( data.stemmata.length > 0 ) { + stemmata = stemmata.concat( data.stemmata ); + if( selectedStemmaID == -1 ) { + // We have a stemma for the first time; load the first one. + load_stemma( 0, true ); } else { - alert( 'Stemweb run finished with no stemmata...huh?!' ); + // Move to the index of the first added stemma. + var newIdx = stemmata.length - data.stemmata.length; + load_stemma( newIdx, true ); } - } else if( data.status === 'running' ) { - // Just tell the user. - alert( 'Your Stemweb query is still running!' ); - } else if( data.status === 'notfound' ) { - // Ask the user to refresh, for now. - alert( 'Your Stemweb query probably finished and reported back. Please reload to check.' ); + alert( 'You have one or more new stemmata!' ); + } else { + alert( 'Stemweb run finished with no stemmata...huh?!' ); } - }); + } else if( data.status === 'running' ) { + // Just tell the user. + alert( 'Your Stemweb query is still running!' ); + } else if( data.status === 'notfound' ) { + // Ask the user to refresh, for now. + alert( 'Your Stemweb query probably finished and reported back. Please reload to check.' ); + } } // Load the SVG we are given @@ -433,7 +437,7 @@ $(document).ready( function() { buttons: { Save: function (evt) { $("#edit_textinfo_status").empty(); - $(evt.target).button("disable"); + $(evt.target).closest('button').button("disable"); var requrl = _get_url([ "textinfo", selectedTextID ]); var reqparam = $('#edit_textinfo').serialize(); $.post( requrl, reqparam, function (data) { @@ -441,7 +445,7 @@ $(document).ready( function() { selectedTextInfo = data; load_textinfo(); // Reenable the button and close the form - $(evt.target).button("enable"); + $(evt.target).closest('button').button("enable"); $('#textinfo-edit-dialog').dialog('close'); }, 'json' ); }, @@ -486,7 +490,7 @@ $(document).ready( function() { buttons: { Save: function (evt) { $("#edit_stemma_status").empty(); - $(evt.target).button("disable"); + $(evt.target).closest('button').button("disable"); var stemmaseq = $('#stemmaseq').val(); var requrl = _get_url([ "stemma", selectedTextID, stemmaseq ]); var reqparam = { 'dot': $('#dot_field').val() }; @@ -503,7 +507,7 @@ $(document).ready( function() { // Display the new stemma load_stemma( selectedStemmaID, true ); // Reenable the button and close the form - $(evt.target).button("enable"); + $(evt.target).closest('button').button("enable"); $('#stemma-edit-dialog').dialog('close'); }, 'json' ); }, @@ -543,24 +547,30 @@ $(document).ready( function() { $('#stemweb-ui-dialog').dialog({ autoOpen: false, height: 160, - width: 225, + width: 240, modal: true, buttons: { Run: function (evt) { $("#stemweb_run_status").empty(); - $(evt.target).button("disable"); + $(evt.target).closest('button').button("disable"); var requrl = _get_url([ "stemweb", "request" ]); var reqparam = $('#call_stemweb').serialize(); // TODO We need to stash the literal SVG string in stemmata // somehow. Implement accept header on server side to decide // whether to send application/json or application/xml? $.getJSON( requrl, reqparam, function (data) { - // Job ID is in data.jobid. TODO do something with it. - selectedTextInfo.stemweb_jobid = data.jobid; - $(evt.target).button("enable"); + $(evt.target).closest('button').button("enable"); $('#stemweb-ui-dialog').dialog('close'); - // Reload the current stemma to rejigger the buttons - load_stemma( selectedStemmaID, true ); + if( 'jobid' in data ) { + // There is a pending job. + selectedTextInfo.stemweb_jobid = data.jobid; + alert("Your request has been submitted to Stemweb.\nThe resulting tree will appear in due course."); + // Reload the current stemma to rejigger the buttons + load_stemma( selectedStemmaID, true ); + } else { + // We appear to have an answer; process it. + process_stemweb_result( data ); + } }, 'json' ); }, Cancel: function() { @@ -628,6 +638,15 @@ $(document).ready( function() { open: function(evt) { $('#stemweb_run_status').empty(); $('#stemweb_tradition').attr('value', selectedTextID ); + $('#stemweb_merge_reltypes').empty(); + $.each( selectedTextInfo.reltypes, function( i, r ) { + var relation_opt = $('