JAL-2795 Forester now NJ-clusters the DistanceMatrix from Jalview
[jalview.git] / src / jalview / ext / archaeopteryx / ArchaeopteryxTreeConverter.java
index 780acee..ce603f5 100644 (file)
@@ -2,73 +2,68 @@ package jalview.ext.archaeopteryx;
 
 import jalview.analysis.TreeBuilder;
 import jalview.datamodel.SequenceI;
-import jalview.math.MatrixI;
+import jalview.ext.forester.ForesterMatrix;
+import jalview.util.MessageManager;
 
+import org.forester.evoinference.distance.NeighborJoiningF;
+import org.forester.evoinference.matrix.distance.DistanceMatrix;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyNode;
 
 public class ArchaeopteryxTreeConverter
 {
-  protected SequenceI[] sequences;
+  protected final SequenceI[] sequences;
 
   private Phylogeny aptxTree;
 
   private PhylogenyNode rootNode;
 
-  protected MatrixI distances;
+  protected final DistanceMatrix distances;
+  
+  protected final TreeBuilder jalviewTree;
+  
+  public String treeTitle;
 
-  {
-    this.aptxTree = new Phylogeny();
-  }
-
-
-  public ArchaeopteryxTreeConverter(TreeBuilder calculatedTree)
-  {
-    this.sequences = calculatedTree.getSequences();
-    this.distances = calculatedTree.getDistances();
-
-    this.rootNode = new PhylogenyNode();
-    this.rootNode.setName("root");
 
-  }
 
-  public ArchaeopteryxTreeConverter(PhylogenyNode treeRoot,
-          TreeBuilder calculatedTree)
+  public ArchaeopteryxTreeConverter(final TreeBuilder calculatedTree)
   {
-    this(calculatedTree);
-    this.rootNode = treeRoot;
+    jalviewTree = calculatedTree;
+    sequences = jalviewTree.getSequences();
+    distances = ForesterMatrix.convertJalviewToForester(
+            jalviewTree.testDistances, sequences);
+    aptxTree = new Phylogeny();
+    rootNode = new PhylogenyNode();
 
   }
 
-  public Phylogeny buildAptxTree()
+  public Phylogeny buildAptxTree(final PhylogenyNode treeRoot)
   {
 
+    rootNode = treeRoot;
 
-    this.aptxTree.setName(
-            "PLEASE FIX ME reheaheth35yheqhb3q5hyq3bt3q5u4jwqjwuh6");
-
-    // final NeighborJoiningF nj = NeighborJoiningF.createInstance(false, 5);
+    buildAptxTree();
 
-    //
-    // final Phylogeny phy = nj.execute(JalviewMatrixToForesterMatrix
-    // .convertJalviewToForester(distances));
 
-    return buildAptxTree(sequences);
+    return aptxTree;
 
   }
 
-  // testing method to be removed
-  public Phylogeny buildAptxTree(SequenceI[] sequences)
+  public Phylogeny buildAptxTree()
   {
 
-    for (SequenceI sequence : sequences)
-    {
-      PhylogenyNode treeNode = new PhylogenyNode(sequence.getName());
-      rootNode.addAsChild(treeNode);
+    NeighborJoiningF foresterClustering = NeighborJoiningF
+            .createInstance();
+    aptxTree = foresterClustering.execute(distances);
+    treeTitle = generateTreeName();
+    aptxTree.setName(treeTitle);
 
-    }
+    return aptxTree;
+
+  }
 
-    aptxTree.setRoot(rootNode);
+  private Phylogeny clusterNodes()
+  {
     return aptxTree;
 
   }
@@ -83,31 +78,32 @@ public class ArchaeopteryxTreeConverter
    * 
    * @return
    */
-  // public String getPanelTitle()
-  // {
-  // if (treeTitle != null)
-  // {
-  // return treeTitle;
-  // }
-  // else
-  // {
-  // /*
-  // * i18n description of Neighbour Joining or Average Distance method
-  // */
-  // String treecalcnm = MessageManager
-  // .getString("label.tree_calc_" + treeType.toLowerCase());
-  //
-  // /*
-  // * short score model name (long description can be too long)
-  // */
-  // String smn = substitutionMatrix;
-  //
-  // /*
-  // * put them together as <method> Using <model>
-  // */
-  // final String ttl = MessageManager
-  // .formatMessage("label.treecalc_title", treecalcnm, smn);
-  // return ttl;
-  // }
-  // }
+  public String generateTreeName()
+  {
+    if (treeTitle != null) // will currently never happen, loaded tree file will
+                           // take a different path
+    {
+      return treeTitle;
+    }
+    else
+    {
+      /*
+      * i18n description of Neighbour Joining or Average Distance method
+      */
+      String treecalcnm = MessageManager
+              .getString("label.tree_calc_" + jalviewTree.getClass()
+                      .getSimpleName().substring(0, 2).toLowerCase());
+      /*
+      * short score model name (long description can be too long)
+      */
+      String smn = jalviewTree.getScoreModel().getName();
+
+      /*
+      * put them together as <method> Using <model>
+      */
+      final String ttl = MessageManager
+              .formatMessage("label.treecalc_title", treecalcnm, smn);
+      return ttl;
+    }
+  }
 }