fileFormat enum wip changes
[jalview.git] / src / jalview / io / FileFormat.java
1 package jalview.io;
2
3 import jalview.ext.jmol.JmolParser;
4 import jalview.structure.StructureImportSettings;
5
6 import java.io.IOException;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 public enum FileFormat implements FileFormatI
11 {
12   Fasta("FASTA", "fa, fasta, mfa, fastq", true, true)
13   {
14     @Override
15     public AlignmentFileI getAlignmentFile(String inFile,
16             DataSourceType sourceType) throws IOException
17     {
18       return new FastaFile(inFile, sourceType);
19     }
20
21     @Override
22     public AlignmentFileI getAlignmentFile(FileParse source)
23             throws IOException
24     {
25       return new FastaFile(source);
26     }
27
28     @Override
29     public AlignmentFileI getAlignmentFile()
30     {
31       return new FastaFile();
32     }
33   },
34   Pfam("PFAM", "pfam", true, true)
35   {
36     @Override
37     public AlignmentFileI getAlignmentFile(String inFile,
38             DataSourceType sourceType) throws IOException
39     {
40       return new PfamFile(inFile, sourceType);
41     }
42
43     @Override
44     public AlignmentFileI getAlignmentFile(FileParse source)
45             throws IOException
46     {
47       return new PfamFile(source);
48     }
49
50     @Override
51     public AlignmentFileI getAlignmentFile()
52     {
53       return new PfamFile();
54     }
55   },
56   Stockholm("STH", "sto,stk", true, true)
57   {
58     @Override
59     public AlignmentFileI getAlignmentFile(String inFile,
60             DataSourceType sourceType) throws IOException
61     {
62       return new StockholmFile(inFile, sourceType);
63     }
64     @Override
65     public AlignmentFileI getAlignmentFile(FileParse source)
66             throws IOException
67     {
68       return new StockholmFile(source);
69     }
70
71     @Override
72     public AlignmentFileI getAlignmentFile()
73     {
74       return new StockholmFile();
75     }
76
77   },
78
79   PIR("PIR", "pir", true, true)
80   {
81     @Override
82     public AlignmentFileI getAlignmentFile(String inFile,
83             DataSourceType sourceType) throws IOException
84     {
85       return new PIRFile(inFile, sourceType);
86     }
87     @Override
88     public AlignmentFileI getAlignmentFile(FileParse source)
89             throws IOException
90     {
91       return new PIRFile(source);
92     }
93
94     @Override
95     public AlignmentFileI getAlignmentFile()
96     {
97       return new PIRFile();
98     }
99   },
100   BLC("BLC", "BLC", true, true)
101   {
102     @Override
103     public AlignmentFileI getAlignmentFile(String inFile,
104             DataSourceType sourceType) throws IOException
105     {
106       return new BLCFile(inFile, sourceType);
107     }    @Override
108     public AlignmentFileI getAlignmentFile(FileParse source)
109             throws IOException
110     {
111       return new BLCFile(source);
112     }
113
114     @Override
115     public AlignmentFileI getAlignmentFile()
116     {
117       return new BLCFile();
118     }
119   },
120   Html("HTML", "html", true, false)
121   {
122     @Override
123     public AlignmentFileI getAlignmentFile(String inFile,
124             DataSourceType sourceType) throws IOException
125     {
126       return new HtmlFile(inFile, sourceType);
127     }    @Override
128     public AlignmentFileI getAlignmentFile(FileParse source)
129             throws IOException
130     {
131       return new HtmlFile(source);
132     }
133
134     @Override
135     public AlignmentFileI getAlignmentFile()
136     {
137       return new HtmlFile();
138     }
139
140     @Override
141     public boolean isComplexAlignFile()
142     {
143       return true;
144     }
145
146   },
147   Rnaml("RNAML", "xml,rnaml", true, false)
148   {
149     @Override
150     public AlignmentFileI getAlignmentFile(String inFile,
151             DataSourceType sourceType) throws IOException
152     {
153       return new RnamlFile(inFile, sourceType);
154     }    @Override
155     public AlignmentFileI getAlignmentFile(FileParse source)
156             throws IOException
157     {
158       return new RnamlFile(source);
159     }
160
161     @Override
162     public AlignmentFileI getAlignmentFile()
163     {
164       return new RnamlFile();
165     }
166
167   },
168   Json("JSON","json", true, true)
169   {
170     @Override
171     public AlignmentFileI getAlignmentFile(String inFile,
172             DataSourceType sourceType) throws IOException
173     {
174       return new JSONFile(inFile, sourceType);
175     }    @Override
176     public AlignmentFileI getAlignmentFile(FileParse source)
177             throws IOException
178     {
179       return new JSONFile(source);
180     }
181
182     @Override
183     public AlignmentFileI getAlignmentFile()
184     {
185       return new JSONFile();
186     }
187
188     @Override
189     public boolean isComplexAlignFile()
190     {
191       return true;
192     }
193
194   },
195   Pileup("PileUp", "?", false, false)
196   {
197     @Override
198     public AlignmentFileI getAlignmentFile(String inFile,
199             DataSourceType sourceType) throws IOException
200     {
201       return new PileUpfile(inFile, sourceType);
202     }    @Override
203     public AlignmentFileI getAlignmentFile(FileParse source)
204             throws IOException
205     {
206       return new PileUpfile(source);
207     }
208
209     @Override
210     public AlignmentFileI getAlignmentFile()
211     {
212       return new PileUpfile();
213     }
214
215   },
216   MSF("MSF", "msf", true, true)
217   {
218     @Override
219     public AlignmentFileI getAlignmentFile(String inFile,
220             DataSourceType sourceType) throws IOException
221     {
222       return new MSFfile(inFile, sourceType);
223     }    @Override
224     public AlignmentFileI getAlignmentFile(FileParse source)
225             throws IOException
226     {
227       return new MSFfile(source);
228     }
229
230     @Override
231     public AlignmentFileI getAlignmentFile()
232     {
233       return new MSFfile();
234     }
235
236   },
237   Clustal("CLUSTAL", "aln", true, true)
238   {
239     @Override
240     public AlignmentFileI getAlignmentFile(String inFile,
241             DataSourceType sourceType) throws IOException
242     {
243       return new ClustalFile(inFile, sourceType);
244     }    @Override
245     public AlignmentFileI getAlignmentFile(FileParse source)
246             throws IOException
247     {
248       return new ClustalFile(source);
249     }
250
251     @Override
252     public AlignmentFileI getAlignmentFile()
253     {
254       return new ClustalFile();
255     }
256   },
257   Phylip("PHYLIP", "phy", true, true)
258   {
259     @Override
260     public AlignmentFileI getAlignmentFile(String inFile,
261             DataSourceType sourceType) throws IOException
262     {
263       return new PhylipFile(inFile, sourceType);
264     }
265
266     @Override
267     public AlignmentFileI getAlignmentFile(FileParse source)
268             throws IOException
269     {
270       return new PhylipFile(source);
271     }
272
273     @Override
274     public AlignmentFileI getAlignmentFile()
275     {
276       return new PhylipFile();
277     }
278   },
279   Jnet("JnetFile", "", false, false)
280   {
281     @Override
282     public AlignmentFileI getAlignmentFile(String inFile,
283             DataSourceType sourceType) throws IOException
284     {
285       JPredFile af = new JPredFile(inFile, sourceType);
286       af.removeNonSequences();
287       return af;
288     }
289
290     @Override
291     public AlignmentFileI getAlignmentFile(FileParse source)
292             throws IOException
293     {
294       JPredFile af = new JPredFile(source);
295       af.removeNonSequences();
296       return af;
297     }
298
299     @Override
300     public AlignmentFileI getAlignmentFile()
301     {
302       return null; // todo is this called?
303     }
304
305   },
306   Features("GFF or Jalview features", "gff2,gff3", false, false)
307   {
308     @Override
309     public AlignmentFileI getAlignmentFile(String inFile,
310             DataSourceType sourceType) throws IOException
311     {
312       return new FeaturesFile(true, inFile, sourceType);
313     }
314
315     @Override
316     public AlignmentFileI getAlignmentFile(FileParse source)
317             throws IOException
318     {
319       return new FeaturesFile(source);
320     }
321
322     @Override
323     public AlignmentFileI getAlignmentFile()
324     {
325       return new FeaturesFile();
326     }
327   },
328   PDB("PDB", "", false, false)
329   {
330
331     @Override
332     public AlignmentFileI getAlignmentFile(String inFile,
333             DataSourceType sourceType) throws IOException
334     {
335       // TODO obtain config value from preference settings.
336       // Set value to 'true' to test PDB processing with Jmol: JAL-1213
337       boolean isParseWithJMOL = !StructureImportSettings
338               .getCurrentDefaultFormat().equalsIgnoreCase("PDB");
339       if (isParseWithJMOL)
340       {
341         return new JmolParser(
342                 StructureImportSettings.isVisibleChainAnnotation(),
343                 StructureImportSettings.isPredictSecondaryStructure(),
344                 StructureImportSettings.isExternalSecondaryStructure(),
345                 inFile,
346                 sourceType);
347       }
348       else
349       {
350         StructureImportSettings.setShowSeqFeatures(true);
351         return new MCview.PDBfile(
352                 StructureImportSettings.isVisibleChainAnnotation(),
353                 StructureImportSettings.isPredictSecondaryStructure(),
354                 StructureImportSettings.isExternalSecondaryStructure(),
355                 inFile,
356                 sourceType);
357       }
358     }
359
360     @Override
361     public AlignmentFileI getAlignmentFile(FileParse source)
362             throws IOException
363     {
364       boolean isParseWithJMOL = !StructureImportSettings
365               .getCurrentDefaultFormat().equalsIgnoreCase("PDB");
366       if (isParseWithJMOL)
367       {
368         return new JmolParser(
369                 StructureImportSettings.isVisibleChainAnnotation(),
370                 StructureImportSettings.isPredictSecondaryStructure(),
371                 StructureImportSettings.isExternalSecondaryStructure(),
372                 source);
373       }
374       else
375       {
376         StructureImportSettings.setShowSeqFeatures(true);
377         return new MCview.PDBfile(
378                 StructureImportSettings.isVisibleChainAnnotation(),
379                 StructureImportSettings.isPredictSecondaryStructure(),
380                 StructureImportSettings.isExternalSecondaryStructure(),
381                 source);
382       }
383     }
384
385     @Override
386     public AlignmentFileI getAlignmentFile()
387     {
388       return new JmolParser(); // todo or null?
389     }
390   },
391   MMCif("mmCIF", "cif", false, false)
392   {
393
394     @Override
395     public AlignmentFileI getAlignmentFile(String inFile,
396             DataSourceType sourceType) throws IOException
397     {
398       return new JmolParser(
399               StructureImportSettings.isVisibleChainAnnotation(),
400               StructureImportSettings.isPredictSecondaryStructure(),
401               StructureImportSettings.isExternalSecondaryStructure(),
402               inFile, sourceType);
403     }
404
405     @Override
406     public AlignmentFileI getAlignmentFile(FileParse source)
407             throws IOException
408     {
409       return new JmolParser(
410               StructureImportSettings.isVisibleChainAnnotation(),
411               StructureImportSettings.isPredictSecondaryStructure(),
412               StructureImportSettings.isExternalSecondaryStructure(),
413               source);
414     }
415
416     @Override
417     public AlignmentFileI getAlignmentFile()
418     {
419       return new JmolParser(); // todo or null?
420     }
421   },
422   Jalview("Jalview", "jar,jvp", true, false)
423   {
424
425     @Override
426     public AlignmentFileI getAlignmentFile(String inFile,
427             DataSourceType sourceType) throws IOException
428     {
429       return null;
430     }
431
432     @Override
433     public AlignmentFileI getAlignmentFile(FileParse source)
434             throws IOException
435     {
436       return null;
437     }
438
439     @Override
440     public AlignmentFileI getAlignmentFile()
441     {
442       return null;
443     }
444   };
445
446   /**
447    * A lookup map of enums by upper-cased name
448    */
449   private static Map<String, FileFormat> names;
450   static
451   {
452     names = new HashMap<String, FileFormat>();
453     for (FileFormat format : FileFormat.values())
454     {
455       names.put(format.toString().toUpperCase(), format);
456     }
457   }
458
459   private boolean writable;
460
461   private boolean readable;
462
463   private String extensions;
464
465   private String name;
466
467   @Override
468   public boolean isComplexAlignFile()
469   {
470     return false;
471   }
472
473   @Override
474   public String getShortDescription()
475   {
476     return toString();
477   }
478
479   /**
480    * Returns the file format with the given name, or null if format is null or
481    * invalid. Unlike valueOf(), this is not case-sensitive, to be kind to
482    * writers of javascript.
483    * 
484    * @param format
485    * @return
486    */
487   public static FileFormatI forName(String format)
488   {
489     // or could store format.getShortDescription().toUpperCase()
490     // in order to decouple 'given name' from enum name
491     return format == null ? null : names.get(format.toUpperCase());
492   }
493
494   @Override
495   public boolean isReadable()
496   {
497     return readable;
498   }
499
500   @Override
501   public boolean isWritable()
502   {
503     return writable;
504   }
505
506   /**
507    * Constructor
508    * 
509    * @param shortName
510    * @param extensions
511    *          comma-separated list of file extensions associated with the format
512    * @param isReadable
513    * @param isWritable
514    */
515   private FileFormat(String shortName, String extensions,
516           boolean isReadable, boolean isWritable)
517   {
518     this.name = shortName;
519     this.extensions = extensions;
520     this.readable = isReadable;
521     this.writable = isWritable;
522   }
523
524   @Override
525   public String getExtensions()
526   {
527     return extensions;
528   }
529 }