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