X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FNJTree.java;h=6c239be1de8a3c2311e99ca0c59a2c0dc95a8ee2;hb=83193bc5c3e150dc7331b5b27a67cb5f9efb477c;hp=75d36c18018ec156aabbcc2d01c61724c60bc3a7;hpb=99c58ee0ae2a848f982552e53feaf6d5cb9925e5;p=jalview.git diff --git a/src/jalview/analysis/NJTree.java b/src/jalview/analysis/NJTree.java index 75d36c1..6c239be 100755 --- a/src/jalview/analysis/NJTree.java +++ b/src/jalview/analysis/NJTree.java @@ -16,750 +16,1161 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ - package jalview.analysis; import jalview.datamodel.*; -import jalview.util.*; -import jalview.schemes.ResidueProperties; -import java.util.*; import jalview.io.NewickFile; -public class NJTree { - - Vector cluster; - SequenceI[] sequence; - - int done[]; - int noseqs; - int noClus; - - float distance[][]; - - int mini; - int minj; - float ri; - float rj; - - Vector groups = new Vector(); - SequenceNode maxdist; - SequenceNode top; - - float maxDistValue; - float maxheight; - - int ycount; - - Vector node; - - String type; - String pwtype; - - Object found = null; - Object leaves = null; - - int start; - int end; - - public NJTree(SequenceNode node) { - top = node; - maxheight = findHeight(top); - } +import jalview.schemes.ResidueProperties; - public String toString() - { - jalview.io.NewickFile fout = new jalview.io.NewickFile(getTopNode()); - return fout.print(false,true); // distances only - } +import jalview.util.*; - public NJTree(SequenceI[] seqs, NewickFile treefile) { - top = treefile.getTree(); - maxheight = findHeight(top); - SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs); +import java.util.*; - Vector leaves = new Vector(); - findLeaves(top, leaves); - int i = 0; - int namesleft = seqs.length; +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class NJTree +{ + Vector cluster; + SequenceI[] sequence; + int[] done; + int noseqs; + int noClus; + float[][] distance; + int mini; + int minj; + float ri; + float rj; + Vector groups = new Vector(); + SequenceNode maxdist; + SequenceNode top; + float maxDistValue; + float maxheight; + int ycount; + Vector node; + String type; + String pwtype; + Object found = null; + Object leaves = null; + int start; + int end; + + /** + * Creates a new NJTree object. + * + * @param node DOCUMENT ME! + */ + public NJTree(SequenceNode node) + { + top = node; + maxheight = findHeight(top); + } - SequenceNode j; - SequenceI nam; - String realnam; - while (i < leaves.size()) + /** + * Creates a new NJTree object. + * + * @param seqs DOCUMENT ME! + * @param treefile DOCUMENT ME! + */ + public NJTree(SequenceI[] seqs, NewickFile treefile) { - j = (SequenceNode) leaves.elementAt(i++); - realnam = j.getName(); - nam = null; - if (namesleft>-1) - nam = algnIds.findIdMatch(realnam); - if (nam != null) { - j.setElement(nam); - namesleft--; - } else { - j.setElement(new Sequence(realnam, "THISISAPLACEHLDER")); - j.setPlaceholder(true); + top = treefile.getTree(); + maxheight = findHeight(top); + + SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs); + + Vector leaves = new Vector(); + findLeaves(top, leaves); + + int i = 0; + int namesleft = seqs.length; + + SequenceNode j; + SequenceI nam; + String realnam; + + while (i < leaves.size()) + { + j = (SequenceNode) leaves.elementAt(i++); + realnam = j.getName(); + nam = null; + + if (namesleft > -1) + { + nam = algnIds.findIdMatch(realnam); + } + + if (nam != null) + { + j.setElement(nam); + namesleft--; + } + else + { + j.setElement(new Sequence(realnam, "THISISAPLACEHLDER")); + j.setPlaceholder(true); + } + } + } - } + /** + * Creates a new NJTree object. + * + * @param sequence DOCUMENT ME! + * @param start DOCUMENT ME! + * @param end DOCUMENT ME! + */ + public NJTree(SequenceI[] sequence, int start, int end) + { + this(sequence, "NJ", "BL", start, end); } - } - /** - * - * used when the alignment associated to a tree has changed. - * - * @param alignment Vector - */ - public void UpdatePlaceHolders(Vector alignment) { - Vector leaves = new Vector(); - findLeaves(top, leaves); - int sz = leaves.size(); - SequenceIdMatcher seqmatcher=null; - int i=0; - while (i 2) + { + if (type.equals("NJ")) + { + float mind = findMinNJDistance(); + } + else + { + float mind = findMinDistance(); + } - makeLeaves(); + Cluster c = joinClusters(mini, minj); - noClus = cluster.size(); + done[minj] = 1; - cluster(); + cluster.setElementAt(null, minj); + cluster.setElementAt(c, mini); - } + noClus--; + } + boolean onefound = false; + + int one = -1; + int two = -1; + + for (int i = 0; i < noseqs; i++) + { + if (done[i] != 1) + { + if (onefound == false) + { + two = i; + onefound = true; + } + else + { + one = i; + } + } + } - public void cluster() { + Cluster c = joinClusters(one, two); + top = (SequenceNode) (node.elementAt(one)); - while (noClus > 2) { - if (type.equals("NJ")) { - float mind = findMinNJDistance(); - } else { - float mind = findMinDistance(); - } + reCount(top); + findHeight(top); + findMaxDist(top); + } - Cluster c = joinClusters(mini,minj); + /** + * DOCUMENT ME! + * + * @param i DOCUMENT ME! + * @param j DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Cluster joinClusters(int i, int j) + { + float dist = distance[i][j]; + int noi = ((Cluster) cluster.elementAt(i)).value.length; + int noj = ((Cluster) cluster.elementAt(j)).value.length; - done[minj] = 1; + int[] value = new int[noi + noj]; - cluster.setElementAt(null,minj); - cluster.setElementAt(c,mini); + for (int ii = 0; ii < noi; ii++) + { + value[ii] = ((Cluster) cluster.elementAt(i)).value[ii]; + } - noClus--; - } + for (int ii = noi; ii < (noi + noj); ii++) + { + value[ii] = ((Cluster) cluster.elementAt(j)).value[ii - noi]; + } - boolean onefound = false; + Cluster c = new Cluster(value); - int one = -1; - int two = -1; + ri = findr(i, j); + rj = findr(j, i); - for (int i=0; i < noseqs; i++) { - if (done[i] != 1) { - if (onefound == false) { - two = i; - onefound = true; - } else { - one = i; + if (type.equals("NJ")) + { + findClusterNJDistance(i, j); + } + else + { + findClusterDistance(i, j); } - } - } - - Cluster c = joinClusters(one,two); - top = (SequenceNode)(node.elementAt(one)); - reCount(top); - findHeight(top); - findMaxDist(top); + SequenceNode sn = new SequenceNode(); - } + sn.setLeft((SequenceNode) (node.elementAt(i))); + sn.setRight((SequenceNode) (node.elementAt(j))); - public Cluster joinClusters(int i, int j) { + SequenceNode tmpi = (SequenceNode) (node.elementAt(i)); + SequenceNode tmpj = (SequenceNode) (node.elementAt(j)); - float dist = distance[i][j]; + if (type.equals("NJ")) + { + findNewNJDistances(tmpi, tmpj, dist); + } + else + { + findNewDistances(tmpi, tmpj, dist); + } - int noi = ((Cluster)cluster.elementAt(i)).value.length; - int noj = ((Cluster)cluster.elementAt(j)).value.length; + tmpi.setParent(sn); + tmpj.setParent(sn); - int[] value = new int[noi + noj]; + node.setElementAt(sn, i); - for (int ii = 0; ii < noi;ii++) { - value[ii] = ((Cluster)cluster.elementAt(i)).value[ii]; + return c; } - for (int ii = noi; ii < noi+ noj;ii++) { - value[ii] = ((Cluster)cluster.elementAt(j)).value[ii-noi]; - } + /** + * DOCUMENT ME! + * + * @param tmpi DOCUMENT ME! + * @param tmpj DOCUMENT ME! + * @param dist DOCUMENT ME! + */ + public void findNewNJDistances(SequenceNode tmpi, SequenceNode tmpj, + float dist) + { + float ih = 0; + float jh = 0; - Cluster c = new Cluster(value); + SequenceNode sni = tmpi; + SequenceNode snj = tmpj; - ri = findr(i,j); - rj = findr(j,i); + tmpi.dist = ((dist + ri) - rj) / 2; + tmpj.dist = (dist - tmpi.dist); + + if (tmpi.dist < 0) + { + tmpi.dist = 0; + } - if (type.equals("NJ")) { - findClusterNJDistance(i,j); - } else { - findClusterDistance(i,j); + if (tmpj.dist < 0) + { + tmpj.dist = 0; + } } - SequenceNode sn = new SequenceNode(); + /** + * DOCUMENT ME! + * + * @param tmpi DOCUMENT ME! + * @param tmpj DOCUMENT ME! + * @param dist DOCUMENT ME! + */ + public void findNewDistances(SequenceNode tmpi, SequenceNode tmpj, + float dist) + { + float ih = 0; + float jh = 0; + + SequenceNode sni = tmpi; + SequenceNode snj = tmpj; - sn.setLeft((SequenceNode)(node.elementAt(i))); - sn.setRight((SequenceNode)(node.elementAt(j))); + while (sni != null) + { + ih = ih + sni.dist; + sni = (SequenceNode) sni.left(); + } - SequenceNode tmpi = (SequenceNode)(node.elementAt(i)); - SequenceNode tmpj = (SequenceNode)(node.elementAt(j)); + while (snj != null) + { + jh = jh + snj.dist; + snj = (SequenceNode) snj.left(); + } - if (type.equals("NJ")) { - findNewNJDistances(tmpi,tmpj,dist); - } else { - findNewDistances(tmpi,tmpj,dist); + tmpi.dist = ((dist / 2) - ih); + tmpj.dist = ((dist / 2) - jh); } - tmpi.setParent(sn); - tmpj.setParent(sn); + /** + * DOCUMENT ME! + * + * @param i DOCUMENT ME! + * @param j DOCUMENT ME! + */ + public void findClusterDistance(int i, int j) + { + int noi = ((Cluster) cluster.elementAt(i)).value.length; + int noj = ((Cluster) cluster.elementAt(j)).value.length; + + // New distances from cluster to others + float[] newdist = new float[noseqs]; + + for (int l = 0; l < noseqs; l++) + { + if ((l != i) && (l != j)) + { + newdist[l] = ((distance[i][l] * noi) + (distance[j][l] * noj)) / (noi + + noj); + } + else + { + newdist[l] = 0; + } + } - node.setElementAt(sn,i); - return c; - } + for (int ii = 0; ii < noseqs; ii++) + { + distance[i][ii] = newdist[ii]; + distance[ii][i] = newdist[ii]; + } + } - public void findNewNJDistances(SequenceNode tmpi, SequenceNode tmpj, float dist) { + /** + * DOCUMENT ME! + * + * @param i DOCUMENT ME! + * @param j DOCUMENT ME! + */ + public void findClusterNJDistance(int i, int j) + { + int noi = ((Cluster) cluster.elementAt(i)).value.length; + int noj = ((Cluster) cluster.elementAt(j)).value.length; + + // New distances from cluster to others + float[] newdist = new float[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; + } + else + { + newdist[l] = 0; + } + } - float ih = 0; - float jh = 0; + for (int ii = 0; ii < noseqs; ii++) + { + distance[i][ii] = newdist[ii]; + distance[ii][i] = newdist[ii]; + } + } - SequenceNode sni = tmpi; - SequenceNode snj = tmpj; + /** + * DOCUMENT ME! + * + * @param i DOCUMENT ME! + * @param j DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public float findr(int i, int j) + { + float tmp = 1; + + for (int k = 0; k < noseqs; k++) + { + if ((k != i) && (k != j) && (done[k] != 1)) + { + tmp = tmp + distance[i][k]; + } + } - tmpi.dist = (dist + ri - rj)/2; - tmpj.dist = (dist - tmpi.dist); + if (noClus > 2) + { + tmp = tmp / (noClus - 2); + } - if (tmpi.dist < 0) { - tmpi.dist = 0; + return tmp; } - if (tmpj.dist < 0) { - tmpj.dist = 0; - } - } - public void findNewDistances(SequenceNode tmpi,SequenceNode tmpj,float dist) { + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public float findMinNJDistance() + { + float min = 100000; + + for (int i = 0; i < (noseqs - 1); i++) + { + for (int j = i + 1; j < noseqs; j++) + { + if ((done[i] != 1) && (done[j] != 1)) + { + float tmp = distance[i][j] - (findr(i, j) + findr(j, i)); + + if (tmp < min) + { + mini = i; + minj = j; + + min = tmp; + } + } + } + } - float ih = 0; - float jh = 0; + return min; + } - SequenceNode sni = tmpi; - SequenceNode snj = tmpj; + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public float findMinDistance() + { + float min = 100000; + + for (int i = 0; i < (noseqs - 1); i++) + { + for (int j = i + 1; j < noseqs; j++) + { + if ((done[i] != 1) && (done[j] != 1)) + { + if (distance[i][j] < min) + { + mini = i; + minj = j; + + min = distance[i][j]; + } + } + } + } - while (sni != null) { - ih = ih + sni.dist; - sni = (SequenceNode)sni.left(); + return min; } - while (snj != null) { - jh = jh + snj.dist; - snj = (SequenceNode)snj.left(); + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + 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(sequence[i], sequence[j], start, end); + distance[j][i] = distance[i][j]; + } + } + } + } + else if (pwtype.equals("BL")) + { + int maxscore = 0; + + for (int i = 0; i < (noseqs - 1); i++) + { + for (int j = i; j < noseqs; j++) + { + int score = 0; + + for (int k = start; k < end; k++) + { + try + { + score += ResidueProperties.getBLOSUM62(sequence[i].getSequence( + k, k + 1), sequence[j].getSequence(k, k + + 1)); + } + 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]; + } + } + } + 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(); + 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]; + } + } + } + + return distance; } - tmpi.dist = (dist/2 - ih); - tmpj.dist = (dist/2 - jh); - } + /** + * DOCUMENT ME! + */ + public void makeLeaves() + { + cluster = new Vector(); + + for (int i = 0; i < noseqs; i++) + { + SequenceNode sn = new SequenceNode(); + sn.setElement(sequence[i]); + sn.setName(sequence[i].getName()); + node.addElement(sn); + int[] value = new int[1]; + value[0] = i; + + Cluster c = new Cluster(value); + cluster.addElement(c); + } + } - public void findClusterDistance(int i, int j) { + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + * @param leaves DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Vector findLeaves(SequenceNode node, Vector leaves) + { + if (node == null) + { + return leaves; + } - int noi = ((Cluster)cluster.elementAt(i)).value.length; - int noj = ((Cluster)cluster.elementAt(j)).value.length; + if ((node.left() == null) && (node.right() == null)) + { + leaves.addElement(node); - // New distances from cluster to others - float[] newdist = new float[noseqs]; + return leaves; + } + else + { + findLeaves((SequenceNode) node.left(), leaves); + findLeaves((SequenceNode) node.right(), leaves); + } - for (int l = 0; l < noseqs; l++) { - if ( l != i && l != j) { - newdist[l] = (distance[i][l] * noi + distance[j][l] * noj)/(noi + noj); - } else { - newdist[l] = 0; - } + return leaves; } - for (int ii=0; ii < noseqs;ii++) { - distance[i][ii] = newdist[ii]; - distance[ii][i] = newdist[ii]; + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + * @param count DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Object findLeaf(SequenceNode node, int count) + { + found = _findLeaf(node, count); + + return found; } - } - public void findClusterNJDistance(int i, int j) { + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + * @param count DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Object _findLeaf(SequenceNode node, int count) + { + if (node == null) + { + return null; + } - int noi = ((Cluster)cluster.elementAt(i)).value.length; - int noj = ((Cluster)cluster.elementAt(j)).value.length; + if (node.ycount == count) + { + found = node.element(); - // New distances from cluster to others - float[] newdist = new float[noseqs]; + return found; + } + else + { + _findLeaf((SequenceNode) node.left(), count); + _findLeaf((SequenceNode) node.right(), count); + } - for (int l = 0; l < noseqs; l++) { - if ( l != i && l != j) { - newdist[l] = (distance[i][l] + distance[j][l] - distance[i][j])/2; - } else { - newdist[l] = 0; - } + return found; } - for (int ii=0; ii < noseqs;ii++) { - distance[i][ii] = newdist[ii]; - distance[ii][i] = newdist[ii]; + /** + * printNode is mainly for debugging purposes. + * + * @param node SequenceNode + */ + public void printNode(SequenceNode node) + { + if (node == null) + { + return; + } + + if ((node.left() == null) && (node.right() == null)) + { + System.out.println("Leaf = " + + ((SequenceI) node.element()).getName()); + System.out.println("Dist " + ((SequenceNode) node).dist); + System.out.println("Boot " + node.getBootstrap()); + } + else + { + System.out.println("Dist " + ((SequenceNode) node).dist); + printNode((SequenceNode) node.left()); + printNode((SequenceNode) node.right()); + } } - } - public float findr(int i, int j) { + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + */ + public void findMaxDist(SequenceNode node) + { + if (node == null) + { + return; + } - float tmp = 1; - for (int k=0; k < noseqs;k++) { - if (k!= i && k!= j && done[k] != 1) { - tmp = tmp + distance[i][k]; - } - } + if ((node.left() == null) && (node.right() == null)) + { + float dist = ((SequenceNode) node).dist; - if (noClus > 2) { - tmp = tmp/(noClus - 2); + if (dist > maxDistValue) + { + maxdist = (SequenceNode) node; + maxDistValue = dist; + } + } + else + { + findMaxDist((SequenceNode) node.left()); + findMaxDist((SequenceNode) node.right()); + } } - return tmp; - } - - public float findMinNJDistance() { + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Vector getGroups() + { + return groups; + } - float min = 100000; + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public float getMaxHeight() + { + return maxheight; + } - for (int i=0; i < noseqs-1; i++) { - for (int j=i+1;j < noseqs;j++) { - if (done[i] != 1 && done[j] != 1) { - float tmp = distance[i][j] - (findr(i,j) + findr(j,i)); - if (tmp < min) { + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + * @param threshold DOCUMENT ME! + */ + public void groupNodes(SequenceNode node, float threshold) + { + if (node == null) + { + return; + } - mini = i; - minj = j; + if ((node.height / maxheight) > threshold) + { + groups.addElement(node); + } + else + { + groupNodes((SequenceNode) node.left(), threshold); + groupNodes((SequenceNode) node.right(), threshold); + } + } - min = tmp; + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public float findHeight(SequenceNode node) + { + if (node == null) + { + return maxheight; + } - } + if ((node.left() == null) && (node.right() == null)) + { + node.height = ((SequenceNode) node.parent()).height + node.dist; + + if (node.height > maxheight) + { + return node.height; + } + else + { + return maxheight; + } + } + else + { + if (node.parent() != null) + { + node.height = ((SequenceNode) node.parent()).height + + node.dist; + } + else + { + maxheight = 0; + node.height = (float) 0.0; + } + + maxheight = findHeight((SequenceNode) (node.left())); + maxheight = findHeight((SequenceNode) (node.right())); } - } + + return maxheight; } - return min; - } - public float findMinDistance() { + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public SequenceNode reRoot() + { + if (maxdist != null) + { + ycount = 0; - float min = 100000; + float tmpdist = maxdist.dist; - for (int i=0; i < noseqs-1;i++) { - for (int j = i+1; j < noseqs;j++) { - if (done[i] != 1 && done[j] != 1) { - if (distance[i][j] < min) { - mini = i; - minj = j; + // New top + SequenceNode sn = new SequenceNode(); + sn.setParent(null); - min = distance[i][j]; - } - } - } - } - return min; - } + // New right hand of top + SequenceNode snr = (SequenceNode) maxdist.parent(); + changeDirection(snr, maxdist); + System.out.println("Printing reversed tree"); + printN(snr); + snr.dist = tmpdist / 2; + maxdist.dist = tmpdist / 2; - public float[][] findDistances() { + snr.setParent(sn); + maxdist.setParent(sn); - 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(sequence[i], sequence[j], start, end); - distance[j][i] = distance[i][j]; - } - } - } - } else if (pwtype.equals("BL")) { - int maxscore = 0; + sn.setRight(snr); + sn.setLeft(maxdist); + + top = sn; - for (int i = 0; i < noseqs-1; i++) { - for (int j = i; j < noseqs; j++) { - int score = 0; - for (int k=start; k < end; k++) { - try{ - score += - ResidueProperties.getBLOSUM62(sequence[i].getSequence(k, - k + 1), - sequence[j].getSequence(k, - k + 1)); - }catch(Exception ex){System.err.println("err creating BLOSUM62 tree");ex.printStackTrace();} - } - distance[i][j] = (float)score; - if (score > maxscore) { - maxscore = score; - } + ycount = 0; + reCount(top); + findHeight(top); } - } - 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]; - } - } - } 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(); - 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]; - } - } - } - - return distance; - } - - public void makeLeaves() { - cluster = new Vector(); - - for (int i=0; i < noseqs; i++) { - SequenceNode sn = new SequenceNode(); - - sn.setElement(sequence[i]); - sn.setName(sequence[i].getName()); - node.addElement(sn); - - int[] value = new int[1]; - value[0] = i; - - Cluster c = new Cluster(value); - cluster.addElement(c); - } - } - - public Vector findLeaves(SequenceNode node, Vector leaves) { - if (node == null) { - return leaves; - } - - if (node.left() == null && node.right() == null) { - leaves.addElement(node); - return leaves; - } else { - findLeaves((SequenceNode)node.left(),leaves); - findLeaves((SequenceNode)node.right(),leaves); - } - return leaves; - } - - public Object findLeaf(SequenceNode node, int count) { - found = _findLeaf(node,count); - - return found; - } - public Object _findLeaf(SequenceNode node,int count) { - if (node == null) { - return null; - } - if (node.ycount == count) { - found = node.element(); - return found; - } else { - _findLeaf((SequenceNode)node.left(),count); - _findLeaf((SequenceNode)node.right(),count); - } - - return found; - } - - /** - * printNode is mainly for debugging purposes. - * - * @param node SequenceNode - */ - public void printNode(SequenceNode node) { - if (node == null) { - return; - } - if (node.left() == null && node.right() == null) { - System.out.println("Leaf = " + ((SequenceI)node.element()).getName()); - System.out.println("Dist " + ((SequenceNode)node).dist); - System.out.println("Boot " + node.getBootstrap()); - } else { - System.out.println("Dist " + ((SequenceNode)node).dist); - printNode((SequenceNode)node.left()); - printNode((SequenceNode)node.right()); - } - } - public void findMaxDist(SequenceNode node) { - if (node == null) { - return; - } - if (node.left() == null && node.right() == null) { - - float dist = ((SequenceNode)node).dist; - if (dist > maxDistValue) { - maxdist = (SequenceNode)node; - maxDistValue = dist; - } - } else { - findMaxDist((SequenceNode)node.left()); - findMaxDist((SequenceNode)node.right()); - } - } - public Vector getGroups() { - return groups; - } - public float getMaxHeight() { - return maxheight; - } - public void groupNodes(SequenceNode node, float threshold) { - if (node == null) { - return; - } - if (node.height/maxheight > threshold) { - groups.addElement(node); - } else { - groupNodes((SequenceNode)node.left(),threshold); - groupNodes((SequenceNode)node.right(),threshold); + return top; } - } - public float findHeight(SequenceNode node) { + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + */ + public static void printN(SequenceNode node) + { + if (node == null) + { + return; + } + + if ((node.left() != null) && (node.right() != null)) + { + printN((SequenceNode) node.left()); + printN((SequenceNode) node.right()); + } + else + { + System.out.println(" name = " + + ((SequenceI) node.element()).getName()); + } - if (node == null) { - return maxheight; + System.out.println(" dist = " + ((SequenceNode) node).dist + " " + + ((SequenceNode) node).count + " " + ((SequenceNode) node).height); } - if (node.left() == null && node.right() == null) { - node.height = ((SequenceNode)node.parent()).height + node.dist; - - if (node.height > maxheight) { - return node.height; - } else { - return maxheight; - } - } else { - if (node.parent() != null) { - node.height = ((SequenceNode)node.parent()).height + node.dist; - } else { - maxheight = 0; - node.height = (float)0.0; - } - - maxheight = findHeight((SequenceNode)(node.left())); - maxheight = findHeight((SequenceNode)(node.right())); - } - return maxheight; - } - public SequenceNode reRoot() { - if (maxdist != null) { - ycount = 0; - float tmpdist = maxdist.dist; - - // New top - SequenceNode sn = new SequenceNode(); - sn.setParent(null); - - // New right hand of top - SequenceNode snr = (SequenceNode)maxdist.parent(); - changeDirection(snr,maxdist); - System.out.println("Printing reversed tree"); - printN(snr); - snr.dist = tmpdist/2; - maxdist.dist = tmpdist/2; - - snr.setParent(sn); - maxdist.setParent(sn); - - sn.setRight(snr); - sn.setLeft(maxdist); - - top = sn; - - ycount = 0; - reCount(top); - findHeight(top); - - } - return top; - } - public static void printN(SequenceNode node) { - if (node == null) { - return; - } - - if (node.left() != null && node.right() != null) { - printN((SequenceNode)node.left()); - printN((SequenceNode)node.right()); - } else { - System.out.println(" name = " + ((SequenceI)node.element()).getName()); - } - System.out.println(" dist = " + ((SequenceNode)node).dist + " " + ((SequenceNode)node).count + " " + ((SequenceNode)node).height); - } - - public void reCount(SequenceNode node) { + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + */ + public void reCount(SequenceNode node) + { ycount = 0; _reCount(node); } - public void _reCount(SequenceNode node) { - if (node == null) { - return; - } - if (node.left() != null && node.right() != null) { - _reCount((SequenceNode)node.left()); - _reCount((SequenceNode)node.right()); + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + */ + public void _reCount(SequenceNode node) + { + if (node == null) + { + return; + } - SequenceNode l = (SequenceNode)node.left(); - SequenceNode r = (SequenceNode)node.right(); + if ((node.left() != null) && (node.right() != null)) + { + _reCount((SequenceNode) node.left()); + _reCount((SequenceNode) node.right()); - ((SequenceNode)node).count = l.count + r.count; - ((SequenceNode)node).ycount = (l.ycount + r.ycount)/2; + SequenceNode l = (SequenceNode) node.left(); + SequenceNode r = (SequenceNode) node.right(); - } else { - ((SequenceNode)node).count = 1; - ((SequenceNode)node).ycount = ycount++; + ((SequenceNode) node).count = l.count + r.count; + ((SequenceNode) node).ycount = (l.ycount + r.ycount) / 2; + } + else + { + ((SequenceNode) node).count = 1; + ((SequenceNode) node).ycount = ycount++; + } } - } - public void swapNodes(SequenceNode node) { - if (node == null) { + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + */ + public void swapNodes(SequenceNode node) + { + if (node == null) + { return; } - SequenceNode tmp = (SequenceNode)node.left(); + + SequenceNode tmp = (SequenceNode) node.left(); node.setLeft(node.right()); node.setRight(tmp); } - public void changeDirection(SequenceNode node, SequenceNode dir) { - if (node == null) { - return; - } - if (node.parent() != top) { - changeDirection((SequenceNode)node.parent(), node); - SequenceNode tmp = (SequenceNode)node.parent(); - - if (dir == node.left()) { - node.setParent(dir); - node.setLeft(tmp); - } else if (dir == node.right()) { - node.setParent(dir); - node.setRight(tmp); - } - - } else { - if (dir == node.left()) { - node.setParent(node.left()); - - if (top.left() == node) { - node.setRight(top.right()); - } else { - node.setRight(top.left()); + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + * @param dir DOCUMENT ME! + */ + public void changeDirection(SequenceNode node, SequenceNode dir) + { + if (node == null) + { + return; } - } else { - node.setParent(node.right()); - if (top.left() == node) { - node.setLeft(top.right()); - } else { - node.setLeft(top.left()); + if (node.parent() != top) + { + changeDirection((SequenceNode) node.parent(), node); + + SequenceNode tmp = (SequenceNode) node.parent(); + + if (dir == node.left()) + { + node.setParent(dir); + node.setLeft(tmp); + } + else if (dir == node.right()) + { + node.setParent(dir); + node.setRight(tmp); + } + } + else + { + if (dir == node.left()) + { + node.setParent(node.left()); + + if (top.left() == node) + { + node.setRight(top.right()); + } + else + { + node.setRight(top.left()); + } + } + else + { + node.setParent(node.right()); + + if (top.left() == node) + { + node.setLeft(top.right()); + } + else + { + node.setLeft(top.left()); + } + } } - } } - } - public void setMaxDist(SequenceNode node) { + + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + */ + public void setMaxDist(SequenceNode node) + { this.maxdist = maxdist; } - public SequenceNode getMaxDist() { + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public SequenceNode getMaxDist() + { return maxdist; } - public SequenceNode getTopNode() { + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public SequenceNode getTopNode() + { return top; } - } - -class Cluster { - - int[] value; - - public Cluster(int[] value) { - this.value = value; - } - +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +class Cluster +{ + int[] value; + + /** + * Creates a new Cluster object. + * + * @param value DOCUMENT ME! + */ + public Cluster(int[] value) + { + this.value = value; + } }