JAL-2805 name change
[jalview.git] / src / jalview / ext / archaeopteryx / ArchaeopteryxTreeBuilder.java
index 1517ed3..21a4e30 100644 (file)
@@ -2,78 +2,117 @@ package jalview.ext.archaeopteryx;
 
 import jalview.analysis.TreeBuilder;
 import jalview.datamodel.SequenceI;
-import jalview.math.MatrixI;
+import jalview.ext.forester.ForesterConversions;
+import jalview.ext.forester.ForesterMatrix;
+import jalview.util.MessageManager;
 
+import java.util.HashMap;
+import java.util.Map;
+
+import org.forester.evoinference.matrix.distance.DistanceMatrix;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyNode;
-
-public class ArchaeopteryxTreeBuilder // cannot inherit
-                                                        // TreeBuilder as that
-                                                        // demands the use of
-                                                        // SequenceNode (instead
-                                                        // of PhylogenyNode)
+import org.forester.phylogeny.data.NodeData;
+import org.forester.phylogeny.data.Sequence;
+
+/**
+ * Class for converting trees made in Jalview (through TreeBuilder) to trees
+ * compatible with Forester (Phylogeny objects).
+ * 
+ * Note that this currently demands a 1:1 relationship between tree nodes and
+ * the sequences used for generating them.
+ * 
+ * @author kjvanderheide
+ *
+ */
+public class ArchaeopteryxTreeConverter
 {
-  protected SequenceI[] sequences;
+  protected final SequenceI[] sequences;
 
-  private Phylogeny aptxTree;
+  protected final DistanceMatrix distances;
+  
+  protected final TreeBuilder jalviewTree;
+  
+  public String treeTitle;
 
-  private PhylogenyNode rootNode;
+  private final Phylogeny aptxTree;
 
-  protected MatrixI distances;
-
-  {
-    this.aptxTree = new Phylogeny();
-  }
-
-
-  public ArchaeopteryxTreeBuilder()
-  {
+  private PhylogenyNode rootNode;
 
-    this.rootNode = new PhylogenyNode();
+  private final Map<SequenceI, PhylogenyNode> alignmentBoundNodes;
 
-  }
+  private final Map<PhylogenyNode, SequenceI> nodesBoundAlignment;
 
-  public ArchaeopteryxTreeBuilder(PhylogenyNode treeRoot)
+  public ArchaeopteryxTreeConverter(final TreeBuilder calculatedTree)
   {
+    jalviewTree = calculatedTree;
+    sequences = jalviewTree.getSequences();
+    distances = ForesterMatrix.convertJalviewToForester(
+            jalviewTree.getDistances(), sequences);
+    aptxTree = new Phylogeny();
+    rootNode = new PhylogenyNode();
+    alignmentBoundNodes = new HashMap<>(distances.getSize());
+    nodesBoundAlignment = new HashMap<>(distances.getSize());
 
-    this.rootNode = treeRoot;
 
   }
 
-  public Phylogeny buildAptxTree(TreeBuilder tree)
+  public Phylogeny buildAptxTree(final PhylogenyNode treeRoot)
   {
-    this.sequences = tree.getSequences();
-    this.distances = tree.getDistances();
-
-    aptxTree.setName(
-            "PLEASE FIX ME reheaheth35yheqhb3q5hyq3bt3q5u4jwqjwuh6");
 
-    // final NeighborJoiningF nj = NeighborJoiningF.createInstance(false, 5);
+    if (treeRoot != null)
+    {
+      rootNode = treeRoot;
+    }
 
-    //
-    // final Phylogeny phy = nj.execute(JalviewMatrixToForesterMatrix
-    // .convertJalviewToForester(distances));
+    buildAptxTree();
 
-    return buildAptxTree(sequences);
+    return aptxTree;
 
   }
 
-  public Phylogeny buildAptxTree(SequenceI[] sequences)
+
+  public Phylogeny buildAptxTree()
   {
 
     for (SequenceI sequence : sequences)
     {
-      PhylogenyNode treeNode = new PhylogenyNode(sequence.getName());
-      rootNode.addAsChild(treeNode);
-
+      Sequence seq = ForesterConversions
+              .createForesterSequence(sequence, true);
+      PhylogenyNode sequenceNode = new PhylogenyNode(sequence.getName());
+      NodeData nodeData = sequenceNode.getNodeData();
+      nodeData.setSequence(seq);
+
+      nodesBoundAlignment.put(sequenceNode, sequence);
+      alignmentBoundNodes.put(sequence, sequenceNode);
+      rootNode.addAsChild(sequenceNode);
     }
 
+
     aptxTree.setRoot(rootNode);
+
+    treeTitle = generateTreeName();
+    aptxTree.setName(treeTitle);
+
     return aptxTree;
 
   }
 
+  public Map<SequenceI, PhylogenyNode> getAlignmentBoundNodes()
+  {
+    return alignmentBoundNodes;
+  }
 
+  public Map<PhylogenyNode, SequenceI> getNodesBoundAlignment()
+  {
+    return nodesBoundAlignment;
+  }
+
+  private Phylogeny clusterNodes()
+  {
+    return aptxTree;
+
+  }
   /**
    * Formats a localised title for the tree panel, like
    * <p>
@@ -83,31 +122,33 @@ public class ArchaeopteryxTreeBuilder // cannot inherit
    * 
    * @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() // Move this and add selection region to the
+                                   // title when applicable
+  {
+    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;
+    }
+  }
 }