pass any warning messages to StdErr - also todo to allow caller to check for any...
[jalview.git] / src / jalview / io / FormatAdapter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 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    * TODO: allow caller to detect errors and warnings encountered when generating output
67    *
68    * 
69    * @param format
70    *                Format string as givien in the AppletFormatAdaptor list
71    *                (exact match to name of class implementing file io for that
72    *                format)
73    * @param seqs
74    *                vector of sequences to write
75    * 
76    * @return String containing sequences in desired format
77    */
78   public String formatSequences(String format, SequenceI[] seqs)
79   {
80
81     try
82     {
83       AlignFile afile = null;
84
85       if (format.equalsIgnoreCase("FASTA"))
86       {
87         afile = new FastaFile();
88         afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
89                 true));
90       }
91       else if (format.equalsIgnoreCase("MSF"))
92       {
93         afile = new MSFfile();
94         afile.addJVSuffix(jalview.bin.Cache
95                 .getDefault("MSF_JVSUFFIX", true));
96       }
97       else if (format.equalsIgnoreCase("PileUp"))
98       {
99         afile = new PileUpfile();
100         afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
101                 true));
102       }
103       else if (format.equalsIgnoreCase("CLUSTAL"))
104       {
105         afile = new ClustalFile();
106         afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
107                 true));
108       }
109       else if (format.equalsIgnoreCase("BLC"))
110       {
111         afile = new BLCFile();
112         afile.addJVSuffix(jalview.bin.Cache
113                 .getDefault("BLC_JVSUFFIX", true));
114       }
115       else if (format.equalsIgnoreCase("PIR"))
116       {
117         afile = new PIRFile();
118         afile.addJVSuffix(jalview.bin.Cache
119                 .getDefault("PIR_JVSUFFIX", true));
120       }
121       else if (format.equalsIgnoreCase("PFAM"))
122       {
123         afile = new PfamFile();
124         afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
125                 true));
126       }
127       /*
128        * amsa is not supported by this function - it requires an alignment
129        * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA")) {
130        * afile = new AMSAFile(); afile.addJVSuffix(
131        * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
132        */
133
134       afile.setSeqs(seqs);
135       String afileresp = afile.print();
136       if (afile.hasWarningMessage())
137       {
138         System.err.println("Warning raised when writing as "+format+" : "+afile.getWarningMessage());
139       }
140       return afileresp;
141     } catch (Exception e)
142     {
143       System.err.println("Failed to write alignment as a '" + format
144               + "' file\n");
145       e.printStackTrace();
146     }
147
148     return null;
149   }
150
151   public boolean getCacheSuffixDefault(String format)
152   {
153     if (isValidFormat(format))
154       return jalview.bin.Cache.getDefault(format.toUpperCase()
155               + "_JVSUFFIX", true);
156     return false;
157   }
158
159   public String formatSequences(String format, AlignmentI alignment,
160           String[] omitHidden, ColumnSelection colSel)
161   {
162     return formatSequences(format, alignment, omitHidden,
163             getCacheSuffixDefault(format), colSel, null);
164   }
165
166   public String formatSequences(String format, AlignmentI alignment,
167           String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
168   {
169     return formatSequences(format, alignment, omitHidden,
170             getCacheSuffixDefault(format), colSel, sgp);
171   }
172
173   /**
174    * hack function to replace seuqences with visible sequence strings before
175    * generating a string of the alignment in the given format.
176    * 
177    * @param format
178    * @param alignment
179    * @param omitHidden
180    *                sequence strings to write out in order of sequences in
181    *                alignment
182    * @param colSel
183    *                defines hidden columns that are edited out of annotation
184    * @return string representation of the alignment formatted as format
185    */
186   public String formatSequences(String format, AlignmentI alignment,
187           String[] omitHidden, boolean suffix, ColumnSelection colSel)
188   {
189     return formatSequences(format, alignment, omitHidden, suffix, colSel,
190             null);
191   }
192
193   public String formatSequences(String format, AlignmentI alignment,
194           String[] omitHidden, boolean suffix, ColumnSelection colSel,
195           jalview.datamodel.SequenceGroup selgp)
196   {
197     if (omitHidden != null)
198     {
199       // 
200       Alignment alv = new Alignment(replaceStrings(alignment
201               .getSequencesArray(), omitHidden));
202       AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
203       for (int i = 0; i < ala.length; i++)
204       {
205         AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
206         if (selgp != null)
207         {
208           colSel.makeVisibleAnnotation(selgp.getStartRes(), selgp
209                   .getEndRes(), na);
210         }
211         else
212         {
213           colSel.makeVisibleAnnotation(na);
214         }
215         alv.addAnnotation(na);
216       }
217       return this.formatSequences(format, alv, suffix);
218     }
219     return this.formatSequences(format, alignment, suffix);
220   }
221
222   /**
223    * validate format is valid for IO in Application. This is basically the
224    * AppletFormatAdapter.isValidFormat call with additional checks for
225    * Application only formats like 'Jalview'.
226    * 
227    * @param format
228    *                a format string to be compared with list of readable or writable formats (READABLE_FORMATS
229    *                or WRITABLE_FORMATS)
230    * @param forwriting
231    *                when true, format is checked against list of writable formats.
232    * @return true if format is valid
233    */
234   public static final boolean isValidIOFormat(String format,
235           boolean forwriting)
236   {
237     if (format.equalsIgnoreCase("jalview"))
238     {
239       return true;
240     }
241     return AppletFormatAdapter.isValidFormat(format, forwriting);
242   }
243 }