Merge branch 'develop' into features/JAL-2393customMatrices
[jalview.git] / src / jalview / gui / TreePanel.java
index 74dd29a..d4c6009 100755 (executable)
 package jalview.gui;
 
 import jalview.analysis.AlignmentSorter;
+import jalview.analysis.AverageDistanceTree;
 import jalview.analysis.NJTree;
-import jalview.analysis.scoremodels.ScoreModels;
+import jalview.analysis.TreeBuilder;
+import jalview.analysis.TreeModel;
 import jalview.api.analysis.ScoreModelI;
+import jalview.api.analysis.SimilarityParamsI;
 import jalview.api.analysis.ViewBasedAnalysisI;
 import jalview.bin.Cache;
 import jalview.commands.CommandI;
@@ -71,47 +74,46 @@ import org.jibble.epsgraphics.EpsGraphics2D;
  */
 public class TreePanel extends GTreePanel
 {
-  String type;
+  String treeType;
 
-  String pwtype;
+  ScoreModelI scoreModel; // if tree computed
+
+  String treeTitle; // if tree loaded
+
+  SimilarityParamsI similarityParams;
 
   TreeCanvas treeCanvas;
 
-  NJTree tree;
+  TreeModel tree;
 
   AlignViewport av;
 
   /**
    * Creates a new TreePanel object.
    * 
-   * @param av
-   *          DOCUMENT ME!
-   * @param seqVector
-   *          DOCUMENT ME!
+   * @param ap
    * @param type
-   *          DOCUMENT ME!
-   * @param pwtype
-   *          DOCUMENT ME!
-   * @param s
-   *          DOCUMENT ME!
-   * @param e
-   *          DOCUMENT ME!
+   * @param sm
+   * @param options
    */
-  public TreePanel(AlignmentPanel ap, String type, String pwtype)
+  public TreePanel(AlignmentPanel ap, String type, ScoreModelI sm,
+          SimilarityParamsI options)
   {
     super();
-    initTreePanel(ap, type, pwtype, null, null);
+    this.similarityParams = options;
+    initTreePanel(ap, type, sm, null, null);
 
     // We know this tree has distances. JBPNote TODO: prolly should add this as
     // a userdefined default
     // showDistances(true);
   }
 
-  public TreePanel(AlignmentPanel av, String type, String pwtype,
-          NewickFile newtree, AlignmentView inputData)
+  public TreePanel(AlignmentPanel alignPanel, NewickFile newtree,
+          String theTitle, AlignmentView inputData)
   {
     super();
-    initTreePanel(av, type, pwtype, newtree, inputData);
+    this.treeTitle = theTitle;
+    initTreePanel(alignPanel, null, null, newtree, inputData);
   }
 
   public AlignmentI getAlignment()
@@ -124,13 +126,13 @@ public class TreePanel extends GTreePanel
     return treeCanvas.av;
   }
 
-  void initTreePanel(AlignmentPanel ap, String type, String pwtype,
+  void initTreePanel(AlignmentPanel ap, String type, ScoreModelI sm,
           NewickFile newTree, AlignmentView inputData)
   {
 
     av = ap.av;
-    this.type = type;
-    this.pwtype = pwtype;
+    this.treeType = type;
+    this.scoreModel = sm;
 
     treeCanvas = new TreeCanvas(this, ap, scrollPane);
     scrollPane.setViewportView(treeCanvas);
@@ -168,11 +170,7 @@ public class TreePanel extends GTreePanel
       }
     });
 
-    TreeLoader tl = new TreeLoader(newTree);
-    if (inputData != null)
-    {
-      tl.odata = inputData;
-    }
+    TreeLoader tl = new TreeLoader(newTree, inputData);
     tl.start();
 
   }
@@ -244,19 +242,21 @@ public class TreePanel extends GTreePanel
 
   class TreeLoader extends Thread
   {
-    NewickFile newtree;
+    private NewickFile newtree;
 
-    jalview.datamodel.AlignmentView odata = null;
+    private AlignmentView odata = null;
 
-    public TreeLoader(NewickFile newtree)
+    public TreeLoader(NewickFile newickFile, AlignmentView inputData)
     {
-      this.newtree = newtree;
-      if (newtree != null)
+      this.newtree = newickFile;
+      this.odata = inputData;
+
+      if (newickFile != null)
       {
         // Must be outside run(), as Jalview2XML tries to
         // update distance/bootstrap visibility at the same time
-        showBootstrap(newtree.HasBootstrap());
-        showDistances(newtree.HasDistances());
+        showBootstrap(newickFile.HasBootstrap());
+        showDistances(newickFile.HasDistances());
       }
     }
 
@@ -266,43 +266,20 @@ public class TreePanel extends GTreePanel
 
       if (newtree != null)
       {
-        if (odata == null)
+        tree = new TreeModel(av.getAlignment().getSequencesArray(), odata,
+                newtree);
+        if (tree.getOriginalData() == null)
         {
-          tree = new NJTree(av.getAlignment().getSequencesArray(), newtree);
-        }
-        else
-        {
-          tree = new NJTree(av.getAlignment().getSequencesArray(), odata,
-                  newtree);
-        }
-        if (!tree.hasOriginalSequenceData())
-        {
-          allowOriginalSeqData(false);
+          originalSeqData.setVisible(false);
         }
       }
       else
       {
-        int start, end;
-        SequenceI[] seqs;
-        boolean selview = av.getSelectionGroup() != null
-                && av.getSelectionGroup().getSize() > 1;
-        AlignmentView seqStrings = av.getAlignmentView(selview);
-        if (!selview)
-        {
-          start = 0;
-          end = av.getAlignment().getWidth();
-          seqs = av.getAlignment().getSequencesArray();
-        }
-        else
-        {
-          start = av.getSelectionGroup().getStartRes();
-          end = av.getSelectionGroup().getEndRes() + 1;
-          seqs = av.getSelectionGroup().getSequencesInOrder(
-                  av.getAlignment());
-        }
-        ScoreModelI sm = ScoreModels.getInstance().forName(pwtype);
-        sm = configureScoreModel(sm);
-        tree = new NJTree(seqs, seqStrings, type, pwtype, sm, start, end);
+        ScoreModelI sm = configureScoreModel();
+        TreeBuilder njtree = treeType.equals(TreeBuilder.NEIGHBOUR_JOINING) ? new NJTree(
+                av, sm, similarityParams) : new AverageDistanceTree(av, sm,
+                similarityParams);
+        tree = new TreeModel(njtree);
         showDistances(true);
       }
 
@@ -336,17 +313,12 @@ public class TreePanel extends GTreePanel
     treeCanvas.setMarkPlaceholders(b);
   }
 
-  private void allowOriginalSeqData(boolean b)
-  {
-    originalSeqData.setVisible(b);
-  }
-
   /**
    * DOCUMENT ME!
    * 
    * @return DOCUMENT ME!
    */
-  public NJTree getTree()
+  public TreeModel getTree()
   {
     return tree;
   }
@@ -362,7 +334,7 @@ public class TreePanel extends GTreePanel
   {
     CutAndPasteTransfer cap = new CutAndPasteTransfer();
 
-    String newTitle = getPanelTitle(type, pwtype);
+    String newTitle = getPanelTitle();
 
     NewickFile fout = new NewickFile(tree.getTopNode());
     try
@@ -435,7 +407,8 @@ public class TreePanel extends GTreePanel
   @Override
   public void originalSeqData_actionPerformed(ActionEvent e)
   {
-    if (!tree.hasOriginalSequenceData())
+    AlignmentView originalData = tree.getOriginalData();
+    if (originalData == null)
     {
       jalview.bin.Cache.log
               .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
@@ -457,8 +430,9 @@ public class TreePanel extends GTreePanel
     } catch (Exception ex)
     {
     }
-    ;
-    Object[] alAndColsel = tree.seqData.getAlignmentAndColumnSelection(gc);
+
+    Object[] alAndColsel = originalData
+            .getAlignmentAndColumnSelection(gc);
 
     if (alAndColsel != null && alAndColsel[0] != null)
     {
@@ -575,11 +549,11 @@ public class TreePanel extends GTreePanel
 
   public CommandI sortAlignmentIn(AlignmentPanel ap)
   {
-    AlignmentViewport av = ap.av;
-    SequenceI[] oldOrder = av.getAlignment().getSequencesArray();
-    AlignmentSorter.sortByTree(av.getAlignment(), tree);
+    AlignmentViewport viewport = ap.av;
+    SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
+    AlignmentSorter.sortByTree(viewport.getAlignment(), tree);
     CommandI undo;
-    undo = new OrderCommand("Tree Sort", oldOrder, av.getAlignment());
+    undo = new OrderCommand("Tree Sort", oldOrder, viewport.getAlignment());
 
     ap.paintAlignment(true);
     return undo;
@@ -607,11 +581,11 @@ public class TreePanel extends GTreePanel
     return treeCanvas.font;
   }
 
-  public void setTreeFont(Font font)
+  public void setTreeFont(Font f)
   {
     if (treeCanvas != null)
     {
-      treeCanvas.setFont(font);
+      treeCanvas.setFont(f);
     }
   }
 
@@ -834,8 +808,8 @@ public class TreePanel extends GTreePanel
           }
           if (newname != null)
           {
-            String oldname = ((SequenceNode) node).getName();
-            // TODO : save in the undo object for this modification.
+            // String oldname = ((SequenceNode) node).getName();
+            // TODO : save oldname in the undo object for this modification.
             ((SequenceNode) node).setName(newname);
           }
         }
@@ -849,17 +823,13 @@ public class TreePanel extends GTreePanel
    * Neighbour Joining Using BLOSUM62
    * <p>
    * For a tree loaded from file, just uses the file name
-   * 
-   * @param treeType
-   *          NJ or AV or FromFile
-   * @param modelOrFileName
    * @return
    */
-  public static String getPanelTitle(String treeType, String modelOrFileName)
+  public String getPanelTitle()
   {
-    if (NJTree.FROM_FILE.equals(treeType))
+    if (treeTitle != null)
     {
-      return modelOrFileName;
+      return treeTitle;
     }
 
     /*
@@ -869,10 +839,10 @@ public class TreePanel extends GTreePanel
             + treeType.toLowerCase());
 
     /*
-     * i18n description (if any) of score model used
+     * i18n description (if available) of score model used
      */
     String smn = MessageManager.getStringOrReturn("label.score_model_",
-            modelOrFileName);
+            scoreModel.getName());
 
     /*
      * put them together as <method> Using <model>
@@ -884,25 +854,25 @@ public class TreePanel extends GTreePanel
 
   /**
    * If the score model is one that requires to get state data from the current
-   * view, allow it to do so
+   * view, create and configure a new instance of it
    * 
-   * @param sm
    * @return
    */
-  protected ScoreModelI configureScoreModel(ScoreModelI sm)
+  protected ScoreModelI configureScoreModel()
   {
-    if (sm instanceof ViewBasedAnalysisI)
+    if (scoreModel instanceof ViewBasedAnalysisI)
     {
       try
       {
-        sm = sm.getClass().newInstance();
-        ((ViewBasedAnalysisI) sm).configureFromAlignmentView(treeCanvas.ap);
+        scoreModel = scoreModel.getClass().newInstance();
+        ((ViewBasedAnalysisI) scoreModel)
+                .configureFromAlignmentView(treeCanvas.ap);
       } catch (Exception q)
       {
         Cache.log.error("Couldn't create a scoremodel instance for "
-                + sm.getName());
+                + scoreModel.getName());
       }
     }
-    return sm;
+    return scoreModel;
   }
 }