1 package jalview.ext.archaeopteryx;
3 import jalview.datamodel.SequenceI;
4 import jalview.ext.treeviewer.ExternalTreeNodeI;
7 import java.util.ArrayList;
8 import java.util.HashMap;
12 import org.forester.phylogeny.PhylogenyMethods;
13 import org.forester.phylogeny.PhylogenyNode;
14 import org.forester.phylogeny.data.BranchColor;
16 public class TreeNode implements ExternalTreeNodeI
18 private final PhylogenyNode node;
20 private SequenceI nodeSeq;
22 private static Map<PhylogenyNode, ExternalTreeNodeI> originalNodes = new HashMap<>(
23 500); // prolly make this size dynamic
25 private static Map<ExternalTreeNodeI, PhylogenyNode> wrappedNodes = new HashMap<>(
28 private TreeNode(PhylogenyNode aptxNode)
31 originalNodes.put(aptxNode, this);
32 wrappedNodes.put(this, aptxNode);
38 public String getNodeName()
40 return node.getName();
45 public List<ExternalTreeNodeI> getAllDescendants()
48 List<PhylogenyNode> descNodes = PhylogenyMethods
49 .getAllDescendants(node);
50 return getUniqueWrappers(descNodes);
56 public List<ExternalTreeNodeI> getExternalDescendants()
58 List<PhylogenyNode> extDescNodes = node.getAllExternalDescendants();
59 return getUniqueWrappers(extDescNodes);
64 public List<ExternalTreeNodeI> getDirectChildren()
66 List<PhylogenyNode> childNodes = node.getDescendants();
67 return getUniqueWrappers(childNodes);
75 public void setSequence(SequenceI seq)
78 org.forester.phylogeny.data.Sequence foresterFormatSeq = DataConversions
79 .createForesterSequence(seq, true);
80 node.getNodeData().setSequence(foresterFormatSeq);
85 public SequenceI getSequence()
88 // ideally this would return a converted node.getNodeData().getSequence()
92 public void addAsChild(ExternalTreeNodeI childNode)
94 PhylogenyNode aptxNode = unwrapNode(childNode);
96 node.addAsChild(aptxNode);
107 public float getXcoord()
109 return node.getXcoord();
113 public void setBranchColor(Color branchColor)
115 node.getBranchData().setBranchColor(new BranchColor(branchColor));
120 public boolean isInternal()
122 return node.isInternal();
125 public static List<ExternalTreeNodeI> getUniqueWrappers(
126 List<PhylogenyNode> aptxNodes)
128 List<ExternalTreeNodeI> wrappedNodes = new ArrayList<>(
131 for (PhylogenyNode aptxNode : aptxNodes)
133 wrappedNodes.add(getUniqueWrapper(aptxNode));
139 * This method should be used to create new wrappers as there is a possibility
140 * the Archaeopteryx node was already introduced to Jalview previously so this
141 * avoids giving one node duplicate wrappers
146 public static ExternalTreeNodeI getUniqueWrapper(
147 PhylogenyNode aptxNode)
149 ExternalTreeNodeI wrappedNode = originalNodes.get(aptxNode);
150 if (wrappedNode == null)
152 wrappedNode = new TreeNode(aptxNode);
159 * Attempts to unwrap the given node, if the unwrapped node already exists it
160 * is simply returned as is. If it is not however, the wrapper will be used to
161 * create a new Archaeopteryx node. This way it becomes possible to construct
162 * new Archaeopteryx nodes from different tree viewers, as long as they
163 * implement the interface.
168 protected static PhylogenyNode unwrapNode(ExternalTreeNodeI wrappedNode)
170 PhylogenyNode aptxNode = wrappedNodes.get(wrappedNode);
171 if (aptxNode == null)
174 aptxNode = new PhylogenyNode(wrappedNode.getNodeName());
182 public int hashCode()
184 final int prime = 31;
186 result = prime * result + ((node == null) ? 0 : node.hashCode());
191 public boolean equals(Object obj)
201 if (getClass() != obj.getClass())
205 TreeNode other = (TreeNode) obj;
208 if (other.node != null)
213 else if (!node.equals(other.node))