fbfaab131fba87b73e372fff2d5513f1b4676697
[jalview.git] / src / jalview / io / AppletFormatAdapter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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 /**
24  * DOCUMENT ME!
25  *
26  * @author $author$
27  * @version $Revision$
28  */
29 public class AppletFormatAdapter
30 {
31   /** DOCUMENT ME!! */
32   public static final String[] READABLE_FORMATS = new String[]
33       {
34       "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB", "JnetFile"
35   };
36
37   public static final String[] WRITEABLE_FORMATS = new String[]
38       {
39       "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM" , "AMSA"
40   };
41
42   public static final String[] WRITABLE_EXTENSIONS = new String[]
43         { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc","amsa","jar" };
44
45   public static final String[] WRITABLE_FNAMES = new String[]
46         { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview" };
47
48   public static final String[] READABLE_EXTENSIONS =         new String[]
49         {
50         "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc",
51         "amsa","jar"
52     };
53   public static final String[] READABLE_FNAMES =         new String[]
54         {
55         "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA","Jalview"
56     };
57         
58   public static String INVALID_CHARACTERS = "Contains invalid characters";
59   // TODO: make these messages dynamic
60   public static String SUPPORTED_FORMATS = "Formats currently supported are\n" +
61     prettyPrint(READABLE_FORMATS);
62   /**
63    * 
64    * @param els
65    * @return grammatically correct(ish) list consisting of els elements.
66    */
67   public static String prettyPrint(String[] els) {
68     StringBuffer list = new StringBuffer();
69     for (int i=0,iSize=els.length-1; i<iSize;i++)
70     {
71       list.append(els[i]);
72       list.append(",");
73     }
74     list.append(" and "+els[els.length-1]+".");
75     return list.toString();
76   }
77   public static String FILE = "File";
78   public static String URL = "URL";
79   public static String PASTE = "Paste";
80   public static String CLASSLOADER = "ClassLoader";
81
82   AlignFile afile = null;
83   String inFile;
84   /**
85    * check that this format is valid for reading
86    * @param format a format string to be compared with READABLE_FORMATS
87    * @return true if format is readable
88    */
89   public static final boolean isValidFormat(String format)
90   {
91     boolean valid = false;
92     for (int i = 0; i < READABLE_FORMATS.length; i++)
93     {
94       if (READABLE_FORMATS[i].equalsIgnoreCase(format))
95       {
96         return true;
97       }
98     }
99
100     return valid;
101   }
102
103   /**
104    * Constructs the correct filetype parser for a characterised datasource
105    *
106    * @param inFile data/data location
107    * @param type type of datasource
108    * @param format File format of data provided by datasource
109    *
110    * @return DOCUMENT ME!
111    */
112   public Alignment readFile(String inFile, String type, String format)
113       throws java.io.IOException
114   {
115     this.inFile = inFile;
116     try
117     {
118       if (format.equals("FASTA"))
119       {
120         afile = new FastaFile(inFile, type);
121       }
122       else if (format.equals("MSF"))
123       {
124         afile = new MSFfile(inFile, type);
125       }
126       else if (format.equals("PileUp"))
127       {
128         afile = new PileUpfile(inFile, type);
129       }
130       else if (format.equals("CLUSTAL"))
131       {
132         afile = new ClustalFile(inFile, type);
133       }
134       else if (format.equals("BLC"))
135       {
136         afile = new BLCFile(inFile, type);
137       }
138       else if (format.equals("PIR"))
139       {
140         afile = new PIRFile(inFile, type);
141       }
142       else if (format.equals("PFAM"))
143       {
144         afile = new PfamFile(inFile, type);
145       }
146       else if (format.equals("JnetFile"))
147       {
148         afile = new JPredFile(inFile, type);
149         ( (JPredFile) afile).removeNonSequences();
150       }
151       else if (format.equals("PDB"))
152       {
153         afile = new MCview.PDBfile(inFile, type);
154       }
155       else if (format.equals("STH"))
156       {
157         afile = new StockholmFile(inFile, type);
158       }
159
160       Alignment al = new Alignment(afile.getSeqsAsArray());
161
162       afile.addAnnotations(al);
163
164       return al;
165     }
166     catch (Exception e)
167     {
168       e.printStackTrace();
169       System.err.println("Failed to read alignment using the '" + format +
170                          "' reader.\n" + e);
171
172       if (e.getMessage() != null &&
173           e.getMessage().startsWith(INVALID_CHARACTERS))
174       {
175         throw new java.io.IOException(e.getMessage());
176       }
177
178       // Finally test if the user has pasted just the sequence, no id
179       if (type.equalsIgnoreCase("Paste"))
180       {
181         try
182         {
183           // Possible sequence is just residues with no label
184           afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
185           Alignment al = new Alignment(afile.getSeqsAsArray());
186           afile.addAnnotations(al);
187           return al;
188
189         }
190         catch (Exception ex)
191         {
192           if (ex.toString().startsWith(INVALID_CHARACTERS))
193           {
194             throw new java.io.IOException(e.getMessage());
195           }
196
197           ex.printStackTrace();
198         }
199       }
200
201       // If we get to this stage, the format was not supported
202       throw new java.io.IOException(SUPPORTED_FORMATS);
203     }
204   }
205
206   /**
207    * Construct an output class for an alignment in a particular filetype
208    *
209    * @param format DOCUMENT ME!
210    * @param seqs DOCUMENT ME!
211    * @param jvsuffix passed to AlnFile class
212    *
213    * @return alignment flat file contents
214    */
215   public String formatSequences(String format,
216                                 AlignmentI alignment,
217                                 boolean jvsuffix)
218   {
219     try
220     {
221       AlignFile afile = null;
222
223       if (format.equalsIgnoreCase("FASTA"))
224       {
225         afile = new FastaFile();
226       }
227       else if (format.equalsIgnoreCase("MSF"))
228       {
229         afile = new MSFfile();
230       }
231       else if (format.equalsIgnoreCase("PileUp"))
232       {
233         afile = new PileUpfile();
234       }
235       else if (format.equalsIgnoreCase("CLUSTAL"))
236       {
237         afile = new ClustalFile();
238       }
239       else if (format.equalsIgnoreCase("BLC"))
240       {
241         afile = new BLCFile();
242       }
243       else if (format.equalsIgnoreCase("PIR"))
244       {
245         afile = new PIRFile();
246       }
247       else if (format.equalsIgnoreCase("PFAM"))
248       {
249         afile = new PfamFile();
250       }
251       else if (format.equalsIgnoreCase("STH"))
252       {
253         afile = new StockholmFile();
254       }
255       else if (format.equalsIgnoreCase("AMSA"))
256       {
257         afile = new AMSAFile(alignment);
258       } else {
259         throw new Exception("Implementation error: Unknown file format string");
260       }
261
262       afile.addJVSuffix(jvsuffix);
263
264       afile.setSeqs(alignment.getSequencesArray());
265
266       return afile.print();
267     }
268     catch (Exception e)
269     {
270       System.err.println("Failed to write alignment as a '" + format +
271                          "' file\n");
272       e.printStackTrace();
273     }
274
275     return null;
276   }
277 }