2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
22 import jalview.datamodel.*;
25 * A low level class for alignment and feature IO with alignment formatting
26 * methods used by both applet and application for generating flat alignment
27 * files. It also holds the lists of magic format names that the applet and
28 * application will allow the user to read or write files with.
33 public class AppletFormatAdapter
36 * List of valid format strings used in the isValidFormat method
38 public static final String[] READABLE_FORMATS = new String[]
39 { "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH",
40 "PDB", "JnetFile" }; // , "SimpleBLAST" };
43 * List of valid format strings for use by callers of the formatSequences
46 public static final String[] WRITEABLE_FORMATS = new String[]
47 { "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "AMSA" };
50 * List of extensions corresponding to file format types in WRITABLE_FNAMES
51 * that are writable by the application.
53 public static final String[] WRITABLE_EXTENSIONS = new String[]
54 { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "jar" };
57 * List of writable formats by the application. Order must correspond with the
58 * WRITABLE_EXTENSIONS list of formats.
60 public static final String[] WRITABLE_FNAMES = new String[]
61 { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview" };
64 * List of readable format file extensions by application in order
65 * corresponding to READABLE_FNAMES
67 public static final String[] READABLE_EXTENSIONS = new String[]
68 { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "jar" }; // ,
73 * List of readable formats by application in order corresponding to
76 public static final String[] READABLE_FNAMES = new String[]
77 { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview" };// ,
81 public static String INVALID_CHARACTERS = "Contains invalid characters";
83 // TODO: make these messages dynamic
84 public static String SUPPORTED_FORMATS = "Formats currently supported are\n"
85 + prettyPrint(READABLE_FORMATS);
90 * @return grammatically correct(ish) list consisting of els elements.
92 public static String prettyPrint(String[] els)
94 StringBuffer list = new StringBuffer();
95 for (int i = 0, iSize = els.length - 1; i < iSize; i++)
100 list.append(" and " + els[els.length - 1] + ".");
101 return list.toString();
104 public static String FILE = "File";
106 public static String URL = "URL";
108 public static String PASTE = "Paste";
110 public static String CLASSLOADER = "ClassLoader";
112 AlignFile afile = null;
117 * check that this format is valid for reading
120 * a format string to be compared with READABLE_FORMATS
121 * @return true if format is readable
123 public static final boolean isValidFormat(String format)
125 return isValidFormat(format, false);
129 * validate format is valid for IO
132 * a format string to be compared with either READABLE_FORMATS or
135 * when true, format is checked for containment in WRITEABLE_FORMATS
136 * @return true if format is valid
138 public static final boolean isValidFormat(String format,
141 boolean valid = false;
142 String[] format_list = (forwriting) ? WRITEABLE_FORMATS
144 for (int i = 0; i < format_list.length; i++)
146 if (format_list[i].equalsIgnoreCase(format))
156 * Constructs the correct filetype parser for a characterised datasource
163 * File format of data provided by datasource
165 * @return DOCUMENT ME!
167 public Alignment readFile(String inFile, String type, String format)
168 throws java.io.IOException
170 // TODO: generalise mapping between format string and io. class instances
171 // using Constructor.invoke reflection
172 this.inFile = inFile;
175 if (format.equals("FASTA"))
177 afile = new FastaFile(inFile, type);
179 else if (format.equals("MSF"))
181 afile = new MSFfile(inFile, type);
183 else if (format.equals("PileUp"))
185 afile = new PileUpfile(inFile, type);
187 else if (format.equals("CLUSTAL"))
189 afile = new ClustalFile(inFile, type);
191 else if (format.equals("BLC"))
193 afile = new BLCFile(inFile, type);
195 else if (format.equals("PIR"))
197 afile = new PIRFile(inFile, type);
199 else if (format.equals("PFAM"))
201 afile = new PfamFile(inFile, type);
203 else if (format.equals("JnetFile"))
205 afile = new JPredFile(inFile, type);
206 ((JPredFile) afile).removeNonSequences();
208 else if (format.equals("PDB"))
210 afile = new MCview.PDBfile(inFile, type);
212 else if (format.equals("STH"))
214 afile = new StockholmFile(inFile, type);
216 else if (format.equals("SimpleBLAST"))
218 afile = new SimpleBlastFile(inFile, type);
221 Alignment al = new Alignment(afile.getSeqsAsArray());
223 afile.addAnnotations(al);
226 } catch (Exception e)
229 System.err.println("Failed to read alignment using the '" + format
230 + "' reader.\n" + e);
232 if (e.getMessage() != null
233 && e.getMessage().startsWith(INVALID_CHARACTERS))
235 throw new java.io.IOException(e.getMessage());
238 // Finally test if the user has pasted just the sequence, no id
239 if (type.equalsIgnoreCase("Paste"))
243 // Possible sequence is just residues with no label
244 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
245 Alignment al = new Alignment(afile.getSeqsAsArray());
246 afile.addAnnotations(al);
249 } catch (Exception ex)
251 if (ex.toString().startsWith(INVALID_CHARACTERS))
253 throw new java.io.IOException(e.getMessage());
256 ex.printStackTrace();
260 // If we get to this stage, the format was not supported
261 throw new java.io.IOException(SUPPORTED_FORMATS);
266 * Constructs the correct filetype parser for an already open datasource
269 * an existing datasource
271 * File format of data that will be provided by datasource
273 * @return DOCUMENT ME!
275 public Alignment readFromFile(FileParse source, String format)
276 throws java.io.IOException
278 // TODO: generalise mapping between format string and io. class instances
279 // using Constructor.invoke reflection
280 // This is exactly the same as the readFile method except we substitute
281 // 'inFile, type' with 'source'
282 this.inFile = source.getInFile();
283 String type = source.type;
286 if (format.equals("FASTA"))
288 afile = new FastaFile(source);
290 else if (format.equals("MSF"))
292 afile = new MSFfile(source);
294 else if (format.equals("PileUp"))
296 afile = new PileUpfile(source);
298 else if (format.equals("CLUSTAL"))
300 afile = new ClustalFile(source);
302 else if (format.equals("BLC"))
304 afile = new BLCFile(source);
306 else if (format.equals("PIR"))
308 afile = new PIRFile(source);
310 else if (format.equals("PFAM"))
312 afile = new PfamFile(source);
314 else if (format.equals("JnetFile"))
316 afile = new JPredFile(source);
317 ((JPredFile) afile).removeNonSequences();
319 else if (format.equals("PDB"))
321 afile = new MCview.PDBfile(source);
323 else if (format.equals("STH"))
325 afile = new StockholmFile(source);
327 else if (format.equals("SimpleBLAST"))
329 afile = new SimpleBlastFile(source);
332 Alignment al = new Alignment(afile.getSeqsAsArray());
334 afile.addAnnotations(al);
337 } catch (Exception e)
340 System.err.println("Failed to read alignment using the '" + format
341 + "' reader.\n" + e);
343 if (e.getMessage() != null
344 && e.getMessage().startsWith(INVALID_CHARACTERS))
346 throw new java.io.IOException(e.getMessage());
349 // Finally test if the user has pasted just the sequence, no id
350 if (type.equalsIgnoreCase("Paste"))
354 // Possible sequence is just residues with no label
355 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
356 Alignment al = new Alignment(afile.getSeqsAsArray());
357 afile.addAnnotations(al);
360 } catch (Exception ex)
362 if (ex.toString().startsWith(INVALID_CHARACTERS))
364 throw new java.io.IOException(e.getMessage());
367 ex.printStackTrace();
371 // If we get to this stage, the format was not supported
372 throw new java.io.IOException(SUPPORTED_FORMATS);
377 * Construct an output class for an alignment in a particular filetype TODO:
378 * allow caller to detect errors and warnings encountered when generating
382 * string name of alignment format
384 * the alignment to be written out
386 * passed to AlnFile class controls whether /START-END is added to
389 * @return alignment flat file contents
391 public String formatSequences(String format, AlignmentI alignment,
396 AlignFile afile = null;
398 if (format.equalsIgnoreCase("FASTA"))
400 afile = new FastaFile();
402 else if (format.equalsIgnoreCase("MSF"))
404 afile = new MSFfile();
406 else if (format.equalsIgnoreCase("PileUp"))
408 afile = new PileUpfile();
410 else if (format.equalsIgnoreCase("CLUSTAL"))
412 afile = new ClustalFile();
414 else if (format.equalsIgnoreCase("BLC"))
416 afile = new BLCFile();
418 else if (format.equalsIgnoreCase("PIR"))
420 afile = new PIRFile();
422 else if (format.equalsIgnoreCase("PFAM"))
424 afile = new PfamFile();
426 else if (format.equalsIgnoreCase("STH"))
428 afile = new StockholmFile();
430 else if (format.equalsIgnoreCase("AMSA"))
432 afile = new AMSAFile(alignment);
437 "Implementation error: Unknown file format string");
440 afile.addJVSuffix(jvsuffix);
442 afile.setSeqs(alignment.getSequencesArray());
444 String afileresp = afile.print();
445 if (afile.hasWarningMessage())
447 System.err.println("Warning raised when writing as " + format
448 + " : " + afile.getWarningMessage());
451 } catch (Exception e)
453 System.err.println("Failed to write alignment as a '" + format
461 public static void main(String[] args)
464 while (i < args.length)
466 File f = new File(args[i]);
471 System.out.println("Reading file: " + f);
472 AppletFormatAdapter afa = new AppletFormatAdapter();
473 Runtime r = Runtime.getRuntime();
475 long memf = -r.totalMemory() + r.freeMemory();
476 long t1 = -System.currentTimeMillis();
477 Alignment al = afa.readFile(args[i], FILE, new IdentifyFile()
478 .Identify(args[i], FILE));
479 t1 += System.currentTimeMillis();
481 memf += r.totalMemory() - r.freeMemory();
484 System.out.println("Alignment contains " + al.getHeight()
485 + " sequences and " + al.getWidth() + " columns.");
488 System.out.println(new AppletFormatAdapter().formatSequences(
490 } catch (Exception e)
493 .println("Couln't format the alignment for output as a FASTA file.");
494 e.printStackTrace(System.err);
499 System.out.println("Couldn't read alignment");
501 System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
503 .println("Difference between free memory now and before is "
504 + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
506 } catch (Exception e)
508 System.err.println("Exception when dealing with " + i
509 + "'th argument: " + args[i] + "\n" + e);
514 System.err.println("Ignoring argument '" + args[i] + "' (" + i
515 + "'th)- not a readable file.");