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.api.AlignViewportI;
24 import jalview.datamodel.*;
27 * Additional formatting methods used by the application in a number of places.
32 public class FormatAdapter extends AppletFormatAdapter
35 public String formatSequences(String format, SequenceI[] seqs,
36 String[] omitHiddenColumns)
39 return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
43 * create sequences with each sequence string replaced with the one given in
47 * @param omitHiddenColumns
48 * @return new sequences
50 public SequenceI[] replaceStrings(SequenceI[] seqs,
51 String[] omitHiddenColumns)
53 if (omitHiddenColumns != null)
55 SequenceI[] tmp = new SequenceI[seqs.length];
56 for (int i = 0; i < seqs.length; i++)
58 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
59 seqs[i].getStart(), seqs[i].getEnd());
60 tmp[i].setDescription(seqs[i].getDescription());
68 * Format a vector of sequences as a flat alignment file. TODO: allow caller
69 * to detect errors and warnings encountered when generating output
73 * Format string as givien in the AppletFormatAdaptor list (exact
74 * match to name of class implementing file io for that format)
76 * vector of sequences to write
78 * @return String containing sequences in desired format
80 public String formatSequences(String format, SequenceI[] seqs)
85 AlignFile afile = null;
87 if (format.equalsIgnoreCase("FASTA"))
89 afile = new FastaFile();
90 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
93 else if (format.equalsIgnoreCase("MSF"))
95 afile = new MSFfile();
96 afile.addJVSuffix(jalview.bin.Cache
97 .getDefault("MSF_JVSUFFIX", true));
99 else if (format.equalsIgnoreCase("PileUp"))
101 afile = new PileUpfile();
102 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
105 else if (format.equalsIgnoreCase("CLUSTAL"))
107 afile = new ClustalFile();
108 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
111 else if (format.equalsIgnoreCase("BLC"))
113 afile = new BLCFile();
114 afile.addJVSuffix(jalview.bin.Cache
115 .getDefault("BLC_JVSUFFIX", true));
117 else if (format.equalsIgnoreCase("PIR"))
119 afile = new PIRFile();
120 afile.addJVSuffix(jalview.bin.Cache
121 .getDefault("PIR_JVSUFFIX", true));
123 else if (format.equalsIgnoreCase("PFAM"))
125 afile = new PfamFile();
126 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
130 * amsa is not supported by this function - it requires an alignment
131 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
132 * { afile = new AMSAFile(); afile.addJVSuffix(
133 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
137 String afileresp = afile.print();
138 if (afile.hasWarningMessage())
140 System.err.println("Warning raised when writing as " + format
141 + " : " + afile.getWarningMessage());
144 } catch (Exception e)
146 System.err.println("Failed to write alignment as a '" + format
154 public boolean getCacheSuffixDefault(String format)
156 if (isValidFormat(format))
157 return jalview.bin.Cache.getDefault(format.toUpperCase()
158 + "_JVSUFFIX", true);
162 public String formatSequences(String format, AlignmentI alignment,
163 String[] omitHidden, ColumnSelection colSel)
165 return formatSequences(format, alignment, omitHidden,
166 getCacheSuffixDefault(format), colSel, null);
169 public String formatSequences(String format, AlignmentI alignment,
170 String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
172 return formatSequences(format, alignment, omitHidden,
173 getCacheSuffixDefault(format), colSel, sgp);
177 * hack function to replace seuqences with visible sequence strings before
178 * generating a string of the alignment in the given format.
183 * sequence strings to write out in order of sequences in alignment
185 * defines hidden columns that are edited out of annotation
186 * @return string representation of the alignment formatted as format
188 public String formatSequences(String format, AlignmentI alignment,
189 String[] omitHidden, boolean suffix, ColumnSelection colSel)
191 return formatSequences(format, alignment, omitHidden, suffix, colSel,
195 public String formatSequences(String format, AlignmentI alignment,
196 String[] omitHidden, boolean suffix, ColumnSelection colSel,
197 jalview.datamodel.SequenceGroup selgp)
199 if (omitHidden != null)
201 // TODO consider using AlignmentView to prune to visible region
202 // TODO prune sequence annotation and groups to visible region
203 // TODO: JAL-1486 - set start and end for output correctly. basically,
204 // AlignmentView.getVisibleContigs does this.
205 Alignment alv = new Alignment(replaceStrings(
206 alignment.getSequencesArray(), omitHidden));
207 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
210 for (int i = 0; i < ala.length; i++)
212 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
215 colSel.makeVisibleAnnotation(selgp.getStartRes(),
216 selgp.getEndRes(), na);
220 colSel.makeVisibleAnnotation(na);
222 alv.addAnnotation(na);
225 return this.formatSequences(format, alv, suffix);
227 return this.formatSequences(format, alignment, suffix);
231 * validate format is valid for IO in Application. This is basically the
232 * AppletFormatAdapter.isValidFormat call with additional checks for
233 * Application only formats like 'Jalview'.
236 * a format string to be compared with list of readable or writable
237 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
239 * when true, format is checked against list of writable formats.
240 * @return true if format is valid
242 public static final boolean isValidIOFormat(String format,
245 if (format.equalsIgnoreCase("jalview"))
249 return AppletFormatAdapter.isValidFormat(format, forwriting);
253 * Create a flat file representation of a given view or selected region of a view
256 * @return String containing flat file
258 public String formatSequences(String format, AlignViewportI av, boolean selectedOnly)
260 return formatSequences(format, getCacheSuffixDefault(format), av, selectedOnly);