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)
76 return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
80 * create sequences with each sequence string replaced with the one given in
84 * @param omitHiddenColumns
85 * @return new sequences
87 public SequenceI[] replaceStrings(SequenceI[] seqs,
88 String[] omitHiddenColumns)
90 if (omitHiddenColumns != null)
92 SequenceI[] tmp = new SequenceI[seqs.length];
93 for (int i = 0; i < seqs.length; i++)
95 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
96 seqs[i].getStart(), seqs[i].getEnd());
97 tmp[i].setDescription(seqs[i].getDescription());
105 * Format a vector of sequences as a flat alignment file. TODO: allow caller
106 * to detect errors and warnings encountered when generating output
110 * Format string as givien in the AppletFormatAdaptor list (exact
111 * match to name of class implementing file io for that format)
113 * vector of sequences to write
115 * @return String containing sequences in desired format
117 public String formatSequences(String format, SequenceI[] seqs)
122 AlignFile afile = null;
124 if (format.equalsIgnoreCase("FASTA"))
126 afile = new FastaFile();
127 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
130 else if (format.equalsIgnoreCase("MSF"))
132 afile = new MSFfile();
133 afile.addJVSuffix(jalview.bin.Cache
134 .getDefault("MSF_JVSUFFIX", true));
136 else if (format.equalsIgnoreCase("PileUp"))
138 afile = new PileUpfile();
139 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
142 else if (format.equalsIgnoreCase("CLUSTAL"))
144 afile = new ClustalFile();
145 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
148 else if (format.equalsIgnoreCase("BLC"))
150 afile = new BLCFile();
151 afile.addJVSuffix(jalview.bin.Cache
152 .getDefault("BLC_JVSUFFIX", true));
154 else if (format.equalsIgnoreCase("PIR"))
156 afile = new PIRFile();
157 afile.addJVSuffix(jalview.bin.Cache
158 .getDefault("PIR_JVSUFFIX", true));
160 else if (format.equalsIgnoreCase("PFAM"))
162 afile = new PfamFile();
163 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
167 * amsa is not supported by this function - it requires an alignment
168 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
169 * { afile = new AMSAFile(); afile.addJVSuffix(
170 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
174 String afileresp = afile.print();
175 if (afile.hasWarningMessage())
177 System.err.println("Warning raised when writing as " + format
178 + " : " + afile.getWarningMessage());
181 } catch (Exception e)
183 System.err.println("Failed to write alignment as a '" + format
191 public boolean getCacheSuffixDefault(String format)
193 if (isValidFormat(format))
195 return jalview.bin.Cache.getDefault(format.toUpperCase()
196 + "_JVSUFFIX", true);
201 public String formatSequences(String format, AlignmentI alignment,
202 String[] omitHidden, ColumnSelection colSel)
204 return formatSequences(format, alignment, omitHidden,
205 getCacheSuffixDefault(format), colSel, null);
208 public String formatSequences(String format, AlignmentI alignment,
209 String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
211 return formatSequences(format, alignment, omitHidden,
212 getCacheSuffixDefault(format), colSel, sgp);
216 * hack function to replace seuqences with visible sequence strings before
217 * generating a string of the alignment in the given format.
222 * sequence strings to write out in order of sequences in alignment
224 * defines hidden columns that are edited out of annotation
225 * @return string representation of the alignment formatted as format
227 public String formatSequences(String format, AlignmentI alignment,
228 String[] omitHidden, boolean suffix, ColumnSelection colSel)
230 return formatSequences(format, alignment, omitHidden, suffix, colSel,
234 public String formatSequences(String format, AlignmentI alignment,
235 String[] omitHidden, boolean suffix, ColumnSelection colSel,
236 jalview.datamodel.SequenceGroup selgp)
238 if (omitHidden != null)
240 // TODO consider using AlignmentView to prune to visible region
241 // TODO prune sequence annotation and groups to visible region
242 // TODO: JAL-1486 - set start and end for output correctly. basically,
243 // AlignmentView.getVisibleContigs does this.
244 Alignment alv = new Alignment(replaceStrings(
245 alignment.getSequencesArray(), omitHidden));
246 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
249 for (int i = 0; i < ala.length; i++)
251 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
254 colSel.makeVisibleAnnotation(selgp.getStartRes(),
255 selgp.getEndRes(), na);
259 colSel.makeVisibleAnnotation(na);
261 alv.addAnnotation(na);
264 return this.formatSequences(format, alv, suffix);
266 return this.formatSequences(format, alignment, suffix);
269 public Alignment readFile(String inFile, String type, String format)
270 throws java.io.IOException
272 Alignment al = super.readFile(inFile, type, format);
276 public AlignmentI readFromFile(FileParse source, String format)
277 throws java.io.IOException
279 Alignment al = (Alignment) super.readFromFile(source, format);
284 * validate format is valid for IO in Application. This is basically the
285 * AppletFormatAdapter.isValidFormat call with additional checks for
286 * Application only formats like 'Jalview'.
289 * a format string to be compared with list of readable or writable
290 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
292 * when true, format is checked against list of writable formats.
293 * @return true if format is valid
295 public static final boolean isValidIOFormat(String format,
298 if (format.equalsIgnoreCase("jalview"))
302 return AppletFormatAdapter.isValidFormat(format, forwriting);
306 * Create a flat file representation of a given view or selected region of a view
309 * @return String containing flat file
311 public String formatSequences(String format, AlignViewportI av, boolean selectedOnly)
313 return formatSequences(format, getCacheSuffixDefault(format), av, selectedOnly);