2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.AlignExportSettingI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceGroup;
31 import jalview.datamodel.SequenceI;
34 * Additional formatting methods used by the application in a number of places.
39 public class FormatAdapter extends AppletFormatAdapter
41 public FormatAdapter(AlignmentViewPanel viewpanel)
47 public FormatAdapter()
53 public FormatAdapter(AlignmentViewPanel alignPanel,
54 AlignExportSettingI settings)
56 super(alignPanel, settings);
61 if (jalview.bin.Cache.getDefault("STRUCT_FROM_PDB", true))
63 annotFromStructure = jalview.bin.Cache.getDefault("ADD_TEMPFACT_ANN",
65 localSecondaryStruct = jalview.bin.Cache.getDefault("ADD_SS_ANN",
67 serviceSecondaryStruct = jalview.bin.Cache.getDefault("USE_RNAVIEW",
72 // disable all PDB annotation options
73 annotFromStructure = false;
74 localSecondaryStruct = false;
75 serviceSecondaryStruct = false;
79 public String formatSequences(String format, SequenceI[] seqs,
80 String[] omitHiddenColumns, int[] exportRange)
83 return formatSequences(format,
84 replaceStrings(seqs, omitHiddenColumns, exportRange));
88 * create sequences with each sequence string replaced with the one given in
92 * @param omitHiddenColumns
93 * @return new sequences
95 public SequenceI[] replaceStrings(SequenceI[] seqs,
96 String[] omitHiddenColumns, int[] startEnd)
98 if (omitHiddenColumns != null)
100 SequenceI[] tmp = new SequenceI[seqs.length];
106 for (int i = 0; i < seqs.length; i++)
108 startRes = seqs[i].getStart();
109 endRes = seqs[i].getEnd();
110 if (startEnd != null)
112 startIndex = startEnd[0];
113 endIndex = startEnd[1];
114 // get first non-gaped residue start position
115 while (jalview.util.Comparison.isGap(seqs[i]
116 .getCharAt(startIndex)) && startIndex < endIndex)
121 // get last non-gaped residue end position
122 while (jalview.util.Comparison.isGap(seqs[i].getCharAt(endIndex))
123 && endIndex > startIndex)
128 startRes = seqs[i].findPosition(startIndex);
129 endRes = seqs[i].findPosition(endIndex);
132 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
134 tmp[i].setDescription(seqs[i].getDescription());
142 * Format a vector of sequences as a flat alignment file. TODO: allow caller
143 * to detect errors and warnings encountered when generating output
147 * Format string as givien in the AppletFormatAdaptor list (exact
148 * match to name of class implementing file io for that format)
150 * vector of sequences to write
152 * @return String containing sequences in desired format
154 public String formatSequences(String format, SequenceI[] seqs)
159 AlignFile afile = null;
161 if (format.equalsIgnoreCase("FASTA"))
163 afile = new FastaFile();
164 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
167 else if (format.equalsIgnoreCase("MSF"))
169 afile = new MSFfile();
170 afile.addJVSuffix(jalview.bin.Cache
171 .getDefault("MSF_JVSUFFIX", true));
173 else if (format.equalsIgnoreCase("PileUp"))
175 afile = new PileUpfile();
176 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
179 else if (format.equalsIgnoreCase("CLUSTAL"))
181 afile = new ClustalFile();
182 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
185 else if (format.equalsIgnoreCase("BLC"))
187 afile = new BLCFile();
188 afile.addJVSuffix(jalview.bin.Cache
189 .getDefault("BLC_JVSUFFIX", true));
191 else if (format.equalsIgnoreCase("PIR"))
193 afile = new PIRFile();
194 afile.addJVSuffix(jalview.bin.Cache
195 .getDefault("PIR_JVSUFFIX", true));
197 else if (format.equalsIgnoreCase("PFAM"))
199 afile = new PfamFile();
200 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
204 * amsa is not supported by this function - it requires an alignment
205 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
206 * { afile = new AMSAFile(); afile.addJVSuffix(
207 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
211 String afileresp = afile.print();
212 if (afile.hasWarningMessage())
214 System.err.println("Warning raised when writing as " + format
215 + " : " + afile.getWarningMessage());
218 } catch (Exception e)
220 System.err.println("Failed to write alignment as a '" + format
228 public boolean getCacheSuffixDefault(String format)
230 if (isValidFormat(format))
232 return jalview.bin.Cache.getDefault(format.toUpperCase()
233 + "_JVSUFFIX", true);
238 public String formatSequences(String format, AlignmentI alignment,
239 String[] omitHidden, int[] exportRange, ColumnSelection colSel)
241 return formatSequences(format, alignment, omitHidden, exportRange,
242 getCacheSuffixDefault(format), colSel, null);
245 public String formatSequences(String format, AlignmentI alignment,
246 String[] omitHidden, int[] exportRange, ColumnSelection colSel,
249 return formatSequences(format, alignment, omitHidden, exportRange,
250 getCacheSuffixDefault(format), colSel, sgp);
254 * hack function to replace seuqences with visible sequence strings before
255 * generating a string of the alignment in the given format.
260 * sequence strings to write out in order of sequences in alignment
262 * defines hidden columns that are edited out of annotation
263 * @return string representation of the alignment formatted as format
265 public String formatSequences(String format, AlignmentI alignment,
266 String[] omitHidden, int[] exportRange, boolean suffix,
267 ColumnSelection colSel)
269 return formatSequences(format, alignment, omitHidden, exportRange,
270 suffix, colSel, null);
273 public String formatSequences(String format, AlignmentI alignment,
274 String[] omitHidden, int[] exportRange, boolean suffix,
275 ColumnSelection colSel, jalview.datamodel.SequenceGroup selgp)
277 if (omitHidden != null)
279 // TODO consider using AlignmentView to prune to visible region
280 // TODO prune sequence annotation and groups to visible region
281 // TODO: JAL-1486 - set start and end for output correctly. basically,
282 // AlignmentView.getVisibleContigs does this.
283 Alignment alv = new Alignment(replaceStrings(
284 alignment.getSequencesArray(), omitHidden, exportRange));
285 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
288 for (int i = 0; i < ala.length; i++)
290 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
293 colSel.makeVisibleAnnotation(selgp.getStartRes(),
294 selgp.getEndRes(), na);
298 colSel.makeVisibleAnnotation(na);
300 alv.addAnnotation(na);
303 return this.formatSequences(format, alv, suffix);
305 return this.formatSequences(format, alignment, suffix);
309 * validate format is valid for IO in Application. This is basically the
310 * AppletFormatAdapter.isValidFormat call with additional checks for
311 * Application only formats like 'Jalview'.
314 * a format string to be compared with list of readable or writable
315 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
317 * when true, format is checked against list of writable formats.
318 * @return true if format is valid
320 public static final boolean isValidIOFormat(String format,
323 if (format.equalsIgnoreCase("jalview"))
327 return AppletFormatAdapter.isValidFormat(format, forwriting);
331 * Create a flat file representation of a given view or selected region of a
336 * alignment panel originating the view
337 * @return String containing flat file
339 public String formatSequences(String format, AlignmentViewPanel ap,
340 boolean selectedOnly)
342 return formatSequences(format, getCacheSuffixDefault(format), ap,