+ int startPos = findPosition(fromColumn - 1); // convert base 1 to base 0
+ int endPos = fromColumn == toColumn ? startPos
+ : findPosition(toColumn - 1);
+
+ List<SequenceFeature> result = getFeatures().findFeatures(startPos,
+ endPos, types);
+
+ /*
+ * if end column is gapped, endPos may be to the right,
+ * and we may have included adjacent or enclosing features;
+ * remove any that are not enclosing, non-contact features
+ */
+ boolean endColumnIsGapped = toColumn > 0 && toColumn <= sequence.length
+ && Comparison.isGap(sequence[toColumn - 1]);
+ if (endPos > this.end || endColumnIsGapped)
+ {
+ ListIterator<SequenceFeature> it = result.listIterator();
+ while (it.hasNext())
+ {
+ SequenceFeature sf = it.next();
+ int sfBegin = sf.getBegin();
+ int sfEnd = sf.getEnd();
+ int featureStartColumn = findIndex(sfBegin);
+ if (featureStartColumn > toColumn)
+ {
+ it.remove();
+ }
+ else if (featureStartColumn < fromColumn)
+ {
+ int featureEndColumn = sfEnd == sfBegin ? featureStartColumn
+ : findIndex(sfEnd);
+ if (featureEndColumn < fromColumn)
+ {
+ it.remove();
+ }
+ else if (featureEndColumn > toColumn && sf.isContactFeature())
+ {
+ /*
+ * remove an enclosing feature if it is a contact feature
+ */
+ it.remove();
+ }
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Invalidates any stale cursors (forcing recalculation) by incrementing the
+ * token that has to match the one presented by the cursor
+ */
+ @Override
+ public void sequenceChanged()
+ {
+ changeCount++;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int replace(char c1, char c2)
+ {
+ if (c1 == c2)
+ {
+ return 0;
+ }
+ int count = 0;
+ synchronized (sequence)
+ {
+ for (int c = 0; c < sequence.length; c++)
+ {
+ if (sequence[c] == c1)
+ {
+ sequence[c] = c2;
+ count++;
+ }
+ }
+ }
+ if (count > 0)
+ {
+ sequenceChanged();
+ }
+
+ return count;
+ }
+
+ @Override
+ public String getSequenceStringFromIterator(Iterator<int[]> it)
+ {
+ StringBuilder newSequence = new StringBuilder();
+ while (it.hasNext())
+ {
+ int[] block = it.next();
+ if (it.hasNext())
+ {
+ newSequence.append(getSequence(block[0], block[1] + 1));
+ }
+ else
+ {
+ newSequence.append(getSequence(block[0], block[1]));
+ }
+ }
+
+ return newSequence.toString();
+ }
+
+ @Override
+ public int firstResidueOutsideIterator(Iterator<int[]> regions)
+ {
+ int start = 0;
+
+ if (!regions.hasNext())
+ {
+ return findIndex(getStart()) - 1;
+ }
+
+ // Simply walk along the sequence whilst watching for region
+ // boundaries
+ int hideStart = getLength();
+ int hideEnd = -1;
+ boolean foundStart = false;
+
+ // step through the non-gapped positions of the sequence
+ for (int i = getStart(); i <= getEnd() && (!foundStart); i++)
+ {
+ // get alignment position of this residue in the sequence
+ int p = findIndex(i) - 1;
+
+ // update region start/end
+ while (hideEnd < p && regions.hasNext())
+ {
+ int[] region = regions.next();
+ hideStart = region[0];
+ hideEnd = region[1];
+ }
+ if (hideEnd < p)
+ {
+ hideStart = getLength();
+ }
+ // update boundary for sequence
+ if (p < hideStart)
+ {
+ start = p;
+ foundStart = true;
+ }
+ }
+
+ if (foundStart)
+ {
+ return start;
+ }
+ // otherwise, sequence was completely hidden
+ return 0;
+ }
+
+ ////
+ //// Contact Matrix Holder Boilerplate
+ ////
+ ContactMapHolderI _cmholder = null;
+
+ private ContactMapHolderI getContactMapHolder()
+ {
+ if (datasetSequence!=null) {
+ return ((Sequence)datasetSequence).getContactMapHolder();
+ }
+ if (_cmholder==null)
+ {
+ _cmholder=new ContactMapHolder();
+ }
+ return _cmholder;
+ }
+ @Override
+ public Collection<ContactMatrixI> getContactMaps()
+ {
+ return getContactMapHolder().getContactMaps();
+ }
+
+ @Override
+ public ContactMatrixI getContactMatrixFor(AlignmentAnnotation ann)
+ {
+ return getContactMapHolder().getContactMatrixFor(ann);
+ }
+
+ @Override
+ public ContactListI getContactListFor(AlignmentAnnotation _aa, int column)
+ {
+ return getContactMapHolder().getContactListFor(_aa, column);
+ }
+
+ @Override
+ public AlignmentAnnotation addContactList(ContactMatrixI cm)
+ {
+ AlignmentAnnotation aa;
+