JAL-1641 round trip test/refactoring
[jalview.git] / src / jalview / io / AppletFormatAdapter.java
index 1633212..90400f4 100755 (executable)
@@ -20,7 +20,8 @@
  */
 package jalview.io;
 
-import jalview.api.AlignViewportI;
+import jalview.api.AlignExportSettingI;
+import jalview.api.AlignmentViewPanel;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
@@ -42,7 +43,7 @@ import java.util.List;
  */
 public class AppletFormatAdapter
 {
-  private AlignViewportI viewport;
+  private AlignmentViewPanel viewpanel;
 
   public static String FILE = "File";
 
@@ -76,6 +77,8 @@ public class AppletFormatAdapter
    */
   protected String newline = System.getProperty("line.separator");
 
+  private AlignExportSettingI exportSettings;
+
   /**
    * List of valid format strings used in the isValidFormat method
    */
@@ -137,9 +140,16 @@ public class AppletFormatAdapter
   {
   }
 
-  public AppletFormatAdapter(AlignViewportI viewport)
+  public AppletFormatAdapter(AlignmentViewPanel viewpanel)
   {
-    this.viewport = viewport;
+    this.viewpanel = viewpanel;
+  }
+
+  public AppletFormatAdapter(AlignmentViewPanel alignPanel,
+          AlignExportSettingI settings)
+  {
+    viewpanel = alignPanel;
+    exportSettings = settings;
   }
 
   /**
@@ -221,7 +231,7 @@ public class AppletFormatAdapter
    *
    * @return DOCUMENT ME!
    */
-  public Alignment readFile(String inFile, String type, String format)
+  public AlignmentI readFile(String inFile, String type, String format)
           throws java.io.IOException
   {
     // TODO: generalise mapping between format string and io. class instances
@@ -229,7 +239,6 @@ public class AppletFormatAdapter
     this.inFile = inFile;
     try
     {
-      Alignment al;
       if (format.equals("FASTA"))
       {
         alignFile = new FastaFile(inFile, type);
@@ -285,26 +294,10 @@ public class AppletFormatAdapter
       else if (format.equals(JSONFile.FILE_DESC))
       {
         alignFile = new JSONFile(inFile, type);
-        al = new Alignment(alignFile.getSeqsAsArray());
-        alignFile.addAnnotations(al);
-        ((JSONFile) alignFile).setViewport(viewport);
-        for (SequenceGroup sg : alignFile.getSeqGroups())
-        {
-          al.addGroup(sg);
-        }
-
-        return al;
       }
       else if (format.equals(HtmlFile.FILE_DESC))
       {
         alignFile = new HtmlFile(inFile, type);
-        al = new Alignment(alignFile.getSeqsAsArray());
-        alignFile.addAnnotations(al);
-        for (SequenceGroup sg : alignFile.getSeqGroups())
-        {
-          al.addGroup(sg);
-        }
-        return al;
       }
       else if (format.equals("RNAML"))
       {
@@ -314,12 +307,7 @@ public class AppletFormatAdapter
       {
         alignFile = new Gff3File(inFile, type);
       }
-
-      al = new Alignment(alignFile.getSeqsAsArray());
-
-      alignFile.addAnnotations(al);
-
-      return al;
+      return buildAlignmentFrom(alignFile);
     } catch (Exception e)
     {
       e.printStackTrace();
@@ -339,11 +327,7 @@ public class AppletFormatAdapter
         {
           // Possible sequence is just residues with no label
           alignFile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
-          Alignment al = new Alignment(alignFile.getSeqsAsArray());
-
-          alignFile.addSeqGroups(al);
-          alignFile.addAnnotations(al);
-          return al;
+          return buildAlignmentFrom(alignFile);
 
         } catch (Exception ex)
         {
@@ -382,7 +366,6 @@ public class AppletFormatAdapter
     String type = source.type;
     try
     {
-      Alignment al;
       if (format.equals("FASTA"))
       {
         alignFile = new FastaFile(source);
@@ -444,19 +427,14 @@ public class AppletFormatAdapter
       else if (format.equals(JSONFile.FILE_DESC))
       {
         alignFile = new JSONFile(source);
-        al = new Alignment(alignFile.getSeqsAsArray());
-        alignFile.addAnnotations(al);
-        alignFile.addSeqGroups(al);
-        return al;
       }
       else if (format.equals(HtmlFile.FILE_DESC))
       {
         alignFile = new HtmlFile(source);
       }
-      al = new Alignment(alignFile.getSeqsAsArray());
-      alignFile.addAnnotations(al);
 
-      return al;
+      return buildAlignmentFrom(alignFile);
+
     } catch (Exception e)
     {
       e.printStackTrace();
@@ -476,10 +454,7 @@ public class AppletFormatAdapter
         {
           // Possible sequence is just residues with no label
           alignFile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
-          Alignment al = new Alignment(alignFile.getSeqsAsArray());
-          alignFile.addAnnotations(al);
-          alignFile.addSeqGroups(al);
-          return al;
+          return buildAlignmentFrom(alignFile);
 
         } catch (Exception ex)
         {
@@ -499,6 +474,27 @@ public class AppletFormatAdapter
 
 
   /**
+   * boilerplate method to handle data from an AlignFile and construct a new
+   * alignment or import to an existing alignment
+   * 
+   * @param alignFile2
+   * @return AlignmentI instance ready to pass to a UI constructor
+   */
+  private AlignmentI buildAlignmentFrom(AlignFile alignFile2)
+  {
+    // Standard boilerplate for creating alignment from parser
+    // alignFile.configureForView(viewpanel);
+
+    AlignmentI al = new Alignment(alignFile.getSeqsAsArray());
+
+    alignFile.addAnnotations(al);
+
+    alignFile.addGroups(al);
+
+    return al;
+  }
+
+  /**
    * create an alignment flatfile from a Jalview alignment view
    * @param format
    * @param jvsuffix
@@ -507,13 +503,14 @@ public class AppletFormatAdapter
    * @return flatfile in a string
    */
   public String formatSequences(String format, boolean jvsuffix,
-          AlignViewportI av, boolean selectedOnly)
+          AlignmentViewPanel ap, boolean selectedOnly)
   {
 
-    AlignmentView selvew = av.getAlignmentView(selectedOnly, false);
-    AlignmentI aselview = selvew.getVisibleAlignment(av
+    AlignmentView selvew = ap.getAlignViewport().getAlignmentView(
+            selectedOnly, false);
+    AlignmentI aselview = selvew.getVisibleAlignment(ap.getAlignViewport()
             .getGapCharacter());
-    List<AlignmentAnnotation> ala = (av
+    List<AlignmentAnnotation> ala = (ap.getAlignViewport()
             .getVisibleAlignmentAnnotation(selectedOnly));
     if (ala != null)
     {
@@ -522,7 +519,7 @@ public class AppletFormatAdapter
         aselview.addAnnotation(aa);
       }
     }
-
+    viewpanel = ap;
     return formatSequences(format, aselview, jvsuffix);
   }
 
@@ -590,22 +587,6 @@ public class AppletFormatAdapter
        else if (format.equalsIgnoreCase(JSONFile.FILE_DESC))
        {
         afile = new JSONFile();
-        afile.setViewport(viewport);
-        // Add groups to AlignFile
-        afile.seqGroups = alignment.getGroups();
-
-        // Add non auto calculated annotation to AlignFile
-        for (AlignmentAnnotation annot : alignment.getAlignmentAnnotation())
-        {
-          if (annot != null && !annot.autoCalculated)
-          {
-            if (annot.label.equals("PDB.CATempFactor"))
-            {
-              continue;
-            }
-            afile.annotations.add(annot);
-          }
-        }
        }
       else if (format.equalsIgnoreCase("RNAML"))
       {
@@ -616,11 +597,23 @@ public class AppletFormatAdapter
       {
         throw new Exception(MessageManager.getString("error.implementation_error_unknown_file_format_string"));
       }
+
       afile.setNewlineString(newline);
       afile.addJVSuffix(jvsuffix);
+      afile.setExportSettings(exportSettings);
+      afile.configureForView(viewpanel);
 
-      afile.setSeqs(alignment.getSequencesArray());
-
+      // check whether we were given a specific alignment to export, rather than
+      // the one in the viewpanel
+      if (viewpanel == null || viewpanel.getAlignment() == null
+              || viewpanel.getAlignment() != alignment)
+      {
+        afile.setSeqs(alignment.getSequencesArray());
+      }
+      else
+      {
+        afile.setSeqs(viewpanel.getAlignment().getSequencesArray());
+      }
 
       String afileresp = afile.print();
       if (afile.hasWarningMessage())
@@ -667,7 +660,7 @@ public class AppletFormatAdapter
           System.gc();
           long memf = -r.totalMemory() + r.freeMemory();
           long t1 = -System.currentTimeMillis();
-          Alignment al = afa.readFile(args[i], FILE,
+          AlignmentI al = afa.readFile(args[i], FILE,
                   new IdentifyFile().Identify(args[i], FILE));
           t1 += System.currentTimeMillis();
           System.gc();