416773d9035986e9b59ee92537fa3f415a616332
[jalview.git] / src / jalview / io / FormatAdapter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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 String formatSequences(String format, SequenceI[] seqs,
42           String[] omitHiddenColumns)
43   {
44
45     return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
46   }
47
48   /**
49    * create sequences with each sequence string replaced with the one given in
50    * omitHiddenCOlumns
51    * 
52    * @param seqs
53    * @param omitHiddenColumns
54    * @return new sequences
55    */
56   public SequenceI[] replaceStrings(SequenceI[] seqs,
57           String[] omitHiddenColumns)
58   {
59     if (omitHiddenColumns != null)
60     {
61       SequenceI[] tmp = new SequenceI[seqs.length];
62       for (int i = 0; i < seqs.length; i++)
63       {
64         tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
65                 seqs[i].getStart(), seqs[i].getEnd());
66         tmp[i].setDescription(seqs[i].getDescription());
67       }
68       seqs = tmp;
69     }
70     return seqs;
71   }
72
73   /**
74    * Format a vector of sequences as a flat alignment file. TODO: allow caller
75    * to detect errors and warnings encountered when generating output
76    * 
77    * 
78    * @param format
79    *          Format string as givien in the AppletFormatAdaptor list (exact
80    *          match to name of class implementing file io for that format)
81    * @param seqs
82    *          vector of sequences to write
83    * 
84    * @return String containing sequences in desired format
85    */
86   public String formatSequences(String format, SequenceI[] seqs)
87   {
88
89     try
90     {
91       AlignFile afile = null;
92
93       if (format.equalsIgnoreCase("FASTA"))
94       {
95         afile = new FastaFile();
96         afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
97                 true));
98       }
99       else if (format.equalsIgnoreCase("MSF"))
100       {
101         afile = new MSFfile();
102         afile.addJVSuffix(jalview.bin.Cache
103                 .getDefault("MSF_JVSUFFIX", true));
104       }
105       else if (format.equalsIgnoreCase("PileUp"))
106       {
107         afile = new PileUpfile();
108         afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
109                 true));
110       }
111       else if (format.equalsIgnoreCase("CLUSTAL"))
112       {
113         afile = new ClustalFile();
114         afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
115                 true));
116       }
117       else if (format.equalsIgnoreCase("BLC"))
118       {
119         afile = new BLCFile();
120         afile.addJVSuffix(jalview.bin.Cache
121                 .getDefault("BLC_JVSUFFIX", true));
122       }
123       else if (format.equalsIgnoreCase("PIR"))
124       {
125         afile = new PIRFile();
126         afile.addJVSuffix(jalview.bin.Cache
127                 .getDefault("PIR_JVSUFFIX", true));
128       }
129       else if (format.equalsIgnoreCase("PFAM"))
130       {
131         afile = new PfamFile();
132         afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
133                 true));
134       }
135       /*
136        * amsa is not supported by this function - it requires an alignment
137        * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
138        * { afile = new AMSAFile(); afile.addJVSuffix(
139        * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
140        */
141
142       afile.setSeqs(seqs);
143       String afileresp = afile.print();
144       if (afile.hasWarningMessage())
145       {
146         System.err.println("Warning raised when writing as " + format
147                 + " : " + afile.getWarningMessage());
148       }
149       return afileresp;
150     } catch (Exception e)
151     {
152       System.err.println("Failed to write alignment as a '" + format
153               + "' file\n");
154       e.printStackTrace();
155     }
156
157     return null;
158   }
159
160   public boolean getCacheSuffixDefault(String format)
161   {
162     if (isValidFormat(format))
163     {
164       return jalview.bin.Cache.getDefault(format.toUpperCase()
165               + "_JVSUFFIX", true);
166     }
167     return false;
168   }
169
170   public String formatSequences(String format, AlignmentI alignment,
171           String[] omitHidden, ColumnSelection colSel)
172   {
173     return formatSequences(format, alignment, omitHidden,
174             getCacheSuffixDefault(format), colSel, null);
175   }
176
177   public String formatSequences(String format, AlignmentI alignment,
178           String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
179   {
180     return formatSequences(format, alignment, omitHidden,
181             getCacheSuffixDefault(format), colSel, sgp);
182   }
183
184   /**
185    * hack function to replace seuqences with visible sequence strings before
186    * generating a string of the alignment in the given format.
187    * 
188    * @param format
189    * @param alignment
190    * @param omitHidden
191    *          sequence strings to write out in order of sequences in alignment
192    * @param colSel
193    *          defines hidden columns that are edited out of annotation
194    * @return string representation of the alignment formatted as format
195    */
196   public String formatSequences(String format, AlignmentI alignment,
197           String[] omitHidden, boolean suffix, ColumnSelection colSel)
198   {
199     return formatSequences(format, alignment, omitHidden, suffix, colSel,
200             null);
201   }
202
203   public String formatSequences(String format, AlignmentI alignment,
204           String[] omitHidden, boolean suffix, ColumnSelection colSel,
205           jalview.datamodel.SequenceGroup selgp)
206   {
207     if (omitHidden != null)
208     {
209       // TODO consider using AlignmentView to prune to visible region
210       // TODO prune sequence annotation and groups to visible region
211       // TODO: JAL-1486 - set start and end for output correctly. basically,
212       // AlignmentView.getVisibleContigs does this.
213       Alignment alv = new Alignment(replaceStrings(
214               alignment.getSequencesArray(), omitHidden));
215       AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
216       if (ala != null)
217       {
218         for (int i = 0; i < ala.length; i++)
219         {
220           AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
221           if (selgp != null)
222           {
223             colSel.makeVisibleAnnotation(selgp.getStartRes(),
224                     selgp.getEndRes(), na);
225           }
226           else
227           {
228             colSel.makeVisibleAnnotation(na);
229           }
230           alv.addAnnotation(na);
231         }
232       }
233       return this.formatSequences(format, alv, suffix);
234     }
235     return this.formatSequences(format, alignment, suffix);
236   }
237
238   public Alignment readFile(String inFile, String type, String format)
239           throws java.io.IOException
240   {
241     Alignment al;
242     if (format.equals("HTML"))
243     {
244       afile = new HtmlFile(inFile, type);
245       al = new Alignment(afile.getSeqsAsArray());
246       afile.addAnnotations(al);
247     }
248     else
249     {
250       al = super.readFile(inFile, type, format);
251     }
252
253     return al;
254   }
255
256   public AlignmentI readFromFile(FileParse source, String format)
257           throws java.io.IOException
258   {
259     Alignment al;
260     if (format.equals("HTML"))
261     {
262       afile = new HtmlFile(source);
263       al = new Alignment(afile.getSeqsAsArray());
264       afile.addAnnotations(al);
265     }
266     else
267     {
268       al = (Alignment) super.readFromFile(source, format);
269     }
270     return al;
271   }
272
273   /**
274    * validate format is valid for IO in Application. This is basically the
275    * AppletFormatAdapter.isValidFormat call with additional checks for
276    * Application only formats like 'Jalview'.
277    * 
278    * @param format
279    *          a format string to be compared with list of readable or writable
280    *          formats (READABLE_FORMATS or WRITABLE_FORMATS)
281    * @param forwriting
282    *          when true, format is checked against list of writable formats.
283    * @return true if format is valid
284    */
285   public static final boolean isValidIOFormat(String format,
286           boolean forwriting)
287   {
288     if (format.equalsIgnoreCase("jalview"))
289     {
290       return true;
291     }
292     return AppletFormatAdapter.isValidFormat(format, forwriting);
293   }
294
295   /**
296    * Create a flat file representation of a given view or selected region of a view
297    * @param format
298    * @param av
299    * @return String containing flat file
300    */
301   public String formatSequences(String format, AlignViewportI av, boolean selectedOnly)
302   {
303     return formatSequences(format, getCacheSuffixDefault(format), av, selectedOnly);
304   }
305
306 }