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.bin.Cache;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.HiddenColumns;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceGroup;
32 import jalview.datamodel.SequenceI;
33 import jalview.util.Comparison;
36 import java.io.IOException;
39 * Additional formatting methods used by the application in a number of places.
44 public class FormatAdapter extends AppletFormatAdapter
46 public FormatAdapter(AlignmentViewPanel viewpanel)
52 public FormatAdapter()
58 public FormatAdapter(AlignmentViewPanel alignPanel,
59 AlignExportSettingI settings)
61 super(alignPanel, settings);
66 if (jalview.bin.Cache.getDefault("STRUCT_FROM_PDB", true))
68 annotFromStructure = jalview.bin.Cache.getDefault("ADD_TEMPFACT_ANN",
70 localSecondaryStruct = jalview.bin.Cache.getDefault("ADD_SS_ANN",
72 serviceSecondaryStruct = jalview.bin.Cache.getDefault("USE_RNAVIEW",
77 // disable all PDB annotation options
78 annotFromStructure = false;
79 localSecondaryStruct = false;
80 serviceSecondaryStruct = false;
84 public String formatSequences(FileFormatI format, SequenceI[] seqs,
85 String[] omitHiddenColumns, int[] exportRange)
88 return formatSequences(format,
89 replaceStrings(seqs, omitHiddenColumns, exportRange));
93 * create sequences with each sequence string replaced with the one given in
97 * @param omitHiddenColumns
98 * @return new sequences
100 public SequenceI[] replaceStrings(SequenceI[] seqs,
101 String[] omitHiddenColumns, int[] startEnd)
103 if (omitHiddenColumns != null)
105 SequenceI[] tmp = new SequenceI[seqs.length];
111 for (int i = 0; i < seqs.length; i++)
113 startRes = seqs[i].getStart();
114 endRes = seqs[i].getEnd();
115 if (startEnd != null)
117 startIndex = startEnd[0];
118 endIndex = startEnd[1];
119 // get first non-gaped residue start position
120 while (Comparison.isGap(seqs[i].getCharAt(startIndex))
121 && startIndex < endIndex)
126 // get last non-gaped residue end position
127 while (Comparison.isGap(seqs[i].getCharAt(endIndex))
128 && endIndex > startIndex)
133 startRes = seqs[i].findPosition(startIndex);
134 endRes = seqs[i].findPosition(endIndex);
137 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
139 tmp[i].setDescription(seqs[i].getDescription());
147 * Format a vector of sequences as a flat alignment file. TODO: allow caller
148 * to detect errors and warnings encountered when generating output
153 * vector of sequences to write
155 * @return String containing sequences in desired format
157 public String formatSequences(FileFormatI format, SequenceI[] seqs)
159 boolean withSuffix = getCacheSuffixDefault(format);
160 return format.getWriter(null).print(seqs, withSuffix);
163 public boolean getCacheSuffixDefault(FileFormatI format)
165 return Cache.getDefault(format.getName().toUpperCase() + "_JVSUFFIX",
169 public String formatSequences(FileFormatI format, AlignmentI alignment,
170 String[] omitHidden, int[] exportRange, HiddenColumns hidden)
172 return formatSequences(format, alignment, omitHidden, exportRange,
173 getCacheSuffixDefault(format), hidden, null);
177 * hack function to replace sequences with visible sequence strings before
178 * generating a string of the alignment in the given format.
183 * sequence strings to write out in order of sequences in alignment
185 * defines hidden columns that are edited out of annotation
186 * @return string representation of the alignment formatted as format
188 public String formatSequences(FileFormatI format, AlignmentI alignment,
189 String[] omitHidden, int[] exportRange, boolean suffix,
190 HiddenColumns hidden)
192 return formatSequences(format, alignment, omitHidden, exportRange,
193 suffix, hidden, null);
196 public String formatSequences(FileFormatI format, AlignmentI alignment,
197 String[] omitHidden, int[] exportRange, boolean suffix,
198 HiddenColumns hidden, SequenceGroup selgp)
200 if (omitHidden != null)
202 // TODO consider using AlignmentView to prune to visible region
203 // TODO prune sequence annotation and groups to visible region
204 // TODO: JAL-1486 - set start and end for output correctly. basically,
205 // AlignmentView.getVisibleContigs does this.
206 Alignment alv = new Alignment(replaceStrings(
207 alignment.getSequencesArray(), omitHidden, exportRange));
208 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
211 for (int i = 0; i < ala.length; i++)
213 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
216 na.makeVisibleAnnotation(selgp.getStartRes(), selgp.getEndRes(),
221 na.makeVisibleAnnotation(hidden);
223 alv.addAnnotation(na);
226 return this.formatSequences(format, alv, suffix);
228 return this.formatSequences(format, alignment, suffix);
232 public AlignmentI readFile(String file, DataSourceType sourceType,
233 FileFormatI fileFormat) throws IOException
235 AlignmentI al = super.readFile(file, sourceType, fileFormat);
239 public AlignmentI readFile(File file, DataSourceType sourceType,
240 FileFormatI fileFormat) throws IOException
242 AlignmentI al = super.readFile(file, null, sourceType, fileFormat);
247 public AlignmentI readFromFile(FileParse source, FileFormatI format)
250 AlignmentI al = super.readFromFile(source, format);
255 * Create a flat file representation of a given view or selected region of a
260 * alignment panel originating the view
261 * @return String containing flat file
263 public String formatSequences(FileFormatI format, AlignmentViewPanel ap,
264 boolean selectedOnly)
266 return formatSequences(format, getCacheSuffixDefault(format), ap,
270 public AlignmentI readFromFile(AlignmentFileReaderI source,
271 FileFormatI format) throws IOException
273 FileParse fp = new FileParse(source.getInFile(),
274 source.getDataSourceType());
275 return readFromFile(fp, format);