AnnotationId is hashcode of object
[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     /**\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 \r
43         public static String INVALID_CHARACTERS = "Contains invalid characters";\r
44 \r
45         public static String SUPPORTED_FORMATS = "Formats currently supported are\n" +\r
46                                                  "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM";\r
47 \r
48 \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
53 \r
54 \r
55         AlignFile afile = null;\r
56         String inFile;\r
57 \r
58         public static final boolean isValidFormat(String format)\r
59         {\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
63               return true;\r
64 \r
65           return valid;\r
66         }\r
67 \r
68         /**\r
69          * DOCUMENT ME!\r
70          *\r
71          * @param inFile DOCUMENT ME!\r
72          * @param type DOCUMENT ME!\r
73          * @param format DOCUMENT ME!\r
74          *\r
75          * @return DOCUMENT ME!\r
76          */\r
77         public Alignment readFile(String inFile, String type, String format)\r
78             throws java.io.IOException\r
79         {\r
80             this.inFile = inFile;\r
81             try\r
82             {\r
83               if (format.equals("FASTA"))\r
84               {\r
85                 afile = new FastaFile(inFile, type);\r
86               }\r
87               else if (format.equals("MSF"))\r
88               {\r
89                 afile = new MSFfile(inFile, type);\r
90               }\r
91               else if (format.equals("PileUp"))\r
92               {\r
93                 afile = new PileUpfile(inFile, type);\r
94               }\r
95               else if (format.equals("CLUSTAL"))\r
96               {\r
97                 afile = new ClustalFile(inFile, type);\r
98               }\r
99               else if (format.equals("BLC"))\r
100               {\r
101                 afile = new BLCFile(inFile, type);\r
102               }\r
103               else if (format.equals("PIR"))\r
104               {\r
105                 afile = new PIRFile(inFile, type);\r
106               }\r
107               else if (format.equals("PFAM"))\r
108               {\r
109                 afile = new PfamFile(inFile, type);\r
110               }\r
111               else if (format.equals("JnetFile"))\r
112               {\r
113                 afile = new JPredFile(inFile, type);\r
114                 ( (JPredFile) afile).removeNonSequences();\r
115               }\r
116               else if (format.equals("PDB"))\r
117               {\r
118                 afile = new MCview.PDBfile(inFile, type);\r
119               }\r
120               else if (format.equals("STH"))\r
121               {\r
122                 afile = new StockholmFile(inFile, type);\r
123               }\r
124 \r
125               Alignment al = new Alignment(afile.getSeqsAsArray());\r
126 \r
127               afile.addAnnotations(al);\r
128 \r
129               return al;\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                   Alignment al = new Alignment(afile.getSeqsAsArray());\r
147                   afile.addAnnotations(al);\r
148                   return al;\r
149 \r
150                 }\r
151                 catch(Exception ex)\r
152                 {\r
153                   if(ex.toString().startsWith(INVALID_CHARACTERS))\r
154                     throw new java.io.IOException(e.getMessage());\r
155 \r
156                   ex.printStackTrace();\r
157                 }\r
158               }\r
159 \r
160               // If we get to this stage, the format was not supported\r
161               throw new java.io.IOException(SUPPORTED_FORMATS);\r
162             }\r
163         }\r
164 \r
165 \r
166         /**\r
167          * DOCUMENT ME!\r
168          *\r
169          * @param format DOCUMENT ME!\r
170          * @param seqs DOCUMENT ME!\r
171          *\r
172          * @return DOCUMENT ME!\r
173          */\r
174         public String formatSequences(String format,\r
175                                       AlignmentI alignment,\r
176                                       boolean jvsuffix)\r
177         {\r
178             try\r
179             {\r
180                 AlignFile afile = null;\r
181 \r
182                 if (format.equalsIgnoreCase("FASTA"))\r
183                 {\r
184                     afile = new FastaFile();\r
185                 }\r
186                 else if (format.equalsIgnoreCase("MSF"))\r
187                 {\r
188                     afile = new MSFfile();\r
189                 }\r
190                 else if (format.equalsIgnoreCase("PileUp"))\r
191                 {\r
192                     afile = new PileUpfile();\r
193                 }\r
194                 else if (format.equalsIgnoreCase("CLUSTAL"))\r
195                 {\r
196                     afile = new ClustalFile();\r
197                 }\r
198                 else if (format.equalsIgnoreCase("BLC"))\r
199                 {\r
200                     afile = new BLCFile();\r
201                 }\r
202                 else if (format.equalsIgnoreCase("PIR"))\r
203                 {\r
204                     afile = new PIRFile();\r
205                 }\r
206                 else if (format.equalsIgnoreCase("PFAM"))\r
207                 {\r
208                     afile = new PfamFile();\r
209                 }\r
210                 else if (format.equalsIgnoreCase("STH"))\r
211                 {\r
212                   afile = new StockholmFile();\r
213                 }\r
214                 else if (format.equals("AMSA"))\r
215                 {\r
216                   afile = new AMSAFile(alignment);\r
217                 }\r
218 \r
219                 afile.addJVSuffix(jvsuffix);\r
220 \r
221                 afile.setSeqs( alignment.getSequencesArray() );\r
222 \r
223                 return afile.print();\r
224             }\r
225             catch (Exception e)\r
226             {\r
227                 System.err.println("Failed to write alignment as a '" + format +\r
228                     "' file\n");\r
229                 e.printStackTrace();\r
230             }\r
231 \r
232             return null;\r
233         }\r
234     }\r