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;
35 import java.io.IOException;
38 * Additional formatting methods used by the application in a number of places.
43 public class FormatAdapter extends AppletFormatAdapter
45 public FormatAdapter(AlignmentViewPanel viewpanel)
51 public FormatAdapter()
57 public FormatAdapter(AlignmentViewPanel alignPanel,
58 AlignExportSettingI settings)
60 super(alignPanel, settings);
65 if (jalview.bin.Cache.getDefault("STRUCT_FROM_PDB", true))
67 annotFromStructure = jalview.bin.Cache.getDefault("ADD_TEMPFACT_ANN",
69 localSecondaryStruct = jalview.bin.Cache.getDefault("ADD_SS_ANN",
71 serviceSecondaryStruct = jalview.bin.Cache.getDefault("USE_RNAVIEW",
76 // disable all PDB annotation options
77 annotFromStructure = false;
78 localSecondaryStruct = false;
79 serviceSecondaryStruct = false;
83 public String formatSequences(FileFormatI format, SequenceI[] seqs,
84 String[] omitHiddenColumns, int[] exportRange)
87 return formatSequences(format,
88 replaceStrings(seqs, omitHiddenColumns, exportRange));
92 * create sequences with each sequence string replaced with the one given in
96 * @param omitHiddenColumns
97 * @return new sequences
99 public SequenceI[] replaceStrings(SequenceI[] seqs,
100 String[] omitHiddenColumns, int[] startEnd)
102 if (omitHiddenColumns != null)
104 SequenceI[] tmp = new SequenceI[seqs.length];
110 for (int i = 0; i < seqs.length; i++)
112 startRes = seqs[i].getStart();
113 endRes = seqs[i].getEnd();
114 if (startEnd != null)
116 startIndex = startEnd[0];
117 endIndex = startEnd[1];
118 // get first non-gaped residue start position
119 while (Comparison.isGap(seqs[i].getCharAt(startIndex))
120 && startIndex < endIndex)
125 // get last non-gaped residue end position
126 while (Comparison.isGap(seqs[i].getCharAt(endIndex))
127 && endIndex > startIndex)
132 startRes = seqs[i].findPosition(startIndex);
133 endRes = seqs[i].findPosition(endIndex);
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
152 * vector of sequences to write
154 * @return String containing sequences in desired format
156 public String formatSequences(FileFormatI format, SequenceI[] seqs)
158 boolean withSuffix = getCacheSuffixDefault(format);
159 return format.getWriter(null).print(seqs, withSuffix);
162 public boolean getCacheSuffixDefault(FileFormatI format)
164 return Cache.getDefault(format.getName() + "_JVSUFFIX", true);
167 public String formatSequences(FileFormatI format, AlignmentI alignment,
168 String[] omitHidden, int[] exportRange, HiddenColumns hidden)
170 return formatSequences(format, alignment, omitHidden, exportRange,
171 getCacheSuffixDefault(format), hidden, null);
175 * hack function to replace sequences with visible sequence strings before
176 * generating a string of the alignment in the given format.
181 * sequence strings to write out in order of sequences in alignment
183 * defines hidden columns that are edited out of annotation
184 * @return string representation of the alignment formatted as format
186 public String formatSequences(FileFormatI format, AlignmentI alignment,
187 String[] omitHidden, int[] exportRange, boolean suffix,
188 HiddenColumns hidden)
190 return formatSequences(format, alignment, omitHidden, exportRange,
191 suffix, hidden, null);
194 public String formatSequences(FileFormatI format, AlignmentI alignment,
195 String[] omitHidden, int[] exportRange, boolean suffix,
196 HiddenColumns hidden, SequenceGroup selgp)
198 if (omitHidden != null)
200 // TODO consider using AlignmentView to prune to visible region
201 // TODO prune sequence annotation and groups to visible region
202 // TODO: JAL-1486 - set start and end for output correctly. basically,
203 // AlignmentView.getVisibleContigs does this.
204 Alignment alv = new Alignment(replaceStrings(
205 alignment.getSequencesArray(), omitHidden, exportRange));
206 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
209 for (int i = 0; i < ala.length; i++)
211 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
214 hidden.makeVisibleAnnotation(selgp.getStartRes(),
215 selgp.getEndRes(), na);
219 hidden.makeVisibleAnnotation(na);
221 alv.addAnnotation(na);
224 return this.formatSequences(format, alv, suffix);
226 return this.formatSequences(format, alignment, suffix);
230 public AlignmentI readFile(String file, DataSourceType sourceType,
231 FileFormatI fileFormat) throws IOException
233 AlignmentI al = super.readFile(file, sourceType, fileFormat);
238 public AlignmentI readFromFile(FileParse source, FileFormatI format)
241 AlignmentI al = super.readFromFile(source, format);
246 * Create a flat file representation of a given view or selected region of a
251 * alignment panel originating the view
252 * @return String containing flat file
254 public String formatSequences(FileFormatI format, AlignmentViewPanel ap,
255 boolean selectedOnly)
257 return formatSequences(format, getCacheSuffixDefault(format), ap,
261 public AlignmentI readFromFile(AlignmentFileReaderI source,
262 FileFormatI format) throws IOException
264 FileParse fp = new FileParse(source.getInFile(),
265 source.getDataSourceType());
266 return readFromFile(fp, format);