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