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.Enumeration;
29 import java.util.List;
30 import java.util.Vector;
33 * NOTE: Columns are zero based.
35 public class ColumnSelection
37 Vector selected = new Vector();
39 // Vector of int [] {startCol, endCol}
43 * Add a column to the selection
48 public void addElement(int col)
50 Integer column = new Integer(col);
51 if (!selected.contains(column))
53 selected.addElement(column);
58 * clears column selection
62 selected.removeAllElements();
66 * removes col from selection
69 * index of column to be removed
71 public void removeElement(int col)
73 Integer colInt = new Integer(col);
75 if (selected.contains(colInt))
77 selected.removeElement(colInt);
82 * removes a range of columns from the selection
85 * int - first column in range to be removed
89 public void removeElements(int start, int end)
92 for (int i = start; i < end; i++)
94 colInt = new Integer(i);
95 if (selected.contains(colInt))
97 selected.removeElement(colInt);
104 * @return Vector containing selected columns as Integers
106 public Vector getSelected()
114 * index to search for in column selection
116 * @return true if Integer(col) is in selection.
118 public boolean contains(int col)
120 return selected.contains(new Integer(col));
124 * Column number at position i in selection
127 * index into selected columns
129 * @return column number in alignment
131 public int columnAt(int i)
133 return ((Integer) selected.elementAt(i)).intValue();
139 * @return DOCUMENT ME!
143 return selected.size();
147 * rightmost selected column
149 * @return rightmost column in alignment that is selected
155 for (int i = 0; i < selected.size(); i++)
157 if (columnAt(i) > max)
167 * Leftmost column in selection
169 * @return column index of leftmost column in selection
173 int min = 1000000000;
175 for (int i = 0; i < selected.size(); i++)
177 if (columnAt(i) < min)
187 * propagate shift in alignment columns to column selection
192 * shift in edit (+ve for removal, or -ve for inserts)
194 public List<int[]> compensateForEdit(int start, int change)
196 List<int[]> deletedHiddenColumns = null;
197 for (int i = 0; i < size(); i++)
199 int temp = columnAt(i);
203 selected.setElementAt(new Integer(temp - change), i);
207 if (hiddenColumns != null)
209 deletedHiddenColumns = new ArrayList<int[]>();
210 int hSize = hiddenColumns.size();
211 for (int i = 0; i < hSize; i++)
213 int[] region = (int[]) hiddenColumns.elementAt(i);
214 if (region[0] > start && start + change > region[1])
216 deletedHiddenColumns.add(region);
218 hiddenColumns.removeElementAt(i);
224 if (region[0] > start)
237 this.revealHiddenColumns(0);
240 return deletedHiddenColumns;
244 * propagate shift in alignment columns to column selection special version of
245 * compensateForEdit - allowing for edits within hidden regions
250 * shift in edit (+ve for removal, or -ve for inserts)
252 private void compensateForDelEdits(int start, int change)
254 for (int i = 0; i < size(); i++)
256 int temp = columnAt(i);
260 selected.setElementAt(new Integer(temp - change), i);
264 if (hiddenColumns != null)
266 for (int i = 0; i < hiddenColumns.size(); i++)
268 int[] region = (int[]) hiddenColumns.elementAt(i);
269 if (region[0] >= start)
273 if (region[1] >= start)
277 if (region[1] < region[0])
279 hiddenColumns.removeElementAt(i--);
295 * Adjust hidden column boundaries based on a series of column additions or
296 * deletions in visible regions.
301 public ShiftList compensateForEdits(ShiftList shiftrecord)
303 if (shiftrecord != null)
305 Vector shifts = shiftrecord.shifts;
306 if (shifts != null && shifts.size() > 0)
309 for (int i = 0, j = shifts.size(); i < j; i++)
311 int[] sh = (int[]) shifts.elementAt(i);
312 // compensateForEdit(shifted+sh[0], sh[1]);
313 compensateForDelEdits(shifted + sh[0], sh[1]);
317 return shiftrecord.getInverse();
323 * removes intersection of position,length ranges in deletions from the
324 * start,end regions marked in intervals.
330 private boolean pruneIntervalVector(Vector deletions, Vector intervals)
332 boolean pruned = false;
333 int i = 0, j = intervals.size() - 1, s = 0, t = deletions.size() - 1;
334 int hr[] = (int[]) intervals.elementAt(i);
335 int sr[] = (int[]) deletions.elementAt(s);
336 while (i <= j && s <= t)
338 boolean trailinghn = hr[1] >= sr[0];
343 hr = (int[]) intervals.elementAt(++i);
351 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
352 if (endshift < hr[0] || endshift < sr[0])
353 { // leadinghc disjoint or not a deletion
356 sr = (int[]) deletions.elementAt(++s);
364 boolean leadinghn = hr[0] >= sr[0];
365 boolean leadinghc = hr[0] < endshift;
366 boolean trailinghc = hr[1] < endshift;
370 { // deleted hidden region.
371 intervals.removeElementAt(i);
376 hr = (int[]) intervals.elementAt(i);
382 hr[0] = endshift; // clip c terminal region
383 leadinghn = !leadinghn;
399 // sr contained in hr
402 sr = (int[]) deletions.elementAt(++s);
412 return pruned; // true if any interval was removed or modified by
416 private boolean pruneColumnList(Vector deletion, Vector list)
418 int s = 0, t = deletion.size();
419 int[] sr = (int[]) list.elementAt(s++);
420 boolean pruned = false;
421 int i = 0, j = list.size();
422 while (i < j && s <= t)
424 int c = ((Integer) list.elementAt(i++)).intValue();
427 if (sr[1] + sr[0] >= c)
428 { // sr[1] -ve means inseriton.
429 list.removeElementAt(--i);
436 sr = (int[]) deletion.elementAt(s);
446 * remove any hiddenColumns or selected columns and shift remaining based on a
447 * series of position, range deletions.
451 public void pruneDeletions(ShiftList deletions)
453 if (deletions != null)
455 Vector shifts = deletions.shifts;
456 if (shifts != null && shifts.size() > 0)
458 // delete any intervals intersecting.
459 if (hiddenColumns != null)
461 pruneIntervalVector(shifts, hiddenColumns);
462 if (hiddenColumns != null && hiddenColumns.size() == 0)
464 hiddenColumns = null;
467 if (selected != null && selected.size() > 0)
469 pruneColumnList(shifts, selected);
470 if (selected != null && selected.size() == 0)
475 // and shift the rest.
476 this.compensateForEdits(deletions);
482 * This Method is used to return all the HiddenColumn regions less than the
489 public Vector getHiddenColumns()
491 return hiddenColumns;
495 * Return absolute column index for a visible column index
498 * int column index in alignment view
499 * @return alignment column index for column
501 public int adjustForHiddenColumns(int column)
504 if (hiddenColumns != null)
506 for (int i = 0; i < hiddenColumns.size(); i++)
508 int[] region = (int[]) hiddenColumns.elementAt(i);
509 if (result >= region[0])
511 result += region[1] - region[0] + 1;
519 * Use this method to find out where a column will appear in the visible
520 * alignment when hidden columns exist. If the column is not visible, then the
521 * left-most visible column will always be returned.
523 * @param hiddenColumn
527 public int findColumnPosition(int hiddenColumn)
529 int result = hiddenColumn;
530 if (hiddenColumns != null)
536 region = (int[]) hiddenColumns.elementAt(index++);
537 if (hiddenColumn > region[1])
539 result -= region[1] + 1 - region[0];
541 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
542 if (hiddenColumn > region[0] && hiddenColumn < region[1])
544 return region[0] + hiddenColumn - result;
547 return result; // return the shifted position after removing hidden columns.
551 * Use this method to determine where the next hiddenRegion starts
553 public int findHiddenRegionPosition(int hiddenRegion)
556 if (hiddenColumns != null)
562 int[] region = (int[]) hiddenColumns.elementAt(index);
563 if (hiddenRegion == 0)
568 gaps += region[1] + 1 - region[0];
569 result = region[1] + 1;
571 } while (index < hiddenRegion + 1);
580 * THis method returns the rightmost limit of a region of an alignment with
581 * hidden columns. In otherwords, the next hidden column.
586 public int getHiddenBoundaryRight(int alPos)
588 if (hiddenColumns != null)
593 int[] region = (int[]) hiddenColumns.elementAt(index);
594 if (alPos < region[0])
600 } while (index < hiddenColumns.size());
608 * This method returns the leftmost limit of a region of an alignment with
609 * hidden columns. In otherwords, the previous hidden column.
614 public int getHiddenBoundaryLeft(int alPos)
616 if (hiddenColumns != null)
618 int index = hiddenColumns.size() - 1;
621 int[] region = (int[]) hiddenColumns.elementAt(index);
622 if (alPos > region[1])
628 } while (index > -1);
635 public void hideSelectedColumns()
639 int column = ((Integer) getSelected().firstElement()).intValue();
645 public void hideColumns(int start, int end)
647 if (hiddenColumns == null)
649 hiddenColumns = new Vector();
652 boolean added = false;
653 boolean overlap = false;
655 for (int i = 0; i < hiddenColumns.size(); i++)
657 int[] region = (int[]) hiddenColumns.elementAt(i);
658 if (start <= region[1] && end >= region[0])
660 hiddenColumns.removeElementAt(i);
664 else if (end < region[0] && start < region[0])
666 hiddenColumns.insertElementAt(new int[]
675 hideColumns(start, end);
679 hiddenColumns.addElement(new int[]
686 * This method will find a range of selected columns around the column
692 public void hideColumns(int col)
694 // First find out range of columns to hide
695 int min = col, max = col + 1;
696 while (contains(min))
702 while (contains(max))
715 hideColumns(min, max);
718 public void revealAllHiddenColumns()
720 if (hiddenColumns != null)
722 for (int i = 0; i < hiddenColumns.size(); i++)
724 int[] region = (int[]) hiddenColumns.elementAt(i);
725 for (int j = region[0]; j < region[1] + 1; j++)
732 hiddenColumns = null;
735 public void revealHiddenColumns(int res)
737 for (int i = 0; i < hiddenColumns.size(); i++)
739 int[] region = (int[]) hiddenColumns.elementAt(i);
740 if (res == region[0])
742 for (int j = region[0]; j < region[1] + 1; j++)
747 hiddenColumns.removeElement(region);
751 if (hiddenColumns.size() == 0)
753 hiddenColumns = null;
757 public boolean isVisible(int column)
759 if (hiddenColumns != null)
761 for (int i = 0; i < hiddenColumns.size(); i++)
763 int[] region = (int[]) hiddenColumns.elementAt(i);
764 if (column >= region[0] && column <= region[1])
779 public ColumnSelection(ColumnSelection copy)
783 if (copy.selected != null)
785 selected = new Vector();
786 for (int i = 0, j = copy.selected.size(); i < j; i++)
788 selected.addElement(copy.selected.elementAt(i));
791 if (copy.hiddenColumns != null)
793 hiddenColumns = new Vector(copy.hiddenColumns.size());
794 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
797 rh = (int[]) copy.hiddenColumns.elementAt(i);
800 cp = new int[rh.length];
801 System.arraycopy(rh, 0, cp, 0, rh.length);
802 hiddenColumns.addElement(cp);
812 public ColumnSelection()
816 public String[] getVisibleSequenceStrings(int start, int end,
819 int i, iSize = seqs.length;
820 String selection[] = new String[iSize];
821 if (hiddenColumns != null && hiddenColumns.size() > 0)
823 for (i = 0; i < iSize; i++)
825 StringBuffer visibleSeq = new StringBuffer();
826 Vector regions = getHiddenColumns();
828 int blockStart = start, blockEnd = end;
830 int hideStart, hideEnd;
832 for (int j = 0; j < regions.size(); j++)
834 region = (int[]) regions.elementAt(j);
835 hideStart = region[0];
838 if (hideStart < start)
843 blockStart = Math.min(blockStart, hideEnd + 1);
844 blockEnd = Math.min(blockEnd, hideStart);
846 if (blockStart > blockEnd)
851 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
853 blockStart = hideEnd + 1;
857 if (end > blockStart)
859 visibleSeq.append(seqs[i].getSequence(blockStart, end));
862 selection[i] = visibleSeq.toString();
867 for (i = 0; i < iSize; i++)
869 selection[i] = seqs[i].getSequenceAsString(start, end);
877 * return all visible segments between the given start and end boundaries
880 * (first column inclusive from 0)
882 * (last column - not inclusive)
883 * @return int[] {i_start, i_end, ..} where intervals lie in
884 * start<=i_start<=i_end<end
886 public int[] getVisibleContigs(int start, int end)
888 if (hiddenColumns != null && hiddenColumns.size() > 0)
890 Vector visiblecontigs = new Vector();
891 Vector regions = getHiddenColumns();
895 int hideStart, hideEnd;
897 for (int j = 0; vstart < end && j < regions.size(); j++)
899 region = (int[]) regions.elementAt(j);
900 hideStart = region[0];
903 if (hideEnd < vstart)
907 if (hideStart > vstart)
909 visiblecontigs.addElement(new int[]
910 { vstart, hideStart - 1 });
912 vstart = hideEnd + 1;
917 visiblecontigs.addElement(new int[]
918 { vstart, end - 1 });
920 int[] vcontigs = new int[visiblecontigs.size() * 2];
921 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
923 int[] vc = (int[]) visiblecontigs.elementAt(i);
924 visiblecontigs.setElementAt(null, i);
925 vcontigs[i * 2] = vc[0];
926 vcontigs[i * 2 + 1] = vc[1];
928 visiblecontigs.removeAllElements();
939 * delete any columns in alignmentAnnotation that are hidden (including
940 * sequence associated annotation).
942 * @param alignmentAnnotation
944 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
946 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
950 * delete any columns in alignmentAnnotation that are hidden (including
951 * sequence associated annotation).
954 * remove any annotation to the right of this column
956 * remove any annotation to the left of this column
957 * @param alignmentAnnotation
958 * the annotation to operate on
960 public void makeVisibleAnnotation(int start, int end,
961 AlignmentAnnotation alignmentAnnotation)
963 if (alignmentAnnotation.annotations == null)
967 if (start == end && end == -1)
970 end = alignmentAnnotation.annotations.length;
972 if (hiddenColumns != null && hiddenColumns.size() > 0)
974 // then mangle the alignmentAnnotation annotation array
975 Vector annels = new Vector();
976 Annotation[] els = null;
977 Vector regions = getHiddenColumns();
978 int blockStart = start, blockEnd = end;
980 int hideStart, hideEnd, w = 0;
982 for (int j = 0; j < regions.size(); j++)
984 region = (int[]) regions.elementAt(j);
985 hideStart = region[0];
988 if (hideStart < start)
993 blockStart = Math.min(blockStart, hideEnd + 1);
994 blockEnd = Math.min(blockEnd, hideStart);
996 if (blockStart > blockEnd)
1001 annels.addElement(els = new Annotation[blockEnd - blockStart]);
1002 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1005 blockStart = hideEnd + 1;
1009 if (end > blockStart)
1011 annels.addElement(els = new Annotation[end - blockStart + 1]);
1012 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
1014 // copy just the visible segment of the annotation row
1015 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1016 els, 0, els.length);
1020 // copy to the end of the annotation row
1021 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1023 (alignmentAnnotation.annotations.length - blockStart));
1031 Enumeration e = annels.elements();
1032 alignmentAnnotation.annotations = new Annotation[w];
1034 while (e.hasMoreElements())
1036 Annotation[] chnk = (Annotation[]) e.nextElement();
1037 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1044 alignmentAnnotation.restrict(start, end);
1049 * Invert the column selection from first to end-1. leaves hiddenColumns
1050 * untouched (and unselected)
1055 public void invertColumnSelection(int first, int width)
1057 boolean hasHidden = hiddenColumns != null && hiddenColumns.size() > 0;
1058 for (int i = first; i < width; i++)
1066 if (!hasHidden || isVisible(i))
1075 * add in any unselected columns from the given column selection, excluding
1076 * any that are hidden.
1080 public void addElementsFrom(ColumnSelection colsel)
1082 if (colsel != null && colsel.size() > 0)
1084 Enumeration e = colsel.getSelected().elements();
1085 while (e.hasMoreElements())
1087 Object eo = e.nextElement();
1088 if (hiddenColumns != null && isVisible(((Integer) eo).intValue()))
1090 if (!selected.contains(eo))
1092 selected.addElement(eo);
1100 * set the selected columns the given column selection, excluding any columns
1105 public void setElementsFrom(ColumnSelection colsel)
1107 selected = new Vector();
1108 if (colsel.selected != null && colsel.selected.size() > 0)
1110 if (hiddenColumns != null && hiddenColumns.size() > 0)
1112 // only select visible columns in this columns selection
1113 selected = new Vector();
1114 addElementsFrom(colsel);
1118 // add everything regardless
1119 Enumeration en = colsel.selected.elements();
1120 while (en.hasMoreElements())
1122 selected.addElement(en.nextElement());
1129 * Add gaps into the sequences aligned to profileseq under the given
1134 * - alignment to have gaps inserted into it
1136 * - alignment view where sequence corresponding to profileseq is
1138 * @return new Column selection for new alignment view, with insertions into
1139 * profileseq marked as hidden.
1141 public static ColumnSelection propagateInsertions(SequenceI profileseq,
1142 Alignment al, AlignmentView input)
1146 // return propagateInsertions(profileseq, al, )
1147 char gc = al.getGapCharacter();
1148 Object[] alandcolsel = input.getAlignmentAndColumnSelection(gc);
1149 ColumnSelection nview = (ColumnSelection) alandcolsel[1];
1150 SequenceI origseq = ((SequenceI[]) alandcolsel[0])[profsqpos];
1151 nview.propagateInsertions(profileseq, al, origseq);
1158 * - sequence in al which corresponds to origseq
1160 * - alignment which is to have gaps inserted into it
1162 * - sequence corresponding to profileseq which defines gap map for
1165 public void propagateInsertions(SequenceI profileseq, AlignmentI al,
1168 char gc = al.getGapCharacter();
1169 // recover mapping between sequence's non-gap positions and positions
1171 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1172 int[] viscontigs = getVisibleContigs(0, profileseq.getLength());
1175 // input.pruneDeletions(ShiftList.parseMap(((SequenceI[])
1176 // alandcolsel[0])[0].gapMap()))
1177 // add profile to visible contigs
1178 for (int v = 0; v < viscontigs.length; v += 2)
1180 if (viscontigs[v] > spos)
1182 StringBuffer sb = new StringBuffer();
1183 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1187 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1189 SequenceI sqobj = al.getSequenceAt(s);
1190 if (sqobj != profileseq)
1192 String sq = al.getSequenceAt(s).getSequenceAsString();
1193 if (sq.length() <= spos + offset)
1196 int diff = spos + offset - sq.length() - 1;
1201 while ((diff = spos + offset - sq.length() - 1) > 0)
1204 // + ((diff >= sb.length()) ? sb.toString() : sb
1205 // .substring(0, diff));
1206 if (diff >= sb.length())
1208 sq += sb.toString();
1212 char[] buf = new char[diff];
1213 sb.getChars(0, diff, buf, 0);
1214 sq += buf.toString();
1218 sq += sb.toString();
1222 al.getSequenceAt(s).setSequence(
1223 sq.substring(0, spos + offset) + sb.toString()
1224 + sq.substring(spos + offset));
1228 // offset+=sb.length();
1230 spos = viscontigs[v + 1] + 1;
1232 if ((offset + spos) < profileseq.getLength())
1234 // pad the final region with gaps.
1235 StringBuffer sb = new StringBuffer();
1236 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1240 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1242 SequenceI sqobj = al.getSequenceAt(s);
1243 if (sqobj == profileseq)
1247 String sq = sqobj.getSequenceAsString();
1249 int diff = origseq.getLength() - sq.length();
1253 // + ((diff >= sb.length()) ? sb.toString() : sb
1254 // .substring(0, diff));
1255 if (diff >= sb.length())
1257 sq += sb.toString();
1261 char[] buf = new char[diff];
1262 sb.getChars(0, diff, buf, 0);
1263 sq += buf.toString();
1265 diff = origseq.getLength() - sq.length();
1271 public boolean filterAnnotations(Annotation[] annotations,
1272 AnnotationFilterParameter filterParams)
1274 this.revealAllHiddenColumns();
1279 if (annotations[count] != null)
1282 boolean itemMatched = false;
1284 if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD
1285 && annotations[count].value >= filterParams
1286 .getThresholdValue())
1290 if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD
1291 && annotations[count].value <= filterParams
1292 .getThresholdValue())
1297 if (filterParams.isFilterAlphaHelix()
1298 && annotations[count].secondaryStructure == 'H')
1303 if (filterParams.isFilterBetaSheet()
1304 && annotations[count].secondaryStructure == 'E')
1309 if (filterParams.isFilterTurn()
1310 && annotations[count].secondaryStructure == 'S')
1315 String regexSearchString = filterParams.getRegexString();
1316 if (regexSearchString != null
1317 && !filterParams.getRegexSearchFields().isEmpty())
1319 List<SearchableAnnotationField> fields = filterParams
1320 .getRegexSearchFields();
1323 if (fields.contains(SearchableAnnotationField.DISPLAY_STRING)
1324 && annotations[count].displayCharacter
1325 .matches(regexSearchString))
1329 } catch (java.util.regex.PatternSyntaxException pse)
1331 if (annotations[count].displayCharacter
1332 .equals(regexSearchString))
1337 if (fields.contains(SearchableAnnotationField.DESCRIPTION)
1338 && annotations[count].description != null
1339 && annotations[count].description
1340 .matches(regexSearchString))
1348 this.addElement(count);
1352 } while (count < annotations.length);