JAL-1620 version bump and release notes
[jalview.git] / src / jalview / analysis / NJTree.java
index e9e1cd7..7852e4d 100644 (file)
@@ -1,24 +1,28 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
- * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
+ * Copyright (C) 2014 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  * Jalview 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 3 of the License, or (at your option) any later version.
- * 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
  * Jalview 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 Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.analysis;
 
 import java.util.*;
 
+import jalview.api.analysis.ScoreModelI;
 import jalview.datamodel.*;
 import jalview.io.*;
 import jalview.schemes.*;
@@ -253,8 +257,7 @@ public class NJTree
 
     noseqs = i++;
 
-    distance = findDistances(this.seqData
-            .getSequenceStrings(Comparison.GapChars.charAt(0)));
+    distance = findDistances();
     // System.err.println("Made distances");// dbg
     makeLeaves();
     // System.err.println("Made leaves");// dbg
@@ -275,18 +278,18 @@ public class NJTree
   {
     jalview.io.NewickFile fout = new jalview.io.NewickFile(getTopNode());
 
-    return fout.print(isHasBootstrap(),
-    isHasDistances(), isHasRootDistance()); // output all data available for tree
+    return fout.print(isHasBootstrap(), isHasDistances(),
+            isHasRootDistance()); // output all data available for tree
   }
 
   /**
    * 
    * used when the alignment associated to a tree has changed.
    * 
-   * @param alignment
-   *          Vector
+   * @param list
+   *          Sequence set to be associated with tree nodes
    */
-  public void UpdatePlaceHolders(Vector alignment)
+  public void UpdatePlaceHolders(List<SequenceI> list)
   {
     Vector leaves = new Vector();
     findLeaves(top, leaves);
@@ -299,7 +302,7 @@ public class NJTree
     {
       SequenceNode leaf = (SequenceNode) leaves.elementAt(i++);
 
-      if (alignment.contains(leaf.element()))
+      if (list.contains(leaf.element()))
       {
         leaf.setPlaceholder(false);
       }
@@ -308,11 +311,11 @@ public class NJTree
         if (seqmatcher == null)
         {
           // Only create this the first time we need it
-          SequenceI[] seqs = new SequenceI[alignment.size()];
+          SequenceI[] seqs = new SequenceI[list.size()];
 
           for (int j = 0; j < seqs.length; j++)
           {
-            seqs[j] = (SequenceI) alignment.elementAt(j);
+            seqs[j] = (SequenceI) list.get(j);
           }
 
           seqmatcher = new SequenceIdMatcher(seqs);
@@ -346,24 +349,29 @@ public class NJTree
       }
     }
   }
+
   /**
-   * rename any nodes according to their associated sequence.
-   * This will modify the tree's metadata! (ie the original NewickFile or newly generated BinaryTree's label data) 
+   * rename any nodes according to their associated sequence. This will modify
+   * the tree's metadata! (ie the original NewickFile or newly generated
+   * BinaryTree's label data)
    */
-  public void renameAssociatedNodes() {
-    applyToNodes(new NodeTransformI() {
+  public void renameAssociatedNodes()
+  {
+    applyToNodes(new NodeTransformI()
+    {
 
       @Override
       public void transform(BinaryNode node)
       {
         Object el = node.element();
-        if (el!=null && el instanceof SequenceI)
+        if (el != null && el instanceof SequenceI)
         {
-          node.setName(((SequenceI)el).getName());
+          node.setName(((SequenceI) el).getName());
         }
       }
     });
   }
+
   /**
    * DOCUMENT ME!
    */
@@ -710,100 +718,24 @@ public class NJTree
   }
 
   /**
-   * DOCUMENT ME!
+   * Calculate a distance matrix given the sequence input data and score model
    * 
-   * @return DOCUMENT ME!
+   * @return similarity matrix used to compute tree
    */
-  public float[][] findDistances(String[] sequenceString)
+  public float[][] findDistances()
   {
-    float[][] distance = new float[noseqs][noseqs];
 
-    if (pwtype.equals("PID"))
-    {
-      for (int i = 0; i < (noseqs - 1); i++)
-      {
-        for (int j = i; j < noseqs; j++)
-        {
-          if (j == i)
-          {
-            distance[i][i] = 0;
-          }
-          else
-          {
-            distance[i][j] = 100 - Comparison.PID(sequenceString[i],
-                    sequenceString[j]);
+    float[][] distance = new float[noseqs][noseqs];
 
-            distance[j][i] = distance[i][j];
-          }
-        }
-      }
-    }
-    else
+    // Pairwise substitution score (with no gap penalties)
+    ScoreModelI _pwmatrix = ResidueProperties.getScoreModel(pwtype);
+    if (_pwmatrix == null)
     {
-      // Pairwise substitution score (with no gap penalties)
-      ScoreMatrix pwmatrix = ResidueProperties.getScoreMatrix(pwtype);
-      if (pwmatrix == null)
-      {
-        pwmatrix = ResidueProperties.getScoreMatrix("BLOSUM62");
-      }
-      int maxscore = 0;
-      int end = sequenceString[0].length();
-      for (int i = 0; i < (noseqs - 1); i++)
-      {
-        for (int j = i; j < noseqs; j++)
-        {
-          int score = 0;
-
-          for (int k = 0; k < end; k++)
-          {
-            try
-            {
-              score += pwmatrix.getPairwiseScore(
-                      sequenceString[i].charAt(k),
-                      sequenceString[j].charAt(k));
-            } catch (Exception ex)
-            {
-              System.err.println("err creating BLOSUM62 tree");
-              ex.printStackTrace();
-            }
-          }
-
-          distance[i][j] = (float) score;
-
-          if (score > maxscore)
-          {
-            maxscore = score;
-          }
-        }
-      }
-
-      for (int i = 0; i < (noseqs - 1); i++)
-      {
-        for (int j = i; j < noseqs; j++)
-        {
-          distance[i][j] = (float) maxscore - distance[i][j];
-          distance[j][i] = distance[i][j];
-        }
-      }
-
+      _pwmatrix = ResidueProperties.getScoreMatrix("BLOSUM62");
     }
+    distance = _pwmatrix.findDistances(seqData);
     return distance;
 
-    // else
-    /*
-     * else if (pwtype.equals("SW")) { float max = -1;
-     * 
-     * for (int i = 0; i < (noseqs - 1); i++) { for (int j = i; j < noseqs; j++)
-     * { AlignSeq as = new AlignSeq(sequence[i], sequence[j], "pep");
-     * as.calcScoreMatrix(); as.traceAlignment(); as.printAlignment(System.out);
-     * distance[i][j] = (float) as.maxscore;
-     * 
-     * if (max < distance[i][j]) { max = distance[i][j]; } } }
-     * 
-     * for (int i = 0; i < (noseqs - 1); i++) { for (int j = i; j < noseqs; j++)
-     * { distance[i][j] = max - distance[i][j]; distance[j][i] = distance[i][j];
-     * } } }/
-     */
   }
 
   /**