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 this.inFile = inFile;
143 if (format.equals("FASTA"))
145 afile = new FastaFile(inFile, type);
147 else if (format.equals("MSF"))
149 afile = new MSFfile(inFile, type);
151 else if (format.equals("PileUp"))
153 afile = new PileUpfile(inFile, type);
155 else if (format.equals("CLUSTAL"))
157 afile = new ClustalFile(inFile, type);
159 else if (format.equals("BLC"))
161 afile = new BLCFile(inFile, type);
163 else if (format.equals("PIR"))
165 afile = new PIRFile(inFile, type);
167 else if (format.equals("PFAM"))
169 afile = new PfamFile(inFile, type);
171 else if (format.equals("JnetFile"))
173 afile = new JPredFile(inFile, type);
174 ( (JPredFile) afile).removeNonSequences();
176 else if (format.equals("PDB"))
178 afile = new MCview.PDBfile(inFile, type);
180 else if (format.equals("STH"))
182 afile = new StockholmFile(inFile, type);
185 Alignment al = new Alignment(afile.getSeqsAsArray());
187 afile.addAnnotations(al);
194 System.err.println("Failed to read alignment using the '" + format +
197 if (e.getMessage() != null &&
198 e.getMessage().startsWith(INVALID_CHARACTERS))
200 throw new java.io.IOException(e.getMessage());
203 // Finally test if the user has pasted just the sequence, no id
204 if (type.equalsIgnoreCase("Paste"))
208 // Possible sequence is just residues with no label
209 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
210 Alignment al = new Alignment(afile.getSeqsAsArray());
211 afile.addAnnotations(al);
217 if (ex.toString().startsWith(INVALID_CHARACTERS))
219 throw new java.io.IOException(e.getMessage());
222 ex.printStackTrace();
226 // If we get to this stage, the format was not supported
227 throw new java.io.IOException(SUPPORTED_FORMATS);
232 * Construct an output class for an alignment in a particular filetype
234 * @param format string name of alignment format
235 * @param alignment the alignment to be written out
236 * @param jvsuffix passed to AlnFile class controls whether /START-END is added to sequence names
238 * @return alignment flat file contents
240 public String formatSequences(String format,
241 AlignmentI alignment,
246 AlignFile afile = null;
248 if (format.equalsIgnoreCase("FASTA"))
250 afile = new FastaFile();
252 else if (format.equalsIgnoreCase("MSF"))
254 afile = new MSFfile();
256 else if (format.equalsIgnoreCase("PileUp"))
258 afile = new PileUpfile();
260 else if (format.equalsIgnoreCase("CLUSTAL"))
262 afile = new ClustalFile();
264 else if (format.equalsIgnoreCase("BLC"))
266 afile = new BLCFile();
268 else if (format.equalsIgnoreCase("PIR"))
270 afile = new PIRFile();
272 else if (format.equalsIgnoreCase("PFAM"))
274 afile = new PfamFile();
276 else if (format.equalsIgnoreCase("STH"))
278 afile = new StockholmFile();
280 else if (format.equalsIgnoreCase("AMSA"))
282 afile = new AMSAFile(alignment);
284 throw new Exception("Implementation error: Unknown file format string");
287 afile.addJVSuffix(jvsuffix);
289 afile.setSeqs(alignment.getSequencesArray());
291 return afile.print();
295 System.err.println("Failed to write alignment as a '" + format +
302 public static void main(String[] args)
305 while (i<args.length)
307 File f = new File(args[i]);
311 AppletFormatAdapter afa = new AppletFormatAdapter();
312 Runtime r = Runtime.getRuntime();
314 long memf = -r.totalMemory()+r.freeMemory();
315 long t1 = -System.currentTimeMillis();
316 Alignment al = afa.readFile(args[i], FILE, new IdentifyFile().Identify(args[i], FILE));
317 t1 +=System.currentTimeMillis();
319 memf += r.totalMemory()-r.freeMemory();
320 System.out.println("Read took "+(t1/1000.0)+" seconds.");
321 System.out.println("Difference between free memory now and before is "+(memf/(1024.0*1024.0)*1.0)+" MB");
323 } catch (Exception e)
325 System.err.println("Exception when dealing with "+i+"'th argument: "+args[i]+"\n"+e);
328 System.err.println("Ignoring argument '"+args[i]+"' ("+i+"'th)- not a readable file.");