Merge branch 'develop' into features/JAL-2446NCList
[jalview.git] / src / jalview / gui / TreePanel.java
index 857e77c..32c5702 100755 (executable)
 package jalview.gui;
 
 import jalview.analysis.AlignmentSorter;
+import jalview.analysis.AverageDistanceTree;
 import jalview.analysis.NJTree;
+import jalview.analysis.TreeBuilder;
+import jalview.analysis.TreeModel;
 import jalview.analysis.scoremodels.ScoreModels;
-import jalview.analysis.scoremodels.SimilarityParams;
 import jalview.api.analysis.ScoreModelI;
-import jalview.api.analysis.ViewBasedAnalysisI;
+import jalview.api.analysis.SimilarityParamsI;
 import jalview.bin.Cache;
 import jalview.commands.CommandI;
 import jalview.commands.OrderCommand;
@@ -33,8 +35,8 @@ import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.BinaryNode;
-import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.NodeTransformI;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
@@ -72,47 +74,46 @@ import org.jibble.epsgraphics.EpsGraphics2D;
  */
 public class TreePanel extends GTreePanel
 {
-  String type;
+  String treeType;
 
-  String pwtype;
+  String scoreModelName; // 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 modelName
+   * @param options
    */
-  public TreePanel(AlignmentPanel ap, String type, String pwtype)
+  public TreePanel(AlignmentPanel ap, String type, String modelName,
+          SimilarityParamsI options)
   {
     super();
-    initTreePanel(ap, type, pwtype, null, null);
+    this.similarityParams = options;
+    initTreePanel(ap, type, modelName, 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()
@@ -125,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, String modelName,
           NewickFile newTree, AlignmentView inputData)
   {
 
     av = ap.av;
-    this.type = type;
-    this.pwtype = pwtype;
+    this.treeType = type;
+    this.scoreModelName = modelName;
 
     treeCanvas = new TreeCanvas(this, ap, scrollPane);
     scrollPane.setViewportView(treeCanvas);
@@ -169,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();
 
   }
@@ -245,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());
       }
     }
 
@@ -267,42 +266,21 @@ public class TreePanel extends GTreePanel
 
       if (newtree != null)
       {
-        if (odata == null)
-        {
-          tree = new NJTree(av.getAlignment().getSequencesArray(), newtree);
-        }
-        else
-        {
-          tree = new NJTree(av.getAlignment().getSequencesArray(), odata,
-                  newtree);
-        }
-        if (!tree.hasOriginalSequenceData())
+        tree = new TreeModel(av.getAlignment().getSequencesArray(), odata,
+                newtree);
+        if (tree.getOriginalData() == null)
         {
-          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 = configureScoreModel(pwtype);
-        tree = new NJTree(av, type, sm, SimilarityParams.Jalview);
+        ScoreModelI sm = ScoreModels.getInstance().getScoreModel(
+                scoreModelName, treeCanvas.ap);
+        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 +314,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 +335,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 +408,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 +431,8 @@ public class TreePanel extends GTreePanel
     } catch (Exception ex)
     {
     }
-    ;
-    Object[] alAndColsel = tree.seqData.getAlignmentAndColumnSelection(gc);
+
+    Object[] alAndColsel = originalData.getAlignmentAndHiddenColumns(gc);
 
     if (alAndColsel != null && alAndColsel[0] != null)
     {
@@ -475,8 +449,8 @@ public class TreePanel extends GTreePanel
       if (true)
       {
         // make a new frame!
-        AlignFrame af = new AlignFrame(al,
-                (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
+        AlignFrame af = new AlignFrame(al, (HiddenColumns) alAndColsel[1],
+                AlignFrame.DEFAULT_WIDTH,
                 AlignFrame.DEFAULT_HEIGHT);
 
         // >>>This is a fix for the moment, until a better solution is
@@ -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);
     }
   }
 
@@ -815,27 +789,25 @@ public class TreePanel extends GTreePanel
             }
             if (newname == null)
             {
-              SequenceFeature sf[] = sq.getSequenceFeatures();
-              for (int i = 0; sf != null && i < sf.length; i++)
+              List<SequenceFeature> features = sq.getFeatures()
+                      .getPositionalFeatures(labelClass);
+              for (SequenceFeature feature : features)
               {
-                if (sf[i].getType().equals(labelClass))
+                if (newname == null)
                 {
-                  if (newname == null)
-                  {
-                    newname = new String(sf[i].getDescription());
-                  }
-                  else
-                  {
-                    newname = newname + "; " + sf[i].getDescription();
-                  }
+                  newname = feature.getDescription();
+                }
+                else
+                {
+                  newname = newname + "; " + feature.getDescription();
                 }
               }
             }
           }
           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 +821,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 +837,9 @@ public class TreePanel extends GTreePanel
             + treeType.toLowerCase());
 
     /*
-     * i18n description (if any) of score model used
+     * short score model name (long description can be too long)
      */
-    String smn = MessageManager.getStringOrReturn("label.score_model_",
-            modelOrFileName);
+    String smn = scoreModelName;
 
     /*
      * put them together as <method> Using <model>
@@ -881,29 +848,4 @@ public class TreePanel extends GTreePanel
             treecalcnm, smn);
     return ttl;
   }
-
-  /**
-   * Gets the score model for the given name. If the score model is one that
-   * requires to get state data from the current view, allow it to do so
-   * 
-   * @param sm
-   * @return
-   */
-  protected ScoreModelI configureScoreModel(String modelName)
-  {
-    ScoreModelI sm = ScoreModels.getInstance().forName(modelName);
-    if (sm instanceof ViewBasedAnalysisI)
-    {
-      try
-      {
-        sm = sm.getClass().newInstance();
-        ((ViewBasedAnalysisI) sm).configureFromAlignmentView(treeCanvas.ap);
-      } catch (Exception q)
-      {
-        Cache.log.error("Couldn't create a scoremodel instance for "
-                + sm.getName());
-      }
-    }
-    return sm;
-  }
 }