2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.datamodel.*;
23 * Additional formatting methods used by the application in a number of places.
28 public class FormatAdapter extends AppletFormatAdapter
31 public String formatSequences(String format, SequenceI[] seqs,
32 String[] omitHiddenColumns)
35 return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
39 * create sequences with each seuqence string replaced with the one given in
43 * @param omitHiddenColumns
44 * @return new sequences
46 public SequenceI[] replaceStrings(SequenceI[] seqs,
47 String[] omitHiddenColumns)
49 if (omitHiddenColumns != null)
51 SequenceI[] tmp = new SequenceI[seqs.length];
52 for (int i = 0; i < seqs.length; i++)
54 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
55 seqs[i].getStart(), seqs[i].getEnd());
56 tmp[i].setDescription(seqs[i].getDescription());
64 * Format a vector of sequences as a flat alignment file. TODO: allow caller
65 * to detect errors and warnings encountered when generating output
69 * Format string as givien in the AppletFormatAdaptor list (exact
70 * match to name of class implementing file io for that format)
72 * vector of sequences to write
74 * @return String containing sequences in desired format
76 public String formatSequences(String format, SequenceI[] seqs)
81 AlignFile afile = null;
83 if (format.equalsIgnoreCase("FASTA"))
85 afile = new FastaFile();
86 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
89 else if (format.equalsIgnoreCase("MSF"))
91 afile = new MSFfile();
92 afile.addJVSuffix(jalview.bin.Cache
93 .getDefault("MSF_JVSUFFIX", true));
95 else if (format.equalsIgnoreCase("PileUp"))
97 afile = new PileUpfile();
98 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
101 else if (format.equalsIgnoreCase("CLUSTAL"))
103 afile = new ClustalFile();
104 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
107 else if (format.equalsIgnoreCase("BLC"))
109 afile = new BLCFile();
110 afile.addJVSuffix(jalview.bin.Cache
111 .getDefault("BLC_JVSUFFIX", true));
113 else if (format.equalsIgnoreCase("PIR"))
115 afile = new PIRFile();
116 afile.addJVSuffix(jalview.bin.Cache
117 .getDefault("PIR_JVSUFFIX", true));
119 else if (format.equalsIgnoreCase("PFAM"))
121 afile = new PfamFile();
122 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
126 * amsa is not supported by this function - it requires an alignment
127 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
128 * { afile = new AMSAFile(); afile.addJVSuffix(
129 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
133 String afileresp = afile.print();
134 if (afile.hasWarningMessage())
136 System.err.println("Warning raised when writing as " + format
137 + " : " + afile.getWarningMessage());
140 } catch (Exception e)
142 System.err.println("Failed to write alignment as a '" + format
150 public boolean getCacheSuffixDefault(String format)
152 if (isValidFormat(format))
153 return jalview.bin.Cache.getDefault(format.toUpperCase()
154 + "_JVSUFFIX", true);
158 public String formatSequences(String format, AlignmentI alignment,
159 String[] omitHidden, ColumnSelection colSel)
161 return formatSequences(format, alignment, omitHidden,
162 getCacheSuffixDefault(format), colSel, null);
165 public String formatSequences(String format, AlignmentI alignment,
166 String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
168 return formatSequences(format, alignment, omitHidden,
169 getCacheSuffixDefault(format), colSel, sgp);
173 * hack function to replace seuqences with visible sequence strings before
174 * generating a string of the alignment in the given format.
179 * sequence strings to write out in order of sequences in alignment
181 * defines hidden columns that are edited out of annotation
182 * @return string representation of the alignment formatted as format
184 public String formatSequences(String format, AlignmentI alignment,
185 String[] omitHidden, boolean suffix, ColumnSelection colSel)
187 return formatSequences(format, alignment, omitHidden, suffix, colSel,
191 public String formatSequences(String format, AlignmentI alignment,
192 String[] omitHidden, boolean suffix, ColumnSelection colSel,
193 jalview.datamodel.SequenceGroup selgp)
195 if (omitHidden != null)
198 Alignment alv = new Alignment(replaceStrings(alignment
199 .getSequencesArray(), omitHidden));
200 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);
218 return this.formatSequences(format, alv, suffix);
220 return this.formatSequences(format, alignment, suffix);
224 * validate format is valid for IO in Application. This is basically the
225 * AppletFormatAdapter.isValidFormat call with additional checks for
226 * Application only formats like 'Jalview'.
229 * a format string to be compared with list of readable or writable
230 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
232 * when true, format is checked against list of writable formats.
233 * @return true if format is valid
235 public static final boolean isValidIOFormat(String format,
238 if (format.equalsIgnoreCase("jalview"))
242 return AppletFormatAdapter.isValidFormat(format, forwriting);