JAL-2344 added FileFormatI.isStructureFile()
[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     @Override
409     public boolean isStructureFile()
410     {
411       return true;
412     }
413   },
414   MMCif("mmCIF", "cif", true, false)
415   {
416
417     @Override
418     public AlignmentFileI getAlignmentFile(String inFile,
419             DataSourceType sourceType) throws IOException
420     {
421       return new JmolParser(inFile, sourceType);
422     }
423
424     @Override
425     public AlignmentFileI getAlignmentFile(FileParse source)
426             throws IOException
427     {
428       return new JmolParser(source);
429     }
430
431     @Override
432     public AlignmentFileI getAlignmentFile(AlignmentI al)
433     {
434       return new JmolParser(); // todo or null?
435     }
436
437     @Override
438     public boolean isStructureFile()
439     {
440       return true;
441     }
442   },
443   Jalview("Jalview", "jar,jvp", true, true)
444   {
445
446     @Override
447     public AlignmentFileI getAlignmentFile(String inFile,
448             DataSourceType sourceType) throws IOException
449     {
450       return null;
451     }
452
453     @Override
454     public AlignmentFileI getAlignmentFile(FileParse source)
455             throws IOException
456     {
457       return null;
458     }
459
460     @Override
461     public AlignmentFileI getAlignmentFile(AlignmentI al)
462     {
463       return null;
464     }
465
466     @Override
467     public boolean isTextFormat()
468     {
469       return false;
470     }
471   };
472
473   /**
474    * A lookup map of enums by upper-cased name
475    */
476   private static Map<String, FileFormat> names;
477   static
478   {
479     names = new HashMap<String, FileFormat>();
480     for (FileFormat format : FileFormat.values())
481     {
482       names.put(format.toString().toUpperCase(), format);
483     }
484   }
485
486   private boolean writable;
487
488   private boolean readable;
489
490   private String extensions;
491
492   private String name;
493
494   /**
495    * Answers a list of writeable file formats (as string, corresponding to the
496    * toString() and forName() methods)
497    * 
498    * @return
499    */
500   public static List<String> getWritableFormats(boolean textOnly)
501   {
502     List<String> l = new ArrayList<String>();
503     for (FileFormatI ff : values())
504     {
505       if (ff.isWritable() && (!textOnly || ff.isTextFormat()))
506       {
507         l.add(ff.toString());
508       }
509     }
510     return l;
511   }
512
513   /**
514    * Answers a list of readable file formats (as string, corresponding to the
515    * toString() and forName() methods)
516    * 
517    * @return
518    */
519   public static List<String> getReadableFormats()
520   {
521     List<String> l = new ArrayList<String>();
522     for (FileFormatI ff : values())
523     {
524       if (ff.isReadable())
525       {
526         l.add(ff.toString());
527       }
528     }
529     return l;
530   }
531
532   @Override
533   public boolean isComplexAlignFile()
534   {
535     return false;
536   }
537
538   /**
539    * Returns the file format with the given name, or null if format is null or
540    * invalid. Unlike valueOf(), this is not case-sensitive, to be kind to
541    * writers of javascript.
542    * 
543    * @param format
544    * @return
545    */
546   public static FileFormatI forName(String format)
547   {
548     // or could store format.getShortDescription().toUpperCase()
549     // in order to decouple 'given name' from enum name
550     return format == null ? null : names.get(format.toUpperCase());
551   }
552
553   @Override
554   public boolean isReadable()
555   {
556     return readable;
557   }
558
559   @Override
560   public boolean isWritable()
561   {
562     return writable;
563   }
564
565   /**
566    * Constructor
567    * 
568    * @param shortName
569    * @param extensions
570    *          comma-separated list of file extensions associated with the format
571    * @param isReadable
572    * @param isWritable
573    */
574   private FileFormat(String shortName, String extensions,
575           boolean isReadable, boolean isWritable)
576   {
577     this.name = shortName;
578     this.extensions = extensions;
579     this.readable = isReadable;
580     this.writable = isWritable;
581   }
582
583   @Override
584   public String getExtensions()
585   {
586     return extensions;
587   }
588
589   @Override
590   public String toString()
591   {
592     return name;
593   }
594
595   @Override
596   public AlignmentFileI getAlignmentFile()
597   {
598     return getAlignmentFile((AlignmentI) null);
599   }
600
601   @Override
602   public boolean isTextFormat()
603   {
604     return true;
605   }
606
607   @Override
608   public boolean isStructureFile()
609   {
610     return false;
611   }
612 }