JAL-2403 JAL-838 remove unused methods and unneeded public visibility
[jalview.git] / src / jalview / analysis / NJTree.java
index 542f15e..4ee0be9 100644 (file)
 package jalview.analysis;
 
 import jalview.analysis.scoremodels.ScoreModels;
+import jalview.analysis.scoremodels.SimilarityParams;
+import jalview.api.analysis.DistanceScoreModelI;
 import jalview.api.analysis.ScoreModelI;
+import jalview.api.analysis.SimilarityScoreModelI;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.BinaryNode;
 import jalview.datamodel.CigarArray;
@@ -31,6 +34,7 @@ import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
 import jalview.datamodel.SequenceNode;
 import jalview.io.NewickFile;
+import jalview.math.MatrixI;
 
 import java.util.Enumeration;
 import java.util.List;
@@ -67,15 +71,15 @@ public class NJTree
 
   int noClus;
 
-  float[][] distance;
+  MatrixI distance;
 
   int mini;
 
   int minj;
 
-  float ri;
+  double ri;
 
-  float rj;
+  double rj;
 
   Vector<SequenceNode> groups = new Vector<SequenceNode>();
 
@@ -83,9 +87,9 @@ public class NJTree
 
   SequenceNode top;
 
-  float maxDistValue;
+  double maxDistValue;
 
-  float maxheight;
+  double maxheight;
 
   int ycount;
 
@@ -214,25 +218,29 @@ public class NJTree
    * 
    * @param sequence
    *          DOCUMENT ME!
-   * @param type
+   * @param treeType
    *          DOCUMENT ME!
-   * @param pwtype
+   * @param modelType
    *          DOCUMENT ME!
    * @param start
    *          DOCUMENT ME!
    * @param end
    *          DOCUMENT ME!
    */
-  public NJTree(SequenceI[] sequence, AlignmentView seqData, String type,
-          String pwtype, ScoreModelI sm, int start, int end)
+  public NJTree(SequenceI[] sqs, AlignmentView seqView, String treeType,
+          String modelType, ScoreModelI sm, int start, int end)
   {
-    this.sequence = sequence;
+    this.sequence = sqs;
     this.node = new Vector<SequenceNode>();
-    this.type = type;
-    this.pwtype = pwtype;
-    if (seqData != null)
+    if (!(treeType.equals(NEIGHBOUR_JOINING)))
     {
-      this.seqData = seqData;
+      treeType = AVERAGE_DISTANCE;
+    }
+    this.type = treeType;
+    this.pwtype = modelType;
+    if (seqView != null)
+    {
+      this.seqData = seqView;
     }
     else
     {
@@ -245,17 +253,12 @@ public class NJTree
       sdata.addOperation(CigarArray.M, end - start + 1);
       this.seqData = new AlignmentView(sdata, start);
     }
-    // System.err.println("Made seqData");// dbg
-    if (!(type.equals(NEIGHBOUR_JOINING)))
-    {
-      type = AVERAGE_DISTANCE;
-    }
 
-    if (sm == null && !(pwtype.equals("PID")))
+    if (sm == null && !(modelType.equals("PID")))
     {
-      if (ScoreModels.getInstance().forName(pwtype) == null)
+      if (ScoreModels.getInstance().forName(modelType) == null)
       {
-        pwtype = "BLOSUM62";
+        modelType = "BLOSUM62";
       }
     }
 
@@ -271,16 +274,28 @@ public class NJTree
 
     noseqs = i++;
 
-    distance = findDistances(sm);
-    // System.err.println("Made distances");// dbg
+    // TODO pass choice of params from GUI in constructo
+    if (sm instanceof DistanceScoreModelI)
+    {
+      distance = ((DistanceScoreModelI) sm).findDistances(seqData,
+              SimilarityParams.Jalview);
+    }
+    else if (sm instanceof SimilarityScoreModelI)
+    {
+      /*
+       * compute similarity and invert it to give a distance measure
+       */
+      MatrixI result = ((SimilarityScoreModelI) sm).findSimilarities(
+              seqData, SimilarityParams.Jalview);
+      result.reverseRange(true);
+      distance = result;
+    }
+
     makeLeaves();
-    // System.err.println("Made leaves");// dbg
 
     noClus = cluster.size();
 
     cluster();
-    // System.err.println("Made clusters");// dbg
-
   }
 
   /**
@@ -293,8 +308,8 @@ 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(hasBootstrap(), hasDistances(),
+            hasRootDistance()); // output all data available for tree
   }
 
   /**
@@ -304,7 +319,7 @@ public class NJTree
    * @param list
    *          Sequence set to be associated with tree nodes
    */
-  public void UpdatePlaceHolders(List<SequenceI> list)
+  public void updatePlaceHolders(List<SequenceI> list)
   {
     Vector<SequenceNode> leaves = findLeaves(top);
 
@@ -389,7 +404,7 @@ public class NJTree
   /**
    * DOCUMENT ME!
    */
-  public void cluster()
+  void cluster()
   {
     while (noClus > 2)
     {
@@ -451,9 +466,9 @@ public class NJTree
    * 
    * @return DOCUMENT ME!
    */
-  public Cluster joinClusters(int i, int j)
+  Cluster joinClusters(int i, int j)
   {
-    float dist = distance[i][j];
+    double dist = distance.getValue(i, j);
 
     int noi = cluster.elementAt(i).value.length;
     int noj = cluster.elementAt(j).value.length;
@@ -519,8 +534,8 @@ public class NJTree
    * @param dist
    *          DOCUMENT ME!
    */
-  public void findNewNJDistances(SequenceNode tmpi, SequenceNode tmpj,
-          float dist)
+  void findNewNJDistances(SequenceNode tmpi, SequenceNode tmpj,
+          double dist)
   {
 
     tmpi.dist = ((dist + ri) - rj) / 2;
@@ -547,11 +562,11 @@ public class NJTree
    * @param dist
    *          DOCUMENT ME!
    */
-  public void findNewDistances(SequenceNode tmpi, SequenceNode tmpj,
-          float dist)
+  void findNewDistances(SequenceNode tmpi, SequenceNode tmpj,
+          double dist)
   {
-    float ih = 0;
-    float jh = 0;
+    double ih = 0;
+    double jh = 0;
 
     SequenceNode sni = tmpi;
     SequenceNode snj = tmpj;
@@ -580,19 +595,22 @@ public class NJTree
    * @param j
    *          DOCUMENT ME!
    */
-  public void findClusterDistance(int i, int j)
+  void findClusterDistance(int i, int j)
   {
     int noi = cluster.elementAt(i).value.length;
     int noj = cluster.elementAt(j).value.length;
 
     // New distances from cluster to others
-    float[] newdist = new float[noseqs];
+    double[] newdist = new double[noseqs];
 
     for (int l = 0; l < noseqs; l++)
     {
       if ((l != i) && (l != j))
       {
-        newdist[l] = ((distance[i][l] * noi) + (distance[j][l] * noj))
+        // newdist[l] = ((distance[i][l] * noi) + (distance[j][l] * noj))
+        // / (noi + noj);
+        newdist[l] = ((distance.getValue(i, l) * noi) + (distance.getValue(
+                j, l) * noj))
                 / (noi + noj);
       }
       else
@@ -603,8 +621,10 @@ public class NJTree
 
     for (int ii = 0; ii < noseqs; ii++)
     {
-      distance[i][ii] = newdist[ii];
-      distance[ii][i] = newdist[ii];
+      // distance[i][ii] = newdist[ii];
+      // distance[ii][i] = newdist[ii];
+      distance.setValue(i, ii, newdist[ii]);
+      distance.setValue(ii, i, newdist[ii]);
     }
   }
 
@@ -616,17 +636,20 @@ public class NJTree
    * @param j
    *          DOCUMENT ME!
    */
-  public void findClusterNJDistance(int i, int j)
+  void findClusterNJDistance(int i, int j)
   {
 
     // New distances from cluster to others
-    float[] newdist = new float[noseqs];
+    double[] newdist = new double[noseqs];
 
     for (int l = 0; l < noseqs; l++)
     {
       if ((l != i) && (l != j))
       {
-        newdist[l] = ((distance[i][l] + distance[j][l]) - distance[i][j]) / 2;
+        // newdist[l] = ((distance[i][l] + distance[j][l]) - distance[i][j]) /
+        // 2;
+        newdist[l] = (distance.getValue(i, l) + distance.getValue(j, l) - distance
+                .getValue(i, j)) / 2;
       }
       else
       {
@@ -636,8 +659,10 @@ public class NJTree
 
     for (int ii = 0; ii < noseqs; ii++)
     {
-      distance[i][ii] = newdist[ii];
-      distance[ii][i] = newdist[ii];
+      // distance[i][ii] = newdist[ii];
+      // distance[ii][i] = newdist[ii];
+      distance.setValue(i, ii, newdist[ii]);
+      distance.setValue(ii, i, newdist[ii]);
     }
   }
 
@@ -651,15 +676,16 @@ public class NJTree
    * 
    * @return DOCUMENT ME!
    */
-  public float findr(int i, int j)
+  double findr(int i, int j)
   {
-    float tmp = 1;
+    double tmp = 1;
 
     for (int k = 0; k < noseqs; k++)
     {
       if ((k != i) && (k != j) && (done[k] != 1))
       {
-        tmp = tmp + distance[i][k];
+        // tmp = tmp + distance[i][k];
+        tmp = tmp + distance.getValue(i, k);
       }
     }
 
@@ -676,9 +702,9 @@ public class NJTree
    * 
    * @return DOCUMENT ME!
    */
-  public float findMinNJDistance()
+  double findMinNJDistance()
   {
-    float min = 100000;
+    double min = Double.MAX_VALUE;
 
     for (int i = 0; i < (noseqs - 1); i++)
     {
@@ -686,7 +712,9 @@ public class NJTree
       {
         if ((done[i] != 1) && (done[j] != 1))
         {
-          float tmp = distance[i][j] - (findr(i, j) + findr(j, i));
+          // float tmp = distance[i][j] - (findr(i, j) + findr(j, i));
+          double tmp = distance.getValue(i, j)
+                  - (findr(i, j) + findr(j, i));
 
           if (tmp < min)
           {
@@ -707,9 +735,9 @@ public class NJTree
    * 
    * @return DOCUMENT ME!
    */
-  public float findMinDistance()
+  double findMinDistance()
   {
-    float min = 100000;
+    double min = Double.MAX_VALUE;
 
     for (int i = 0; i < (noseqs - 1); i++)
     {
@@ -717,12 +745,14 @@ public class NJTree
       {
         if ((done[i] != 1) && (done[j] != 1))
         {
-          if (distance[i][j] < min)
+          // if (distance[i][j] < min)
+          if (distance.getValue(i, j) < min)
           {
             mini = i;
             minj = j;
 
-            min = distance[i][j];
+            // min = distance[i][j];
+            min = distance.getValue(i, j);
           }
         }
       }
@@ -732,32 +762,9 @@ public class NJTree
   }
 
   /**
-   * Calculate a distance matrix given the sequence input data and score model
-   * 
-   * @return similarity matrix used to compute tree
-   */
-  public float[][] findDistances(ScoreModelI _pwmatrix)
-  {
-
-    float[][] dist = new float[noseqs][noseqs];
-    if (_pwmatrix == null)
-    {
-      // Resolve substitution model
-      _pwmatrix = ScoreModels.getInstance().forName(pwtype);
-      if (_pwmatrix == null)
-      {
-        _pwmatrix = ScoreModels.getInstance().forName("BLOSUM62");
-      }
-    }
-    dist = _pwmatrix.findDistances(seqData);
-    return dist;
-
-  }
-
-  /**
    * DOCUMENT ME!
    */
-  public void makeLeaves()
+  void makeLeaves()
   {
     cluster = new Vector<Cluster>();
 
@@ -831,54 +838,12 @@ public class NJTree
   }
 
   /**
-   * Find the leaf node with a particular ycount
-   * 
-   * @param nd
-   *          initial point on tree to search from
-   * @param count
-   *          value to search for
-   * 
-   * @return null or the node with ycound=count
-   */
-  public Object findLeaf(SequenceNode nd, int count)
-  {
-    found = _findLeaf(nd, count);
-
-    return found;
-  }
-
-  /*
-   * #see findLeaf(SequenceNode node, count)
-   */
-  public Object _findLeaf(SequenceNode nd, int count)
-  {
-    if (nd == null)
-    {
-      return null;
-    }
-
-    if (nd.ycount == count)
-    {
-      found = nd.element();
-
-      return found;
-    }
-    else
-    {
-      _findLeaf((SequenceNode) nd.left(), count);
-      _findLeaf((SequenceNode) nd.right(), count);
-    }
-
-    return found;
-  }
-
-  /**
    * printNode is mainly for debugging purposes.
    * 
    * @param nd
    *          SequenceNode
    */
-  public void printNode(SequenceNode nd)
+  void printNode(SequenceNode nd)
   {
     if (nd == null)
     {
@@ -905,7 +870,7 @@ public class NJTree
    * @param nd
    *          DOCUMENT ME!
    */
-  public void findMaxDist(SequenceNode nd)
+  void findMaxDist(SequenceNode nd)
   {
     if (nd == null)
     {
@@ -914,7 +879,7 @@ public class NJTree
 
     if ((nd.left() == null) && (nd.right() == null))
     {
-      float dist = nd.dist;
+      double dist = nd.dist;
 
       if (dist > maxDistValue)
       {
@@ -944,7 +909,7 @@ public class NJTree
    * 
    * @return DOCUMENT ME!
    */
-  public float getMaxHeight()
+  public double getMaxHeight()
   {
     return maxheight;
   }
@@ -983,7 +948,7 @@ public class NJTree
    * 
    * @return DOCUMENT ME!
    */
-  public float findHeight(SequenceNode nd)
+  public double findHeight(SequenceNode nd)
   {
     if (nd == null)
     {
@@ -1027,13 +992,14 @@ public class NJTree
    * 
    * @return DOCUMENT ME!
    */
-  public SequenceNode reRoot()
+  SequenceNode reRoot()
   {
+    // TODO not used - remove?
     if (maxdist != null)
     {
       ycount = 0;
 
-      float tmpdist = maxdist.dist;
+      double tmpdist = maxdist.dist;
 
       // New top
       SequenceNode sn = new SequenceNode();
@@ -1102,7 +1068,7 @@ public class NJTree
    * @param nd
    *          DOCUMENT ME!
    */
-  public void printN(SequenceNode nd)
+  void printN(SequenceNode nd)
   {
     if (nd == null)
     {
@@ -1145,7 +1111,7 @@ public class NJTree
    * @param nd
    *          DOCUMENT ME!
    */
-  public void _reCount(SequenceNode nd)
+  void _reCount(SequenceNode nd)
   {
     // if (_lycount<_lylimit)
     // {
@@ -1204,7 +1170,7 @@ public class NJTree
    * @param dir
    *          DOCUMENT ME!
    */
-  public void changeDirection(SequenceNode nd, SequenceNode dir)
+  void changeDirection(SequenceNode nd, SequenceNode dir)
   {
     if (nd == null)
     {
@@ -1283,7 +1249,7 @@ public class NJTree
    * 
    * @return true if tree has real distances
    */
-  public boolean isHasDistances()
+  public boolean hasDistances()
   {
     return hasDistances;
   }
@@ -1292,12 +1258,12 @@ public class NJTree
    * 
    * @return true if tree has real bootstrap values
    */
-  public boolean isHasBootstrap()
+  public boolean hasBootstrap()
   {
     return hasBootstrap;
   }
 
-  public boolean isHasRootDistance()
+  public boolean hasRootDistance()
   {
     return hasRootDistance;
   }
@@ -1324,6 +1290,7 @@ public class NJTree
  * @author $author$
  * @version $Revision$
  */
+// TODO what does this class have that int[] doesn't have already?
 class Cluster
 {
   int[] value;