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 String INVALID_CHARACTERS = "Contains invalid characters";
43 // TODO: make these messages dynamic
44 public static String SUPPORTED_FORMATS = "Formats currently supported are\n" +
45 "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM";
47 public static String FILE = "File";
48 public static String URL = "URL";
49 public static String PASTE = "Paste";
50 public static String CLASSLOADER = "ClassLoader";
52 AlignFile afile = null;
55 public static final boolean isValidFormat(String format)
57 boolean valid = false;
58 for (int i = 0; i < READABLE_FORMATS.length; i++)
60 if (READABLE_FORMATS[i].equalsIgnoreCase(format))
70 * Constructs the correct filetype parser for a characterised datasource
72 * @param inFile data/data location
73 * @param type type of datasource
74 * @param format File format of data provided by datasource
76 * @return DOCUMENT ME!
78 public Alignment readFile(String inFile, String type, String format)
79 throws java.io.IOException
84 if (format.equals("FASTA"))
86 afile = new FastaFile(inFile, type);
88 else if (format.equals("MSF"))
90 afile = new MSFfile(inFile, type);
92 else if (format.equals("PileUp"))
94 afile = new PileUpfile(inFile, type);
96 else if (format.equals("CLUSTAL"))
98 afile = new ClustalFile(inFile, type);
100 else if (format.equals("BLC"))
102 afile = new BLCFile(inFile, type);
104 else if (format.equals("PIR"))
106 afile = new PIRFile(inFile, type);
108 else if (format.equals("PFAM"))
110 afile = new PfamFile(inFile, type);
112 else if (format.equals("JnetFile"))
114 afile = new JPredFile(inFile, type);
115 ( (JPredFile) afile).removeNonSequences();
117 else if (format.equals("PDB"))
119 afile = new MCview.PDBfile(inFile, type);
121 else if (format.equals("STH"))
123 afile = new StockholmFile(inFile, type);
126 Alignment al = new Alignment(afile.getSeqsAsArray());
128 afile.addAnnotations(al);
135 System.err.println("Failed to read alignment using the '" + format +
138 if (e.getMessage() != null &&
139 e.getMessage().startsWith(INVALID_CHARACTERS))
141 throw new java.io.IOException(e.getMessage());
144 // Finally test if the user has pasted just the sequence, no id
145 if (type.equalsIgnoreCase("Paste"))
149 // Possible sequence is just residues with no label
150 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
151 Alignment al = new Alignment(afile.getSeqsAsArray());
152 afile.addAnnotations(al);
158 if (ex.toString().startsWith(INVALID_CHARACTERS))
160 throw new java.io.IOException(e.getMessage());
163 ex.printStackTrace();
167 // If we get to this stage, the format was not supported
168 throw new java.io.IOException(SUPPORTED_FORMATS);
173 * Construct an output class for an alignment in a particular filetype
175 * @param format DOCUMENT ME!
176 * @param seqs DOCUMENT ME!
177 * @param jvsuffix passed to AlnFile class
179 * @return alignment flat file contents
181 public String formatSequences(String format,
182 AlignmentI alignment,
187 AlignFile afile = null;
189 if (format.equalsIgnoreCase("FASTA"))
191 afile = new FastaFile();
193 else if (format.equalsIgnoreCase("MSF"))
195 afile = new MSFfile();
197 else if (format.equalsIgnoreCase("PileUp"))
199 afile = new PileUpfile();
201 else if (format.equalsIgnoreCase("CLUSTAL"))
203 afile = new ClustalFile();
205 else if (format.equalsIgnoreCase("BLC"))
207 afile = new BLCFile();
209 else if (format.equalsIgnoreCase("PIR"))
211 afile = new PIRFile();
213 else if (format.equalsIgnoreCase("PFAM"))
215 afile = new PfamFile();
217 else if (format.equalsIgnoreCase("STH"))
219 afile = new StockholmFile();
221 else if (format.equalsIgnoreCase("AMSA"))
223 afile = new AMSAFile(alignment);
225 throw new Exception("Implementation error: Unknown file format string");
228 afile.addJVSuffix(jvsuffix);
230 afile.setSeqs(alignment.getSequencesArray());
232 return afile.print();
236 System.err.println("Failed to write alignment as a '" + format +