JAL-4021 todo & fix FileFormat tests
[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 java.io.IOException;
24
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefSource;
27 import jalview.datamodel.PDBEntry;
28 import jalview.ext.forester.io.PhyloXmlFile;
29 import jalview.ext.jmol.JmolParser;
30 import jalview.structure.StructureImportSettings;
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   GenBank("GenBank Flatfile", "gb, gbk", true, false)
249   {
250     @Override
251     public AlignmentFileReaderI getReader(FileParse source)
252             throws IOException
253     {
254       return new GenBankFile(source, "GenBank");
255     }
256
257     @Override
258     public AlignmentFileWriterI getWriter(AlignmentI al)
259     {
260       return null;
261     }
262   },
263   Embl("ENA Flatfile", "txt", true, false)
264   {
265     @Override
266     public AlignmentFileReaderI getReader(FileParse source)
267             throws IOException
268     {
269       // Always assume we import from EMBL for now
270       return new EmblFlatFile(source, DBRefSource.EMBL);
271     }
272
273     @Override
274     public AlignmentFileWriterI getWriter(AlignmentI al)
275     {
276       return null;
277     }
278   },
279   Jnet("JnetFile", "", false, false)
280   {
281     @Override
282     public AlignmentFileReaderI getReader(FileParse source)
283             throws IOException
284     {
285       JPredFile af = new JPredFile(source);
286       af.removeNonSequences();
287       return af;
288     }
289
290     @Override
291     public AlignmentFileWriterI getWriter(AlignmentI al)
292     {
293       return null; // todo is this called?
294     }
295
296   },
297   Features("GFF or Jalview features", "gff2,gff3", true, false)
298   {
299     @Override
300     public AlignmentFileReaderI getReader(FileParse source)
301             throws IOException
302     {
303       return new FeaturesFile(source);
304     }
305
306     @Override
307     public AlignmentFileWriterI getWriter(AlignmentI al)
308     {
309       return new FeaturesFile();
310     }
311   },
312   ScoreMatrix("Substitution matrix", "", false, false)
313   {
314     @Override
315     public AlignmentFileReaderI getReader(FileParse source)
316             throws IOException
317     {
318       return new ScoreMatrixFile(source);
319     }
320
321     @Override
322     public AlignmentFileWriterI getWriter(AlignmentI al)
323     {
324       return null;
325     }
326   },
327   PDB("PDB", "pdb,ent", true, false)
328   {
329     @Override
330     public AlignmentFileReaderI getReader(FileParse source)
331             throws IOException
332     {
333       boolean isParseWithJMOL = StructureImportSettings
334               .getDefaultStructureFileFormat() != PDBEntry.Type.PDB;
335       if (isParseWithJMOL)
336       {
337         return new JmolParser(source);
338       }
339       else
340       {
341         StructureImportSettings.setShowSeqFeatures(true);
342         return new mc_view.PDBfile(
343                 StructureImportSettings.isVisibleChainAnnotation(),
344                 StructureImportSettings.isProcessSecondaryStructure(),
345                 StructureImportSettings.isExternalSecondaryStructure(),
346                 source);
347       }
348     }
349
350     @Override
351     public AlignmentFileWriterI getWriter(AlignmentI al)
352     {
353       return new JmolParser(); // todo or null?
354     }
355
356     @Override
357     public boolean isStructureFile()
358     {
359       return true;
360     }
361   },
362   MMCif("mmCIF", "cif", true, false)
363   {
364     @Override
365     public AlignmentFileReaderI getReader(FileParse source)
366             throws IOException
367     {
368       return new JmolParser(source);
369     }
370
371     @Override
372     public AlignmentFileWriterI getWriter(AlignmentI al)
373     {
374       return new JmolParser(); // todo or null?
375     }
376
377     @Override
378     public boolean isStructureFile()
379     {
380       return true;
381     }
382   },
383   Jalview("Jalview", "jvp, jar", true, true)
384   {
385     @Override
386     public AlignmentFileReaderI getReader(FileParse source)
387             throws IOException
388     {
389       return null;
390     }
391
392     @Override
393     public AlignmentFileWriterI getWriter(AlignmentI al)
394     {
395       return null;
396     }
397
398     @Override
399     public boolean isTextFormat()
400     {
401       return false;
402     }
403
404     @Override
405     public boolean isIdentifiable()
406     {
407       return true;
408     }
409   },
410   // Nexus("Nexus", "nex,nexus,nx,tre", true, true)
411   // {
412   //
413   // @Override
414   // public AlignmentFileReaderI getReader(FileParse source)
415   // throws IOException
416   // {
417   // return new NexusFile(source);
418   // }
419   //
420   // @Override
421   // public AlignmentFileWriterI getWriter(AlignmentI al)
422   // {
423   // // handle within Aptx?
424   // return null;
425   // }
426   //
427   // @Override
428   // public boolean isTextFormat()
429   // {
430   // return true;
431   // }
432   //
433   // @Override
434   // public boolean isTreeFile()
435   // {
436   // return true;
437   // }
438   //
439   // },
440   PhyloXML("PhyloXML", "phyloxml,phylo.xml,pxml", true, false)
441   {
442
443     @Override
444     public AlignmentFileReaderI getReader(FileParse source)
445             throws IOException
446     {
447       return new PhyloXmlFile(source);
448     }
449
450     @Override
451     public AlignmentFileWriterI getWriter(AlignmentI al)
452     {
453       // TODO: JAL-4021
454       // handle within Aptx?
455       return null;
456     }
457
458     @Override
459     public boolean isTextFormat()
460     {
461       return true;
462     }
463
464     @Override
465     public boolean isTreeFile()
466     {
467       return true;
468     }
469
470   };
471   private boolean writable;
472
473   private boolean readable;
474
475   private String extensions;
476
477   private String name;
478
479   @Override
480   public boolean isComplexAlignFile()
481   {
482     return false;
483   }
484
485   @Override
486   public boolean isReadable()
487   {
488     return readable;
489   }
490
491   @Override
492   public boolean isWritable()
493   {
494     return writable;
495   }
496
497   /**
498    * Constructor
499    * 
500    * @param shortName
501    * @param extensions
502    *          comma-separated list of file extensions associated with the format
503    * @param isReadable
504    *          - can be recognised by IdentifyFile and imported with the given
505    *          reader
506    * @param isWritable
507    *          - can be exported with the returned writer
508    */
509   private FileFormat(String shortName, String extensions,
510           boolean isReadable, boolean isWritable)
511   {
512     this.name = shortName;
513     this.extensions = extensions;
514     this.readable = isReadable;
515     this.writable = isWritable;
516   }
517
518   @Override
519   public String getExtensions()
520   {
521     return extensions;
522   }
523
524   /**
525    * Answers the display name of the file format (as for example shown in menu
526    * options). This name should not be locale (language) dependent.
527    */
528   @Override
529   public String getName()
530   {
531     return name;
532   }
533
534   @Override
535   public boolean isTextFormat()
536   {
537     return true;
538   }
539
540   @Override
541   public boolean isStructureFile()
542   {
543     return false;
544   }
545
546   @Override
547   public boolean isTreeFile()
548   {
549     return false;
550   }
551
552   /**
553    * By default, answers true, indicating the format is one that can be
554    * identified by IdentifyFile. Formats that cannot be identified should
555    * override this method to return false.
556    */
557   public boolean isIdentifiable()
558   {
559     return true;
560   }
561 }