updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / io / AppletFormatAdapter.java
1     /*
2     * Jalview - A Sequence Alignment Editor and Viewer
3     * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * as published by the Free Software Foundation; either version 2
8     * of the License, or (at your option) any later version.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software
17     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18     */
19     package jalview.io;
20
21     import jalview.datamodel.*;
22
23     import java.util.Vector;
24
25
26     /**
27      * DOCUMENT ME!
28      *
29      * @author $author$
30      * @version $Revision$
31      */
32     public class AppletFormatAdapter
33     {
34         /** DOCUMENT ME!! */
35         public static final String [] READABLE_FORMATS = new String[]
36         {
37             "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB"
38         };
39
40         public static final String [] WRITEABLE_FORMATS = new String[]
41             {
42             "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM"
43         };
44
45
46         public static String INVALID_CHARACTERS = "Contains invalid characters";
47
48         public static String SUPPORTED_FORMATS = "Formats currently supported are\n" +
49                                                  "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM";
50
51
52         public static String FILE = "File";
53         public static String URL = "URL";
54         public static String PASTE = "Paste";
55         public static String CLASSLOADER = "ClassLoader";
56
57
58         AlignFile afile = null;
59         String inFile;
60
61         public static final boolean isValidFormat(String format)
62         {
63           boolean valid = false;
64           for(int i=0; i<READABLE_FORMATS.length; i++)
65             if(READABLE_FORMATS[i].equalsIgnoreCase(format))
66               return true;
67
68           return valid;
69         }
70
71         /**
72          * DOCUMENT ME!
73          *
74          * @param inFile DOCUMENT ME!
75          * @param type DOCUMENT ME!
76          * @param format DOCUMENT ME!
77          *
78          * @return DOCUMENT ME!
79          */
80         public SequenceI[] readFile(String inFile, String type, String format)
81             throws java.io.IOException
82         {
83             this.inFile = inFile;
84             try
85             {
86               if (format.equals("FASTA"))
87               {
88                 afile = new FastaFile(inFile, type);
89               }
90               else if (format.equals("MSF"))
91               {
92                 afile = new MSFfile(inFile, type);
93               }
94               else if (format.equals("PileUp"))
95               {
96                 afile = new PileUpfile(inFile, type);
97               }
98               else if (format.equals("CLUSTAL"))
99               {
100                 afile = new ClustalFile(inFile, type);
101               }
102               else if (format.equals("BLC"))
103               {
104                 afile = new BLCFile(inFile, type);
105               }
106               else if (format.equals("PIR"))
107               {
108                 afile = new PIRFile(inFile, type);
109               }
110               else if (format.equals("PFAM"))
111               {
112                 afile = new PfamFile(inFile, type);
113               }
114               else if (format.equals("JnetFile"))
115               {
116                 afile = new JPredFile(inFile, type);
117                 ( (JPredFile) afile).removeNonSequences();
118               }
119               else if (format.equals("PDB"))
120               {
121                 afile = new MCview.PDBfile(inFile, type);
122               }
123               else if (format.equals("STH"))
124               {
125                 afile = new StockholmFile(inFile, type);
126               }
127
128
129               return afile.getSeqsAsArray();
130             }
131             catch (Exception e)
132             {
133               e.printStackTrace();
134               System.err.println("Failed to read alignment using the '" + format +
135                                  "' reader.\n"+e);
136
137               if(e.getMessage()!=null && e.getMessage().startsWith(INVALID_CHARACTERS))
138                 throw new java.io.IOException(e.getMessage());
139
140               // Finally test if the user has pasted just the sequence, no id
141               if(type.equalsIgnoreCase("Paste"))
142               {
143                 try{
144                   // Possible sequence is just residues with no label
145                   afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
146                   return afile.getSeqsAsArray();
147                 }
148                 catch(Exception ex)
149                 {
150                   if(ex.toString().startsWith(INVALID_CHARACTERS))
151                     throw new java.io.IOException(e.getMessage());
152
153                   ex.printStackTrace();
154                 }
155               }
156
157               // If we get to this stage, the format was not supported
158               throw new java.io.IOException(SUPPORTED_FORMATS);
159             }
160         }
161
162
163         /**
164          * DOCUMENT ME!
165          *
166          * @param format DOCUMENT ME!
167          * @param seqs DOCUMENT ME!
168          *
169          * @return DOCUMENT ME!
170          */
171         public String formatSequences(String format,
172                                       Vector seqs,
173                                       boolean jvsuffix)
174         {
175             SequenceI[] s = new SequenceI[seqs.size()];
176
177             for (int i = 0; i < seqs.size(); i++)
178                 s[i] = (SequenceI) seqs.elementAt(i);
179
180             try
181             {
182                 AlignFile afile = null;
183
184                 if (format.equalsIgnoreCase("FASTA"))
185                 {
186                     afile = new FastaFile();
187                 }
188                 else if (format.equalsIgnoreCase("MSF"))
189                 {
190                     afile = new MSFfile();
191                 }
192                 else if (format.equalsIgnoreCase("PileUp"))
193                 {
194                     afile = new PileUpfile();
195                 }
196                 else if (format.equalsIgnoreCase("CLUSTAL"))
197                 {
198                     afile = new ClustalFile();
199                 }
200                 else if (format.equalsIgnoreCase("BLC"))
201                 {
202                     afile = new BLCFile();
203                 }
204                 else if (format.equalsIgnoreCase("PIR"))
205                 {
206                     afile = new PIRFile();
207                 }
208                 else if (format.equalsIgnoreCase("PFAM"))
209                 {
210                     afile = new PfamFile();
211                 }
212                 else if (format.equalsIgnoreCase("STH"))
213                 {
214                   afile = new StockholmFile();
215                 }
216
217
218                 afile.addJVSuffix(jvsuffix);
219
220                 afile.setSeqs(s);
221
222                 return afile.print();
223             }
224             catch (Exception e)
225             {
226                 System.err.println("Failed to write alignment as a '" + format +
227                     "' file\n");
228                 e.printStackTrace();
229             }
230
231             return null;
232         }
233     }