2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.commands;
22 import jalview.datamodel.*;
31 * Description: Essential information for performing undo and redo for cut/paste
32 * insert/delete gap which can be stored in the HistoryList
36 * Copyright: Copyright (c) 2006
40 * Company: Dundee University
43 * @author not attributable
46 public class EditCommand implements CommandI
48 public static final int INSERT_GAP = 0;
50 public static final int DELETE_GAP = 1;
52 public static final int CUT = 2;
54 public static final int PASTE = 3;
56 public static final int REPLACE = 4;
66 public EditCommand(String description)
68 this.description = description;
71 public EditCommand(String description, int command, SequenceI[] seqs,
72 int position, int number, AlignmentI al)
74 this.description = description;
75 if (command == CUT || command == PASTE)
78 { new Edit(command, seqs, position, number, al) };
84 public EditCommand(String description, int command, String replace,
85 SequenceI[] seqs, int position, int number, AlignmentI al)
87 this.description = description;
88 if (command == REPLACE)
91 { new Edit(command, seqs, position, number, al, replace) };
97 final public String getDescription()
104 return edits == null ? 0 : edits.length;
107 final public AlignmentI getAlignment()
113 * append a new editCommand Note. this shouldn't be called if the edit is an
114 * operation affects more alignment objects than the one referenced in al (for
115 * example, cut or pasting whole sequences). Use the form with an additional
116 * AlignmentI[] views parameter.
125 final public void appendEdit(int command, SequenceI[] seqs, int position,
126 int number, AlignmentI al, boolean performEdit)
128 appendEdit(command, seqs, position, number, al, performEdit, null);
132 * append a new edit command with a set of alignment views that may be
143 final public void appendEdit(int command, SequenceI[] seqs, int position,
144 int number, AlignmentI al, boolean performEdit, AlignmentI[] views)
146 Edit edit = new Edit(command, seqs, position, number,
147 al.getGapCharacter());
148 if (al.getHeight() == seqs.length)
151 edit.fullAlignmentHeight = true;
156 Edit[] temp = new Edit[edits.length + 1];
157 System.arraycopy(edits, 0, temp, 0, edits.length);
159 edits[edits.length - 1] = edit;
169 performEdit(edits.length - 1, views);
173 final void performEdit(int commandIndex, AlignmentI[] views)
175 int eSize = edits.length;
176 for (int e = commandIndex; e < eSize; e++)
178 switch (edits[e].command)
187 cut(edits[e], views);
190 paste(edits[e], views);
199 final public void doCommand(AlignmentI[] views)
201 performEdit(0, views);
204 final public void undoCommand(AlignmentI[] views)
206 int e = 0, eSize = edits.length;
207 for (e = eSize - 1; e > -1; e--)
209 switch (edits[e].command)
218 paste(edits[e], views);
221 cut(edits[e], views);
230 final void insertGap(Edit command)
233 for (int s = 0; s < command.seqs.length; s++)
235 command.seqs[s].insertCharAt(command.position, command.number,
239 adjustAnnotations(command, true, false, null);
242 final void deleteGap(Edit command)
244 for (int s = 0; s < command.seqs.length; s++)
246 command.seqs[s].deleteChars(command.position, command.position
250 adjustAnnotations(command, false, false, null);
253 void cut(Edit command, AlignmentI[] views)
255 boolean seqDeleted = false;
256 command.string = new char[command.seqs.length][];
258 for (int i = 0; i < command.seqs.length; i++)
260 if (command.seqs[i].getLength() > command.position)
262 command.string[i] = command.seqs[i].getSequence(command.position,
263 command.position + command.number);
264 SequenceI oldds = command.seqs[i].getDatasetSequence();
265 if (command.oldds != null && command.oldds[i] != null)
267 // we are redoing an undone cut.
268 command.seqs[i].setDatasetSequence(null);
270 command.seqs[i].deleteChars(command.position, command.position
272 if (command.oldds != null && command.oldds[i] != null)
274 // oldds entry contains the cut dataset sequence.
275 command.seqs[i].setDatasetSequence(command.oldds[i]);
276 command.oldds[i] = oldds;
280 // modify the oldds if necessary
281 if (oldds != command.seqs[i].getDatasetSequence()
282 || command.seqs[i].getSequenceFeatures() != null)
284 if (command.oldds == null)
286 command.oldds = new SequenceI[command.seqs.length];
288 command.oldds[i] = oldds;
292 command.seqs[i].findPosition(command.position),
293 command.seqs[i].findPosition(command.position
294 + command.number), false);
299 if (command.seqs[i].getLength() < 1)
301 command.al.deleteSequence(command.seqs[i]);
306 adjustAnnotations(command, false, seqDeleted, views);
309 void paste(Edit command, AlignmentI[] views)
313 boolean newDSWasNeeded;
314 int newstart, newend;
315 boolean seqWasDeleted = false;
316 int start = 0, end = 0;
318 for (int i = 0; i < command.seqs.length; i++)
321 newDSWasNeeded = command.oldds != null && command.oldds[i] != null;
322 if (command.seqs[i].getLength() < 1)
324 // ie this sequence was deleted, we need to
325 // read it to the alignment
326 if (command.alIndex[i] < command.al.getHeight())
328 command.al.getSequences().insertElementAt(command.seqs[i],
333 command.al.addSequence(command.seqs[i]);
335 seqWasDeleted = true;
337 newstart = command.seqs[i].getStart();
338 newend = command.seqs[i].getEnd();
340 tmp = new StringBuffer();
341 tmp.append(command.seqs[i].getSequence());
342 // Undo of a delete does not replace original dataset sequence on to
343 // alignment sequence.
345 if (command.string != null && command.string[i] != null)
347 if (command.position >= tmp.length())
349 // This occurs if padding is on, and residues
350 // are removed from end of alignment
351 int length = command.position - tmp.length();
354 tmp.append(command.gapChar);
358 tmp.insert(command.position, command.string[i]);
359 for (int s = 0; s < command.string[i].length; s++)
361 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
366 start = command.seqs[i].findPosition(command.position);
367 end = command.seqs[i].findPosition(command.position
370 if (command.seqs[i].getStart() == start)
376 command.string[i] = null;
379 command.seqs[i].setSequence(tmp.toString());
380 command.seqs[i].setStart(newstart);
381 command.seqs[i].setEnd(newend);
384 if (command.seqs[i].getDatasetSequence() != null)
389 ds = command.oldds[i];
393 // make a new DS sequence
394 // use new ds mechanism here
395 ds = new Sequence(command.seqs[i].getName(),
396 jalview.analysis.AlignSeq.extractGaps(
397 jalview.util.Comparison.GapChars,
398 command.seqs[i].getSequenceAsString()),
399 command.seqs[i].getStart(), command.seqs[i].getEnd());
400 ds.setDescription(command.seqs[i].getDescription());
402 if (command.oldds == null)
404 command.oldds = new SequenceI[command.seqs.length];
406 command.oldds[i] = command.seqs[i].getDatasetSequence();
407 command.seqs[i].setDatasetSequence(ds);
409 adjustFeatures(command, i, start, end, true);
412 adjustAnnotations(command, true, seqWasDeleted, views);
414 command.string = null;
417 void replace(Edit command)
421 int start = command.position;
422 int end = command.number;
423 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
425 // TODO Jalview 2.4 bugfix change to an aggregate command - original
426 // sequence string is cut, new string is pasted in.
427 command.number = start + command.string[0].length;
428 for (int i = 0; i < command.seqs.length; i++)
431 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
432 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
433 * viewport.alignment));
437 * then addHistoryItem(new EditCommand( "Add sequences",
438 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
441 oldstring = command.seqs[i].getSequenceAsString();
442 tmp = new StringBuffer(oldstring.substring(0, start));
443 tmp.append(command.string[i]);
444 tmp.append(oldstring.substring(end));
445 command.seqs[i].setSequence(tmp.toString());
446 command.string[i] = oldstring.substring(start, end).toCharArray();
452 final void adjustAnnotations(Edit command, boolean insert,
453 boolean modifyVisibility, AlignmentI[] views)
455 AlignmentAnnotation[] annotations = null;
457 if (modifyVisibility && !insert)
459 // only occurs if a sequence was added or deleted.
460 command.deletedAnnotationRows = new Hashtable();
462 if (command.fullAlignmentHeight)
464 annotations = command.al.getAlignmentAnnotation();
469 AlignmentAnnotation[] tmp;
470 for (int s = 0; s < command.seqs.length; s++)
472 if (modifyVisibility)
474 // Rows are only removed or added to sequence object.
478 tmp = command.seqs[s].getAnnotation();
481 int alen = tmp.length;
482 for (int aa = 0; aa < tmp.length; aa++)
484 if (!command.al.deleteAnnotation(tmp[aa]))
486 // strip out annotation not in the current al (will be put
487 // back on insert in all views)
492 command.seqs[s].setAlignmentAnnotation(null);
493 if (alen != tmp.length)
495 // save the non-null annotation references only
496 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
497 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
501 saved[aapos++] = tmp[aa];
506 command.deletedAnnotationRows.put(command.seqs[s], saved);
507 // and then remove any annotation in the other views
508 for (int alview = 0; views != null && alview < views.length; alview++)
510 if (views[alview] != command.al)
512 AlignmentAnnotation[] toremove = views[alview]
513 .getAlignmentAnnotation();
514 if (toremove == null || toremove.length == 0)
518 // remove any alignment annotation on this sequence that's
519 // on that alignment view.
520 for (int aa = 0; aa < toremove.length; aa++)
522 if (toremove[aa].sequenceRef == command.seqs[s])
524 views[alview].deleteAnnotation(toremove[aa]);
532 // save all the annotation
533 command.deletedAnnotationRows.put(command.seqs[s], tmp);
540 if (command.deletedAnnotationRows != null
541 && command.deletedAnnotationRows
542 .containsKey(command.seqs[s]))
544 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
545 .get(command.seqs[s]);
546 command.seqs[s].setAlignmentAnnotation(revealed);
547 if (revealed != null)
549 for (int aa = 0; aa < revealed.length; aa++)
551 // iterate through al adding original annotation
552 command.al.addAnnotation(revealed[aa]);
554 for (int aa = 0; aa < revealed.length; aa++)
556 command.al.setAnnotationIndex(revealed[aa], aa);
558 // and then duplicate added annotation on every other alignment
560 for (int vnum = 0; views != null && vnum < views.length; vnum++)
562 if (views[vnum] != command.al)
564 int avwidth = views[vnum].getWidth() + 1;
565 // duplicate in this view
566 for (int a = 0; a < revealed.length; a++)
568 AlignmentAnnotation newann = new AlignmentAnnotation(
570 command.seqs[s].addAlignmentAnnotation(newann);
571 newann.padAnnotation(avwidth);
572 views[vnum].addAnnotation(newann);
573 views[vnum].setAnnotationIndex(newann, a);
583 if (command.seqs[s].getAnnotation() == null)
590 annotations = command.seqs[s].getAnnotation();
594 tmp = new AlignmentAnnotation[aSize
595 + command.seqs[s].getAnnotation().length];
597 System.arraycopy(annotations, 0, tmp, 0, aSize);
599 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
600 command.seqs[s].getAnnotation().length);
604 aSize = annotations.length;
608 if (annotations == null)
615 command.deletedAnnotations = new Hashtable();
620 for (int a = 0; a < annotations.length; a++)
622 if (annotations[a].autoCalculated
623 || annotations[a].annotations == null)
630 aSize = annotations[a].annotations.length;
633 temp = new Annotation[aSize + command.number];
634 if (annotations[a].padGaps)
635 for (int aa = 0; aa < temp.length; aa++)
637 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
642 if (command.position < aSize)
644 if (command.position + command.number >= aSize)
650 tSize = aSize - command.number;
662 temp = new Annotation[tSize];
667 if (command.position < annotations[a].annotations.length)
669 System.arraycopy(annotations[a].annotations, 0, temp, 0,
672 if (command.deletedAnnotations != null
673 && command.deletedAnnotations
674 .containsKey(annotations[a].annotationId))
676 Annotation[] restore = (Annotation[]) command.deletedAnnotations
677 .get(annotations[a].annotationId);
679 System.arraycopy(restore, 0, temp, command.position,
684 System.arraycopy(annotations[a].annotations, command.position,
685 temp, command.position + command.number, aSize
690 if (command.deletedAnnotations != null
691 && command.deletedAnnotations
692 .containsKey(annotations[a].annotationId))
694 Annotation[] restore = (Annotation[]) command.deletedAnnotations
695 .get(annotations[a].annotationId);
697 temp = new Annotation[annotations[a].annotations.length
699 System.arraycopy(annotations[a].annotations, 0, temp, 0,
700 annotations[a].annotations.length);
701 System.arraycopy(restore, 0, temp,
702 annotations[a].annotations.length, restore.length);
706 temp = annotations[a].annotations;
712 if (tSize != aSize || command.position < 2)
714 int copylen = Math.min(command.position,
715 annotations[a].annotations.length);
717 System.arraycopy(annotations[a].annotations, 0, temp, 0,
718 copylen); // command.position);
720 Annotation[] deleted = new Annotation[command.number];
721 if (copylen >= command.position)
723 copylen = Math.min(command.number,
724 annotations[a].annotations.length - command.position);
727 System.arraycopy(annotations[a].annotations,
728 command.position, deleted, 0, copylen); // command.number);
732 command.deletedAnnotations.put(annotations[a].annotationId,
734 if (annotations[a].annotations.length > command.position
737 System.arraycopy(annotations[a].annotations, command.position
738 + command.number, temp, command.position,
739 annotations[a].annotations.length - command.position
740 - command.number); // aSize
745 int dSize = aSize - command.position;
749 Annotation[] deleted = new Annotation[command.number];
750 System.arraycopy(annotations[a].annotations, command.position,
753 command.deletedAnnotations.put(annotations[a].annotationId,
756 tSize = Math.min(annotations[a].annotations.length,
758 temp = new Annotation[tSize];
759 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
763 temp = annotations[a].annotations;
768 annotations[a].annotations = temp;
772 final void adjustFeatures(Edit command, int index, int i, int j,
775 SequenceI seq = command.seqs[index];
776 SequenceI sequence = seq.getDatasetSequence();
777 if (sequence == null)
784 if (command.editedFeatures != null
785 && command.editedFeatures.containsKey(seq))
787 sequence.setSequenceFeatures((SequenceFeature[]) command.editedFeatures
794 SequenceFeature[] sf = sequence.getSequenceFeatures();
801 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
805 for (int s = 0; s < sf.length; s++)
807 SequenceFeature copy = new SequenceFeature(sf[s]);
811 if (sf[s].getEnd() < i)
816 if (sf[s].getBegin() > j)
818 sf[s].setBegin(copy.getBegin() - cSize);
819 sf[s].setEnd(copy.getEnd() - cSize);
823 if (sf[s].getBegin() >= i)
828 if (sf[s].getEnd() < j)
833 sf[s].setEnd(sf[s].getEnd() - (cSize));
835 if (sf[s].getBegin() > sf[s].getEnd())
837 sequence.deleteFeature(sf[s]);
841 if (command.editedFeatures == null)
843 command.editedFeatures = new Hashtable();
846 command.editedFeatures.put(seq, oldsf);
852 public SequenceI[] oldds;
854 boolean fullAlignmentHeight = false;
856 Hashtable deletedAnnotationRows;
858 Hashtable deletedAnnotations;
860 Hashtable editedFeatures;
872 int position, number;
876 Edit(int command, SequenceI[] seqs, int position, int number,
879 this.command = command;
881 this.position = position;
882 this.number = number;
883 this.gapChar = gapChar;
886 Edit(int command, SequenceI[] seqs, int position, int number,
889 this.gapChar = al.getGapCharacter();
890 this.command = command;
892 this.position = position;
893 this.number = number;
896 alIndex = new int[seqs.length];
897 for (int i = 0; i < seqs.length; i++)
899 alIndex[i] = al.findIndex(seqs[i]);
902 fullAlignmentHeight = (al.getHeight() == seqs.length);
905 Edit(int command, SequenceI[] seqs, int position, int number,
906 AlignmentI al, String replace)
908 this.command = command;
910 this.position = position;
911 this.number = number;
913 this.gapChar = al.getGapCharacter();
914 string = new char[seqs.length][];
915 for (int i = 0; i < seqs.length; i++)
917 string[i] = replace.toCharArray();
920 fullAlignmentHeight = (al.getHeight() == seqs.length);