JAL-161 JAL-4173 allow trees embedded as NH tags in stockholm to be loaded as trees...
authorJames Procter <j.procter@dundee.ac.uk>
Fri, 28 Apr 2023 15:49:27 +0000 (16:49 +0100)
committerJames Procter <j.procter@dundee.ac.uk>
Thu, 1 Jun 2023 14:51:14 +0000 (15:51 +0100)
src/jalview/io/AlignFile.java
src/jalview/io/AlignmentFileReaderI.java
src/jalview/io/FileLoader.java

index 1233940..1bea24b 100755 (executable)
@@ -433,12 +433,24 @@ public abstract class AlignFile extends FileParse
     newickStrings.addElement(new String[] { treeName, newickString });
   }
 
-  protected int getTreeCount()
+  @Override
+  public int getTreeCount()
   {
     return newickStrings == null ? 0 : newickStrings.size();
   }
 
   @Override
+  public boolean hasTrees()
+  {
+    return getTreeCount()>0;
+  }
+  
+  @Override
+  public List<String[]> getNewickTrees()
+  {
+    return newickStrings;
+  }
+  @Override
   public void addGroups(AlignmentI al)
   {
 
index c3e148d..2ea1fa1 100644 (file)
@@ -20,6 +20,9 @@
  */
 package jalview.io;
 
+import java.util.Arrays;
+import java.util.List;
+
 import jalview.api.AlignExportSettingsI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureSettingsModelI;
@@ -47,4 +50,16 @@ public interface AlignmentFileReaderI
 
   FeatureSettingsModelI getFeatureColourScheme();
 
+  default int getTreeCount() {
+    return 0;
+  }
+
+  default boolean hasTrees() {
+    return false;
+  }
+
+  default List<String[]> getNewickTrees() {
+    return Arrays.asList(new String[][] {});
+  }
+
 }
index 449c685..7b2ec80 100755 (executable)
@@ -32,6 +32,7 @@ import jalview.api.FeatureSettingsModelI;
 import jalview.api.FeaturesDisplayedI;
 import jalview.api.FeaturesSourceI;
 import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.bin.Jalview;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.HiddenColumns;
@@ -328,7 +329,6 @@ public class FileLoader implements Runnable
       }
       loadtime = -System.currentTimeMillis();
       AlignmentI al = null;
-
       if (FileFormat.Jalview.equals(format))
       {
         if (source != null)
@@ -344,19 +344,21 @@ public class FileLoader implements Runnable
       }
       else
       {
+        FormatAdapter fa = null; // populated for simple and complex flatfile formats - but not Jalview Projects
         String error = AppletFormatAdapter.getSupportedFormats();
         try
         {
           if (source != null)
           {
+            fa=new FormatAdapter();
             // read from the provided source
-            al = new FormatAdapter().readFromFile(source, format);
+            al = fa.readFromFile(source, format);
           }
           else
           {
 
             // open a new source and read from it
-            FormatAdapter fa = new FormatAdapter();
+            fa = new FormatAdapter();
             boolean downloadStructureFile = format.isStructureFile()
                     && protocol.equals(DataSourceType.URL);
             if (downloadStructureFile)
@@ -474,6 +476,33 @@ public class FileLoader implements Runnable
               alignFrame.getViewport()
                       .applyFeaturesStyle(proxyColourScheme);
             }
+            if (source.hasTrees())
+            {
+              for (String[] nhxtree : source.getNewickTrees())
+              {
+                NewickFile fin = null;
+                try
+                {
+                  fin = new NewickFile(new FileParse(nhxtree[1],
+                          DataSourceType.PASTE));
+                  if (fin.hasWarningMessage())
+                  {
+                    Console.warn(
+                            "Warnings when importing tree from file: "
+                                    + fin.getWarningMessage());
+                  }
+                  if (fin.isValid())// && fin.getTree() != null)
+                  {
+                    alignFrame.showNewickTree(fin, nhxtree[0]);
+                  } else {
+                    Console.warn("Ignoring tree "+nhxtree[0]+"\t"+nhxtree[1]);
+                  }
+                } catch (Throwable thr)
+                {
+                  Console.warn("Couldn't import tree from file", thr);
+                }
+              }
+            }
             alignFrame.setStatus(MessageManager.formatMessage(
                     "label.successfully_loaded_file", new String[]
                     { title }));