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