2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
21 import jalview.datamodel.*;
\r
27 * @version $Revision$
\r
29 public class AppletFormatAdapter
\r
31 /** DOCUMENT ME!! */
\r
32 public static final String [] READABLE_FORMATS = new String[]
\r
34 "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB"
\r
37 public static final String [] WRITEABLE_FORMATS = new String[]
\r
39 "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM" //, "AMSA"
\r
43 public static String INVALID_CHARACTERS = "Contains invalid characters";
\r
45 public static String SUPPORTED_FORMATS = "Formats currently supported are\n" +
\r
46 "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM";
\r
49 public static String FILE = "File";
\r
50 public static String URL = "URL";
\r
51 public static String PASTE = "Paste";
\r
52 public static String CLASSLOADER = "ClassLoader";
\r
55 AlignFile afile = null;
\r
58 public static final boolean isValidFormat(String format)
\r
60 boolean valid = false;
\r
61 for(int i=0; i<READABLE_FORMATS.length; i++)
\r
62 if(READABLE_FORMATS[i].equalsIgnoreCase(format))
\r
71 * @param inFile DOCUMENT ME!
\r
72 * @param type DOCUMENT ME!
\r
73 * @param format DOCUMENT ME!
\r
75 * @return DOCUMENT ME!
\r
77 public Alignment readFile(String inFile, String type, String format)
\r
78 throws java.io.IOException
\r
80 this.inFile = inFile;
\r
83 if (format.equals("FASTA"))
\r
85 afile = new FastaFile(inFile, type);
\r
87 else if (format.equals("MSF"))
\r
89 afile = new MSFfile(inFile, type);
\r
91 else if (format.equals("PileUp"))
\r
93 afile = new PileUpfile(inFile, type);
\r
95 else if (format.equals("CLUSTAL"))
\r
97 afile = new ClustalFile(inFile, type);
\r
99 else if (format.equals("BLC"))
\r
101 afile = new BLCFile(inFile, type);
\r
103 else if (format.equals("PIR"))
\r
105 afile = new PIRFile(inFile, type);
\r
107 else if (format.equals("PFAM"))
\r
109 afile = new PfamFile(inFile, type);
\r
111 else if (format.equals("JnetFile"))
\r
113 afile = new JPredFile(inFile, type);
\r
114 ( (JPredFile) afile).removeNonSequences();
\r
116 else if (format.equals("PDB"))
\r
118 afile = new MCview.PDBfile(inFile, type);
\r
120 else if (format.equals("STH"))
\r
122 afile = new StockholmFile(inFile, type);
\r
125 Alignment al = new Alignment(afile.getSeqsAsArray());
\r
127 afile.addAnnotations(al);
\r
131 catch (Exception e)
\r
133 e.printStackTrace();
\r
134 System.err.println("Failed to read alignment using the '" + format +
\r
137 if(e.getMessage()!=null && e.getMessage().startsWith(INVALID_CHARACTERS))
\r
138 throw new java.io.IOException(e.getMessage());
\r
140 // Finally test if the user has pasted just the sequence, no id
\r
141 if(type.equalsIgnoreCase("Paste"))
\r
144 // Possible sequence is just residues with no label
\r
145 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
\r
146 Alignment al = new Alignment(afile.getSeqsAsArray());
\r
147 afile.addAnnotations(al);
\r
151 catch(Exception ex)
\r
153 if(ex.toString().startsWith(INVALID_CHARACTERS))
\r
154 throw new java.io.IOException(e.getMessage());
\r
156 ex.printStackTrace();
\r
160 // If we get to this stage, the format was not supported
\r
161 throw new java.io.IOException(SUPPORTED_FORMATS);
\r
169 * @param format DOCUMENT ME!
\r
170 * @param seqs DOCUMENT ME!
\r
172 * @return DOCUMENT ME!
\r
174 public String formatSequences(String format,
\r
175 AlignmentI alignment,
\r
180 AlignFile afile = null;
\r
182 if (format.equalsIgnoreCase("FASTA"))
\r
184 afile = new FastaFile();
\r
186 else if (format.equalsIgnoreCase("MSF"))
\r
188 afile = new MSFfile();
\r
190 else if (format.equalsIgnoreCase("PileUp"))
\r
192 afile = new PileUpfile();
\r
194 else if (format.equalsIgnoreCase("CLUSTAL"))
\r
196 afile = new ClustalFile();
\r
198 else if (format.equalsIgnoreCase("BLC"))
\r
200 afile = new BLCFile();
\r
202 else if (format.equalsIgnoreCase("PIR"))
\r
204 afile = new PIRFile();
\r
206 else if (format.equalsIgnoreCase("PFAM"))
\r
208 afile = new PfamFile();
\r
210 else if (format.equalsIgnoreCase("STH"))
\r
212 afile = new StockholmFile();
\r
214 else if (format.equals("AMSA"))
\r
216 afile = new AMSAFile(alignment);
\r
219 afile.addJVSuffix(jvsuffix);
\r
221 afile.setSeqs( alignment.getSequencesArray() );
\r
223 return afile.print();
\r
225 catch (Exception e)
\r
227 System.err.println("Failed to write alignment as a '" + format +
\r
229 e.printStackTrace();
\r