Alogirthm working, but still giving slightly off results
[jalview.git] / src / jalview / gui / PairwiseAlignPanel.java
index 4aea4b1..2379e27 100755 (executable)
@@ -43,21 +43,38 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
 
   private static final String DASHES = "---------------------\n";
 
+  private float[][] scores;
+
+  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;
+
   /**
    * 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);    // default penalties used in AlignSeq
+  }
+  public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost)
+  {
     super();
     this.av = viewport;
 
+    StringBuilder sb = new StringBuilder(1024);
+
     sequences = new Vector<SequenceI>();
 
     SequenceGroup selectionGroup = viewport.getSelectionGroup();
@@ -65,14 +82,14 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
             && selectionGroup.getSize() > 0;
     AlignmentView view = viewport.getAlignmentView(isSelection);
     // String[] seqStrings = viewport.getViewAsString(true);
-    String[] seqStrings = view.getSequenceStrings(viewport
-            .getGapCharacter());
+    String[] seqStrings = view
+            .getSequenceStrings(viewport.getGapCharacter());
 
     SequenceI[] seqs;
     if (isSelection)
     {
-      seqs = (SequenceI[]) view.getAlignmentAndHiddenColumns(viewport
-              .getGapCharacter())[0];
+      seqs = (SequenceI[]) view
+              .getAlignmentAndHiddenColumns(viewport.getGapCharacter())[0];
     }
     else
     {
@@ -83,16 +100,21 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
             : AlignSeq.PEP;
 
     float[][] scores = new float[seqs.length][seqs.length];
-    double totscore = 0;
+    float[][] alignmentScores = new float[seqs.length][seqs.length];
+    double totscore = 0D;
     int count = seqs.length;
     boolean first = true;
+    //AlignSeq as = new AlignSeq(seqs[1], seqStrings[1], seqs[0], seqStrings[0], type, gapOpenCost, gapExtendCost);
 
     for (int i = 1; i < count; i++)
     {
+      // fill diagonal alignmentScores with 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, gapOpenCost, gapExtendCost);
+//     as.seqInit(seqs[i], seqStrings[i], seqs[j], seqStrings[j], type);
 
         if (as.s1str.length() == 0 || as.s2str.length() == 0)
         {
@@ -100,47 +122,104 @@ 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);
-        scores[i][j] = as.getMaxScore()
-                / as.getASeq1().length;
+        scores[i][j] = as.getMaxScore() / as.getASeq1().length;
+System.out.println(String.format("%s x %s -> [%d][%d] (%f)", seqs[i].getName(), seqs[j].getName(), i, j, as.getAlignmentScore()));
+        alignmentScores[i][j] = as.getAlignmentScore();
         totscore = totscore + scores[i][j];
 
         textarea.append(as.getOutput());
+       sb.append(as.getOutput());
         sequences.add(as.getAlignedSeq1());
         sequences.add(as.getAlignedSeq2());
       }
     }
+    alignmentScores[count-1][count-1] = Float.NaN;
+
+    this.scores = scores;
+    this.alignmentScores = alignmentScores;
 
     if (count > 2)
     {
+      printScoreMatrix(seqs, scores, totscore);
+    }
+
+    alignmentOutput = sb.toString();
+  }
+
+  public float[][] getScores()
+  {
+    return this.scores;
+  }
+
+  public float[][] getAlignmentScores()
+  {
+    return this.alignmentScores;
+  }
+
+  public String getAlignmentOutput()
+  {
+    return this.alignmentOutput;
+  }
+
+  /**
+   * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
+   * 
+   * @param seqs
+   * @param scores
+   * @param totscore
+   */
+  protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
+          double totscore)
+  {
+    System.out
+            .println("Pairwise alignment scaled similarity score matrix\n");
+
+    for (int i = 0; i < seqs.length; i++)
+    {
       System.out.println(
-              "Pairwise alignment scaled similarity score matrix\n");
+              String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
+    }
 
-      for (int i = 0; i < count; i++)
-      {
-        System.out.println(String.format("%d %s", i,
-                seqs[i].getDisplayId(true)));
-      }
+    /*
+     * table heading columns for sequences 1, 2, 3...
+     */
+    System.out.print("\n ");
+    for (int i = 0; i < seqs.length; i++)
+    {
+      System.out.print(String.format("%7d", i + 1));
+    }
+    System.out.println();
 
-      for (int i = 0; i < count; i++)
+    for (int i = 0; i < seqs.length; i++)
+    {
+      System.out.print(String.format("%3d", i + 1));
+      for (int j = 0; j < i; j++)
       {
-        for (int j = 0; j < i; j++)
-        {
-          System.out.print(String.format("%7.3f", scores[i][j] / totscore));
-        }
-        System.out.println();
+        /*
+         * as a fraction of tot score, outputs are 0 <= score <= 1
+         */
+        System.out.print(String.format("%7.3f", scores[i][j] / totscore));
       }
-
-      System.out.println("\n");
+      System.out.println();
     }
+
+    System.out.println("\n");
   }
 
   /**