Clover icon

Coverage Report

  1. Project Clover database Thu Nov 28 2024 18:06:40 GMT
  2. Package jalview.io

File FormatAdapter.java

 

Coverage histogram

../../img/srcFileCovDistChart5.png
45% of files have more coverage

Code metrics

20
63
17
1
288
190
29
0.46
3.71
17
1.71

Classes

Class Line # Actions
FormatAdapter 47 63 29
0.4646%
 

Contributing tests

This file is covered by 182 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.io;
22   
23    import java.io.File;
24    import java.io.IOException;
25    import java.util.Locale;
26   
27    import jalview.api.AlignExportSettingsI;
28    import jalview.api.AlignmentViewPanel;
29    import jalview.bin.Cache;
30    import jalview.datamodel.Alignment;
31    import jalview.datamodel.AlignmentAnnotation;
32    import jalview.datamodel.AlignmentI;
33    import jalview.datamodel.HiddenColumns;
34    import jalview.datamodel.Sequence;
35    import jalview.datamodel.SequenceGroup;
36    import jalview.datamodel.SequenceI;
37    import jalview.structure.StructureImportSettings;
38    import jalview.structure.StructureImportSettings.TFType;
39    import jalview.util.Comparison;
40   
41    /**
42    * Additional formatting methods used by the application in a number of places.
43    *
44    * @author $author$
45    * @version $Revision$
46    */
 
47    public class FormatAdapter extends AppletFormatAdapter
48    {
 
49  1 toggle public FormatAdapter(AlignmentViewPanel viewpanel)
50    {
51  1 super(viewpanel);
52  1 init();
53    }
54   
 
55  470 toggle public FormatAdapter()
56    {
57  470 super();
58  470 init();
59    }
60   
 
61  121 toggle public FormatAdapter(AlignmentViewPanel alignPanel,
62    AlignExportSettingsI settings)
63    {
64  121 super(alignPanel, settings);
65    }
66   
 
67  471 toggle private void init()
68    {
69  471 if (Cache.getDefault("STRUCT_FROM_PDB", true))
70    {
71  470 annotFromStructure = Cache.getDefault("ADD_TEMPFACT_ANN", true);
72  470 localSecondaryStruct = Cache.getDefault("ADD_SS_ANN", true);
73  470 serviceSecondaryStruct = Cache.getDefault("USE_RNAVIEW", true);
74    }
75    else
76    {
77    // disable all PDB annotation options
78  1 annotFromStructure = false;
79  1 localSecondaryStruct = false;
80  1 serviceSecondaryStruct = false;
81    }
82    }
83   
 
84  1 toggle public String formatSequences(FileFormatI format, SequenceI[] seqs,
85    String[] omitHiddenColumns, int[] exportRange)
86    {
87   
88  1 return formatSequences(format,
89    replaceStrings(seqs, omitHiddenColumns, exportRange));
90    }
91   
92    /**
93    * create sequences with each sequence string replaced with the one given in
94    * omitHiddenCOlumns
95    *
96    * @param seqs
97    * @param omitHiddenColumns
98    * @return new sequences
99    */
 
100  1 toggle public SequenceI[] replaceStrings(SequenceI[] seqs,
101    String[] omitHiddenColumns, int[] startEnd)
102    {
103  1 if (omitHiddenColumns != null)
104    {
105  0 SequenceI[] tmp = new SequenceI[seqs.length];
106   
107  0 int startRes;
108  0 int endRes;
109  0 int startIndex;
110  0 int endIndex;
111  0 for (int i = 0; i < seqs.length; i++)
112    {
113  0 startRes = seqs[i].getStart();
114  0 endRes = seqs[i].getEnd();
115  0 if (startEnd != null)
116    {
117  0 startIndex = startEnd[0];
118  0 endIndex = startEnd[1];
119    // get first non-gaped residue start position
120  0 while (Comparison.isGap(seqs[i].getCharAt(startIndex))
121    && startIndex < endIndex)
122    {
123  0 startIndex++;
124    }
125   
126    // get last non-gaped residue end position
127  0 while (Comparison.isGap(seqs[i].getCharAt(endIndex))
128    && endIndex > startIndex)
129    {
130  0 endIndex--;
131    }
132   
133  0 startRes = seqs[i].findPosition(startIndex);
134  0 endRes = seqs[i].findPosition(endIndex);
135    }
136   
137  0 tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i],
138    startRes, endRes);
139  0 tmp[i].setDescription(seqs[i].getDescription());
140    }
141  0 seqs = tmp;
142    }
143  1 return seqs;
144    }
145   
146    /**
147    * Format a vector of sequences as a flat alignment file. TODO: allow caller
148    * to detect errors and warnings encountered when generating output
149    *
150    *
151    * @param format
152    * @param seqs
153    * vector of sequences to write
154    *
155    * @return String containing sequences in desired format
156    */
 
157  1 toggle public String formatSequences(FileFormatI format, SequenceI[] seqs)
158    {
159  1 boolean withSuffix = getCacheSuffixDefault(format);
160  1 return format.getWriter(null).print(seqs, withSuffix);
161    }
162   
 
163  123 toggle public boolean getCacheSuffixDefault(FileFormatI format)
164    {
165  123 return Cache.getDefault(
166    format.getName().toUpperCase(Locale.ROOT) + "_JVSUFFIX", true);
167    }
168   
 
169  121 toggle public String formatSequences(FileFormatI format, AlignmentI alignment,
170    String[] omitHidden, int[] exportRange, HiddenColumns hidden)
171    {
172  121 return formatSequences(format, alignment, omitHidden, exportRange,
173    getCacheSuffixDefault(format), hidden, null);
174    }
175   
176    /**
177    * hack function to replace sequences with visible sequence strings before
178    * generating a string of the alignment in the given format.
179    *
180    * @param format
181    * @param alignment
182    * @param omitHidden
183    * sequence strings to write out in order of sequences in alignment
184    * @param colSel
185    * defines hidden columns that are edited out of annotation
186    * @return string representation of the alignment formatted as format
187    */
 
188  0 toggle public String formatSequences(FileFormatI format, AlignmentI alignment,
189    String[] omitHidden, int[] exportRange, boolean suffix,
190    HiddenColumns hidden)
191    {
192  0 return formatSequences(format, alignment, omitHidden, exportRange,
193    suffix, hidden, null);
194    }
195   
 
196  121 toggle public String formatSequences(FileFormatI format, AlignmentI alignment,
197    String[] omitHidden, int[] exportRange, boolean suffix,
198    HiddenColumns hidden, SequenceGroup selgp)
199    {
200  121 if (omitHidden != null)
201    {
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  0 Alignment alv = new Alignment(replaceStrings(
207    alignment.getSequencesArray(), omitHidden, exportRange));
208  0 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
209  0 if (ala != null)
210    {
211  0 for (int i = 0; i < ala.length; i++)
212    {
213  0 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
214  0 if (selgp != null)
215    {
216  0 na.makeVisibleAnnotation(selgp.getStartRes(), selgp.getEndRes(),
217    hidden);
218    }
219    else
220    {
221  0 na.makeVisibleAnnotation(hidden);
222    }
223  0 alv.addAnnotation(na);
224    }
225    }
226  0 return this.formatSequences(format, alv, suffix);
227    }
228  121 return this.formatSequences(format, alignment, suffix);
229    }
230   
 
231  456 toggle @Override
232    public AlignmentI readFile(File selectedFile, String file,
233    DataSourceType sourceType, FileFormatI fileFormat,
234    StructureImportSettings.TFType tempfacType) throws IOException
235    {
236  456 AlignmentI al = super.readFile(selectedFile, file, sourceType,
237    fileFormat, tempfacType);
238  456 return al;
239    }
240   
 
241  141 toggle @Override
242    public AlignmentI readFile(String file, DataSourceType sourceType,
243    FileFormatI fileFormat) throws IOException
244    {
245  141 AlignmentI al = super.readFile(file, sourceType, fileFormat);
246  141 return al;
247    }
248   
 
249  1 toggle public AlignmentI readFile(File file, DataSourceType sourceType,
250    FileFormatI fileFormat) throws IOException
251    {
252  1 AlignmentI al = super.readFile(file, null, sourceType, fileFormat);
253  1 return al;
254    }
255   
 
256  0 toggle @Override
257    public AlignmentI readFromFile(FileParse source, FileFormatI format)
258    throws IOException
259    {
260  0 AlignmentI al = super.readFromFile(source, format);
261  0 return al;
262    }
263   
264    /**
265    * Create a flat file representation of a given view or selected region of a
266    * view
267    *
268    * @param format
269    * @param ap
270    * alignment panel originating the view
271    * @return String containing flat file
272    */
 
273  1 toggle public String formatSequences(FileFormatI format, AlignmentViewPanel ap,
274    boolean selectedOnly)
275    {
276  1 return formatSequences(format, getCacheSuffixDefault(format), ap,
277    selectedOnly);
278    }
279   
 
280  0 toggle public AlignmentI readFromFile(AlignmentFileReaderI source,
281    FileFormatI format) throws IOException
282    {
283  0 FileParse fp = new FileParse(source.getInFile(),
284    source.getDataSourceType());
285  0 return readFromFile(fp, format);
286    }
287   
288    }