application specific validIOformat method
[jalview.git] / src / jalview / io / FormatAdapter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 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  * Additional formatting methods used by the application in a number of places.
25  * 
26  * @author $author$
27  * @version $Revision$
28  */
29 public class FormatAdapter extends AppletFormatAdapter
30 {
31
32   public String formatSequences(String format, SequenceI[] seqs,
33           String[] omitHiddenColumns)
34   {
35
36     return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
37   }
38
39   /**
40    * create sequences with each seuqence string replaced with the one given in
41    * omitHiddenCOlumns
42    * 
43    * @param seqs
44    * @param omitHiddenColumns
45    * @return new sequences
46    */
47   public SequenceI[] replaceStrings(SequenceI[] seqs,
48           String[] omitHiddenColumns)
49   {
50     if (omitHiddenColumns != null)
51     {
52       SequenceI[] tmp = new SequenceI[seqs.length];
53       for (int i = 0; i < seqs.length; i++)
54       {
55         tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
56                 seqs[i].getStart(), seqs[i].getEnd());
57         tmp[i].setDescription(seqs[i].getDescription());
58       }
59       seqs = tmp;
60     }
61     return seqs;
62   }
63
64   /**
65    * Format a vector of sequences as a flat alignment file.
66    * 
67    * @param format
68    *                Format string as givien in the AppletFormatAdaptor list
69    *                (exact match to name of class implementing file io for that
70    *                format)
71    * @param seqs
72    *                vector of sequences to write
73    * 
74    * @return String containing sequences in desired format
75    */
76   public String formatSequences(String format, SequenceI[] seqs)
77   {
78
79     try
80     {
81       AlignFile afile = null;
82
83       if (format.equalsIgnoreCase("FASTA"))
84       {
85         afile = new FastaFile();
86         afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
87                 true));
88       }
89       else if (format.equalsIgnoreCase("MSF"))
90       {
91         afile = new MSFfile();
92         afile.addJVSuffix(jalview.bin.Cache
93                 .getDefault("MSF_JVSUFFIX", true));
94       }
95       else if (format.equalsIgnoreCase("PileUp"))
96       {
97         afile = new PileUpfile();
98         afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
99                 true));
100       }
101       else if (format.equalsIgnoreCase("CLUSTAL"))
102       {
103         afile = new ClustalFile();
104         afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
105                 true));
106       }
107       else if (format.equalsIgnoreCase("BLC"))
108       {
109         afile = new BLCFile();
110         afile.addJVSuffix(jalview.bin.Cache
111                 .getDefault("BLC_JVSUFFIX", true));
112       }
113       else if (format.equalsIgnoreCase("PIR"))
114       {
115         afile = new PIRFile();
116         afile.addJVSuffix(jalview.bin.Cache
117                 .getDefault("PIR_JVSUFFIX", true));
118       }
119       else if (format.equalsIgnoreCase("PFAM"))
120       {
121         afile = new PfamFile();
122         afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
123                 true));
124       }
125       /*
126        * amsa is not supported by this function - it requires an alignment
127        * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA")) {
128        * afile = new AMSAFile(); afile.addJVSuffix(
129        * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
130        */
131
132       afile.setSeqs(seqs);
133
134       return afile.print();
135     } catch (Exception e)
136     {
137       System.err.println("Failed to write alignment as a '" + format
138               + "' file\n");
139       e.printStackTrace();
140     }
141
142     return null;
143   }
144
145   public boolean getCacheSuffixDefault(String format)
146   {
147     if (isValidFormat(format))
148       return jalview.bin.Cache.getDefault(format.toUpperCase()
149               + "_JVSUFFIX", true);
150     return false;
151   }
152
153   public String formatSequences(String format, AlignmentI alignment,
154           String[] omitHidden, ColumnSelection colSel)
155   {
156     return formatSequences(format, alignment, omitHidden,
157             getCacheSuffixDefault(format), colSel, null);
158   }
159
160   public String formatSequences(String format, AlignmentI alignment,
161           String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
162   {
163     return formatSequences(format, alignment, omitHidden,
164             getCacheSuffixDefault(format), colSel, sgp);
165   }
166
167   /**
168    * hack function to replace seuqences with visible sequence strings before
169    * generating a string of the alignment in the given format.
170    * 
171    * @param format
172    * @param alignment
173    * @param omitHidden
174    *                sequence strings to write out in order of sequences in
175    *                alignment
176    * @param colSel
177    *                defines hidden columns that are edited out of annotation
178    * @return string representation of the alignment formatted as format
179    */
180   public String formatSequences(String format, AlignmentI alignment,
181           String[] omitHidden, boolean suffix, ColumnSelection colSel)
182   {
183     return formatSequences(format, alignment, omitHidden, suffix, colSel,
184             null);
185   }
186
187   public String formatSequences(String format, AlignmentI alignment,
188           String[] omitHidden, boolean suffix, ColumnSelection colSel,
189           jalview.datamodel.SequenceGroup selgp)
190   {
191     if (omitHidden != null)
192     {
193       // 
194       Alignment alv = new Alignment(replaceStrings(alignment
195               .getSequencesArray(), omitHidden));
196       AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
197       for (int i = 0; i < ala.length; i++)
198       {
199         AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
200         if (selgp != null)
201         {
202           colSel.makeVisibleAnnotation(selgp.getStartRes(), selgp
203                   .getEndRes(), na);
204         }
205         else
206         {
207           colSel.makeVisibleAnnotation(na);
208         }
209         alv.addAnnotation(na);
210       }
211       return this.formatSequences(format, alv, suffix);
212     }
213     return this.formatSequences(format, alignment, suffix);
214   }
215
216   /**
217    * validate format is valid for IO in Application. This is basically the
218    * AppletFormatAdapter.isValidFormat call with additional checks for
219    * Application only formats like 'Jalview'.
220    * 
221    * @param format
222    *                a format string to be compared with list of readable or writable formats (READABLE_FORMATS
223    *                or WRITABLE_FORMATS)
224    * @param forwriting
225    *                when true, format is checked against list of writable formats.
226    * @return true if format is valid
227    */
228   public static final boolean isValidIOFormat(String format,
229           boolean forwriting)
230   {
231     if (format.equalsIgnoreCase("jalview"))
232     {
233       return true;
234     }
235     return AppletFormatAdapter.isValidFormat(format, forwriting);
236   }
237 }