JAL-2344 now flagging formats as 'identifiable' instead of 'dynamic'
[jalview.git] / src / jalview / io / FileFormat.java
1 package jalview.io;
2
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.PDBEntry;
5 import jalview.ext.jmol.JmolParser;
6 import jalview.structure.StructureImportSettings;
7
8 import java.io.IOException;
9
10 public enum FileFormat implements FileFormatI
11 {
12   Fasta("Fasta", "fa, fasta, mfa, fastq", true, true)
13   {
14     @Override
15     public AlignmentFileReaderI getReader(FileParse source)
16             throws IOException
17     {
18       return new FastaFile(source);
19     }
20
21     @Override
22     public AlignmentFileWriterI getWriter(AlignmentI al)
23     {
24       return new FastaFile();
25     }
26   },
27   Pfam("PFAM", "pfam", true, true)
28   {
29     @Override
30     public AlignmentFileReaderI getReader(FileParse source)
31             throws IOException
32     {
33       return new PfamFile(source);
34     }
35
36     @Override
37     public AlignmentFileWriterI getWriter(AlignmentI al)
38     {
39       return new PfamFile();
40     }
41   },
42   Stockholm("Stockholm", "sto,stk", true, true)
43   {
44     @Override
45     public AlignmentFileReaderI getReader(FileParse source)
46             throws IOException
47     {
48       return new StockholmFile(source);
49     }
50
51     @Override
52     public AlignmentFileWriterI getWriter(AlignmentI al)
53     {
54       return new StockholmFile(al);
55     }
56
57   },
58
59   PIR("PIR", "pir", true, true)
60   {
61     @Override
62     public AlignmentFileReaderI getReader(FileParse source)
63             throws IOException
64     {
65       return new PIRFile(source);
66     }
67
68     @Override
69     public AlignmentFileWriterI getWriter(AlignmentI al)
70     {
71       return new PIRFile();
72     }
73   },
74   BLC("BLC", "BLC", true, true)
75   {
76     @Override
77     public AlignmentFileReaderI getReader(FileParse source)
78             throws IOException
79     {
80       return new BLCFile(source);
81     }
82
83     @Override
84     public AlignmentFileWriterI getWriter(AlignmentI al)
85     {
86       return new BLCFile();
87     }
88   },
89   AMSA("AMSA", "amsa", true, true)
90   {
91     @Override
92     public AlignmentFileReaderI getReader(FileParse source)
93             throws IOException
94     {
95       return new AMSAFile(source);
96     }
97
98     @Override
99     public AlignmentFileWriterI getWriter(AlignmentI al)
100     {
101       return new AMSAFile(al);
102     }
103   },
104   Html("HTML", "html", true, false)
105   {
106     @Override
107     public AlignmentFileReaderI getReader(FileParse source)
108             throws IOException
109     {
110       return new HtmlFile(source);
111     }
112
113     @Override
114     public AlignmentFileWriterI getWriter(AlignmentI al)
115     {
116       return new HtmlFile();
117     }
118
119     @Override
120     public boolean isComplexAlignFile()
121     {
122       return true;
123     }
124
125   },
126   Rnaml("RNAML", "xml,rnaml", true, false)
127   {
128     @Override
129     public AlignmentFileReaderI getReader(FileParse source)
130             throws IOException
131     {
132       return new RnamlFile(source);
133     }
134
135     @Override
136     public AlignmentFileWriterI getWriter(AlignmentI al)
137     {
138       return new RnamlFile();
139     }
140
141   },
142   Json("JSON", "json", true, true)
143   {
144     @Override
145     public AlignmentFileReaderI getReader(FileParse source)
146             throws IOException
147     {
148       return new JSONFile(source);
149     }
150
151     @Override
152     public AlignmentFileWriterI getWriter(AlignmentI al)
153     {
154       return new JSONFile();
155     }
156
157     @Override
158     public boolean isComplexAlignFile()
159     {
160       return true;
161     }
162
163   },
164   Pileup("PileUp", "pileup", true, true)
165   {
166     @Override
167     public AlignmentFileReaderI getReader(FileParse source)
168             throws IOException
169     {
170       return new PileUpfile(source);
171     }
172
173     @Override
174     public AlignmentFileWriterI getWriter(AlignmentI al)
175     {
176       return new PileUpfile();
177     }
178
179   },
180   MSF("MSF", "msf", true, true)
181   {
182     @Override
183     public AlignmentFileReaderI getReader(FileParse source)
184             throws IOException
185     {
186       return new MSFfile(source);
187     }
188
189     @Override
190     public AlignmentFileWriterI getWriter(AlignmentI al)
191     {
192       return new MSFfile();
193     }
194
195   },
196   Clustal("Clustal", "aln", true, true)
197   {
198     @Override
199     public AlignmentFileReaderI getReader(FileParse source)
200             throws IOException
201     {
202       return new ClustalFile(source);
203     }
204
205     @Override
206     public AlignmentFileWriterI getWriter(AlignmentI al)
207     {
208       return new ClustalFile();
209     }
210   },
211   Phylip("PHYLIP", "phy", true, true)
212   {
213     @Override
214     public AlignmentFileReaderI getReader(FileParse source)
215             throws IOException
216     {
217       return new PhylipFile(source);
218     }
219
220     @Override
221     public AlignmentFileWriterI getWriter(AlignmentI al)
222     {
223       return new PhylipFile();
224     }
225   },
226   Jnet("JnetFile", "", false, false)
227   {
228     @Override
229     public AlignmentFileReaderI getReader(FileParse source)
230             throws IOException
231     {
232       JPredFile af = new JPredFile(source);
233       af.removeNonSequences();
234       return af;
235     }
236
237     @Override
238     public AlignmentFileWriterI getWriter(AlignmentI al)
239     {
240       return null; // todo is this called?
241     }
242
243   },
244   Features("GFF or Jalview features", "gff2,gff3", true, false)
245   {
246     @Override
247     public AlignmentFileReaderI getReader(FileParse source)
248             throws IOException
249     {
250       return new FeaturesFile(source);
251     }
252
253     @Override
254     public AlignmentFileWriterI getWriter(AlignmentI al)
255     {
256       return new FeaturesFile();
257     }
258   },
259   PDB("PDB", "pdb,ent", true, false)
260   {
261     @Override
262     public AlignmentFileReaderI getReader(FileParse source)
263             throws IOException
264     {
265       boolean isParseWithJMOL = StructureImportSettings
266               .getDefaultStructureFileFormat() != PDBEntry.Type.PDB;
267       if (isParseWithJMOL)
268       {
269         return new JmolParser(source);
270       }
271       else
272       {
273         StructureImportSettings.setShowSeqFeatures(true);
274         return new MCview.PDBfile(
275                 StructureImportSettings.isVisibleChainAnnotation(),
276                 StructureImportSettings.isProcessSecondaryStructure(),
277                 StructureImportSettings.isExternalSecondaryStructure(),
278                 source);
279       }
280     }
281
282     @Override
283     public AlignmentFileWriterI getWriter(AlignmentI al)
284     {
285       return new JmolParser(); // todo or null?
286     }
287
288     @Override
289     public boolean isStructureFile()
290     {
291       return true;
292     }
293   },
294   MMCif("mmCIF", "cif", true, false)
295   {
296     @Override
297     public AlignmentFileReaderI getReader(FileParse source)
298             throws IOException
299     {
300       return new JmolParser(source);
301     }
302
303     @Override
304     public AlignmentFileWriterI getWriter(AlignmentI al)
305     {
306       return new JmolParser(); // todo or null?
307     }
308
309     @Override
310     public boolean isStructureFile()
311     {
312       return true;
313     }
314   },
315   Jalview("Jalview", "jar,jvp", true, true)
316   {
317     @Override
318     public AlignmentFileReaderI getReader(FileParse source)
319             throws IOException
320     {
321       return null;
322     }
323
324     @Override
325     public AlignmentFileWriterI getWriter(AlignmentI al)
326     {
327       return null;
328     }
329
330     @Override
331     public boolean isTextFormat()
332     {
333       return false;
334     }
335
336     @Override
337     public boolean isIdentifiable()
338     {
339       return false;
340     }
341   };
342
343   private boolean writable;
344
345   private boolean readable;
346
347   private String extensions;
348
349   private String name;
350
351   @Override
352   public boolean isComplexAlignFile()
353   {
354     return false;
355   }
356
357   @Override
358   public boolean isReadable()
359   {
360     return readable;
361   }
362
363   @Override
364   public boolean isWritable()
365   {
366     return writable;
367   }
368
369   /**
370    * Constructor
371    * 
372    * @param shortName
373    * @param extensions
374    *          comma-separated list of file extensions associated with the format
375    * @param isReadable
376    * @param isWritable
377    */
378   private FileFormat(String shortName, String extensions,
379           boolean isReadable, boolean isWritable)
380   {
381     this.name = shortName;
382     this.extensions = extensions;
383     this.readable = isReadable;
384     this.writable = isWritable;
385   }
386
387   @Override
388   public String getExtensions()
389   {
390     return extensions;
391   }
392
393   /**
394    * Answers the display name of the file format (as for example shown in menu
395    * options). This name should not be locale (language) dependent.
396    */
397   @Override
398   public String getName()
399   {
400     return name;
401   }
402
403   @Override
404   public boolean isTextFormat()
405   {
406     return true;
407   }
408
409   @Override
410   public boolean isStructureFile()
411   {
412     return false;
413   }
414
415   /**
416    * By default, answers true, indicating the format is one that can be
417    * identified by IdentifyFile. Formats that cannot be identified should
418    * override this method to return false.
419    */
420   public boolean isIdentifiable()
421   {
422     return true;
423   }
424 }