2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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 return isValidFormat(format, false);
119 * validate format is valid for IO
120 * @param format a format string to be compared with either READABLE_FORMATS or WRITEABLE_FORMATS
121 * @param forwriting when true, format is checked for containment in WRITEABLE_FORMATS
122 * @return true if format is valid
124 public static final boolean isValidFormat(String format, boolean forwriting)
126 boolean valid = false;
127 String[] format_list = (forwriting) ? WRITEABLE_FORMATS : READABLE_FORMATS;
128 for (int i = 0; i < format_list.length; i++)
130 if (format_list[i].equalsIgnoreCase(format))
140 * Constructs the correct filetype parser for a characterised datasource
142 * @param inFile data/data location
143 * @param type type of datasource
144 * @param format File format of data provided by datasource
146 * @return DOCUMENT ME!
148 public Alignment readFile(String inFile, String type, String format)
149 throws java.io.IOException
151 // TODO: generalise mapping between format string and io. class instances using Constructor.invoke reflection
152 this.inFile = inFile;
155 if (format.equals("FASTA"))
157 afile = new FastaFile(inFile, type);
159 else if (format.equals("MSF"))
161 afile = new MSFfile(inFile, type);
163 else if (format.equals("PileUp"))
165 afile = new PileUpfile(inFile, type);
167 else if (format.equals("CLUSTAL"))
169 afile = new ClustalFile(inFile, type);
171 else if (format.equals("BLC"))
173 afile = new BLCFile(inFile, type);
175 else if (format.equals("PIR"))
177 afile = new PIRFile(inFile, type);
179 else if (format.equals("PFAM"))
181 afile = new PfamFile(inFile, type);
183 else if (format.equals("JnetFile"))
185 afile = new JPredFile(inFile, type);
186 ( (JPredFile) afile).removeNonSequences();
188 else if (format.equals("PDB"))
190 afile = new MCview.PDBfile(inFile, type);
192 else if (format.equals("STH"))
194 afile = new StockholmFile(inFile, type);
197 Alignment al = new Alignment(afile.getSeqsAsArray());
199 afile.addAnnotations(al);
206 System.err.println("Failed to read alignment using the '" + format +
209 if (e.getMessage() != null &&
210 e.getMessage().startsWith(INVALID_CHARACTERS))
212 throw new java.io.IOException(e.getMessage());
215 // Finally test if the user has pasted just the sequence, no id
216 if (type.equalsIgnoreCase("Paste"))
220 // Possible sequence is just residues with no label
221 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
222 Alignment al = new Alignment(afile.getSeqsAsArray());
223 afile.addAnnotations(al);
229 if (ex.toString().startsWith(INVALID_CHARACTERS))
231 throw new java.io.IOException(e.getMessage());
234 ex.printStackTrace();
238 // If we get to this stage, the format was not supported
239 throw new java.io.IOException(SUPPORTED_FORMATS);
243 * Constructs the correct filetype parser for an already open datasource
245 * @param source an existing datasource
246 * @param format File format of data that will be provided by datasource
248 * @return DOCUMENT ME!
250 public Alignment readFromFile(FileParse source, String format)
251 throws java.io.IOException
253 // TODO: generalise mapping between format string and io. class instances using Constructor.invoke reflection
254 // This is exactly the same as the readFile method except we substitute 'inFile, type' with 'source'
255 this.inFile = source.getInFile();
256 String type = source.type;
259 if (format.equals("FASTA"))
261 afile = new FastaFile(source);
263 else if (format.equals("MSF"))
265 afile = new MSFfile(source);
267 else if (format.equals("PileUp"))
269 afile = new PileUpfile(source);
271 else if (format.equals("CLUSTAL"))
273 afile = new ClustalFile(source);
275 else if (format.equals("BLC"))
277 afile = new BLCFile(source);
279 else if (format.equals("PIR"))
281 afile = new PIRFile(source);
283 else if (format.equals("PFAM"))
285 afile = new PfamFile(source);
287 else if (format.equals("JnetFile"))
289 afile = new JPredFile(source);
290 ( (JPredFile) afile).removeNonSequences();
292 else if (format.equals("PDB"))
294 afile = new MCview.PDBfile(source);
296 else if (format.equals("STH"))
298 afile = new StockholmFile(source);
301 Alignment al = new Alignment(afile.getSeqsAsArray());
303 afile.addAnnotations(al);
310 System.err.println("Failed to read alignment using the '" + format +
313 if (e.getMessage() != null &&
314 e.getMessage().startsWith(INVALID_CHARACTERS))
316 throw new java.io.IOException(e.getMessage());
319 // Finally test if the user has pasted just the sequence, no id
320 if (type.equalsIgnoreCase("Paste"))
324 // Possible sequence is just residues with no label
325 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
326 Alignment al = new Alignment(afile.getSeqsAsArray());
327 afile.addAnnotations(al);
333 if (ex.toString().startsWith(INVALID_CHARACTERS))
335 throw new java.io.IOException(e.getMessage());
338 ex.printStackTrace();
342 // If we get to this stage, the format was not supported
343 throw new java.io.IOException(SUPPORTED_FORMATS);
348 * Construct an output class for an alignment in a particular filetype
350 * @param format string name of alignment format
351 * @param alignment the alignment to be written out
352 * @param jvsuffix passed to AlnFile class controls whether /START-END is added to sequence names
354 * @return alignment flat file contents
356 public String formatSequences(String format,
357 AlignmentI alignment,
362 AlignFile afile = null;
364 if (format.equalsIgnoreCase("FASTA"))
366 afile = new FastaFile();
368 else if (format.equalsIgnoreCase("MSF"))
370 afile = new MSFfile();
372 else if (format.equalsIgnoreCase("PileUp"))
374 afile = new PileUpfile();
376 else if (format.equalsIgnoreCase("CLUSTAL"))
378 afile = new ClustalFile();
380 else if (format.equalsIgnoreCase("BLC"))
382 afile = new BLCFile();
384 else if (format.equalsIgnoreCase("PIR"))
386 afile = new PIRFile();
388 else if (format.equalsIgnoreCase("PFAM"))
390 afile = new PfamFile();
392 else if (format.equalsIgnoreCase("STH"))
394 afile = new StockholmFile();
396 else if (format.equalsIgnoreCase("AMSA"))
398 afile = new AMSAFile(alignment);
400 throw new Exception("Implementation error: Unknown file format string");
403 afile.addJVSuffix(jvsuffix);
405 afile.setSeqs(alignment.getSequencesArray());
407 return afile.print();
411 System.err.println("Failed to write alignment as a '" + format +
418 public static void main(String[] args)
421 while (i<args.length)
423 File f = new File(args[i]);
427 System.out.println("Reading file: "+f);
428 AppletFormatAdapter afa = new AppletFormatAdapter();
429 Runtime r = Runtime.getRuntime();
431 long memf = -r.totalMemory()+r.freeMemory();
432 long t1 = -System.currentTimeMillis();
433 Alignment al = afa.readFile(args[i], FILE, new IdentifyFile().Identify(args[i], FILE));
434 t1 +=System.currentTimeMillis();
436 memf += r.totalMemory()-r.freeMemory();
439 System.out.println("Alignment contains "+al.getHeight()+" sequences and "+al.getWidth()+" columns.");
441 System.out.println("Couldn't read alignment");
443 System.out.println("Read took "+(t1/1000.0)+" seconds.");
444 System.out.println("Difference between free memory now and before is "+(memf/(1024.0*1024.0)*1.0)+" MB");
446 } catch (Exception e)
448 System.err.println("Exception when dealing with "+i+"'th argument: "+args[i]+"\n"+e);
451 System.err.println("Ignoring argument '"+args[i]+"' ("+i+"'th)- not a readable file.");