X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FNJTree.java;h=be3b96d01355532ffc897155761d70c24aeaa6d9;hb=88eaada36951276cf8b2863bb95aa37148166ea2;hp=8f3483d37a27c0918a2a83f88c52ba02215caebf;hpb=00b702186f1676036db687a8df5d335a4d5da54d;p=jalview.git diff --git a/src/jalview/analysis/NJTree.java b/src/jalview/analysis/NJTree.java index 8f3483d..be3b96d 100755 --- a/src/jalview/analysis/NJTree.java +++ b/src/jalview/analysis/NJTree.java @@ -1,735 +1,1207 @@ +/* +* Jalview - A Sequence Alignment Editor and Viewer +* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle +* +* This program 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 2 +* of the License, or (at your option) any later version. +* +* This program 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 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; +import jalview.schemes.ResidueProperties; - int done[]; - int noseqs; - int noClus; +import jalview.util.*; - float distance[][]; +import java.util.*; - int mini; - int minj; - float ri; - float rj; - Vector groups = new Vector(); - SequenceNode maxdist; - SequenceNode top; +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class NJTree +{ + Vector cluster; + SequenceI[] sequence; + + //SequenceData is a string representation of what the user + //sees. The display may contain hidden columns. + String [] sequenceString; + + 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; + + boolean hasDistances = true; // normal case for jalview trees + boolean hasBootstrap = false; // normal case for jalview trees + + private boolean hasRootDistance = true; + + + /** + * Creates a new NJTree object. + * + * @param seqs DOCUMENT ME! + * @param treefile DOCUMENT ME! + */ + public NJTree(SequenceI[] seqs, NewickFile treefile) + { + top = treefile.getTree(); + + if (sequenceString == null) + { + sequenceString = new String[sequence.length]; + for (int i = 0; i < sequence.length; i++) + { + sequenceString[i] = sequence[i].getSequence(); + } + } - float maxDistValue; - float maxheight; - int ycount; + hasDistances = treefile.HasDistances(); + hasBootstrap = treefile.HasBootstrap(); + hasRootDistance = treefile.HasRootDistance(); - Vector node; + maxheight = findHeight(top); - String type; - String pwtype; + SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs); - Object found = null; - Object leaves = null; + Vector leaves = new Vector(); + findLeaves(top, leaves); - int start; - int end; + int i = 0; + int namesleft = seqs.length; - public NJTree(SequenceNode node) { - top = node; - maxheight = findHeight(top); + SequenceNode j; + SequenceI nam; + String realnam; - } - // Private SequenceID class to do fuzzy .equals() method for Hashtable. + while (i < leaves.size()) + { + j = (SequenceNode) leaves.elementAt(i++); + realnam = j.getName(); + nam = null; - private class SeqIdname { - String id; + if (namesleft > -1) + { + nam = algnIds.findIdMatch(realnam); + } - SeqIdname(String s) { - id = new String(s); - } - public int hashCode() { - return (id.substring(0,4).hashCode()); - } - public boolean equals(Object s) { - if (s instanceof SeqIdname) { - return this.equals((SeqIdname) s); - } else { - if (s instanceof String) { - return this.equals((String) s); + if (nam != null) + { + j.setElement(nam); + namesleft--; + } + else + { + j.setElement(new Sequence(realnam, "THISISAPLACEHLDER")); + j.setPlaceholder(true); + } } - } - return false; } + /** + * Creates a new NJTree object. + * + * @param sequence DOCUMENT ME! + * @param type DOCUMENT ME! + * @param pwtype DOCUMENT ME! + * @param start DOCUMENT ME! + * @param end DOCUMENT ME! + */ + public NJTree(SequenceI[] sequence, + String [] sequenceString, + String type, + String pwtype, + int start, int end) + { + this.sequence = sequence; + this.node = new Vector(); + this.type = type; + this.pwtype = pwtype; + + if (sequenceString == null) + { + this.sequenceString = new String[sequence.length]; + for(int i=0; i-1) - && names.containsKey(nam)) - { - j.setElement(names.get(nam)); - namesleft--; - } else { - j.setElement(new Sequence(nam.id, "THISISAPLACEHLDER")); - } - } - } + if (!(type.equals("NJ"))) + { + type = "AV"; + } - public NJTree(SequenceI[] sequence,int start, int end) { - this(sequence,"NJ","BL",start,end); - } + if (!(pwtype.equals("PID"))) + { + type = "BL"; + } - public NJTree(SequenceI[] sequence,String type,String pwtype,int start, int end ) { + int i = 0; - this.sequence = sequence; - this.node = new Vector(); - this.type = type; - this.pwtype = pwtype; - this.start = start; - this.end = end; + done = new int[sequence.length]; - if (!(type.equals("NJ"))) { - type = "AV"; - } + while ((i < sequence.length) && (sequence[i] != null)) + { + done[i] = 0; + i++; + } - if (!(pwtype.equals("PID"))) { - type = "BL"; - } + noseqs = i++; - int i=0; + distance = findDistances(); - done = new int[sequence.length]; + makeLeaves(); + noClus = cluster.size(); - while (i < sequence.length && sequence[i] != null) { - done[i] = 0; - i++; + cluster(); } - noseqs = i++; - - distance = findDistances(); - - makeLeaves(); - - noClus = cluster.size(); + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String toString() + { + jalview.io.NewickFile fout = new jalview.io.NewickFile(getTopNode()); - cluster(); + return fout.print(false, true); // distances only + } - } + /** + * + * 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 < sz) + { + SequenceNode leaf = (SequenceNode) leaves.elementAt(i++); + + if (alignment.contains(leaf.element())) + { + leaf.setPlaceholder(false); + } + else + { + if (seqmatcher == null) + { + // Only create this the first time we need it + SequenceI[] seqs = new SequenceI[alignment.size()]; + + for (int j = 0; j < seqs.length; j++) + seqs[j] = (SequenceI) alignment.elementAt(j); + + seqmatcher = new SequenceIdMatcher(seqs); + } + + SequenceI nam = seqmatcher.findIdMatch(leaf.getName()); + + if (nam != null) + { + leaf.setPlaceholder(false); + leaf.setElement(nam); + } + else + { + leaf.setPlaceholder(true); + } + } + } + } + /** + * DOCUMENT ME! + */ + public void cluster() + { + while (noClus > 2) + { + if (type.equals("NJ")) + { + findMinNJDistance(); + } + else + { + findMinDistance(); + } - public void cluster() { + Cluster c = joinClusters(mini, minj); - while (noClus > 2) { - if (type.equals("NJ")) { - float mind = findMinNJDistance(); - } else { - float mind = findMinDistance(); - } + done[minj] = 1; - Cluster c = joinClusters(mini,minj); + cluster.setElementAt(null, minj); + cluster.setElementAt(c, mini); + noClus--; + } - done[minj] = 1; + 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; + } + } + } - cluster.setElementAt(null,minj); - cluster.setElementAt(c,mini); + joinClusters(one, two); + top = (SequenceNode) (node.elementAt(one)); - noClus--; + reCount(top); + findHeight(top); + findMaxDist(top); } - boolean onefound = false; + /** + * 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 one = -1; - int two = -1; + int noi = ((Cluster) cluster.elementAt(i)).value.length; + int noj = ((Cluster) cluster.elementAt(j)).value.length; - for (int i=0; i < noseqs; i++) { - if (done[i] != 1) { - if (onefound == false) { - two = i; - onefound = true; - } else { - one = i; - } - } - } + int[] value = new int[noi + noj]; - Cluster c = joinClusters(one,two); - top = (SequenceNode)(node.elementAt(one)); + for (int ii = 0; ii < noi; ii++) + { + value[ii] = ((Cluster) cluster.elementAt(i)).value[ii]; + } - reCount(top); - findHeight(top); - findMaxDist(top); + for (int ii = noi; ii < (noi + noj); ii++) + { + value[ii] = ((Cluster) cluster.elementAt(j)).value[ii - noi]; + } - } + Cluster c = new Cluster(value); - public Cluster joinClusters(int i, int j) { + ri = findr(i, j); + rj = findr(j, i); - float dist = distance[i][j]; + if (type.equals("NJ")) + { + findClusterNJDistance(i, j); + } + else + { + findClusterDistance(i, j); + } - int noi = ((Cluster)cluster.elementAt(i)).value.length; - int noj = ((Cluster)cluster.elementAt(j)).value.length; + SequenceNode sn = new SequenceNode(); - int[] value = new int[noi + noj]; + sn.setLeft((SequenceNode) (node.elementAt(i))); + sn.setRight((SequenceNode) (node.elementAt(j))); - for (int ii = 0; ii < noi;ii++) { - value[ii] = ((Cluster)cluster.elementAt(i)).value[ii]; - } + SequenceNode tmpi = (SequenceNode) (node.elementAt(i)); + SequenceNode tmpj = (SequenceNode) (node.elementAt(j)); - for (int ii = noi; ii < noi+ noj;ii++) { - value[ii] = ((Cluster)cluster.elementAt(j)).value[ii-noi]; - } + if (type.equals("NJ")) + { + findNewNJDistances(tmpi, tmpj, dist); + } + else + { + findNewDistances(tmpi, tmpj, dist); + } - Cluster c = new Cluster(value); + tmpi.setParent(sn); + tmpj.setParent(sn); - ri = findr(i,j); - rj = findr(j,i); + node.setElementAt(sn, i); - if (type.equals("NJ")) { - findClusterNJDistance(i,j); - } else { - findClusterDistance(i,j); + return c; } - SequenceNode sn = new SequenceNode(); + /** + * DOCUMENT ME! + * + * @param tmpi DOCUMENT ME! + * @param tmpj DOCUMENT ME! + * @param dist DOCUMENT ME! + */ + public void findNewNJDistances(SequenceNode tmpi, SequenceNode tmpj, + float dist) + { - sn.setLeft((SequenceNode)(node.elementAt(i))); - sn.setRight((SequenceNode)(node.elementAt(j))); + tmpi.dist = ((dist + ri) - rj) / 2; + tmpj.dist = (dist - tmpi.dist); - SequenceNode tmpi = (SequenceNode)(node.elementAt(i)); - SequenceNode tmpj = (SequenceNode)(node.elementAt(j)); + if (tmpi.dist < 0) + { + tmpi.dist = 0; + } - if (type.equals("NJ")) { - findNewNJDistances(tmpi,tmpj,dist); - } else { - findNewDistances(tmpi,tmpj,dist); + if (tmpj.dist < 0) + { + tmpj.dist = 0; + } } - tmpi.setParent(sn); - tmpj.setParent(sn); - - node.setElementAt(sn,i); - return c; - } - - public void findNewNJDistances(SequenceNode tmpi, SequenceNode tmpj, float dist) { + /** + * 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; - float ih = 0; - float jh = 0; + SequenceNode sni = tmpi; + SequenceNode snj = tmpj; - SequenceNode sni = tmpi; - SequenceNode snj = tmpj; + while (sni != null) + { + ih = ih + sni.dist; + sni = (SequenceNode) sni.left(); + } - tmpi.dist = (dist + ri - rj)/2; - tmpj.dist = (dist - tmpi.dist); + while (snj != null) + { + jh = jh + snj.dist; + snj = (SequenceNode) snj.left(); + } - if (tmpi.dist < 0) { - tmpi.dist = 0; + tmpi.dist = ((dist / 2) - ih); + tmpj.dist = ((dist / 2) - jh); } - if (tmpj.dist < 0) { - tmpj.dist = 0; - } - } - public void findNewDistances(SequenceNode tmpi,SequenceNode tmpj,float dist) { + /** + * 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; + } + } - 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! + */ + public void findClusterNJDistance(int i, int j) + { - while (sni != null) { - ih = ih + sni.dist; - sni = (SequenceNode)sni.left(); - } + // 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; + } + } - while (snj != null) { - jh = jh + snj.dist; - snj = (SequenceNode)snj.left(); + for (int ii = 0; ii < noseqs; ii++) + { + distance[i][ii] = newdist[ii]; + distance[ii][i] = newdist[ii]; + } } - tmpi.dist = (dist/2 - ih); - tmpj.dist = (dist/2 - jh); - } + /** + * 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]; + } + } + if (noClus > 2) + { + tmp = tmp / (noClus - 2); + } + return tmp; + } - public void findClusterDistance(int i, int j) { + /** + * 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; + } + } + } + } - int noi = ((Cluster)cluster.elementAt(i)).value.length; - int noj = ((Cluster)cluster.elementAt(j)).value.length; + return min; + } - // New distances from cluster to others - float[] newdist = new float[noseqs]; + /** + * 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]; + } + } + } + } - 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 min; } - for (int ii=0; ii < noseqs;ii++) { - distance[i][ii] = newdist[ii]; - distance[ii][i] = newdist[ii]; + /** + * 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(sequenceString[i], sequenceString[j]); + + distance[j][i] = distance[i][j]; + } + } + } + } + else if (pwtype.equals("BL")) + { + 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 += ResidueProperties.getBLOSUM62( + sequenceString[i].substring(k, k + 1), + sequenceString[j].substring(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(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]; + } + } + }/*/ + + return distance; } - } - - 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; - } - } + /** + * DOCUMENT ME! + */ + public void makeLeaves() + { + cluster = new Vector(); - for (int ii=0; ii < noseqs;ii++) { - distance[i][ii] = newdist[ii]; - distance[ii][i] = newdist[ii]; - } - } + for (int i = 0; i < noseqs; i++) + { + SequenceNode sn = new SequenceNode(); - public float findr(int i, int j) { + sn.setElement(sequence[i]); + sn.setName(sequence[i].getName()); + node.addElement(sn); - float tmp = 1; - for (int k=0; k < noseqs;k++) { - if (k!= i && k!= j && done[k] != 1) { - tmp = tmp + distance[i][k]; - } - } + int[] value = new int[1]; + value[0] = i; - if (noClus > 2) { - tmp = tmp/(noClus - 2); + Cluster c = new Cluster(value); + cluster.addElement(c); + } } - return tmp; - } - - public float findMinNJDistance() { + /** + * 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; + } - float min = 100000; + if ((node.left() == null) && (node.right() == null)) + { + leaves.addElement(node); - 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) { + return leaves; + } + else + { + findLeaves((SequenceNode) node.left(), leaves); + findLeaves((SequenceNode) node.right(), leaves); + } - mini = i; - minj = j; + return leaves; + } - min = tmp; + /** + * 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; } - return min; - } - public float findMinDistance() { - - float min = 100000; + /** + * 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; + } - 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; + if (node.ycount == count) + { + found = node.element(); - min = distance[i][j]; - } + return found; } - } + else + { + _findLeaf((SequenceNode) node.left(), count); + _findLeaf((SequenceNode) node.right(), count); + } + + return found; } - return min; - } - 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]); - 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=0; k < sequence[i].getLength(); k++) { - try{ - score += - ResidueProperties.getBLOSUM62(sequence[i].getSequence(k, - k + 1), - sequence[j].getSequence(k, - k + 1)); - }catch(Exception ex){System.out.println("err creating BLOSUM62 tree");} - } - 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]; + /** + * printNode is mainly for debugging purposes. + * + * @param node SequenceNode + */ + public void printNode(SequenceNode node) + { + if (node == null) + { + return; } - } - } 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]; - } + + 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()); } - } - 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]; + else + { + System.out.println("Dist " + ((SequenceNode) node).dist); + printNode((SequenceNode) node.left()); + printNode((SequenceNode) node.right()); } - } } - 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); + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + */ + public void findMaxDist(SequenceNode node) + { + if (node == null) + { + return; + } - int[] value = new int[1]; - value[0] = i; + if ((node.left() == null) && (node.right() == null)) + { + float dist = ((SequenceNode) node).dist; - Cluster c = new Cluster(value); - cluster.addElement(c); + if (dist > maxDistValue) + { + maxdist = (SequenceNode) node; + maxDistValue = dist; + } + } + else + { + findMaxDist((SequenceNode) node.left()); + findMaxDist((SequenceNode) node.right()); + } } - } - public Vector findLeaves(SequenceNode node, Vector leaves) { - if (node == null) { - return leaves; + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Vector getGroups() + { + return groups; } - if (node.left() == null && node.right() == null) { - leaves.addElement(node); - return leaves; - } else { - findLeaves((SequenceNode)node.left(),leaves); - findLeaves((SequenceNode)node.right(),leaves); + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public float getMaxHeight() + { + return maxheight; } - return leaves; - } - public Object findLeaf(SequenceNode node, int count) { - found = _findLeaf(node,count); + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + * @param threshold DOCUMENT ME! + */ + public void groupNodes(SequenceNode node, float threshold) + { + if (node == null) + { + return; + } - 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); + if ((node.height / maxheight) > threshold) + { + groups.addElement(node); + } + else + { + groupNodes((SequenceNode) node.left(), threshold); + groupNodes((SequenceNode) node.right(), threshold); + } } - return found; - } + /** + * DOCUMENT ME! + * + * @param node DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public float findHeight(SequenceNode node) + { + if (node == null) + { + return maxheight; + } - 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) { + 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())); + } - 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); - } - } + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public SequenceNode reRoot() + { + if (maxdist != null) + { + ycount = 0; - public float findHeight(SequenceNode node) { + float tmpdist = maxdist.dist; - if (node == null) { - return maxheight; - } + // New top + SequenceNode sn = new SequenceNode(); + sn.setParent(null); - if (node.left() == null && node.right() == null) { - node.height = ((SequenceNode)node.parent()).height + node.dist; + // 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; - 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; + snr.setParent(sn); + maxdist.setParent(sn); - // New top - SequenceNode sn = new SequenceNode(); - sn.setParent(null); + sn.setRight(snr); + sn.setLeft(maxdist); - // 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; + top = sn; - snr.setParent(sn); - maxdist.setParent(sn); + ycount = 0; + reCount(top); + findHeight(top); + } - sn.setRight(snr); - sn.setLeft(maxdist); + return top; + } - top = sn; + public String printOriginalSequenceData() + { + StringBuffer sb = new StringBuffer(); + for(int i=0; i