JAL-4435 WIP - to squash
authorJim Procter <jprocter@dundee.ac.uk>
Tue, 2 Jul 2024 11:31:35 +0000 (12:31 +0100)
committerJim Procter <jprocter@dundee.ac.uk>
Tue, 2 Jul 2024 11:31:35 +0000 (12:31 +0100)
src/jalview/analysis/TreeModel.java
src/jalview/gui/AlignFrame.java
src/jalview/gui/TreePanel.java
src/jalview/project/Jalview2XML.java

index 1068042..e29eb95 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.analysis;
 
+import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.BinaryNode;
 import jalview.datamodel.NodeTransformI;
@@ -78,13 +79,21 @@ public class TreeModel
    *          NewickFile
    */
   public TreeModel(SequenceI[] seqs, AlignmentView odata,
-          NewickFile treefile)
+          NewickFile treefile, AlignmentAnnotation[] leafAnnotations)
   {
     this(seqs, treefile.getTree(), treefile.HasDistances(),
             treefile.HasBootstrap(), treefile.HasRootDistance());
     seqData = odata;
-
-    associateLeavesToSequences(seqs);
+    if (leafAnnotations != null)
+    {
+      // leaf names are seuqence ID + annotation for display
+      // leaf links are annotationId <> annotation object for import from project
+      // --> need to resolve before passing for display
+      // ==> do leafs need to be renamed after import ?
+      // ==> implies the 'nodeLabel' business needs to be callable independently of score function
+    } else {
+      associateLeavesToSequences(seqs);
+    }
   }
 
   /**
index 766b35e..d4de475 100644 (file)
@@ -4198,6 +4198,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     return seqs;
   }
 
+  //// ***************************************************
+  //// TODO REFACTOR TREE I/O TO CONTROLLER/SEPARATE CLASS
+  //// ***************************************************
+  
   /**
    * DOCUMENT ME!
    * 
@@ -4304,6 +4308,36 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
     return tp;
   }
+  public TreePanel showNewickTreeForAnnotation(NewickFile nf, String treeTitle,
+          AlignmentView input, int w, int h, int x, int y)
+  {
+    TreePanel tp = null;
+
+    try
+    {
+      nf.parse();
+
+      if (nf.getTree() != null)
+      {
+        tp = TreePanel.newTreeForAnnotations(alignPanel, nf, treeTitle, input);
+        // TODO - refactor layout code to one place for tree view methods
+        tp.setSize(w, h);
+
+        if (x > 0 && y > 0)
+        {
+          tp.setLocation(x, y);
+        }
+
+        Desktop.addInternalFrame(tp, treeTitle, w, h);
+      }
+    } catch (Exception ex)
+    {
+      ex.printStackTrace();
+    }
+
+    return tp;
+  }
+
 
   public void showContactMapTree(AlignmentAnnotation aa, ContactMatrixI cm)
   {
@@ -4355,6 +4389,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
     return null;
   }
+  //// ***************************************************
+  //// TODO REFACTOR TREE I/O TO CONTROLLER/SEPARATE CLASS - END OF METHODS
+  //// ***************************************************
 
   private boolean buildingMenu = false;
 
index 24a5547..c80bf3a 100755 (executable)
@@ -119,7 +119,7 @@ public class TreePanel extends GTreePanel
     super();
     this.setFrameIcon(null);
     this.similarityParams = options;
-    initTreePanel(ap, type, modelName, null, null);
+    initTreePanel(ap, type, modelName, null, null, null);
 
     // We know this tree has distances. JBPNote TODO: prolly should add this as
     // a userdefined default
@@ -129,10 +129,22 @@ public class TreePanel extends GTreePanel
   public TreePanel(AlignmentPanel alignPanel, NewickFile newtree,
           String theTitle, AlignmentView inputData)
   {
+    this(alignPanel, newtree, theTitle, inputData, null,null);
+  }
+
+  /**
+   * when true, leaf labels are annotations
+   */
+  boolean forAnnotation = false;
+
+  public TreePanel(AlignmentPanel alignPanel, NewickFile newtree,
+          String theTitle, AlignmentView inputData, AlignmentAnnotation[] leafAnnotations, String subTitle)
+  {
     super();
+    this.forAnnotation=leafAnnotations!=null && leafAnnotations.length>0;
     this.setFrameIcon(null);
     this.treeTitle = theTitle;
-    initTreePanel(alignPanel, null, null, newtree, inputData);
+    initTreePanel(alignPanel, null, null, newtree, inputData, null);
   }
 
   /**
@@ -151,7 +163,7 @@ public class TreePanel extends GTreePanel
     assocAnnotation = aa;
     this.setFrameIcon(null);
     this.treeTitle = title;
-    initTreePanel(alignPanel, null, null, fin, null);
+    initTreePanel(alignPanel, null, null, fin, null, null);
   }
 
   boolean columnWise = false;
@@ -179,8 +191,19 @@ public class TreePanel extends GTreePanel
     return getTreeCanvas().getViewport();
   }
 
+  private void addSubtitlePanel(String subTitle)
+  {
+    subtitleLabel = new JLabel(subTitle, SwingConstants.LEFT);
+
+    JPanel panel = new JPanel(new BorderLayout());
+    panel.add(subtitleLabel, BorderLayout.NORTH);
+    panel.add(scrollPane, BorderLayout.CENTER);
+
+    this.add(panel);
+  }
+
   void initTreePanel(AlignmentPanel ap, String type, String modelName,
-          NewickFile newTree, AlignmentView inputData)
+          NewickFile newTree, AlignmentView inputData, AlignmentAnnotation[] leafAnnotations)
   {
 
     av = ap.av;
@@ -190,19 +213,17 @@ public class TreePanel extends GTreePanel
     treeCanvas = new TreeCanvas(this, ap, scrollPane);
     scrollPane.setViewportView(treeCanvas);
     
-    if(this.similarityParams!=null)
-      if(this.similarityParams.getSecondaryStructureSource()!=null ) {
-    
-      subtitleLabel = new JLabel(" Secondary Structure Provider : " 
-                + this.similarityParams.getSecondaryStructureSource(), SwingConstants.LEFT);
-  
-      JPanel panel = new JPanel(new BorderLayout());
-      panel.add(subtitleLabel, BorderLayout.NORTH);
-      panel.add(scrollPane, BorderLayout.CENTER);
-  
-      this.add(panel);
-    }
+    if (similarityParams != null
+            && similarityParams.getSecondaryStructureSource() != null)
+    {
 
+      addSubtitlePanel(" Secondary Structure Provider : "
+              + similarityParams.getSecondaryStructureSource());
+    }
+    if (leafAnnotations!=null)
+    {
+      forAnnotation=true;
+    }
     if (columnWise)
     {
       bootstrapMenu.setVisible(false);
@@ -251,7 +272,7 @@ public class TreePanel extends GTreePanel
       }
     });
 
-    TreeLoader tl = new TreeLoader(newTree, inputData);
+    TreeLoader tl = new TreeLoader(newTree, inputData, leafAnnotations);
     tl.start();
 
   }
@@ -377,11 +398,13 @@ public class TreePanel extends GTreePanel
 
     private AlignmentView odata = null;
 
-    public TreeLoader(NewickFile newickFile, AlignmentView inputData)
+    private AlignmentAnnotation[] leafAnnotations;
+    public TreeLoader(NewickFile newickFile, AlignmentView inputData, AlignmentAnnotation[] leafAnnotations)
     {
       this.newtree = newickFile;
       this.odata = inputData;
-
+      this.leafAnnotations = leafAnnotations;
+      
       if (newickFile != null)
       {
         // Must be outside run(), as Jalview2XML tries to
@@ -398,7 +421,7 @@ public class TreePanel extends GTreePanel
       if (newtree != null)
       {
         tree = new TreeModel(av.getAlignment().getSequencesArray(), odata,
-                newtree);
+                newtree, leafAnnotations);
         if (tree.getOriginalData() == null)
         {
           originalSeqData.setVisible(false);
@@ -939,4 +962,12 @@ public class TreePanel extends GTreePanel
   {
     return treeCanvas;
   }
+
+  public static TreePanel newTreeForAnnotations(AlignmentPanel alignPanel,
+          NewickFile nf, String treeTitle2, AlignmentView input)
+  {
+    // TODO - recover subpanel name
+    TreePanel tp = new TreePanel(alignPanel, nf, treeTitle2, input, null, "Annotation Tree");
+    return tp;
+  }
 }
index e620c1c..d8b688a 100644 (file)
@@ -4675,10 +4675,21 @@ public class Jalview2XML
           }
           else
           {
-            tp = af.showNewickTree(new NewickFile(tree.getNewick()),
+            if (false)//tree.isAnnotationAssociated())
+            {
+              // TODO - resolve annotation IDs against newick tree labels
+              // then fix up labels so they are human readable
+              
+              tp = af.showNewickTreeForAnnotation(new NewickFile(tree.getNewick()),
+                      tree.getTitle(), null, safeInt(tree.getWidth()),
+                      safeInt(tree.getHeight()), safeInt(tree.getXpos()),
+                      safeInt(tree.getYpos()));
+            } else {
+              tp = af.showNewickTree(new NewickFile(tree.getNewick()),
                     tree.getTitle(), safeInt(tree.getWidth()),
                     safeInt(tree.getHeight()), safeInt(tree.getXpos()),
                     safeInt(tree.getYpos()));
+            }
           }
           if (tree.getId() != null)
           {