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