2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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
27 * with alignment formatting methods used by both applet
28 * and application for generating flat alignment files.
29 * It also holds the lists of magic format names
30 * that the applet and application will allow the user to read or write files with.
35 public class AppletFormatAdapter
38 * List of valid format strings used in the isValidFormat method
40 public static final String[] READABLE_FORMATS = new String[]
42 "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB", "JnetFile"
45 * List of valid format strings for use by callers of the formatSequences method
47 public static final String[] WRITEABLE_FORMATS = new String[]
49 "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM" , "AMSA"
52 * List of extensions corresponding to file format types
53 * in WRITABLE_FNAMES that are writable by the
56 public static final String[] WRITABLE_EXTENSIONS = new String[]
57 { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc","amsa","jar" };
59 * List of writable formats by the application. Order must
60 * correspond with the WRITABLE_EXTENSIONS list of formats.
62 public static final String[] WRITABLE_FNAMES = new String[]
63 { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview" };
66 * List of readable format file extensions by application in order
67 * corresponding to READABLE_FNAMES
69 public static final String[] READABLE_EXTENSIONS = new String[]
71 "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
75 * List of readable formats by application in order
76 * corresponding to READABLE_EXTENSIONS
78 public static final String[] READABLE_FNAMES = new String[]
80 "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA","Jalview"
83 public static String INVALID_CHARACTERS = "Contains invalid characters";
84 // TODO: make these messages dynamic
85 public static String SUPPORTED_FORMATS = "Formats currently supported are\n" +
86 prettyPrint(READABLE_FORMATS);
90 * @return grammatically correct(ish) list consisting of els elements.
92 public static String prettyPrint(String[] els) {
93 StringBuffer list = new StringBuffer();
94 for (int i=0,iSize=els.length-1; i<iSize;i++)
99 list.append(" and "+els[els.length-1]+".");
100 return list.toString();
102 public static String FILE = "File";
103 public static String URL = "URL";
104 public static String PASTE = "Paste";
105 public static String CLASSLOADER = "ClassLoader";
107 AlignFile afile = null;
110 * check that this format is valid for reading
111 * @param format a format string to be compared with READABLE_FORMATS
112 * @return true if format is readable
114 public static final boolean isValidFormat(String format)
116 boolean valid = false;
117 for (int i = 0; i < READABLE_FORMATS.length; i++)
119 if (READABLE_FORMATS[i].equalsIgnoreCase(format))
129 * Constructs the correct filetype parser for a characterised datasource
131 * @param inFile data/data location
132 * @param type type of datasource
133 * @param format File format of data provided by datasource
135 * @return DOCUMENT ME!
137 public Alignment readFile(String inFile, String type, String format)
138 throws java.io.IOException
140 // TODO: generalise mapping between format string and io. class instances using Constructor.invoke reflection
141 this.inFile = inFile;
144 if (format.equals("FASTA"))
146 afile = new FastaFile(inFile, type);
148 else if (format.equals("MSF"))
150 afile = new MSFfile(inFile, type);
152 else if (format.equals("PileUp"))
154 afile = new PileUpfile(inFile, type);
156 else if (format.equals("CLUSTAL"))
158 afile = new ClustalFile(inFile, type);
160 else if (format.equals("BLC"))
162 afile = new BLCFile(inFile, type);
164 else if (format.equals("PIR"))
166 afile = new PIRFile(inFile, type);
168 else if (format.equals("PFAM"))
170 afile = new PfamFile(inFile, type);
172 else if (format.equals("JnetFile"))
174 afile = new JPredFile(inFile, type);
175 ( (JPredFile) afile).removeNonSequences();
177 else if (format.equals("PDB"))
179 afile = new MCview.PDBfile(inFile, type);
181 else if (format.equals("STH"))
183 afile = new StockholmFile(inFile, type);
186 Alignment al = new Alignment(afile.getSeqsAsArray());
188 afile.addAnnotations(al);
195 System.err.println("Failed to read alignment using the '" + format +
198 if (e.getMessage() != null &&
199 e.getMessage().startsWith(INVALID_CHARACTERS))
201 throw new java.io.IOException(e.getMessage());
204 // Finally test if the user has pasted just the sequence, no id
205 if (type.equalsIgnoreCase("Paste"))
209 // Possible sequence is just residues with no label
210 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
211 Alignment al = new Alignment(afile.getSeqsAsArray());
212 afile.addAnnotations(al);
218 if (ex.toString().startsWith(INVALID_CHARACTERS))
220 throw new java.io.IOException(e.getMessage());
223 ex.printStackTrace();
227 // If we get to this stage, the format was not supported
228 throw new java.io.IOException(SUPPORTED_FORMATS);
232 * Constructs the correct filetype parser for an already open datasource
234 * @param source an existing datasource
235 * @param format File format of data that will be provided by datasource
237 * @return DOCUMENT ME!
239 public Alignment readFromFile(FileParse source, String format)
240 throws java.io.IOException
242 // TODO: generalise mapping between format string and io. class instances using Constructor.invoke reflection
243 // This is exactly the same as the readFile method except we substitute 'inFile, type' with 'source'
244 this.inFile = source.getInFile();
245 String type = source.type;
248 if (format.equals("FASTA"))
250 afile = new FastaFile(source);
252 else if (format.equals("MSF"))
254 afile = new MSFfile(source);
256 else if (format.equals("PileUp"))
258 afile = new PileUpfile(source);
260 else if (format.equals("CLUSTAL"))
262 afile = new ClustalFile(source);
264 else if (format.equals("BLC"))
266 afile = new BLCFile(source);
268 else if (format.equals("PIR"))
270 afile = new PIRFile(source);
272 else if (format.equals("PFAM"))
274 afile = new PfamFile(source);
276 else if (format.equals("JnetFile"))
278 afile = new JPredFile(source);
279 ( (JPredFile) afile).removeNonSequences();
281 else if (format.equals("PDB"))
283 afile = new MCview.PDBfile(source);
285 else if (format.equals("STH"))
287 afile = new StockholmFile(source);
290 Alignment al = new Alignment(afile.getSeqsAsArray());
292 afile.addAnnotations(al);
299 System.err.println("Failed to read alignment using the '" + format +
302 if (e.getMessage() != null &&
303 e.getMessage().startsWith(INVALID_CHARACTERS))
305 throw new java.io.IOException(e.getMessage());
308 // Finally test if the user has pasted just the sequence, no id
309 if (type.equalsIgnoreCase("Paste"))
313 // Possible sequence is just residues with no label
314 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
315 Alignment al = new Alignment(afile.getSeqsAsArray());
316 afile.addAnnotations(al);
322 if (ex.toString().startsWith(INVALID_CHARACTERS))
324 throw new java.io.IOException(e.getMessage());
327 ex.printStackTrace();
331 // If we get to this stage, the format was not supported
332 throw new java.io.IOException(SUPPORTED_FORMATS);
337 * Construct an output class for an alignment in a particular filetype
339 * @param format string name of alignment format
340 * @param alignment the alignment to be written out
341 * @param jvsuffix passed to AlnFile class controls whether /START-END is added to sequence names
343 * @return alignment flat file contents
345 public String formatSequences(String format,
346 AlignmentI alignment,
351 AlignFile afile = null;
353 if (format.equalsIgnoreCase("FASTA"))
355 afile = new FastaFile();
357 else if (format.equalsIgnoreCase("MSF"))
359 afile = new MSFfile();
361 else if (format.equalsIgnoreCase("PileUp"))
363 afile = new PileUpfile();
365 else if (format.equalsIgnoreCase("CLUSTAL"))
367 afile = new ClustalFile();
369 else if (format.equalsIgnoreCase("BLC"))
371 afile = new BLCFile();
373 else if (format.equalsIgnoreCase("PIR"))
375 afile = new PIRFile();
377 else if (format.equalsIgnoreCase("PFAM"))
379 afile = new PfamFile();
381 else if (format.equalsIgnoreCase("STH"))
383 afile = new StockholmFile();
385 else if (format.equalsIgnoreCase("AMSA"))
387 afile = new AMSAFile(alignment);
389 throw new Exception("Implementation error: Unknown file format string");
392 afile.addJVSuffix(jvsuffix);
394 afile.setSeqs(alignment.getSequencesArray());
396 return afile.print();
400 System.err.println("Failed to write alignment as a '" + format +
407 public static void main(String[] args)
410 while (i<args.length)
412 File f = new File(args[i]);
416 System.out.println("Reading file: "+f);
417 AppletFormatAdapter afa = new AppletFormatAdapter();
418 Runtime r = Runtime.getRuntime();
420 long memf = -r.totalMemory()+r.freeMemory();
421 long t1 = -System.currentTimeMillis();
422 Alignment al = afa.readFile(args[i], FILE, new IdentifyFile().Identify(args[i], FILE));
423 t1 +=System.currentTimeMillis();
425 memf += r.totalMemory()-r.freeMemory();
428 System.out.println("Alignment contains "+al.getHeight()+" sequences and "+al.getWidth()+" columns.");
430 System.out.println("Couldn't read alignment");
432 System.out.println("Read took "+(t1/1000.0)+" seconds.");
433 System.out.println("Difference between free memory now and before is "+(memf/(1024.0*1024.0)*1.0)+" MB");
435 } catch (Exception e)
437 System.err.println("Exception when dealing with "+i+"'th argument: "+args[i]+"\n"+e);
440 System.err.println("Ignoring argument '"+args[i]+"' ("+i+"'th)- not a readable file.");