Further tweaks to get tests passing
[jalview.git] / src / jalview / io / FileFormat.java
1 package jalview.io;
2
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.PDBEntry;
5 import jalview.ext.jmol.JmolParser;
6 import jalview.structure.StructureImportSettings;
7
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 public enum FileFormat implements FileFormatI
15 {
16   Fasta("Fasta", "fa, fasta, mfa, fastq", true, true)
17   {
18     @Override
19     public AlignmentFileI getAlignmentFile(String inFile,
20             DataSourceType sourceType) throws IOException
21     {
22       return new FastaFile(inFile, sourceType);
23     }
24
25     @Override
26     public AlignmentFileI getAlignmentFile(FileParse source)
27             throws IOException
28     {
29       return new FastaFile(source);
30     }
31
32     @Override
33     public AlignmentFileI getAlignmentFile(AlignmentI al)
34     {
35       return new FastaFile();
36     }
37   },
38   Pfam("PFAM", "pfam", true, true)
39   {
40     @Override
41     public AlignmentFileI getAlignmentFile(String inFile,
42             DataSourceType sourceType) throws IOException
43     {
44       return new PfamFile(inFile, sourceType);
45     }
46
47     @Override
48     public AlignmentFileI getAlignmentFile(FileParse source)
49             throws IOException
50     {
51       return new PfamFile(source);
52     }
53
54     @Override
55     public AlignmentFileI getAlignmentFile(AlignmentI al)
56     {
57       return new PfamFile();
58     }
59   },
60   Stockholm("Stockholm", "sto,stk", true, true)
61   {
62     @Override
63     public AlignmentFileI getAlignmentFile(String inFile,
64             DataSourceType sourceType) throws IOException
65     {
66       return new StockholmFile(inFile, sourceType);
67     }
68     @Override
69     public AlignmentFileI getAlignmentFile(FileParse source)
70             throws IOException
71     {
72       return new StockholmFile(source);
73     }
74
75     @Override
76     public AlignmentFileI getAlignmentFile(AlignmentI al)
77     {
78       return new StockholmFile(al);
79     }
80
81   },
82
83   PIR("PIR", "pir", true, true)
84   {
85     @Override
86     public AlignmentFileI getAlignmentFile(String inFile,
87             DataSourceType sourceType) throws IOException
88     {
89       return new PIRFile(inFile, sourceType);
90     }
91     @Override
92     public AlignmentFileI getAlignmentFile(FileParse source)
93             throws IOException
94     {
95       return new PIRFile(source);
96     }
97
98     @Override
99     public AlignmentFileI getAlignmentFile(AlignmentI al)
100     {
101       return new PIRFile();
102     }
103   },
104   BLC("BLC", "BLC", true, true)
105   {
106     @Override
107     public AlignmentFileI getAlignmentFile(String inFile,
108             DataSourceType sourceType) throws IOException
109     {
110       return new BLCFile(inFile, sourceType);
111     }    @Override
112     public AlignmentFileI getAlignmentFile(FileParse source)
113             throws IOException
114     {
115       return new BLCFile(source);
116     }
117
118     @Override
119     public AlignmentFileI getAlignmentFile(AlignmentI al)
120     {
121       return new BLCFile();
122     }
123   },
124   AMSA("AMSA", "amsa", true, true)
125   {
126     @Override
127     public AlignmentFileI getAlignmentFile(String inFile,
128             DataSourceType sourceType) throws IOException
129     {
130       return new AMSAFile(inFile, sourceType);
131     }
132
133     @Override
134     public AlignmentFileI getAlignmentFile(FileParse source)
135             throws IOException
136     {
137       return new AMSAFile(source);
138     }
139
140     @Override
141     public AlignmentFileI getAlignmentFile(AlignmentI al)
142     {
143       return new AMSAFile(al);
144     }
145   },
146   Html("HTML", "html", true, false)
147   {
148     @Override
149     public AlignmentFileI getAlignmentFile(String inFile,
150             DataSourceType sourceType) throws IOException
151     {
152       return new HtmlFile(inFile, sourceType);
153     }    @Override
154     public AlignmentFileI getAlignmentFile(FileParse source)
155             throws IOException
156     {
157       return new HtmlFile(source);
158     }
159
160     @Override
161     public AlignmentFileI getAlignmentFile(AlignmentI al)
162     {
163       return new HtmlFile();
164     }
165
166     @Override
167     public boolean isComplexAlignFile()
168     {
169       return true;
170     }
171
172   },
173   Rnaml("RNAML", "xml,rnaml", true, false)
174   {
175     @Override
176     public AlignmentFileI getAlignmentFile(String inFile,
177             DataSourceType sourceType) throws IOException
178     {
179       return new RnamlFile(inFile, sourceType);
180     }    @Override
181     public AlignmentFileI getAlignmentFile(FileParse source)
182             throws IOException
183     {
184       return new RnamlFile(source);
185     }
186
187     @Override
188     public AlignmentFileI getAlignmentFile(AlignmentI al)
189     {
190       return new RnamlFile();
191     }
192
193   },
194   Json("JSON","json", true, true)
195   {
196     @Override
197     public AlignmentFileI getAlignmentFile(String inFile,
198             DataSourceType sourceType) throws IOException
199     {
200       return new JSONFile(inFile, sourceType);
201     }    @Override
202     public AlignmentFileI getAlignmentFile(FileParse source)
203             throws IOException
204     {
205       return new JSONFile(source);
206     }
207
208     @Override
209     public AlignmentFileI getAlignmentFile(AlignmentI al)
210     {
211       return new JSONFile();
212     }
213
214     @Override
215     public boolean isComplexAlignFile()
216     {
217       return true;
218     }
219
220   },
221   Pileup("PileUp", "pileup", false, true)
222   {
223     @Override
224     public AlignmentFileI getAlignmentFile(String inFile,
225             DataSourceType sourceType) throws IOException
226     {
227       return new PileUpfile(inFile, sourceType);
228     }    @Override
229     public AlignmentFileI getAlignmentFile(FileParse source)
230             throws IOException
231     {
232       return new PileUpfile(source);
233     }
234
235     @Override
236     public AlignmentFileI getAlignmentFile(AlignmentI al)
237     {
238       return new PileUpfile();
239     }
240
241   },
242   MSF("MSF", "msf", true, true)
243   {
244     @Override
245     public AlignmentFileI getAlignmentFile(String inFile,
246             DataSourceType sourceType) throws IOException
247     {
248       return new MSFfile(inFile, sourceType);
249     }    @Override
250     public AlignmentFileI getAlignmentFile(FileParse source)
251             throws IOException
252     {
253       return new MSFfile(source);
254     }
255
256     @Override
257     public AlignmentFileI getAlignmentFile(AlignmentI al)
258     {
259       return new MSFfile();
260     }
261
262   },
263   Clustal("Clustal", "aln", true, true)
264   {
265     @Override
266     public AlignmentFileI getAlignmentFile(String inFile,
267             DataSourceType sourceType) throws IOException
268     {
269       return new ClustalFile(inFile, sourceType);
270     }    @Override
271     public AlignmentFileI getAlignmentFile(FileParse source)
272             throws IOException
273     {
274       return new ClustalFile(source);
275     }
276
277     @Override
278     public AlignmentFileI getAlignmentFile(AlignmentI al)
279     {
280       return new ClustalFile();
281     }
282   },
283   Phylip("PHYLIP", "phy", true, true)
284   {
285     @Override
286     public AlignmentFileI getAlignmentFile(String inFile,
287             DataSourceType sourceType) throws IOException
288     {
289       return new PhylipFile(inFile, sourceType);
290     }
291
292     @Override
293     public AlignmentFileI getAlignmentFile(FileParse source)
294             throws IOException
295     {
296       return new PhylipFile(source);
297     }
298
299     @Override
300     public AlignmentFileI getAlignmentFile(AlignmentI al)
301     {
302       return new PhylipFile();
303     }
304   },
305   Jnet("JnetFile", "", false, false)
306   {
307     @Override
308     public AlignmentFileI getAlignmentFile(String inFile,
309             DataSourceType sourceType) throws IOException
310     {
311       JPredFile af = new JPredFile(inFile, sourceType);
312       af.removeNonSequences();
313       return af;
314     }
315
316     @Override
317     public AlignmentFileI getAlignmentFile(FileParse source)
318             throws IOException
319     {
320       JPredFile af = new JPredFile(source);
321       af.removeNonSequences();
322       return af;
323     }
324
325     @Override
326     public AlignmentFileI getAlignmentFile(AlignmentI al)
327     {
328       return null; // todo is this called?
329     }
330
331   },
332   Features("GFF or Jalview features", "gff2,gff3", false, false)
333   {
334     @Override
335     public AlignmentFileI getAlignmentFile(String inFile,
336             DataSourceType sourceType) throws IOException
337     {
338       return new FeaturesFile(true, inFile, sourceType);
339     }
340
341     @Override
342     public AlignmentFileI getAlignmentFile(FileParse source)
343             throws IOException
344     {
345       return new FeaturesFile(source);
346     }
347
348     @Override
349     public AlignmentFileI getAlignmentFile(AlignmentI al)
350     {
351       return new FeaturesFile();
352     }
353   },
354   PDB("PDB", "", false, false)
355   {
356
357     @Override
358     public AlignmentFileI getAlignmentFile(String inFile,
359             DataSourceType sourceType) throws IOException
360     {
361       // TODO obtain config value from preference settings.
362       // Set value to 'true' to test PDB processing with Jmol: JAL-1213
363       boolean isParseWithJMOL = StructureImportSettings
364               .getDefaultStructureFileFormat() != PDBEntry.Type.PDB;
365       if (isParseWithJMOL)
366       {
367         return new JmolParser(
368                 StructureImportSettings.isVisibleChainAnnotation(),
369                 StructureImportSettings.isProcessSecondaryStructure(),
370                 StructureImportSettings.isExternalSecondaryStructure(),
371                 inFile,
372                 sourceType);
373       }
374       else
375       {
376         StructureImportSettings.setShowSeqFeatures(true);
377         return new MCview.PDBfile(
378                 StructureImportSettings.isVisibleChainAnnotation(),
379                 StructureImportSettings.isProcessSecondaryStructure(),
380                 StructureImportSettings.isExternalSecondaryStructure(),
381                 inFile,
382                 sourceType);
383       }
384     }
385
386     @Override
387     public AlignmentFileI getAlignmentFile(FileParse source)
388             throws IOException
389     {
390       boolean isParseWithJMOL = StructureImportSettings
391               .getDefaultStructureFileFormat() != PDBEntry.Type.PDB;
392       if (isParseWithJMOL)
393       {
394         return new JmolParser(
395                 StructureImportSettings.isVisibleChainAnnotation(),
396                 StructureImportSettings.isProcessSecondaryStructure(),
397                 StructureImportSettings.isExternalSecondaryStructure(),
398                 source);
399       }
400       else
401       {
402         StructureImportSettings.setShowSeqFeatures(true);
403         return new MCview.PDBfile(
404                 StructureImportSettings.isVisibleChainAnnotation(),
405                 StructureImportSettings.isProcessSecondaryStructure(),
406                 StructureImportSettings.isExternalSecondaryStructure(),
407                 source);
408       }
409     }
410
411     @Override
412     public AlignmentFileI getAlignmentFile(AlignmentI al)
413     {
414       return new JmolParser(); // todo or null?
415     }
416   },
417   MMCif("mmCIF", "cif", false, false)
418   {
419
420     @Override
421     public AlignmentFileI getAlignmentFile(String inFile,
422             DataSourceType sourceType) throws IOException
423     {
424       return new JmolParser(
425               StructureImportSettings.isVisibleChainAnnotation(),
426               StructureImportSettings.isProcessSecondaryStructure(),
427               StructureImportSettings.isExternalSecondaryStructure(),
428               inFile, sourceType);
429     }
430
431     @Override
432     public AlignmentFileI getAlignmentFile(FileParse source)
433             throws IOException
434     {
435       return new JmolParser(
436               StructureImportSettings.isVisibleChainAnnotation(),
437               StructureImportSettings.isProcessSecondaryStructure(),
438               StructureImportSettings.isExternalSecondaryStructure(),
439               source);
440     }
441
442     @Override
443     public AlignmentFileI getAlignmentFile(AlignmentI al)
444     {
445       return new JmolParser(); // todo or null?
446     }
447   },
448   Jalview("Jalview", "jar,jvp", true, false)
449   {
450
451     @Override
452     public AlignmentFileI getAlignmentFile(String inFile,
453             DataSourceType sourceType) throws IOException
454     {
455       return null;
456     }
457
458     @Override
459     public AlignmentFileI getAlignmentFile(FileParse source)
460             throws IOException
461     {
462       return null;
463     }
464
465     @Override
466     public AlignmentFileI getAlignmentFile(AlignmentI al)
467     {
468       return null;
469     }
470   };
471
472   /**
473    * A lookup map of enums by upper-cased name
474    */
475   private static Map<String, FileFormat> names;
476   static
477   {
478     names = new HashMap<String, FileFormat>();
479     for (FileFormat format : FileFormat.values())
480     {
481       names.put(format.toString().toUpperCase(), format);
482     }
483   }
484
485   private boolean writable;
486
487   private boolean readable;
488
489   private String extensions;
490
491   private String name;
492
493   /**
494    * Answers a list of writeable file formats (as string, corresponding to the
495    * toString() and forName() methods)
496    * 
497    * @return
498    */
499   public static List<String> getWritableFormats()
500   {
501     List<String> l = new ArrayList<String>();
502     for (FileFormatI ff : values())
503     {
504       if (ff.isWritable())
505       {
506         l.add(ff.toString());
507       }
508     }
509     return l;
510   }
511
512   /**
513    * Answers a list of readable file formats (as string, corresponding to the
514    * toString() and forName() methods)
515    * 
516    * @return
517    */
518   public static List<String> getReadableFormats()
519   {
520     List<String> l = new ArrayList<String>();
521     for (FileFormatI ff : values())
522     {
523       if (ff.isReadable())
524       {
525         l.add(ff.toString());
526       }
527     }
528     return l;
529   }
530
531   @Override
532   public boolean isComplexAlignFile()
533   {
534     return false;
535   }
536
537   /**
538    * Returns the file format with the given name, or null if format is null or
539    * invalid. Unlike valueOf(), this is not case-sensitive, to be kind to
540    * writers of javascript.
541    * 
542    * @param format
543    * @return
544    */
545   public static FileFormatI forName(String format)
546   {
547     // or could store format.getShortDescription().toUpperCase()
548     // in order to decouple 'given name' from enum name
549     return format == null ? null : names.get(format.toUpperCase());
550   }
551
552   @Override
553   public boolean isReadable()
554   {
555     return readable;
556   }
557
558   @Override
559   public boolean isWritable()
560   {
561     return writable;
562   }
563
564   /**
565    * Constructor
566    * 
567    * @param shortName
568    * @param extensions
569    *          comma-separated list of file extensions associated with the format
570    * @param isReadable
571    * @param isWritable
572    */
573   private FileFormat(String shortName, String extensions,
574           boolean isReadable, boolean isWritable)
575   {
576     this.name = shortName;
577     this.extensions = extensions;
578     this.readable = isReadable;
579     this.writable = isWritable;
580   }
581
582   @Override
583   public String getExtensions()
584   {
585     return extensions;
586   }
587
588   @Override
589   public String toString()
590   {
591     return name;
592   }
593
594   @Override
595   public AlignmentFileI getAlignmentFile()
596   {
597     return getAlignmentFile((AlignmentI) null);
598   }
599 }