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