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