FileFormatI further tweaks, clean compile
[jalview.git] / src / jalview / io / FileFormat.java
index 893a8ac..770f762 100644 (file)
@@ -1,13 +1,18 @@
 package jalview.io;
 
+import jalview.datamodel.PDBEntry;
 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("Fasta", "fa, fasta, mfa, fastq", true, true)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -29,7 +34,7 @@ public enum FileFormat implements FileFormatI
       return new FastaFile();
     }
   },
-  Pfam
+  Pfam("PFAM", "pfam", true, true)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -51,7 +56,7 @@ public enum FileFormat implements FileFormatI
       return new PfamFile();
     }
   },
-  Stockholm
+  Stockholm("Stockholm", "sto,stk", true, true)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -73,62 +78,62 @@ public enum FileFormat implements FileFormatI
     }
 
   },
-  SimpleBlast
+
+  PIR("PIR", "pir", true, true)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
             DataSourceType sourceType) throws IOException
     {
-      return new SimpleBlastFile(inFile, sourceType);
+      return new PIRFile(inFile, sourceType);
     }
-
     @Override
     public AlignmentFileI getAlignmentFile(FileParse source)
             throws IOException
     {
-      return new SimpleBlastFile(source);
+      return new PIRFile(source);
     }
 
     @Override
     public AlignmentFileI getAlignmentFile()
     {
-      return new SimpleBlastFile();
+      return new PIRFile();
     }
   },
-
-  PIR
+  BLC("BLC", "BLC", true, true)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
             DataSourceType sourceType) throws IOException
     {
-      return new PIRFile(inFile, sourceType);
-    }
-    @Override
+      return new BLCFile(inFile, sourceType);
+    }    @Override
     public AlignmentFileI getAlignmentFile(FileParse source)
             throws IOException
     {
-      return new PIRFile(source);
+      return new BLCFile(source);
     }
 
     @Override
     public AlignmentFileI getAlignmentFile()
     {
-      return new PIRFile();
+      return new BLCFile();
     }
   },
-  BLC
+  AMSA("AMSA", "amsa", true, true)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
             DataSourceType sourceType) throws IOException
     {
-      return new BLCFile(inFile, sourceType);
-    }    @Override
+      return new AMSAFile(inFile, sourceType);
+    }
+
+    @Override
     public AlignmentFileI getAlignmentFile(FileParse source)
             throws IOException
     {
-      return new BLCFile(source);
+      return new AMSAFile(source);
     }
 
     @Override
@@ -136,9 +141,8 @@ public enum FileFormat implements FileFormatI
     {
       return new BLCFile();
     }
-
   },
-  Html
+  Html("HTML", "html", true, false)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -165,7 +169,7 @@ public enum FileFormat implements FileFormatI
     }
 
   },
-  Rnaml
+  Rnaml("RNAML", "xml,rnaml", true, false)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -186,7 +190,7 @@ public enum FileFormat implements FileFormatI
     }
 
   },
-  Json
+  Json("JSON","json", true, true)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -213,7 +217,7 @@ public enum FileFormat implements FileFormatI
     }
 
   },
-  Pileup
+  Pileup("PileUp", "pileup", false, true)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -234,7 +238,7 @@ public enum FileFormat implements FileFormatI
     }
 
   },
-  MSF
+  MSF("MSF", "msf", true, true)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -255,7 +259,7 @@ public enum FileFormat implements FileFormatI
     }
 
   },
-  Clustal
+  Clustal("Clustal", "aln", true, true)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -274,9 +278,8 @@ public enum FileFormat implements FileFormatI
     {
       return new ClustalFile();
     }
-
   },
-  Phylip
+  Phylip("PHYLIP", "phy", true, true)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -297,9 +300,8 @@ public enum FileFormat implements FileFormatI
     {
       return new PhylipFile();
     }
-
   },
-  Jnet
+  Jnet("JnetFile", "", false, false)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -326,7 +328,7 @@ public enum FileFormat implements FileFormatI
     }
 
   },
-  Features
+  Features("GFF or Jalview features", "gff2,gff3", false, false)
   {
     @Override
     public AlignmentFileI getAlignmentFile(String inFile,
@@ -348,7 +350,7 @@ public enum FileFormat implements FileFormatI
       return new FeaturesFile();
     }
   },
-  PDB
+  PDB("PDB", "", false, false)
   {
 
     @Override
@@ -357,13 +359,13 @@ public enum FileFormat implements FileFormatI
     {
       // TODO obtain config value from preference settings.
       // Set value to 'true' to test PDB processing with Jmol: JAL-1213
-      boolean isParseWithJMOL = !StructureImportSettings
-              .getCurrentDefaultFormat().equalsIgnoreCase("PDB");
+      boolean isParseWithJMOL = StructureImportSettings
+              .getDefaultStructureFileFormat() != PDBEntry.Type.PDB;
       if (isParseWithJMOL)
       {
         return new JmolParser(
                 StructureImportSettings.isVisibleChainAnnotation(),
-                StructureImportSettings.isPredictSecondaryStructure(),
+                StructureImportSettings.isProcessSecondaryStructure(),
                 StructureImportSettings.isExternalSecondaryStructure(),
                 inFile,
                 sourceType);
@@ -373,7 +375,7 @@ public enum FileFormat implements FileFormatI
         StructureImportSettings.setShowSeqFeatures(true);
         return new MCview.PDBfile(
                 StructureImportSettings.isVisibleChainAnnotation(),
-                StructureImportSettings.isPredictSecondaryStructure(),
+                StructureImportSettings.isProcessSecondaryStructure(),
                 StructureImportSettings.isExternalSecondaryStructure(),
                 inFile,
                 sourceType);
@@ -384,13 +386,13 @@ public enum FileFormat implements FileFormatI
     public AlignmentFileI getAlignmentFile(FileParse source)
             throws IOException
     {
-      boolean isParseWithJMOL = !StructureImportSettings
-              .getCurrentDefaultFormat().equalsIgnoreCase("PDB");
+      boolean isParseWithJMOL = StructureImportSettings
+              .getDefaultStructureFileFormat() != PDBEntry.Type.PDB;
       if (isParseWithJMOL)
       {
         return new JmolParser(
                 StructureImportSettings.isVisibleChainAnnotation(),
-                StructureImportSettings.isPredictSecondaryStructure(),
+                StructureImportSettings.isProcessSecondaryStructure(),
                 StructureImportSettings.isExternalSecondaryStructure(),
                 source);
       }
@@ -399,7 +401,7 @@ public enum FileFormat implements FileFormatI
         StructureImportSettings.setShowSeqFeatures(true);
         return new MCview.PDBfile(
                 StructureImportSettings.isVisibleChainAnnotation(),
-                StructureImportSettings.isPredictSecondaryStructure(),
+                StructureImportSettings.isProcessSecondaryStructure(),
                 StructureImportSettings.isExternalSecondaryStructure(),
                 source);
       }
@@ -410,9 +412,8 @@ public enum FileFormat implements FileFormatI
     {
       return new JmolParser(); // todo or null?
     }
-
   },
-  MMCif
+  MMCif("mmCIF", "cif", false, false)
   {
 
     @Override
@@ -421,7 +422,7 @@ public enum FileFormat implements FileFormatI
     {
       return new JmolParser(
               StructureImportSettings.isVisibleChainAnnotation(),
-              StructureImportSettings.isPredictSecondaryStructure(),
+              StructureImportSettings.isProcessSecondaryStructure(),
               StructureImportSettings.isExternalSecondaryStructure(),
               inFile, sourceType);
     }
@@ -432,7 +433,7 @@ public enum FileFormat implements FileFormatI
     {
       return new JmolParser(
               StructureImportSettings.isVisibleChainAnnotation(),
-              StructureImportSettings.isPredictSecondaryStructure(),
+              StructureImportSettings.isProcessSecondaryStructure(),
               StructureImportSettings.isExternalSecondaryStructure(),
               source);
     }
@@ -443,7 +444,7 @@ public enum FileFormat implements FileFormatI
       return new JmolParser(); // todo or null?
     }
   },
-  Jalview
+  Jalview("Jalview", "jar,jvp", true, false)
   {
 
     @Override
@@ -467,6 +468,65 @@ 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;
+
+  private String extensions;
+
+  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()
+  {
+    List<String> l = new ArrayList<String>();
+    for (FileFormatI ff : values())
+    {
+      if (ff.isWritable())
+      {
+        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()
   {
@@ -478,4 +538,61 @@ public enum FileFormat implements FileFormatI
   {
     return toString();
   }
+
+  /**
+   * 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()
+  {
+    return readable;
+  }
+
+  @Override
+  public boolean isWritable()
+  {
+    return writable;
+  }
+
+  /**
+   * Constructor
+   * 
+   * @param shortName
+   * @param extensions
+   *          comma-separated list of file extensions associated with the format
+   * @param isReadable
+   * @param isWritable
+   */
+  private FileFormat(String shortName, String extensions,
+          boolean isReadable, boolean isWritable)
+  {
+    this.name = shortName;
+    this.extensions = extensions;
+    this.readable = isReadable;
+    this.writable = isWritable;
+  }
+
+  @Override
+  public String getExtensions()
+  {
+    return extensions;
+  }
+
+  @Override
+  public String toString()
+  {
+    return name;
+  }
 }