2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
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, al
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;
289 adjustFeatures(command, i, command.seqs[i]
290 .findPosition(command.position), command.seqs[i]
291 .findPosition(command.position + command.number), false);
296 if (command.seqs[i].getLength() < 1)
298 command.al.deleteSequence(command.seqs[i]);
303 adjustAnnotations(command, false, seqDeleted, views);
306 void paste(Edit command, AlignmentI[] views)
310 boolean newDSWasNeeded;
311 int newstart, newend;
312 boolean seqWasDeleted = false;
313 int start = 0, end = 0;
315 for (int i = 0; i < command.seqs.length; i++)
318 newDSWasNeeded = command.oldds != null && command.oldds[i] != null;
319 if (command.seqs[i].getLength() < 1)
321 // ie this sequence was deleted, we need to
322 // read it to the alignment
323 if (command.alIndex[i] < command.al.getHeight())
325 command.al.getSequences().insertElementAt(command.seqs[i],
330 command.al.addSequence(command.seqs[i]);
332 seqWasDeleted = true;
334 newstart = command.seqs[i].getStart();
335 newend = command.seqs[i].getEnd();
337 tmp = new StringBuffer();
338 tmp.append(command.seqs[i].getSequence());
339 // Undo of a delete does not replace original dataset sequence on to
340 // alignment sequence.
342 if (command.string != null && command.string[i] != null)
344 if (command.position >= tmp.length())
346 // This occurs if padding is on, and residues
347 // are removed from end of alignment
348 int length = command.position - tmp.length();
351 tmp.append(command.gapChar);
355 tmp.insert(command.position, command.string[i]);
356 for (int s = 0; s < command.string[i].length; s++)
358 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
363 start = command.seqs[i].findPosition(command.position);
364 end = command.seqs[i].findPosition(command.position
367 if (command.seqs[i].getStart() == start)
373 command.string[i] = null;
376 command.seqs[i].setSequence(tmp.toString());
377 command.seqs[i].setStart(newstart);
378 command.seqs[i].setEnd(newend);
381 if (command.seqs[i].getDatasetSequence() != null)
386 ds = command.oldds[i];
390 // make a new DS sequence
391 // use new ds mechanism here
392 ds = new Sequence(command.seqs[i].getName(),
393 jalview.analysis.AlignSeq.extractGaps(
394 jalview.util.Comparison.GapChars,
395 command.seqs[i].getSequenceAsString()),
396 command.seqs[i].getStart(), command.seqs[i].getEnd());
397 ds.setDescription(command.seqs[i].getDescription());
399 if (command.oldds == null)
401 command.oldds = new SequenceI[command.seqs.length];
403 command.oldds[i] = command.seqs[i].getDatasetSequence();
404 command.seqs[i].setDatasetSequence(ds);
406 adjustFeatures(command, i, start, end, true);
409 adjustAnnotations(command, true, seqWasDeleted, views);
411 command.string = null;
414 void replace(Edit command)
418 int start = command.position;
419 int end = command.number;
420 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
422 // TODO Jalview 2.4 bugfix change to an aggregate command - original
423 // sequence string is cut, new string is pasted in.
424 command.number = start + command.string[0].length;
425 for (int i = 0; i < command.seqs.length; i++)
428 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
429 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
430 * viewport.alignment));
434 * then addHistoryItem(new EditCommand( "Add sequences",
435 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
438 oldstring = command.seqs[i].getSequenceAsString();
439 tmp = new StringBuffer(oldstring.substring(0, start));
440 tmp.append(command.string[i]);
441 tmp.append(oldstring.substring(end));
442 command.seqs[i].setSequence(tmp.toString());
443 command.string[i] = oldstring.substring(start, end).toCharArray();
449 final void adjustAnnotations(Edit command, boolean insert,
450 boolean modifyVisibility, AlignmentI[] views)
452 AlignmentAnnotation[] annotations = null;
454 if (modifyVisibility && !insert)
456 // only occurs if a sequence was added or deleted.
457 command.deletedAnnotationRows = new Hashtable();
459 if (command.fullAlignmentHeight)
461 annotations = command.al.getAlignmentAnnotation();
466 AlignmentAnnotation[] tmp;
467 for (int s = 0; s < command.seqs.length; s++)
469 if (modifyVisibility)
471 // Rows are only removed or added to sequence object.
475 tmp = command.seqs[s].getAnnotation();
478 int alen = tmp.length;
479 for (int aa = 0; aa < tmp.length; aa++)
481 if (!command.al.deleteAnnotation(tmp[aa]))
483 // strip out annotation not in the current al (will be put
484 // back on insert in all views)
489 command.seqs[s].setAlignmentAnnotation(null);
490 if (alen != tmp.length)
492 // save the non-null annotation references only
493 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
494 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
498 saved[aapos++] = tmp[aa];
503 command.deletedAnnotationRows.put(command.seqs[s], saved);
504 // and then remove any annotation in the other views
505 for (int alview = 0; views != null && alview < views.length; alview++)
507 if (views[alview] != command.al)
509 AlignmentAnnotation[] toremove = views[alview]
510 .getAlignmentAnnotation();
511 if (toremove == null || toremove.length == 0)
515 // remove any alignment annotation on this sequence that's
516 // on that alignment view.
517 for (int aa = 0; aa < toremove.length; aa++)
519 if (toremove[aa].sequenceRef == command.seqs[s])
521 views[alview].deleteAnnotation(toremove[aa]);
529 // save all the annotation
530 command.deletedAnnotationRows.put(command.seqs[s], tmp);
537 if (command.deletedAnnotationRows != null
538 && command.deletedAnnotationRows
539 .containsKey(command.seqs[s]))
541 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
542 .get(command.seqs[s]);
543 command.seqs[s].setAlignmentAnnotation(revealed);
544 if (revealed != null)
546 for (int aa = 0; aa < revealed.length; aa++)
548 // iterate through al adding original annotation
549 command.al.addAnnotation(revealed[aa]);
551 for (int aa = 0; aa < revealed.length; aa++)
553 command.al.setAnnotationIndex(revealed[aa], aa);
555 // and then duplicate added annotation on every other alignment
557 for (int vnum = 0; views != null && vnum < views.length; vnum++)
559 if (views[vnum] != command.al)
561 int avwidth = views[vnum].getWidth() + 1;
562 // duplicate in this view
563 for (int a = 0; a < revealed.length; a++)
565 AlignmentAnnotation newann = new AlignmentAnnotation(
567 command.seqs[s].addAlignmentAnnotation(newann);
568 newann.padAnnotation(avwidth);
569 views[vnum].addAnnotation(newann);
570 views[vnum].setAnnotationIndex(newann, a);
580 if (command.seqs[s].getAnnotation() == null)
587 annotations = command.seqs[s].getAnnotation();
591 tmp = new AlignmentAnnotation[aSize
592 + command.seqs[s].getAnnotation().length];
594 System.arraycopy(annotations, 0, tmp, 0, aSize);
596 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
597 command.seqs[s].getAnnotation().length);
601 aSize = annotations.length;
605 if (annotations == null)
612 command.deletedAnnotations = new Hashtable();
617 for (int a = 0; a < annotations.length; a++)
619 if (annotations[a].autoCalculated
620 || annotations[a].annotations == null)
627 aSize = annotations[a].annotations.length;
630 temp = new Annotation[aSize + command.number];
631 if (annotations[a].padGaps)
632 for (int aa = 0; aa < temp.length; aa++)
634 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
639 if (command.position < aSize)
641 if (command.position + command.number >= aSize)
647 tSize = aSize - command.number;
659 temp = new Annotation[tSize];
664 if (command.position < annotations[a].annotations.length)
666 System.arraycopy(annotations[a].annotations, 0, temp, 0,
669 if (command.deletedAnnotations != null
670 && command.deletedAnnotations
671 .containsKey(annotations[a].annotationId))
673 Annotation[] restore = (Annotation[]) command.deletedAnnotations
674 .get(annotations[a].annotationId);
676 System.arraycopy(restore, 0, temp, command.position,
681 System.arraycopy(annotations[a].annotations, command.position,
682 temp, command.position + command.number, aSize
687 if (command.deletedAnnotations != null
688 && command.deletedAnnotations
689 .containsKey(annotations[a].annotationId))
691 Annotation[] restore = (Annotation[]) command.deletedAnnotations
692 .get(annotations[a].annotationId);
694 temp = new Annotation[annotations[a].annotations.length
696 System.arraycopy(annotations[a].annotations, 0, temp, 0,
697 annotations[a].annotations.length);
698 System.arraycopy(restore, 0, temp,
699 annotations[a].annotations.length, restore.length);
703 temp = annotations[a].annotations;
709 if (tSize != aSize || command.position < 2)
711 int copylen = Math.min(command.position,
712 annotations[a].annotations.length);
714 System.arraycopy(annotations[a].annotations, 0, temp, 0,
715 copylen); // command.position);
717 Annotation[] deleted = new Annotation[command.number];
718 if (copylen >= command.position)
720 copylen = Math.min(command.number,
721 annotations[a].annotations.length - command.position);
724 System.arraycopy(annotations[a].annotations,
725 command.position, deleted, 0, copylen); // command.number);
729 command.deletedAnnotations.put(annotations[a].annotationId,
731 if (annotations[a].annotations.length > command.position
734 System.arraycopy(annotations[a].annotations, command.position
735 + command.number, temp, command.position,
736 annotations[a].annotations.length - command.position
737 - command.number); // aSize
742 int dSize = aSize - command.position;
746 Annotation[] deleted = new Annotation[command.number];
747 System.arraycopy(annotations[a].annotations, command.position,
750 command.deletedAnnotations.put(annotations[a].annotationId,
753 tSize = Math.min(annotations[a].annotations.length,
755 temp = new Annotation[tSize];
756 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
760 temp = annotations[a].annotations;
765 annotations[a].annotations = temp;
769 final void adjustFeatures(Edit command, int index, int i, int j,
772 SequenceI seq = command.seqs[index];
773 SequenceI sequence = seq.getDatasetSequence();
774 if (sequence == null)
781 if (command.editedFeatures != null
782 && command.editedFeatures.containsKey(seq))
785 .setSequenceFeatures((SequenceFeature[]) command.editedFeatures
792 SequenceFeature[] sf = sequence.getSequenceFeatures();
799 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
803 for (int s = 0; s < sf.length; s++)
805 SequenceFeature copy = new SequenceFeature(sf[s]);
809 if (sf[s].getEnd() < i)
814 if (sf[s].getBegin() > j)
816 sf[s].setBegin(copy.getBegin() - cSize);
817 sf[s].setEnd(copy.getEnd() - cSize);
821 if (sf[s].getBegin() >= i)
826 if (sf[s].getEnd() < j)
831 sf[s].setEnd(sf[s].getEnd() - (cSize));
833 if (sf[s].getBegin() > sf[s].getEnd())
835 sequence.deleteFeature(sf[s]);
839 if (command.editedFeatures == null)
841 command.editedFeatures = new Hashtable();
844 command.editedFeatures.put(seq, oldsf);
850 public SequenceI[] oldds;
852 boolean fullAlignmentHeight = false;
854 Hashtable deletedAnnotationRows;
856 Hashtable deletedAnnotations;
858 Hashtable editedFeatures;
870 int position, number;
874 Edit(int command, SequenceI[] seqs, int position, int number,
877 this.command = command;
879 this.position = position;
880 this.number = number;
881 this.gapChar = gapChar;
884 Edit(int command, SequenceI[] seqs, int position, int number,
887 this.gapChar = al.getGapCharacter();
888 this.command = command;
890 this.position = position;
891 this.number = number;
894 alIndex = new int[seqs.length];
895 for (int i = 0; i < seqs.length; i++)
897 alIndex[i] = al.findIndex(seqs[i]);
900 fullAlignmentHeight = (al.getHeight() == seqs.length);
903 Edit(int command, SequenceI[] seqs, int position, int number,
904 AlignmentI al, String replace)
906 this.command = command;
908 this.position = position;
909 this.number = number;
911 this.gapChar = al.getGapCharacter();
912 string = new char[seqs.length][];
913 for (int i = 0; i < seqs.length; i++)
915 string[i] = replace.toCharArray();
918 fullAlignmentHeight = (al.getHeight() == seqs.length);