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();
111 if (startEnd != null)
113 startIndex = startEnd[0];
114 endIndex = startEnd[1];
115 // get first non-gaped residue start position
116 while (jalview.util.Comparison.isGap(seqs[i]
117 .getCharAt(startIndex)) && startIndex < endIndex)
122 // get last non-gaped residue end position
123 while (jalview.util.Comparison.isGap(seqs[i].getCharAt(endIndex))
124 && endIndex > startIndex)
129 startRes = seqs[i].findPosition(startIndex);
130 startRes = seqs[i].getStart() > 1 ? startRes - seqs[i].getStart()
132 endRes = seqs[i].findPosition(endIndex) - seqs[i].getStart();
135 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
137 tmp[i].setDescription(seqs[i].getDescription());
145 * Format a vector of sequences as a flat alignment file. TODO: allow caller
146 * to detect errors and warnings encountered when generating output
150 * Format string as givien in the AppletFormatAdaptor list (exact
151 * match to name of class implementing file io for that format)
153 * vector of sequences to write
155 * @return String containing sequences in desired format
157 public String formatSequences(String format, SequenceI[] seqs)
162 AlignFile afile = null;
164 if (format.equalsIgnoreCase("FASTA"))
166 afile = new FastaFile();
167 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
170 else if (format.equalsIgnoreCase("MSF"))
172 afile = new MSFfile();
173 afile.addJVSuffix(jalview.bin.Cache
174 .getDefault("MSF_JVSUFFIX", true));
176 else if (format.equalsIgnoreCase("PileUp"))
178 afile = new PileUpfile();
179 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
182 else if (format.equalsIgnoreCase("CLUSTAL"))
184 afile = new ClustalFile();
185 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
188 else if (format.equalsIgnoreCase("BLC"))
190 afile = new BLCFile();
191 afile.addJVSuffix(jalview.bin.Cache
192 .getDefault("BLC_JVSUFFIX", true));
194 else if (format.equalsIgnoreCase("PIR"))
196 afile = new PIRFile();
197 afile.addJVSuffix(jalview.bin.Cache
198 .getDefault("PIR_JVSUFFIX", true));
200 else if (format.equalsIgnoreCase("PFAM"))
202 afile = new PfamFile();
203 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
207 * amsa is not supported by this function - it requires an alignment
208 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
209 * { afile = new AMSAFile(); afile.addJVSuffix(
210 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
214 String afileresp = afile.print();
215 if (afile.hasWarningMessage())
217 System.err.println("Warning raised when writing as " + format
218 + " : " + afile.getWarningMessage());
221 } catch (Exception e)
223 System.err.println("Failed to write alignment as a '" + format
231 public boolean getCacheSuffixDefault(String format)
233 if (isValidFormat(format))
235 return jalview.bin.Cache.getDefault(format.toUpperCase()
236 + "_JVSUFFIX", true);
241 public String formatSequences(String format, AlignmentI alignment,
242 String[] omitHidden, int[] exportRange, ColumnSelection colSel)
244 return formatSequences(format, alignment, omitHidden, exportRange,
245 getCacheSuffixDefault(format), colSel, null);
248 public String formatSequences(String format, AlignmentI alignment,
249 String[] omitHidden, int[] exportRange, ColumnSelection colSel,
252 return formatSequences(format, alignment, omitHidden, exportRange,
253 getCacheSuffixDefault(format), colSel, sgp);
257 * hack function to replace seuqences with visible sequence strings before
258 * generating a string of the alignment in the given format.
263 * sequence strings to write out in order of sequences in alignment
265 * defines hidden columns that are edited out of annotation
266 * @return string representation of the alignment formatted as format
268 public String formatSequences(String format, AlignmentI alignment,
269 String[] omitHidden, int[] exportRange, boolean suffix,
270 ColumnSelection colSel)
272 return formatSequences(format, alignment, omitHidden, exportRange,
273 suffix, colSel, null);
276 public String formatSequences(String format, AlignmentI alignment,
277 String[] omitHidden, int[] exportRange, boolean suffix,
278 ColumnSelection colSel, jalview.datamodel.SequenceGroup selgp)
280 if (omitHidden != null)
282 // TODO consider using AlignmentView to prune to visible region
283 // TODO prune sequence annotation and groups to visible region
284 // TODO: JAL-1486 - set start and end for output correctly. basically,
285 // AlignmentView.getVisibleContigs does this.
286 Alignment alv = new Alignment(replaceStrings(
287 alignment.getSequencesArray(), omitHidden, exportRange));
288 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
291 for (int i = 0; i < ala.length; i++)
293 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
296 colSel.makeVisibleAnnotation(selgp.getStartRes(),
297 selgp.getEndRes(), na);
301 colSel.makeVisibleAnnotation(na);
303 alv.addAnnotation(na);
306 return this.formatSequences(format, alv, suffix);
308 return this.formatSequences(format, alignment, suffix);
311 public AlignmentI readFile(String inFile, String type, String format)
312 throws java.io.IOException
314 AlignmentI al = super.readFile(inFile, type, format);
318 public AlignmentI readFromFile(FileParse source, String format)
319 throws java.io.IOException
321 AlignmentI al = super.readFromFile(source, format);
326 * validate format is valid for IO in Application. This is basically the
327 * AppletFormatAdapter.isValidFormat call with additional checks for
328 * Application only formats like 'Jalview'.
331 * a format string to be compared with list of readable or writable
332 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
334 * when true, format is checked against list of writable formats.
335 * @return true if format is valid
337 public static final boolean isValidIOFormat(String format,
340 if (format.equalsIgnoreCase("jalview"))
344 return AppletFormatAdapter.isValidFormat(format, forwriting);
348 * Create a flat file representation of a given view or selected region of a
353 * alignment panel originating the view
354 * @return String containing flat file
356 public String formatSequences(String format, AlignmentViewPanel ap,
357 boolean selectedOnly)
359 return formatSequences(format, getCacheSuffixDefault(format), ap,