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