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.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
40 public FormatAdapter(AlignViewportI viewport)
46 public FormatAdapter()
54 if (jalview.bin.Cache.getDefault("STRUCT_FROM_PDB", true))
56 annotFromStructure = jalview.bin.Cache.getDefault("ADD_TEMPFACT_ANN",
58 localSecondaryStruct = jalview.bin.Cache.getDefault("ADD_SS_ANN",
60 serviceSecondaryStruct = jalview.bin.Cache.getDefault("USE_RNAVIEW",
65 // disable all PDB annotation options
66 annotFromStructure = false;
67 localSecondaryStruct = false;
68 serviceSecondaryStruct = false;
72 public String formatSequences(String format, SequenceI[] seqs,
73 String[] omitHiddenColumns, int[] exportRange)
76 return formatSequences(format,
77 replaceStrings(seqs, omitHiddenColumns, exportRange));
81 * create sequences with each sequence string replaced with the one given in
85 * @param omitHiddenColumns
86 * @return new sequences
88 public SequenceI[] replaceStrings(SequenceI[] seqs,
89 String[] omitHiddenColumns, int[] startEnd)
91 if (omitHiddenColumns != null)
93 SequenceI[] tmp = new SequenceI[seqs.length];
99 for (int i = 0; i < seqs.length; i++)
101 startRes = seqs[i].getStart();
102 endRes = seqs[i].getEnd();
104 startIndex = startEnd[0];
105 endIndex = startEnd[1];
107 if (startEnd != null)
109 // get first non-gaped residue start position
110 while (jalview.util.Comparison.isGap(seqs[i]
111 .getCharAt(startIndex)) && startIndex < endIndex)
116 // get last non-gaped residue end position
117 while (jalview.util.Comparison.isGap(seqs[i].getCharAt(endIndex))
118 && endIndex > startIndex)
123 startRes = seqs[i].findPosition(startIndex);
124 startRes = seqs[i].getStart() > 1 ? startRes - seqs[i].getStart()
126 endRes = seqs[i].findPosition(endIndex) - seqs[i].getStart();
129 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
131 tmp[i].setDescription(seqs[i].getDescription());
139 * Format a vector of sequences as a flat alignment file. TODO: allow caller
140 * to detect errors and warnings encountered when generating output
144 * Format string as givien in the AppletFormatAdaptor list (exact
145 * match to name of class implementing file io for that format)
147 * vector of sequences to write
149 * @return String containing sequences in desired format
151 public String formatSequences(String format, SequenceI[] seqs)
156 AlignFile afile = null;
158 if (format.equalsIgnoreCase("FASTA"))
160 afile = new FastaFile();
161 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
164 else if (format.equalsIgnoreCase("MSF"))
166 afile = new MSFfile();
167 afile.addJVSuffix(jalview.bin.Cache
168 .getDefault("MSF_JVSUFFIX", true));
170 else if (format.equalsIgnoreCase("PileUp"))
172 afile = new PileUpfile();
173 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
176 else if (format.equalsIgnoreCase("CLUSTAL"))
178 afile = new ClustalFile();
179 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
182 else if (format.equalsIgnoreCase("BLC"))
184 afile = new BLCFile();
185 afile.addJVSuffix(jalview.bin.Cache
186 .getDefault("BLC_JVSUFFIX", true));
188 else if (format.equalsIgnoreCase("PIR"))
190 afile = new PIRFile();
191 afile.addJVSuffix(jalview.bin.Cache
192 .getDefault("PIR_JVSUFFIX", true));
194 else if (format.equalsIgnoreCase("PFAM"))
196 afile = new PfamFile();
197 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
201 * amsa is not supported by this function - it requires an alignment
202 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
203 * { afile = new AMSAFile(); afile.addJVSuffix(
204 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
208 String afileresp = afile.print();
209 if (afile.hasWarningMessage())
211 System.err.println("Warning raised when writing as " + format
212 + " : " + afile.getWarningMessage());
215 } catch (Exception e)
217 System.err.println("Failed to write alignment as a '" + format
225 public boolean getCacheSuffixDefault(String format)
227 if (isValidFormat(format))
229 return jalview.bin.Cache.getDefault(format.toUpperCase()
230 + "_JVSUFFIX", true);
235 public String formatSequences(String format, AlignmentI alignment,
236 String[] omitHidden, int[] exportRange, ColumnSelection colSel)
238 return formatSequences(format, alignment, omitHidden, exportRange,
239 getCacheSuffixDefault(format), colSel, null);
242 public String formatSequences(String format, AlignmentI alignment,
243 String[] omitHidden, int[] exportRange, ColumnSelection colSel,
246 return formatSequences(format, alignment, omitHidden, exportRange,
247 getCacheSuffixDefault(format), colSel, sgp);
251 * hack function to replace seuqences with visible sequence strings before
252 * generating a string of the alignment in the given format.
257 * sequence strings to write out in order of sequences in alignment
259 * defines hidden columns that are edited out of annotation
260 * @return string representation of the alignment formatted as format
262 public String formatSequences(String format, AlignmentI alignment,
263 String[] omitHidden, int[] exportRange, boolean suffix,
264 ColumnSelection colSel)
266 return formatSequences(format, alignment, omitHidden, exportRange,
271 public String formatSequences(String format, AlignmentI alignment,
272 String[] omitHidden, int[] exportRange, boolean suffix,
273 ColumnSelection colSel,
274 jalview.datamodel.SequenceGroup selgp)
276 if (omitHidden != null)
278 // TODO consider using AlignmentView to prune to visible region
279 // TODO prune sequence annotation and groups to visible region
280 // TODO: JAL-1486 - set start and end for output correctly. basically,
281 // AlignmentView.getVisibleContigs does this.
282 Alignment alv = new Alignment(replaceStrings(
283 alignment.getSequencesArray(), omitHidden, exportRange));
284 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
287 for (int i = 0; i < ala.length; i++)
289 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
292 colSel.makeVisibleAnnotation(selgp.getStartRes(),
293 selgp.getEndRes(), na);
297 colSel.makeVisibleAnnotation(na);
299 alv.addAnnotation(na);
302 return this.formatSequences(format, alv, suffix);
304 return this.formatSequences(format, alignment, suffix);
307 public Alignment readFile(String inFile, String type, String format)
308 throws java.io.IOException
310 Alignment al = super.readFile(inFile, type, format);
314 public AlignmentI readFromFile(FileParse source, String format)
315 throws java.io.IOException
317 Alignment al = (Alignment) super.readFromFile(source, format);
322 * validate format is valid for IO in Application. This is basically the
323 * AppletFormatAdapter.isValidFormat call with additional checks for
324 * Application only formats like 'Jalview'.
327 * a format string to be compared with list of readable or writable
328 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
330 * when true, format is checked against list of writable formats.
331 * @return true if format is valid
333 public static final boolean isValidIOFormat(String format,
336 if (format.equalsIgnoreCase("jalview"))
340 return AppletFormatAdapter.isValidFormat(format, forwriting);
344 * Create a flat file representation of a given view or selected region of a view
347 * @return String containing flat file
349 public String formatSequences(String format, AlignViewportI av, boolean selectedOnly)
351 return formatSequences(format, getCacheSuffixDefault(format), av, selectedOnly);