+package jalview.ws.jws2.jabaws2;
+
+import jalview.bin.Cache;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
+import jalview.gui.WebserviceInfo;
+import jalview.util.MessageManager;
+import jalview.ws.api.CancellableI;
+import jalview.ws.api.JobId;
+import jalview.ws.api.MultipleSequenceAlignmentI;
+import jalview.ws.gui.WsJob;
+import jalview.ws.gui.WsJob.JobState;
+import jalview.ws.jws2.JabaParamStore;
+import jalview.ws.jws2.JabaPreset;
+import jalview.ws.jws2.dm.JabaWsParamSet;
+import jalview.ws.params.ArgumentI;
+import jalview.ws.params.InvalidArgumentException;
+import jalview.ws.params.WsParamSetI;
+
+import java.io.IOError;
+import java.rmi.ServerError;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import compbio.data.msa.MsaWS;
+import compbio.data.sequence.Alignment;
+import compbio.data.sequence.FastaSequence;
+import compbio.metadata.Argument;
+import compbio.metadata.ChunkHolder;
+import compbio.metadata.JobStatus;
+import compbio.metadata.Preset;
+import compbio.metadata.ResultNotAvailableException;
+
+public class JabawsMsaInstance
+ implements MultipleSequenceAlignmentI, CancellableI
+{
+ /**
+ * our service instance handler generated by the discoverer
+ */
+ Jws2Instance our;
+
+ MsaWS<?> service;
+
+ @Override
+ public JobId align(List<SequenceI> toalign, WsParamSetI parameters,
+ List<ArgumentI> arguments) throws Throwable
+ {
+ List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<>();
+ for (SequenceI seq : toalign)
+ {
+ seqs.add(new FastaSequence(seq.getName(), seq.getSequenceAsString()));
+ }
+ String jobid = null;
+ if (parameters != null)
+ {
+ if (parameters instanceof JabaPreset)
+ {
+ jobid = service.presetAlign(seqs,
+ ((JabaPreset) parameters).getJabaPreset());
+ }
+ else
+ {
+ jobid = service.customAlign(seqs, JabaParamStore
+ .getJabafromJwsArgs(parameters.getArguments()));
+ }
+ }
+ else if (arguments != null && arguments.size() > 0)
+ {
+ jobid = service.customAlign(seqs,
+ JabaParamStore.getJabafromJwsArgs(arguments));
+ }
+ else
+ {
+ jobid = service.align(seqs);
+ }
+
+ if (jobid == null)
+ {
+ return null;
+ }
+ return new JobId(our.getServiceType(), our.getName(), jobid);
+ }
+
+ @Override
+ public AlignmentI getAlignmentFor(JobId jobId)
+ throws InvalidArgumentException, ServerError, IOError
+ {
+ Alignment alignment = null;
+ try
+ {
+ alignment = service.getResult(jobId.getJobId());
+ } catch (ResultNotAvailableException rnotav)
+ {
+
+ // TODO - migrate JABA exception
+ // throw new ServerError("Couldn't get result for job",rnotav);
+ }
+ SequenceI[] alseqs;
+ int alseq_l = 0;
+ if (alignment.getSequences().size() == 0)
+ {
+ return null;
+ }
+
+ alseqs = new SequenceI[alignment.getSequences().size()];
+ for (compbio.data.sequence.FastaSequence seq : alignment.getSequences())
+ {
+ alseqs[alseq_l++] = new Sequence(seq.getId(), seq.getSequence());
+ }
+ AlignmentI jv_al = new jalview.datamodel.Alignment(alseqs);
+ jv_al.setGapCharacter(alignment.getMetadata().getGapchar());
+ return jv_al;
+
+ }
+
+ public JabawsMsaInstance(Jws2Instance handle)
+ {
+ our = handle;
+ service = (MsaWS<?>) our.service;
+ }
+
+ @Override
+ public boolean cancel(WsJob job)
+ {
+ service.cancelJob(job.getJobId());
+ // if the Jaba server indicates the job can't be cancelled, its
+ // because its running on the server's local execution engine
+ // so we just close the window anyway.
+
+ return true;
+ }
+
+ Map<JobStatus, JobState> jwsState = new HashMap<>();
+ {
+ jwsState.put(JobStatus.CANCELLED, JobState.CANCELLED);
+ jwsState.put(JobStatus.COLLECTED, JobState.FINISHED);
+ jwsState.put(JobStatus.FAILED, JobState.FAILED);
+ jwsState.put(JobStatus.FINISHED, JobState.FINISHED);
+ jwsState.put(JobStatus.PENDING, JobState.QUEUED);
+ jwsState.put(JobStatus.RUNNING, JobState.RUNNING);
+ jwsState.put(JobStatus.STARTED, JobState.RUNNING);
+ jwsState.put(JobStatus.SUBMITTED, JobState.SUBMITTED);
+ jwsState.put(JobStatus.UNDEFINED, JobState.UNKNOWN);
+ }
+ @Override
+ public void updateStatus(WsJob job)
+ {
+ JobStatus jwsstatus = service.getJobStatus(job.getJobId());
+ job.setState(jwsState.get(jwsstatus));
+ }
+
+ @Override
+ public boolean updateJobProgress(WsJob job) throws Exception
+ {
+ StringBuilder response = new StringBuilder(job.getStatus());
+ long lastchunk = job.getNextChunk();
+ boolean changed = false;
+ do
+ {
+ ChunkHolder chunk = service.pullExecStatistics(job.getJobId(),
+ lastchunk);
+ if (chunk != null)
+ {
+ changed |= chunk.getChunk().length() > 0;
+ response.append(chunk.getChunk());
+ lastchunk = chunk.getNextPosition();
+ try
+ {
+ Thread.sleep(50);
+ } catch (InterruptedException x)
+ {
+ }
+ ;
+ }
+ ;
+ job.setnextChunk(lastchunk);
+ } while (lastchunk >= 0 && job.getNextChunk() != lastchunk);
+ if (job instanceof WsJob)
+ {
+ // TODO decide if WsJob will be the bean for all ng-webservices
+ job.setStatus(response.toString());
+ }
+ return changed;
+ }
+
+ public boolean isPresetJob(WsJob job)
+ {
+ return job.getPreset() != null && job.getPreset() instanceof JabaPreset;
+ }
+
+ public Preset getServerPreset(WsJob job)
+ {
+ return (isPresetJob(job))
+ ? ((JabaPreset) job.getPreset()).getJabaPreset()
+ : null;
+ }
+
+ public List<Argument> getJabaArguments(WsParamSetI preset)
+ {
+ List<Argument> newargs = new ArrayList<>();
+ if (preset != null)
+ {
+ if (preset instanceof JabaWsParamSet)
+ {
+ newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
+ }
+ else
+ {
+ newargs.addAll(
+ JabaParamStore.getJabafromJwsArgs(preset.getArguments()));
+ }
+ }
+ return newargs;
+ }
+
+ @Override
+ public boolean handleSubmitError(Throwable _lex, WsJob j,
+ WebserviceInfo wsInfo) throws Exception, Error
+ {
+ if (_lex instanceof compbio.metadata.UnsupportedRuntimeException)
+ {
+ wsInfo.appendProgressText(MessageManager.formatMessage(
+ "info.job_couldnt_be_run_server_doesnt_support_program",
+ new String[]
+ { _lex.getMessage() }));
+ wsInfo.warnUser(_lex.getMessage(),
+ MessageManager.getString("warn.service_not_supported"));
+ wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
+ wsInfo.setStatus(j.getJobnum(),
+ WebserviceInfo.STATE_STOPPED_SERVERERROR);
+ return true;
+ }
+ if (_lex instanceof compbio.metadata.LimitExceededException)
+ {
+ wsInfo.appendProgressText(MessageManager.formatMessage(
+ "info.job_couldnt_be_run_exceeded_hard_limit", new String[]
+ { _lex.getMessage() }));
+ wsInfo.warnUser(_lex.getMessage(),
+ MessageManager.getString("warn.input_is_too_big"));
+ wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
+ wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
+ return true;
+ }
+ if (_lex instanceof compbio.metadata.WrongParameterException)
+ {
+ wsInfo.warnUser(_lex.getMessage(),
+ MessageManager.getString("warn.invalid_job_param_set"));
+ wsInfo.appendProgressText(MessageManager.formatMessage(
+ "info.job_couldnt_be_run_incorrect_param_setting",
+ new String[]
+ { _lex.getMessage() }));
+ wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
+ wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
+ return true;
+ }
+ // pass on to generic error handler
+ return false;
+ }
+
+ @Override
+ public boolean handleCollectionException(Exception ex, WsJob msjob,
+ WebserviceInfo wsInfo)
+ {
+ if (ex instanceof compbio.metadata.ResultNotAvailableException)
+ {
+ // job has failed for some reason - probably due to invalid
+ // parameters
+ Cache.log.debug(
+ "Results not available for finished job - marking as broken job.",
+ ex);
+ String status = msjob.getStatus();
+
+ msjob.setStatus(status
+ +
+ "\nResult not available. Probably due to invalid input or parameter settings. Server error message below:\n\n"
+ + ex.getLocalizedMessage());
+ msjob.setState(WsJob.JobState.BROKEN);
+ return true;
+ }
+ return false;
+ }
+}