e4475ab2408918db7194f8c25990c26ee61e7802
[jalview.git] / src / jalview / io / AppletFormatAdapter.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\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
9  *\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
14  *\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
18  */\r
19 package jalview.io;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 /**\r
24  * DOCUMENT ME!\r
25  *\r
26  * @author $author$\r
27  * @version $Revision$\r
28  */\r
29 public class AppletFormatAdapter\r
30 {\r
31   /** DOCUMENT ME!! */\r
32   public static final String[] READABLE_FORMATS = new String[]\r
33       {\r
34       "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB"\r
35   };\r
36 \r
37   public static final String[] WRITEABLE_FORMATS = new String[]\r
38       {\r
39       "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM" //, "AMSA"\r
40   };\r
41 \r
42   public static String INVALID_CHARACTERS = "Contains invalid characters";\r
43 \r
44   public static String SUPPORTED_FORMATS = "Formats currently supported are\n" +\r
45       "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM";\r
46 \r
47   public static String FILE = "File";\r
48   public static String URL = "URL";\r
49   public static String PASTE = "Paste";\r
50   public static String CLASSLOADER = "ClassLoader";\r
51 \r
52   AlignFile afile = null;\r
53   String inFile;\r
54 \r
55   public static final boolean isValidFormat(String format)\r
56   {\r
57     boolean valid = false;\r
58     for (int i = 0; i < READABLE_FORMATS.length; i++)\r
59     {\r
60       if (READABLE_FORMATS[i].equalsIgnoreCase(format))\r
61       {\r
62         return true;\r
63       }\r
64     }\r
65 \r
66     return valid;\r
67   }\r
68 \r
69   /**\r
70    * DOCUMENT ME!\r
71    *\r
72    * @param inFile DOCUMENT ME!\r
73    * @param type DOCUMENT ME!\r
74    * @param format DOCUMENT ME!\r
75    *\r
76    * @return DOCUMENT ME!\r
77    */\r
78   public Alignment readFile(String inFile, String type, String format)\r
79       throws java.io.IOException\r
80   {\r
81     this.inFile = inFile;\r
82     try\r
83     {\r
84       if (format.equals("FASTA"))\r
85       {\r
86         afile = new FastaFile(inFile, type);\r
87       }\r
88       else if (format.equals("MSF"))\r
89       {\r
90         afile = new MSFfile(inFile, type);\r
91       }\r
92       else if (format.equals("PileUp"))\r
93       {\r
94         afile = new PileUpfile(inFile, type);\r
95       }\r
96       else if (format.equals("CLUSTAL"))\r
97       {\r
98         afile = new ClustalFile(inFile, type);\r
99       }\r
100       else if (format.equals("BLC"))\r
101       {\r
102         afile = new BLCFile(inFile, type);\r
103       }\r
104       else if (format.equals("PIR"))\r
105       {\r
106         afile = new PIRFile(inFile, type);\r
107       }\r
108       else if (format.equals("PFAM"))\r
109       {\r
110         afile = new PfamFile(inFile, type);\r
111       }\r
112       else if (format.equals("JnetFile"))\r
113       {\r
114         afile = new JPredFile(inFile, type);\r
115         ( (JPredFile) afile).removeNonSequences();\r
116       }\r
117       else if (format.equals("PDB"))\r
118       {\r
119         afile = new MCview.PDBfile(inFile, type);\r
120       }\r
121       else if (format.equals("STH"))\r
122       {\r
123         afile = new StockholmFile(inFile, type);\r
124       }\r
125 \r
126       Alignment al = new Alignment(afile.getSeqsAsArray());\r
127 \r
128       afile.addAnnotations(al);\r
129 \r
130       return al;\r
131     }\r
132     catch (Exception e)\r
133     {\r
134       e.printStackTrace();\r
135       System.err.println("Failed to read alignment using the '" + format +\r
136                          "' reader.\n" + e);\r
137 \r
138       if (e.getMessage() != null &&\r
139           e.getMessage().startsWith(INVALID_CHARACTERS))\r
140       {\r
141         throw new java.io.IOException(e.getMessage());\r
142       }\r
143 \r
144       // Finally test if the user has pasted just the sequence, no id\r
145       if (type.equalsIgnoreCase("Paste"))\r
146       {\r
147         try\r
148         {\r
149           // Possible sequence is just residues with no label\r
150           afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");\r
151           Alignment al = new Alignment(afile.getSeqsAsArray());\r
152           afile.addAnnotations(al);\r
153           return al;\r
154 \r
155         }\r
156         catch (Exception ex)\r
157         {\r
158           if (ex.toString().startsWith(INVALID_CHARACTERS))\r
159           {\r
160             throw new java.io.IOException(e.getMessage());\r
161           }\r
162 \r
163           ex.printStackTrace();\r
164         }\r
165       }\r
166 \r
167       // If we get to this stage, the format was not supported\r
168       throw new java.io.IOException(SUPPORTED_FORMATS);\r
169     }\r
170   }\r
171 \r
172   /**\r
173    * DOCUMENT ME!\r
174    *\r
175    * @param format DOCUMENT ME!\r
176    * @param seqs DOCUMENT ME!\r
177    *\r
178    * @return DOCUMENT ME!\r
179    */\r
180   public String formatSequences(String format,\r
181                                 AlignmentI alignment,\r
182                                 boolean jvsuffix)\r
183   {\r
184     try\r
185     {\r
186       AlignFile afile = null;\r
187 \r
188       if (format.equalsIgnoreCase("FASTA"))\r
189       {\r
190         afile = new FastaFile();\r
191       }\r
192       else if (format.equalsIgnoreCase("MSF"))\r
193       {\r
194         afile = new MSFfile();\r
195       }\r
196       else if (format.equalsIgnoreCase("PileUp"))\r
197       {\r
198         afile = new PileUpfile();\r
199       }\r
200       else if (format.equalsIgnoreCase("CLUSTAL"))\r
201       {\r
202         afile = new ClustalFile();\r
203       }\r
204       else if (format.equalsIgnoreCase("BLC"))\r
205       {\r
206         afile = new BLCFile();\r
207       }\r
208       else if (format.equalsIgnoreCase("PIR"))\r
209       {\r
210         afile = new PIRFile();\r
211       }\r
212       else if (format.equalsIgnoreCase("PFAM"))\r
213       {\r
214         afile = new PfamFile();\r
215       }\r
216       else if (format.equalsIgnoreCase("STH"))\r
217       {\r
218         afile = new StockholmFile();\r
219       }\r
220       else if (format.equals("AMSA"))\r
221       {\r
222         afile = new AMSAFile(alignment);\r
223       }\r
224 \r
225       afile.addJVSuffix(jvsuffix);\r
226 \r
227       afile.setSeqs(alignment.getSequencesArray());\r
228 \r
229       return afile.print();\r
230     }\r
231     catch (Exception e)\r
232     {\r
233       System.err.println("Failed to write alignment as a '" + format +\r
234                          "' file\n");\r
235       e.printStackTrace();\r
236     }\r
237 \r
238     return null;\r
239   }\r
240 }\r