1 package jalview.ext.archaeopteryx;
3 import jalview.analysis.TreeBuilder;
4 import jalview.datamodel.SequenceI;
5 import jalview.ext.forester.DataConversions;
6 import jalview.ext.forester.ForesterMatrix;
7 import jalview.ext.treeviewer.ExternalTreeBuilderI;
8 import jalview.util.MessageManager;
10 import java.util.HashMap;
13 import org.forester.evoinference.matrix.distance.DistanceMatrix;
14 import org.forester.phylogeny.Phylogeny;
15 import org.forester.phylogeny.PhylogenyNode;
16 import org.forester.phylogeny.data.NodeData;
17 import org.forester.phylogeny.data.Sequence;
20 * Class for converting trees made in Jalview (through TreeBuilder) to trees
21 * compatible with Forester (Phylogeny objects).
23 * Note that this currently demands a 1:1 relationship between tree nodes and
24 * the sequences used for generating them.
26 * @author kjvanderheide
29 public class AptxTreeBuilder
30 implements ExternalTreeBuilderI<Phylogeny, PhylogenyNode>
32 protected final SequenceI[] sequences;
34 protected final DistanceMatrix distances;
36 protected final TreeBuilder jalviewTree;
38 public String treeTitle;
40 private final Phylogeny aptxTree;
42 private PhylogenyNode rootNode;
44 private final Map<SequenceI, PhylogenyNode> alignmentWithNodes;
46 private final Map<PhylogenyNode, SequenceI> nodesWithAlignment;
48 public AptxTreeBuilder(final TreeBuilder calculatedTree)
50 jalviewTree = calculatedTree;
51 sequences = jalviewTree.getSequences();
52 distances = ForesterMatrix.convertJalviewToForester(
53 jalviewTree.getDistances(), sequences);
55 aptxTree = new Phylogeny();
56 rootNode = new PhylogenyNode();
58 int amountOfSequences = distances.getSize();
59 alignmentWithNodes = new HashMap<>(amountOfSequences);
60 nodesWithAlignment = new HashMap<>(amountOfSequences);
66 public Phylogeny buildTree(final PhylogenyNode treeRoot)
82 public Phylogeny buildTree()
85 for (SequenceI sequence : sequences)
87 Sequence seq = DataConversions
88 .createForesterSequence(sequence, true);
89 PhylogenyNode sequenceNode = new PhylogenyNode(sequence.getName());
91 NodeData nodeData = sequenceNode.getNodeData();
92 nodeData.setSequence(seq);
94 ExternalTreeBuilderI.putWithDuplicationCheck(nodesWithAlignment,
95 sequenceNode, sequence);
96 ExternalTreeBuilderI.putWithDuplicationCheck(alignmentWithNodes,
97 sequence, sequenceNode);
98 rootNode.addAsChild(sequenceNode);
102 aptxTree.setRoot(rootNode);
104 treeTitle = generateTreeName();
105 aptxTree.setName(treeTitle);
112 public Map<SequenceI, PhylogenyNode> getAlignmentBoundNodes()
114 return alignmentWithNodes;
118 public Map<PhylogenyNode, SequenceI> getNodesBoundAlignment()
120 return nodesWithAlignment;
123 private Phylogeny clusterNodes()
129 * Formats a localised title for the tree panel, like
131 * Neighbour Joining Using BLOSUM62
133 * For a tree loaded from file, just uses the file name
138 public String generateTreeName() // Move this and add selection region to the
139 // title when applicable
141 if (treeTitle != null) // will currently never happen, loaded tree file will
142 // take a different path
149 * i18n description of Neighbour Joining or Average Distance method
151 String treecalcnm = MessageManager
152 .getString("label.tree_calc_" + jalviewTree.getClass()
153 .getSimpleName().substring(0, 2).toLowerCase());
155 * short score model name (long description can be too long)
157 String smn = jalviewTree.getScoreModel().getName();
160 * put them together as <method> Using <model>
162 final String ttl = MessageManager
163 .formatMessage("label.treecalc_title", treecalcnm, smn);