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 List<SequenceI> sequences;
348 synchronized (sequences=command.al.getSequences()) {
349 sequences.add(command.alIndex[i], command.seqs[i]);
354 command.al.addSequence(command.seqs[i]);
356 seqWasDeleted = true;
358 newstart = command.seqs[i].getStart();
359 newend = command.seqs[i].getEnd();
361 tmp = new StringBuffer();
362 tmp.append(command.seqs[i].getSequence());
363 // Undo of a delete does not replace original dataset sequence on to
364 // alignment sequence.
366 if (command.string != null && command.string[i] != null)
368 if (command.position >= tmp.length())
370 // This occurs if padding is on, and residues
371 // are removed from end of alignment
372 int length = command.position - tmp.length();
375 tmp.append(command.gapChar);
379 tmp.insert(command.position, command.string[i]);
380 for (int s = 0; s < command.string[i].length; s++)
382 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
387 start = command.seqs[i].findPosition(command.position);
388 end = command.seqs[i].findPosition(command.position
391 if (command.seqs[i].getStart() == start)
397 command.string[i] = null;
400 command.seqs[i].setSequence(tmp.toString());
401 command.seqs[i].setStart(newstart);
402 command.seqs[i].setEnd(newend);
405 if (command.seqs[i].getDatasetSequence() != null)
410 ds = command.oldds[i];
414 // make a new DS sequence
415 // use new ds mechanism here
416 ds = new Sequence(command.seqs[i].getName(),
417 jalview.analysis.AlignSeq.extractGaps(
418 jalview.util.Comparison.GapChars,
419 command.seqs[i].getSequenceAsString()),
420 command.seqs[i].getStart(), command.seqs[i].getEnd());
421 ds.setDescription(command.seqs[i].getDescription());
423 if (command.oldds == null)
425 command.oldds = new SequenceI[command.seqs.length];
427 command.oldds[i] = command.seqs[i].getDatasetSequence();
428 command.seqs[i].setDatasetSequence(ds);
430 adjustFeatures(command, i, start, end, true);
433 adjustAnnotations(command, true, seqWasDeleted, views);
435 command.string = null;
438 void replace(Edit command)
442 int start = command.position;
443 int end = command.number;
444 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
446 // TODO Jalview 2.4 bugfix change to an aggregate command - original
447 // sequence string is cut, new string is pasted in.
448 command.number = start + command.string[0].length;
449 for (int i = 0; i < command.seqs.length; i++)
452 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
453 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
454 * viewport.alignment));
458 * then addHistoryItem(new EditCommand( "Add sequences",
459 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
462 oldstring = command.seqs[i].getSequenceAsString();
463 tmp = new StringBuffer(oldstring.substring(0, start));
464 tmp.append(command.string[i]);
465 tmp.append(oldstring.substring(end));
466 command.seqs[i].setSequence(tmp.toString());
467 command.string[i] = oldstring.substring(start, end).toCharArray();
473 final void adjustAnnotations(Edit command, boolean insert,
474 boolean modifyVisibility, AlignmentI[] views)
476 AlignmentAnnotation[] annotations = null;
478 if (modifyVisibility && !insert)
480 // only occurs if a sequence was added or deleted.
481 command.deletedAnnotationRows = new Hashtable();
483 if (command.fullAlignmentHeight)
485 annotations = command.al.getAlignmentAnnotation();
490 AlignmentAnnotation[] tmp;
491 for (int s = 0; s < command.seqs.length; s++)
493 if (modifyVisibility)
495 // Rows are only removed or added to sequence object.
499 tmp = command.seqs[s].getAnnotation();
502 int alen = tmp.length;
503 for (int aa = 0; aa < tmp.length; aa++)
505 if (!command.al.deleteAnnotation(tmp[aa]))
507 // strip out annotation not in the current al (will be put
508 // back on insert in all views)
513 command.seqs[s].setAlignmentAnnotation(null);
514 if (alen != tmp.length)
516 // save the non-null annotation references only
517 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
518 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
522 saved[aapos++] = tmp[aa];
527 command.deletedAnnotationRows.put(command.seqs[s], saved);
528 // and then remove any annotation in the other views
529 for (int alview = 0; views != null && alview < views.length; alview++)
531 if (views[alview] != command.al)
533 AlignmentAnnotation[] toremove = views[alview]
534 .getAlignmentAnnotation();
535 if (toremove == null || toremove.length == 0)
539 // remove any alignment annotation on this sequence that's
540 // on that alignment view.
541 for (int aa = 0; aa < toremove.length; aa++)
543 if (toremove[aa].sequenceRef == command.seqs[s])
545 views[alview].deleteAnnotation(toremove[aa]);
553 // save all the annotation
554 command.deletedAnnotationRows.put(command.seqs[s], tmp);
561 if (command.deletedAnnotationRows != null
562 && command.deletedAnnotationRows
563 .containsKey(command.seqs[s]))
565 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
566 .get(command.seqs[s]);
567 command.seqs[s].setAlignmentAnnotation(revealed);
568 if (revealed != null)
570 for (int aa = 0; aa < revealed.length; aa++)
572 // iterate through al adding original annotation
573 command.al.addAnnotation(revealed[aa]);
575 for (int aa = 0; aa < revealed.length; aa++)
577 command.al.setAnnotationIndex(revealed[aa], aa);
579 // and then duplicate added annotation on every other alignment
581 for (int vnum = 0; views != null && vnum < views.length; vnum++)
583 if (views[vnum] != command.al)
585 int avwidth = views[vnum].getWidth() + 1;
586 // duplicate in this view
587 for (int a = 0; a < revealed.length; a++)
589 AlignmentAnnotation newann = new AlignmentAnnotation(
591 command.seqs[s].addAlignmentAnnotation(newann);
592 newann.padAnnotation(avwidth);
593 views[vnum].addAnnotation(newann);
594 views[vnum].setAnnotationIndex(newann, a);
604 if (command.seqs[s].getAnnotation() == null)
611 annotations = command.seqs[s].getAnnotation();
615 tmp = new AlignmentAnnotation[aSize
616 + command.seqs[s].getAnnotation().length];
618 System.arraycopy(annotations, 0, tmp, 0, aSize);
620 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
621 command.seqs[s].getAnnotation().length);
625 aSize = annotations.length;
629 if (annotations == null)
636 command.deletedAnnotations = new Hashtable();
641 for (int a = 0; a < annotations.length; a++)
643 if (annotations[a].autoCalculated
644 || annotations[a].annotations == null)
651 aSize = annotations[a].annotations.length;
654 temp = new Annotation[aSize + command.number];
655 if (annotations[a].padGaps)
656 for (int aa = 0; aa < temp.length; aa++)
658 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
663 if (command.position < aSize)
665 if (command.position + command.number >= aSize)
671 tSize = aSize - command.number;
683 temp = new Annotation[tSize];
688 if (command.position < annotations[a].annotations.length)
690 System.arraycopy(annotations[a].annotations, 0, temp, 0,
693 if (command.deletedAnnotations != null
694 && command.deletedAnnotations
695 .containsKey(annotations[a].annotationId))
697 Annotation[] restore = (Annotation[]) command.deletedAnnotations
698 .get(annotations[a].annotationId);
700 System.arraycopy(restore, 0, temp, command.position,
705 System.arraycopy(annotations[a].annotations, command.position,
706 temp, command.position + command.number, aSize
711 if (command.deletedAnnotations != null
712 && command.deletedAnnotations
713 .containsKey(annotations[a].annotationId))
715 Annotation[] restore = (Annotation[]) command.deletedAnnotations
716 .get(annotations[a].annotationId);
718 temp = new Annotation[annotations[a].annotations.length
720 System.arraycopy(annotations[a].annotations, 0, temp, 0,
721 annotations[a].annotations.length);
722 System.arraycopy(restore, 0, temp,
723 annotations[a].annotations.length, restore.length);
727 temp = annotations[a].annotations;
733 if (tSize != aSize || command.position < 2)
735 int copylen = Math.min(command.position,
736 annotations[a].annotations.length);
738 System.arraycopy(annotations[a].annotations, 0, temp, 0,
739 copylen); // command.position);
741 Annotation[] deleted = new Annotation[command.number];
742 if (copylen >= command.position)
744 copylen = Math.min(command.number,
745 annotations[a].annotations.length - command.position);
748 System.arraycopy(annotations[a].annotations,
749 command.position, deleted, 0, copylen); // command.number);
753 command.deletedAnnotations.put(annotations[a].annotationId,
755 if (annotations[a].annotations.length > command.position
758 System.arraycopy(annotations[a].annotations, command.position
759 + command.number, temp, command.position,
760 annotations[a].annotations.length - command.position
761 - command.number); // aSize
766 int dSize = aSize - command.position;
770 Annotation[] deleted = new Annotation[command.number];
771 System.arraycopy(annotations[a].annotations, command.position,
774 command.deletedAnnotations.put(annotations[a].annotationId,
777 tSize = Math.min(annotations[a].annotations.length,
779 temp = new Annotation[tSize];
780 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
784 temp = annotations[a].annotations;
789 annotations[a].annotations = temp;
793 final void adjustFeatures(Edit command, int index, int i, int j,
796 SequenceI seq = command.seqs[index];
797 SequenceI sequence = seq.getDatasetSequence();
798 if (sequence == null)
805 if (command.editedFeatures != null
806 && command.editedFeatures.containsKey(seq))
808 sequence.setSequenceFeatures((SequenceFeature[]) command.editedFeatures
815 SequenceFeature[] sf = sequence.getSequenceFeatures();
822 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
826 for (int s = 0; s < sf.length; s++)
828 SequenceFeature copy = new SequenceFeature(sf[s]);
832 if (sf[s].getEnd() < i)
837 if (sf[s].getBegin() > j)
839 sf[s].setBegin(copy.getBegin() - cSize);
840 sf[s].setEnd(copy.getEnd() - cSize);
844 if (sf[s].getBegin() >= i)
849 if (sf[s].getEnd() < j)
854 sf[s].setEnd(sf[s].getEnd() - (cSize));
856 if (sf[s].getBegin() > sf[s].getEnd())
858 sequence.deleteFeature(sf[s]);
862 if (command.editedFeatures == null)
864 command.editedFeatures = new Hashtable();
867 command.editedFeatures.put(seq, oldsf);
873 public SequenceI[] oldds;
875 boolean fullAlignmentHeight = false;
877 Hashtable deletedAnnotationRows;
879 Hashtable deletedAnnotations;
881 Hashtable editedFeatures;
893 int position, number;
897 Edit(int command, SequenceI[] seqs, int position, int number,
900 this.command = command;
902 this.position = position;
903 this.number = number;
904 this.gapChar = gapChar;
907 Edit(int command, SequenceI[] seqs, int position, int number,
910 this.gapChar = al.getGapCharacter();
911 this.command = command;
913 this.position = position;
914 this.number = number;
917 alIndex = new int[seqs.length];
918 for (int i = 0; i < seqs.length; i++)
920 alIndex[i] = al.findIndex(seqs[i]);
923 fullAlignmentHeight = (al.getHeight() == seqs.length);
926 Edit(int command, SequenceI[] seqs, int position, int number,
927 AlignmentI al, String replace)
929 this.command = command;
931 this.position = position;
932 this.number = number;
934 this.gapChar = al.getGapCharacter();
935 string = new char[seqs.length][];
936 for (int i = 0; i < seqs.length; i++)
938 string[i] = replace.toCharArray();
941 fullAlignmentHeight = (al.getHeight() == seqs.length);