3685ab0ededfb7c249f412b1bdce9e7d67cf99f0
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
1 package jalview.datamodel;
2
3 import jalview.util.Comparison;
4 import jalview.util.ShiftList;
5
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.List;
9 import java.util.Vector;
10
11 public class HiddenColumns
12 {
13   /*
14    * list of hidden column [start, end] ranges; the list is maintained in
15    * ascending start column order
16    */
17   private Vector<int[]> hiddenColumns;
18
19   /**
20    * This Method is used to return all the HiddenColumn regions
21    * 
22    * @return empty list or List of hidden column intervals
23    */
24   public List<int[]> getHiddenRegions()
25   {
26     return hiddenColumns == null ? Collections.<int[]> emptyList()
27             : hiddenColumns;
28   }
29
30   /**
31    * Find the number of hidden columns
32    * 
33    * @return number of hidden columns
34    */
35   public int getSize()
36   {
37     int size = 0;
38     if (hasHidden())
39     {
40       for (int[] range : hiddenColumns)
41       {
42         size += range[1] - range[0] + 1;
43       }
44     }
45     return size;
46   }
47
48   /**
49    * Answers if there are any hidden columns
50    * 
51    * @return true if there are hidden columns
52    */
53   public boolean hasHidden()
54   {
55     return (hiddenColumns != null) && (!hiddenColumns.isEmpty());
56   }
57
58   @Override
59   public boolean equals(Object obj)
60   {
61     if (!(obj instanceof HiddenColumns))
62     {
63       return false;
64     }
65     HiddenColumns that = (HiddenColumns) obj;
66
67     /*
68      * check hidden columns are either both null, or match
69      */
70     if (this.hiddenColumns == null)
71     {
72       return (that.hiddenColumns == null);
73     }
74     if (that.hiddenColumns == null
75             || that.hiddenColumns.size() != this.hiddenColumns.size())
76     {
77       return false;
78     }
79     int i = 0;
80     for (int[] thisRange : hiddenColumns)
81     {
82       int[] thatRange = that.hiddenColumns.get(i++);
83       if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
84       {
85         return false;
86       }
87     }
88     return true;
89   }
90
91   /**
92    * Return absolute column index for a visible column index
93    * 
94    * @param column
95    *          int column index in alignment view (count from zero)
96    * @return alignment column index for column
97    */
98   public int adjustForHiddenColumns(int column)
99   {
100     int result = column;
101     if (hiddenColumns != null)
102     {
103       for (int i = 0; i < hiddenColumns.size(); i++)
104       {
105         int[] region = hiddenColumns.elementAt(i);
106         if (result >= region[0])
107         {
108           result += region[1] - region[0] + 1;
109         }
110       }
111     }
112     return result;
113   }
114
115   /**
116    * Use this method to find out where a column will appear in the visible
117    * alignment when hidden columns exist. If the column is not visible, then the
118    * left-most visible column will always be returned.
119    * 
120    * @param hiddenColumn
121    *          the column index in the full alignment including hidden columns
122    * @return the position of the column in the visible alignment
123    */
124   public int findColumnPosition(int hiddenColumn)
125   {
126     int result = hiddenColumn;
127     if (hiddenColumns != null)
128     {
129       int index = 0;
130       int[] region;
131       do
132       {
133         region = hiddenColumns.elementAt(index++);
134         if (hiddenColumn > region[1])
135         {
136           result -= region[1] + 1 - region[0];
137         }
138       } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
139
140       if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
141       {
142         // Here the hidden column is within a region, so
143         // we want to return the position of region[0]-1, adjusted for any
144         // earlier hidden columns.
145         // Calculate the difference between the actual hidden col position
146         // and region[0]-1, and then subtract from result to convert result from
147         // the adjusted hiddenColumn value to the adjusted region[0]-1 value
148
149         // However, if the region begins at 0 we cannot return region[0]-1
150         // just return 0
151         if (region[0] == 0)
152         {
153           return 0;
154         }
155         else
156         {
157           return result - (hiddenColumn - region[0] + 1);
158         }
159       }
160     }
161     return result; // return the shifted position after removing hidden columns.
162   }
163
164   /**
165    * Find the visible column which is a given visible number of columns to the
166    * left of another visible column. i.e. for a startColumn x, the column which
167    * is distance 1 away will be column x-1.
168    * 
169    * @param visibleDistance
170    *          the number of visible columns to offset by
171    * @param startColumn
172    *          the column to start from
173    * @return the position of the column in the visible alignment
174    */
175   public int subtractVisibleColumns(int visibleDistance, int startColumn)
176   {
177     int distance = visibleDistance;
178
179     // in case startColumn is in a hidden region, move it to the left
180     int start = adjustForHiddenColumns(findColumnPosition(startColumn));
181
182     // get index of hidden region to left of start
183     int index = getHiddenIndexLeft(start);
184     if (index == -1)
185     {
186       // no hidden regions to left of startColumn
187       return start - distance;
188     }
189
190     // walk backwards through the alignment subtracting the counts of visible
191     // columns from distance
192     int[] region;
193     int gap = 0;
194     int nextstart = start;
195
196     while ((index > -1) && (distance - gap > 0))
197     {
198       // subtract the gap to right of region from distance
199       distance -= gap;
200       start = nextstart;
201
202       // calculate the next gap
203       region = hiddenColumns.get(index);
204       gap = start - region[1];
205
206       // set start to just to left of current region
207       nextstart = region[0] - 1;
208       index--;
209     }
210
211     if (distance - gap > 0)
212     {
213       // fell out of loop because there are no more hidden regions
214       distance -= gap;
215       return nextstart - distance;
216     }
217     return start - distance;
218
219   }
220
221   /**
222    * Use this method to determine where the next hiddenRegion starts
223    * 
224    * @param hiddenRegion
225    *          index of hidden region (counts from 0)
226    * @return column number in visible view
227    */
228   public int findHiddenRegionPosition(int hiddenRegion)
229   {
230     int result = 0;
231     if (hiddenColumns != null)
232     {
233       int index = 0;
234       int gaps = 0;
235       do
236       {
237         int[] region = hiddenColumns.elementAt(index);
238         if (hiddenRegion == 0)
239         {
240           return region[0];
241         }
242
243         gaps += region[1] + 1 - region[0];
244         result = region[1] + 1;
245         index++;
246       } while (index <= hiddenRegion);
247
248       result -= gaps;
249     }
250
251     return result;
252   }
253
254   /**
255    * This method returns the rightmost limit of a region of an alignment with
256    * hidden columns. In otherwords, the next hidden column.
257    * 
258    * @param index
259    *          int
260    */
261   public int getHiddenBoundaryRight(int alPos)
262   {
263     if (hiddenColumns != null)
264     {
265       int index = 0;
266       do
267       {
268         int[] region = hiddenColumns.elementAt(index);
269         if (alPos < region[0])
270         {
271           return region[0];
272         }
273
274         index++;
275       } while (index < hiddenColumns.size());
276     }
277
278     return alPos;
279
280   }
281
282   /**
283    * This method returns the leftmost limit of a region of an alignment with
284    * hidden columns. In otherwords, the previous hidden column.
285    * 
286    * @param index
287    *          int
288    */
289   public int getHiddenBoundaryLeft(int alPos)
290   {
291     if (hiddenColumns != null)
292     {
293       int index = hiddenColumns.size() - 1;
294       do
295       {
296         int[] region = hiddenColumns.elementAt(index);
297         if (alPos > region[1])
298         {
299           return region[1];
300         }
301
302         index--;
303       } while (index > -1);
304     }
305
306     return alPos;
307
308   }
309
310   /**
311    * This method returns the index of the hidden region to the left of a column
312    * position. If the column is in a hidden region it returns the index of the
313    * region to the left. If there is no hidden region to the left it returns -1.
314    * 
315    * @param pos
316    *          int
317    */
318   private int getHiddenIndexLeft(int pos)
319   {
320     if (hiddenColumns != null)
321     {
322       int index = hiddenColumns.size() - 1;
323       do
324       {
325         int[] region = hiddenColumns.elementAt(index);
326         if (pos > region[1])
327         {
328           return index;
329         }
330
331         index--;
332       } while (index > -1);
333     }
334
335     return -1;
336
337   }
338
339   /**
340    * Adds the specified column range to the hidden columns
341    * 
342    * @param start
343    * @param end
344    */
345   public void hideColumns(int start, int end)
346   {
347     if (hiddenColumns == null)
348     {
349       hiddenColumns = new Vector<int[]>();
350     }
351
352     /*
353      * traverse existing hidden ranges and insert / amend / append as
354      * appropriate
355      */
356     for (int i = 0; i < hiddenColumns.size(); i++)
357     {
358       int[] region = hiddenColumns.elementAt(i);
359
360       if (end < region[0] - 1)
361       {
362         /*
363          * insert discontiguous preceding range
364          */
365         hiddenColumns.insertElementAt(new int[] { start, end }, i);
366         return;
367       }
368
369       if (end <= region[1])
370       {
371         /*
372          * new range overlaps existing, or is contiguous preceding it - adjust
373          * start column
374          */
375         region[0] = Math.min(region[0], start);
376         return;
377       }
378
379       if (start <= region[1] + 1)
380       {
381         /*
382          * new range overlaps existing, or is contiguous following it - adjust
383          * start and end columns
384          */
385         region[0] = Math.min(region[0], start);
386         region[1] = Math.max(region[1], end);
387
388         /*
389          * also update or remove any subsequent ranges 
390          * that are overlapped
391          */
392         while (i < hiddenColumns.size() - 1)
393         {
394           int[] nextRegion = hiddenColumns.get(i + 1);
395           if (nextRegion[0] > end + 1)
396           {
397             /*
398              * gap to next hidden range - no more to update
399              */
400             break;
401           }
402           region[1] = Math.max(nextRegion[1], end);
403           hiddenColumns.remove(i + 1);
404         }
405         return;
406       }
407     }
408
409     /*
410      * remaining case is that the new range follows everything else
411      */
412     hiddenColumns.addElement(new int[] { start, end });
413   }
414
415   public boolean isVisible(int column)
416   {
417     if (hiddenColumns != null)
418     {
419       for (int[] region : hiddenColumns)
420       {
421         if (column >= region[0] && column <= region[1])
422         {
423           return false;
424         }
425       }
426     }
427
428     return true;
429   }
430
431   /**
432    * ColumnSelection
433    */
434   public HiddenColumns()
435   {
436   }
437
438   /**
439    * Copy constructor
440    * 
441    * @param copy
442    */
443   public HiddenColumns(HiddenColumns copy)
444   {
445     if (copy != null)
446     {
447       if (copy.hiddenColumns != null)
448       {
449         hiddenColumns = new Vector<int[]>(copy.hiddenColumns.size());
450         for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
451         {
452           int[] rh, cp;
453           rh = copy.hiddenColumns.elementAt(i);
454           if (rh != null)
455           {
456             cp = new int[rh.length];
457             System.arraycopy(rh, 0, cp, 0, rh.length);
458             hiddenColumns.addElement(cp);
459           }
460         }
461       }
462     }
463   }
464
465   /**
466    * propagate shift in alignment columns to column selection
467    * 
468    * @param start
469    *          beginning of edit
470    * @param left
471    *          shift in edit (+ve for removal, or -ve for inserts)
472    */
473   public List<int[]> compensateForEdit(int start, int change,
474           ColumnSelection sel)
475   {
476     List<int[]> deletedHiddenColumns = null;
477
478     if (hiddenColumns != null)
479     {
480       deletedHiddenColumns = new ArrayList<int[]>();
481       int hSize = hiddenColumns.size();
482       for (int i = 0; i < hSize; i++)
483       {
484         int[] region = hiddenColumns.elementAt(i);
485         if (region[0] > start && start + change > region[1])
486         {
487           deletedHiddenColumns.add(region);
488
489           hiddenColumns.removeElementAt(i);
490           i--;
491           hSize--;
492           continue;
493         }
494
495         if (region[0] > start)
496         {
497           region[0] -= change;
498           region[1] -= change;
499         }
500
501         if (region[0] < 0)
502         {
503           region[0] = 0;
504         }
505
506       }
507
508       this.revealHiddenColumns(0, sel);
509     }
510
511     return deletedHiddenColumns;
512   }
513
514   /**
515    * propagate shift in alignment columns to column selection special version of
516    * compensateForEdit - allowing for edits within hidden regions
517    * 
518    * @param start
519    *          beginning of edit
520    * @param left
521    *          shift in edit (+ve for removal, or -ve for inserts)
522    */
523   public void compensateForDelEdits(int start, int change)
524   {
525     if (hiddenColumns != null)
526     {
527       for (int i = 0; i < hiddenColumns.size(); i++)
528       {
529         int[] region = hiddenColumns.elementAt(i);
530         if (region[0] >= start)
531         {
532           region[0] -= change;
533         }
534         if (region[1] >= start)
535         {
536           region[1] -= change;
537         }
538         if (region[1] < region[0])
539         {
540           hiddenColumns.removeElementAt(i--);
541         }
542
543         if (region[0] < 0)
544         {
545           region[0] = 0;
546         }
547         if (region[1] < 0)
548         {
549           region[1] = 0;
550         }
551       }
552     }
553   }
554
555   /**
556    * return all visible segments between the given start and end boundaries
557    * 
558    * @param start
559    *          (first column inclusive from 0)
560    * @param end
561    *          (last column - not inclusive)
562    * @return int[] {i_start, i_end, ..} where intervals lie in
563    *         start<=i_start<=i_end<end
564    */
565   public int[] getVisibleContigs(int start, int end)
566   {
567     if (hiddenColumns != null && hiddenColumns.size() > 0)
568     {
569       List<int[]> visiblecontigs = new ArrayList<int[]>();
570       List<int[]> regions = getHiddenRegions();
571
572       int vstart = start;
573       int[] region;
574       int hideStart, hideEnd;
575
576       for (int j = 0; vstart < end && j < regions.size(); j++)
577       {
578         region = regions.get(j);
579         hideStart = region[0];
580         hideEnd = region[1];
581
582         if (hideEnd < vstart)
583         {
584           continue;
585         }
586         if (hideStart > vstart)
587         {
588           visiblecontigs.add(new int[] { vstart, hideStart - 1 });
589         }
590         vstart = hideEnd + 1;
591       }
592
593       if (vstart < end)
594       {
595         visiblecontigs.add(new int[] { vstart, end - 1 });
596       }
597       int[] vcontigs = new int[visiblecontigs.size() * 2];
598       for (int i = 0, j = visiblecontigs.size(); i < j; i++)
599       {
600         int[] vc = visiblecontigs.get(i);
601         visiblecontigs.set(i, null);
602         vcontigs[i * 2] = vc[0];
603         vcontigs[i * 2 + 1] = vc[1];
604       }
605       visiblecontigs.clear();
606       return vcontigs;
607     }
608     else
609     {
610       return new int[] { start, end - 1 };
611     }
612   }
613
614   public String[] getVisibleSequenceStrings(int start, int end,
615           SequenceI[] seqs)
616   {
617     int i, iSize = seqs.length;
618     String selections[] = new String[iSize];
619     if (hiddenColumns != null && hiddenColumns.size() > 0)
620     {
621       for (i = 0; i < iSize; i++)
622       {
623         StringBuffer visibleSeq = new StringBuffer();
624         List<int[]> regions = getHiddenRegions();
625
626         int blockStart = start, blockEnd = end;
627         int[] region;
628         int hideStart, hideEnd;
629
630         for (int j = 0; j < regions.size(); j++)
631         {
632           region = regions.get(j);
633           hideStart = region[0];
634           hideEnd = region[1];
635
636           if (hideStart < start)
637           {
638             continue;
639           }
640
641           blockStart = Math.min(blockStart, hideEnd + 1);
642           blockEnd = Math.min(blockEnd, hideStart);
643
644           if (blockStart > blockEnd)
645           {
646             break;
647           }
648
649           visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
650
651           blockStart = hideEnd + 1;
652           blockEnd = end;
653         }
654
655         if (end > blockStart)
656         {
657           visibleSeq.append(seqs[i].getSequence(blockStart, end));
658         }
659
660         selections[i] = visibleSeq.toString();
661       }
662     }
663     else
664     {
665       for (i = 0; i < iSize; i++)
666       {
667         selections[i] = seqs[i].getSequenceAsString(start, end);
668       }
669     }
670
671     return selections;
672   }
673
674   /**
675    * Locate the first and last position visible for this sequence. if seq isn't
676    * visible then return the position of the left and right of the hidden
677    * boundary region, and the corresponding alignment column indices for the
678    * extent of the sequence
679    * 
680    * @param seq
681    * @return int[] { visible start, visible end, first seqpos, last seqpos,
682    *         alignment index for seq start, alignment index for seq end }
683    */
684   public int[] locateVisibleBoundsOfSequence(SequenceI seq)
685   {
686     int fpos = seq.getStart(), lpos = seq.getEnd();
687     int start = 0;
688
689     if (hiddenColumns == null || hiddenColumns.size() == 0)
690     {
691       int ifpos = seq.findIndex(fpos) - 1, ilpos = seq.findIndex(lpos) - 1;
692       return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
693     }
694
695     // Simply walk along the sequence whilst watching for hidden column
696     // boundaries
697     List<int[]> regions = getHiddenRegions();
698     int spos = fpos, lastvispos = -1, rcount = 0, hideStart = seq
699             .getLength(), hideEnd = -1;
700     int visPrev = 0, visNext = 0, firstP = -1, lastP = -1;
701     boolean foundStart = false;
702     for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
703             && p < pLen; p++)
704     {
705       if (!Comparison.isGap(seq.getCharAt(p)))
706       {
707         // keep track of first/last column
708         // containing sequence data regardless of visibility
709         if (firstP == -1)
710         {
711           firstP = p;
712         }
713         lastP = p;
714         // update hidden region start/end
715         while (hideEnd < p && rcount < regions.size())
716         {
717           int[] region = regions.get(rcount++);
718           visPrev = visNext;
719           visNext += region[0] - visPrev;
720           hideStart = region[0];
721           hideEnd = region[1];
722         }
723         if (hideEnd < p)
724         {
725           hideStart = seq.getLength();
726         }
727         // update visible boundary for sequence
728         if (p < hideStart)
729         {
730           if (!foundStart)
731           {
732             fpos = spos;
733             start = p;
734             foundStart = true;
735           }
736           lastvispos = p;
737           lpos = spos;
738         }
739         // look for next sequence position
740         spos++;
741       }
742     }
743     if (foundStart)
744     {
745       return new int[] { findColumnPosition(start),
746           findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
747     }
748     // otherwise, sequence was completely hidden
749     return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
750   }
751
752   /**
753    * delete any columns in alignmentAnnotation that are hidden (including
754    * sequence associated annotation).
755    * 
756    * @param alignmentAnnotation
757    */
758   public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
759   {
760     makeVisibleAnnotation(-1, -1, alignmentAnnotation);
761   }
762
763   /**
764    * delete any columns in alignmentAnnotation that are hidden (including
765    * sequence associated annotation).
766    * 
767    * @param start
768    *          remove any annotation to the right of this column
769    * @param end
770    *          remove any annotation to the left of this column
771    * @param alignmentAnnotation
772    *          the annotation to operate on
773    */
774   public void makeVisibleAnnotation(int start, int end,
775           AlignmentAnnotation alignmentAnnotation)
776   {
777     if (alignmentAnnotation.annotations == null)
778     {
779       return;
780     }
781     if (start == end && end == -1)
782     {
783       start = 0;
784       end = alignmentAnnotation.annotations.length;
785     }
786     if (hiddenColumns != null && hiddenColumns.size() > 0)
787     {
788       // then mangle the alignmentAnnotation annotation array
789       Vector<Annotation[]> annels = new Vector<Annotation[]>();
790       Annotation[] els = null;
791       List<int[]> regions = getHiddenRegions();
792       int blockStart = start, blockEnd = end;
793       int[] region;
794       int hideStart, hideEnd, w = 0;
795
796       for (int j = 0; j < regions.size(); j++)
797       {
798         region = regions.get(j);
799         hideStart = region[0];
800         hideEnd = region[1];
801
802         if (hideStart < start)
803         {
804           continue;
805         }
806
807         blockStart = Math.min(blockStart, hideEnd + 1);
808         blockEnd = Math.min(blockEnd, hideStart);
809
810         if (blockStart > blockEnd)
811         {
812           break;
813         }
814
815         annels.addElement(els = new Annotation[blockEnd - blockStart]);
816         System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
817                 0, els.length);
818         w += els.length;
819         blockStart = hideEnd + 1;
820         blockEnd = end;
821       }
822
823       if (end > blockStart)
824       {
825         annels.addElement(els = new Annotation[end - blockStart + 1]);
826         if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
827         {
828           // copy just the visible segment of the annotation row
829           System.arraycopy(alignmentAnnotation.annotations, blockStart,
830                   els, 0, els.length);
831         }
832         else
833         {
834           // copy to the end of the annotation row
835           System.arraycopy(alignmentAnnotation.annotations, blockStart,
836                   els, 0,
837                   (alignmentAnnotation.annotations.length - blockStart));
838         }
839         w += els.length;
840       }
841       if (w == 0)
842       {
843         return;
844       }
845
846       alignmentAnnotation.annotations = new Annotation[w];
847       w = 0;
848
849       for (Annotation[] chnk : annels)
850       {
851         System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
852                 chnk.length);
853         w += chnk.length;
854       }
855     }
856     else
857     {
858       alignmentAnnotation.restrict(start, end);
859     }
860   }
861
862   /**
863    * 
864    * @return true if there are columns hidden
865    */
866   public boolean hasHiddenColumns()
867   {
868     return hiddenColumns != null && hiddenColumns.size() > 0;
869   }
870
871   /**
872    * 
873    * @return true if there are more than one set of columns hidden
874    */
875   public boolean hasManyHiddenColumns()
876   {
877     return hiddenColumns != null && hiddenColumns.size() > 1;
878   }
879
880   /**
881    * mark the columns corresponding to gap characters as hidden in the column
882    * selection
883    * 
884    * @param sr
885    */
886   public void hideInsertionsFor(SequenceI sr)
887   {
888     List<int[]> inserts = sr.getInsertions();
889     for (int[] r : inserts)
890     {
891       hideColumns(r[0], r[1]);
892     }
893   }
894
895   /**
896    * Unhides, and adds to the selection list, all hidden columns
897    */
898   public void revealAllHiddenColumns(ColumnSelection sel)
899   {
900     if (hiddenColumns != null)
901     {
902       for (int i = 0; i < hiddenColumns.size(); i++)
903       {
904         int[] region = hiddenColumns.elementAt(i);
905         for (int j = region[0]; j < region[1] + 1; j++)
906         {
907           sel.addElement(j);
908         }
909       }
910     }
911
912     hiddenColumns = null;
913   }
914
915   /**
916    * Reveals, and marks as selected, the hidden column range with the given
917    * start column
918    * 
919    * @param start
920    */
921   public void revealHiddenColumns(int start, ColumnSelection sel)
922   {
923     for (int i = 0; i < hiddenColumns.size(); i++)
924     {
925       int[] region = hiddenColumns.elementAt(i);
926       if (start == region[0])
927       {
928         for (int j = region[0]; j < region[1] + 1; j++)
929         {
930           sel.addElement(j);
931         }
932
933         hiddenColumns.removeElement(region);
934         break;
935       }
936     }
937     if (hiddenColumns.size() == 0)
938     {
939       hiddenColumns = null;
940     }
941   }
942
943   /**
944    * removes intersection of position,length ranges in deletions from the
945    * start,end regions marked in intervals.
946    * 
947    * @param shifts
948    * @param intervals
949    * @return
950    */
951   private boolean pruneIntervalVector(final List<int[]> shifts,
952           Vector<int[]> intervals)
953   {
954     boolean pruned = false;
955     int i = 0, j = intervals.size() - 1, s = 0, t = shifts.size() - 1;
956     int hr[] = intervals.elementAt(i);
957     int sr[] = shifts.get(s);
958     while (i <= j && s <= t)
959     {
960       boolean trailinghn = hr[1] >= sr[0];
961       if (!trailinghn)
962       {
963         if (i < j)
964         {
965           hr = intervals.elementAt(++i);
966         }
967         else
968         {
969           i++;
970         }
971         continue;
972       }
973       int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
974       if (endshift < hr[0] || endshift < sr[0])
975       { // leadinghc disjoint or not a deletion
976         if (s < t)
977         {
978           sr = shifts.get(++s);
979         }
980         else
981         {
982           s++;
983         }
984         continue;
985       }
986       boolean leadinghn = hr[0] >= sr[0];
987       boolean leadinghc = hr[0] < endshift;
988       boolean trailinghc = hr[1] < endshift;
989       if (leadinghn)
990       {
991         if (trailinghc)
992         { // deleted hidden region.
993           intervals.removeElementAt(i);
994           pruned = true;
995           j--;
996           if (i <= j)
997           {
998             hr = intervals.elementAt(i);
999           }
1000           continue;
1001         }
1002         if (leadinghc)
1003         {
1004           hr[0] = endshift; // clip c terminal region
1005           leadinghn = !leadinghn;
1006           pruned = true;
1007         }
1008       }
1009       if (!leadinghn)
1010       {
1011         if (trailinghc)
1012         {
1013           if (trailinghn)
1014           {
1015             hr[1] = sr[0] - 1;
1016             pruned = true;
1017           }
1018         }
1019         else
1020         {
1021           // sr contained in hr
1022           if (s < t)
1023           {
1024             sr = shifts.get(++s);
1025           }
1026           else
1027           {
1028             s++;
1029           }
1030           continue;
1031         }
1032       }
1033     }
1034     return pruned; // true if any interval was removed or modified by
1035     // operations.
1036   }
1037
1038   /**
1039    * remove any hiddenColumns or selected columns and shift remaining based on a
1040    * series of position, range deletions.
1041    * 
1042    * @param deletions
1043    */
1044   public void pruneDeletions(List<int[]> shifts)
1045   {
1046     // delete any intervals intersecting.
1047     if (hiddenColumns != null)
1048     {
1049       pruneIntervalVector(shifts, hiddenColumns);
1050       if (hiddenColumns != null && hiddenColumns.size() == 0)
1051       {
1052         hiddenColumns = null;
1053       }
1054     }
1055   }
1056
1057   /**
1058    * Add gaps into the sequences aligned to profileseq under the given
1059    * AlignmentView
1060    * 
1061    * @param profileseq
1062    * @param al
1063    *          - alignment to have gaps inserted into it
1064    * @param input
1065    *          - alignment view where sequence corresponding to profileseq is
1066    *          first entry
1067    * @return new HiddenColumns for new alignment view, with insertions into
1068    *         profileseq marked as hidden.
1069    */
1070   public static HiddenColumns propagateInsertions(SequenceI profileseq,
1071           AlignmentI al, AlignmentView input)
1072   {
1073     int profsqpos = 0;
1074
1075     char gc = al.getGapCharacter();
1076     Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1077     HiddenColumns nview = (HiddenColumns) alandhidden[1];
1078     SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1079     nview.propagateInsertions(profileseq, al, origseq);
1080     return nview;
1081   }
1082
1083   /**
1084    * 
1085    * @param profileseq
1086    *          - sequence in al which corresponds to origseq
1087    * @param al
1088    *          - alignment which is to have gaps inserted into it
1089    * @param origseq
1090    *          - sequence corresponding to profileseq which defines gap map for
1091    *          modifying al
1092    */
1093   private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1094           SequenceI origseq)
1095   {
1096     char gc = al.getGapCharacter();
1097     // recover mapping between sequence's non-gap positions and positions
1098     // mapping to view.
1099     pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1100     int[] viscontigs = al.getHiddenColumns().getVisibleContigs(0,
1101             profileseq.getLength());
1102     int spos = 0;
1103     int offset = 0;
1104
1105     // add profile to visible contigs
1106     for (int v = 0; v < viscontigs.length; v += 2)
1107     {
1108       if (viscontigs[v] > spos)
1109       {
1110         StringBuffer sb = new StringBuffer();
1111         for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1112         {
1113           sb.append(gc);
1114         }
1115         for (int s = 0, ns = al.getHeight(); s < ns; s++)
1116         {
1117           SequenceI sqobj = al.getSequenceAt(s);
1118           if (sqobj != profileseq)
1119           {
1120             String sq = al.getSequenceAt(s).getSequenceAsString();
1121             if (sq.length() <= spos + offset)
1122             {
1123               // pad sequence
1124               int diff = spos + offset - sq.length() - 1;
1125               if (diff > 0)
1126               {
1127                 // pad gaps
1128                 sq = sq + sb;
1129                 while ((diff = spos + offset - sq.length() - 1) > 0)
1130                 {
1131                   // sq = sq
1132                   // + ((diff >= sb.length()) ? sb.toString() : sb
1133                   // .substring(0, diff));
1134                   if (diff >= sb.length())
1135                   {
1136                     sq += sb.toString();
1137                   }
1138                   else
1139                   {
1140                     char[] buf = new char[diff];
1141                     sb.getChars(0, diff, buf, 0);
1142                     sq += buf.toString();
1143                   }
1144                 }
1145               }
1146               sq += sb.toString();
1147             }
1148             else
1149             {
1150               al.getSequenceAt(s).setSequence(
1151                       sq.substring(0, spos + offset) + sb.toString()
1152                               + sq.substring(spos + offset));
1153             }
1154           }
1155         }
1156         // offset+=sb.length();
1157       }
1158       spos = viscontigs[v + 1] + 1;
1159     }
1160     if ((offset + spos) < profileseq.getLength())
1161     {
1162       // pad the final region with gaps.
1163       StringBuffer sb = new StringBuffer();
1164       for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1165       {
1166         sb.append(gc);
1167       }
1168       for (int s = 0, ns = al.getHeight(); s < ns; s++)
1169       {
1170         SequenceI sqobj = al.getSequenceAt(s);
1171         if (sqobj == profileseq)
1172         {
1173           continue;
1174         }
1175         String sq = sqobj.getSequenceAsString();
1176         // pad sequence
1177         int diff = origseq.getLength() - sq.length();
1178         while (diff > 0)
1179         {
1180           // sq = sq
1181           // + ((diff >= sb.length()) ? sb.toString() : sb
1182           // .substring(0, diff));
1183           if (diff >= sb.length())
1184           {
1185             sq += sb.toString();
1186           }
1187           else
1188           {
1189             char[] buf = new char[diff];
1190             sb.getChars(0, diff, buf, 0);
1191             sq += buf.toString();
1192           }
1193           diff = origseq.getLength() - sq.length();
1194         }
1195       }
1196     }
1197   }
1198
1199   /**
1200    * remove any hiddenColumns or selected columns and shift remaining based on a
1201    * series of position, range deletions.
1202    * 
1203    * @param deletions
1204    */
1205   private void pruneDeletions(ShiftList deletions)
1206   {
1207     if (deletions != null)
1208     {
1209       final List<int[]> shifts = deletions.getShifts();
1210       if (shifts != null && shifts.size() > 0)
1211       {
1212         pruneDeletions(shifts);
1213
1214         // and shift the rest.
1215         this.compensateForEdits(deletions);
1216       }
1217     }
1218   }
1219
1220   /**
1221    * Adjust hidden column boundaries based on a series of column additions or
1222    * deletions in visible regions.
1223    * 
1224    * @param shiftrecord
1225    * @return
1226    */
1227   private ShiftList compensateForEdits(ShiftList shiftrecord)
1228   {
1229     if (shiftrecord != null)
1230     {
1231       final List<int[]> shifts = shiftrecord.getShifts();
1232       if (shifts != null && shifts.size() > 0)
1233       {
1234         int shifted = 0;
1235         for (int i = 0, j = shifts.size(); i < j; i++)
1236         {
1237           int[] sh = shifts.get(i);
1238           compensateForDelEdits(shifted + sh[0], sh[1]);
1239           shifted -= sh[1];
1240         }
1241       }
1242       return shiftrecord.getInverse();
1243     }
1244     return null;
1245   }
1246
1247   /**
1248    * Returns a hashCode built from hidden column ranges
1249    */
1250   @Override
1251   public int hashCode()
1252   {
1253     int hashCode = 1;
1254     if (hiddenColumns != null)
1255     {
1256       for (int[] hidden : hiddenColumns)
1257       {
1258         hashCode = 31 * hashCode + hidden[0];
1259         hashCode = 31 * hashCode + hidden[1];
1260       }
1261     }
1262     return hashCode;
1263   }
1264
1265 }