2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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;
58 public static final int INSERT_NUC=5;
68 public EditCommand(String description)
70 this.description = description;
73 public EditCommand(String description, int command, SequenceI[] seqs,
74 int position, int number, AlignmentI al)
76 this.description = description;
77 if (command == CUT || command == PASTE)
80 { new Edit(command, seqs, position, number, al) };
86 public EditCommand(String description, int command, String replace,
87 SequenceI[] seqs, int position, int number, AlignmentI al)
89 this.description = description;
90 if (command == REPLACE)
93 { new Edit(command, seqs, position, number, al, replace) };
100 final public String getDescription()
108 return edits == null ? 0 : edits.length;
111 final public AlignmentI getAlignment()
117 * append a new editCommand Note. this shouldn't be called if the edit is an
118 * operation affects more alignment objects than the one referenced in al (for
119 * example, cut or pasting whole sequences). Use the form with an additional
120 * AlignmentI[] views parameter.
129 final public void appendEdit(int command, SequenceI[] seqs, int position,
130 int number, AlignmentI al, boolean performEdit)
132 appendEdit(command, seqs, position, number, al, performEdit, null);
136 * append a new edit command with a set of alignment views that may be
147 final public void appendEdit(int command, SequenceI[] seqs, int position,
148 int number, AlignmentI al, boolean performEdit, AlignmentI[] views)
150 Edit edit = new Edit(command, seqs, position, number,
151 al.getGapCharacter());
152 if (al.getHeight() == seqs.length)
155 edit.fullAlignmentHeight = true;
160 Edit[] temp = new Edit[edits.length + 1];
161 System.arraycopy(edits, 0, temp, 0, edits.length);
163 edits[edits.length - 1] = edit;
173 performEdit(edits.length - 1, views);
177 final void performEdit(int commandIndex, AlignmentI[] views)
179 int eSize = edits.length;
180 for (int e = commandIndex; e < eSize; e++)
182 switch (edits[e].command)
191 cut(edits[e], views);
194 paste(edits[e], views);
199 //TODO:add deleteNuc for UNDO
201 // insertNuc(edits[e]);
208 final public void doCommand(AlignmentI[] views)
210 performEdit(0, views);
214 final public void undoCommand(AlignmentI[] views)
216 int e = 0, eSize = edits.length;
217 for (e = eSize - 1; e > -1; e--)
219 switch (edits[e].command)
228 paste(edits[e], views);
231 cut(edits[e], views);
240 final void insertGap(Edit command)
243 for (int s = 0; s < command.seqs.length; s++)
245 command.seqs[s].insertCharAt(command.position, command.number,
247 // System.out.println("pos: "+command.position+" number: "+command.number);
250 adjustAnnotations(command, true, false, null);
253 // final void insertNuc(Edit command)
256 // for (int s = 0; s < command.seqs.length; s++)
258 // System.out.println("pos: "+command.position+" number: "+command.number);
259 // command.seqs[s].insertCharAt(command.position, command.number,'A');
262 // adjustAnnotations(command, true, false, null);
265 final void deleteGap(Edit command)
267 for (int s = 0; s < command.seqs.length; s++)
269 command.seqs[s].deleteChars(command.position, command.position
273 adjustAnnotations(command, false, false, null);
276 void cut(Edit command, AlignmentI[] views)
278 boolean seqDeleted = false;
279 command.string = new char[command.seqs.length][];
281 for (int i = 0; i < command.seqs.length; i++)
283 if (command.seqs[i].getLength() > command.position)
285 command.string[i] = command.seqs[i].getSequence(command.position,
286 command.position + command.number);
287 SequenceI oldds = command.seqs[i].getDatasetSequence();
288 if (command.oldds != null && command.oldds[i] != null)
290 // we are redoing an undone cut.
291 command.seqs[i].setDatasetSequence(null);
293 command.seqs[i].deleteChars(command.position, command.position
295 if (command.oldds != null && command.oldds[i] != null)
297 // oldds entry contains the cut dataset sequence.
298 command.seqs[i].setDatasetSequence(command.oldds[i]);
299 command.oldds[i] = oldds;
303 // modify the oldds if necessary
304 if (oldds != command.seqs[i].getDatasetSequence()
305 || command.seqs[i].getSequenceFeatures() != null)
307 if (command.oldds == null)
309 command.oldds = new SequenceI[command.seqs.length];
311 command.oldds[i] = oldds;
315 command.seqs[i].findPosition(command.position),
316 command.seqs[i].findPosition(command.position
317 + command.number), false);
322 if (command.seqs[i].getLength() < 1)
324 command.al.deleteSequence(command.seqs[i]);
329 adjustAnnotations(command, false, seqDeleted, views);
332 void paste(Edit command, AlignmentI[] views)
336 boolean newDSWasNeeded;
337 int newstart, newend;
338 boolean seqWasDeleted = false;
339 int start = 0, end = 0;
341 for (int i = 0; i < command.seqs.length; i++)
344 newDSWasNeeded = command.oldds != null && command.oldds[i] != null;
345 if (command.seqs[i].getLength() < 1)
347 // ie this sequence was deleted, we need to
348 // read it to the alignment
349 if (command.alIndex[i] < command.al.getHeight())
351 List<SequenceI> sequences;
352 synchronized (sequences=command.al.getSequences()) {
353 sequences.add(command.alIndex[i], command.seqs[i]);
358 command.al.addSequence(command.seqs[i]);
360 seqWasDeleted = true;
362 newstart = command.seqs[i].getStart();
363 newend = command.seqs[i].getEnd();
365 tmp = new StringBuffer();
366 tmp.append(command.seqs[i].getSequence());
367 // Undo of a delete does not replace original dataset sequence on to
368 // alignment sequence.
370 if (command.string != null && command.string[i] != null)
372 if (command.position >= tmp.length())
374 // This occurs if padding is on, and residues
375 // are removed from end of alignment
376 int length = command.position - tmp.length();
379 tmp.append(command.gapChar);
383 tmp.insert(command.position, command.string[i]);
384 for (int s = 0; s < command.string[i].length; s++)
386 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
391 start = command.seqs[i].findPosition(command.position);
392 end = command.seqs[i].findPosition(command.position
395 if (command.seqs[i].getStart() == start)
401 command.string[i] = null;
404 command.seqs[i].setSequence(tmp.toString());
405 command.seqs[i].setStart(newstart);
406 command.seqs[i].setEnd(newend);
409 if (command.seqs[i].getDatasetSequence() != null)
414 ds = command.oldds[i];
418 // make a new DS sequence
419 // use new ds mechanism here
420 ds = new Sequence(command.seqs[i].getName(),
421 jalview.analysis.AlignSeq.extractGaps(
422 jalview.util.Comparison.GapChars,
423 command.seqs[i].getSequenceAsString()),
424 command.seqs[i].getStart(), command.seqs[i].getEnd());
425 ds.setDescription(command.seqs[i].getDescription());
427 if (command.oldds == null)
429 command.oldds = new SequenceI[command.seqs.length];
431 command.oldds[i] = command.seqs[i].getDatasetSequence();
432 command.seqs[i].setDatasetSequence(ds);
434 adjustFeatures(command, i, start, end, true);
437 adjustAnnotations(command, true, seqWasDeleted, views);
439 command.string = null;
442 void replace(Edit command)
446 int start = command.position;
447 int end = command.number;
448 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
450 // TODO Jalview 2.4 bugfix change to an aggregate command - original
451 // sequence string is cut, new string is pasted in.
452 command.number = start + command.string[0].length;
453 for (int i = 0; i < command.seqs.length; i++)
456 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
457 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
458 * viewport.alignment));
462 * then addHistoryItem(new EditCommand( "Add sequences",
463 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
466 oldstring = command.seqs[i].getSequenceAsString();
467 tmp = new StringBuffer(oldstring.substring(0, start));
468 tmp.append(command.string[i]);
469 tmp.append(oldstring.substring(end));
470 command.seqs[i].setSequence(tmp.toString());
471 command.string[i] = oldstring.substring(start, end).toCharArray();
477 final void adjustAnnotations(Edit command, boolean insert,
478 boolean modifyVisibility, AlignmentI[] views)
480 AlignmentAnnotation[] annotations = null;
482 if (modifyVisibility && !insert)
484 // only occurs if a sequence was added or deleted.
485 command.deletedAnnotationRows = new Hashtable();
487 if (command.fullAlignmentHeight)
489 annotations = command.al.getAlignmentAnnotation();
494 AlignmentAnnotation[] tmp;
495 for (int s = 0; s < command.seqs.length; s++)
497 if (modifyVisibility)
499 // Rows are only removed or added to sequence object.
503 tmp = command.seqs[s].getAnnotation();
506 int alen = tmp.length;
507 for (int aa = 0; aa < tmp.length; aa++)
509 if (!command.al.deleteAnnotation(tmp[aa]))
511 // strip out annotation not in the current al (will be put
512 // back on insert in all views)
517 command.seqs[s].setAlignmentAnnotation(null);
518 if (alen != tmp.length)
520 // save the non-null annotation references only
521 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
522 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
526 saved[aapos++] = tmp[aa];
531 command.deletedAnnotationRows.put(command.seqs[s], saved);
532 // and then remove any annotation in the other views
533 for (int alview = 0; views != null && alview < views.length; alview++)
535 if (views[alview] != command.al)
537 AlignmentAnnotation[] toremove = views[alview]
538 .getAlignmentAnnotation();
539 if (toremove == null || toremove.length == 0)
543 // remove any alignment annotation on this sequence that's
544 // on that alignment view.
545 for (int aa = 0; aa < toremove.length; aa++)
547 if (toremove[aa].sequenceRef == command.seqs[s])
549 views[alview].deleteAnnotation(toremove[aa]);
557 // save all the annotation
558 command.deletedAnnotationRows.put(command.seqs[s], tmp);
565 if (command.deletedAnnotationRows != null
566 && command.deletedAnnotationRows
567 .containsKey(command.seqs[s]))
569 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
570 .get(command.seqs[s]);
571 command.seqs[s].setAlignmentAnnotation(revealed);
572 if (revealed != null)
574 for (int aa = 0; aa < revealed.length; aa++)
576 // iterate through al adding original annotation
577 command.al.addAnnotation(revealed[aa]);
579 for (int aa = 0; aa < revealed.length; aa++)
581 command.al.setAnnotationIndex(revealed[aa], aa);
583 // and then duplicate added annotation on every other alignment
585 for (int vnum = 0; views != null && vnum < views.length; vnum++)
587 if (views[vnum] != command.al)
589 int avwidth = views[vnum].getWidth() + 1;
590 // duplicate in this view
591 for (int a = 0; a < revealed.length; a++)
593 AlignmentAnnotation newann = new AlignmentAnnotation(
595 command.seqs[s].addAlignmentAnnotation(newann);
596 newann.padAnnotation(avwidth);
597 views[vnum].addAnnotation(newann);
598 views[vnum].setAnnotationIndex(newann, a);
608 if (command.seqs[s].getAnnotation() == null)
615 annotations = command.seqs[s].getAnnotation();
619 tmp = new AlignmentAnnotation[aSize
620 + command.seqs[s].getAnnotation().length];
622 System.arraycopy(annotations, 0, tmp, 0, aSize);
624 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
625 command.seqs[s].getAnnotation().length);
629 aSize = annotations.length;
633 if (annotations == null)
640 command.deletedAnnotations = new Hashtable();
645 for (int a = 0; a < annotations.length; a++)
647 if (annotations[a].autoCalculated
648 || annotations[a].annotations == null)
655 aSize = annotations[a].annotations.length;
658 temp = new Annotation[aSize + command.number];
659 if (annotations[a].padGaps)
660 for (int aa = 0; aa < temp.length; aa++)
662 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
667 if (command.position < aSize)
669 if (command.position + command.number >= aSize)
675 tSize = aSize - command.number;
687 temp = new Annotation[tSize];
692 if (command.position < annotations[a].annotations.length)
694 System.arraycopy(annotations[a].annotations, 0, temp, 0,
697 if (command.deletedAnnotations != null
698 && command.deletedAnnotations
699 .containsKey(annotations[a].annotationId))
701 Annotation[] restore = (Annotation[]) command.deletedAnnotations
702 .get(annotations[a].annotationId);
704 System.arraycopy(restore, 0, temp, command.position,
709 System.arraycopy(annotations[a].annotations, command.position,
710 temp, command.position + command.number, aSize
715 if (command.deletedAnnotations != null
716 && command.deletedAnnotations
717 .containsKey(annotations[a].annotationId))
719 Annotation[] restore = (Annotation[]) command.deletedAnnotations
720 .get(annotations[a].annotationId);
722 temp = new Annotation[annotations[a].annotations.length
724 System.arraycopy(annotations[a].annotations, 0, temp, 0,
725 annotations[a].annotations.length);
726 System.arraycopy(restore, 0, temp,
727 annotations[a].annotations.length, restore.length);
731 temp = annotations[a].annotations;
737 if (tSize != aSize || command.position < 2)
739 int copylen = Math.min(command.position,
740 annotations[a].annotations.length);
742 System.arraycopy(annotations[a].annotations, 0, temp, 0,
743 copylen); // command.position);
745 Annotation[] deleted = new Annotation[command.number];
746 if (copylen >= command.position)
748 copylen = Math.min(command.number,
749 annotations[a].annotations.length - command.position);
752 System.arraycopy(annotations[a].annotations,
753 command.position, deleted, 0, copylen); // command.number);
757 command.deletedAnnotations.put(annotations[a].annotationId,
759 if (annotations[a].annotations.length > command.position
762 System.arraycopy(annotations[a].annotations, command.position
763 + command.number, temp, command.position,
764 annotations[a].annotations.length - command.position
765 - command.number); // aSize
770 int dSize = aSize - command.position;
774 Annotation[] deleted = new Annotation[command.number];
775 System.arraycopy(annotations[a].annotations, command.position,
778 command.deletedAnnotations.put(annotations[a].annotationId,
781 tSize = Math.min(annotations[a].annotations.length,
783 temp = new Annotation[tSize];
784 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
788 temp = annotations[a].annotations;
793 annotations[a].annotations = temp;
797 final void adjustFeatures(Edit command, int index, int i, int j,
800 SequenceI seq = command.seqs[index];
801 SequenceI sequence = seq.getDatasetSequence();
802 if (sequence == null)
809 if (command.editedFeatures != null
810 && command.editedFeatures.containsKey(seq))
812 sequence.setSequenceFeatures((SequenceFeature[]) command.editedFeatures
819 SequenceFeature[] sf = sequence.getSequenceFeatures();
826 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
830 for (int s = 0; s < sf.length; s++)
832 SequenceFeature copy = new SequenceFeature(sf[s]);
836 if (sf[s].getEnd() < i)
841 if (sf[s].getBegin() > j)
843 sf[s].setBegin(copy.getBegin() - cSize);
844 sf[s].setEnd(copy.getEnd() - cSize);
848 if (sf[s].getBegin() >= i)
853 if (sf[s].getEnd() < j)
858 sf[s].setEnd(sf[s].getEnd() - (cSize));
860 if (sf[s].getBegin() > sf[s].getEnd())
862 sequence.deleteFeature(sf[s]);
866 if (command.editedFeatures == null)
868 command.editedFeatures = new Hashtable();
871 command.editedFeatures.put(seq, oldsf);
877 public SequenceI[] oldds;
879 boolean fullAlignmentHeight = false;
881 Hashtable deletedAnnotationRows;
883 Hashtable deletedAnnotations;
885 Hashtable editedFeatures;
897 int position, number;
901 Edit(int command, SequenceI[] seqs, int position, int number,
904 this.command = command;
906 this.position = position;
907 this.number = number;
908 this.gapChar = gapChar;
911 Edit(int command, SequenceI[] seqs, int position, int number,
914 this.gapChar = al.getGapCharacter();
915 this.command = command;
917 this.position = position;
918 this.number = number;
921 alIndex = new int[seqs.length];
922 for (int i = 0; i < seqs.length; i++)
924 alIndex[i] = al.findIndex(seqs[i]);
927 fullAlignmentHeight = (al.getHeight() == seqs.length);
930 Edit(int command, SequenceI[] seqs, int position, int number,
931 AlignmentI al, String replace)
933 this.command = command;
935 this.position = position;
936 this.number = number;
938 this.gapChar = al.getGapCharacter();
939 string = new char[seqs.length][];
940 for (int i = 0; i < seqs.length; i++)
942 string[i] = replace.toCharArray();
945 fullAlignmentHeight = (al.getHeight() == seqs.length);