Merge branch 'test/progressBar' into feature/JAL-4159_pasimap
[jalview.git] / src / jalview / gui / PairwiseAlignPanel.java
index 1cca215..087d7f6 100755 (executable)
@@ -28,9 +28,12 @@ import jalview.datamodel.SequenceI;
 import jalview.jbgui.GPairwiseAlignPanel;
 import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
+import jalview.math.MiscMath;
 
+import java.beans.PropertyChangeListener;
 import java.awt.event.ActionEvent;
 import java.util.Vector;
+import javax.swing.event.SwingPropertyChangeSupport;
 
 /**
  * DOCUMENT ME!
@@ -47,43 +50,87 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
 
   private float[][] alignmentScores;   // scores used by PaSiMap
 
+  private int GAP_OPEN_COST;
+
+  private int GAP_EXTEND_COST;
+
   AlignmentViewport av;
 
   Vector<SequenceI> sequences;
 
+  private String alignmentOutput;
+
+  private boolean suppressTextbox;
+  private boolean discardAlignments;
+
+  private boolean endGaps;
+
+  // for listening
+  public static final String TOTAL = "total";
+
+  public static final String PROGRESS = "progress";
+
+  private int total;
+
+  private int progress;
+
   /**
    * Creates a new PairwiseAlignPanel object.
    * 
    * @param viewport
    *          DOCUMENT ME!
+   * @param endGaps ~ toggle gaps and the beginning and end of sequences
    */
   public PairwiseAlignPanel(AlignmentViewport viewport)
   {
+    this(viewport, false, 120, 20, true);      // default penalties used in AlignSeq
+  }
+  public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost)
+  {
+    this(viewport, endGaps, gapOpenCost, gapExtendCost, true);
+  }
+  public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost, boolean run)
+  {
     super();
     this.av = viewport;
+    this.GAP_OPEN_COST = gapOpenCost;
+    this.GAP_EXTEND_COST = gapExtendCost;
+    this.endGaps = endGaps;
+    this.total = MiscMath.combinations(av.getAlignment().getHeight(), 2);
+    
+    if (run)
+      calculate();
+System.out.println("Creating pap");
+  }
+
+  public void calculate()
+  {
+
+    SequenceGroup selectionGroup = av.getSelectionGroup();
+    StringBuilder sb = new StringBuilder(1024);
 
     sequences = new Vector<SequenceI>();
 
-    SequenceGroup selectionGroup = viewport.getSelectionGroup();
     boolean isSelection = selectionGroup != null
             && selectionGroup.getSize() > 0;
-    AlignmentView view = viewport.getAlignmentView(isSelection);
-    // String[] seqStrings = viewport.getViewAsString(true);
+    AlignmentView view = av.getAlignmentView(isSelection);
+    // String[] seqStrings = av.getViewAsString(true);
     String[] seqStrings = view
-            .getSequenceStrings(viewport.getGapCharacter());
+            .getSequenceStrings(av.getGapCharacter());
 
     SequenceI[] seqs;
     if (isSelection)
     {
       seqs = (SequenceI[]) view
-              .getAlignmentAndHiddenColumns(viewport.getGapCharacter())[0];
+              .getAlignmentAndHiddenColumns(av.getGapCharacter())[0];
     }
     else
     {
       seqs = av.getAlignment().getSequencesArray();
     }
 
-    String type = (viewport.getAlignment().isNucleotide()) ? AlignSeq.DNA
+    String type = (av.getAlignment().isNucleotide()) ? AlignSeq.DNA
             : AlignSeq.PEP;
 
     float[][] scores = new float[seqs.length][seqs.length];
@@ -92,14 +139,20 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
     int count = seqs.length;
     boolean first = true;
 
+    progress = 0;
+    firePropertyChange(TOTAL, 0, total);
+
+    suppressTextbox = count<10;
+    discardAlignments = count<15;
+
     for (int i = 1; i < count; i++)
     {
       // fill diagonal alignmentScores with Float.NaN
-      alignmentScores[i-1][i-1] = Float.NaN;
+      alignmentScores[i - 1][i - 1] = Float.NaN;
       for (int j = 0; j < i; j++)
       {
         AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
-                seqStrings[j], type);
+                seqStrings[j], type, GAP_OPEN_COST, GAP_EXTEND_COST);
 
         if (as.s1str.length() == 0 || as.s2str.length() == 0)
         {
@@ -107,27 +160,43 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
         }
 
         as.calcScoreMatrix();
-        as.traceAlignment();
+       if (endGaps)
+       {
+          as.traceAlignmentWithEndGaps();
+       } else {
+         as.traceAlignment();
+       }
        as.scoreAlignment();
 
         if (!first)
         {
           System.out.println(DASHES);
           textarea.append(DASHES);
+         sb.append(DASHES);
         }
         first = false;
-        as.printAlignment(System.out);
+        if (discardAlignments) {
+          as.printAlignment(System.out);
+       }
         scores[i][j] = as.getMaxScore() / as.getASeq1().length;
         alignmentScores[i][j] = as.getAlignmentScore();
-       //&!
         totscore = totscore + scores[i][j];
 
-        textarea.append(as.getOutput());
-        sequences.add(as.getAlignedSeq1());
-        sequences.add(as.getAlignedSeq2());
+       if (suppressTextbox)
+       {
+          textarea.append(as.getOutput());
+         sb.append(as.getOutput());
+       }
+       if (discardAlignments)
+       {
+          sequences.add(as.getAlignedSeq1());
+          sequences.add(as.getAlignedSeq2());
+       }
+
+       firePropertyChange(PROGRESS, progress, ++progress);
       }
     }
-    alignmentScores[count-1][count-1] = Float.NaN;
+    alignmentScores[count - 1][count - 1] = Float.NaN;
 
     this.scores = scores;
     this.alignmentScores = alignmentScores;
@@ -136,6 +205,8 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
     {
       printScoreMatrix(seqs, scores, totscore);
     }
+
+    alignmentOutput = sb.toString();
   }
 
   public float[][] getScores()
@@ -148,6 +219,11 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
     return this.alignmentScores;
   }
 
+  public String getAlignmentOutput()
+  {
+    return this.alignmentOutput;
+  }
+
   /**
    * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
    * 
@@ -216,4 +292,14 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
             MessageManager.getString("label.pairwise_aligned_sequences"),
             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
   }
+
+  public long getTotal()
+  {
+    return total;
+  }
+
+  public long getProgress()
+  {
+    return progress;
+  }
 }