2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.datamodel;
23 import jalview.util.ShiftList;
24 import jalview.viewmodel.annotationfilter.AnnotationFilterParameter;
25 import jalview.viewmodel.annotationfilter.AnnotationFilterParameter.SearchableAnnotationField;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.Enumeration;
30 import java.util.List;
31 import java.util.Vector;
34 * NOTE: Columns are zero based.
36 public class ColumnSelection
38 Vector selected = new Vector();
40 // Vector of int [] {startCol, endCol}
41 Vector<int[]> hiddenColumns;
44 * Add a column to the selection
49 public void addElement(int col)
51 Integer column = new Integer(col);
52 if (!selected.contains(column))
54 selected.addElement(column);
59 * clears column selection
63 selected.removeAllElements();
67 * removes col from selection
70 * index of column to be removed
72 public void removeElement(int col)
74 Integer colInt = new Integer(col);
76 if (selected.contains(colInt))
78 selected.removeElement(colInt);
83 * removes a range of columns from the selection
86 * int - first column in range to be removed
90 public void removeElements(int start, int end)
93 for (int i = start; i < end; i++)
95 colInt = new Integer(i);
96 if (selected.contains(colInt))
98 selected.removeElement(colInt);
105 * @return Vector containing selected columns as Integers
107 public Vector getSelected()
115 * index to search for in column selection
117 * @return true if Integer(col) is in selection.
119 public boolean contains(int col)
121 return selected.contains(new Integer(col));
125 * Column number at position i in selection
128 * index into selected columns
130 * @return column number in alignment
132 public int columnAt(int i)
134 return ((Integer) selected.elementAt(i)).intValue();
140 * @return DOCUMENT ME!
144 return selected.size();
148 * rightmost selected column
150 * @return rightmost column in alignment that is selected
156 for (int i = 0; i < selected.size(); i++)
158 if (columnAt(i) > max)
168 * Leftmost column in selection
170 * @return column index of leftmost column in selection
174 int min = 1000000000;
176 for (int i = 0; i < selected.size(); i++)
178 if (columnAt(i) < min)
188 * propagate shift in alignment columns to column selection
193 * shift in edit (+ve for removal, or -ve for inserts)
195 public List<int[]> compensateForEdit(int start, int change)
197 List<int[]> deletedHiddenColumns = null;
198 for (int i = 0; i < size(); i++)
200 int temp = columnAt(i);
204 selected.setElementAt(new Integer(temp - change), i);
208 if (hiddenColumns != null)
210 deletedHiddenColumns = new ArrayList<int[]>();
211 int hSize = hiddenColumns.size();
212 for (int i = 0; i < hSize; i++)
214 int[] region = hiddenColumns.elementAt(i);
215 if (region[0] > start && start + change > region[1])
217 deletedHiddenColumns.add(region);
219 hiddenColumns.removeElementAt(i);
225 if (region[0] > start)
238 this.revealHiddenColumns(0);
241 return deletedHiddenColumns;
245 * propagate shift in alignment columns to column selection special version of
246 * compensateForEdit - allowing for edits within hidden regions
251 * shift in edit (+ve for removal, or -ve for inserts)
253 private void compensateForDelEdits(int start, int change)
255 for (int i = 0; i < size(); i++)
257 int temp = columnAt(i);
261 selected.setElementAt(new Integer(temp - change), i);
265 if (hiddenColumns != null)
267 for (int i = 0; i < hiddenColumns.size(); i++)
269 int[] region = hiddenColumns.elementAt(i);
270 if (region[0] >= start)
274 if (region[1] >= start)
278 if (region[1] < region[0])
280 hiddenColumns.removeElementAt(i--);
296 * Adjust hidden column boundaries based on a series of column additions or
297 * deletions in visible regions.
302 public ShiftList compensateForEdits(ShiftList shiftrecord)
304 if (shiftrecord != null)
306 List<int[]> shifts = shiftrecord.getShifts();
307 if (shifts != null && shifts.size() > 0)
310 for (int i = 0, j = shifts.size(); i < j; i++)
312 int[] sh = shifts.get(i);
313 // compensateForEdit(shifted+sh[0], sh[1]);
314 compensateForDelEdits(shifted + sh[0], sh[1]);
318 return shiftrecord.getInverse();
324 * removes intersection of position,length ranges in deletions from the
325 * start,end regions marked in intervals.
331 private boolean pruneIntervalVector(List<int[]> shifts, Vector intervals)
333 boolean pruned = false;
334 int i = 0, j = intervals.size() - 1, s = 0, t = shifts.size() - 1;
335 int hr[] = (int[]) intervals.elementAt(i);
336 int sr[] = shifts.get(s);
337 while (i <= j && s <= t)
339 boolean trailinghn = hr[1] >= sr[0];
344 hr = (int[]) intervals.elementAt(++i);
352 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
353 if (endshift < hr[0] || endshift < sr[0])
354 { // leadinghc disjoint or not a deletion
357 sr = shifts.get(++s);
365 boolean leadinghn = hr[0] >= sr[0];
366 boolean leadinghc = hr[0] < endshift;
367 boolean trailinghc = hr[1] < endshift;
371 { // deleted hidden region.
372 intervals.removeElementAt(i);
377 hr = (int[]) intervals.elementAt(i);
383 hr[0] = endshift; // clip c terminal region
384 leadinghn = !leadinghn;
400 // sr contained in hr
403 sr = shifts.get(++s);
413 return pruned; // true if any interval was removed or modified by
417 private boolean pruneColumnList(List<int[]> shifts, Vector list)
419 int s = 0, t = shifts.size();
420 int[] sr = shifts.get(s++);
421 boolean pruned = false;
422 int i = 0, j = list.size();
423 while (i < j && s <= t)
425 int c = ((Integer) list.elementAt(i++)).intValue();
428 if (sr[1] + sr[0] >= c)
429 { // sr[1] -ve means inseriton.
430 list.removeElementAt(--i);
447 * remove any hiddenColumns or selected columns and shift remaining based on a
448 * series of position, range deletions.
452 public void pruneDeletions(ShiftList deletions)
454 if (deletions != null)
456 List<int[]> shifts = deletions.getShifts();
457 if (shifts != null && shifts.size() > 0)
459 // delete any intervals intersecting.
460 if (hiddenColumns != null)
462 pruneIntervalVector(shifts, hiddenColumns);
463 if (hiddenColumns != null && hiddenColumns.size() == 0)
465 hiddenColumns = null;
468 if (selected != null && selected.size() > 0)
470 pruneColumnList(shifts, selected);
471 if (selected != null && selected.size() == 0)
476 // and shift the rest.
477 this.compensateForEdits(deletions);
483 * This Method is used to return all the HiddenColumn regions
484 * @return empty list or List of hidden column intervals
486 public List<int[]> getHiddenColumns()
488 return hiddenColumns == null ? Collections.<int[]> emptyList()
493 * Return absolute column index for a visible column index
496 * int column index in alignment view
497 * @return alignment column index for column
499 public int adjustForHiddenColumns(int column)
502 if (hiddenColumns != null)
504 for (int i = 0; i < hiddenColumns.size(); i++)
506 int[] region = hiddenColumns.elementAt(i);
507 if (result >= region[0])
509 result += region[1] - region[0] + 1;
517 * Use this method to find out where a column will appear in the visible
518 * alignment when hidden columns exist. If the column is not visible, then the
519 * left-most visible column will always be returned.
521 * @param hiddenColumn
525 public int findColumnPosition(int hiddenColumn)
527 int result = hiddenColumn;
528 if (hiddenColumns != null)
534 region = hiddenColumns.elementAt(index++);
535 if (hiddenColumn > region[1])
537 result -= region[1] + 1 - region[0];
539 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
540 if (hiddenColumn > region[0] && hiddenColumn < region[1])
542 return region[0] + hiddenColumn - result;
545 return result; // return the shifted position after removing hidden columns.
549 * Use this method to determine where the next hiddenRegion starts
551 public int findHiddenRegionPosition(int hiddenRegion)
554 if (hiddenColumns != null)
560 int[] region = hiddenColumns.elementAt(index);
561 if (hiddenRegion == 0)
566 gaps += region[1] + 1 - region[0];
567 result = region[1] + 1;
569 } while (index < hiddenRegion + 1);
578 * THis method returns the rightmost limit of a region of an alignment with
579 * hidden columns. In otherwords, the next hidden column.
584 public int getHiddenBoundaryRight(int alPos)
586 if (hiddenColumns != null)
591 int[] region = hiddenColumns.elementAt(index);
592 if (alPos < region[0])
598 } while (index < hiddenColumns.size());
606 * This method returns the leftmost limit of a region of an alignment with
607 * hidden columns. In otherwords, the previous hidden column.
612 public int getHiddenBoundaryLeft(int alPos)
614 if (hiddenColumns != null)
616 int index = hiddenColumns.size() - 1;
619 int[] region = hiddenColumns.elementAt(index);
620 if (alPos > region[1])
626 } while (index > -1);
633 public void hideSelectedColumns()
637 int column = ((Integer) getSelected().firstElement()).intValue();
643 public void hideColumns(int start, int end)
645 if (hiddenColumns == null)
647 hiddenColumns = new Vector();
650 boolean added = false;
651 boolean overlap = false;
653 for (int i = 0; i < hiddenColumns.size(); i++)
655 int[] region = hiddenColumns.elementAt(i);
656 if (start <= region[1] && end >= region[0])
658 hiddenColumns.removeElementAt(i);
662 else if (end < region[0] && start < region[0])
664 hiddenColumns.insertElementAt(new int[]
673 hideColumns(start, end);
677 hiddenColumns.addElement(new int[]
684 * This method will find a range of selected columns around the column
690 public void hideColumns(int col)
692 // First find out range of columns to hide
693 int min = col, max = col + 1;
694 while (contains(min))
700 while (contains(max))
713 hideColumns(min, max);
716 public void revealAllHiddenColumns()
718 if (hiddenColumns != null)
720 for (int i = 0; i < hiddenColumns.size(); i++)
722 int[] region = hiddenColumns.elementAt(i);
723 for (int j = region[0]; j < region[1] + 1; j++)
730 hiddenColumns = null;
733 public void revealHiddenColumns(int res)
735 for (int i = 0; i < hiddenColumns.size(); i++)
737 int[] region = hiddenColumns.elementAt(i);
738 if (res == region[0])
740 for (int j = region[0]; j < region[1] + 1; j++)
745 hiddenColumns.removeElement(region);
749 if (hiddenColumns.size() == 0)
751 hiddenColumns = null;
755 public boolean isVisible(int column)
757 if (hiddenColumns != null)
759 for (int i = 0; i < hiddenColumns.size(); i++)
761 int[] region = hiddenColumns.elementAt(i);
762 if (column >= region[0] && column <= region[1])
777 public ColumnSelection(ColumnSelection copy)
781 if (copy.selected != null)
783 selected = new Vector();
784 for (int i = 0, j = copy.selected.size(); i < j; i++)
786 selected.addElement(copy.selected.elementAt(i));
789 if (copy.hiddenColumns != null)
791 hiddenColumns = new Vector(copy.hiddenColumns.size());
792 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
795 rh = copy.hiddenColumns.elementAt(i);
798 cp = new int[rh.length];
799 System.arraycopy(rh, 0, cp, 0, rh.length);
800 hiddenColumns.addElement(cp);
810 public ColumnSelection()
814 public String[] getVisibleSequenceStrings(int start, int end,
817 int i, iSize = seqs.length;
818 String selection[] = new String[iSize];
819 if (hiddenColumns != null && hiddenColumns.size() > 0)
821 for (i = 0; i < iSize; i++)
823 StringBuffer visibleSeq = new StringBuffer();
824 List<int[]> regions = getHiddenColumns();
826 int blockStart = start, blockEnd = end;
828 int hideStart, hideEnd;
830 for (int j = 0; j < regions.size(); j++)
832 region = regions.get(j);
833 hideStart = region[0];
836 if (hideStart < start)
841 blockStart = Math.min(blockStart, hideEnd + 1);
842 blockEnd = Math.min(blockEnd, hideStart);
844 if (blockStart > blockEnd)
849 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
851 blockStart = hideEnd + 1;
855 if (end > blockStart)
857 visibleSeq.append(seqs[i].getSequence(blockStart, end));
860 selection[i] = visibleSeq.toString();
865 for (i = 0; i < iSize; i++)
867 selection[i] = seqs[i].getSequenceAsString(start, end);
875 * return all visible segments between the given start and end boundaries
878 * (first column inclusive from 0)
880 * (last column - not inclusive)
881 * @return int[] {i_start, i_end, ..} where intervals lie in
882 * start<=i_start<=i_end<end
884 public int[] getVisibleContigs(int start, int end)
886 if (hiddenColumns != null && hiddenColumns.size() > 0)
888 Vector visiblecontigs = new Vector();
889 List<int[]> regions = getHiddenColumns();
893 int hideStart, hideEnd;
895 for (int j = 0; vstart < end && j < regions.size(); j++)
897 region = regions.get(j);
898 hideStart = region[0];
901 if (hideEnd < vstart)
905 if (hideStart > vstart)
907 visiblecontigs.addElement(new int[]
908 { vstart, hideStart - 1 });
910 vstart = hideEnd + 1;
915 visiblecontigs.addElement(new int[]
916 { vstart, end - 1 });
918 int[] vcontigs = new int[visiblecontigs.size() * 2];
919 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
921 int[] vc = (int[]) visiblecontigs.elementAt(i);
922 visiblecontigs.setElementAt(null, i);
923 vcontigs[i * 2] = vc[0];
924 vcontigs[i * 2 + 1] = vc[1];
926 visiblecontigs.removeAllElements();
937 * delete any columns in alignmentAnnotation that are hidden (including
938 * sequence associated annotation).
940 * @param alignmentAnnotation
942 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
944 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
948 * delete any columns in alignmentAnnotation that are hidden (including
949 * sequence associated annotation).
952 * remove any annotation to the right of this column
954 * remove any annotation to the left of this column
955 * @param alignmentAnnotation
956 * the annotation to operate on
958 public void makeVisibleAnnotation(int start, int end,
959 AlignmentAnnotation alignmentAnnotation)
961 if (alignmentAnnotation.annotations == null)
965 if (start == end && end == -1)
968 end = alignmentAnnotation.annotations.length;
970 if (hiddenColumns != null && hiddenColumns.size() > 0)
972 // then mangle the alignmentAnnotation annotation array
973 Vector annels = new Vector();
974 Annotation[] els = null;
975 List<int[]> regions = getHiddenColumns();
976 int blockStart = start, blockEnd = end;
978 int hideStart, hideEnd, w = 0;
980 for (int j = 0; j < regions.size(); j++)
982 region = regions.get(j);
983 hideStart = region[0];
986 if (hideStart < start)
991 blockStart = Math.min(blockStart, hideEnd + 1);
992 blockEnd = Math.min(blockEnd, hideStart);
994 if (blockStart > blockEnd)
999 annels.addElement(els = new Annotation[blockEnd - blockStart]);
1000 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1003 blockStart = hideEnd + 1;
1007 if (end > blockStart)
1009 annels.addElement(els = new Annotation[end - blockStart + 1]);
1010 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
1012 // copy just the visible segment of the annotation row
1013 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1014 els, 0, els.length);
1018 // copy to the end of the annotation row
1019 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1021 (alignmentAnnotation.annotations.length - blockStart));
1029 Enumeration e = annels.elements();
1030 alignmentAnnotation.annotations = new Annotation[w];
1032 while (e.hasMoreElements())
1034 Annotation[] chnk = (Annotation[]) e.nextElement();
1035 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1042 alignmentAnnotation.restrict(start, end);
1047 * Invert the column selection from first to end-1. leaves hiddenColumns
1048 * untouched (and unselected)
1053 public void invertColumnSelection(int first, int width)
1055 boolean hasHidden = hiddenColumns != null && hiddenColumns.size() > 0;
1056 for (int i = first; i < width; i++)
1064 if (!hasHidden || isVisible(i))
1073 * add in any unselected columns from the given column selection, excluding
1074 * any that are hidden.
1078 public void addElementsFrom(ColumnSelection colsel)
1080 if (colsel != null && colsel.size() > 0)
1082 Enumeration e = colsel.getSelected().elements();
1083 while (e.hasMoreElements())
1085 Object eo = e.nextElement();
1086 if (hiddenColumns != null && isVisible(((Integer) eo).intValue()))
1088 if (!selected.contains(eo))
1090 selected.addElement(eo);
1098 * set the selected columns the given column selection, excluding any columns
1103 public void setElementsFrom(ColumnSelection colsel)
1105 selected = new Vector();
1106 if (colsel.selected != null && colsel.selected.size() > 0)
1108 if (hiddenColumns != null && hiddenColumns.size() > 0)
1110 // only select visible columns in this columns selection
1111 selected = new Vector();
1112 addElementsFrom(colsel);
1116 // add everything regardless
1117 Enumeration en = colsel.selected.elements();
1118 while (en.hasMoreElements())
1120 selected.addElement(en.nextElement());
1127 * Add gaps into the sequences aligned to profileseq under the given
1132 * - alignment to have gaps inserted into it
1134 * - alignment view where sequence corresponding to profileseq is
1136 * @return new Column selection for new alignment view, with insertions into
1137 * profileseq marked as hidden.
1139 public static ColumnSelection propagateInsertions(SequenceI profileseq,
1140 Alignment al, AlignmentView input)
1144 // return propagateInsertions(profileseq, al, )
1145 char gc = al.getGapCharacter();
1146 Object[] alandcolsel = input.getAlignmentAndColumnSelection(gc);
1147 ColumnSelection nview = (ColumnSelection) alandcolsel[1];
1148 SequenceI origseq = ((SequenceI[]) alandcolsel[0])[profsqpos];
1149 nview.propagateInsertions(profileseq, al, origseq);
1156 * - sequence in al which corresponds to origseq
1158 * - alignment which is to have gaps inserted into it
1160 * - sequence corresponding to profileseq which defines gap map for
1163 public void propagateInsertions(SequenceI profileseq, AlignmentI al,
1166 char gc = al.getGapCharacter();
1167 // recover mapping between sequence's non-gap positions and positions
1169 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1170 int[] viscontigs = getVisibleContigs(0, profileseq.getLength());
1173 // input.pruneDeletions(ShiftList.parseMap(((SequenceI[])
1174 // alandcolsel[0])[0].gapMap()))
1175 // add profile to visible contigs
1176 for (int v = 0; v < viscontigs.length; v += 2)
1178 if (viscontigs[v] > spos)
1180 StringBuffer sb = new StringBuffer();
1181 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1185 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1187 SequenceI sqobj = al.getSequenceAt(s);
1188 if (sqobj != profileseq)
1190 String sq = al.getSequenceAt(s).getSequenceAsString();
1191 if (sq.length() <= spos + offset)
1194 int diff = spos + offset - sq.length() - 1;
1199 while ((diff = spos + offset - sq.length() - 1) > 0)
1202 // + ((diff >= sb.length()) ? sb.toString() : sb
1203 // .substring(0, diff));
1204 if (diff >= sb.length())
1206 sq += sb.toString();
1210 char[] buf = new char[diff];
1211 sb.getChars(0, diff, buf, 0);
1212 sq += buf.toString();
1216 sq += sb.toString();
1220 al.getSequenceAt(s).setSequence(
1221 sq.substring(0, spos + offset) + sb.toString()
1222 + sq.substring(spos + offset));
1226 // offset+=sb.length();
1228 spos = viscontigs[v + 1] + 1;
1230 if ((offset + spos) < profileseq.getLength())
1232 // pad the final region with gaps.
1233 StringBuffer sb = new StringBuffer();
1234 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1238 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1240 SequenceI sqobj = al.getSequenceAt(s);
1241 if (sqobj == profileseq)
1245 String sq = sqobj.getSequenceAsString();
1247 int diff = origseq.getLength() - sq.length();
1251 // + ((diff >= sb.length()) ? sb.toString() : sb
1252 // .substring(0, diff));
1253 if (diff >= sb.length())
1255 sq += sb.toString();
1259 char[] buf = new char[diff];
1260 sb.getChars(0, diff, buf, 0);
1261 sq += buf.toString();
1263 diff = origseq.getLength() - sq.length();
1271 * @return true if there are columns marked
1273 public boolean hasSelectedColumns()
1275 return (selected != null && selected.size() > 0);
1280 * @return true if there are columns hidden
1282 public boolean hasHiddenColumns()
1284 return hiddenColumns != null && hiddenColumns.size() > 0;
1289 * @return true if there are more than one set of columns hidden
1291 public boolean hasManyHiddenColumns()
1293 return hiddenColumns != null && hiddenColumns.size() > 1;
1297 * mark the columns corresponding to gap characters as hidden in the column
1302 public void hideInsertionsFor(SequenceI sr)
1304 List<int[]> inserts = sr.getInsertions();
1305 for (int[] r : inserts)
1307 hideColumns(r[0], r[1]);
1311 public boolean filterAnnotations(Annotation[] annotations,
1312 AnnotationFilterParameter filterParams)
1314 this.revealAllHiddenColumns();
1319 if (annotations[count] != null)
1322 boolean itemMatched = false;
1324 if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD
1325 && annotations[count].value >= filterParams
1326 .getThresholdValue())
1330 if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD
1331 && annotations[count].value <= filterParams
1332 .getThresholdValue())
1337 if (filterParams.isFilterAlphaHelix()
1338 && annotations[count].secondaryStructure == 'H')
1343 if (filterParams.isFilterBetaSheet()
1344 && annotations[count].secondaryStructure == 'E')
1349 if (filterParams.isFilterTurn()
1350 && annotations[count].secondaryStructure == 'S')
1355 String regexSearchString = filterParams.getRegexString();
1356 if (regexSearchString != null
1357 && !filterParams.getRegexSearchFields().isEmpty())
1359 List<SearchableAnnotationField> fields = filterParams
1360 .getRegexSearchFields();
1363 if (fields.contains(SearchableAnnotationField.DISPLAY_STRING)
1364 && annotations[count].displayCharacter
1365 .matches(regexSearchString))
1369 } catch (java.util.regex.PatternSyntaxException pse)
1371 if (annotations[count].displayCharacter
1372 .equals(regexSearchString))
1377 if (fields.contains(SearchableAnnotationField.DESCRIPTION)
1378 && annotations[count].description != null
1379 && annotations[count].description
1380 .matches(regexSearchString))
1388 this.addElement(count);
1392 } while (count < annotations.length);