2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
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.
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.
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.ColumnSelection;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceGroup;
29 import jalview.datamodel.SequenceI;
32 * Additional formatting methods used by the application in a number of places.
37 public class FormatAdapter extends AppletFormatAdapter
40 public String formatSequences(String format, SequenceI[] seqs,
41 String[] omitHiddenColumns)
44 return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
48 * create sequences with each sequence string replaced with the one given in
52 * @param omitHiddenColumns
53 * @return new sequences
55 public SequenceI[] replaceStrings(SequenceI[] seqs,
56 String[] omitHiddenColumns)
58 if (omitHiddenColumns != null)
60 SequenceI[] tmp = new SequenceI[seqs.length];
61 for (int i = 0; i < seqs.length; i++)
63 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
64 seqs[i].getStart(), seqs[i].getEnd());
65 tmp[i].setDescription(seqs[i].getDescription());
73 * Format a vector of sequences as a flat alignment file. TODO: allow caller
74 * to detect errors and warnings encountered when generating output
78 * Format string as givien in the AppletFormatAdaptor list (exact
79 * match to name of class implementing file io for that format)
81 * vector of sequences to write
83 * @return String containing sequences in desired format
85 public String formatSequences(String format, SequenceI[] seqs)
90 AlignFile afile = null;
92 if (format.equalsIgnoreCase("FASTA"))
94 afile = new FastaFile();
95 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
98 else if (format.equalsIgnoreCase("MSF"))
100 afile = new MSFfile();
101 afile.addJVSuffix(jalview.bin.Cache
102 .getDefault("MSF_JVSUFFIX", true));
104 else if (format.equalsIgnoreCase("PileUp"))
106 afile = new PileUpfile();
107 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
110 else if (format.equalsIgnoreCase("CLUSTAL"))
112 afile = new ClustalFile();
113 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
116 else if (format.equalsIgnoreCase("BLC"))
118 afile = new BLCFile();
119 afile.addJVSuffix(jalview.bin.Cache
120 .getDefault("BLC_JVSUFFIX", true));
122 else if (format.equalsIgnoreCase("PIR"))
124 afile = new PIRFile();
125 afile.addJVSuffix(jalview.bin.Cache
126 .getDefault("PIR_JVSUFFIX", true));
128 else if (format.equalsIgnoreCase("PFAM"))
130 afile = new PfamFile();
131 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
134 else if (format.equalsIgnoreCase("MEGA"))
136 afile = new MegaFile();
137 afile.addJVSuffix(jalview.bin.Cache.getDefault("MEGA_JVSUFFIX",
141 * amsa is not supported by this function - it requires an alignment
142 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
143 * { afile = new AMSAFile(); afile.addJVSuffix(
144 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
148 String afileresp = afile.print();
149 if (afile.hasWarningMessage())
151 System.err.println("Warning raised when writing as " + format
152 + " : " + afile.getWarningMessage());
155 } catch (Exception e)
157 System.err.println("Failed to write alignment as a '" + format
165 public boolean getCacheSuffixDefault(String format)
167 if (isValidFormat(format))
168 return jalview.bin.Cache.getDefault(format.toUpperCase()
169 + "_JVSUFFIX", true);
173 public String formatSequences(String format, AlignmentI alignment,
174 String[] omitHidden, ColumnSelection colSel)
176 return formatSequences(format, alignment, omitHidden,
177 getCacheSuffixDefault(format), colSel, null);
180 public String formatSequences(String format, AlignmentI alignment,
181 String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
183 return formatSequences(format, alignment, omitHidden,
184 getCacheSuffixDefault(format), colSel, sgp);
188 * hack function to replace seuqences with visible sequence strings before
189 * generating a string of the alignment in the given format.
194 * sequence strings to write out in order of sequences in alignment
196 * defines hidden columns that are edited out of annotation
197 * @return string representation of the alignment formatted as format
199 public String formatSequences(String format, AlignmentI alignment,
200 String[] omitHidden, boolean suffix, ColumnSelection colSel)
202 return formatSequences(format, alignment, omitHidden, suffix, colSel,
206 public String formatSequences(String format, AlignmentI alignment,
207 String[] omitHidden, boolean suffix, ColumnSelection colSel,
208 jalview.datamodel.SequenceGroup selgp)
210 if (omitHidden != null)
212 // TODO consider using AlignmentView to prune to visible region
213 // TODO prune sequence annotation and groups to visible region
214 // TODO: JAL-1486 - set start and end for output correctly. basically,
215 // AlignmentView.getVisibleContigs does this.
216 Alignment alv = new Alignment(replaceStrings(
217 alignment.getSequencesArray(), omitHidden));
218 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
221 for (int i = 0; i < ala.length; i++)
223 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
226 colSel.makeVisibleAnnotation(selgp.getStartRes(),
227 selgp.getEndRes(), na);
231 colSel.makeVisibleAnnotation(na);
233 alv.addAnnotation(na);
236 return this.formatSequences(format, alv, suffix);
238 return this.formatSequences(format, alignment, suffix);
242 * validate format is valid for IO in Application. This is basically the
243 * AppletFormatAdapter.isValidFormat call with additional checks for
244 * Application only formats like 'Jalview'.
247 * a format string to be compared with list of readable or writable
248 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
250 * when true, format is checked against list of writable formats.
251 * @return true if format is valid
253 public static final boolean isValidIOFormat(String format,
256 if (format.equalsIgnoreCase("jalview"))
260 return AppletFormatAdapter.isValidFormat(format, forwriting);