Merge branch 'develop' into features/JAL-2393customMatrices
[jalview.git] / src / jalview / analysis / NJTree.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.analysis;
22
23 import jalview.api.analysis.ScoreModelI;
24 import jalview.api.analysis.SimilarityParamsI;
25 import jalview.datamodel.SequenceNode;
26 import jalview.viewmodel.AlignmentViewport;
27
28 /**
29  * DOCUMENT ME!
30  * 
31  * @author $author$
32  * @version $Revision$
33  */
34 public class NJTree extends TreeBuilder
35 {
36   /**
37    * Constructor given a viewport, tree type and score model
38    * 
39    * @param av
40    *          the current alignment viewport
41    * @param sm
42    *          a distance or similarity score model to use to compute the tree
43    * @param scoreParameters
44    */
45   public NJTree(AlignmentViewport av, ScoreModelI sm,
46           SimilarityParamsI scoreParameters)
47   {
48     super(av, sm, scoreParameters);
49   }
50
51   /**
52    * DOCUMENT ME!
53    * 
54    * @return DOCUMENT ME!
55    */
56   @Override
57   protected
58   double findMinDistance()
59   {
60     double min = Double.MAX_VALUE;
61
62     for (int i = 0; i < (noseqs - 1); i++)
63     {
64       for (int j = i + 1; j < noseqs; j++)
65       {
66         if (!done.get(i) && !done.get(j))
67         {
68           double tmp = distances.getValue(i, j)
69                   - (findr(i, j) + findr(j, i));
70
71           if (tmp < min)
72           {
73             mini = i;
74             minj = j;
75
76             min = tmp;
77           }
78         }
79       }
80     }
81
82     return min;
83   }
84
85   /**
86    * DOCUMENT ME!
87    * 
88    * @param tmpi
89    *          DOCUMENT ME!
90    * @param tmpj
91    *          DOCUMENT ME!
92    * @param dist
93    *          DOCUMENT ME!
94    */
95   @Override
96   protected
97   void findNewDistances(SequenceNode tmpi, SequenceNode tmpj, double dist)
98   {
99
100     tmpi.dist = ((dist + ri) - rj) / 2;
101     tmpj.dist = (dist - tmpi.dist);
102
103     if (tmpi.dist < 0)
104     {
105       tmpi.dist = 0;
106     }
107
108     if (tmpj.dist < 0)
109     {
110       tmpj.dist = 0;
111     }
112   }
113
114
115
116   /**
117    * Calculates and saves the distance between the combination of cluster(i) and
118    * cluster(j) and all other clusters. The new distance to cluster k is
119    * calculated as the average of the distances from i to k and from j to k,
120    * less half the distance from i to j.
121    * 
122    * @param i
123    * @param j
124    */
125   @Override
126   protected
127   void findClusterDistance(int i, int j)
128   {
129     // New distances from cluster i to others
130     double[] newdist = new double[noseqs];
131   
132     double ijDistance = distances.getValue(i, j);
133     for (int l = 0; l < noseqs; l++)
134     {
135       if ((l != i) && (l != j))
136       {
137         newdist[l] = (distances.getValue(i, l) + distances.getValue(j, l) - ijDistance) / 2;
138       }
139       else
140       {
141         newdist[l] = 0;
142       }
143     }
144   
145     for (int ii = 0; ii < noseqs; ii++)
146     {
147       distances.setValue(i, ii, newdist[ii]);
148       distances.setValue(ii, i, newdist[ii]);
149     }
150   }
151 }