2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 import jalview.datamodel.*;
26 * A low level class for alignment and feature IO with alignment formatting
27 * methods used by both applet and application for generating flat alignment
28 * files. It also holds the lists of magic format names that the applet and
29 * application will allow the user to read or write files with.
34 public class AppletFormatAdapter
37 * List of valid format strings used in the isValidFormat method
39 public static final String[] READABLE_FORMATS = new String[]
40 { "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH",
44 * List of valid format strings for use by callers of the formatSequences
47 public static final String[] WRITEABLE_FORMATS = new String[]
48 { "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "AMSA" };
51 * List of extensions corresponding to file format types in WRITABLE_FNAMES
52 * that are writable by the application.
54 public static final String[] WRITABLE_EXTENSIONS = new String[]
55 { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "jar" };
58 * List of writable formats by the application. Order must correspond with the
59 * WRITABLE_EXTENSIONS list of formats.
61 public static final String[] WRITABLE_FNAMES = new String[]
62 { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview" };
65 * List of readable format file extensions by application in order
66 * corresponding to READABLE_FNAMES
68 public static final String[] READABLE_EXTENSIONS = new String[]
69 { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "jar" };
72 * List of readable formats by application in order corresponding to
75 public static final String[] READABLE_FNAMES = new String[]
76 { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview" };
78 public static String INVALID_CHARACTERS = "Contains invalid characters";
80 // TODO: make these messages dynamic
81 public static String SUPPORTED_FORMATS = "Formats currently supported are\n"
82 + prettyPrint(READABLE_FORMATS);
87 * @return grammatically correct(ish) list consisting of els elements.
89 public static String prettyPrint(String[] els)
91 StringBuffer list = new StringBuffer();
92 for (int i = 0, iSize = els.length - 1; i < iSize; i++)
97 list.append(" and " + els[els.length - 1] + ".");
98 return list.toString();
101 public static String FILE = "File";
103 public static String URL = "URL";
105 public static String PASTE = "Paste";
107 public static String CLASSLOADER = "ClassLoader";
109 AlignFile afile = null;
114 * check that this format is valid for reading
117 * a format string to be compared with READABLE_FORMATS
118 * @return true if format is readable
120 public static final boolean isValidFormat(String format)
122 return isValidFormat(format, false);
126 * validate format is valid for IO
129 * a format string to be compared with either READABLE_FORMATS
130 * or WRITEABLE_FORMATS
132 * when true, format is checked for containment in
134 * @return true if format is valid
136 public static final boolean isValidFormat(String format,
139 boolean valid = false;
140 String[] format_list = (forwriting) ? WRITEABLE_FORMATS
142 for (int i = 0; i < format_list.length; i++)
144 if (format_list[i].equalsIgnoreCase(format))
154 * Constructs the correct filetype parser for a characterised datasource
161 * File format of data provided by datasource
163 * @return DOCUMENT ME!
165 public Alignment readFile(String inFile, String type, String format)
166 throws java.io.IOException
168 // TODO: generalise mapping between format string and io. class instances
169 // using Constructor.invoke reflection
170 this.inFile = inFile;
173 if (format.equals("FASTA"))
175 afile = new FastaFile(inFile, type);
177 else if (format.equals("MSF"))
179 afile = new MSFfile(inFile, type);
181 else if (format.equals("PileUp"))
183 afile = new PileUpfile(inFile, type);
185 else if (format.equals("CLUSTAL"))
187 afile = new ClustalFile(inFile, type);
189 else if (format.equals("BLC"))
191 afile = new BLCFile(inFile, type);
193 else if (format.equals("PIR"))
195 afile = new PIRFile(inFile, type);
197 else if (format.equals("PFAM"))
199 afile = new PfamFile(inFile, type);
201 else if (format.equals("JnetFile"))
203 afile = new JPredFile(inFile, type);
204 ((JPredFile) afile).removeNonSequences();
206 else if (format.equals("PDB"))
208 afile = new MCview.PDBfile(inFile, type);
210 else if (format.equals("STH"))
212 afile = new StockholmFile(inFile, type);
215 Alignment al = new Alignment(afile.getSeqsAsArray());
217 afile.addAnnotations(al);
220 } catch (Exception e)
223 System.err.println("Failed to read alignment using the '" + format
224 + "' reader.\n" + e);
226 if (e.getMessage() != null
227 && e.getMessage().startsWith(INVALID_CHARACTERS))
229 throw new java.io.IOException(e.getMessage());
232 // Finally test if the user has pasted just the sequence, no id
233 if (type.equalsIgnoreCase("Paste"))
237 // Possible sequence is just residues with no label
238 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
239 Alignment al = new Alignment(afile.getSeqsAsArray());
240 afile.addAnnotations(al);
243 } catch (Exception ex)
245 if (ex.toString().startsWith(INVALID_CHARACTERS))
247 throw new java.io.IOException(e.getMessage());
250 ex.printStackTrace();
254 // If we get to this stage, the format was not supported
255 throw new java.io.IOException(SUPPORTED_FORMATS);
260 * Constructs the correct filetype parser for an already open datasource
263 * an existing datasource
265 * File format of data that will be provided by datasource
267 * @return DOCUMENT ME!
269 public Alignment readFromFile(FileParse source, String format)
270 throws java.io.IOException
272 // TODO: generalise mapping between format string and io. class instances
273 // using Constructor.invoke reflection
274 // This is exactly the same as the readFile method except we substitute
275 // 'inFile, type' with 'source'
276 this.inFile = source.getInFile();
277 String type = source.type;
280 if (format.equals("FASTA"))
282 afile = new FastaFile(source);
284 else if (format.equals("MSF"))
286 afile = new MSFfile(source);
288 else if (format.equals("PileUp"))
290 afile = new PileUpfile(source);
292 else if (format.equals("CLUSTAL"))
294 afile = new ClustalFile(source);
296 else if (format.equals("BLC"))
298 afile = new BLCFile(source);
300 else if (format.equals("PIR"))
302 afile = new PIRFile(source);
304 else if (format.equals("PFAM"))
306 afile = new PfamFile(source);
308 else if (format.equals("JnetFile"))
310 afile = new JPredFile(source);
311 ((JPredFile) afile).removeNonSequences();
313 else if (format.equals("PDB"))
315 afile = new MCview.PDBfile(source);
317 else if (format.equals("STH"))
319 afile = new StockholmFile(source);
322 Alignment al = new Alignment(afile.getSeqsAsArray());
324 afile.addAnnotations(al);
327 } catch (Exception e)
330 System.err.println("Failed to read alignment using the '" + format
331 + "' reader.\n" + e);
333 if (e.getMessage() != null
334 && e.getMessage().startsWith(INVALID_CHARACTERS))
336 throw new java.io.IOException(e.getMessage());
339 // Finally test if the user has pasted just the sequence, no id
340 if (type.equalsIgnoreCase("Paste"))
344 // Possible sequence is just residues with no label
345 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
346 Alignment al = new Alignment(afile.getSeqsAsArray());
347 afile.addAnnotations(al);
350 } catch (Exception ex)
352 if (ex.toString().startsWith(INVALID_CHARACTERS))
354 throw new java.io.IOException(e.getMessage());
357 ex.printStackTrace();
361 // If we get to this stage, the format was not supported
362 throw new java.io.IOException(SUPPORTED_FORMATS);
367 * Construct an output class for an alignment in a particular filetype
370 * string name of alignment format
372 * the alignment to be written out
374 * passed to AlnFile class controls whether /START-END is added
377 * @return alignment flat file contents
379 public String formatSequences(String format, AlignmentI alignment,
384 AlignFile afile = null;
386 if (format.equalsIgnoreCase("FASTA"))
388 afile = new FastaFile();
390 else if (format.equalsIgnoreCase("MSF"))
392 afile = new MSFfile();
394 else if (format.equalsIgnoreCase("PileUp"))
396 afile = new PileUpfile();
398 else if (format.equalsIgnoreCase("CLUSTAL"))
400 afile = new ClustalFile();
402 else if (format.equalsIgnoreCase("BLC"))
404 afile = new BLCFile();
406 else if (format.equalsIgnoreCase("PIR"))
408 afile = new PIRFile();
410 else if (format.equalsIgnoreCase("PFAM"))
412 afile = new PfamFile();
414 else if (format.equalsIgnoreCase("STH"))
416 afile = new StockholmFile();
418 else if (format.equalsIgnoreCase("AMSA"))
420 afile = new AMSAFile(alignment);
425 "Implementation error: Unknown file format string");
428 afile.addJVSuffix(jvsuffix);
430 afile.setSeqs(alignment.getSequencesArray());
432 return afile.print();
433 } catch (Exception e)
435 System.err.println("Failed to write alignment as a '" + format
443 public static void main(String[] args)
446 while (i < args.length)
448 File f = new File(args[i]);
453 System.out.println("Reading file: " + f);
454 AppletFormatAdapter afa = new AppletFormatAdapter();
455 Runtime r = Runtime.getRuntime();
457 long memf = -r.totalMemory() + r.freeMemory();
458 long t1 = -System.currentTimeMillis();
459 Alignment al = afa.readFile(args[i], FILE, new IdentifyFile()
460 .Identify(args[i], FILE));
461 t1 += System.currentTimeMillis();
463 memf += r.totalMemory() - r.freeMemory();
466 System.out.println("Alignment contains " + al.getHeight()
467 + " sequences and " + al.getWidth() + " columns.");
471 System.out.println("Couldn't read alignment");
473 System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
475 .println("Difference between free memory now and before is "
476 + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
478 } catch (Exception e)
480 System.err.println("Exception when dealing with " + i
481 + "'th argument: " + args[i] + "\n" + e);
486 System.err.println("Ignoring argument '" + args[i] + "' (" + i
487 + "'th)- not a readable file.");