JAL-4366 provide options for updating (or not) the linked alignment or existing align...
[jalview.git] / src / jalview / gui / PairwiseAlignPanel.java
index 4aea4b1..2c65bcc 100755 (executable)
@@ -21,6 +21,9 @@
 package jalview.gui;
 
 import jalview.analysis.AlignSeq;
+import jalview.analysis.scoremodels.ScoreMatrix;
+import jalview.api.analysis.ScoreModelI;
+import jalview.api.analysis.SimilarityParamsI;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.SequenceGroup;
@@ -51,10 +54,22 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
    * Creates a new PairwiseAlignPanel object.
    * 
    * @param viewport
-   *          DOCUMENT ME!
+   *          contains selected sequences to align
    */
   public PairwiseAlignPanel(AlignmentViewport viewport)
   {
+    this(viewport,null);
+  }
+  /**
+   * Creates a new PairwiseAlignPanel object.
+   * 
+   * @param viewport
+   *          contains selected sequences to align
+   */
+
+  public PairwiseAlignPanel(AlignmentViewport viewport, ScoreMatrix params)
+  {
     super();
     this.av = viewport;
 
@@ -65,14 +80,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,7 +98,7 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
             : AlignSeq.PEP;
 
     float[][] scores = new float[seqs.length][seqs.length];
-    double totscore = 0;
+    double totscore = 0D;
     int count = seqs.length;
     boolean first = true;
 
@@ -98,19 +113,21 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
         {
           continue;
         }
-
+        if (params!=null)
+        {
+          as.setScoreMatrix(params);
+        }
         as.calcScoreMatrix();
         as.traceAlignment();
 
         if (!first)
         {
-          System.out.println(DASHES);
+          jalview.bin.Console.outPrintln(DASHES);
           textarea.append(DASHES);
         }
         first = false;
         as.printAlignment(System.out);
-        scores[i][j] = as.getMaxScore()
-                / as.getASeq1().length;
+        scores[i][j] = as.getMaxScore() / as.getASeq1().length;
         totscore = totscore + scores[i][j];
 
         textarea.append(as.getOutput());
@@ -121,26 +138,53 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
 
     if (count > 2)
     {
-      System.out.println(
-              "Pairwise alignment scaled similarity score matrix\n");
+      printScoreMatrix(seqs, scores, totscore);
+    }
+  }
 
-      for (int i = 0; i < count; i++)
-      {
-        System.out.println(String.format("%d %s", i,
-                seqs[i].getDisplayId(true)));
-      }
+  /**
+   * 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++)
+    {
+      jalview.bin.Console.outPrintln(
+              String.format("%3d %s", i + 1, 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));
+    }
+    jalview.bin.Console.outPrintln();
 
-      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");
+      jalview.bin.Console.outPrintln();
     }
+
+    jalview.bin.Console.outPrintln("\n");
   }
 
   /**