JnetFIle is a readable format
[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 String INVALID_CHARACTERS = "Contains invalid characters";
43   // TODO: make these messages dynamic
44   public static String SUPPORTED_FORMATS = "Formats currently supported are\n" +
45       "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM";
46
47   public static String FILE = "File";
48   public static String URL = "URL";
49   public static String PASTE = "Paste";
50   public static String CLASSLOADER = "ClassLoader";
51
52   AlignFile afile = null;
53   String inFile;
54
55   public static final boolean isValidFormat(String format)
56   {
57     boolean valid = false;
58     for (int i = 0; i < READABLE_FORMATS.length; i++)
59     {
60       if (READABLE_FORMATS[i].equalsIgnoreCase(format))
61       {
62         return true;
63       }
64     }
65
66     return valid;
67   }
68
69   /**
70    * Constructs the correct filetype parser for a characterised datasource
71    *
72    * @param inFile data/data location
73    * @param type type of datasource
74    * @param format File format of data provided by datasource
75    *
76    * @return DOCUMENT ME!
77    */
78   public Alignment readFile(String inFile, String type, String format)
79       throws java.io.IOException
80   {
81     this.inFile = inFile;
82     try
83     {
84       if (format.equals("FASTA"))
85       {
86         afile = new FastaFile(inFile, type);
87       }
88       else if (format.equals("MSF"))
89       {
90         afile = new MSFfile(inFile, type);
91       }
92       else if (format.equals("PileUp"))
93       {
94         afile = new PileUpfile(inFile, type);
95       }
96       else if (format.equals("CLUSTAL"))
97       {
98         afile = new ClustalFile(inFile, type);
99       }
100       else if (format.equals("BLC"))
101       {
102         afile = new BLCFile(inFile, type);
103       }
104       else if (format.equals("PIR"))
105       {
106         afile = new PIRFile(inFile, type);
107       }
108       else if (format.equals("PFAM"))
109       {
110         afile = new PfamFile(inFile, type);
111       }
112       else if (format.equals("JnetFile"))
113       {
114         afile = new JPredFile(inFile, type);
115         ( (JPredFile) afile).removeNonSequences();
116       }
117       else if (format.equals("PDB"))
118       {
119         afile = new MCview.PDBfile(inFile, type);
120       }
121       else if (format.equals("STH"))
122       {
123         afile = new StockholmFile(inFile, type);
124       }
125
126       Alignment al = new Alignment(afile.getSeqsAsArray());
127
128       afile.addAnnotations(al);
129
130       return al;
131     }
132     catch (Exception e)
133     {
134       e.printStackTrace();
135       System.err.println("Failed to read alignment using the '" + format +
136                          "' reader.\n" + e);
137
138       if (e.getMessage() != null &&
139           e.getMessage().startsWith(INVALID_CHARACTERS))
140       {
141         throw new java.io.IOException(e.getMessage());
142       }
143
144       // Finally test if the user has pasted just the sequence, no id
145       if (type.equalsIgnoreCase("Paste"))
146       {
147         try
148         {
149           // Possible sequence is just residues with no label
150           afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
151           Alignment al = new Alignment(afile.getSeqsAsArray());
152           afile.addAnnotations(al);
153           return al;
154
155         }
156         catch (Exception ex)
157         {
158           if (ex.toString().startsWith(INVALID_CHARACTERS))
159           {
160             throw new java.io.IOException(e.getMessage());
161           }
162
163           ex.printStackTrace();
164         }
165       }
166
167       // If we get to this stage, the format was not supported
168       throw new java.io.IOException(SUPPORTED_FORMATS);
169     }
170   }
171
172   /**
173    * Construct an output class for an alignment in a particular filetype
174    *
175    * @param format DOCUMENT ME!
176    * @param seqs DOCUMENT ME!
177    * @param jvsuffix passed to AlnFile class
178    *
179    * @return alignment flat file contents
180    */
181   public String formatSequences(String format,
182                                 AlignmentI alignment,
183                                 boolean jvsuffix)
184   {
185     try
186     {
187       AlignFile afile = null;
188
189       if (format.equalsIgnoreCase("FASTA"))
190       {
191         afile = new FastaFile();
192       }
193       else if (format.equalsIgnoreCase("MSF"))
194       {
195         afile = new MSFfile();
196       }
197       else if (format.equalsIgnoreCase("PileUp"))
198       {
199         afile = new PileUpfile();
200       }
201       else if (format.equalsIgnoreCase("CLUSTAL"))
202       {
203         afile = new ClustalFile();
204       }
205       else if (format.equalsIgnoreCase("BLC"))
206       {
207         afile = new BLCFile();
208       }
209       else if (format.equalsIgnoreCase("PIR"))
210       {
211         afile = new PIRFile();
212       }
213       else if (format.equalsIgnoreCase("PFAM"))
214       {
215         afile = new PfamFile();
216       }
217       else if (format.equalsIgnoreCase("STH"))
218       {
219         afile = new StockholmFile();
220       }
221       else if (format.equals("AMSA"))
222       {
223         afile = new AMSAFile(alignment);
224       }
225
226       afile.addJVSuffix(jvsuffix);
227
228       afile.setSeqs(alignment.getSequencesArray());
229
230       return afile.print();
231     }
232     catch (Exception e)
233     {
234       System.err.println("Failed to write alignment as a '" + format +
235                          "' file\n");
236       e.printStackTrace();
237     }
238
239     return null;
240   }
241 }