JAL-1951 added comment to NexusFile clarifying Forester's broken parser
[jalview.git] / src / jalview / io / FileFormat.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.PDBEntry;
25 import jalview.ext.forester.io.NexusFile;
26 import jalview.ext.forester.io.PhyloXmlFile;
27 import jalview.ext.jmol.JmolParser;
28 import jalview.structure.StructureImportSettings;
29
30 import java.io.IOException;
31
32 public enum FileFormat implements FileFormatI
33 {
34   Fasta("Fasta", "fa, fasta, mfa, fastq", true, true)
35   {
36     @Override
37     public AlignmentFileReaderI getReader(FileParse source)
38             throws IOException
39     {
40       return new FastaFile(source);
41     }
42
43     @Override
44     public AlignmentFileWriterI getWriter(AlignmentI al)
45     {
46       return new FastaFile();
47     }
48   },
49   Pfam("PFAM", "pfam", true, true)
50   {
51     @Override
52     public AlignmentFileReaderI getReader(FileParse source)
53             throws IOException
54     {
55       return new PfamFile(source);
56     }
57
58     @Override
59     public AlignmentFileWriterI getWriter(AlignmentI al)
60     {
61       return new PfamFile();
62     }
63   },
64   Stockholm("Stockholm", "sto,stk", true, true)
65   {
66     @Override
67     public AlignmentFileReaderI getReader(FileParse source)
68             throws IOException
69     {
70       return new StockholmFile(source);
71     }
72
73     @Override
74     public AlignmentFileWriterI getWriter(AlignmentI al)
75     {
76       return new StockholmFile(al);
77     }
78
79   },
80
81   PIR("PIR", "pir", true, true)
82   {
83     @Override
84     public AlignmentFileReaderI getReader(FileParse source)
85             throws IOException
86     {
87       return new PIRFile(source);
88     }
89
90     @Override
91     public AlignmentFileWriterI getWriter(AlignmentI al)
92     {
93       return new PIRFile();
94     }
95   },
96   BLC("BLC", "BLC", true, true)
97   {
98     @Override
99     public AlignmentFileReaderI getReader(FileParse source)
100             throws IOException
101     {
102       return new BLCFile(source);
103     }
104
105     @Override
106     public AlignmentFileWriterI getWriter(AlignmentI al)
107     {
108       return new BLCFile();
109     }
110   },
111   AMSA("AMSA", "amsa", true, true)
112   {
113     @Override
114     public AlignmentFileReaderI getReader(FileParse source)
115             throws IOException
116     {
117       return new AMSAFile(source);
118     }
119
120     @Override
121     public AlignmentFileWriterI getWriter(AlignmentI al)
122     {
123       return new AMSAFile(al);
124     }
125   },
126   Html("HTML", "html", true, false)
127   {
128     @Override
129     public AlignmentFileReaderI getReader(FileParse source)
130             throws IOException
131     {
132       return new HtmlFile(source);
133     }
134
135     @Override
136     public AlignmentFileWriterI getWriter(AlignmentI al)
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 AlignmentFileReaderI getReader(FileParse source)
152             throws IOException
153     {
154       return new RnamlFile(source);
155     }
156
157     @Override
158     public AlignmentFileWriterI getWriter(AlignmentI al)
159     {
160       return new RnamlFile();
161     }
162
163   },
164   Json("JSON", "json", true, true)
165   {
166     @Override
167     public AlignmentFileReaderI getReader(FileParse source)
168             throws IOException
169     {
170       return new JSONFile(source);
171     }
172
173     @Override
174     public AlignmentFileWriterI getWriter(AlignmentI al)
175     {
176       return new JSONFile();
177     }
178
179     @Override
180     public boolean isComplexAlignFile()
181     {
182       return true;
183     }
184
185   },
186   Pileup("PileUp", "pileup", true, true)
187   {
188     @Override
189     public AlignmentFileReaderI getReader(FileParse source)
190             throws IOException
191     {
192       return new PileUpfile(source);
193     }
194
195     @Override
196     public AlignmentFileWriterI getWriter(AlignmentI al)
197     {
198       return new PileUpfile();
199     }
200
201   },
202   MSF("MSF", "msf", true, true)
203   {
204     @Override
205     public AlignmentFileReaderI getReader(FileParse source)
206             throws IOException
207     {
208       return new MSFfile(source);
209     }
210
211     @Override
212     public AlignmentFileWriterI getWriter(AlignmentI al)
213     {
214       return new MSFfile();
215     }
216
217   },
218   Clustal("Clustal", "aln", true, true)
219   {
220     @Override
221     public AlignmentFileReaderI getReader(FileParse source)
222             throws IOException
223     {
224       return new ClustalFile(source);
225     }
226
227     @Override
228     public AlignmentFileWriterI getWriter(AlignmentI al)
229     {
230       return new ClustalFile();
231     }
232   },
233   Phylip("PHYLIP", "phy", true, true)
234   {
235     @Override
236     public AlignmentFileReaderI getReader(FileParse source)
237             throws IOException
238     {
239       return new PhylipFile(source);
240     }
241
242     @Override
243     public AlignmentFileWriterI getWriter(AlignmentI al)
244     {
245       return new PhylipFile();
246     }
247   },
248   Jnet("JnetFile", "", false, false)
249   {
250     @Override
251     public AlignmentFileReaderI getReader(FileParse source)
252             throws IOException
253     {
254       JPredFile af = new JPredFile(source);
255       af.removeNonSequences();
256       return af;
257     }
258
259     @Override
260     public AlignmentFileWriterI getWriter(AlignmentI al)
261     {
262       return null; // todo is this called?
263     }
264
265   },
266   Features("GFF or Jalview features", "gff2,gff3", true, false)
267   {
268     @Override
269     public AlignmentFileReaderI getReader(FileParse source)
270             throws IOException
271     {
272       return new FeaturesFile(source);
273     }
274
275     @Override
276     public AlignmentFileWriterI getWriter(AlignmentI al)
277     {
278       return new FeaturesFile();
279     }
280   },
281   ScoreMatrix("Substitution matrix", "", false, false)
282   {
283     @Override
284     public AlignmentFileReaderI getReader(FileParse source)
285             throws IOException
286     {
287       return new ScoreMatrixFile(source);
288     }
289
290     @Override
291     public AlignmentFileWriterI getWriter(AlignmentI al)
292     {
293       return null;
294     }
295   },
296   PDB("PDB", "pdb,ent", true, false)
297   {
298     @Override
299     public AlignmentFileReaderI getReader(FileParse source)
300             throws IOException
301     {
302       boolean isParseWithJMOL = StructureImportSettings
303               .getDefaultStructureFileFormat() != PDBEntry.Type.PDB;
304       if (isParseWithJMOL)
305       {
306         return new JmolParser(source);
307       }
308       else
309       {
310         StructureImportSettings.setShowSeqFeatures(true);
311         return new MCview.PDBfile(
312                 StructureImportSettings.isVisibleChainAnnotation(),
313                 StructureImportSettings.isProcessSecondaryStructure(),
314                 StructureImportSettings.isExternalSecondaryStructure(),
315                 source);
316       }
317     }
318
319     @Override
320     public AlignmentFileWriterI getWriter(AlignmentI al)
321     {
322       return new JmolParser(); // todo or null?
323     }
324
325     @Override
326     public boolean isStructureFile()
327     {
328       return true;
329     }
330   },
331   MMCif("mmCIF", "cif", true, false)
332   {
333     @Override
334     public AlignmentFileReaderI getReader(FileParse source)
335             throws IOException
336     {
337       return new JmolParser(source);
338     }
339
340     @Override
341     public AlignmentFileWriterI getWriter(AlignmentI al)
342     {
343       return new JmolParser(); // todo or null?
344     }
345
346     @Override
347     public boolean isStructureFile()
348     {
349       return true;
350     }
351   },
352   Jalview("Jalview", "jar,jvp", true, true)
353   {
354     @Override
355     public AlignmentFileReaderI getReader(FileParse source)
356             throws IOException
357     {
358       return null;
359     }
360
361     @Override
362     public AlignmentFileWriterI getWriter(AlignmentI al)
363     {
364       return null;
365     }
366
367     @Override
368     public boolean isTextFormat()
369     {
370       return false;
371     }
372
373     @Override
374     public boolean isIdentifiable()
375     {
376       return false;
377     }
378   },
379   Nexus("Nexus", "nex,nexus,nx,tre", true, true)
380   {
381
382     @Override
383     public AlignmentFileReaderI getReader(FileParse source)
384             throws IOException
385     {
386       return new NexusFile(source);
387     }
388
389     @Override
390     public AlignmentFileWriterI getWriter(AlignmentI al)
391     {
392       // handle within Aptx?
393       return null;
394     }
395
396     @Override
397     public boolean isTextFormat()
398     {
399       return true;
400     }
401
402     @Override
403     public boolean isTreeFile()
404     {
405       return true;
406     }
407
408   },
409   PhyloXML("PhyloXML", "phyloxml,phylo.xml,pxml", true, true)
410   {
411
412     @Override
413     public AlignmentFileReaderI getReader(FileParse source)
414             throws IOException
415     {
416       return new PhyloXmlFile(source);
417     }
418
419     @Override
420     public AlignmentFileWriterI getWriter(AlignmentI al)
421     {
422       // handle within Aptx?
423       return null;
424     }
425
426     @Override
427     public boolean isTextFormat()
428     {
429       return true;
430     }
431
432     @Override
433     public boolean isTreeFile()
434     {
435       return true;
436     }
437
438   };
439   private boolean writable;
440
441   private boolean readable;
442
443   private String extensions;
444
445   private String name;
446
447   @Override
448   public boolean isComplexAlignFile()
449   {
450     return false;
451   }
452
453   @Override
454   public boolean isReadable()
455   {
456     return readable;
457   }
458
459   @Override
460   public boolean isWritable()
461   {
462     return writable;
463   }
464
465   /**
466    * Constructor
467    * 
468    * @param shortName
469    * @param extensions
470    *          comma-separated list of file extensions associated with the format
471    * @param isReadable
472    * @param isWritable
473    */
474   private FileFormat(String shortName, String extensions,
475           boolean isReadable, boolean isWritable)
476   {
477     this.name = shortName;
478     this.extensions = extensions;
479     this.readable = isReadable;
480     this.writable = isWritable;
481   }
482
483   @Override
484   public String getExtensions()
485   {
486     return extensions;
487   }
488
489   /**
490    * Answers the display name of the file format (as for example shown in menu
491    * options). This name should not be locale (language) dependent.
492    */
493   @Override
494   public String getName()
495   {
496     return name;
497   }
498
499   @Override
500   public boolean isTextFormat()
501   {
502     return true;
503   }
504
505   @Override
506   public boolean isStructureFile()
507   {
508     return false;
509   }
510
511   @Override
512   public boolean isTreeFile()
513   {
514     return false;
515   }
516
517   /**
518    * By default, answers true, indicating the format is one that can be
519    * identified by IdentifyFile. Formats that cannot be identified should
520    * override this method to return false.
521    */
522   public boolean isIdentifiable()
523   {
524     return true;
525   }
526 }