update author list in license for (JAL-826)
[jalview.git] / src / jalview / io / FormatAdapter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.io;
19
20 import jalview.datamodel.*;
21
22 /**
23  * Additional formatting methods used by the application in a number of places.
24  * 
25  * @author $author$
26  * @version $Revision$
27  */
28 public class FormatAdapter extends AppletFormatAdapter
29 {
30
31   public String formatSequences(String format, SequenceI[] seqs,
32           String[] omitHiddenColumns)
33   {
34
35     return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
36   }
37
38   /**
39    * create sequences with each sequence string replaced with the one given in
40    * omitHiddenCOlumns
41    * 
42    * @param seqs
43    * @param omitHiddenColumns
44    * @return new sequences
45    */
46   public SequenceI[] replaceStrings(SequenceI[] seqs,
47           String[] omitHiddenColumns)
48   {
49     if (omitHiddenColumns != null)
50     {
51       SequenceI[] tmp = new SequenceI[seqs.length];
52       for (int i = 0; i < seqs.length; i++)
53       {
54         tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
55                 seqs[i].getStart(), seqs[i].getEnd());
56         tmp[i].setDescription(seqs[i].getDescription());
57       }
58       seqs = tmp;
59     }
60     return seqs;
61   }
62
63   /**
64    * Format a vector of sequences as a flat alignment file. TODO: allow caller
65    * to detect errors and warnings encountered when generating output
66    * 
67    * 
68    * @param format
69    *          Format string as givien in the AppletFormatAdaptor list (exact
70    *          match to name of class implementing file io for that 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       String afileresp = afile.print();
134       if (afile.hasWarningMessage())
135       {
136         System.err.println("Warning raised when writing as " + format
137                 + " : " + afile.getWarningMessage());
138       }
139       return afileresp;
140     } catch (Exception e)
141     {
142       System.err.println("Failed to write alignment as a '" + format
143               + "' file\n");
144       e.printStackTrace();
145     }
146
147     return null;
148   }
149
150   public boolean getCacheSuffixDefault(String format)
151   {
152     if (isValidFormat(format))
153       return jalview.bin.Cache.getDefault(format.toUpperCase()
154               + "_JVSUFFIX", true);
155     return false;
156   }
157
158   public String formatSequences(String format, AlignmentI alignment,
159           String[] omitHidden, ColumnSelection colSel)
160   {
161     return formatSequences(format, alignment, omitHidden,
162             getCacheSuffixDefault(format), colSel, null);
163   }
164
165   public String formatSequences(String format, AlignmentI alignment,
166           String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
167   {
168     return formatSequences(format, alignment, omitHidden,
169             getCacheSuffixDefault(format), colSel, sgp);
170   }
171
172   /**
173    * hack function to replace seuqences with visible sequence strings before
174    * generating a string of the alignment in the given format.
175    * 
176    * @param format
177    * @param alignment
178    * @param omitHidden
179    *          sequence strings to write out in order of sequences in alignment
180    * @param colSel
181    *          defines hidden columns that are edited out of annotation
182    * @return string representation of the alignment formatted as format
183    */
184   public String formatSequences(String format, AlignmentI alignment,
185           String[] omitHidden, boolean suffix, ColumnSelection colSel)
186   {
187     return formatSequences(format, alignment, omitHidden, suffix, colSel,
188             null);
189   }
190
191   public String formatSequences(String format, AlignmentI alignment,
192           String[] omitHidden, boolean suffix, ColumnSelection colSel,
193           jalview.datamodel.SequenceGroup selgp)
194   {
195     if (omitHidden != null)
196     {
197       // TODO consider using AlignmentView to prune to visible region
198       // TODO prune sequence annotation and groups to visible region
199       Alignment alv = new Alignment(replaceStrings(
200               alignment.getSequencesArray(), omitHidden));
201       AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
202       if (ala != null)
203       {
204         for (int i = 0; i < ala.length; i++)
205         {
206           AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
207           if (selgp != null)
208           {
209             colSel.makeVisibleAnnotation(selgp.getStartRes(),
210                     selgp.getEndRes(), na);
211           }
212           else
213           {
214             colSel.makeVisibleAnnotation(na);
215           }
216           alv.addAnnotation(na);
217         }
218       }
219       return this.formatSequences(format, alv, suffix);
220     }
221     return this.formatSequences(format, alignment, suffix);
222   }
223
224   /**
225    * validate format is valid for IO in Application. This is basically the
226    * AppletFormatAdapter.isValidFormat call with additional checks for
227    * Application only formats like 'Jalview'.
228    * 
229    * @param format
230    *          a format string to be compared with list of readable or writable
231    *          formats (READABLE_FORMATS or WRITABLE_FORMATS)
232    * @param forwriting
233    *          when true, format is checked against list of writable formats.
234    * @return true if format is valid
235    */
236   public static final boolean isValidIOFormat(String format,
237           boolean forwriting)
238   {
239     if (format.equalsIgnoreCase("jalview"))
240     {
241       return true;
242     }
243     return AppletFormatAdapter.isValidFormat(format, forwriting);
244   }
245 }