JAL-1260 FileFormat entry for EMBL/GENBANK: TODO: create tests and IdentifyFile routi...
[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.DBRefSource;
25 import jalview.datamodel.PDBEntry;
26 import jalview.ext.jmol.JmolParser;
27 import jalview.structure.StructureImportSettings;
28
29 import java.io.IOException;
30
31 public enum FileFormat implements FileFormatI
32 {
33   Fasta("Fasta", "fa, fasta, mfa, fastq", true, true)
34   {
35     @Override
36     public AlignmentFileReaderI getReader(FileParse source)
37             throws IOException
38     {
39       return new FastaFile(source);
40     }
41
42     @Override
43     public AlignmentFileWriterI getWriter(AlignmentI al)
44     {
45       return new FastaFile();
46     }
47   },
48   Pfam("PFAM", "pfam", true, true)
49   {
50     @Override
51     public AlignmentFileReaderI getReader(FileParse source)
52             throws IOException
53     {
54       return new PfamFile(source);
55     }
56
57     @Override
58     public AlignmentFileWriterI getWriter(AlignmentI al)
59     {
60       return new PfamFile();
61     }
62   },
63   Stockholm("Stockholm", "sto,stk", true, true)
64   {
65     @Override
66     public AlignmentFileReaderI getReader(FileParse source)
67             throws IOException
68     {
69       return new StockholmFile(source);
70     }
71
72     @Override
73     public AlignmentFileWriterI getWriter(AlignmentI al)
74     {
75       return new StockholmFile(al);
76     }
77
78   },
79
80   PIR("PIR", "pir", true, true)
81   {
82     @Override
83     public AlignmentFileReaderI getReader(FileParse source)
84             throws IOException
85     {
86       return new PIRFile(source);
87     }
88
89     @Override
90     public AlignmentFileWriterI getWriter(AlignmentI al)
91     {
92       return new PIRFile();
93     }
94   },
95   BLC("BLC", "BLC", true, true)
96   {
97     @Override
98     public AlignmentFileReaderI getReader(FileParse source)
99             throws IOException
100     {
101       return new BLCFile(source);
102     }
103
104     @Override
105     public AlignmentFileWriterI getWriter(AlignmentI al)
106     {
107       return new BLCFile();
108     }
109   },
110   AMSA("AMSA", "amsa", true, true)
111   {
112     @Override
113     public AlignmentFileReaderI getReader(FileParse source)
114             throws IOException
115     {
116       return new AMSAFile(source);
117     }
118
119     @Override
120     public AlignmentFileWriterI getWriter(AlignmentI al)
121     {
122       return new AMSAFile(al);
123     }
124   },
125   Html("HTML", "html", true, false)
126   {
127     @Override
128     public AlignmentFileReaderI getReader(FileParse source)
129             throws IOException
130     {
131       return new HtmlFile(source);
132     }
133
134     @Override
135     public AlignmentFileWriterI getWriter(AlignmentI al)
136     {
137       return new HtmlFile();
138     }
139
140     @Override
141     public boolean isComplexAlignFile()
142     {
143       return true;
144     }
145
146   },
147   Rnaml("RNAML", "xml,rnaml", true, false)
148   {
149     @Override
150     public AlignmentFileReaderI getReader(FileParse source)
151             throws IOException
152     {
153       return new RnamlFile(source);
154     }
155
156     @Override
157     public AlignmentFileWriterI getWriter(AlignmentI al)
158     {
159       return new RnamlFile();
160     }
161
162   },
163   Json("JSON", "json", true, true)
164   {
165     @Override
166     public AlignmentFileReaderI getReader(FileParse source)
167             throws IOException
168     {
169       return new JSONFile(source);
170     }
171
172     @Override
173     public AlignmentFileWriterI getWriter(AlignmentI al)
174     {
175       return new JSONFile();
176     }
177
178     @Override
179     public boolean isComplexAlignFile()
180     {
181       return true;
182     }
183
184   },
185   Pileup("PileUp", "pileup", true, true)
186   {
187     @Override
188     public AlignmentFileReaderI getReader(FileParse source)
189             throws IOException
190     {
191       return new PileUpfile(source);
192     }
193
194     @Override
195     public AlignmentFileWriterI getWriter(AlignmentI al)
196     {
197       return new PileUpfile();
198     }
199
200   },
201   MSF("MSF", "msf", true, true)
202   {
203     @Override
204     public AlignmentFileReaderI getReader(FileParse source)
205             throws IOException
206     {
207       return new MSFfile(source);
208     }
209
210     @Override
211     public AlignmentFileWriterI getWriter(AlignmentI al)
212     {
213       return new MSFfile();
214     }
215
216   },
217   Clustal("Clustal", "aln", true, true)
218   {
219     @Override
220     public AlignmentFileReaderI getReader(FileParse source)
221             throws IOException
222     {
223       return new ClustalFile(source);
224     }
225
226     @Override
227     public AlignmentFileWriterI getWriter(AlignmentI al)
228     {
229       return new ClustalFile();
230     }
231   },
232   Phylip("PHYLIP", "phy", true, true)
233   {
234     @Override
235     public AlignmentFileReaderI getReader(FileParse source)
236             throws IOException
237     {
238       return new PhylipFile(source);
239     }
240
241     @Override
242     public AlignmentFileWriterI getWriter(AlignmentI al)
243     {
244       return new PhylipFile();
245     }
246   },
247   GenBank("GenBank/ENA Flatfile","gb",false,false)
248   {
249     @Override
250     public AlignmentFileReaderI getReader(FileParse source)
251             throws IOException
252     {
253       // Always assume we import from EMBL for now
254       return new EmblFlatFile(source, DBRefSource.EMBL);
255     }
256     @Override
257     public AlignmentFileWriterI getWriter(AlignmentI al)
258     {
259       return null;
260     }
261   }
262   ,
263   Jnet("JnetFile", "", false, false)
264   {
265     @Override
266     public AlignmentFileReaderI getReader(FileParse source)
267             throws IOException
268     {
269       JPredFile af = new JPredFile(source);
270       af.removeNonSequences();
271       return af;
272     }
273
274     @Override
275     public AlignmentFileWriterI getWriter(AlignmentI al)
276     {
277       return null; // todo is this called?
278     }
279
280   },
281   Features("GFF or Jalview features", "gff2,gff3", true, false)
282   {
283     @Override
284     public AlignmentFileReaderI getReader(FileParse source)
285             throws IOException
286     {
287       return new FeaturesFile(source);
288     }
289
290     @Override
291     public AlignmentFileWriterI getWriter(AlignmentI al)
292     {
293       return new FeaturesFile();
294     }
295   },
296   ScoreMatrix("Substitution matrix", "", false, false)
297   {
298     @Override
299     public AlignmentFileReaderI getReader(FileParse source)
300             throws IOException
301     {
302       return new ScoreMatrixFile(source);
303     }
304
305     @Override
306     public AlignmentFileWriterI getWriter(AlignmentI al)
307     {
308       return null;
309     }
310   },
311   PDB("PDB", "pdb,ent", true, false)
312   {
313     @Override
314     public AlignmentFileReaderI getReader(FileParse source)
315             throws IOException
316     {
317       boolean isParseWithJMOL = StructureImportSettings
318               .getDefaultStructureFileFormat() != PDBEntry.Type.PDB;
319       if (isParseWithJMOL)
320       {
321         return new JmolParser(source);
322       }
323       else
324       {
325         StructureImportSettings.setShowSeqFeatures(true);
326         return new MCview.PDBfile(
327                 StructureImportSettings.isVisibleChainAnnotation(),
328                 StructureImportSettings.isProcessSecondaryStructure(),
329                 StructureImportSettings.isExternalSecondaryStructure(),
330                 source);
331       }
332     }
333
334     @Override
335     public AlignmentFileWriterI getWriter(AlignmentI al)
336     {
337       return new JmolParser(); // todo or null?
338     }
339
340     @Override
341     public boolean isStructureFile()
342     {
343       return true;
344     }
345   },
346   MMCif("mmCIF", "cif", true, false)
347   {
348     @Override
349     public AlignmentFileReaderI getReader(FileParse source)
350             throws IOException
351     {
352       return new JmolParser(source);
353     }
354
355     @Override
356     public AlignmentFileWriterI getWriter(AlignmentI al)
357     {
358       return new JmolParser(); // todo or null?
359     }
360
361     @Override
362     public boolean isStructureFile()
363     {
364       return true;
365     }
366   },
367   Jalview("Jalview", "jvp, jar", true, true)
368   {
369     @Override
370     public AlignmentFileReaderI getReader(FileParse source)
371             throws IOException
372     {
373       return null;
374     }
375
376     @Override
377     public AlignmentFileWriterI getWriter(AlignmentI al)
378     {
379       return null;
380     }
381
382     @Override
383     public boolean isTextFormat()
384     {
385       return false;
386     }
387
388     @Override
389     public boolean isIdentifiable()
390     {
391       return false;
392     }
393   };
394
395   private boolean writable;
396
397   private boolean readable;
398
399   private String extensions;
400
401   private String name;
402
403   @Override
404   public boolean isComplexAlignFile()
405   {
406     return false;
407   }
408
409   @Override
410   public boolean isReadable()
411   {
412     return readable;
413   }
414
415   @Override
416   public boolean isWritable()
417   {
418     return writable;
419   }
420
421   /**
422    * Constructor
423    * 
424    * @param shortName
425    * @param extensions
426    *          comma-separated list of file extensions associated with the format
427    * @param isReadable - can be recognised by IdentifyFile and imported with the given reader
428    * @param isWritable - can be exported with the returned writer
429    */
430   private FileFormat(String shortName, String extensions,
431           boolean isReadable, boolean isWritable)
432   {
433     this.name = shortName;
434     this.extensions = extensions;
435     this.readable = isReadable;
436     this.writable = isWritable;
437   }
438
439   @Override
440   public String getExtensions()
441   {
442     return extensions;
443   }
444
445   /**
446    * Answers the display name of the file format (as for example shown in menu
447    * options). This name should not be locale (language) dependent.
448    */
449   @Override
450   public String getName()
451   {
452     return name;
453   }
454
455   @Override
456   public boolean isTextFormat()
457   {
458     return true;
459   }
460
461   @Override
462   public boolean isStructureFile()
463   {
464     return false;
465   }
466
467   /**
468    * By default, answers true, indicating the format is one that can be
469    * identified by IdentifyFile. Formats that cannot be identified should
470    * override this method to return false.
471    */
472   public boolean isIdentifiable()
473   {
474     return true;
475   }
476 }