JAL-2344 FileFormats singleton for formats, FileFormatI simplified
[jalview.git] / src / jalview / io / FileFormat.java
index 5f441d2..6d7042d 100644 (file)
@@ -6,31 +6,19 @@ import jalview.ext.jmol.JmolParser;
 import jalview.structure.StructureImportSettings;
 
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
 
 public enum FileFormat implements FileFormatI
 {
   Fasta("Fasta", "fa, fasta, mfa, fastq", true, true)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new FastaFile(inFile, sourceType);
-    }
-
-    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new FastaFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new FastaFile();
     }
@@ -38,21 +26,13 @@ public enum FileFormat implements FileFormatI
   Pfam("PFAM", "pfam", true, true)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new PfamFile(inFile, sourceType);
-    }
-
-    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new PfamFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new PfamFile();
     }
@@ -60,20 +40,13 @@ public enum FileFormat implements FileFormatI
   Stockholm("Stockholm", "sto,stk", true, true)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new StockholmFile(inFile, sourceType);
-    }
-    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new StockholmFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new StockholmFile(al);
     }
@@ -83,20 +56,13 @@ public enum FileFormat implements FileFormatI
   PIR("PIR", "pir", true, true)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new PIRFile(inFile, sourceType);
-    }
-    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new PIRFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new PIRFile();
     }
@@ -104,19 +70,13 @@ public enum FileFormat implements FileFormatI
   BLC("BLC", "BLC", true, true)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new BLCFile(inFile, sourceType);
-    }    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new BLCFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new BLCFile();
     }
@@ -124,21 +84,13 @@ public enum FileFormat implements FileFormatI
   AMSA("AMSA", "amsa", true, true)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new AMSAFile(inFile, sourceType);
-    }
-
-    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new AMSAFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new AMSAFile(al);
     }
@@ -146,19 +98,13 @@ public enum FileFormat implements FileFormatI
   Html("HTML", "html", true, false)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new HtmlFile(inFile, sourceType);
-    }    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new HtmlFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new HtmlFile();
     }
@@ -173,40 +119,28 @@ public enum FileFormat implements FileFormatI
   Rnaml("RNAML", "xml,rnaml", true, false)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new RnamlFile(inFile, sourceType);
-    }    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new RnamlFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new RnamlFile();
     }
 
   },
-  Json("JSON","json", true, true)
+  Json("JSON", "json", true, true)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new JSONFile(inFile, sourceType);
-    }    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new JSONFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new JSONFile();
     }
@@ -221,19 +155,13 @@ public enum FileFormat implements FileFormatI
   Pileup("PileUp", "pileup", true, true)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new PileUpfile(inFile, sourceType);
-    }    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new PileUpfile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new PileUpfile();
     }
@@ -242,19 +170,13 @@ public enum FileFormat implements FileFormatI
   MSF("MSF", "msf", true, true)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new MSFfile(inFile, sourceType);
-    }    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new MSFfile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new MSFfile();
     }
@@ -263,19 +185,13 @@ public enum FileFormat implements FileFormatI
   Clustal("Clustal", "aln", true, true)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new ClustalFile(inFile, sourceType);
-    }    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new ClustalFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new ClustalFile();
     }
@@ -283,21 +199,13 @@ public enum FileFormat implements FileFormatI
   Phylip("PHYLIP", "phy", true, true)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new PhylipFile(inFile, sourceType);
-    }
-
-    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new PhylipFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new PhylipFile();
     }
@@ -305,17 +213,7 @@ public enum FileFormat implements FileFormatI
   Jnet("JnetFile", "", false, false)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      JPredFile af = new JPredFile(inFile, sourceType);
-      af.removeNonSequences();
-      return af;
-    }
-
-    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       JPredFile af = new JPredFile(source);
       af.removeNonSequences();
@@ -323,7 +221,7 @@ public enum FileFormat implements FileFormatI
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return null; // todo is this called?
     }
@@ -332,55 +230,21 @@ public enum FileFormat implements FileFormatI
   Features("GFF or Jalview features", "gff2,gff3", true, false)
   {
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new FeaturesFile(true, inFile, sourceType);
-    }
-
-    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new FeaturesFile(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new FeaturesFile();
     }
   },
   PDB("PDB", "pdb,ent", true, false)
   {
-
     @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      // TODO obtain config value from preference settings.
-      // Set value to 'true' to test PDB processing with Jmol: JAL-1213
-      boolean isParseWithJMOL = StructureImportSettings
-              .getDefaultStructureFileFormat() != PDBEntry.Type.PDB;
-      if (isParseWithJMOL)
-      {
-        return new JmolParser(inFile, sourceType);
-      }
-      else
-      {
-        StructureImportSettings.setShowSeqFeatures(true);
-        return new MCview.PDBfile(
-                StructureImportSettings.isVisibleChainAnnotation(),
-                StructureImportSettings.isProcessSecondaryStructure(),
-                StructureImportSettings.isExternalSecondaryStructure(),
-                inFile,
-                sourceType);
-      }
-    }
-
-    @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       boolean isParseWithJMOL = StructureImportSettings
               .getDefaultStructureFileFormat() != PDBEntry.Type.PDB;
@@ -400,7 +264,7 @@ public enum FileFormat implements FileFormatI
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new JmolParser(); // todo or null?
     }
@@ -413,23 +277,14 @@ public enum FileFormat implements FileFormatI
   },
   MMCif("mmCIF", "cif", true, false)
   {
-
-    @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return new JmolParser(inFile, sourceType);
-    }
-
     @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return new JmolParser(source);
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return new JmolParser(); // todo or null?
     }
@@ -442,23 +297,14 @@ public enum FileFormat implements FileFormatI
   },
   Jalview("Jalview", "jar,jvp", true, true)
   {
-
-    @Override
-    public AlignmentFileI getAlignmentFile(String inFile,
-            DataSourceType sourceType) throws IOException
-    {
-      return null;
-    }
-
     @Override
-    public AlignmentFileI getAlignmentFile(FileParse source)
-            throws IOException
+    public AlignmentFileI getReader(FileParse source) throws IOException
     {
       return null;
     }
 
     @Override
-    public AlignmentFileI getAlignmentFile(AlignmentI al)
+    public AlignmentFileI getWriter(AlignmentI al)
     {
       return null;
     }
@@ -470,19 +316,6 @@ public enum FileFormat implements FileFormatI
     }
   };
 
-  /**
-   * A lookup map of enums by upper-cased name
-   */
-  private static Map<String, FileFormat> names;
-  static
-  {
-    names = new HashMap<String, FileFormat>();
-    for (FileFormat format : FileFormat.values())
-    {
-      names.put(format.toString().toUpperCase(), format);
-    }
-  }
-
   private boolean writable;
 
   private boolean readable;
@@ -491,65 +324,12 @@ public enum FileFormat implements FileFormatI
 
   private String name;
 
-  /**
-   * Answers a list of writeable file formats (as string, corresponding to the
-   * toString() and forName() methods)
-   * 
-   * @return
-   */
-  public static List<String> getWritableFormats(boolean textOnly)
-  {
-    List<String> l = new ArrayList<String>();
-    for (FileFormatI ff : values())
-    {
-      if (ff.isWritable() && (!textOnly || ff.isTextFormat()))
-      {
-        l.add(ff.toString());
-      }
-    }
-    return l;
-  }
-
-  /**
-   * Answers a list of readable file formats (as string, corresponding to the
-   * toString() and forName() methods)
-   * 
-   * @return
-   */
-  public static List<String> getReadableFormats()
-  {
-    List<String> l = new ArrayList<String>();
-    for (FileFormatI ff : values())
-    {
-      if (ff.isReadable())
-      {
-        l.add(ff.toString());
-      }
-    }
-    return l;
-  }
-
   @Override
   public boolean isComplexAlignFile()
   {
     return false;
   }
 
-  /**
-   * Returns the file format with the given name, or null if format is null or
-   * invalid. Unlike valueOf(), this is not case-sensitive, to be kind to
-   * writers of javascript.
-   * 
-   * @param format
-   * @return
-   */
-  public static FileFormatI forName(String format)
-  {
-    // or could store format.getShortDescription().toUpperCase()
-    // in order to decouple 'given name' from enum name
-    return format == null ? null : names.get(format.toUpperCase());
-  }
-
   @Override
   public boolean isReadable()
   {
@@ -586,19 +366,17 @@ public enum FileFormat implements FileFormatI
     return extensions;
   }
 
+  /**
+   * Answers the display name of the file format (as for example shown in menu
+   * options). This name should not be locale (language) dependent.
+   */
   @Override
-  public String toString()
+  public String getName()
   {
     return name;
   }
 
   @Override
-  public AlignmentFileI getAlignmentFile()
-  {
-    return getAlignmentFile((AlignmentI) null);
-  }
-
-  @Override
   public boolean isTextFormat()
   {
     return true;