2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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.*;
23 import java.util.Vector;
32 public class AppletFormatAdapter
35 public static final String [] READABLE_FORMATS = new String[]
37 "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB"
40 public static final String [] WRITEABLE_FORMATS = new String[]
42 "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM"
46 public static String INVALID_CHARACTERS = "Contains invalid characters";
48 public static String SUPPORTED_FORMATS = "Formats currently supported are\n" +
49 "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM";
52 public static String FILE = "File";
53 public static String URL = "URL";
54 public static String PASTE = "Paste";
55 public static String CLASSLOADER = "ClassLoader";
58 AlignFile afile = null;
61 public static final boolean isValidFormat(String format)
63 boolean valid = false;
64 for(int i=0; i<READABLE_FORMATS.length; i++)
65 if(READABLE_FORMATS[i].equalsIgnoreCase(format))
74 * @param inFile DOCUMENT ME!
75 * @param type DOCUMENT ME!
76 * @param format DOCUMENT ME!
78 * @return DOCUMENT ME!
80 public SequenceI[] readFile(String inFile, String type, String format)
81 throws java.io.IOException
86 if (format.equals("FASTA"))
88 afile = new FastaFile(inFile, type);
90 else if (format.equals("MSF"))
92 afile = new MSFfile(inFile, type);
94 else if (format.equals("PileUp"))
96 afile = new PileUpfile(inFile, type);
98 else if (format.equals("CLUSTAL"))
100 afile = new ClustalFile(inFile, type);
102 else if (format.equals("BLC"))
104 afile = new BLCFile(inFile, type);
106 else if (format.equals("PIR"))
108 afile = new PIRFile(inFile, type);
110 else if (format.equals("PFAM"))
112 afile = new PfamFile(inFile, type);
114 else if (format.equals("JnetFile"))
116 afile = new JPredFile(inFile, type);
117 ( (JPredFile) afile).removeNonSequences();
119 else if (format.equals("PDB"))
121 afile = new MCview.PDBfile(inFile, type);
123 else if (format.equals("STH"))
125 afile = new StockholmFile(inFile, type);
129 return afile.getSeqsAsArray();
134 System.err.println("Failed to read alignment using the '" + format +
137 if(e.getMessage()!=null && e.getMessage().startsWith(INVALID_CHARACTERS))
138 throw new java.io.IOException(e.getMessage());
140 // Finally test if the user has pasted just the sequence, no id
141 if(type.equalsIgnoreCase("Paste"))
144 // Possible sequence is just residues with no label
145 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
146 return afile.getSeqsAsArray();
150 if(ex.toString().startsWith(INVALID_CHARACTERS))
151 throw new java.io.IOException(e.getMessage());
153 ex.printStackTrace();
157 // If we get to this stage, the format was not supported
158 throw new java.io.IOException(SUPPORTED_FORMATS);
166 * @param format DOCUMENT ME!
167 * @param seqs DOCUMENT ME!
169 * @return DOCUMENT ME!
171 public String formatSequences(String format,
175 SequenceI[] s = new SequenceI[seqs.size()];
177 for (int i = 0; i < seqs.size(); i++)
178 s[i] = (SequenceI) seqs.elementAt(i);
182 AlignFile afile = null;
184 if (format.equalsIgnoreCase("FASTA"))
186 afile = new FastaFile();
188 else if (format.equalsIgnoreCase("MSF"))
190 afile = new MSFfile();
192 else if (format.equalsIgnoreCase("PileUp"))
194 afile = new PileUpfile();
196 else if (format.equalsIgnoreCase("CLUSTAL"))
198 afile = new ClustalFile();
200 else if (format.equalsIgnoreCase("BLC"))
202 afile = new BLCFile();
204 else if (format.equalsIgnoreCase("PIR"))
206 afile = new PIRFile();
208 else if (format.equalsIgnoreCase("PFAM"))
210 afile = new PfamFile();
212 else if (format.equalsIgnoreCase("STH"))
214 afile = new StockholmFile();
218 afile.addJVSuffix(jvsuffix);
222 return afile.print();
226 System.err.println("Failed to write alignment as a '" + format +