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) };
99 final public String getDescription()
106 return edits == null ? 0 : edits.length;
109 final public AlignmentI getAlignment()
115 * append a new editCommand Note. this shouldn't be called if the edit is an
116 * operation affects more alignment objects than the one referenced in al (for
117 * example, cut or pasting whole sequences). Use the form with an additional
118 * AlignmentI[] views parameter.
127 final public void appendEdit(int command, SequenceI[] seqs, int position,
128 int number, AlignmentI al, boolean performEdit)
130 appendEdit(command, seqs, position, number, al, performEdit, null);
134 * append a new edit command with a set of alignment views that may be
145 final public void appendEdit(int command, SequenceI[] seqs, int position,
146 int number, AlignmentI al, boolean performEdit, AlignmentI[] views)
148 Edit edit = new Edit(command, seqs, position, number,
149 al.getGapCharacter());
150 if (al.getHeight() == seqs.length)
153 edit.fullAlignmentHeight = true;
158 Edit[] temp = new Edit[edits.length + 1];
159 System.arraycopy(edits, 0, temp, 0, edits.length);
161 edits[edits.length - 1] = edit;
171 performEdit(edits.length - 1, views);
175 final void performEdit(int commandIndex, AlignmentI[] views)
177 int eSize = edits.length;
178 for (int e = commandIndex; e < eSize; e++)
180 switch (edits[e].command)
189 cut(edits[e], views);
192 paste(edits[e], views);
197 //TODO:add deleteNuc for UNDO
199 // insertNuc(edits[e]);
205 final public void doCommand(AlignmentI[] views)
207 performEdit(0, views);
210 final public void undoCommand(AlignmentI[] views)
212 int e = 0, eSize = edits.length;
213 for (e = eSize - 1; e > -1; e--)
215 switch (edits[e].command)
224 paste(edits[e], views);
227 cut(edits[e], views);
236 final void insertGap(Edit command)
239 for (int s = 0; s < command.seqs.length; s++)
241 command.seqs[s].insertCharAt(command.position, command.number,
243 // System.out.println("pos: "+command.position+" number: "+command.number);
246 adjustAnnotations(command, true, false, null);
249 // final void insertNuc(Edit command)
252 // for (int s = 0; s < command.seqs.length; s++)
254 // System.out.println("pos: "+command.position+" number: "+command.number);
255 // command.seqs[s].insertCharAt(command.position, command.number,'A');
258 // adjustAnnotations(command, true, false, null);
261 final void deleteGap(Edit command)
263 for (int s = 0; s < command.seqs.length; s++)
265 command.seqs[s].deleteChars(command.position, command.position
269 adjustAnnotations(command, false, false, null);
272 void cut(Edit command, AlignmentI[] views)
274 boolean seqDeleted = false;
275 command.string = new char[command.seqs.length][];
277 for (int i = 0; i < command.seqs.length; i++)
279 if (command.seqs[i].getLength() > command.position)
281 command.string[i] = command.seqs[i].getSequence(command.position,
282 command.position + command.number);
283 SequenceI oldds = command.seqs[i].getDatasetSequence();
284 if (command.oldds != null && command.oldds[i] != null)
286 // we are redoing an undone cut.
287 command.seqs[i].setDatasetSequence(null);
289 command.seqs[i].deleteChars(command.position, command.position
291 if (command.oldds != null && command.oldds[i] != null)
293 // oldds entry contains the cut dataset sequence.
294 command.seqs[i].setDatasetSequence(command.oldds[i]);
295 command.oldds[i] = oldds;
299 // modify the oldds if necessary
300 if (oldds != command.seqs[i].getDatasetSequence()
301 || command.seqs[i].getSequenceFeatures() != null)
303 if (command.oldds == null)
305 command.oldds = new SequenceI[command.seqs.length];
307 command.oldds[i] = oldds;
311 command.seqs[i].findPosition(command.position),
312 command.seqs[i].findPosition(command.position
313 + command.number), false);
318 if (command.seqs[i].getLength() < 1)
320 command.al.deleteSequence(command.seqs[i]);
325 adjustAnnotations(command, false, seqDeleted, views);
328 void paste(Edit command, AlignmentI[] views)
332 boolean newDSWasNeeded;
333 int newstart, newend;
334 boolean seqWasDeleted = false;
335 int start = 0, end = 0;
337 for (int i = 0; i < command.seqs.length; i++)
340 newDSWasNeeded = command.oldds != null && command.oldds[i] != null;
341 if (command.seqs[i].getLength() < 1)
343 // ie this sequence was deleted, we need to
344 // read it to the alignment
345 if (command.alIndex[i] < command.al.getHeight())
347 command.al.getSequences().insertElementAt(command.seqs[i],
352 command.al.addSequence(command.seqs[i]);
354 seqWasDeleted = true;
356 newstart = command.seqs[i].getStart();
357 newend = command.seqs[i].getEnd();
359 tmp = new StringBuffer();
360 tmp.append(command.seqs[i].getSequence());
361 // Undo of a delete does not replace original dataset sequence on to
362 // alignment sequence.
364 if (command.string != null && command.string[i] != null)
366 if (command.position >= tmp.length())
368 // This occurs if padding is on, and residues
369 // are removed from end of alignment
370 int length = command.position - tmp.length();
373 tmp.append(command.gapChar);
377 tmp.insert(command.position, command.string[i]);
378 for (int s = 0; s < command.string[i].length; s++)
380 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
385 start = command.seqs[i].findPosition(command.position);
386 end = command.seqs[i].findPosition(command.position
389 if (command.seqs[i].getStart() == start)
395 command.string[i] = null;
398 command.seqs[i].setSequence(tmp.toString());
399 command.seqs[i].setStart(newstart);
400 command.seqs[i].setEnd(newend);
403 if (command.seqs[i].getDatasetSequence() != null)
408 ds = command.oldds[i];
412 // make a new DS sequence
413 // use new ds mechanism here
414 ds = new Sequence(command.seqs[i].getName(),
415 jalview.analysis.AlignSeq.extractGaps(
416 jalview.util.Comparison.GapChars,
417 command.seqs[i].getSequenceAsString()),
418 command.seqs[i].getStart(), command.seqs[i].getEnd());
419 ds.setDescription(command.seqs[i].getDescription());
421 if (command.oldds == null)
423 command.oldds = new SequenceI[command.seqs.length];
425 command.oldds[i] = command.seqs[i].getDatasetSequence();
426 command.seqs[i].setDatasetSequence(ds);
428 adjustFeatures(command, i, start, end, true);
431 adjustAnnotations(command, true, seqWasDeleted, views);
433 command.string = null;
436 void replace(Edit command)
440 int start = command.position;
441 int end = command.number;
442 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
444 // TODO Jalview 2.4 bugfix change to an aggregate command - original
445 // sequence string is cut, new string is pasted in.
446 command.number = start + command.string[0].length;
447 for (int i = 0; i < command.seqs.length; i++)
450 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
451 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
452 * viewport.alignment));
456 * then addHistoryItem(new EditCommand( "Add sequences",
457 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
460 oldstring = command.seqs[i].getSequenceAsString();
461 tmp = new StringBuffer(oldstring.substring(0, start));
462 tmp.append(command.string[i]);
463 tmp.append(oldstring.substring(end));
464 command.seqs[i].setSequence(tmp.toString());
465 command.string[i] = oldstring.substring(start, end).toCharArray();
471 final void adjustAnnotations(Edit command, boolean insert,
472 boolean modifyVisibility, AlignmentI[] views)
474 AlignmentAnnotation[] annotations = null;
476 if (modifyVisibility && !insert)
478 // only occurs if a sequence was added or deleted.
479 command.deletedAnnotationRows = new Hashtable();
481 if (command.fullAlignmentHeight)
483 annotations = command.al.getAlignmentAnnotation();
488 AlignmentAnnotation[] tmp;
489 for (int s = 0; s < command.seqs.length; s++)
491 if (modifyVisibility)
493 // Rows are only removed or added to sequence object.
497 tmp = command.seqs[s].getAnnotation();
500 int alen = tmp.length;
501 for (int aa = 0; aa < tmp.length; aa++)
503 if (!command.al.deleteAnnotation(tmp[aa]))
505 // strip out annotation not in the current al (will be put
506 // back on insert in all views)
511 command.seqs[s].setAlignmentAnnotation(null);
512 if (alen != tmp.length)
514 // save the non-null annotation references only
515 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
516 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
520 saved[aapos++] = tmp[aa];
525 command.deletedAnnotationRows.put(command.seqs[s], saved);
526 // and then remove any annotation in the other views
527 for (int alview = 0; views != null && alview < views.length; alview++)
529 if (views[alview] != command.al)
531 AlignmentAnnotation[] toremove = views[alview]
532 .getAlignmentAnnotation();
533 if (toremove == null || toremove.length == 0)
537 // remove any alignment annotation on this sequence that's
538 // on that alignment view.
539 for (int aa = 0; aa < toremove.length; aa++)
541 if (toremove[aa].sequenceRef == command.seqs[s])
543 views[alview].deleteAnnotation(toremove[aa]);
551 // save all the annotation
552 command.deletedAnnotationRows.put(command.seqs[s], tmp);
559 if (command.deletedAnnotationRows != null
560 && command.deletedAnnotationRows
561 .containsKey(command.seqs[s]))
563 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
564 .get(command.seqs[s]);
565 command.seqs[s].setAlignmentAnnotation(revealed);
566 if (revealed != null)
568 for (int aa = 0; aa < revealed.length; aa++)
570 // iterate through al adding original annotation
571 command.al.addAnnotation(revealed[aa]);
573 for (int aa = 0; aa < revealed.length; aa++)
575 command.al.setAnnotationIndex(revealed[aa], aa);
577 // and then duplicate added annotation on every other alignment
579 for (int vnum = 0; views != null && vnum < views.length; vnum++)
581 if (views[vnum] != command.al)
583 int avwidth = views[vnum].getWidth() + 1;
584 // duplicate in this view
585 for (int a = 0; a < revealed.length; a++)
587 AlignmentAnnotation newann = new AlignmentAnnotation(
589 command.seqs[s].addAlignmentAnnotation(newann);
590 newann.padAnnotation(avwidth);
591 views[vnum].addAnnotation(newann);
592 views[vnum].setAnnotationIndex(newann, a);
602 if (command.seqs[s].getAnnotation() == null)
609 annotations = command.seqs[s].getAnnotation();
613 tmp = new AlignmentAnnotation[aSize
614 + command.seqs[s].getAnnotation().length];
616 System.arraycopy(annotations, 0, tmp, 0, aSize);
618 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
619 command.seqs[s].getAnnotation().length);
623 aSize = annotations.length;
627 if (annotations == null)
634 command.deletedAnnotations = new Hashtable();
639 for (int a = 0; a < annotations.length; a++)
641 if (annotations[a].autoCalculated
642 || annotations[a].annotations == null)
649 aSize = annotations[a].annotations.length;
652 temp = new Annotation[aSize + command.number];
653 if (annotations[a].padGaps)
654 for (int aa = 0; aa < temp.length; aa++)
656 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
661 if (command.position < aSize)
663 if (command.position + command.number >= aSize)
669 tSize = aSize - command.number;
681 temp = new Annotation[tSize];
686 if (command.position < annotations[a].annotations.length)
688 System.arraycopy(annotations[a].annotations, 0, temp, 0,
691 if (command.deletedAnnotations != null
692 && command.deletedAnnotations
693 .containsKey(annotations[a].annotationId))
695 Annotation[] restore = (Annotation[]) command.deletedAnnotations
696 .get(annotations[a].annotationId);
698 System.arraycopy(restore, 0, temp, command.position,
703 System.arraycopy(annotations[a].annotations, command.position,
704 temp, command.position + command.number, aSize
709 if (command.deletedAnnotations != null
710 && command.deletedAnnotations
711 .containsKey(annotations[a].annotationId))
713 Annotation[] restore = (Annotation[]) command.deletedAnnotations
714 .get(annotations[a].annotationId);
716 temp = new Annotation[annotations[a].annotations.length
718 System.arraycopy(annotations[a].annotations, 0, temp, 0,
719 annotations[a].annotations.length);
720 System.arraycopy(restore, 0, temp,
721 annotations[a].annotations.length, restore.length);
725 temp = annotations[a].annotations;
731 if (tSize != aSize || command.position < 2)
733 int copylen = Math.min(command.position,
734 annotations[a].annotations.length);
736 System.arraycopy(annotations[a].annotations, 0, temp, 0,
737 copylen); // command.position);
739 Annotation[] deleted = new Annotation[command.number];
740 if (copylen >= command.position)
742 copylen = Math.min(command.number,
743 annotations[a].annotations.length - command.position);
746 System.arraycopy(annotations[a].annotations,
747 command.position, deleted, 0, copylen); // command.number);
751 command.deletedAnnotations.put(annotations[a].annotationId,
753 if (annotations[a].annotations.length > command.position
756 System.arraycopy(annotations[a].annotations, command.position
757 + command.number, temp, command.position,
758 annotations[a].annotations.length - command.position
759 - command.number); // aSize
764 int dSize = aSize - command.position;
768 Annotation[] deleted = new Annotation[command.number];
769 System.arraycopy(annotations[a].annotations, command.position,
772 command.deletedAnnotations.put(annotations[a].annotationId,
775 tSize = Math.min(annotations[a].annotations.length,
777 temp = new Annotation[tSize];
778 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
782 temp = annotations[a].annotations;
787 annotations[a].annotations = temp;
791 final void adjustFeatures(Edit command, int index, int i, int j,
794 SequenceI seq = command.seqs[index];
795 SequenceI sequence = seq.getDatasetSequence();
796 if (sequence == null)
803 if (command.editedFeatures != null
804 && command.editedFeatures.containsKey(seq))
806 sequence.setSequenceFeatures((SequenceFeature[]) command.editedFeatures
813 SequenceFeature[] sf = sequence.getSequenceFeatures();
820 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
824 for (int s = 0; s < sf.length; s++)
826 SequenceFeature copy = new SequenceFeature(sf[s]);
830 if (sf[s].getEnd() < i)
835 if (sf[s].getBegin() > j)
837 sf[s].setBegin(copy.getBegin() - cSize);
838 sf[s].setEnd(copy.getEnd() - cSize);
842 if (sf[s].getBegin() >= i)
847 if (sf[s].getEnd() < j)
852 sf[s].setEnd(sf[s].getEnd() - (cSize));
854 if (sf[s].getBegin() > sf[s].getEnd())
856 sequence.deleteFeature(sf[s]);
860 if (command.editedFeatures == null)
862 command.editedFeatures = new Hashtable();
865 command.editedFeatures.put(seq, oldsf);
871 public SequenceI[] oldds;
873 boolean fullAlignmentHeight = false;
875 Hashtable deletedAnnotationRows;
877 Hashtable deletedAnnotations;
879 Hashtable editedFeatures;
891 int position, number;
895 Edit(int command, SequenceI[] seqs, int position, int number,
898 this.command = command;
900 this.position = position;
901 this.number = number;
902 this.gapChar = gapChar;
905 Edit(int command, SequenceI[] seqs, int position, int number,
908 this.gapChar = al.getGapCharacter();
909 this.command = command;
911 this.position = position;
912 this.number = number;
915 alIndex = new int[seqs.length];
916 for (int i = 0; i < seqs.length; i++)
918 alIndex[i] = al.findIndex(seqs[i]);
921 fullAlignmentHeight = (al.getHeight() == seqs.length);
924 Edit(int command, SequenceI[] seqs, int position, int number,
925 AlignmentI al, String replace)
927 this.command = command;
929 this.position = position;
930 this.number = number;
932 this.gapChar = al.getGapCharacter();
933 string = new char[seqs.length][];
934 for (int i = 0; i < seqs.length; i++)
936 string[i] = replace.toCharArray();
939 fullAlignmentHeight = (al.getHeight() == seqs.length);