Revert "JAL-2799 all tabs now get mouse listeners and Jalview switches tree view"
[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.jmol.JmolParser;
29 import jalview.structure.StructureImportSettings;
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 Flatfile", "gb, gbk", true, false)
248   {
249     @Override
250     public AlignmentFileReaderI getReader(FileParse source)
251             throws IOException
252     {
253       return new GenBankFile(source, "GenBank");
254     }
255
256     @Override
257     public AlignmentFileWriterI getWriter(AlignmentI al)
258     {
259       return null;
260     }
261   },
262   Embl("ENA Flatfile", "txt", true, false)
263   {
264     @Override
265     public AlignmentFileReaderI getReader(FileParse source)
266             throws IOException
267     {
268       // Always assume we import from EMBL for now
269       return new EmblFlatFile(source, DBRefSource.EMBL);
270     }
271
272     @Override
273     public AlignmentFileWriterI getWriter(AlignmentI al)
274     {
275       return null;
276     }
277   },
278   Jnet("JnetFile", "", false, false)
279   {
280     @Override
281     public AlignmentFileReaderI getReader(FileParse source)
282             throws IOException
283     {
284       JPredFile af = new JPredFile(source);
285       af.removeNonSequences();
286       return af;
287     }
288
289     @Override
290     public AlignmentFileWriterI getWriter(AlignmentI al)
291     {
292       return null; // todo is this called?
293     }
294
295   },
296   Features("GFF or Jalview features", "gff2,gff3", true, false)
297   {
298     @Override
299     public AlignmentFileReaderI getReader(FileParse source)
300             throws IOException
301     {
302       return new FeaturesFile(source);
303     }
304
305     @Override
306     public AlignmentFileWriterI getWriter(AlignmentI al)
307     {
308       return new FeaturesFile();
309     }
310   },
311   ScoreMatrix("Substitution matrix", "", false, false)
312   {
313     @Override
314     public AlignmentFileReaderI getReader(FileParse source)
315             throws IOException
316     {
317       return new ScoreMatrixFile(source);
318     }
319
320     @Override
321     public AlignmentFileWriterI getWriter(AlignmentI al)
322     {
323       return null;
324     }
325   },
326   PDB("PDB", "pdb,ent", true, false)
327   {
328     @Override
329     public AlignmentFileReaderI getReader(FileParse source)
330             throws IOException
331     {
332       boolean isParseWithJMOL = StructureImportSettings
333               .getDefaultStructureFileFormat() != PDBEntry.Type.PDB;
334       if (isParseWithJMOL)
335       {
336         return new JmolParser(source);
337       }
338       else
339       {
340         StructureImportSettings.setShowSeqFeatures(true);
341         return new mc_view.PDBfile(
342                 StructureImportSettings.isVisibleChainAnnotation(),
343                 StructureImportSettings.isProcessSecondaryStructure(),
344                 StructureImportSettings.isExternalSecondaryStructure(),
345                 source);
346       }
347     }
348
349     @Override
350     public AlignmentFileWriterI getWriter(AlignmentI al)
351     {
352       return new JmolParser(); // todo or null?
353     }
354
355     @Override
356     public boolean isStructureFile()
357     {
358       return true;
359     }
360   },
361   MMCif("mmCIF", "cif", true, false)
362   {
363     @Override
364     public AlignmentFileReaderI getReader(FileParse source)
365             throws IOException
366     {
367       return new JmolParser(source);
368     }
369
370     @Override
371     public AlignmentFileWriterI getWriter(AlignmentI al)
372     {
373       return new JmolParser(); // todo or null?
374     }
375
376     @Override
377     public boolean isStructureFile()
378     {
379       return true;
380     }
381   },
382   Jalview("Jalview", "jvp, jar", true, true)
383   {
384     @Override
385     public AlignmentFileReaderI getReader(FileParse source)
386             throws IOException
387     {
388       return null;
389     }
390
391     @Override
392     public AlignmentFileWriterI getWriter(AlignmentI al)
393     {
394       return null;
395     }
396
397     @Override
398     public boolean isTextFormat()
399     {
400       return false;
401     }
402
403     @Override
404     public boolean isIdentifiable()
405     {
406       return true;
407     }
408   };
409
410   private boolean writable;
411
412   private boolean readable;
413
414   private String extensions;
415
416   private String name;
417
418   @Override
419   public boolean isComplexAlignFile()
420   {
421     return false;
422   }
423
424   @Override
425   public boolean isReadable()
426   {
427     return readable;
428   }
429
430   @Override
431   public boolean isWritable()
432   {
433     return writable;
434   }
435
436   /**
437    * Constructor
438    * 
439    * @param shortName
440    * @param extensions
441    *          comma-separated list of file extensions associated with the format
442    * @param isReadable
443    *          - can be recognised by IdentifyFile and imported with the given
444    *          reader
445    * @param isWritable
446    *          - can be exported with the returned writer
447    */
448   private FileFormat(String shortName, String extensions,
449           boolean isReadable, boolean isWritable)
450   {
451     this.name = shortName;
452     this.extensions = extensions;
453     this.readable = isReadable;
454     this.writable = isWritable;
455   }
456
457   @Override
458   public String getExtensions()
459   {
460     return extensions;
461   }
462
463   /**
464    * Answers the display name of the file format (as for example shown in menu
465    * options). This name should not be locale (language) dependent.
466    */
467   @Override
468   public String getName()
469   {
470     return name;
471   }
472
473   @Override
474   public boolean isTextFormat()
475   {
476     return true;
477   }
478
479   @Override
480   public boolean isStructureFile()
481   {
482     return false;
483   }
484
485   /**
486    * By default, answers true, indicating the format is one that can be
487    * identified by IdentifyFile. Formats that cannot be identified should
488    * override this method to return false.
489    */
490   public boolean isIdentifiable()
491   {
492     return true;
493   }
494 }