int j = start;
int i = 0;
- // Rely on end being at least as long as the length of the sequence.
+ int startColumn = 0;
+
+ /*
+ * traverse sequence from the start counting gaps; make a note of
+ * the column of the first residue to save in the cursor
+ */
while ((i < sequence.length) && (j <= end) && (j <= pos))
{
if (!Comparison.isGap(sequence[i]))
{
+ if (j == start)
+ {
+ startColumn = i;
+ }
j++;
}
i++;
return end + 1;
}
- updateCursor(pos, i);
+ updateCursor(pos, i, startColumn);
return i;
}
* (start..)
* @param column
* (1..)
+ * @param startColumn
+ * column position of the first sequence residue
*/
- protected void updateCursor(int residuePos, int column)
+ protected void updateCursor(int residuePos, int column, int startColumn)
{
- cursor = new SequenceCursor(this, residuePos, column, this.changeCount);
+ int endColumn = cursor == null ? 0 : cursor.lastColumnPosition;
+ if (residuePos == this.end)
+ {
+ endColumn = column;
+ }
+
+ cursor = new SequenceCursor(this, residuePos, column, startColumn,
+ endColumn, this.changeCount);
}
/**
}
col++; // convert back to base 1
- updateCursor(pos, col);
+ updateCursor(pos, col, curs.firstColumnPosition);
return col;
}
{
return findPosition(column + 1, cursor);
}
-
+
// TODO recode this more naturally i.e. count residues only
// as they are found, not 'in anticipation'
+ /*
+ * traverse the sequence counting gaps; note the column position
+ * of the first residue, to save in the cursor
+ */
+ int firstResidueColumn = 0;
int lastPosFound = 0;
int lastPosFoundColumn = 0;
int seqlen = sequence.length;
+
if (seqlen > 0 && !Comparison.isGap(sequence[0]))
{
lastPosFound = start;
{
lastPosFound = pos;
lastPosFoundColumn = j;
+ if (pos == this.start)
+ {
+ firstResidueColumn = j;
+ }
pos++;
}
j++;
{
lastPosFound = pos;
lastPosFoundColumn = j;
+ if (pos == this.start)
+ {
+ firstResidueColumn = j;
+ }
}
/*
*/
if (lastPosFound != 0)
{
- updateCursor(lastPosFound, lastPosFoundColumn + 1);
+ updateCursor(lastPosFound, lastPosFoundColumn + 1,
+ firstResidueColumn + 1);
}
return pos;
return curs.residuePosition; // easy case :-)
}
+ if (curs.lastColumnPosition > 0 && curs.lastColumnPosition <= col)
+ {
+ /*
+ * sequence lies entirely to the left of col
+ * - return last residue + 1
+ */
+ return end + 1;
+ }
+
+ if (curs.firstColumnPosition > 0 && curs.firstColumnPosition >= col)
+ {
+ /*
+ * sequence lies entirely to the right of col
+ * - return first residue
+ */
+ return start;
+ }
+
+ // todo could choose closest to col out of column,
+ // firstColumnPosition, lastColumnPosition as a start point
+
/*
* move left or right to find pos from cursor position
*/
+ int firstResidueColumn = curs.firstColumnPosition;
int column = curs.columnPosition - 1; // to base 0
int newPos = curs.residuePosition;
int delta = curs.columnPosition > col ? -1 : 1;
newPos += delta;
lastFoundPosition = newPos;
lastFoundPositionColumn = column + 1;
+ if (lastFoundPosition == this.start)
+ {
+ firstResidueColumn = column + 1;
+ }
}
}
if (cursor == null || lastFoundPosition != cursor.residuePosition)
{
- updateCursor(lastFoundPosition, lastFoundPositionColumn);
+ updateCursor(lastFoundPosition, lastFoundPositionColumn,
+ firstResidueColumn);
}
/*
{
int startPos = findPosition(fromColumn - 1); // convert base 1 to base 0
int endPos = findPosition(toColumn - 1);
-
+ // to trace / debug behaviour:
+ // System.out
+ // .println(String
+ // .format("%s.findFeatures columns [%d-%d] positions [%d-%d] leaves cursor %s",
+ // getName(), fromColumn, toColumn, startPos,
+ // endPos, cursor));
List<SequenceFeature> result = new ArrayList<>();
if (datasetSequence != null)
{
SequenceI sq = new Sequence("test/8-13", "ABCDEF");
assertEquals(8, sq.findPosition(0));
// Sequence should now hold a cursor at [8, 0]
+ assertEquals("test:Pos8:Col1:startCol1:endCol0:tok0",
+ PA.getValue(sq, "cursor").toString());
SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
int token = (int) PA.getValue(sq, "changeCount");
assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
/*
* find F13 at column offset 5, cursor should update to [13, 6]
+ * endColumn is found and saved in cursor
*/
assertEquals(13, sq.findPosition(5));
cursor = (SequenceCursor) PA.getValue(sq, "cursor");
assertEquals(++token, (int) PA.getValue(sq, "changeCount"));
assertEquals(new SequenceCursor(sq, 13, 6, token), cursor);
+ assertEquals("test:Pos13:Col6:startCol1:endCol6:tok1",
+ PA.getValue(sq, "cursor").toString());
// assertEquals(-1, seq.findPosition(6)); // fails
assertEquals(8, sq.findPosition(0));
cursor = (SequenceCursor) PA.getValue(sq, "cursor");
assertEquals(new SequenceCursor(sq, 8, 1, token), cursor);
+ assertEquals("test:Pos8:Col1:startCol1:endCol0:tok0",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
assertEquals(9, sq.findPosition(1));
cursor = (SequenceCursor) PA.getValue(sq, "cursor");
assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
+ assertEquals("test:Pos9:Col2:startCol1:endCol0:tok1",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
// gap position 'finds' residue to the right (not the left as per javadoc)
assertEquals(10, sq.findPosition(2));
cursor = (SequenceCursor) PA.getValue(sq, "cursor");
assertEquals(new SequenceCursor(sq, 9, 2, ++token), cursor);
+ assertEquals("test:Pos9:Col2:startCol1:endCol0:tok2",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
assertEquals(10, sq.findPosition(3));
cursor = (SequenceCursor) PA.getValue(sq, "cursor");
assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
+ assertEquals("test:Pos10:Col4:startCol1:endCol0:tok3",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
// column[4] is the gap after C - returns D11
assertEquals(11, sq.findPosition(4));
cursor = (SequenceCursor) PA.getValue(sq, "cursor");
assertEquals(new SequenceCursor(sq, 10, 4, ++token), cursor);
+ assertEquals("test:Pos10:Col4:startCol1:endCol0:tok4",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
assertEquals(11, sq.findPosition(5)); // D
cursor = (SequenceCursor) PA.getValue(sq, "cursor");
assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
+ // lastCol has been found and saved in the cursor
+ assertEquals("test:Pos11:Col6:startCol1:endCol6:tok5",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
// returns 1 more than sequence length if off the end ?!?
sq.sequenceChanged();
assertEquals(12, sq.findPosition(7));
+ /*
+ * first findPosition should also set firstResCol in cursor
+ */
sq = new Sequence("test/8-13", "--AB-C-DEF--");
assertEquals(8, sq.findPosition(0));
+ assertNull(PA.getValue(sq, "cursor"));
sq.sequenceChanged();
assertEquals(8, sq.findPosition(1));
+ assertNull(PA.getValue(sq, "cursor"));
sq.sequenceChanged();
assertEquals(8, sq.findPosition(2));
+ assertEquals("test:Pos8:Col3:startCol3:endCol0:tok2",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
assertEquals(9, sq.findPosition(3));
+ assertEquals("test:Pos9:Col4:startCol3:endCol0:tok3",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
+ // column[4] is a gap, returns next residue pos (C10)
+ // cursor is set to last residue found [B]
assertEquals(10, sq.findPosition(4));
+ assertEquals("test:Pos9:Col4:startCol3:endCol0:tok4",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
assertEquals(10, sq.findPosition(5));
+ assertEquals("test:Pos10:Col6:startCol3:endCol0:tok5",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
+ // column[6] is a gap, returns next residue pos (D11)
+ // cursor is set to last residue found [C]
assertEquals(11, sq.findPosition(6));
+ assertEquals("test:Pos10:Col6:startCol3:endCol0:tok6",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
assertEquals(11, sq.findPosition(7));
+ assertEquals("test:Pos11:Col8:startCol3:endCol0:tok7",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
assertEquals(12, sq.findPosition(8));
+ assertEquals("test:Pos12:Col9:startCol3:endCol0:tok8",
+ PA.getValue(sq, "cursor").toString());
+ /*
+ * when the last residue column is found, it is set in the cursor
+ */
sq.sequenceChanged();
assertEquals(13, sq.findPosition(9));
+ assertEquals("test:Pos13:Col10:startCol3:endCol10:tok9",
+ PA.getValue(sq, "cursor").toString());
sq.sequenceChanged();
assertEquals(14, sq.findPosition(10));
+ assertEquals("test:Pos13:Col10:startCol3:endCol10:tok10",
+ PA.getValue(sq, "cursor").toString());
/*
* findPosition for column beyond sequence length
*/
sq.sequenceChanged();
assertEquals(14, sq.findPosition(11));
+ assertEquals("test:Pos13:Col10:startCol3:endCol10:tok11",
+ PA.getValue(sq, "cursor").toString());
+
sq.sequenceChanged();
assertEquals(14, sq.findPosition(99));
+ assertEquals("test:Pos13:Col10:startCol3:endCol10:tok12",
+ PA.getValue(sq, "cursor").toString());
}
@Test(groups = { "Functional" })
{
Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
- // find F pos given A
+ // find F pos given A - lastCol gets set in cursor
assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
- int token = (int) PA.getValue(sq, "changeCount"); // 0
- SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
- assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
+ assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
+ PA.getValue(sq, "cursor").toString());
- // find A pos given F
+ // find A pos given F - first residue column is saved in cursor
assertEquals(8, sq.findPosition(2, new SequenceCursor(sq, 13, 10, 0)));
- cursor = (SequenceCursor) PA.getValue(sq, "cursor");
- assertEquals(new SequenceCursor(sq, 8, 2, token), cursor);
+ assertEquals("test:Pos8:Col2:startCol2:endCol10:tok0",
+ PA.getValue(sq, "cursor").toString());
- // find C pos given C
+ // find C pos given C (neither startCol nor endCol is set)
assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 10, 6, 0)));
- cursor = (SequenceCursor) PA.getValue(sq, "cursor");
- assertEquals(new SequenceCursor(sq, 10, 6, token), cursor);
+ assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
+ PA.getValue(sq, "cursor").toString());
// now the grey area - what residue position for a gapped column? JAL-2562
// find 'residue' for column 3 given cursor for D (so working left)
// returns B9; cursor is updated to [B 5]
assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, 0)));
- cursor = (SequenceCursor) PA.getValue(sq, "cursor");
- assertEquals(new SequenceCursor(sq, 9, 5, token), cursor);
+ assertEquals("test:Pos9:Col5:startCol0:endCol0:tok0",
+ PA.getValue(sq, "cursor").toString());
// find 'residue' for column 8 given cursor for D (so working right)
// returns E12; cursor is updated to [D 7]
assertEquals(12, sq.findPosition(8, new SequenceCursor(sq, 11, 7, 0)));
- cursor = (SequenceCursor) PA.getValue(sq, "cursor");
- assertEquals(new SequenceCursor(sq, 11, 7, token), cursor);
+ assertEquals("test:Pos11:Col7:startCol0:endCol0:tok0",
+ PA.getValue(sq, "cursor").toString());
// find 'residue' for column 12 given cursor for B
// returns 1 more than last residue position; cursor is updated to [F 10]
+ // lastCol position is saved in cursor
assertEquals(14, sq.findPosition(12, new SequenceCursor(sq, 9, 5, 0)));
- cursor = (SequenceCursor) PA.getValue(sq, "cursor");
- assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
+ assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
+ PA.getValue(sq, "cursor").toString());
/*
* findPosition for column beyond length of sequence
* cursor is set to last real residue position [F 10]
*/
assertEquals(14, sq.findPosition(99, new SequenceCursor(sq, 8, 2, 0)));
- cursor = (SequenceCursor) PA.getValue(sq, "cursor");
- assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
+ assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
+ PA.getValue(sq, "cursor").toString());
/*
* and the case without a trailing gap
sq = new Sequence("test/8-13", "-A--BCD-EF");
// first find C from A
assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, 0)));
- cursor = (SequenceCursor) PA.getValue(sq, "cursor");
- assertEquals(new SequenceCursor(sq, 10, 6, token), cursor);
+ SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
+ cursor.toString());
// now 'find' 99 from C
- // cursor is set to [F 10]
+ // cursor is set to [F 10] and saved lastCol
assertEquals(14, sq.findPosition(99, cursor));
- cursor = (SequenceCursor) PA.getValue(sq, "cursor");
- assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
+ assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
+ PA.getValue(sq, "cursor").toString());
}
@Test
assertTrue(sq.isValidCursor(cursor));
/*
- * column position outside [0 - length-1] is rejected
+ * column position outside [0 - length] is rejected
*/
cursor = new SequenceCursor(sq, 13, -1, changeCount);
assertFalse(sq.isValidCursor(cursor));
- cursor = new SequenceCursor(sq, 13, 9, changeCount);
+ cursor = new SequenceCursor(sq, 13, 10, changeCount);
assertFalse(sq.isValidCursor(cursor));
cursor = new SequenceCursor(sq, 7, 8, changeCount);
assertFalse(sq.isValidCursor(cursor));