}
@Override
- public void deleteChars(int i, int j)
+ public void deleteChars(final int i, final int j)
{
int newstart = start, newend = end;
if (i >= sequence.length || i < 0)
boolean createNewDs = false;
// TODO: take a (second look) at the dataset creation validation method for
// the very large sequence case
- int eindex = -1, sindex = -1;
- boolean ecalc = false, scalc = false;
+ int startIndex = findIndex(start) - 1;
+ int endIndex = findIndex(end) - 1;
+ int startDeleteColumn = -1; // for dataset sequence deletions
+ int deleteCount = 0;
+
for (int s = i; s < j; s++)
{
- if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
+ if (Comparison.isGap(sequence[s]))
+ {
+ continue;
+ }
+ deleteCount++;
+ if (startDeleteColumn == -1)
{
- if (createNewDs)
+ startDeleteColumn = findPosition(s) - start;
+ }
+ if (createNewDs)
+ {
+ newend--;
+ }
+ else
+ {
+ if (startIndex == s)
{
- newend--;
+ /*
+ * deleting characters from start of sequence; new start is the
+ * sequence position of the next column (position to the right
+ * if the column position is gapped)
+ */
+ newstart = findPosition(j);
+ break;
}
else
{
- if (!scalc)
- {
- sindex = findIndex(start) - 1;
- scalc = true;
- }
- if (sindex == s)
+ if (endIndex < j)
{
- // delete characters including start of sequence
- newstart = findPosition(j);
- break; // don't need to search for any more residue characters.
+ /*
+ * deleting characters at end of sequence; new end is the sequence
+ * position of the column before the deletion; subtract 1 if this is
+ * gapped since findPosition returns the next sequence position
+ */
+ newend = findPosition(i - 1);
+ if (Comparison.isGap(sequence[i - 1]))
+ {
+ newend--;
+ }
+ break;
}
else
{
- // delete characters after start.
- if (!ecalc)
- {
- eindex = findIndex(end) - 1;
- ecalc = true;
- }
- if (eindex < j)
- {
- // delete characters at end of sequence
- newend = findPosition(i - 1);
- break; // don't need to search for any more residue characters.
- }
- else
- {
- createNewDs = true;
- newend--; // decrease end position by one for the deleted residue
- // and search further
- }
+ createNewDs = true;
+ newend--;
}
}
}
}
- // deletion occured in the middle of the sequence
+
if (createNewDs && this.datasetSequence != null)
{
- // construct a new sequence
+ /*
+ * if deletion occured in the middle of the sequence,
+ * construct a new dataset sequence and delete the residues
+ * that were deleted from the aligned sequence
+ */
Sequence ds = new Sequence(datasetSequence);
+ ds.deleteChars(startDeleteColumn, startDeleteColumn + deleteCount);
+ datasetSequence = ds;
// TODO: remove any non-inheritable properties ?
// TODO: create a sequence mapping (since there is a relation here ?)
- ds.deleteChars(i, j);
- datasetSequence = ds;
}
start = newstart;
end = newend;
/*
* delete gaps and residues internally (new dataset sequence)
+ * first delete from gap to residue
*/
sq = new Sequence("test/8-10", "A-B-C");
sq.createDatasetSequence();
assertEquals("A-C", sq.getSequenceAsString());
assertEquals(8, sq.getStart());
assertEquals(9, sq.getEnd());
- assertEquals("ACDEF", sq.getDatasetSequence().getSequenceAsString());
+ assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
assertEquals(8, sq.getDatasetSequence().getStart());
+ assertEquals(9, sq.getDatasetSequence().getEnd());
+
+ /*
+ * internal delete from gap to gap
+ */
+ sq = new Sequence("test/8-10", "A-B-C");
+ sq.createDatasetSequence();
+ sq.deleteChars(1, 4); // delete -B-
+ assertEquals("AC", sq.getSequenceAsString());
+ assertEquals(8, sq.getStart());
+ assertEquals(9, sq.getEnd());
+ assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
+ assertEquals(8, sq.getDatasetSequence().getStart());
+ assertEquals(9, sq.getDatasetSequence().getEnd());
+
+ /*
+ * internal delete from residue to residue
+ */
+ sq = new Sequence("test/8-10", "A-B-C");
+ sq.createDatasetSequence();
+ sq.deleteChars(2, 3); // delete B
+ assertEquals("A--C", sq.getSequenceAsString());
+ assertEquals(8, sq.getStart());
+ assertEquals(9, sq.getEnd());
+ assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
+ assertEquals(8, sq.getDatasetSequence().getStart());
+ assertEquals(9, sq.getDatasetSequence().getEnd());
}
}