RNAalifoldClient updated to be more like AAConClient and now (attempts) to support...
[jalview.git] / src / jalview / ws / jws2 / JabawsAlignCalcWorker.java
index d8807e7..720e76a 100644 (file)
@@ -1,14 +1,29 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
+ * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ */
 package jalview.ws.jws2;
 
 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.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.IProgressIndicator;
@@ -17,15 +32,12 @@ 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;
@@ -41,6 +53,7 @@ import compbio.metadata.WrongParameterException;
 public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
 {
   Jws2Instance service;
+
   @SuppressWarnings("unchecked")
   protected SequenceAnnotation aaservice;
 
@@ -81,10 +94,10 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
   }
 
   /**
-   * reconfigure and restart the AAConsClient. This method will spawn a new
+   * reconfigure and restart the AAConClient. This method will spawn a new
    * thread that will wait until any current jobs are finished, modify the
    * parameters and restart the conservation calculation with the new values.
-   *
+   * 
    * @param newpreset
    * @param newarguments
    */
@@ -128,7 +141,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     int serverErrorsLeft = 3;
 
     String rslt = "JOB NOT DEFINED";
-    StringBuffer msg=new StringBuffer();
+    StringBuffer msg = new StringBuffer();
     try
     {
       if (checkDone())
@@ -151,7 +164,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
         guiProgress.setProgressBar("JABA " + getServiceActionText(),
                 progressId = System.currentTimeMillis());
       }
-      if (preset == null && arguments==null)
+      if (preset == null && arguments == null)
       {
         rslt = aaservice.analize(seqs);
       }
@@ -163,7 +176,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
         } catch (WrongParameterException x)
         {
           throw new JobSubmissionException(
-                  "Invalid paremeter set. Check Jalview implementation.", x);
+                  "Invalid parameter set. Check Jalview implementation.", x);
 
         }
       }
@@ -176,7 +189,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
         {
           finished = true;
         }
-        if (calcMan.isPending(this) && this instanceof AAConsClient)
+        if (calcMan.isPending(this) && this instanceof AAConClient)
         {
           finished = true;
           // cancel this job and yield to the new job
@@ -188,7 +201,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
             }
             else
             {
-              System.err.println("FAILED TO CANCELL AACon job: " + rslt);
+              System.err.println("FAILED TO CANCEL AACon job: " + rslt);
             }
 
           } catch (Exception x)
@@ -209,7 +222,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
             try
             {
               stats = aaservice.pullExecStatistics(rslt, rpos);
-            }  catch (Exception x)
+            } catch (Exception x)
             {
 
               if (x.getMessage().contains(
@@ -226,10 +239,16 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
                 if (--serverErrorsLeft > 0)
                 {
                   retry = true;
-                  try {
+                  try
+                  {
                     Thread.sleep(200);
-                  } catch (InterruptedException q) {};
-                } else {
+                  } catch (InterruptedException q)
+                  {
+                  }
+                  ;
+                }
+                else
+                {
                   throw x;
                 }
               }
@@ -254,7 +273,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
           ;
         }
       } while (!finished);
-      if (serverErrorsLeft>0)
+      if (serverErrorsLeft > 0)
       {
         try
         {
@@ -270,6 +289,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
                   .debug("Updating result annotation from Job " + rslt
                           + " at " + service.getUri());
           updateResultAnnotation(true);
+          ap.adjustAnnotationHeight();
         }
       }
     }
@@ -310,19 +330,22 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
       if (ap != null)
       {
         calcMan.workerComplete(this);
-        if (guiProgress != null && progressId!=-1)
+        if (guiProgress != null && progressId != -1)
         {
           guiProgress.setProgressBar("", progressId);
         }
         ap.paintAlignment(true);
       }
-      if (msg.length()>0)
+      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); */
+        /*
+         * jalview.gui.CutAndPasteTransfer cap = new
+         * jalview.gui.CutAndPasteTransfer(); cap.setText(msg.toString());
+         * jalview.gui.Desktop.addInternalFrame(cap,
+         * "Job Status for "+getServiceActionText(), 600, 400);
+         */
       }
     }
 
@@ -352,8 +375,11 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
   protected boolean bySequence = false;
 
   Map<String, SequenceI> seqNames;
+
   boolean[] gapMap;
+
   int realw;
+
   public List<FastaSequence> getInputSequences(AlignmentI alignment)
   {
     if (alignment == null || alignment.getWidth() <= 0
@@ -372,7 +398,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     {
       seqNames = new HashMap<String, SequenceI>();
     }
-    gapMap=new boolean[0];
+    gapMap = new boolean[0];
     for (SequenceI sq : ((List<SequenceI>) alignment.getSequences()))
     {
       if (sq.getEnd() - sq.getStart() > minlen - 1)
@@ -386,25 +412,28 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
         FastaSequence seq;
         if (submitGaps)
         {
-          seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,sq.getSequenceAsString()));
-          if (gapMap==null || gapMap.length<seq.getSequence().length())
+          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()];
+            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++)
+            for (int p = tg.length; p < gapMap.length; p++)
             {
-              gapMap[p]=false; // init as a gap
+              gapMap[p] = false; // init as a gap
             }
           }
-          for (int apos:sq.gapMap()) {
-            gapMap[apos]=true; // aligned.
+          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())));
+        }
+        else
+        {
+          seqs.add(seq = new compbio.data.sequence.FastaSequence(newname,
+                  AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
+                          sq.getSequenceAsString())));
         }
         if (seq.getSequence().length() > ln)
         {
@@ -415,7 +444,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     if (alignedSeqs && submitGaps)
     {
       realw = 0;
-      for (int i=0;i<gapMap.length;i++)
+      for (int i = 0; i < gapMap.length; i++)
       {
         if (gapMap[i])
         {
@@ -423,28 +452,31 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
         }
       }
       // 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.
+      // TODO: some of AAcon measures need a minimum of two or three amino
+      // acids at each position, and AAcon doesn't gracefully degrade.
       for (int p = 0; p < seqs.size(); p++)
       {
         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[realw], orig = sq.getSequence()
+                .toCharArray();
+        for (int i = 0, pp = 0; i < realw; pp++)
         {
           if (gapMap[pp])
           {
-            if (orig.length>pp)
+            if (orig.length > pp)
             {
-              padded[i++]=orig[pp];
-            } else {
-              padded[i++]='-';
-            }       
+              padded[i++] = orig[pp];
+            }
+            else
+            {
+              padded[i++] = '-';
+            }
           }
         }
         seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
-                  new String(padded)));
+                new String(padded)));
       }
     }
     return seqs;
@@ -452,7 +484,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
 
   /**
    * notify manager that we have started, and wait for a free calculation slot
-   *
+   * 
    * @return true if slot is obtained and work still valid, false if another
    *         thread has done our work for us.
    */
@@ -515,8 +547,8 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     // 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);
+    constructAnnotationFromScore(annotation, 0, dseq.getLength(), scr);
+    annotation.createSequenceMapping(dseq, base, false);
     annotation.adjustForAlignment();
     dseq.addAlignmentAnnotation(annotation);
     ourAnnot.add(annotation);
@@ -529,7 +561,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     Annotation[] elm = new Annotation[alWidth];
     Iterator<Float> vals = scr.getScores().iterator();
     float m = 0f, x = 0f;
-    for (int i = base; vals.hasNext(); i++)
+    for (int i = 0; vals.hasNext(); i++)
     {
       float val = vals.next().floatValue();
       if (i == 0)