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();
112 if (startEnd != null)
114 startIndex = startEnd[0];
115 endIndex = startEnd[1];
116 // get first non-gaped residue start position
117 while (jalview.util.Comparison.isGap(seqs[i]
118 .getCharAt(startIndex)) && startIndex < endIndex)
123 // get last non-gaped residue end position
124 while (jalview.util.Comparison.isGap(seqs[i].getCharAt(endIndex))
125 && endIndex > startIndex)
130 startRes = seqs[i].findPosition(startIndex);
131 startRes = seqs[i].getStart() > 1 ? startRes - seqs[i].getStart()
133 endRes = seqs[i].findPosition(endIndex) - seqs[i].getStart();
136 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
138 tmp[i].setDescription(seqs[i].getDescription());
146 * Format a vector of sequences as a flat alignment file. TODO: allow caller
147 * to detect errors and warnings encountered when generating output
151 * Format string as givien in the AppletFormatAdaptor list (exact
152 * match to name of class implementing file io for that format)
154 * vector of sequences to write
156 * @return String containing sequences in desired format
158 public String formatSequences(String format, SequenceI[] seqs)
163 AlignFile afile = null;
165 if (format.equalsIgnoreCase("FASTA"))
167 afile = new FastaFile();
168 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
171 else if (format.equalsIgnoreCase("MSF"))
173 afile = new MSFfile();
174 afile.addJVSuffix(jalview.bin.Cache
175 .getDefault("MSF_JVSUFFIX", true));
177 else if (format.equalsIgnoreCase("PileUp"))
179 afile = new PileUpfile();
180 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
183 else if (format.equalsIgnoreCase("CLUSTAL"))
185 afile = new ClustalFile();
186 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
189 else if (format.equalsIgnoreCase("BLC"))
191 afile = new BLCFile();
192 afile.addJVSuffix(jalview.bin.Cache
193 .getDefault("BLC_JVSUFFIX", true));
195 else if (format.equalsIgnoreCase("PIR"))
197 afile = new PIRFile();
198 afile.addJVSuffix(jalview.bin.Cache
199 .getDefault("PIR_JVSUFFIX", true));
201 else if (format.equalsIgnoreCase("PFAM"))
203 afile = new PfamFile();
204 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
208 * amsa is not supported by this function - it requires an alignment
209 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
210 * { afile = new AMSAFile(); afile.addJVSuffix(
211 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
215 String afileresp = afile.print();
216 if (afile.hasWarningMessage())
218 System.err.println("Warning raised when writing as " + format
219 + " : " + afile.getWarningMessage());
222 } catch (Exception e)
224 System.err.println("Failed to write alignment as a '" + format
232 public boolean getCacheSuffixDefault(String format)
234 if (isValidFormat(format))
236 return jalview.bin.Cache.getDefault(format.toUpperCase()
237 + "_JVSUFFIX", true);
242 public String formatSequences(String format, AlignmentI alignment,
243 String[] omitHidden, int[] exportRange, ColumnSelection colSel)
245 return formatSequences(format, alignment, omitHidden, exportRange,
246 getCacheSuffixDefault(format), colSel, null);
249 public String formatSequences(String format, AlignmentI alignment,
250 String[] omitHidden, int[] exportRange, ColumnSelection colSel,
253 return formatSequences(format, alignment, omitHidden, exportRange,
254 getCacheSuffixDefault(format), colSel, sgp);
258 * hack function to replace seuqences with visible sequence strings before
259 * generating a string of the alignment in the given format.
264 * sequence strings to write out in order of sequences in alignment
266 * defines hidden columns that are edited out of annotation
267 * @return string representation of the alignment formatted as format
269 public String formatSequences(String format, AlignmentI alignment,
270 String[] omitHidden, int[] exportRange, boolean suffix,
271 ColumnSelection colSel)
273 return formatSequences(format, alignment, omitHidden, exportRange,
278 public String formatSequences(String format, AlignmentI alignment,
279 String[] omitHidden, int[] exportRange, boolean suffix,
280 ColumnSelection colSel,
281 jalview.datamodel.SequenceGroup selgp)
283 if (omitHidden != null)
285 // TODO consider using AlignmentView to prune to visible region
286 // TODO prune sequence annotation and groups to visible region
287 // TODO: JAL-1486 - set start and end for output correctly. basically,
288 // AlignmentView.getVisibleContigs does this.
289 Alignment alv = new Alignment(replaceStrings(
290 alignment.getSequencesArray(), omitHidden, exportRange));
291 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
294 for (int i = 0; i < ala.length; i++)
296 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
299 colSel.makeVisibleAnnotation(selgp.getStartRes(),
300 selgp.getEndRes(), na);
304 colSel.makeVisibleAnnotation(na);
306 alv.addAnnotation(na);
309 return this.formatSequences(format, alv, suffix);
311 return this.formatSequences(format, alignment, suffix);
314 public AlignmentI readFile(String inFile, String type, String format)
315 throws java.io.IOException
317 AlignmentI al = super.readFile(inFile, type, format);
321 public AlignmentI readFromFile(FileParse source, String format)
322 throws java.io.IOException
324 AlignmentI al = super.readFromFile(source, format);
329 * validate format is valid for IO in Application. This is basically the
330 * AppletFormatAdapter.isValidFormat call with additional checks for
331 * Application only formats like 'Jalview'.
334 * a format string to be compared with list of readable or writable
335 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
337 * when true, format is checked against list of writable formats.
338 * @return true if format is valid
340 public static final boolean isValidIOFormat(String format,
343 if (format.equalsIgnoreCase("jalview"))
347 return AppletFormatAdapter.isValidFormat(format, forwriting);
351 * Create a flat file representation of a given view or selected region of a
356 * alignment panel originating the view
357 * @return String containing flat file
359 public String formatSequences(String format, AlignmentViewPanel ap,
360 boolean selectedOnly)
362 return formatSequences(format, getCacheSuffixDefault(format), ap,