import java.util.ListIterator;
import java.util.Vector;
-import com.stevesoft.pat.Regex;
-
import fr.orsay.lri.varna.models.rna.RNA;
/**
*/
public class Sequence extends ASequence implements SequenceI
{
- private static final Regex limitrx = new Regex(
- "[/][0-9]{1,}[-][0-9]{1,}$");
-
- private static final Regex endrx = new Regex("[0-9]{1,}$");
-
SequenceI datasetSequence;
String name;
*/
Vector<AlignmentAnnotation> annotation;
- /**
- * The index of the sequence in a MSA
- */
- int index = -1;
-
private SequenceFeaturesI sequenceFeatureStore;
/*
checkValidRange();
}
+ /**
+ * If 'name' ends in /i-j, where i >= j > 0 are integers, extracts i and j as
+ * start and end respectively and removes the suffix from the name
+ */
void parseId()
{
if (name == null)
"POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
name = "";
}
- // Does sequence have the /start-end signature?
- if (limitrx.search(name))
+ int slashPos = name.lastIndexOf('/');
+ if (slashPos > -1 && slashPos < name.length() - 1)
{
- name = limitrx.left();
- endrx.search(limitrx.stringMatched());
- setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
- endrx.matchedFrom() - 1)));
- setEnd(Integer.parseInt(endrx.stringMatched()));
+ String suffix = name.substring(slashPos + 1);
+ String[] range = suffix.split("-");
+ if (range.length == 2)
+ {
+ try
+ {
+ int from = Integer.valueOf(range[0]);
+ int to = Integer.valueOf(range[1]);
+ if (from > 0 && to >= from)
+ {
+ name = name.substring(0, slashPos);
+ setStart(from);
+ setEnd(to);
+ checkValidRange();
+ }
+ } catch (NumberFormatException e)
+ {
+ // leave name unchanged if suffix is invalid
+ }
+ }
}
}
+ /**
+ * Ensures that 'end' is not before the end of the sequence, that is,
+ * (end-start+1) is at least as long as the count of ungapped positions. Note
+ * that end is permitted to be beyond the end of the sequence data.
+ */
void checkValidRange()
{
// Note: JAL-774 :
int endRes = 0;
for (int j = 0; j < sequence.length; j++)
{
- if (!jalview.util.Comparison.isGap(sequence[j]))
+ if (!Comparison.isGap(sequence[j]))
{
endRes++;
}
}
/**
- * DOCUMENT ME!
+ * Sets the sequence name. If the name ends in /start-end, then the start-end
+ * values are parsed out and set, and the suffix is removed from the name.
*
- * @param name
- * DOCUMENT ME!
+ * @param theName
*/
@Override
- public void setName(String name)
+ public void setName(String theName)
{
- this.name = name;
+ this.name = theName;
this.parseId();
}
}
@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)
+ {
+ startDeleteColumn = findPosition(s) - start;
+ }
+ if (createNewDs)
{
- 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;
}
}
- /**
- * @return The index (zero-based) on this sequence in the MSA. It returns
- * {@code -1} if this information is not available.
- */
- @Override
- public int getIndex()
- {
- return index;
- }
-
- /**
- * Defines the position of this sequence in the MSA. Use the value {@code -1}
- * if this information is undefined.
- *
- * @param The
- * position for this sequence. This value is zero-based (zero for
- * this first sequence)
- */
- @Override
- public void setIndex(int value)
- {
- index = value;
- }
-
@Override
public void setRNA(RNA r)
{
* and we may have included adjacent or enclosing features;
* remove any that are not enclosing, non-contact features
*/
- if (endPos > this.end || Comparison.isGap(sequence[toColumn - 1]))
+ 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())