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