JAL-2388 Unit tests and tidies
[jalview.git] / src / jalview / datamodel / HiddenSequences.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.datamodel;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26
27 public class HiddenSequences
28 {
29   /**
30    * holds a list of hidden sequences associated with an alignment.
31    */
32   public SequenceI[] hiddenSequences;
33
34   AlignmentI alignment;
35
36   /**
37    * Constructor given a reference to an alignment (with no hidden sequences)
38    * 
39    * @param al
40    */
41   public HiddenSequences(AlignmentI al)
42   {
43     alignment = al;
44   }
45
46   /**
47    * Answers the number of hidden sequences
48    * 
49    * @return
50    */
51   public int getSize()
52   {
53     if (hiddenSequences == null)
54     {
55       return 0;
56     }
57     int count = 0;
58     for (SequenceI seq : hiddenSequences)
59     {
60       if (seq != null)
61       {
62         count++;
63       }
64     }
65
66     return count;
67   }
68
69   /**
70    * Answers the length of the longest hidden sequence
71    * 
72    * @return
73    */
74   public int getWidth()
75   {
76     if (hiddenSequences == null)
77     {
78       return 0;
79     }
80     int width = 0;
81     for (SequenceI seq : hiddenSequences)
82     {
83       if (seq != null && seq.getLength() > width)
84       {
85         width = seq.getLength();
86       }
87     }
88
89     return width;
90   }
91
92   /**
93    * Call this method after a sequence is removed from the main alignment
94    */
95   public void adjustHeightSequenceDeleted(int seqIndex)
96   {
97     if (hiddenSequences == null)
98     {
99       return;
100     }
101
102     int alHeight = alignment.getHeight();
103
104     SequenceI[] tmp = new SequenceI[alHeight + getSize()];
105     int deletionIndex = adjustForHiddenSeqs(seqIndex);
106
107     for (int i = 0; i < hiddenSequences.length; i++)
108     {
109       if (hiddenSequences[i] == null)
110       {
111         continue;
112       }
113
114       if (i > deletionIndex)
115       {
116         tmp[i - 1] = hiddenSequences[i];
117       }
118       else
119       {
120         tmp[i] = hiddenSequences[i];
121       }
122     }
123
124     hiddenSequences = tmp;
125
126   }
127
128   /**
129    * Call this method after a sequence is added to the main alignment
130    */
131   public void adjustHeightSequenceAdded()
132   {
133     if (hiddenSequences == null)
134     {
135       return;
136     }
137
138     int alHeight = alignment.getHeight();
139
140     SequenceI[] tmp = new SequenceI[alHeight + getSize()];
141     System.arraycopy(hiddenSequences, 0, tmp, 0, hiddenSequences.length);
142     hiddenSequences = tmp;
143   }
144
145   /**
146    * Mark the specified sequence as hidden
147    * 
148    * @param sequence
149    */
150   public void hideSequence(SequenceI sequence)
151   {
152     if (hiddenSequences == null)
153     {
154       hiddenSequences = new SequenceI[alignment.getHeight()];
155     }
156
157     int absAlignmentIndex = alignment.findIndex(sequence);
158     int alignmentIndex = adjustForHiddenSeqs(absAlignmentIndex);
159
160     if (hiddenSequences[alignmentIndex] != null)
161     {
162       System.out.println("ERROR!!!!!!!!!!!");
163     }
164
165     hiddenSequences[alignmentIndex] = sequence;
166
167     alignment.deleteHiddenSequence(absAlignmentIndex);
168   }
169
170   public List<SequenceI> showAll(
171           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
172   {
173     List<SequenceI> revealedSeqs = new ArrayList<SequenceI>();
174     for (int i = 0; i < hiddenSequences.length; i++)
175     {
176       if (hiddenSequences[i] != null)
177       {
178         List<SequenceI> tmp = showSequence(i, hiddenRepSequences);
179         for (SequenceI seq : tmp)
180         {
181           revealedSeqs.add(seq);
182         }
183       }
184     }
185     return revealedSeqs;
186   }
187
188   /**
189    * Reveals (unhides) consecutive hidden sequences just above the given
190    * alignment index. The revealed sequences are selected (including their
191    * visible representative sequence if there was one and 'reveal' is being
192    * performed on it).
193    * 
194    * @param alignmentIndex
195    * @param hiddenRepSequences
196    *          a map of representative sequences to the sequences they represent
197    * @return
198    */
199   public List<SequenceI> showSequence(int alignmentIndex,
200           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
201   {
202     List<SequenceI> revealedSeqs = new ArrayList<SequenceI>();
203     SequenceI repSequence = alignment.getSequenceAt(alignmentIndex);
204     if (repSequence != null && hiddenRepSequences != null
205             && hiddenRepSequences.containsKey(repSequence))
206     {
207       hiddenRepSequences.remove(repSequence);
208       revealedSeqs.add(repSequence);
209     }
210
211     int start = adjustForHiddenSeqs(alignmentIndex - 1);
212     int end = adjustForHiddenSeqs(alignmentIndex);
213     if (end >= hiddenSequences.length)
214     {
215       end = hiddenSequences.length - 1;
216     }
217
218     List<SequenceI> asequences;
219     synchronized (asequences = alignment.getSequences())
220     {
221       for (int index = end; index > start; index--)
222       {
223         SequenceI seq = hiddenSequences[index];
224         hiddenSequences[index] = null;
225
226         if (seq != null)
227         {
228           if (seq.getLength() > 0)
229           {
230             revealedSeqs.add(seq);
231             asequences.add(alignmentIndex, seq);
232           }
233           else
234           {
235             System.out.println(seq.getName()
236                     + " has been deleted whilst hidden");
237           }
238         }
239       }
240     }
241     return revealedSeqs;
242   }
243
244   public SequenceI getHiddenSequence(int alignmentIndex)
245   {
246     return hiddenSequences == null ? null : hiddenSequences[alignmentIndex];
247   }
248
249   /**
250    * Convert absolute alignment index to visible alignment index
251    * 
252    * @param alignmentIndex
253    * @return
254    */
255   public int findIndexWithoutHiddenSeqs(int alignmentIndex)
256   {
257     if (hiddenSequences == null)
258     {
259       return alignmentIndex;
260     }
261     int index = 0;
262     int hiddenSeqs = 0;
263     int diff = 0;
264     if (hiddenSequences.length <= alignmentIndex)
265     {
266       // if the alignmentIndex runs past the end of hidden sequences
267       // and therefore actually past the end of the alignment
268       // store the difference to add back on at the end, so that behaviour
269       // is consistent with hidden columns behaviour (used by overview panel)
270       diff = alignmentIndex - hiddenSequences.length + 1;
271       alignmentIndex = hiddenSequences.length - 1;
272     }
273
274     while (index <= alignmentIndex)
275     {
276       if (hiddenSequences[index] != null)
277       {
278         hiddenSeqs++;
279       }
280       index++;
281     }
282
283     return (alignmentIndex - hiddenSeqs + diff);
284   }
285
286   /**
287    * Find the visible row which is a given visible number of rows above another
288    * visible row. i.e. for a startRow x, the row which is distance 1 away will
289    * be row x-1.
290    * 
291    * @param visibleDistance
292    *          the number of visible rows to offset by
293    * @param startRow
294    *          the row to start from
295    * @return the position of the row in the visible alignment
296    */
297   public int findIndexNAboveRow(int visibleDistance, int startRow)
298   {
299     // walk upwards through the alignment
300     // count all the non-null sequences until we have visibleDistance counted
301     // then return the next visible sequence
302     if (hiddenSequences == null)
303     {
304       return startRow - visibleDistance;
305     }
306
307     int index = startRow;
308     int count = 0;
309     while ((index > -1) && (count < visibleDistance))
310     {
311       if (hiddenSequences[index] == null)
312       {
313         // count visible sequences
314         count++;
315       }
316       index--;
317     }
318     return index;
319   }
320
321   /**
322    * Convert alignment index from visible alignment to absolute alignment
323    * 
324    * @param alignmentIndex
325    * @return
326    */
327   public int adjustForHiddenSeqs(int alignmentIndex)
328   {
329     if (hiddenSequences == null)
330     {
331       return alignmentIndex;
332     }
333     int index = 0;
334     int hSize = hiddenSequences.length;
335     while (index <= alignmentIndex && index < hSize)
336     {
337       if (hiddenSequences[index] != null)
338       {
339         alignmentIndex++;
340       }
341       index++;
342     }
343     ;
344
345     return alignmentIndex;
346   }
347
348   /**
349    * makes a copy of the alignment with hidden sequences included. Using the
350    * copy for anything other than simple output is not recommended. Note - this
351    * method DOES NOT USE THE AlignmentI COPY CONSTRUCTOR!
352    * 
353    * @return
354    */
355   public AlignmentI getFullAlignment()
356   {
357     SequenceI[] seq;
358     if (hiddenSequences == null)
359     {
360       seq = alignment.getSequencesArray();
361     }
362     else
363     {
364       int isize = hiddenSequences.length;
365       seq = new Sequence[isize];
366
367       int index = 0;
368       for (int i = 0; i < hiddenSequences.length; i++)
369       {
370         if (hiddenSequences[i] != null)
371         {
372           seq[i] = hiddenSequences[i];
373         }
374         else
375         {
376           seq[i] = alignment.getSequenceAt(index);
377           index++;
378         }
379       }
380     }
381     Alignment fAlignmt = new Alignment(seq);
382     fAlignmt.annotations = alignment.getAlignmentAnnotation();
383     fAlignmt.alignmentProperties = alignment.getProperties();
384     fAlignmt.groups = alignment.getGroups();
385     fAlignmt.hasRNAStructure = alignment.hasRNAStructure();
386     fAlignmt.setSeqrep(alignment.getSeqrep());
387
388     return fAlignmt;
389   }
390
391   public boolean isHidden(SequenceI seq)
392   {
393     if (hiddenSequences != null)
394     {
395       for (int i = 0; i < hiddenSequences.length; i++)
396       {
397         if (hiddenSequences[i] != null && hiddenSequences[i] == seq)
398         {
399           return true;
400         }
401       }
402     }
403
404     return false;
405   }
406 }