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
41 public FormatAdapter()
44 if (jalview.bin.Cache.getDefault("STRUCT_FROM_PDB", true))
46 annotFromStructure = jalview.bin.Cache.getDefault("ADD_TEMPFACT_ANN",
48 localSecondaryStruct = jalview.bin.Cache.getDefault("ADD_SS_ANN",
50 serviceSecondaryStruct = jalview.bin.Cache.getDefault("USE_RNAVIEW",
55 // disable all PDB annotation options
56 annotFromStructure = false;
57 localSecondaryStruct = false;
58 serviceSecondaryStruct = false;
61 public String formatSequences(String format, SequenceI[] seqs,
62 String[] omitHiddenColumns)
65 return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
69 * create sequences with each sequence string replaced with the one given in
73 * @param omitHiddenColumns
74 * @return new sequences
76 public SequenceI[] replaceStrings(SequenceI[] seqs,
77 String[] omitHiddenColumns)
79 if (omitHiddenColumns != null)
81 SequenceI[] tmp = new SequenceI[seqs.length];
82 for (int i = 0; i < seqs.length; i++)
84 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
85 seqs[i].getStart(), seqs[i].getEnd());
86 tmp[i].setDescription(seqs[i].getDescription());
94 * Format a vector of sequences as a flat alignment file. TODO: allow caller
95 * to detect errors and warnings encountered when generating output
99 * Format string as givien in the AppletFormatAdaptor list (exact
100 * match to name of class implementing file io for that format)
102 * vector of sequences to write
104 * @return String containing sequences in desired format
106 public String formatSequences(String format, SequenceI[] seqs)
111 AlignFile afile = null;
113 if (format.equalsIgnoreCase("FASTA"))
115 afile = new FastaFile();
116 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
119 else if (format.equalsIgnoreCase("MSF"))
121 afile = new MSFfile();
122 afile.addJVSuffix(jalview.bin.Cache
123 .getDefault("MSF_JVSUFFIX", true));
125 else if (format.equalsIgnoreCase("PileUp"))
127 afile = new PileUpfile();
128 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
131 else if (format.equalsIgnoreCase("CLUSTAL"))
133 afile = new ClustalFile();
134 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
137 else if (format.equalsIgnoreCase("BLC"))
139 afile = new BLCFile();
140 afile.addJVSuffix(jalview.bin.Cache
141 .getDefault("BLC_JVSUFFIX", true));
143 else if (format.equalsIgnoreCase("PIR"))
145 afile = new PIRFile();
146 afile.addJVSuffix(jalview.bin.Cache
147 .getDefault("PIR_JVSUFFIX", true));
149 else if (format.equalsIgnoreCase("PFAM"))
151 afile = new PfamFile();
152 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
156 * amsa is not supported by this function - it requires an alignment
157 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA"))
158 * { afile = new AMSAFile(); afile.addJVSuffix(
159 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
163 String afileresp = afile.print();
164 if (afile.hasWarningMessage())
166 System.err.println("Warning raised when writing as " + format
167 + " : " + afile.getWarningMessage());
170 } catch (Exception e)
172 System.err.println("Failed to write alignment as a '" + format
180 public boolean getCacheSuffixDefault(String format)
182 if (isValidFormat(format))
184 return jalview.bin.Cache.getDefault(format.toUpperCase()
185 + "_JVSUFFIX", true);
190 public String formatSequences(String format, AlignmentI alignment,
191 String[] omitHidden, ColumnSelection colSel)
193 return formatSequences(format, alignment, omitHidden,
194 getCacheSuffixDefault(format), colSel, null);
197 public String formatSequences(String format, AlignmentI alignment,
198 String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
200 return formatSequences(format, alignment, omitHidden,
201 getCacheSuffixDefault(format), colSel, sgp);
205 * hack function to replace seuqences with visible sequence strings before
206 * generating a string of the alignment in the given format.
211 * sequence strings to write out in order of sequences in alignment
213 * defines hidden columns that are edited out of annotation
214 * @return string representation of the alignment formatted as format
216 public String formatSequences(String format, AlignmentI alignment,
217 String[] omitHidden, boolean suffix, ColumnSelection colSel)
219 return formatSequences(format, alignment, omitHidden, suffix, colSel,
223 public String formatSequences(String format, AlignmentI alignment,
224 String[] omitHidden, boolean suffix, ColumnSelection colSel,
225 jalview.datamodel.SequenceGroup selgp)
227 if (omitHidden != null)
229 // TODO consider using AlignmentView to prune to visible region
230 // TODO prune sequence annotation and groups to visible region
231 // TODO: JAL-1486 - set start and end for output correctly. basically,
232 // AlignmentView.getVisibleContigs does this.
233 Alignment alv = new Alignment(replaceStrings(
234 alignment.getSequencesArray(), omitHidden));
235 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
238 for (int i = 0; i < ala.length; i++)
240 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
243 colSel.makeVisibleAnnotation(selgp.getStartRes(),
244 selgp.getEndRes(), na);
248 colSel.makeVisibleAnnotation(na);
250 alv.addAnnotation(na);
253 return this.formatSequences(format, alv, suffix);
255 return this.formatSequences(format, alignment, suffix);
258 public Alignment readFile(String inFile, String type, String format)
259 throws java.io.IOException
262 // if (format.equals(JSONFile.FILE_DESC))
264 // // afile = new HtmlFile(inFile, type);
265 // al = new Alignment(afile.getSeqsAsArray());
266 // afile.addAnnotations(al);
270 al = super.readFile(inFile, type, format);
273 afile.addSeqGroups(al);
274 afile.addAnnotations(al);
278 public AlignmentI readFromFile(FileParse source, String format)
279 throws java.io.IOException
282 // if (format.equals(JSONFile.FILE_DESC))
284 // // afile = new HtmlFile(source);
285 // al = new Alignment(afile.getSeqsAsArray());
286 // afile.addAnnotations(al);
290 al = (Alignment) super.readFromFile(source, format);
292 afile.addSeqGroups(al);
293 afile.addAnnotations(al);
298 * validate format is valid for IO in Application. This is basically the
299 * AppletFormatAdapter.isValidFormat call with additional checks for
300 * Application only formats like 'Jalview'.
303 * a format string to be compared with list of readable or writable
304 * formats (READABLE_FORMATS or WRITABLE_FORMATS)
306 * when true, format is checked against list of writable formats.
307 * @return true if format is valid
309 public static final boolean isValidIOFormat(String format,
312 if (format.equalsIgnoreCase("jalview"))
316 return AppletFormatAdapter.isValidFormat(format, forwriting);
320 * Create a flat file representation of a given view or selected region of a view
323 * @return String containing flat file
325 public String formatSequences(String format, AlignViewportI av, boolean selectedOnly)
327 return formatSequences(format, getCacheSuffixDefault(format), av, selectedOnly);