JAL-2799 ALL trees are now loaded in as separate Aptx instances
authorkjvdheide <kjvanderheide@dundee.ac.uk>
Fri, 24 Nov 2017 19:12:41 +0000 (19:12 +0000)
committerkjvdheide <kjvanderheide@dundee.ac.uk>
Fri, 24 Nov 2017 19:12:41 +0000 (19:12 +0000)
src/jalview/ext/archaeopteryx/AptxInit.java
src/jalview/ext/forester/io/TreeParser.java
src/jalview/gui/AlignFrame.java

index e58a38d..7dbddad 100644 (file)
@@ -5,9 +5,12 @@ import jalview.datamodel.SequenceI;
 import jalview.ext.treeviewer.ExternalTreeBuilderI;
 import jalview.ext.treeviewer.ExternalTreeViewerBindingI;
 import jalview.gui.Desktop;
+import jalview.gui.JvOptionPane;
+import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
 
 import java.awt.Dimension;
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.net.URL;
@@ -17,9 +20,14 @@ import org.forester.archaeopteryx.AptxUtil;
 import org.forester.archaeopteryx.Archaeopteryx;
 import org.forester.archaeopteryx.Configuration;
 import org.forester.archaeopteryx.MainFrame;
+import org.forester.io.parsers.PhylogenyParser;
+import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
 import org.forester.io.parsers.nhx.NHXParser;
+import org.forester.io.parsers.util.ParserUtils;
 import org.forester.phylogeny.Phylogeny;
+import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
+import org.forester.util.ForesterUtil;
 
 /**
  * Static class for creating Archaeopteryx tree viewer instances from calculated
@@ -58,8 +66,7 @@ public final class AptxInit
    */
   public static MainFrame createUnboundInstance(final Phylogeny aptxTree)
   {
-    Phylogeny[] aptxTrees = { aptxTree };
-    return createAptxFrame(aptxTrees);
+    return createAptxFrame(aptxTree);
   }
 
   // public static MainFrame createInstance(final Phylogeny[] aptxTrees,
@@ -85,10 +92,8 @@ public final class AptxInit
             calculatedTree);
 
     Phylogeny aptxTree = aptxTreeBuilder.buildTree();
-    Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
-                                          // several trees simultaneously
 
-    MainFrame aptxApp = createAptxFrame(aptxTrees);
+    MainFrame aptxApp = createAptxFrame(aptxTree);
             
     bindNodesToJalviewSequences(aptxApp, calculatedTree.getAvport(),
             aptxTreeBuilder.getAlignmentBoundNodes(),
@@ -98,50 +103,95 @@ public final class AptxInit
 
   }
 
-  public static MainFrame createInstanceFromFile(String filePath,
+  /**
+   * Refactored from Archaeopteryx.main
+   * 
+   * @param filePath
+   * @param viewport
+   * @return
+   * @throws IOException
+   * @throws FileNotFoundException
+   */
+  public static MainFrame[] createInstancesFromFile(String filePath,
           AlignmentViewport viewport)
+          throws FileNotFoundException, IOException
   {
-    String[] AptxArgs = new String[] { "-c",
-        APTX_CONFIG.getConfigFilename(), filePath };
-    MainFrame aptxApp = Archaeopteryx.main(AptxArgs);
-
-    int tabCount = aptxApp.getMainPanel().getTabbedPane().getTabCount();
-
-    for (int i = 0; i < tabCount; i++)
+    File treeFile = new File(filePath);
+    final String err = ForesterUtil.isReadableFile(treeFile);
+    if (!ForesterUtil.isEmpty(err))
     {
-      // roundabout way to select each tree because getComponentAt(i) requires
-      // casting to TreePanel which doesn't work
-      aptxApp.getMainPanel().getTabbedPane().setSelectedIndex(i);
-      Phylogeny tree = aptxApp.getMainPanel().getCurrentTreePanel()
-              .getPhylogeny();
-
-    LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
-            viewport.getAlignment().getSequencesArray(),
-              tree);
+      JvOptionPane.showMessageDialog(Desktop.desktop, err,
+              MessageManager.getString("label.problem_reading_tree_file"),
+              JvOptionPane.WARNING_MESSAGE);
+    }
+    boolean nhx_or_nexus = false;
+    final PhylogenyParser p = ParserUtils.createParserDependingOnFileType(
+            treeFile, VALIDATE_PHYLOXML_XSD);
+    if (p instanceof NHXParser)
+    {
+      nhx_or_nexus = true;
+      final NHXParser nhx = (NHXParser) p;
+      nhx.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
+      nhx.setIgnoreQuotes(false);
+      nhx.setTaxonomyExtraction(TAXONOMY_EXTRACTION);
+    }
+    else if (p instanceof NexusPhylogeniesParser)
+    {
+      nhx_or_nexus = true;
+      final NexusPhylogeniesParser nex = (NexusPhylogeniesParser) p;
+      nex.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
+      nex.setIgnoreQuotes(false);
+    }
+//    else if (p instanceof PhyloXmlParser)
+//    {
+//      MainFrameApplication.warnIfNotPhyloXmlValidation(APTX_CONFIG);
+//    }
+    Phylogeny[] phylogenies = PhylogenyMethods.readPhylogenies(p, treeFile);
+    MainFrame[] aptxFrames = new MainFrame[phylogenies.length];
+    String treeTitle = treeFile.getName();
+
+    for (int i = 0; i < phylogenies.length; i++)
+      {
+      Phylogeny tree = phylogenies[i];
+      if (nhx_or_nexus && INTERNAL_NUMBERS_AS_CONFIDENCE)
+      {
+        PhylogenyMethods.transferInternalNodeNamesToConfidence(tree, "");
+      }
+
+      MainFrame aptxApp = Archaeopteryx.createApplication(tree, APTX_CONFIG,
+              treeTitle);
+      LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
+              viewport.getAlignment().getSequencesArray(), tree);
 
-    bindAptxNodes.associateLeavesToSequences();
+      bindAptxNodes.associateLeavesToSequences();
+      bindNodesToJalviewSequences(aptxApp, viewport,
+              bindAptxNodes.getAlignmentWithNodes(),
+              bindAptxNodes.getNodesWithAlignment());
+      bindFrameToJalview(aptxApp);
+      aptxFrames[i] = aptxApp;
+    }
 
-    bindNodesToJalviewSequences(aptxApp, viewport,
-            bindAptxNodes.getAlignmentWithNodes(),
-            bindAptxNodes.getNodesWithAlignment());
+    return aptxFrames;
     }
-    return bindFrameToJalview(aptxApp);
-  }
 
-  public static MainFrame createInstanceFromUrl(URL treeUrl,
+
+  public static MainFrame[] createInstancesFromUrl(URL treeUrl,
           AlignmentViewport viewport)
           throws FileNotFoundException, IOException, RuntimeException
   {
+    
     String treeTitle = treeUrl.getFile();
     Phylogeny[] trees = AptxUtil.readPhylogeniesFromUrl(treeUrl,
             VALIDATE_PHYLOXML_XSD,
              REPLACE_NHX_UNDERSCORES, INTERNAL_NUMBERS_AS_CONFIDENCE,
             TAXONOMY_EXTRACTION, MIDPOINT_REROOT);
-    MainFrame aptxApp = Archaeopteryx.createApplication(trees, APTX_CONFIG,
-            treeTitle);
 
-    for (Phylogeny tree : trees)
+    MainFrame[] aptxFrames = new MainFrame[trees.length];
+    for (int i = 0; i < trees.length; i++)
     {
+      Phylogeny tree = trees[i];
+      MainFrame aptxApp = Archaeopteryx.createApplication(tree, APTX_CONFIG,
+              treeTitle);
       LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
               viewport.getAlignment().getSequencesArray(), tree);
 
@@ -149,10 +199,11 @@ public final class AptxInit
       bindNodesToJalviewSequences(aptxApp, viewport,
               bindAptxNodes.getAlignmentWithNodes(),
               bindAptxNodes.getNodesWithAlignment());
-
+      bindFrameToJalview(aptxApp);
+      aptxFrames[i] = aptxApp;
     }
 
-    return bindFrameToJalview(aptxApp);
+    return aptxFrames;
 
   }
 
@@ -160,9 +211,9 @@ public final class AptxInit
 
 
   public static MainFrame createAptxFrame(
-          final Phylogeny[] aptxTrees)
+          final Phylogeny aptxTree)
   {
-    MainFrame aptxApp = Archaeopteryx.createApplication(aptxTrees,
+    MainFrame aptxApp = Archaeopteryx.createApplication(aptxTree,
             APTX_CONFIG, null);
     return aptxApp;
   }
index 43fd1fa..e11591d 100644 (file)
@@ -12,8 +12,6 @@ import jalview.util.MessageManager;
 import java.io.File;
 import java.io.IOException;
 
-import org.forester.util.ForesterUtil;
-
 public class TreeParser // implements ExternalTreeParserI<MainFrame>
 {
   private final String filePath;
@@ -22,14 +20,7 @@ public class TreeParser // implements ExternalTreeParserI<MainFrame>
 
   public TreeParser(final String treeFilePath)
   {
-    final String possibleError = ForesterUtil.isReadableFile(treeFilePath);
-    if (possibleError != null)
-    {
-      JvOptionPane.showMessageDialog(Desktop.desktop, possibleError,
-              MessageManager.getString("label.problem_reading_tree_file"),
-              JvOptionPane.WARNING_MESSAGE);
 
-    }
     filePath = treeFilePath;
     file = new File(filePath);
 
@@ -37,14 +28,6 @@ public class TreeParser // implements ExternalTreeParserI<MainFrame>
 
   public TreeParser(final File treeFile) throws IOException
   {
-    final String possibleError = ForesterUtil.isReadableFile(treeFile);
-    if (possibleError != null)
-    {
-      JvOptionPane.showMessageDialog(Desktop.desktop, possibleError,
-              MessageManager.getString("label.problem_reading_tree_file"),
-              JvOptionPane.WARNING_MESSAGE);
-
-    }
     file = treeFile;
     filePath = file.getCanonicalPath();
   }
@@ -55,7 +38,7 @@ public class TreeParser // implements ExternalTreeParserI<MainFrame>
     NewickFile fin = null; // old tree
       try
       {
-        AptxInit.createInstanceFromFile(filePath, viewport);
+        AptxInit.createInstancesFromFile(filePath, viewport);
 
         fin = new NewickFile(filePath, DataSourceType.FILE);
         viewport.setCurrentTree(viewport.getAlignPanel().alignFrame
index deb8d11..128eccc 100644 (file)
@@ -4012,7 +4012,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
         format = new IdentifyFile().identify(urlString, DataSourceType.URL);
         // add actual use for the format identification (jalview .jar files)
         treeUrl = new URL(urlString);
-        AptxInit.createInstanceFromUrl(treeUrl, viewport);
+        AptxInit.createInstancesFromUrl(treeUrl, viewport);
 
       } catch (IOException | RuntimeException e)
       {