JAL-1503 update version in GPL header
[jalview.git] / src / jalview / io / FormatAdapter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
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 sequence 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. TODO: allow caller
66    * to detect errors and warnings encountered when generating output
67    * 
68    * 
69    * @param format
70    *          Format string as givien in the AppletFormatAdaptor list (exact
71    *          match to name of class implementing file io for that format)
72    * @param seqs
73    *          vector of sequences to write
74    * 
75    * @return String containing sequences in desired format
76    */
77   public String formatSequences(String format, SequenceI[] seqs)
78   {
79
80     try
81     {
82       AlignFile afile = null;
83
84       if (format.equalsIgnoreCase("FASTA"))
85       {
86         afile = new FastaFile();
87         afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
88                 true));
89       }
90       else if (format.equalsIgnoreCase("MSF"))
91       {
92         afile = new MSFfile();
93         afile.addJVSuffix(jalview.bin.Cache
94                 .getDefault("MSF_JVSUFFIX", true));
95       }
96       else if (format.equalsIgnoreCase("PileUp"))
97       {
98         afile = new PileUpfile();
99         afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
100                 true));
101       }
102       else if (format.equalsIgnoreCase("CLUSTAL"))
103       {
104         afile = new ClustalFile();
105         afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
106                 true));
107       }
108       else if (format.equalsIgnoreCase("BLC"))
109       {
110         afile = new BLCFile();
111         afile.addJVSuffix(jalview.bin.Cache
112                 .getDefault("BLC_JVSUFFIX", true));
113       }
114       else if (format.equalsIgnoreCase("PIR"))
115       {
116         afile = new PIRFile();
117         afile.addJVSuffix(jalview.bin.Cache
118                 .getDefault("PIR_JVSUFFIX", true));
119       }
120       else if (format.equalsIgnoreCase("PFAM"))
121       {
122         afile = new PfamFile();
123         afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
124                 true));
125       }
126       /*
127        * amsa is not supported by this function - it requires an alignment
128        * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
129        * { afile = new AMSAFile(); afile.addJVSuffix(
130        * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
131        */
132
133       afile.setSeqs(seqs);
134       String afileresp = afile.print();
135       if (afile.hasWarningMessage())
136       {
137         System.err.println("Warning raised when writing as " + format
138                 + " : " + 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 alignment
181    * @param colSel
182    *          defines hidden columns that are edited out of annotation
183    * @return string representation of the alignment formatted as format
184    */
185   public String formatSequences(String format, AlignmentI alignment,
186           String[] omitHidden, boolean suffix, ColumnSelection colSel)
187   {
188     return formatSequences(format, alignment, omitHidden, suffix, colSel,
189             null);
190   }
191
192   public String formatSequences(String format, AlignmentI alignment,
193           String[] omitHidden, boolean suffix, ColumnSelection colSel,
194           jalview.datamodel.SequenceGroup selgp)
195   {
196     if (omitHidden != null)
197     {
198       // TODO consider using AlignmentView to prune to visible region
199       // TODO prune sequence annotation and groups to visible region
200       // TODO: JAL-1486 - set start and end for output correctly. basically, AlignmentView.getVisibleContigs does this.
201       Alignment alv = new Alignment(replaceStrings(
202               alignment.getSequencesArray(), omitHidden));
203       AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
204       if (ala != null)
205       {
206         for (int i = 0; i < ala.length; i++)
207         {
208           AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
209           if (selgp != null)
210           {
211             colSel.makeVisibleAnnotation(selgp.getStartRes(),
212                     selgp.getEndRes(), na);
213           }
214           else
215           {
216             colSel.makeVisibleAnnotation(na);
217           }
218           alv.addAnnotation(na);
219         }
220       }
221       return this.formatSequences(format, alv, suffix);
222     }
223     return this.formatSequences(format, alignment, suffix);
224   }
225
226   /**
227    * validate format is valid for IO in Application. This is basically the
228    * AppletFormatAdapter.isValidFormat call with additional checks for
229    * Application only formats like 'Jalview'.
230    * 
231    * @param format
232    *          a format string to be compared with list of readable or writable
233    *          formats (READABLE_FORMATS or WRITABLE_FORMATS)
234    * @param forwriting
235    *          when true, format is checked against list of writable formats.
236    * @return true if format is valid
237    */
238   public static final boolean isValidIOFormat(String format,
239           boolean forwriting)
240   {
241     if (format.equalsIgnoreCase("jalview"))
242     {
243       return true;
244     }
245     return AppletFormatAdapter.isValidFormat(format, forwriting);
246   }
247 }