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