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.*;
26 * Additional formatting methods used by the application in a number of places.
31 public class FormatAdapter extends AppletFormatAdapter
34 public String formatSequences(String format, SequenceI[] seqs,
35 String[] omitHiddenColumns)
38 return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
42 * create sequences with each sequence string replaced with the one given in
46 * @param omitHiddenColumns
47 * @return new sequences
49 public SequenceI[] replaceStrings(SequenceI[] seqs,
50 String[] omitHiddenColumns)
52 if (omitHiddenColumns != null)
54 SequenceI[] tmp = new SequenceI[seqs.length];
55 for (int i = 0; i < seqs.length; i++)
57 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
58 seqs[i].getStart(), seqs[i].getEnd());
59 tmp[i].setDescription(seqs[i].getDescription());
67 * Format a vector of sequences as a flat alignment file. TODO: allow caller
68 * to detect errors and warnings encountered when generating output
72 * Format string as givien in the AppletFormatAdaptor list (exact
73 * match to name of class implementing file io for that format)
75 * vector of sequences to write
77 * @return String containing sequences in desired format
79 public String formatSequences(String format, SequenceI[] seqs)
84 AlignFile afile = null;
86 if (format.equalsIgnoreCase("FASTA"))
88 afile = new FastaFile();
89 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
92 else if (format.equalsIgnoreCase("MSF"))
94 afile = new MSFfile();
95 afile.addJVSuffix(jalview.bin.Cache
96 .getDefault("MSF_JVSUFFIX", true));
98 else if (format.equalsIgnoreCase("PileUp"))
100 afile = new PileUpfile();
101 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
104 else if (format.equalsIgnoreCase("CLUSTAL"))
106 afile = new ClustalFile();
107 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
110 else if (format.equalsIgnoreCase("BLC"))
112 afile = new BLCFile();
113 afile.addJVSuffix(jalview.bin.Cache
114 .getDefault("BLC_JVSUFFIX", true));
116 else if (format.equalsIgnoreCase("PIR"))
118 afile = new PIRFile();
119 afile.addJVSuffix(jalview.bin.Cache
120 .getDefault("PIR_JVSUFFIX", true));
122 else if (format.equalsIgnoreCase("PFAM"))
124 afile = new PfamFile();
125 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
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)); }
136 String afileresp = afile.print();
137 if (afile.hasWarningMessage())
139 System.err.println("Warning raised when writing as " + format
140 + " : " + afile.getWarningMessage());
143 } catch (Exception e)
145 System.err.println("Failed to write alignment as a '" + format
153 public boolean getCacheSuffixDefault(String format)
155 if (isValidFormat(format))
156 return jalview.bin.Cache.getDefault(format.toUpperCase()
157 + "_JVSUFFIX", true);
161 public String formatSequences(String format, AlignmentI alignment,
162 String[] omitHidden, ColumnSelection colSel)
164 return formatSequences(format, alignment, omitHidden,
165 getCacheSuffixDefault(format), colSel, null);
168 public String formatSequences(String format, AlignmentI alignment,
169 String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
171 return formatSequences(format, alignment, omitHidden,
172 getCacheSuffixDefault(format), colSel, sgp);
176 * hack function to replace seuqences with visible sequence strings before
177 * generating a string of the alignment in the given format.
182 * sequence strings to write out in order of sequences in alignment
184 * defines hidden columns that are edited out of annotation
185 * @return string representation of the alignment formatted as format
187 public String formatSequences(String format, AlignmentI alignment,
188 String[] omitHidden, boolean suffix, ColumnSelection colSel)
190 return formatSequences(format, alignment, omitHidden, suffix, colSel,
194 public String formatSequences(String format, AlignmentI alignment,
195 String[] omitHidden, boolean suffix, ColumnSelection colSel,
196 jalview.datamodel.SequenceGroup selgp)
198 if (omitHidden != null)
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();
209 for (int i = 0; i < ala.length; i++)
211 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
214 colSel.makeVisibleAnnotation(selgp.getStartRes(),
215 selgp.getEndRes(), na);
219 colSel.makeVisibleAnnotation(na);
221 alv.addAnnotation(na);
224 return this.formatSequences(format, alv, suffix);
226 return this.formatSequences(format, alignment, suffix);
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'.
235 * a format string to be compared with list of readable or writable
236 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
238 * when true, format is checked against list of writable formats.
239 * @return true if format is valid
241 public static final boolean isValidIOFormat(String format,
244 if (format.equalsIgnoreCase("jalview"))
248 return AppletFormatAdapter.isValidFormat(format, forwriting);