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
21 import jalview.datamodel.*;
29 public class AppletFormatAdapter
32 public static final String[] READABLE_FORMATS = new String[]
34 "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB", "JnetFile"
37 public static final String[] WRITEABLE_FORMATS = new String[]
39 "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM" , "AMSA"
42 public static final String[] WRITABLE_EXTENSIONS = new String[]
43 { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc","amsa","jar" };
45 public static final String[] WRITABLE_FNAMES = new String[]
46 { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview" };
48 public static final String[] READABLE_EXTENSIONS = new String[]
50 "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
53 public static final String[] READABLE_FNAMES = new String[]
55 "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA","Jalview"
58 public static String INVALID_CHARACTERS = "Contains invalid characters";
59 // TODO: make these messages dynamic
60 public static String SUPPORTED_FORMATS = "Formats currently supported are\n" +
61 prettyPrint(READABLE_FORMATS);
65 * @return grammatically correct(ish) list consisting of els elements.
67 public static String prettyPrint(String[] els) {
68 StringBuffer list = new StringBuffer();
69 for (int i=0,iSize=els.length-1; i<iSize;i++)
74 list.append(" and "+els[els.length-1]+".");
75 return list.toString();
77 public static String FILE = "File";
78 public static String URL = "URL";
79 public static String PASTE = "Paste";
80 public static String CLASSLOADER = "ClassLoader";
82 AlignFile afile = null;
85 * check that this format is valid for reading
86 * @param format a format string to be compared with READABLE_FORMATS
87 * @return true if format is readable
89 public static final boolean isValidFormat(String format)
91 boolean valid = false;
92 for (int i = 0; i < READABLE_FORMATS.length; i++)
94 if (READABLE_FORMATS[i].equalsIgnoreCase(format))
104 * Constructs the correct filetype parser for a characterised datasource
106 * @param inFile data/data location
107 * @param type type of datasource
108 * @param format File format of data provided by datasource
110 * @return DOCUMENT ME!
112 public Alignment readFile(String inFile, String type, String format)
113 throws java.io.IOException
115 this.inFile = inFile;
118 if (format.equals("FASTA"))
120 afile = new FastaFile(inFile, type);
122 else if (format.equals("MSF"))
124 afile = new MSFfile(inFile, type);
126 else if (format.equals("PileUp"))
128 afile = new PileUpfile(inFile, type);
130 else if (format.equals("CLUSTAL"))
132 afile = new ClustalFile(inFile, type);
134 else if (format.equals("BLC"))
136 afile = new BLCFile(inFile, type);
138 else if (format.equals("PIR"))
140 afile = new PIRFile(inFile, type);
142 else if (format.equals("PFAM"))
144 afile = new PfamFile(inFile, type);
146 else if (format.equals("JnetFile"))
148 afile = new JPredFile(inFile, type);
149 ( (JPredFile) afile).removeNonSequences();
151 else if (format.equals("PDB"))
153 afile = new MCview.PDBfile(inFile, type);
155 else if (format.equals("STH"))
157 afile = new StockholmFile(inFile, type);
160 Alignment al = new Alignment(afile.getSeqsAsArray());
162 afile.addAnnotations(al);
169 System.err.println("Failed to read alignment using the '" + format +
172 if (e.getMessage() != null &&
173 e.getMessage().startsWith(INVALID_CHARACTERS))
175 throw new java.io.IOException(e.getMessage());
178 // Finally test if the user has pasted just the sequence, no id
179 if (type.equalsIgnoreCase("Paste"))
183 // Possible sequence is just residues with no label
184 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
185 Alignment al = new Alignment(afile.getSeqsAsArray());
186 afile.addAnnotations(al);
192 if (ex.toString().startsWith(INVALID_CHARACTERS))
194 throw new java.io.IOException(e.getMessage());
197 ex.printStackTrace();
201 // If we get to this stage, the format was not supported
202 throw new java.io.IOException(SUPPORTED_FORMATS);
207 * Construct an output class for an alignment in a particular filetype
209 * @param format DOCUMENT ME!
210 * @param seqs DOCUMENT ME!
211 * @param jvsuffix passed to AlnFile class
213 * @return alignment flat file contents
215 public String formatSequences(String format,
216 AlignmentI alignment,
221 AlignFile afile = null;
223 if (format.equalsIgnoreCase("FASTA"))
225 afile = new FastaFile();
227 else if (format.equalsIgnoreCase("MSF"))
229 afile = new MSFfile();
231 else if (format.equalsIgnoreCase("PileUp"))
233 afile = new PileUpfile();
235 else if (format.equalsIgnoreCase("CLUSTAL"))
237 afile = new ClustalFile();
239 else if (format.equalsIgnoreCase("BLC"))
241 afile = new BLCFile();
243 else if (format.equalsIgnoreCase("PIR"))
245 afile = new PIRFile();
247 else if (format.equalsIgnoreCase("PFAM"))
249 afile = new PfamFile();
251 else if (format.equalsIgnoreCase("STH"))
253 afile = new StockholmFile();
255 else if (format.equalsIgnoreCase("AMSA"))
257 afile = new AMSAFile(alignment);
259 throw new Exception("Implementation error: Unknown file format string");
262 afile.addJVSuffix(jvsuffix);
264 afile.setSeqs(alignment.getSequencesArray());
266 return afile.print();
270 System.err.println("Failed to write alignment as a '" + format +