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