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.Alignment;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
33 * Additional formatting methods used by the application in a number of places.
38 public class FormatAdapter extends AppletFormatAdapter
41 public String formatSequences(String format, SequenceI[] seqs,
42 String[] omitHiddenColumns)
45 return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
49 * create sequences with each sequence string replaced with the one given in
53 * @param omitHiddenColumns
54 * @return new sequences
56 public SequenceI[] replaceStrings(SequenceI[] seqs,
57 String[] omitHiddenColumns)
59 if (omitHiddenColumns != null)
61 SequenceI[] tmp = new SequenceI[seqs.length];
62 for (int i = 0; i < seqs.length; i++)
64 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
65 seqs[i].getStart(), seqs[i].getEnd());
66 tmp[i].setDescription(seqs[i].getDescription());
74 * Format a vector of sequences as a flat alignment file. TODO: allow caller
75 * to detect errors and warnings encountered when generating output
79 * Format string as givien in the AppletFormatAdaptor list (exact
80 * match to name of class implementing file io for that format)
82 * vector of sequences to write
84 * @return String containing sequences in desired format
86 public String formatSequences(String format, SequenceI[] seqs)
91 AlignFile afile = null;
93 if (format.equalsIgnoreCase("FASTA"))
95 afile = new FastaFile();
96 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
99 else if (format.equalsIgnoreCase("MSF"))
101 afile = new MSFfile();
102 afile.addJVSuffix(jalview.bin.Cache
103 .getDefault("MSF_JVSUFFIX", true));
105 else if (format.equalsIgnoreCase("PileUp"))
107 afile = new PileUpfile();
108 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
111 else if (format.equalsIgnoreCase("CLUSTAL"))
113 afile = new ClustalFile();
114 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
117 else if (format.equalsIgnoreCase("BLC"))
119 afile = new BLCFile();
120 afile.addJVSuffix(jalview.bin.Cache
121 .getDefault("BLC_JVSUFFIX", true));
123 else if (format.equalsIgnoreCase("PIR"))
125 afile = new PIRFile();
126 afile.addJVSuffix(jalview.bin.Cache
127 .getDefault("PIR_JVSUFFIX", true));
129 else if (format.equalsIgnoreCase("PFAM"))
131 afile = new PfamFile();
132 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
136 * amsa is not supported by this function - it requires an alignment
137 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
138 * { afile = new AMSAFile(); afile.addJVSuffix(
139 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
143 String afileresp = afile.print();
144 if (afile.hasWarningMessage())
146 System.err.println("Warning raised when writing as " + format
147 + " : " + afile.getWarningMessage());
150 } catch (Exception e)
152 System.err.println("Failed to write alignment as a '" + format
160 public boolean getCacheSuffixDefault(String format)
162 if (isValidFormat(format))
164 return jalview.bin.Cache.getDefault(format.toUpperCase()
165 + "_JVSUFFIX", true);
170 public String formatSequences(String format, AlignmentI alignment,
171 String[] omitHidden, ColumnSelection colSel)
173 return formatSequences(format, alignment, omitHidden,
174 getCacheSuffixDefault(format), colSel, null);
177 public String formatSequences(String format, AlignmentI alignment,
178 String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
180 return formatSequences(format, alignment, omitHidden,
181 getCacheSuffixDefault(format), colSel, sgp);
185 * hack function to replace seuqences with visible sequence strings before
186 * generating a string of the alignment in the given format.
191 * sequence strings to write out in order of sequences in alignment
193 * defines hidden columns that are edited out of annotation
194 * @return string representation of the alignment formatted as format
196 public String formatSequences(String format, AlignmentI alignment,
197 String[] omitHidden, boolean suffix, ColumnSelection colSel)
199 return formatSequences(format, alignment, omitHidden, suffix, colSel,
203 public String formatSequences(String format, AlignmentI alignment,
204 String[] omitHidden, boolean suffix, ColumnSelection colSel,
205 jalview.datamodel.SequenceGroup selgp)
207 if (omitHidden != null)
209 // TODO consider using AlignmentView to prune to visible region
210 // TODO prune sequence annotation and groups to visible region
211 // TODO: JAL-1486 - set start and end for output correctly. basically,
212 // AlignmentView.getVisibleContigs does this.
213 Alignment alv = new Alignment(replaceStrings(
214 alignment.getSequencesArray(), omitHidden));
215 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
218 for (int i = 0; i < ala.length; i++)
220 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
223 colSel.makeVisibleAnnotation(selgp.getStartRes(),
224 selgp.getEndRes(), na);
228 colSel.makeVisibleAnnotation(na);
230 alv.addAnnotation(na);
233 return this.formatSequences(format, alv, suffix);
235 return this.formatSequences(format, alignment, suffix);
238 public Alignment readFile(String inFile, String type, String format)
239 throws java.io.IOException
242 if (format.equals("HTML"))
244 afile = new HtmlFile(inFile, type);
245 al = new Alignment(afile.getSeqsAsArray());
246 afile.addAnnotations(al);
250 al = super.readFile(inFile, type, format);
256 public AlignmentI readFromFile(FileParse source, String format)
257 throws java.io.IOException
260 if (format.equals("HTML"))
262 afile = new HtmlFile(source);
263 al = new Alignment(afile.getSeqsAsArray());
264 afile.addAnnotations(al);
268 al = (Alignment) super.readFromFile(source, format);
274 * validate format is valid for IO in Application. This is basically the
275 * AppletFormatAdapter.isValidFormat call with additional checks for
276 * Application only formats like 'Jalview'.
279 * a format string to be compared with list of readable or writable
280 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
282 * when true, format is checked against list of writable formats.
283 * @return true if format is valid
285 public static final boolean isValidIOFormat(String format,
288 if (format.equalsIgnoreCase("jalview"))
292 return AppletFormatAdapter.isValidFormat(format, forwriting);
296 * Create a flat file representation of a given view or selected region of a view
299 * @return String containing flat file
301 public String formatSequences(String format, AlignViewportI av, boolean selectedOnly)
303 return formatSequences(format, getCacheSuffixDefault(format), av, selectedOnly);