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