refactored and ensured alCodonMappings are propagated to new alignments
[jalview.git] / src / jalview / ws / MsaWSThread.java
index 62f076a..0dedce2 100644 (file)
@@ -1,3 +1,21 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ *
+ * This program 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 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
 package jalview.ws;
 
 import java.util.*;
@@ -6,7 +24,6 @@ import jalview.analysis.*;
 import jalview.bin.*;
 import jalview.datamodel.*;
 import jalview.datamodel.Alignment;
-import jalview.datamodel.Sequence;
 import jalview.gui.*;
 import vamsas.objects.simple.MsaResult;
 
@@ -41,7 +58,8 @@ class MsaWSThread
 
   // order
 
-  class MsaWSJob extends WSThread.WSJob
+  class MsaWSJob
+      extends WSThread.WSJob
   {
     // hold special input for this
     vamsas.objects.simple.SequenceSet seqs = new vamsas.objects.simple.
@@ -109,20 +127,20 @@ class MsaWSThread
         {
           seqarray[n] = new vamsas.objects.simple.Sequence();
           seqarray[n].setId(newname);
-          seqarray[n++].setSeq( (submitGaps) ? seqs[i].getSequence()
+          seqarray[n++].setSeq( (submitGaps) ? seqs[i].getSequenceAsString()
                                : AlignSeq.extractGaps(
                                    jalview.util.Comparison.GapChars, seqs[i]
-                                   .getSequence()));
+                                   .getSequenceAsString()));
         }
         else
         {
           String empty = null;
           if (seqs[i].getEnd() >= seqs[i].getStart())
           {
-            empty = (submitGaps) ? seqs[i].getSequence()
+            empty = (submitGaps) ? seqs[i].getSequenceAsString()
                 : AlignSeq.extractGaps(
                     jalview.util.Comparison.GapChars, seqs[i]
-                    .getSequence());
+                    .getSequenceAsString());
           }
           emptySeqs.add(new String[]
                         {newname, empty});
@@ -140,7 +158,8 @@ class MsaWSThread
     public boolean hasResults()
     {
       if (subjobComplete && result != null && result.isFinished()
-          && ((MsaResult) result).getMsa() != null && ((MsaResult) result).getMsa().getSeqs() != null)
+          && ( (MsaResult) result).getMsa() != null &&
+          ( (MsaResult) result).getMsa().getSeqs() != null)
       {
         return true;
       }
@@ -155,10 +174,10 @@ class MsaWSThread
         SequenceI[] alseqs = null;
         char alseq_gapchar = '-';
         int alseq_l = 0;
-        if (((MsaResult) result).getMsa() != null)
+        if ( ( (MsaResult) result).getMsa() != null)
         {
-          alseqs = getVamsasAlignment(((MsaResult) result).getMsa());
-          alseq_gapchar = ((MsaResult) result).getMsa().getGapchar().charAt(0);
+          alseqs = getVamsasAlignment( ( (MsaResult) result).getMsa());
+          alseq_gapchar = ( (MsaResult) result).getMsa().getGapchar().charAt(0);
           alseq_l = alseqs.length;
         }
         if (emptySeqs.size() > 0)
@@ -206,7 +225,7 @@ class MsaWSThread
               if (nw > sw)
               {
                 // pad at end
-                alseqs[i].setSequence(t_alseqs[i].getSequence() +
+                alseqs[i].setSequence(t_alseqs[i].getSequenceAsString() +
                                       insbuff.substring(0, sw - nw));
               }
             }
@@ -249,26 +268,31 @@ class MsaWSThread
       }
       return null;
     }
+
     /**
      * mark subjob as cancelled and set result object appropriatly
      */
-    void cancel() {
-      cancelled=true;
+    void cancel()
+    {
+      cancelled = true;
       subjobComplete = true;
       result = null;
     }
+
     /**
      *
      * @return boolean true if job can be submitted.
      */
-    boolean hasValidInput() {
-      if (seqs.getSeqs()!=null)
+    boolean hasValidInput()
+    {
+      if (seqs.getSeqs() != null)
+      {
         return true;
+      }
       return false;
     }
   }
 
-
   String alTitle; // name which will be used to form new alignment window.
   Alignment dataset; // dataset to which the new alignment will be
 
@@ -288,14 +312,10 @@ class MsaWSThread
               AlignmentView alview,
               String wsname, boolean subgaps, boolean presorder)
   {
+    super(alFrame, wsinfo, alview, wsname, wsUrl);
     this.server = server;
-    this.WsUrl = wsUrl;
-    this.wsInfo = wsinfo;
-    this.WebServiceName = wsname;
-    this.input = alview;
     this.submitGaps = subgaps;
     this.preserveOrder = presorder;
-    this.alignFrame = alFrame;
   }
 
   /**
@@ -321,85 +341,28 @@ class MsaWSThread
     OutputHeader = wsInfo.getProgressText();
     alTitle = title;
     dataset = seqset;
-    SeqCigar[] msa = _msa.getSequences();
-    int[] contigs = _msa.getContigs();
-    int njobs = 1;
-    if (contigs != null && contigs.length > 0)
+
+    SequenceI[][] conmsa = _msa.getVisibleContigs('-');
+    if (conmsa != null)
     {
-      int start = 0;
-      njobs = 0;
-      int width = _msa.getWidth();
-      for (int contig = 0; contig < contigs.length; contig += 3)
-      {
-        if ( (contigs[contig + 1] - start) > 0)
-        {
-          njobs++;
-        }
-        width += contigs[contig + 2]; // end up with full region width (including hidden regions)
-        start = contigs[contig + 1] + contigs[contig + 2];
-      }
-      if (start < width)
-      {
-        njobs++;
-      }
+      int njobs = conmsa.length;
       jobs = new MsaWSJob[njobs];
-      start = 0;
-      int j = 0;
-      for (int contig = 0; contig < contigs.length; contig += 3)
-      {
-        if (contigs[contig + 1] - start > 0)
-        {
-          SequenceI mseq[] = new SequenceI[msa.length];
-          for (int s = 0; s < mseq.length; s++)
-          {
-            mseq[s] = msa[s].getSeq('-').getSubSequence(start,
-                contigs[contig + 1]);
-          }
-          if (j != 0)
-          {
-            jobs[j] = new MsaWSJob(wsinfo.addJobPane(), mseq);
-          }
-          else
-          {
-            jobs[j] = new MsaWSJob(0, mseq);
-          }
-          wsinfo.setProgressName("region " + jobs[j].jobnum, jobs[j].jobnum);
-          wsinfo.setProgressText(jobs[j].jobnum, OutputHeader);
-          j++;
-        }
-        start = contigs[contig + 1] + contigs[contig + 2];
-      }
-      if (start < width)
+      for (int j = 0; j < njobs; j++)
       {
-        SequenceI mseq[] = new SequenceI[msa.length];
-        for (int s = 0; s < mseq.length; s++)
-        {
-          mseq[s] = msa[s].getSeq('-').getSubSequence(start,
-              width + 1);
-        }
         if (j != 0)
         {
-          jobs[j] = new MsaWSJob(wsinfo.addJobPane(), mseq);
+          jobs[j] = new MsaWSJob(wsinfo.addJobPane(), conmsa[j]);
         }
         else
         {
-          jobs[j] = new MsaWSJob(0, mseq);
+          jobs[j] = new MsaWSJob(0, conmsa[j]);
+        }
+        if (njobs > 0)
+        {
+          wsinfo.setProgressName("region " + jobs[j].jobnum, jobs[j].jobnum);
         }
-        wsinfo.setProgressName("region " + jobs[j].jobnum, jobs[j].jobnum);
         wsinfo.setProgressText(jobs[j].jobnum, OutputHeader);
-        j++;
-      }
-    }
-    else
-    {
-      SequenceI mseq[] = new SequenceI[msa.length];
-      for (int s = 0; s < mseq.length; s++)
-      {
-        mseq[s] = msa[s].getSeq('-');
       }
-      jobs = new MsaWSJob[1];
-      wsinfo.setProgressText(OutputHeader); // ensures default text
-      jobs[0] = new MsaWSJob(0, mseq);
     }
   }
 
@@ -426,7 +389,7 @@ class MsaWSThread
             {
               // CANCELLED_JOB
               cancelledMessage = "Job cancelled.";
-              ((MsaWSJob) jobs[job]).cancel();
+              ( (MsaWSJob) jobs[job]).cancel();
               wsInfo.setStatus(jobs[job].jobnum,
                                WebserviceInfo.STATE_CANCELLED_OK);
             }
@@ -452,7 +415,8 @@ class MsaWSThread
             cancelledMessage +=
                 ("\nProblems cancelling the job : Exception received...\n"
                  + exc + "\n");
-            Cache.log.warn("Exception whilst cancelling "+jobs[job].jobId,exc);
+            Cache.log.warn("Exception whilst cancelling " + jobs[job].jobId,
+                           exc);
           }
           wsInfo.setProgressText(jobs[job].jobnum, OutputHeader
                                  + cancelledMessage + "\n");
@@ -475,13 +439,19 @@ class MsaWSThread
       }
     }
   }
-  void pollJob(WSJob job) throws Exception {
-    ((MsaWSJob) job).result = server.getResult(((MsaWSJob) job).jobId);
+
+  void pollJob(WSJob job)
+      throws Exception
+  {
+    ( (MsaWSJob) job).result = server.getResult( ( (MsaWSJob) job).jobId);
   }
+
   void StartJob(WSJob job)
   {
-    if (!(job instanceof MsaWSJob)) {
-      throw new Error("StartJob(MsaWSJob) called on a WSJobInstance "+job.getClass());
+    if (! (job instanceof MsaWSJob))
+    {
+      throw new Error("StartJob(MsaWSJob) called on a WSJobInstance " +
+                      job.getClass());
     }
     MsaWSJob j = (MsaWSJob) job;
     if (j.submitted)
@@ -499,7 +469,7 @@ class MsaWSThread
       j.result = new MsaResult();
       j.result.setFinished(true);
       j.result.setStatus("Empty Alignment Job");
-      ((MsaResult) j.result).setMsa(null);
+      ( (MsaResult) j.result).setMsa(null);
     }
     try
     {
@@ -577,7 +547,8 @@ class MsaWSThread
         if (jobs[j].submitted && jobs[j].subjobComplete && jobs[j].hasResults())
         {
           results++;
-          vamsas.objects.simple.Alignment valign = ((MsaResult) jobs[j].result).getMsa();
+          vamsas.objects.simple.Alignment valign = ( (MsaResult) jobs[j].result).
+              getMsa();
           if (valign != null)
           {
             wsInfo.appendProgressText(jobs[j].jobnum,
@@ -622,213 +593,65 @@ class MsaWSThread
         }
       });
       wsInfo.setResultsReady();
-    } else
+    }
+    else
+    {
       wsInfo.setFinishedNoResults();
+    }
   }
 
   void displayResults(boolean newFrame)
   {
     // view input or result data for each block
-    // warn user if a block is input rather than aligned data ?
-
-    int contigs[] = input.getContigs();
-    SeqCigar[] seqs = input.getSequences();
-    SequenceI[] alignment = new SequenceI[seqs.length];
-    ColumnSelection columnselection = new ColumnSelection();
     Vector alorders = new Vector();
-    if (contigs != null && contigs.length > 0)
-    {
-      int start = 0;
-      int nwidth = 0;
-      int owidth = input.getWidth();
-      int j = 0;
-      for (int contig = 0; contig < contigs.length; contig += 3)
-      {
-        owidth += contigs[contig + 2]; // recover final column width
-        if (contigs[contig + 1] - start > 0)
-        {
-          int width = 0; // subalignment width
-          if (jobs[j].hasResults())
-          {
-            Object[] subalg = ((MsaWSJob) jobs[j++]).getAlignment();
-            alorders.add(subalg[1]);
-            SequenceI mseq[] = (SequenceI[]) subalg[0];
-            width = mseq[0].getLength();
-            for (int s = 0; s < mseq.length; s++)
-            {
-              if (alignment[s] == null)
-              {
-                alignment[s] = mseq[s];
-              }
-              else
-              {
-                alignment[s].setSequence(alignment[s].getSequence() +
-                                         mseq[s].getSequence());
-                if (mseq[s].getStart() <= mseq[s].getEnd())
-                {
-                  alignment[s].setEnd(mseq[s].getEnd());
-                }
-                ( (AlignmentOrder) subalg[1]).updateSequence(mseq[s],
-                    alignment[s]);
-              }
-            }
-          }
-          else
-          {
-            // recover input data or place gaps
-            if (true)
-            {
-              // recover input data
-              for (int s = 0; s < seqs.length; s++)
-              {
-                SequenceI oseq = seqs[s].getSeq('-').getSubSequence(start,
-                    contigs[contig + 1]);
-                if (width < oseq.getLength())
-                {
-                  width = oseq.getLength();
-                }
-                if (alignment[s] == null)
-                {
-                  alignment[s] = oseq;
-                }
-                else
-                {
-                  alignment[s].setSequence(alignment[s].getSequence() +
-                                           oseq.getSequence());
-                  if (oseq.getEnd() >= oseq.getStart())
-                  {
-                    alignment[s].setEnd(oseq.getEnd());
-                  }
-                }
-              }
+    SequenceI[][] results = new SequenceI[jobs.length][];
+    AlignmentOrder[] orders = new AlignmentOrder[jobs.length];
 
-            }
-            j++;
-          }
-          nwidth += width;
-        }
-        // advance to begining of visible region
-        start = contigs[contig + 1] + contigs[contig + 2];
-        // add hidden segment to right of next region
-        for (int s = 0; s < seqs.length; s++)
-        {
-          SequenceI hseq = seqs[s].getSeq('-').getSubSequence(contigs[contig +
-              1], start);
-          if (alignment[s] == null)
-          {
-            alignment[s] = hseq;
-          }
-          else
-          {
-            alignment[s].setSequence(alignment[s].getSequence() +
-                                     hseq.getSequence());
-            if (hseq.getEnd() >= hseq.getStart())
-            {
-              alignment[s].setEnd(hseq.getEnd());
-            }
-          }
-        }
-        // mark hidden segment as hidden in the new alignment
-        columnselection.hideColumns(nwidth, nwidth + contigs[contig + 2] - 1);
-        nwidth += contigs[contig + 2];
-      }
-      // Do final job - if it exists
-      if (j < jobs.length)
-      {
-        int width = 0;
-        if (jobs[j].hasResults())
-        {
-          Object[] subalg = ((MsaWSJob) jobs[j]).getAlignment();
-          alorders.add(subalg[1]);
-          SequenceI mseq[] = (SequenceI[]) subalg[0];
-          width = mseq[0].getLength();
-          for (int s = 0; s < mseq.length; s++)
-          {
-            if (alignment[s] == null)
-            {
-              alignment[s] = mseq[s];
-            }
-            else
-            {
-              alignment[s].setSequence(alignment[s].getSequence() +
-                                       mseq[s].getSequence());
-              if (mseq[s].getEnd() >= mseq[s].getStart())
-              {
-                alignment[s].setEnd(mseq[s].getEnd());
-              }
-              ( (AlignmentOrder) subalg[1]).updateSequence(mseq[s], alignment[s]);
-            }
-          }
-        }
-        else
-        {
-          if (start < owidth)
-          {
-            // recover input data or place gaps
-            if (true)
-            {
-              // recover input data
-              for (int s = 0; s < seqs.length; s++)
-              {
-                SequenceI oseq = seqs[s].getSeq('-').getSubSequence(start,
-                    owidth + 1);
-                if (width < oseq.getLength())
-                {
-                  width = oseq.getLength();
-                }
-                if (alignment[s] == null)
-                {
-                  alignment[s] = oseq;
-                }
-                else
-                {
-                  alignment[s].setSequence(alignment[s].getSequence() +
-                                           oseq.getSequence());
-                  if (oseq.getEnd() >= oseq.getStart())
-                  {
-                    alignment[s].setEnd(oseq.getEnd());
-                  }
-                }
-              }
-              nwidth += width;
-            }
-            else
-            {
-              // place gaps.
-              throw new Error("Padding not yet implemented.");
-            }
-          }
-        }
-      }
-    }
-    else
+    for (int j = 0; j < jobs.length; j++)
     {
-      if (jobs[0].hasResults())
+      if (jobs[j].hasResults())
       {
-        Object[] alg = ((MsaWSJob) jobs[0]).getAlignment();
-        alignment = (SequenceI[]) alg[0];
-        alorders.add(alg[1]);
+        Object[] res = ( (MsaWSJob) jobs[j]).getAlignment();
+        alorders.add(res[1]);
+        results[j] = (SequenceI[]) res[0];
+        orders[j] = (AlignmentOrder) res[1];
+//    SequenceI[] alignment = input.getUpdated
       }
       else
       {
-        alignment = SeqCigar.createAlignmentSequences(seqs, '-',
-            columnselection, null);
+        results[j] = null;
       }
     }
+    Object[] newview = input.getUpdatedView(results, orders, '-');
+    // trash references to original result data
+    for (int j = 0; j < jobs.length; j++)
+    {
+      results[j] = null;
+      orders[j] = null;
+    }
+    SequenceI[] alignment = (SequenceI[]) newview[0];
+    ColumnSelection columnselection = (ColumnSelection) newview[1];
     Alignment al = new Alignment(alignment);
+
     if (dataset != null)
     {
       al.setDataset(dataset);
     }
 
+    propagateDatasetMappings(al);
+    // JBNote- TODO: warn user if a block is input rather than aligned data ?
+
     if (newFrame)
     {
-      AlignFrame af = new AlignFrame(al, columnselection);
+      AlignFrame af = new AlignFrame(al, columnselection,
+                                     AlignFrame.DEFAULT_WIDTH,
+                                     AlignFrame.DEFAULT_HEIGHT);
 
       // >>>This is a fix for the moment, until a better solution is
       // found!!<<<
       af.getFeatureRenderer().transferSettings(
           alignFrame.getFeatureRenderer());
+      // update orders
       if (alorders.size() > 0)
       {
         if (alorders.size() == 1)
@@ -844,7 +667,7 @@ class MsaWSThread
           {
             String orderName = new String(" Region " + i);
             int j = i + 1;
-            int r = l;
+
             while (j < l)
             {
               if ( ( (AlignmentOrder) alorders.get(i)).equals( ( (
@@ -872,7 +695,7 @@ class MsaWSThread
           for (int i = 0, l = alorders.size(); i < l; i++)
           {
             af.addSortByOrderMenuItem(WebServiceName
-                                      +( (String) names.get(i)) +
+                                      + ( (String) names.get(i)) +
                                       " Ordering",
                                       (AlignmentOrder) alorders.get(i));
           }
@@ -880,8 +703,8 @@ class MsaWSThread
       }
 
       Desktop.addInternalFrame(af, alTitle,
-                               AlignFrame.NEW_WINDOW_WIDTH,
-                               AlignFrame.NEW_WINDOW_HEIGHT);
+                               AlignFrame.DEFAULT_WIDTH,
+                               AlignFrame.DEFAULT_HEIGHT);
 
     }
     else
@@ -890,4 +713,10 @@ class MsaWSThread
 
     }
   }
+
+
+  public boolean canMergeResults()
+  {
+    return false;
+  }
 }