formatting
[jalview.git] / src / jalview / ws / jws2 / JabawsAlignCalcWorker.java
index 029c842..58cb3ac 100644 (file)
@@ -4,24 +4,31 @@ import jalview.analysis.AlignSeq;
 import jalview.analysis.SeqsetUtils;
 import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
+import jalview.bin.Cache;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
-import jalview.datamodel.SequenceGroup;
+import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
+import jalview.gui.IProgressIndicator;
 import jalview.workers.AlignCalcWorker;
 import jalview.ws.jws2.dm.JabaWsParamSet;
 import jalview.ws.jws2.jabaws2.Jws2Instance;
 import jalview.ws.params.WsParamSetI;
 
+import java.awt.Color;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import com.sun.xml.internal.ws.client.ClientTransportException;
+
 import compbio.data.msa.SequenceAnnotation;
 import compbio.data.sequence.FastaSequence;
+import compbio.data.sequence.Score;
 import compbio.data.sequence.ScoreManager;
 import compbio.metadata.Argument;
 import compbio.metadata.ChunkHolder;
@@ -33,6 +40,7 @@ import compbio.metadata.WrongParameterException;
 
 public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
 {
+  Jws2Instance service;
 
   @SuppressWarnings("unchecked")
   protected SequenceAnnotation aaservice;
@@ -49,12 +57,16 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     super(alignViewport, alignPanel);
   }
 
+  IProgressIndicator guiProgress;
+
   public JabawsAlignCalcWorker(Jws2Instance service, AlignFrame alignFrame,
           WsParamSetI preset, List<Argument> paramset)
   {
     this(alignFrame.getCurrentView(), alignFrame.alignPanel);
+    this.guiProgress = alignFrame;
     this.preset = preset;
     this.arguments = paramset;
+    this.service = service;
     aaservice = (SequenceAnnotation) service.service;
 
   }
@@ -80,31 +92,9 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
   public void updateParameters(final WsParamSetI newpreset,
           final List<Argument> newarguments)
   {
-    if (calcMan.isWorking(this))
-    {
-      new Thread(new Runnable()
-      {
-        @Override
-        public void run()
-        {
-
-          try
-          {
-            Thread.sleep(200);
-          } catch (InterruptedException x)
-          {
-          }
-          ;
-          updateParameters(newpreset, newarguments);
-        }
-      }).start();
-    }
-    else
-    {
-      preset = newpreset;
-      arguments = newarguments;
-      calcMan.startWorker(this);
-    }
+    preset = newpreset;
+    arguments = newarguments;
+    calcMan.startWorker(this);
   }
 
   public List<Option> getJabaArguments()
@@ -134,7 +124,12 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     {
       return;
     }
+    long progressId = -1;
+
+    int serverErrorsLeft = 3;
 
+    String rslt = "JOB NOT DEFINED";
+    StringBuffer msg = new StringBuffer();
     try
     {
       if (checkDone())
@@ -152,9 +147,12 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
 
       AlignmentAnnotation[] aa = alignViewport.getAlignment()
               .getAlignmentAnnotation();
-
-      String rslt;
-      if (preset == null)
+      if (guiProgress != null)
+      {
+        guiProgress.setProgressBar("JABA " + getServiceActionText(),
+                progressId = System.currentTimeMillis());
+      }
+      if (preset == null && arguments == null)
       {
         rslt = aaservice.analize(seqs);
       }
@@ -179,34 +177,69 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
         {
           finished = true;
         }
-        long cpos;
-        ChunkHolder stats;
-        do
+        if (calcMan.isPending(this) && this instanceof AAConsClient)
         {
-          cpos = rpos;
+          finished = true;
+          // cancel this job and yield to the new job
           try
           {
-            stats = aaservice.pullExecStatistics(rslt, rpos);
-          } catch (Exception x)
-          {
-
-            if (x.getMessage().contains(
-                    "Position in a file could not be negative!"))
+            if (aaservice.cancelJob(rslt))
             {
-              // squash index out of bounds exception- seems to happen for
-              // disorder predictors which don't (apparently) produce any
-              // progress information and JABA server throws an exception
-              // because progress length is -1.
-              stats = null;
+              System.err.println("Cancelled AACon job: " + rslt);
             }
             else
             {
-              throw x;
+              System.err.println("FAILED TO CANCELL AACon job: " + rslt);
             }
+
+          } catch (Exception x)
+          {
+
           }
+
+          return;
+        }
+        long cpos;
+        ChunkHolder stats = null;
+        do
+        {
+          cpos = rpos;
+          boolean retry = false;
+          do
+          {
+            try
+            {
+              stats = aaservice.pullExecStatistics(rslt, rpos);
+            }  catch (Exception x)
+            {
+
+              if (x.getMessage().contains(
+                      "Position in a file could not be negative!"))
+              {
+                // squash index out of bounds exception- seems to happen for
+                // disorder predictors which don't (apparently) produce any
+                // progress information and JABA server throws an exception
+                // because progress length is -1.
+                stats = null;
+              }
+              else
+              {
+                if (--serverErrorsLeft > 0)
+                {
+                  retry = true;
+                  try {
+                    Thread.sleep(200);
+                  } catch (InterruptedException q) {};
+                } else {
+                  throw x;
+                }
+              }
+            }
+          } while (retry);
           if (stats != null)
           {
             System.out.print(stats.getChunk());
+            msg.append(stats);
             rpos = stats.getNextPosition();
           }
         } while (stats != null && rpos > cpos);
@@ -221,29 +254,37 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
           }
           ;
         }
-
       } while (!finished);
-      try
-      {
-        Thread.sleep(200);
-      } catch (InterruptedException x)
+      if (serverErrorsLeft>0)
       {
+        try
+        {
+          Thread.sleep(200);
+        } catch (InterruptedException x)
+        {
+        }
+        ;
+        scoremanager = aaservice.getAnnotation(rslt);
+        if (scoremanager != null)
+        {
+          jalview.bin.Cache.log
+                  .debug("Updating result annotation from Job " + rslt
+                          + " at " + service.getUri());
+          updateResultAnnotation(true);
+        }
       }
-      ;
-      scoremanager = aaservice.getAnnotation(rslt);
-      if (scoremanager != null)
-      {
-        updateResultAnnotation(true);
-      }
-    } catch (JobSubmissionException x)
+    }
+
+    catch (JobSubmissionException x)
     {
 
-      System.err.println("submission error:");
+      System.err.println("submission error with " + getServiceActionText()
+              + " :");
       x.printStackTrace();
       calcMan.workerCannotRun(this);
     } catch (ResultNotAvailableException x)
     {
-      System.err.println("collection error:");
+      System.err.println("collection error:\nJob ID: " + rslt);
       x.printStackTrace();
       calcMan.workerCannotRun(this);
 
@@ -269,12 +310,29 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
       calcMan.workerComplete(this);
       if (ap != null)
       {
+        calcMan.workerComplete(this);
+        if (guiProgress != null && progressId != -1)
+        {
+          guiProgress.setProgressBar("", progressId);
+        }
         ap.paintAlignment(true);
       }
+      if (msg.length() > 0)
+      {
+        // TODO: stash message somewhere in annotation or alignment view.
+        // code below shows result in a text box popup
+        /*
+         * jalview.gui.CutAndPasteTransfer cap = new
+         * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString());
+         * jalview.gui.Desktop.addInternalFrame(cap,
+         * "Job Status for "+getServiceActionText(), 600, 400);
+         */
+      }
     }
 
   }
 
+  @Override
   public void updateAnnotation()
   {
     updateResultAnnotation(false);
@@ -299,12 +357,15 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
 
   Map<String, SequenceI> seqNames;
 
+  boolean[] gapMap;
+
+  int realw;
+
   public List<FastaSequence> getInputSequences(AlignmentI alignment)
   {
-
     if (alignment == null || alignment.getWidth() <= 0
             || alignment.getSequences() == null
-//             || (alignedSeqs && !alignment.isAligned() && !submitGaps)
+            // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
             || alignment.isNucleotide() ? !nucleotidesAllowed
             : !proteinAllowed)
     {
@@ -313,14 +374,14 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
 
     int minlen = 10;
-    int ln=-1;
+    int ln = -1;
     if (bySequence)
     {
       seqNames = new HashMap<String, SequenceI>();
     }
+    gapMap = new boolean[0];
     for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
     {
-
       if (sq.getEnd() - sq.getStart() > minlen - 1)
       {
         String newname = SeqsetUtils.unique_name(seqs.size() + 1);
@@ -330,11 +391,32 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
           seqNames.put(newname, sq);
         }
         FastaSequence seq;
-        seqs.add(seq=new compbio.data.sequence.FastaSequence(newname,
-                (submitGaps) ? sq.getSequenceAsString() : AlignSeq
-                        .extractGaps(jalview.util.Comparison.GapChars,
-                                sq.getSequenceAsString())));
-        if (seq.getSequence().length()>ln)
+        if (submitGaps)
+        {
+          seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
+                  sq.getSequenceAsString()));
+          if (gapMap == null || gapMap.length < seq.getSequence().length())
+          {
+            boolean[] tg = gapMap;
+            gapMap = new boolean[seq.getLength()];
+            System.arraycopy(tg, 0, gapMap, 0, tg.length);
+            for (int p = tg.length; p < gapMap.length; p++)
+            {
+              gapMap[p] = false; // init as a gap
+            }
+          }
+          for (int apos : sq.gapMap())
+          {
+            gapMap[apos] = true; // aligned.
+          }
+        }
+        else
+        {
+          seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
+                  AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
+                          sq.getSequenceAsString())));
+        }
+        if (seq.getSequence().length() > ln)
         {
           ln = seq.getSequence().length();
         }
@@ -342,24 +424,41 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     }
     if (alignedSeqs && submitGaps)
     {
+      realw = 0;
+      for (int i = 0; i < gapMap.length; i++)
+      {
+        if (gapMap[i])
+        {
+          realw++;
+        }
+      }
       // try real hard to return something submittable
-      // TODO: some of AAcons measures need a minimum of two or three amino acids at each position, and aacons doesn't gracefully degrade.
-      for (int p=0; p<seqs.size();p++)
+      // TODO: some of AAcons measures need a minimum of two or three amino
+      // acids at each position, and aacons doesn't gracefully degrade.
+      for (int p = 0; p < seqs.size(); p++)
       {
-        FastaSequence sq=seqs.get(p);
-        int l=sq.getSequence().length();
-        if (l<ln)
+        FastaSequence sq = seqs.get(p);
+        int l = sq.getSequence().length();
+        // strip gapped columns
+        char[] padded = new char[realw], orig = sq.getSequence()
+                .toCharArray();
+        for (int i = 0, pp = 0; i < realw; pp++)
         {
-          char[] padded=new char[ln];
-          System.arraycopy(sq.getSequence().toCharArray(),0,padded,0,sq.getSequence().length());
-          while (l<ln)
+          if (gapMap[pp])
           {
-            padded[l++]='-';
+            if (orig.length > pp)
+            {
+              padded[i++] = orig[pp];
+            }
+            else
+            {
+              padded[i++] = '-';
+            }
           }
-          seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(), new String(padded)));
         }
+        seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
+                new String(padded)));
       }
-      
     }
     return seqs;
   }
@@ -373,7 +472,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
   boolean checkDone()
   {
     calcMan.notifyStart(this);
-    // ap.paintAlignment(false);
+    ap.paintAlignment(false);
     while (!calcMan.notifyWorking(this))
     {
       if (calcMan.isWorking(this))
@@ -401,4 +500,112 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     return false;
   }
 
+  protected void createAnnotationRowsForScores(
+          List<AlignmentAnnotation> ourAnnot, String calcId, int alWidth,
+          Score scr)
+  {
+    // simple annotation row
+    AlignmentAnnotation annotation = alignViewport.getAlignment()
+            .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
+                    null);
+    if (alWidth == gapMap.length) // scr.getScores().size())
+    {
+      constructAnnotationFromScore(annotation, 0, alWidth, scr);
+      ourAnnot.add(annotation);
+    }
+  }
+
+  protected AlignmentAnnotation createAnnotationRowsForScores(
+          List<AlignmentAnnotation> ourAnnot, String typeName,
+          String calcId, SequenceI dseq, int base, Score scr)
+  {
+    System.out.println("Creating annotation on dseq:" + dseq.getStart()
+            + " base is " + base + " and length=" + dseq.getLength()
+            + " == " + scr.getScores().size());
+    // AlignmentAnnotation annotation = new AlignmentAnnotation(
+    // scr.getMethod(), typeName, new Annotation[]
+    // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
+    // annotation.setCalcId(calcId);
+    AlignmentAnnotation annotation = alignViewport.getAlignment()
+            .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
+    constructAnnotationFromScore(annotation, base, dseq.getLength(), scr);
+    annotation.createSequenceMapping(dseq, dseq.findPosition(base), false);
+    annotation.adjustForAlignment();
+    dseq.addAlignmentAnnotation(annotation);
+    ourAnnot.add(annotation);
+    return annotation;
+  }
+
+  private void constructAnnotationFromScore(AlignmentAnnotation annotation,
+          int base, int alWidth, Score scr)
+  {
+    Annotation[] elm = new Annotation[alWidth];
+    Iterator<Float> vals = scr.getScores().iterator();
+    float m = 0f, x = 0f;
+    for (int i = base; vals.hasNext(); i++)
+    {
+      float val = vals.next().floatValue();
+      if (i == 0)
+      {
+        m = val;
+        x = val;
+      }
+      else
+      {
+        if (m > val)
+        {
+          m = val;
+        }
+        ;
+        if (x < val)
+        {
+          x = val;
+        }
+      }
+      // if we're at a gapped column then skip to next ungapped position
+      if (gapMap != null && gapMap.length > 0)
+      {
+        while (!gapMap[i])
+        {
+          elm[i++] = new Annotation("", "", ' ', Float.NaN);
+        }
+      }
+      elm[i] = new Annotation("", "" + val, ' ', val);
+    }
+
+    annotation.annotations = elm;
+    annotation.belowAlignment = true;
+    if (x < 0)
+    {
+      x = 0;
+    }
+    x += (x - m) * 0.1;
+    annotation.graphMax = x;
+    annotation.graphMin = m;
+    annotation.validateRangeAndDisplay();
+  }
+
+  protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
+  {
+    List<AlignmentAnnotation> our = ourAnnots;
+    ourAnnots = ourAnnot;
+    AlignmentI alignment = alignViewport.getAlignment();
+    if (our != null)
+    {
+      if (our.size() > 0)
+      {
+        for (AlignmentAnnotation an : our)
+        {
+          if (!ourAnnots.contains(an))
+          {
+            // remove the old annotation
+            alignment.deleteAnnotation(an);
+          }
+        }
+      }
+      our.clear();
+
+      ap.adjustAnnotationHeight();
+    }
+  }
 }