From: jprocter Date: Fri, 18 Mar 2005 16:53:46 +0000 (+0000) Subject: New constructors for turning NewickFile objects into renderable trees X-Git-Tag: Release_2_0~556 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=3e891bdfe440363774a5daa1ff77c7b07541451e;p=jalview.git New constructors for turning NewickFile objects into renderable trees mapped onto the current set of sequences by matching leafnames to sequence names (where one string must contain the other, rather than direct equivalence). --- diff --git a/src/jalview/analysis/NJTree.java b/src/jalview/analysis/NJTree.java index 7324b6c..fb0ec33 100755 --- a/src/jalview/analysis/NJTree.java +++ b/src/jalview/analysis/NJTree.java @@ -5,6 +5,8 @@ import jalview.util.*; import jalview.schemes.ResidueProperties; import java.util.*; +import jalview.io.NewickFile; + public class NJTree { Vector cluster; @@ -44,6 +46,73 @@ public class NJTree { public NJTree(SequenceNode node) { top = node; maxheight = findHeight(top); + + } + // Private SequenceID class to do fuzzy .equals() method for Hashtable. + + private class SeqIdname { + String id; + + 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); + } + } + return false; + } + + + public boolean equals(SeqIdname s) { + if (id.startsWith(s.id) || s.id.startsWith(id)) { + return true; + } + return false; + } + + public boolean equals(String s) { + if (id.startsWith(s) || s.startsWith(id)) { + return true; + } + return false; + } + } + + public NJTree(SequenceI[] seqs, NewickFile treefile) { + top = treefile.getTree(); + maxheight = findHeight(top); + Hashtable names = new Hashtable(); + for (int i = 0; i < seqs.length; i++) + { + names.put(new SeqIdname(seqs[i].getDisplayId()), seqs[i]); + } + Vector leaves = new Vector(); + findLeaves(top, leaves); + int i = 0; + int namesleft = seqs.length; + SequenceNode j; + SeqIdname nam; + while (i < leaves.size()) + { + j = (SequenceNode) leaves.elementAt(i++); + nam = new SeqIdname(j.getName()); + if ((namesleft>-1) + && names.containsKey(nam)) + { + j.setElement(names.get(nam)); + namesleft--; + } else { + j.setElement(new Sequence(nam.id, "THISISAPLACEHLDER")); + } + } } public NJTree(SequenceI[] sequence,int start, int end) {