JAL-1780 JAL-653 Format/AppletFormat import and export pipeline regularised, uses...
[jalview.git] / src / jalview / io / AppletFormatAdapter.java
index 7810771..b235bcc 100755 (executable)
  */
 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;
 import jalview.datamodel.AlignmentView;
+import jalview.gui.AlignmentPanel;
 import jalview.util.MessageManager;
 
 import java.io.File;
@@ -42,7 +44,7 @@ import java.util.List;
  */
 public class AppletFormatAdapter
 {
-  private AlignViewportI viewport;
+  private AlignmentViewPanel viewpanel;
 
   public static String FILE = "File";
 
@@ -76,6 +78,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 +141,16 @@ public class AppletFormatAdapter
   {
   }
 
-  public AppletFormatAdapter(AlignViewportI viewport)
+  public AppletFormatAdapter(AlignmentViewPanel viewpanel)
+  {
+    this.viewpanel = viewpanel;
+  }
+
+  public AppletFormatAdapter(AlignmentPanel alignPanel,
+          AlignExportSettingI settings)
   {
-    this.viewport = viewport;
+    viewpanel = alignPanel;
+    exportSettings = settings;
   }
 
   /**
@@ -221,7 +232,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 +240,6 @@ public class AppletFormatAdapter
     this.inFile = inFile;
     try
     {
-      Alignment al;
       if (format.equals("FASTA"))
       {
         alignFile = new FastaFile(inFile, type);
@@ -298,16 +308,7 @@ public class AppletFormatAdapter
       {
         alignFile = new Gff3File(inFile, type);
       }
-      // Standard boilerplate for creating alignment from parser
-      alignFile.setViewport(viewport);
-      
-      al = new Alignment(alignFile.getSeqsAsArray());
-
-      alignFile.addAnnotations(al);
-
-      alignFile.addGroups(al);
-
-      return al;
+      return buildAlignmentFrom(alignFile);
     } catch (Exception e)
     {
       e.printStackTrace();
@@ -327,11 +328,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)
         {
@@ -370,7 +367,6 @@ public class AppletFormatAdapter
     String type = source.type;
     try
     {
-      Alignment al;
       if (format.equals("FASTA"))
       {
         alignFile = new FastaFile(source);
@@ -432,19 +428,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();
@@ -464,10 +455,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)
         {
@@ -487,6 +475,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
@@ -495,13 +504,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)
     {
@@ -578,22 +588,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"))
       {
@@ -607,8 +601,21 @@ public class AppletFormatAdapter
       afile.setNewlineString(newline);
       afile.addJVSuffix(jvsuffix);
 
-      afile.setSeqs(alignment.getSequencesArray());
+      afile.setExportSettings(exportSettings);
+
+      afile.configureForView(viewpanel);
 
+      // 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())
@@ -655,7 +662,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();