bugfix - use currently held parameters
[jalview.git] / src / jalview / ws / jws2 / JabawsAlignCalcWorker.java
index 7ee6e08..7ef85d3 100644 (file)
@@ -6,10 +6,9 @@ import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
-import jalview.datamodel.Annotation;
-import jalview.datamodel.SequenceGroup;
 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;
@@ -20,7 +19,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import com.sun.xml.internal.ws.developer.ServerSideException;
 import compbio.data.msa.SequenceAnnotation;
 import compbio.data.sequence.FastaSequence;
 import compbio.data.sequence.ScoreManager;
@@ -50,10 +48,13 @@ 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;
     aaservice = (SequenceAnnotation) service.service;
@@ -74,14 +75,14 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
    * reconfigure and restart the AAConsClient. 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
    */
   public void updateParameters(final WsParamSetI newpreset,
           final List<Argument> newarguments)
   {
-    if (calcMan.isWorking(this))
+    if (false) // || calcMan.isWorking(this))
     {
       new Thread(new Runnable()
       {
@@ -135,6 +136,9 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     {
       return;
     }
+    long progressId = -1;
+
+    String rslt = "JOB NOT DEFINED";
 
     try
     {
@@ -153,9 +157,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);
       }
@@ -180,6 +187,28 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
         {
           finished = true;
         }
+        if (calcMan.isPending(this) && this instanceof AAConsClient)
+        {
+          finished = true;
+          // cancel this job and yield to the new job
+          try
+          {
+            if (aaservice.cancelJob(rslt))
+            {
+              System.err.println("Cancelled AACon job: " + rslt);
+            }
+            else
+            {
+              System.err.println("FAILED TO CANCELL AACon job: " + rslt);
+            }
+
+          } catch (Exception x)
+          {
+
+          }
+
+          return;
+        }
         long cpos;
         ChunkHolder stats;
         do
@@ -191,20 +220,14 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
           } catch (Exception x)
           {
 
-            if (x.getCause() instanceof ServerSideException)
+            if (x.getMessage().contains(
+                    "Position in a file could not be negative!"))
             {
-              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
-              {
-                throw x;
-              }
+              // 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
             {
@@ -250,7 +273,7 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
       calcMan.workerCannotRun(this);
     } catch (ResultNotAvailableException x)
     {
-      System.err.println("collection error:");
+      System.err.println("collection error:\nJob ID: " + rslt);
       x.printStackTrace();
       calcMan.workerCannotRun(this);
 
@@ -276,12 +299,18 @@ 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);
       }
     }
 
   }
 
+  @Override
   public void updateAnnotation()
   {
     updateResultAnnotation(false);
@@ -298,19 +327,20 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
   boolean nucleotidesAllowed = false;
 
   boolean proteinAllowed = false;
-  
+
   /**
    * record sequences for mapping result back to afterwards
    */
-  protected boolean bySequence=false;
+  protected boolean bySequence = false;
 
-  Map<String,SequenceI> seqNames;
+  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())
+            // || (alignedSeqs && !alignment.isAligned() && !submitGaps)
             || alignment.isNucleotide() ? !nucleotidesAllowed
             : !proteinAllowed)
     {
@@ -319,67 +349,99 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     List<compbio.data.sequence.FastaSequence> seqs = new ArrayList<compbio.data.sequence.FastaSequence>();
 
     int minlen = 10;
+    int ln = -1;
     if (bySequence)
     {
-      seqNames=new HashMap<String,SequenceI>();
+      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);
         // make new input sequence with or without gaps
-        if (seqNames!=null) {
-          seqNames.put(newname,sq);
+        if (seqNames != null)
+        {
+          seqNames.put(newname, sq);
         }
-        seqs.add(new compbio.data.sequence.FastaSequence(newname,
-                (submitGaps) ? sq.getSequenceAsString() : AlignSeq
+        FastaSequence seq;
+        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();
+        }
       }
     }
-    return seqs;
-  }
-
-  protected AlignmentAnnotation findOrCreate(String name, boolean autoCalc,
-          SequenceI seqRef, SequenceGroup groupRef)
-  {
-    for (AlignmentAnnotation annot : alignViewport.getAlignment()
-            .getAlignmentAnnotation())
+    if (alignedSeqs && submitGaps)
     {
-      if (annot.autoCalculated == autoCalc
-              && annot.getCalcId().equals(name)
-              && annot.sequenceRef == seqRef && annot.groupRef == groupRef)
+      realw = 0;
+      for (int i=0;i<gapMap.length;i++)
       {
-        return annot;
+        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++)
+      {
+        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++)
+        {
+          if (gapMap[pp])
+          {
+            if (orig.length>pp)
+            {
+              padded[i++]=orig[pp];
+            } else {
+              padded[i++]='-';
+            }       
+          }
+        }
+        seqs.set(p, new compbio.data.sequence.FastaSequence(sq.getId(),
+                  new String(padded)));
       }
     }
-    AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
-            new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
-    annot.hasText = false;
-    annot.setCalcId(new String(name));
-    annot.autoCalculated = autoCalc;
-    if (seqRef != null)
-    {
-      annot.setSequenceRef(seqRef);
-    }
-    annot.groupRef = groupRef;
-    alignViewport.getAlignment().addAnnotation(annot);
-
-    return annot;
+    return seqs;
   }
 
   /**
    * 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.
    */
   boolean checkDone()
   {
     calcMan.notifyStart(this);
-    // ap.paintAlignment(false);
+    ap.paintAlignment(false);
     while (!calcMan.notifyWorking(this))
     {
       if (calcMan.isWorking(this))