2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import jalview.datamodel.*;
24 * Additional formatting methods used by the application in a number of places.
29 public class FormatAdapter extends AppletFormatAdapter
32 public String formatSequences(String format, SequenceI[] seqs,
33 String[] omitHiddenColumns)
36 return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
40 * create sequences with each seuqence string replaced with the one given in
44 * @param omitHiddenColumns
45 * @return new sequences
47 public SequenceI[] replaceStrings(SequenceI[] seqs,
48 String[] omitHiddenColumns)
50 if (omitHiddenColumns != null)
52 SequenceI[] tmp = new SequenceI[seqs.length];
53 for (int i = 0; i < seqs.length; i++)
55 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
56 seqs[i].getStart(), seqs[i].getEnd());
57 tmp[i].setDescription(seqs[i].getDescription());
65 * Format a vector of sequences as a flat alignment file.
66 * TODO: allow caller to detect errors and warnings encountered when generating output
70 * Format string as givien in the AppletFormatAdaptor list
71 * (exact match to name of class implementing file io for that
74 * vector of sequences to write
76 * @return String containing sequences in desired format
78 public String formatSequences(String format, SequenceI[] seqs)
83 AlignFile afile = null;
85 if (format.equalsIgnoreCase("FASTA"))
87 afile = new FastaFile();
88 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
91 else if (format.equalsIgnoreCase("MSF"))
93 afile = new MSFfile();
94 afile.addJVSuffix(jalview.bin.Cache
95 .getDefault("MSF_JVSUFFIX", true));
97 else if (format.equalsIgnoreCase("PileUp"))
99 afile = new PileUpfile();
100 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
103 else if (format.equalsIgnoreCase("CLUSTAL"))
105 afile = new ClustalFile();
106 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
109 else if (format.equalsIgnoreCase("BLC"))
111 afile = new BLCFile();
112 afile.addJVSuffix(jalview.bin.Cache
113 .getDefault("BLC_JVSUFFIX", true));
115 else if (format.equalsIgnoreCase("PIR"))
117 afile = new PIRFile();
118 afile.addJVSuffix(jalview.bin.Cache
119 .getDefault("PIR_JVSUFFIX", true));
121 else if (format.equalsIgnoreCase("PFAM"))
123 afile = new PfamFile();
124 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
128 * amsa is not supported by this function - it requires an alignment
129 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA")) {
130 * afile = new AMSAFile(); afile.addJVSuffix(
131 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
135 String afileresp = afile.print();
136 if (afile.hasWarningMessage())
138 System.err.println("Warning raised when writing as "+format+" : "+afile.getWarningMessage());
141 } catch (Exception e)
143 System.err.println("Failed to write alignment as a '" + format
151 public boolean getCacheSuffixDefault(String format)
153 if (isValidFormat(format))
154 return jalview.bin.Cache.getDefault(format.toUpperCase()
155 + "_JVSUFFIX", true);
159 public String formatSequences(String format, AlignmentI alignment,
160 String[] omitHidden, ColumnSelection colSel)
162 return formatSequences(format, alignment, omitHidden,
163 getCacheSuffixDefault(format), colSel, null);
166 public String formatSequences(String format, AlignmentI alignment,
167 String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
169 return formatSequences(format, alignment, omitHidden,
170 getCacheSuffixDefault(format), colSel, sgp);
174 * hack function to replace seuqences with visible sequence strings before
175 * generating a string of the alignment in the given format.
180 * sequence strings to write out in order of sequences in
183 * defines hidden columns that are edited out of annotation
184 * @return string representation of the alignment formatted as format
186 public String formatSequences(String format, AlignmentI alignment,
187 String[] omitHidden, boolean suffix, ColumnSelection colSel)
189 return formatSequences(format, alignment, omitHidden, suffix, colSel,
193 public String formatSequences(String format, AlignmentI alignment,
194 String[] omitHidden, boolean suffix, ColumnSelection colSel,
195 jalview.datamodel.SequenceGroup selgp)
197 if (omitHidden != null)
200 Alignment alv = new Alignment(replaceStrings(alignment
201 .getSequencesArray(), omitHidden));
202 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
203 for (int i = 0; i < ala.length; i++)
205 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
208 colSel.makeVisibleAnnotation(selgp.getStartRes(), selgp
213 colSel.makeVisibleAnnotation(na);
215 alv.addAnnotation(na);
217 return this.formatSequences(format, alv, suffix);
219 return this.formatSequences(format, alignment, suffix);
223 * validate format is valid for IO in Application. This is basically the
224 * AppletFormatAdapter.isValidFormat call with additional checks for
225 * Application only formats like 'Jalview'.
228 * a format string to be compared with list of readable or writable formats (READABLE_FORMATS
229 * or WRITABLE_FORMATS)
231 * when true, format is checked against list of writable formats.
232 * @return true if format is valid
234 public static final boolean isValidIOFormat(String format,
237 if (format.equalsIgnoreCase("jalview"))
241 return AppletFormatAdapter.isValidFormat(format, forwriting);