JAL-1620 version bump and release notes
[jalview.git] / src / jalview / io / FormatAdapter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.Alignment;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31
32 /**
33  * Additional formatting methods used by the application in a number of places.
34  * 
35  * @author $author$
36  * @version $Revision$
37  */
38 public class FormatAdapter extends AppletFormatAdapter
39 {
40
41   public FormatAdapter()
42   {
43     super();
44     if (jalview.bin.Cache.getDefault("STRUCT_FROM_PDB", true))
45     {
46       annotFromStructure = jalview.bin.Cache.getDefault("ADD_TEMPFACT_ANN",
47               true);
48       localSecondaryStruct = jalview.bin.Cache.getDefault("ADD_SS_ANN",
49             true);
50     serviceSecondaryStruct = jalview.bin.Cache.getDefault("USE_RNAVIEW",
51             true);
52     }
53     else
54     {
55       // disable all PDB annotation options
56       annotFromStructure = false;
57       localSecondaryStruct = false;
58       serviceSecondaryStruct = false;
59     }
60   }
61   public String formatSequences(String format, SequenceI[] seqs,
62           String[] omitHiddenColumns)
63   {
64
65     return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
66   }
67
68   /**
69    * create sequences with each sequence string replaced with the one given in
70    * omitHiddenCOlumns
71    * 
72    * @param seqs
73    * @param omitHiddenColumns
74    * @return new sequences
75    */
76   public SequenceI[] replaceStrings(SequenceI[] seqs,
77           String[] omitHiddenColumns)
78   {
79     if (omitHiddenColumns != null)
80     {
81       SequenceI[] tmp = new SequenceI[seqs.length];
82       for (int i = 0; i < seqs.length; i++)
83       {
84         tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
85                 seqs[i].getStart(), seqs[i].getEnd());
86         tmp[i].setDescription(seqs[i].getDescription());
87       }
88       seqs = tmp;
89     }
90     return seqs;
91   }
92
93   /**
94    * Format a vector of sequences as a flat alignment file. TODO: allow caller
95    * to detect errors and warnings encountered when generating output
96    * 
97    * 
98    * @param format
99    *          Format string as givien in the AppletFormatAdaptor list (exact
100    *          match to name of class implementing file io for that format)
101    * @param seqs
102    *          vector of sequences to write
103    * 
104    * @return String containing sequences in desired format
105    */
106   public String formatSequences(String format, SequenceI[] seqs)
107   {
108
109     try
110     {
111       AlignFile afile = null;
112
113       if (format.equalsIgnoreCase("FASTA"))
114       {
115         afile = new FastaFile();
116         afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
117                 true));
118       }
119       else if (format.equalsIgnoreCase("MSF"))
120       {
121         afile = new MSFfile();
122         afile.addJVSuffix(jalview.bin.Cache
123                 .getDefault("MSF_JVSUFFIX", true));
124       }
125       else if (format.equalsIgnoreCase("PileUp"))
126       {
127         afile = new PileUpfile();
128         afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
129                 true));
130       }
131       else if (format.equalsIgnoreCase("CLUSTAL"))
132       {
133         afile = new ClustalFile();
134         afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
135                 true));
136       }
137       else if (format.equalsIgnoreCase("BLC"))
138       {
139         afile = new BLCFile();
140         afile.addJVSuffix(jalview.bin.Cache
141                 .getDefault("BLC_JVSUFFIX", true));
142       }
143       else if (format.equalsIgnoreCase("PIR"))
144       {
145         afile = new PIRFile();
146         afile.addJVSuffix(jalview.bin.Cache
147                 .getDefault("PIR_JVSUFFIX", true));
148       }
149       else if (format.equalsIgnoreCase("PFAM"))
150       {
151         afile = new PfamFile();
152         afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
153                 true));
154       }
155       /*
156        * amsa is not supported by this function - it requires an alignment
157        * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
158        * { afile = new AMSAFile(); afile.addJVSuffix(
159        * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
160        */
161
162       afile.setSeqs(seqs);
163       String afileresp = afile.print();
164       if (afile.hasWarningMessage())
165       {
166         System.err.println("Warning raised when writing as " + format
167                 + " : " + afile.getWarningMessage());
168       }
169       return afileresp;
170     } catch (Exception e)
171     {
172       System.err.println("Failed to write alignment as a '" + format
173               + "' file\n");
174       e.printStackTrace();
175     }
176
177     return null;
178   }
179
180   public boolean getCacheSuffixDefault(String format)
181   {
182     if (isValidFormat(format))
183     {
184       return jalview.bin.Cache.getDefault(format.toUpperCase()
185               + "_JVSUFFIX", true);
186     }
187     return false;
188   }
189
190   public String formatSequences(String format, AlignmentI alignment,
191           String[] omitHidden, ColumnSelection colSel)
192   {
193     return formatSequences(format, alignment, omitHidden,
194             getCacheSuffixDefault(format), colSel, null);
195   }
196
197   public String formatSequences(String format, AlignmentI alignment,
198           String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
199   {
200     return formatSequences(format, alignment, omitHidden,
201             getCacheSuffixDefault(format), colSel, sgp);
202   }
203
204   /**
205    * hack function to replace seuqences with visible sequence strings before
206    * generating a string of the alignment in the given format.
207    * 
208    * @param format
209    * @param alignment
210    * @param omitHidden
211    *          sequence strings to write out in order of sequences in alignment
212    * @param colSel
213    *          defines hidden columns that are edited out of annotation
214    * @return string representation of the alignment formatted as format
215    */
216   public String formatSequences(String format, AlignmentI alignment,
217           String[] omitHidden, boolean suffix, ColumnSelection colSel)
218   {
219     return formatSequences(format, alignment, omitHidden, suffix, colSel,
220             null);
221   }
222
223   public String formatSequences(String format, AlignmentI alignment,
224           String[] omitHidden, boolean suffix, ColumnSelection colSel,
225           jalview.datamodel.SequenceGroup selgp)
226   {
227     if (omitHidden != null)
228     {
229       // TODO consider using AlignmentView to prune to visible region
230       // TODO prune sequence annotation and groups to visible region
231       // TODO: JAL-1486 - set start and end for output correctly. basically,
232       // AlignmentView.getVisibleContigs does this.
233       Alignment alv = new Alignment(replaceStrings(
234               alignment.getSequencesArray(), omitHidden));
235       AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
236       if (ala != null)
237       {
238         for (int i = 0; i < ala.length; i++)
239         {
240           AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
241           if (selgp != null)
242           {
243             colSel.makeVisibleAnnotation(selgp.getStartRes(),
244                     selgp.getEndRes(), na);
245           }
246           else
247           {
248             colSel.makeVisibleAnnotation(na);
249           }
250           alv.addAnnotation(na);
251         }
252       }
253       return this.formatSequences(format, alv, suffix);
254     }
255     return this.formatSequences(format, alignment, suffix);
256   }
257
258   /**
259    * validate format is valid for IO in Application. This is basically the
260    * AppletFormatAdapter.isValidFormat call with additional checks for
261    * Application only formats like 'Jalview'.
262    * 
263    * @param format
264    *          a format string to be compared with list of readable or writable
265    *          formats (READABLE_FORMATS or WRITABLE_FORMATS)
266    * @param forwriting
267    *          when true, format is checked against list of writable formats.
268    * @return true if format is valid
269    */
270   public static final boolean isValidIOFormat(String format,
271           boolean forwriting)
272   {
273     if (format.equalsIgnoreCase("jalview"))
274     {
275       return true;
276     }
277     return AppletFormatAdapter.isValidFormat(format, forwriting);
278   }
279
280   /**
281    * Create a flat file representation of a given view or selected region of a view
282    * @param format
283    * @param av
284    * @return String containing flat file
285    */
286   public String formatSequences(String format, AlignViewportI av, boolean selectedOnly)
287   {
288     return formatSequences(format, getCacheSuffixDefault(format), av, selectedOnly);
289   }
290
291 }