Clover icon

Coverage Report

  1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.datamodel

File HiddenSequences.java

 

Coverage histogram

../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

74
130
15
1
430
280
59
0.45
8.67
15
3.93

Classes

Class Line # Actions
HiddenSequences 27 130 59
0.97716997.7%
 

Contributing tests

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