2da8dd3c0cc92b93ccce503084c31187e8c6a55f
[jalview.git] / src / jalview / ws / jws2 / AbstractJabaCalcWorker.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.jws2;
22
23 import jalview.analysis.AlignSeq;
24 import jalview.analysis.SeqsetUtils;
25 import jalview.api.AlignViewportI;
26 import jalview.api.AlignmentViewPanel;
27 import jalview.bin.Console;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.AnnotatedCollectionI;
31 import jalview.datamodel.SequenceI;
32 import jalview.gui.AlignFrame;
33 import jalview.gui.IProgressIndicator;
34 import jalview.gui.IProgressIndicatorHandler;
35 import jalview.schemes.ResidueProperties;
36 import jalview.workers.AlignCalcWorker;
37 import jalview.ws.jws2.dm.AAConSettings;
38 import jalview.ws.jws2.dm.JabaWsParamSet;
39 import jalview.ws.jws2.jabaws2.Jws2Instance;
40 import jalview.ws.params.WsParamSetI;
41
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 import compbio.data.sequence.FastaSequence;
48 import compbio.metadata.Argument;
49 import compbio.metadata.ChunkHolder;
50 import compbio.metadata.JobStatus;
51 import compbio.metadata.JobSubmissionException;
52 import compbio.metadata.Option;
53 import compbio.metadata.ResultNotAvailableException;
54
55 public abstract class AbstractJabaCalcWorker extends AlignCalcWorker
56 {
57
58   protected Jws2Instance service;
59
60   protected WsParamSetI preset;
61
62   protected List<Argument> arguments;
63
64   protected IProgressIndicator guiProgress;
65
66   protected boolean submitGaps = true;
67
68   /**
69    * by default, we filter out non-standard residues before submission
70    */
71   protected boolean filterNonStandardResidues = true;
72
73   /**
74    * Recover any existing parameters for this service
75    */
76   protected void initViewportParams()
77   {
78     if (getCalcId() != null)
79     {
80       ((jalview.gui.AlignViewport) alignViewport).setCalcIdSettingsFor(
81               getCalcId(),
82               new AAConSettings(true, service, this.preset,
83                       (arguments != null)
84                               ? JabaParamStore.getJwsArgsfromJaba(arguments)
85                               : null),
86               true);
87     }
88   }
89
90   /**
91    * 
92    * @return null or a string used to recover all annotation generated by this
93    *         worker
94    */
95   public abstract String getCalcId();
96
97   public WsParamSetI getPreset()
98   {
99     return preset;
100   }
101
102   public List<Argument> getArguments()
103   {
104     return arguments;
105   }
106
107   /**
108    * reconfigure and restart the AAConClient. This method will spawn a new
109    * thread that will wait until any current jobs are finished, modify the
110    * parameters and restart the conservation calculation with the new values.
111    * 
112    * @param newpreset
113    * @param newarguments
114    */
115   public void updateParameters(final WsParamSetI newpreset,
116           final List<Argument> newarguments)
117   {
118     preset = newpreset;
119     arguments = newarguments;
120     calcMan.startWorker(this);
121     initViewportParams();
122   }
123
124   public List<Option> getJabaArguments()
125   {
126     List<Option> newargs = new ArrayList<>();
127     if (preset != null && preset instanceof JabaWsParamSet)
128     {
129       newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
130     }
131     if (arguments != null && arguments.size() > 0)
132     {
133       for (Argument rg : arguments)
134       {
135         if (Option.class.isAssignableFrom(rg.getClass()))
136         {
137           newargs.add((Option) rg);
138         }
139       }
140     }
141     return newargs;
142   }
143
144   protected boolean alignedSeqs = true;
145
146   protected boolean nucleotidesAllowed = false;
147
148   protected boolean proteinAllowed = false;
149
150   /**
151    * record sequences for mapping result back to afterwards
152    */
153   protected boolean bySequence = false;
154
155   protected Map<String, SequenceI> seqNames;
156
157   protected boolean[] gapMap;
158
159   int realw;
160
161   protected int start;
162
163   int end;
164
165   public AbstractJabaCalcWorker(AlignViewportI alignViewport,
166           AlignmentViewPanel alignPanel)
167   {
168     super(alignViewport, alignPanel);
169   }
170
171   public AbstractJabaCalcWorker(Jws2Instance service, AlignFrame alignFrame,
172           WsParamSetI preset, List<Argument> paramset)
173   {
174     this(alignFrame.getCurrentView(), alignFrame.alignPanel);
175     this.guiProgress = alignFrame;
176     this.preset = preset;
177     this.arguments = paramset;
178     this.service = service;
179   }
180
181   /**
182    * 
183    * @return true if the submission thread should attempt to submit data
184    */
185   abstract boolean hasService();
186
187   volatile String rslt = "JOB NOT DEFINED";
188
189   @Override
190   public void run()
191   {
192     if (!hasService())
193     {
194       return;
195     }
196     long progressId = -1;
197
198     int serverErrorsLeft = 3;
199
200     StringBuffer msg = new StringBuffer();
201     try
202     {
203       if (checkDone())
204       {
205         return;
206       }
207       List<compbio.data.sequence.FastaSequence> seqs = getInputSequences(
208               alignViewport.getAlignment(),
209               bySequence ? alignViewport.getSelectionGroup() : null);
210
211       if (seqs == null || !checkValidInputSeqs(true, seqs))
212       {
213         calcMan.workerComplete(this);
214         return;
215       }
216
217       AlignmentAnnotation[] aa = alignViewport.getAlignment()
218               .getAlignmentAnnotation();
219       if (guiProgress != null)
220       {
221         guiProgress.setProgressBar("JABA " + getServiceActionText(),
222                 progressId = System.currentTimeMillis());
223       }
224       rslt = submitToService(seqs);
225       if (guiProgress != null)
226       {
227         guiProgress.registerHandler(progressId,
228                 new IProgressIndicatorHandler()
229                 {
230
231                   @Override
232                   public boolean cancelActivity(long id)
233                   {
234                     cancelCurrentJob();
235                     return true;
236                   }
237
238                   @Override
239                   public boolean canCancel()
240                   {
241                     return true;
242                   }
243                 });
244       }
245       boolean finished = false;
246       long rpos = 0;
247       do
248       {
249         JobStatus status = getJobStatus(rslt);
250         if (status.equals(JobStatus.FINISHED))
251         {
252           finished = true;
253         }
254         if (calcMan.isPending(this) && isInteractiveUpdate())
255         {
256           finished = true;
257           // cancel this job and yield to the new job
258           try
259           {
260             if (cancelJob(rslt))
261             {
262               jalview.bin.Console
263                       .errPrintln("Cancelled AACon job: " + rslt);
264             }
265             else
266             {
267               jalview.bin.Console
268                       .errPrintln("FAILED TO CANCEL AACon job: " + rslt);
269             }
270
271           } catch (Exception x)
272           {
273
274           }
275           rslt = "CANCELLED JOB";
276           return;
277         }
278         long cpos;
279         ChunkHolder stats = null;
280         do
281         {
282           cpos = rpos;
283           boolean retry = false;
284           do
285           {
286             try
287             {
288               stats = pullExecStatistics(rslt, rpos);
289             } catch (Exception x)
290             {
291
292               if (x.getMessage().contains(
293                       "Position in a file could not be negative!"))
294               {
295                 // squash index out of bounds exception- seems to happen for
296                 // disorder predictors which don't (apparently) produce any
297                 // progress information and JABA server throws an exception
298                 // because progress length is -1.
299                 stats = null;
300               }
301               else
302               {
303                 if (--serverErrorsLeft > 0)
304                 {
305                   retry = true;
306                   try
307                   {
308                     Thread.sleep(200);
309                   } catch (InterruptedException q)
310                   {
311                   }
312                   ;
313                 }
314                 else
315                 {
316                   throw x;
317                 }
318               }
319             }
320           } while (retry);
321           if (stats != null)
322           {
323             System.out.print(stats.getChunk());
324             msg.append(stats);
325             rpos = stats.getNextPosition();
326           }
327         } while (stats != null && rpos > cpos);
328
329         if (!finished && status.equals(JobStatus.FAILED))
330         {
331           try
332           {
333             Thread.sleep(200);
334           } catch (InterruptedException x)
335           {
336           }
337           ;
338         }
339       } while (!finished);
340       if (serverErrorsLeft > 0)
341       {
342         try
343         {
344           Thread.sleep(200);
345         } catch (InterruptedException x)
346         {
347         }
348         if (collectAnnotationResultsFor(rslt))
349         {
350           Console.debug("Updating result annotation from Job " + rslt
351                   + " at " + service.getUri());
352           updateResultAnnotation(true);
353           ap.adjustAnnotationHeight();
354         }
355       }
356     }
357
358     catch (JobSubmissionException x)
359     {
360
361       jalview.bin.Console.errPrintln(
362               "submission error with " + getServiceActionText() + " :");
363       x.printStackTrace();
364       calcMan.disableWorker(this);
365     } catch (ResultNotAvailableException x)
366     {
367       jalview.bin.Console.errPrintln("collection error:\nJob ID: " + rslt);
368       x.printStackTrace();
369       calcMan.disableWorker(this);
370
371     } catch (OutOfMemoryError error)
372     {
373       calcMan.disableWorker(this);
374
375       // consensus = null;
376       // hconsensus = null;
377       ap.raiseOOMWarning(getServiceActionText(), error);
378     } catch (Exception x)
379     {
380       calcMan.disableWorker(this);
381
382       // consensus = null;
383       // hconsensus = null;
384       System.err
385               .println("Blacklisting worker due to unexpected exception:");
386       x.printStackTrace();
387     } finally
388     {
389
390       calcMan.workerComplete(this);
391       if (ap != null)
392       {
393         calcMan.workerComplete(this);
394         if (guiProgress != null && progressId != -1)
395         {
396           guiProgress.setProgressBar("", progressId);
397         }
398         // TODO: may not need to paintAlignment again !
399         ap.paintAlignment(false, false);
400       }
401       if (msg.length() > 0)
402       {
403         // TODO: stash message somewhere in annotation or alignment view.
404         // code below shows result in a text box popup
405         /*
406          * jalview.gui.CutAndPasteTransfer cap = new
407          * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString());
408          * jalview.gui.Desktop.addInternalFrame(cap,
409          * "Job Status for "+getServiceActionText(), 600, 400);
410          */
411       }
412     }
413
414   }
415
416   /**
417    * validate input for dynamic/non-dynamic update context
418    * 
419    * @param dynamic
420    * @param seqs
421    * @return true if input is valid
422    */
423   abstract boolean checkValidInputSeqs(boolean dynamic,
424           List<FastaSequence> seqs);
425
426   abstract String submitToService(
427           List<compbio.data.sequence.FastaSequence> seqs)
428           throws JobSubmissionException;
429
430   abstract boolean cancelJob(String rslt) throws Exception;
431
432   abstract JobStatus getJobStatus(String rslt) throws Exception;
433
434   abstract ChunkHolder pullExecStatistics(String rslt, long rpos);
435
436   abstract boolean collectAnnotationResultsFor(String rslt)
437           throws ResultNotAvailableException;
438
439   public void cancelCurrentJob()
440   {
441     try
442     {
443       String id = rslt;
444       if (cancelJob(rslt))
445       {
446         jalview.bin.Console.errPrintln("Cancelled job " + id);
447       }
448       else
449       {
450         jalview.bin.Console
451                 .errPrintln("Job " + id + " couldn't be cancelled.");
452       }
453     } catch (Exception q)
454     {
455       q.printStackTrace();
456     }
457   }
458
459   /**
460    * Interactive updating. Analysis calculations that work on the currently
461    * displayed alignment data should cancel existing jobs when the input data
462    * has changed.
463    * 
464    * @return true if a running job should be cancelled because new input data is
465    *         available for analysis
466    */
467   abstract boolean isInteractiveUpdate();
468
469   public List<FastaSequence> getInputSequences(AlignmentI alignment,
470           AnnotatedCollectionI inputSeqs)
471   {
472     if (alignment == null || alignment.getWidth() <= 0
473             || alignment.getSequences() == null || alignment.isNucleotide()
474                     ? !nucleotidesAllowed
475                     : !proteinAllowed)
476     {
477       return null;
478     }
479     if (inputSeqs == null || inputSeqs.getWidth() <= 0
480             || inputSeqs.getSequences() == null
481             || inputSeqs.getSequences().size() < 1)
482     {
483       inputSeqs = alignment;
484     }
485
486     List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<>();
487
488     int minlen = 10;
489     int ln = -1;
490     if (bySequence)
491     {
492       seqNames = new HashMap<>();
493     }
494     gapMap = new boolean[0];
495     start = inputSeqs.getStartRes();
496     end = inputSeqs.getEndRes();
497
498     for (SequenceI sq : (inputSeqs.getSequences()))
499     {
500       if (bySequence
501               ? sq.findPosition(end + 1)
502                       - sq.findPosition(start + 1) > minlen - 1
503               : sq.getEnd() - sq.getStart() > minlen - 1)
504       {
505         String newname = SeqsetUtils.unique_name(seqs.size() + 1);
506         // make new input sequence with or without gaps
507         if (seqNames != null)
508         {
509           seqNames.put(newname, sq);
510         }
511         FastaSequence seq;
512         if (submitGaps)
513         {
514           seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
515                   sq.getSequenceAsString()));
516           if (gapMap == null || gapMap.length < seq.getSequence().length())
517           {
518             boolean[] tg = gapMap;
519             gapMap = new boolean[seq.getLength()];
520             System.arraycopy(tg, 0, gapMap, 0, tg.length);
521             for (int p = tg.length; p < gapMap.length; p++)
522             {
523               gapMap[p] = false; // init as a gap
524             }
525           }
526           for (int apos : sq.gapMap())
527           {
528             char sqc = sq.getCharAt(apos);
529             if (!filterNonStandardResidues
530                     || (sq.isProtein() ? ResidueProperties.aaIndex[sqc] < 20
531                             : ResidueProperties.nucleotideIndex[sqc] < 5))
532             {
533               gapMap[apos] = true; // aligned and real amino acid residue
534             }
535             ;
536           }
537         }
538         else
539         {
540           seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
541                   AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
542                           sq.getSequenceAsString(start, end + 1))));
543         }
544         if (seq.getSequence().length() > ln)
545         {
546           ln = seq.getSequence().length();
547         }
548       }
549     }
550     if (alignedSeqs && submitGaps)
551     {
552       realw = 0;
553       for (int i = 0; i < gapMap.length; i++)
554       {
555         if (gapMap[i])
556         {
557           realw++;
558         }
559       }
560       // try real hard to return something submittable
561       // TODO: some of AAcon measures need a minimum of two or three amino
562       // acids at each position, and AAcon doesn't gracefully degrade.
563       for (int p = 0; p < seqs.size(); p++)
564       {
565         FastaSequence sq = seqs.get(p);
566         int l = sq.getSequence().length();
567         // strip gapped columns
568         char[] padded = new char[realw],
569                 orig = sq.getSequence().toCharArray();
570         for (int i = 0, pp = 0; i < realw; pp++)
571         {
572           if (gapMap[pp])
573           {
574             if (orig.length > pp)
575             {
576               padded[i++] = orig[pp];
577             }
578             else
579             {
580               padded[i++] = '-';
581             }
582           }
583         }
584         seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
585                 new String(padded)));
586       }
587     }
588     return seqs;
589   }
590
591   @Override
592   public void updateAnnotation()
593   {
594     updateResultAnnotation(false);
595   }
596
597   public abstract void updateResultAnnotation(boolean immediate);
598
599   public abstract String getServiceActionText();
600
601   /**
602    * notify manager that we have started, and wait for a free calculation slot
603    * 
604    * @return true if slot is obtained and work still valid, false if another
605    *         thread has done our work for us.
606    */
607   protected boolean checkDone()
608   {
609     calcMan.notifyStart(this);
610     ap.paintAlignment(false, false);
611     while (!calcMan.notifyWorking(this))
612     {
613       if (calcMan.isWorking(this))
614       {
615         return true;
616       }
617       try
618       {
619         if (ap != null)
620         {
621           ap.paintAlignment(false, false);
622         }
623
624         Thread.sleep(200);
625       } catch (Exception ex)
626       {
627         ex.printStackTrace();
628       }
629     }
630     if (alignViewport.isClosed())
631     {
632       abortAndDestroy();
633       return true;
634     }
635     return false;
636   }
637
638   protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
639   {
640     List<AlignmentAnnotation> our = ourAnnots;
641     ourAnnots = ourAnnot;
642     AlignmentI alignment = alignViewport.getAlignment();
643     if (our != null)
644     {
645       if (our.size() > 0)
646       {
647         for (AlignmentAnnotation an : our)
648         {
649           if (!ourAnnots.contains(an))
650           {
651             // remove the old annotation
652             alignment.deleteAnnotation(an);
653           }
654         }
655       }
656       our.clear();
657
658       ap.adjustAnnotationHeight();
659     }
660   }
661
662 }