/*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
*
* This file is part of Jalview.
*
import jalview.datamodel.Sequence;
import jalview.datamodel.SequenceFeature;
import jalview.datamodel.SequenceI;
+import jalview.util.ReverseListIterator;
+import jalview.util.StringUtils;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Hashtable;
+import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
+import java.util.Map;
/**
*
{
public enum Action
{
- INSERT_GAP, DELETE_GAP, CUT, PASTE, REPLACE, INSERT_NUC
+ INSERT_GAP
+ {
+ @Override
+ public Action getUndoAction()
+ {
+ return DELETE_GAP;
+ }
+ },
+ DELETE_GAP
+ {
+ @Override
+ public Action getUndoAction()
+ {
+ return INSERT_GAP;
+ }
+ },
+ CUT
+ {
+ @Override
+ public Action getUndoAction()
+ {
+ return PASTE;
+ }
+ },
+ PASTE
+ {
+ @Override
+ public Action getUndoAction()
+ {
+ return CUT;
+ }
+ },
+ REPLACE
+ {
+ @Override
+ public Action getUndoAction()
+ {
+ return REPLACE;
+ }
+ },
+ INSERT_NUC
+ {
+ @Override
+ public Action getUndoAction()
+ {
+ return null;
+ }
+ };
+ public abstract Action getUndoAction();
};
private List<Edit> edits = new ArrayList<Edit>();
{
}
- public EditCommand(String description)
+ public EditCommand(String desc)
{
- this.description = description;
+ this.description = desc;
}
- public EditCommand(String description, Action command, SequenceI[] seqs,
+ public EditCommand(String desc, Action command, SequenceI[] seqs,
int position, int number, AlignmentI al)
{
- this.description = description;
+ this.description = desc;
if (command == Action.CUT || command == Action.PASTE)
{
setEdit(new Edit(command, seqs, position, number, al));
performEdit(0, null);
}
- public EditCommand(String description, Action command, String replace,
+ public EditCommand(String desc, Action command, String replace,
SequenceI[] seqs, int position, int number, AlignmentI al)
{
- this.description = description;
+ this.description = desc;
if (command == Action.REPLACE)
{
setEdit(new Edit(command, seqs, position, number, al, replace));
}
/**
- * Add the given edit command to the stored list of commands.
+ * Add the given edit command to the stored list of commands. If simply
+ * expanding the range of the last command added, then modify it instead of
+ * adding a new command.
*
* @param e
*/
- protected void addEdit(Edit e)
+ public void addEdit(Edit e)
{
- edits.add(e);
+ if (!expandEdit(edits, e))
+ {
+ edits.add(e);
+ }
+ }
+
+ /**
+ * Returns true if the new edit is incorporated by updating (expanding the
+ * range of) the last edit on the list, else false. We can 'expand' the last
+ * edit if the new one is the same action, on the same sequences, and acts on
+ * a contiguous range. This is the case where a mouse drag generates a series
+ * of contiguous gap insertions or deletions.
+ *
+ * @param edits
+ * @param e
+ * @return
+ */
+ protected static boolean expandEdit(List<Edit> edits, Edit e)
+ {
+ if (edits == null || edits.isEmpty())
+ {
+ return false;
+ }
+ Edit lastEdit = edits.get(edits.size() - 1);
+ Action action = e.command;
+ if (lastEdit.command != action)
+ {
+ return false;
+ }
+
+ /*
+ * Both commands must act on the same sequences - compare the underlying
+ * dataset sequences, rather than the aligned sequences, which change as
+ * they are edited.
+ */
+ if (lastEdit.seqs.length != e.seqs.length)
+ {
+ return false;
+ }
+ for (int i = 0; i < e.seqs.length; i++)
+ {
+ if (lastEdit.seqs[i].getDatasetSequence() != e.seqs[i]
+ .getDatasetSequence())
+ {
+ return false;
+ }
+ }
+
+ /**
+ * Check a contiguous edit; either
+ * <ul>
+ * <li>a new Insert <n> positions to the right of the last <insert n>, or</li>
+ * <li>a new Delete <n> gaps which is <n> positions to the left of the last
+ * delete.</li>
+ * </ul>
+ */
+ boolean contiguous = (action == Action.INSERT_GAP && e.position == lastEdit.position
+ + lastEdit.number)
+ || (action == Action.DELETE_GAP && e.position + e.number == lastEdit.position);
+ if (contiguous)
+ {
+ /*
+ * We are just expanding the range of the last edit. For delete gap, also
+ * moving the start position left.
+ */
+ lastEdit.number += e.number;
+ lastEdit.seqs = e.seqs;
+ if (action == Action.DELETE_GAP)
+ {
+ lastEdit.position--;
+ }
+ return true;
+ }
+ return false;
}
/**
* @param performEdit
*/
final public void appendEdit(Action command, SequenceI[] seqs,
- int position,
- int number, AlignmentI al, boolean performEdit)
+ int position, int number, AlignmentI al, boolean performEdit)
{
appendEdit(command, seqs, position, number, al, performEdit, null);
}
* @param views
*/
final public void appendEdit(Action command, SequenceI[] seqs,
- int position,
- int number, AlignmentI al, boolean performEdit, AlignmentI[] views)
+ int position, int number, AlignmentI al, boolean performEdit,
+ AlignmentI[] views)
{
Edit edit = new Edit(command, seqs, position, number,
al.getGapCharacter());
edit.fullAlignmentHeight = true;
}
- edits.add(edit);
+ addEdit(edit);
+
+ if (performEdit)
+ {
+ performEdit(edit, views);
+ }
+ }
+
+ /**
+ * Overloaded method that accepts an Edit object with additional parameters.
+ *
+ * @param edit
+ * @param al
+ * @param performEdit
+ * @param views
+ */
+ final public void appendEdit(Edit edit, AlignmentI al,
+ boolean performEdit, AlignmentI[] views)
+ {
+ if (al.getHeight() == edit.seqs.length)
+ {
+ edit.al = al;
+ edit.fullAlignmentHeight = true;
+ }
+
+ addEdit(edit);
if (performEdit)
{
* @param commandIndex
* @param views
*/
- final void performEdit(int commandIndex, AlignmentI[] views)
+ public final void performEdit(int commandIndex, AlignmentI[] views)
{
ListIterator<Edit> iterator = edits.listIterator(commandIndex);
while (iterator.hasNext())
* @param edit
* @param views
*/
- protected void performEdit(Edit edit, AlignmentI[] views)
+ protected static void performEdit(Edit edit, AlignmentI[] views)
{
switch (edit.command)
{
*/
@Override
final public void undoCommand(AlignmentI[] views)
- {
+ {
ListIterator<Edit> iterator = edits.listIterator(edits.size());
while (iterator.hasPrevious())
{
*
* @param command
*/
- final private void insertGap(Edit command)
+ final private static void insertGap(Edit command)
{
for (int s = 0; s < command.seqs.length; s++)
*
* @param command
*/
- final private void deleteGap(Edit command)
+ final static private void deleteGap(Edit command)
{
for (int s = 0; s < command.seqs.length; s++)
{
* @param command
* @param views
*/
- void cut(Edit command, AlignmentI[] views)
+ static void cut(Edit command, AlignmentI[] views)
{
boolean seqDeleted = false;
command.string = new char[command.seqs.length][];
{
// modify the oldds if necessary
if (oldds != sequence.getDatasetSequence()
- || sequence.getSequenceFeatures() != null)
+ || sequence.getFeatures().hasFeatures())
{
if (command.oldds == null)
{
command.oldds = new SequenceI[command.seqs.length];
}
command.oldds[i] = oldds;
+ // FIXME JAL-2541 JAL-2526 get correct positions if on a gap
adjustFeatures(
command,
i,
sequence.findPosition(command.position),
- sequence.findPosition(command.position
- + command.number), false);
+ sequence.findPosition(command.position + command.number),
+ false);
}
}
}
* @param command
* @param views
*/
- void paste(Edit command, AlignmentI[] views)
+ static void paste(Edit command, AlignmentI[] views)
{
StringBuffer tmp;
boolean newDSNeeded;
if (command.seqs[i].getLength() < 1)
{
// ie this sequence was deleted, we need to
- // read it to the alignment
+ // readd it to the alignment
if (command.alIndex[i] < command.al.getHeight())
{
List<SequenceI> sequences;
command.string = null;
}
- void replace(Edit command)
+ static void replace(Edit command)
{
StringBuffer tmp;
String oldstring;
}
}
- final void adjustAnnotations(Edit command, boolean insert,
+ final static void adjustAnnotations(Edit command, boolean insert,
boolean modifyVisibility, AlignmentI[] views)
{
AlignmentAnnotation[] annotations = null;
AlignmentAnnotation[] tmp;
for (int s = 0; s < command.seqs.length; s++)
{
+ command.seqs[s].sequenceChanged();
+
if (modifyVisibility)
{
// Rows are only removed or added to sequence object.
int copylen = Math.min(command.position,
annotations[a].annotations.length);
if (copylen > 0)
- {
+ {
System.arraycopy(annotations[a].annotations, 0, temp, 0,
copylen); // command.position);
}
}
}
- final void adjustFeatures(Edit command, int index, int i, int j,
- boolean insert)
+ final static void adjustFeatures(Edit command, int index, final int i,
+ final int j, boolean insert)
{
SequenceI seq = command.seqs[index];
SequenceI sequence = seq.getDatasetSequence();
if (command.editedFeatures != null
&& command.editedFeatures.containsKey(seq))
{
- sequence.setSequenceFeatures(command.editedFeatures
- .get(seq));
+ sequence.setSequenceFeatures(command.editedFeatures.get(seq));
}
return;
}
- SequenceFeature[] sf = sequence.getSequenceFeatures();
+ List<SequenceFeature> sf = sequence.getFeatures()
+ .getPositionalFeatures();
- if (sf == null)
+ if (sf.isEmpty())
{
return;
}
- SequenceFeature[] oldsf = new SequenceFeature[sf.length];
+ List<SequenceFeature> oldsf = new ArrayList<SequenceFeature>();
int cSize = j - i;
- for (int s = 0; s < sf.length; s++)
+ for (SequenceFeature feature : sf)
{
- SequenceFeature copy = new SequenceFeature(sf[s]);
+ SequenceFeature copy = new SequenceFeature(feature);
- oldsf[s] = copy;
+ oldsf.add(copy);
- if (sf[s].getEnd() < i)
+ if (feature.getEnd() < i)
{
continue;
}
- if (sf[s].getBegin() > j)
+ if (feature.getBegin() > j)
{
- sf[s].setBegin(copy.getBegin() - cSize);
- sf[s].setEnd(copy.getEnd() - cSize);
+ int newBegin = copy.getBegin() - cSize;
+ int newEnd = copy.getEnd() - cSize;
+ SequenceFeature newSf = new SequenceFeature(feature, newBegin,
+ newEnd, feature.getFeatureGroup(), feature.getScore());
+ sequence.deleteFeature(feature);
+ sequence.addSequenceFeature(newSf);
+ // feature.setBegin(newBegin);
+ // feature.setEnd(newEnd);
continue;
}
- if (sf[s].getBegin() >= i)
+ int newBegin = feature.getBegin();
+ int newEnd = feature.getEnd();
+ if (newBegin >= i)
{
- sf[s].setBegin(i);
+ newBegin = i;
+ // feature.setBegin(i);
}
- if (sf[s].getEnd() < j)
+ if (newEnd < j)
{
- sf[s].setEnd(j - 1);
+ newEnd = j - 1;
+ // feature.setEnd(j - 1);
}
+ newEnd = newEnd - cSize;
+ // feature.setEnd(feature.getEnd() - (cSize));
- sf[s].setEnd(sf[s].getEnd() - (cSize));
-
- if (sf[s].getBegin() > sf[s].getEnd())
+ sequence.deleteFeature(feature);
+ if (newEnd >= newBegin)
{
- sequence.deleteFeature(sf[s]);
+ sequence.addSequenceFeature(new SequenceFeature(feature, newBegin,
+ newEnd, feature.getFeatureGroup(), feature.getScore()));
}
+ // if (feature.getBegin() > feature.getEnd())
+ // {
+ // sequence.deleteFeature(feature);
+ // }
}
if (command.editedFeatures == null)
{
- command.editedFeatures = new Hashtable<SequenceI, SequenceFeature[]>();
+ command.editedFeatures = new Hashtable<SequenceI, List<SequenceFeature>>();
}
command.editedFeatures.put(seq, oldsf);
}
- class Edit
+ /**
+ * Returns the list of edit commands wrapped by this object.
+ *
+ * @return
+ */
+ public List<Edit> getEdits()
+ {
+ return this.edits;
+ }
+
+ /**
+ * Returns a map whose keys are the dataset sequences, and values their
+ * aligned sequences before the command edit list was applied. The aligned
+ * sequences are copies, which may be updated without affecting the originals.
+ *
+ * The command holds references to the aligned sequences (after editing). If
+ * the command is an 'undo',then the prior state is simply the aligned state.
+ * Otherwise, we have to derive the prior state by working backwards through
+ * the edit list to infer the aligned sequences before editing.
+ *
+ * Note: an alternative solution would be to cache the 'before' state of each
+ * edit, but this would be expensive in space in the common case that the
+ * original is never needed (edits are not mirrored).
+ *
+ * @return
+ * @throws IllegalStateException
+ * on detecting an edit command of a type that can't be unwound
+ */
+ public Map<SequenceI, SequenceI> priorState(boolean forUndo)
+ {
+ Map<SequenceI, SequenceI> result = new HashMap<SequenceI, SequenceI>();
+ if (getEdits() == null)
+ {
+ return result;
+ }
+ if (forUndo)
+ {
+ for (Edit e : getEdits())
+ {
+ for (SequenceI seq : e.getSequences())
+ {
+ SequenceI ds = seq.getDatasetSequence();
+ // SequenceI preEdit = result.get(ds);
+ if (!result.containsKey(ds))
+ {
+ /*
+ * copy sequence including start/end (but don't use copy constructor
+ * as we don't need annotations)
+ */
+ SequenceI preEdit = new Sequence("", seq.getSequenceAsString(),
+ seq.getStart(), seq.getEnd());
+ preEdit.setDatasetSequence(ds);
+ result.put(ds, preEdit);
+ }
+ }
+ }
+ return result;
+ }
+
+ /*
+ * Work backwards through the edit list, deriving the sequences before each
+ * was applied. The final result is the sequence set before any edits.
+ */
+ Iterator<Edit> editList = new ReverseListIterator<Edit>(getEdits());
+ while (editList.hasNext())
+ {
+ Edit oldEdit = editList.next();
+ Action action = oldEdit.getAction();
+ int position = oldEdit.getPosition();
+ int number = oldEdit.getNumber();
+ final char gap = oldEdit.getGapCharacter();
+ for (SequenceI seq : oldEdit.getSequences())
+ {
+ SequenceI ds = seq.getDatasetSequence();
+ SequenceI preEdit = result.get(ds);
+ if (preEdit == null)
+ {
+ preEdit = new Sequence("", seq.getSequenceAsString(),
+ seq.getStart(), seq.getEnd());
+ preEdit.setDatasetSequence(ds);
+ result.put(ds, preEdit);
+ }
+ /*
+ * 'Undo' this edit action on the sequence (updating the value in the
+ * map).
+ */
+ if (ds != null)
+ {
+ if (action == Action.DELETE_GAP)
+ {
+ preEdit.setSequence(new String(StringUtils.insertCharAt(
+ preEdit.getSequence(), position, number, gap)));
+ }
+ else if (action == Action.INSERT_GAP)
+ {
+ preEdit.setSequence(new String(StringUtils.deleteChars(
+ preEdit.getSequence(), position, position + number)));
+ }
+ else
+ {
+ System.err.println("Can't undo edit action " + action);
+ // throw new IllegalStateException("Can't undo edit action " +
+ // action);
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ public class Edit
{
public SequenceI[] oldds;
Hashtable<String, Annotation[]> deletedAnnotations;
- Hashtable<SequenceI, SequenceFeature[]> editedFeatures;
+ Hashtable<SequenceI, List<SequenceFeature>> editedFeatures;
AlignmentI al;
char gapChar;
- Edit(Action command, SequenceI[] seqs, int position, int number,
- char gapChar)
+ public Edit(Action cmd, SequenceI[] sqs, int pos, int count,
+ char gap)
{
- this.command = command;
- this.seqs = seqs;
- this.position = position;
- this.number = number;
- this.gapChar = gapChar;
+ this.command = cmd;
+ this.seqs = sqs;
+ this.position = pos;
+ this.number = count;
+ this.gapChar = gap;
}
- Edit(Action command, SequenceI[] seqs, int position, int number,
- AlignmentI al)
+ Edit(Action cmd, SequenceI[] sqs, int pos, int count,
+ AlignmentI align)
{
- this.gapChar = al.getGapCharacter();
- this.command = command;
- this.seqs = seqs;
- this.position = position;
- this.number = number;
- this.al = al;
-
- alIndex = new int[seqs.length];
- for (int i = 0; i < seqs.length; i++)
+ this.gapChar = align.getGapCharacter();
+ this.command = cmd;
+ this.seqs = sqs;
+ this.position = pos;
+ this.number = count;
+ this.al = align;
+
+ alIndex = new int[sqs.length];
+ for (int i = 0; i < sqs.length; i++)
{
- alIndex[i] = al.findIndex(seqs[i]);
+ alIndex[i] = align.findIndex(sqs[i]);
}
- fullAlignmentHeight = (al.getHeight() == seqs.length);
+ fullAlignmentHeight = (align.getHeight() == sqs.length);
}
- Edit(Action command, SequenceI[] seqs, int position, int number,
- AlignmentI al, String replace)
+ Edit(Action cmd, SequenceI[] sqs, int pos, int count,
+ AlignmentI align, String replace)
{
- this.command = command;
- this.seqs = seqs;
- this.position = position;
- this.number = number;
- this.al = al;
- this.gapChar = al.getGapCharacter();
- string = new char[seqs.length][];
- for (int i = 0; i < seqs.length; i++)
+ this.command = cmd;
+ this.seqs = sqs;
+ this.position = pos;
+ this.number = count;
+ this.al = align;
+ this.gapChar = align.getGapCharacter();
+ string = new char[sqs.length][];
+ for (int i = 0; i < sqs.length; i++)
{
string[i] = replace.toCharArray();
}
- fullAlignmentHeight = (al.getHeight() == seqs.length);
+ fullAlignmentHeight = (align.getHeight() == sqs.length);
+ }
+
+ public SequenceI[] getSequences()
+ {
+ return seqs;
+ }
+
+ public int getPosition()
+ {
+ return position;
+ }
+
+ public Action getAction()
+ {
+ return command;
+ }
+
+ public int getNumber()
+ {
+ return number;
+ }
+
+ public char getGapCharacter()
+ {
+ return gapChar;
+ }
+ }
+
+ /**
+ * Returns an iterator over the list of edit commands which traverses the list
+ * either forwards or backwards.
+ *
+ * @param forwards
+ * @return
+ */
+ public Iterator<Edit> getEditIterator(boolean forwards)
+ {
+ if (forwards)
+ {
+ return getEdits().iterator();
+ }
+ else
+ {
+ return new ReverseListIterator<Edit>(getEdits());
}
}
}