JAL-2795 moved matrix conversion to the ForesterMatrix class
[jalview.git] / src / jalview / ext / archaeopteryx / ArchaeopteryxTreeConverter.java
1 package jalview.ext.archaeopteryx;
2
3 import jalview.analysis.TreeBuilder;
4 import jalview.datamodel.SequenceI;
5 import jalview.util.MessageManager;
6
7 import org.forester.evoinference.matrix.distance.DistanceMatrix;
8 import org.forester.phylogeny.Phylogeny;
9 import org.forester.phylogeny.PhylogenyNode;
10
11 public class ArchaeopteryxTreeConverter
12 {
13   protected SequenceI[] sequences;
14
15   private Phylogeny aptxTree;
16
17   private PhylogenyNode rootNode;
18
19   protected DistanceMatrix distances;
20   
21   protected TreeBuilder jalviewTree;
22   
23   public String treeTitle;
24
25
26
27   public ArchaeopteryxTreeConverter(TreeBuilder calculatedTree)
28   {
29     this.jalviewTree = calculatedTree;
30     this.sequences = jalviewTree.getSequences();
31     this.distances = MatrixConverter.convertJalviewToForester(
32             jalviewTree.getDistances(), jalviewTree.getSequences());
33
34   }
35
36   public Phylogeny buildAptxTree()
37   {
38     this.rootNode = new PhylogenyNode();
39
40     return buildAptxTree(rootNode);
41
42   }
43
44   public Phylogeny buildAptxTree(PhylogenyNode treeRoot)
45   {
46
47     this.rootNode = treeRoot;
48
49     this.aptxTree = new Phylogeny();
50
51     this.treeTitle = generateTreeName();
52     this.aptxTree.setName(treeTitle);
53
54     // final NeighborJoiningF nj = NeighborJoiningF.createInstance(false, 5);
55
56     //
57     // final Phylogeny phy = nj.execute(JalviewMatrixToForesterMatrix
58     // .convertJalviewToForester(distances));
59
60     return buildAptxTree(sequences);
61
62   }
63
64   // testing method to be removed
65   public Phylogeny buildAptxTree(SequenceI[] sequences)
66   {
67
68     for (SequenceI sequence : sequences)
69     {
70       PhylogenyNode treeNode = new PhylogenyNode(sequence.getName());
71       rootNode.addAsChild(treeNode);
72
73     }
74
75     aptxTree.setRoot(rootNode);
76     return aptxTree;
77
78   }
79
80
81   /**
82    * Formats a localised title for the tree panel, like
83    * <p>
84    * Neighbour Joining Using BLOSUM62
85    * <p>
86    * For a tree loaded from file, just uses the file name
87    * 
88    * @return
89    */
90   public String generateTreeName()
91   {
92     if (treeTitle != null) // will currently never happen, loaded tree file will
93                            // take a different path
94     {
95       return treeTitle;
96     }
97     else
98     {
99       /*
100       * i18n description of Neighbour Joining or Average Distance method
101       */
102       String treecalcnm = MessageManager
103               .getString("label.tree_calc_" + jalviewTree.getClass()
104                       .getSimpleName().substring(0, 2).toLowerCase());
105       /*
106       * short score model name (long description can be too long)
107       */
108       String smn = jalviewTree.getScoreModel().getName();
109
110       /*
111       * put them together as <method> Using <model>
112       */
113       final String ttl = MessageManager
114               .formatMessage("label.treecalc_title", treecalcnm, smn);
115       return ttl;
116     }
117   }
118 }