2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
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 sequence 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. TODO: allow caller
66 * to detect errors and warnings encountered when generating output
70 * Format string as givien in the AppletFormatAdaptor list (exact
71 * match to name of class implementing file io for that format)
73 * vector of sequences to write
75 * @return String containing sequences in desired format
77 public String formatSequences(String format, SequenceI[] seqs)
82 AlignFile afile = null;
84 if (format.equalsIgnoreCase("FASTA"))
86 afile = new FastaFile();
87 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
90 else if (format.equalsIgnoreCase("MSF"))
92 afile = new MSFfile();
93 afile.addJVSuffix(jalview.bin.Cache
94 .getDefault("MSF_JVSUFFIX", true));
96 else if (format.equalsIgnoreCase("PileUp"))
98 afile = new PileUpfile();
99 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
102 else if (format.equalsIgnoreCase("CLUSTAL"))
104 afile = new ClustalFile();
105 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
108 else if (format.equalsIgnoreCase("BLC"))
110 afile = new BLCFile();
111 afile.addJVSuffix(jalview.bin.Cache
112 .getDefault("BLC_JVSUFFIX", true));
114 else if (format.equalsIgnoreCase("PIR"))
116 afile = new PIRFile();
117 afile.addJVSuffix(jalview.bin.Cache
118 .getDefault("PIR_JVSUFFIX", true));
120 else if (format.equalsIgnoreCase("PFAM"))
122 afile = new PfamFile();
123 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
127 * amsa is not supported by this function - it requires an alignment
128 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
129 * { afile = new AMSAFile(); afile.addJVSuffix(
130 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
134 String afileresp = afile.print();
135 if (afile.hasWarningMessage())
137 System.err.println("Warning raised when writing as " + format
138 + " : " + 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 alignment
182 * defines hidden columns that are edited out of annotation
183 * @return string representation of the alignment formatted as format
185 public String formatSequences(String format, AlignmentI alignment,
186 String[] omitHidden, boolean suffix, ColumnSelection colSel)
188 return formatSequences(format, alignment, omitHidden, suffix, colSel,
192 public String formatSequences(String format, AlignmentI alignment,
193 String[] omitHidden, boolean suffix, ColumnSelection colSel,
194 jalview.datamodel.SequenceGroup selgp)
196 if (omitHidden != null)
198 // TODO consider using AlignmentView to prune to visible region
199 // TODO prune sequence annotation and groups to visible region
200 // TODO: JAL-1486 - set start and end for output correctly. basically, AlignmentView.getVisibleContigs does this.
201 Alignment alv = new Alignment(replaceStrings(
202 alignment.getSequencesArray(), omitHidden));
203 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
206 for (int i = 0; i < ala.length; i++)
208 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
211 colSel.makeVisibleAnnotation(selgp.getStartRes(),
212 selgp.getEndRes(), na);
216 colSel.makeVisibleAnnotation(na);
218 alv.addAnnotation(na);
221 return this.formatSequences(format, alv, suffix);
223 return this.formatSequences(format, alignment, suffix);
227 * validate format is valid for IO in Application. This is basically the
228 * AppletFormatAdapter.isValidFormat call with additional checks for
229 * Application only formats like 'Jalview'.
232 * a format string to be compared with list of readable or writable
233 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
235 * when true, format is checked against list of writable formats.
236 * @return true if format is valid
238 public static final boolean isValidIOFormat(String format,
241 if (format.equalsIgnoreCase("jalview"))
245 return AppletFormatAdapter.isValidFormat(format, forwriting);