2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.commands;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.Annotation;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
30 import java.util.ArrayList;
31 import java.util.Hashtable;
32 import java.util.List;
33 import java.util.ListIterator;
42 * Description: Essential information for performing undo and redo for cut/paste
43 * insert/delete gap which can be stored in the HistoryList
47 * Copyright: Copyright (c) 2006
51 * Company: Dundee University
54 * @author not attributable
57 public class EditCommand implements CommandI
61 INSERT_GAP, DELETE_GAP, CUT, PASTE, REPLACE, INSERT_NUC
64 private List<Edit> edits = new ArrayList<Edit>();
72 public EditCommand(String description)
74 this.description = description;
77 public EditCommand(String description, Action command, SequenceI[] seqs,
78 int position, int number, AlignmentI al)
80 this.description = description;
81 if (command == Action.CUT || command == Action.PASTE)
83 setEdit(new Edit(command, seqs, position, number, al));
89 public EditCommand(String description, Action command, String replace,
90 SequenceI[] seqs, int position, int number, AlignmentI al)
92 this.description = description;
93 if (command == Action.REPLACE)
95 setEdit(new Edit(command, seqs, position, number, al, replace));
102 * Set the list of edits to the specified item (only).
106 protected void setEdit(Edit e)
113 * Add the given edit command to the stored list of commands.
117 protected void addEdit(Edit e)
123 * Clear the list of stored edit commands.
126 protected void clearEdits()
132 * Returns the i'th stored Edit command.
137 protected Edit getEdit(int i)
139 if (i >= 0 && i < edits.size())
147 final public String getDescription()
159 * Return the alignment for the first edit (or null if no edit).
163 final public AlignmentI getAlignment()
165 return (edits.isEmpty() ? null : edits.get(0).al);
169 * append a new editCommand Note. this shouldn't be called if the edit is an
170 * operation affects more alignment objects than the one referenced in al (for
171 * example, cut or pasting whole sequences). Use the form with an additional
172 * AlignmentI[] views parameter.
181 final public void appendEdit(Action command, SequenceI[] seqs,
183 int number, AlignmentI al, boolean performEdit)
185 appendEdit(command, seqs, position, number, al, performEdit, null);
189 * append a new edit command with a set of alignment views that may be
200 final public void appendEdit(Action command, SequenceI[] seqs,
202 int number, AlignmentI al, boolean performEdit, AlignmentI[] views)
204 Edit edit = new Edit(command, seqs, position, number,
205 al.getGapCharacter());
206 if (al.getHeight() == seqs.length)
209 edit.fullAlignmentHeight = true;
216 performEdit(edit, views);
221 * Execute all the edit commands, starting at the given commandIndex
223 * @param commandIndex
226 final void performEdit(int commandIndex, AlignmentI[] views)
228 ListIterator<Edit> iterator = edits.listIterator(commandIndex);
229 while (iterator.hasNext())
231 Edit edit = iterator.next();
232 performEdit(edit, views);
237 * Execute one edit command in all the specified alignment views
242 protected void performEdit(Edit edit, AlignmentI[] views)
244 switch (edit.command)
262 // TODO:add deleteNuc for UNDO
264 // insertNuc(edits[e]);
272 final public void doCommand(AlignmentI[] views)
274 performEdit(0, views);
278 * Undo the stored list of commands, in reverse order.
281 final public void undoCommand(AlignmentI[] views)
283 ListIterator<Edit> iterator = edits.listIterator(edits.size());
284 while (iterator.hasPrevious())
286 Edit e = iterator.previous();
314 * Insert gap(s) in sequences as specified by the command, and adjust
319 final private void insertGap(Edit command)
322 for (int s = 0; s < command.seqs.length; s++)
324 command.seqs[s].insertCharAt(command.position, command.number,
326 // System.out.println("pos: "+command.position+" number: "+command.number);
329 adjustAnnotations(command, true, false, null);
333 // final void insertNuc(Edit command)
336 // for (int s = 0; s < command.seqs.length; s++)
338 // System.out.println("pos: "+command.position+" number: "+command.number);
339 // command.seqs[s].insertCharAt(command.position, command.number,'A');
342 // adjustAnnotations(command, true, false, null);
346 * Delete gap(s) in sequences as specified by the command, and adjust
351 final private void deleteGap(Edit command)
353 for (int s = 0; s < command.seqs.length; s++)
355 command.seqs[s].deleteChars(command.position, command.position
359 adjustAnnotations(command, false, false, null);
363 * Carry out a Cut action. The cut characters are saved in case Undo is
369 void cut(Edit command, AlignmentI[] views)
371 boolean seqDeleted = false;
372 command.string = new char[command.seqs.length][];
374 for (int i = 0; i < command.seqs.length; i++)
376 final SequenceI sequence = command.seqs[i];
377 if (sequence.getLength() > command.position)
379 command.string[i] = sequence.getSequence(command.position,
380 command.position + command.number);
381 SequenceI oldds = sequence.getDatasetSequence();
382 if (command.oldds != null && command.oldds[i] != null)
384 // we are redoing an undone cut.
385 sequence.setDatasetSequence(null);
387 sequence.deleteChars(command.position, command.position
389 if (command.oldds != null && command.oldds[i] != null)
391 // oldds entry contains the cut dataset sequence.
392 sequence.setDatasetSequence(command.oldds[i]);
393 command.oldds[i] = oldds;
397 // modify the oldds if necessary
398 if (oldds != sequence.getDatasetSequence()
399 || sequence.getSequenceFeatures() != null)
401 if (command.oldds == null)
403 command.oldds = new SequenceI[command.seqs.length];
405 command.oldds[i] = oldds;
409 sequence.findPosition(command.position),
410 sequence.findPosition(command.position
411 + command.number), false);
416 if (sequence.getLength() < 1)
418 command.al.deleteSequence(sequence);
423 adjustAnnotations(command, false, seqDeleted, views);
427 * Perform the given Paste command. This may be to add cut or copied sequences
428 * to an alignment, or to undo a 'Cut' action on a region of the alignment.
433 void paste(Edit command, AlignmentI[] views)
437 boolean newDSWasNeeded;
438 int newstart, newend;
439 boolean seqWasDeleted = false;
440 int start = 0, end = 0;
442 for (int i = 0; i < command.seqs.length; i++)
445 newDSWasNeeded = command.oldds != null && command.oldds[i] != null;
446 if (command.seqs[i].getLength() < 1)
448 // ie this sequence was deleted, we need to
449 // read it to the alignment
450 if (command.alIndex[i] < command.al.getHeight())
452 List<SequenceI> sequences;
453 synchronized (sequences = command.al.getSequences())
455 if (!(command.alIndex[i] < 0))
457 sequences.add(command.alIndex[i], command.seqs[i]);
463 command.al.addSequence(command.seqs[i]);
465 seqWasDeleted = true;
467 newstart = command.seqs[i].getStart();
468 newend = command.seqs[i].getEnd();
470 tmp = new StringBuffer();
471 tmp.append(command.seqs[i].getSequence());
472 // Undo of a delete does not replace original dataset sequence on to
473 // alignment sequence.
475 if (command.string != null && command.string[i] != null)
477 if (command.position >= tmp.length())
479 // This occurs if padding is on, and residues
480 // are removed from end of alignment
481 int length = command.position - tmp.length();
484 tmp.append(command.gapChar);
488 tmp.insert(command.position, command.string[i]);
489 for (int s = 0; s < command.string[i].length; s++)
491 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
496 start = command.seqs[i].findPosition(command.position);
497 end = command.seqs[i].findPosition(command.position
500 if (command.seqs[i].getStart() == start)
510 command.string[i] = null;
513 command.seqs[i].setSequence(tmp.toString());
514 command.seqs[i].setStart(newstart);
515 command.seqs[i].setEnd(newend);
518 if (command.seqs[i].getDatasetSequence() != null)
523 ds = command.oldds[i];
527 // make a new DS sequence
528 // use new ds mechanism here
529 ds = new Sequence(command.seqs[i].getName(),
530 jalview.analysis.AlignSeq.extractGaps(
531 jalview.util.Comparison.GapChars,
532 command.seqs[i].getSequenceAsString()),
533 command.seqs[i].getStart(), command.seqs[i].getEnd());
534 ds.setDescription(command.seqs[i].getDescription());
536 if (command.oldds == null)
538 command.oldds = new SequenceI[command.seqs.length];
540 command.oldds[i] = command.seqs[i].getDatasetSequence();
541 command.seqs[i].setDatasetSequence(ds);
543 adjustFeatures(command, i, start, end, true);
546 adjustAnnotations(command, true, seqWasDeleted, views);
548 command.string = null;
551 void replace(Edit command)
555 int start = command.position;
556 int end = command.number;
557 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
559 // TODO Jalview 2.4 bugfix change to an aggregate command - original
560 // sequence string is cut, new string is pasted in.
561 command.number = start + command.string[0].length;
562 for (int i = 0; i < command.seqs.length; i++)
564 boolean newDSWasNeeded = command.oldds != null
565 && command.oldds[i] != null;
568 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
569 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
570 * viewport.alignment));
574 * then addHistoryItem(new EditCommand( "Add sequences",
575 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
578 oldstring = command.seqs[i].getSequenceAsString();
579 tmp = new StringBuffer(oldstring.substring(0, start));
580 tmp.append(command.string[i]);
581 String nogaprep = jalview.analysis.AlignSeq.extractGaps(
582 jalview.util.Comparison.GapChars, new String(
584 int ipos = command.seqs[i].findPosition(start)
585 - command.seqs[i].getStart();
586 tmp.append(oldstring.substring(end));
587 command.seqs[i].setSequence(tmp.toString());
588 command.string[i] = oldstring.substring(start, end).toCharArray();
589 String nogapold = jalview.analysis.AlignSeq.extractGaps(
590 jalview.util.Comparison.GapChars, new String(
592 if (!nogaprep.toLowerCase().equals(nogapold.toLowerCase()))
596 SequenceI oldds = command.seqs[i].getDatasetSequence();
597 command.seqs[i].setDatasetSequence(command.oldds[i]);
598 command.oldds[i] = oldds;
602 if (command.oldds == null)
604 command.oldds = new SequenceI[command.seqs.length];
606 command.oldds[i] = command.seqs[i].getDatasetSequence();
607 SequenceI newds = new Sequence(
608 command.seqs[i].getDatasetSequence());
609 String fullseq, osp = newds.getSequenceAsString();
610 fullseq = osp.substring(0, ipos) + nogaprep
611 + osp.substring(ipos + nogaprep.length());
612 newds.setSequence(fullseq.toUpperCase());
613 // TODO: JAL-1131 ensure newly created dataset sequence is added to
615 // dataset sequences associated with the alignment.
616 // TODO: JAL-1131 fix up any annotation associated with new dataset
617 // sequence to ensure that original sequence/annotation relationships
619 command.seqs[i].setDatasetSequence(newds);
628 final void adjustAnnotations(Edit command, boolean insert,
629 boolean modifyVisibility, AlignmentI[] views)
631 AlignmentAnnotation[] annotations = null;
633 if (modifyVisibility && !insert)
635 // only occurs if a sequence was added or deleted.
636 command.deletedAnnotationRows = new Hashtable<SequenceI, AlignmentAnnotation[]>();
638 if (command.fullAlignmentHeight)
640 annotations = command.al.getAlignmentAnnotation();
645 AlignmentAnnotation[] tmp;
646 for (int s = 0; s < command.seqs.length; s++)
648 if (modifyVisibility)
650 // Rows are only removed or added to sequence object.
654 tmp = command.seqs[s].getAnnotation();
657 int alen = tmp.length;
658 for (int aa = 0; aa < tmp.length; aa++)
660 if (!command.al.deleteAnnotation(tmp[aa]))
662 // strip out annotation not in the current al (will be put
663 // back on insert in all views)
668 command.seqs[s].setAlignmentAnnotation(null);
669 if (alen != tmp.length)
671 // save the non-null annotation references only
672 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
673 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
677 saved[aapos++] = tmp[aa];
682 command.deletedAnnotationRows.put(command.seqs[s], saved);
683 // and then remove any annotation in the other views
684 for (int alview = 0; views != null && alview < views.length; alview++)
686 if (views[alview] != command.al)
688 AlignmentAnnotation[] toremove = views[alview]
689 .getAlignmentAnnotation();
690 if (toremove == null || toremove.length == 0)
694 // remove any alignment annotation on this sequence that's
695 // on that alignment view.
696 for (int aa = 0; aa < toremove.length; aa++)
698 if (toremove[aa].sequenceRef == command.seqs[s])
700 views[alview].deleteAnnotation(toremove[aa]);
708 // save all the annotation
709 command.deletedAnnotationRows.put(command.seqs[s], tmp);
716 if (command.deletedAnnotationRows != null
717 && command.deletedAnnotationRows
718 .containsKey(command.seqs[s]))
720 AlignmentAnnotation[] revealed = command.deletedAnnotationRows
721 .get(command.seqs[s]);
722 command.seqs[s].setAlignmentAnnotation(revealed);
723 if (revealed != null)
725 for (int aa = 0; aa < revealed.length; aa++)
727 // iterate through al adding original annotation
728 command.al.addAnnotation(revealed[aa]);
730 for (int aa = 0; aa < revealed.length; aa++)
732 command.al.setAnnotationIndex(revealed[aa], aa);
734 // and then duplicate added annotation on every other alignment
736 for (int vnum = 0; views != null && vnum < views.length; vnum++)
738 if (views[vnum] != command.al)
740 int avwidth = views[vnum].getWidth() + 1;
741 // duplicate in this view
742 for (int a = 0; a < revealed.length; a++)
744 AlignmentAnnotation newann = new AlignmentAnnotation(
746 command.seqs[s].addAlignmentAnnotation(newann);
747 newann.padAnnotation(avwidth);
748 views[vnum].addAnnotation(newann);
749 views[vnum].setAnnotationIndex(newann, a);
759 if (command.seqs[s].getAnnotation() == null)
766 annotations = command.seqs[s].getAnnotation();
770 tmp = new AlignmentAnnotation[aSize
771 + command.seqs[s].getAnnotation().length];
773 System.arraycopy(annotations, 0, tmp, 0, aSize);
775 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
776 command.seqs[s].getAnnotation().length);
780 aSize = annotations.length;
784 if (annotations == null)
791 command.deletedAnnotations = new Hashtable<String, Annotation[]>();
796 for (int a = 0; a < annotations.length; a++)
798 if (annotations[a].autoCalculated
799 || annotations[a].annotations == null)
806 aSize = annotations[a].annotations.length;
809 temp = new Annotation[aSize + command.number];
810 if (annotations[a].padGaps)
812 for (int aa = 0; aa < temp.length; aa++)
814 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
820 if (command.position < aSize)
822 if (command.position + command.number >= aSize)
828 tSize = aSize - command.number;
840 temp = new Annotation[tSize];
845 if (command.position < annotations[a].annotations.length)
847 System.arraycopy(annotations[a].annotations, 0, temp, 0,
850 if (command.deletedAnnotations != null
851 && command.deletedAnnotations
852 .containsKey(annotations[a].annotationId))
854 Annotation[] restore = command.deletedAnnotations
855 .get(annotations[a].annotationId);
857 System.arraycopy(restore, 0, temp, command.position,
862 System.arraycopy(annotations[a].annotations, command.position,
863 temp, command.position + command.number, aSize
868 if (command.deletedAnnotations != null
869 && command.deletedAnnotations
870 .containsKey(annotations[a].annotationId))
872 Annotation[] restore = command.deletedAnnotations
873 .get(annotations[a].annotationId);
875 temp = new Annotation[annotations[a].annotations.length
877 System.arraycopy(annotations[a].annotations, 0, temp, 0,
878 annotations[a].annotations.length);
879 System.arraycopy(restore, 0, temp,
880 annotations[a].annotations.length, restore.length);
884 temp = annotations[a].annotations;
890 if (tSize != aSize || command.position < 2)
892 int copylen = Math.min(command.position,
893 annotations[a].annotations.length);
896 System.arraycopy(annotations[a].annotations, 0, temp, 0,
897 copylen); // command.position);
900 Annotation[] deleted = new Annotation[command.number];
901 if (copylen >= command.position)
903 copylen = Math.min(command.number,
904 annotations[a].annotations.length - command.position);
907 System.arraycopy(annotations[a].annotations,
908 command.position, deleted, 0, copylen); // command.number);
912 command.deletedAnnotations.put(annotations[a].annotationId,
914 if (annotations[a].annotations.length > command.position
917 System.arraycopy(annotations[a].annotations, command.position
918 + command.number, temp, command.position,
919 annotations[a].annotations.length - command.position
920 - command.number); // aSize
925 int dSize = aSize - command.position;
929 Annotation[] deleted = new Annotation[command.number];
930 System.arraycopy(annotations[a].annotations, command.position,
933 command.deletedAnnotations.put(annotations[a].annotationId,
936 tSize = Math.min(annotations[a].annotations.length,
938 temp = new Annotation[tSize];
939 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
943 temp = annotations[a].annotations;
948 annotations[a].annotations = temp;
952 final void adjustFeatures(Edit command, int index, int i, int j,
955 SequenceI seq = command.seqs[index];
956 SequenceI sequence = seq.getDatasetSequence();
957 if (sequence == null)
964 if (command.editedFeatures != null
965 && command.editedFeatures.containsKey(seq))
967 sequence.setSequenceFeatures(command.editedFeatures
974 SequenceFeature[] sf = sequence.getSequenceFeatures();
981 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
985 for (int s = 0; s < sf.length; s++)
987 SequenceFeature copy = new SequenceFeature(sf[s]);
991 if (sf[s].getEnd() < i)
996 if (sf[s].getBegin() > j)
998 sf[s].setBegin(copy.getBegin() - cSize);
999 sf[s].setEnd(copy.getEnd() - cSize);
1003 if (sf[s].getBegin() >= i)
1008 if (sf[s].getEnd() < j)
1010 sf[s].setEnd(j - 1);
1013 sf[s].setEnd(sf[s].getEnd() - (cSize));
1015 if (sf[s].getBegin() > sf[s].getEnd())
1017 sequence.deleteFeature(sf[s]);
1021 if (command.editedFeatures == null)
1023 command.editedFeatures = new Hashtable<SequenceI, SequenceFeature[]>();
1026 command.editedFeatures.put(seq, oldsf);
1032 public SequenceI[] oldds;
1034 boolean fullAlignmentHeight = false;
1036 Hashtable<SequenceI, AlignmentAnnotation[]> deletedAnnotationRows;
1038 Hashtable<String, Annotation[]> deletedAnnotations;
1040 Hashtable<SequenceI, SequenceFeature[]> editedFeatures;
1052 int position, number;
1056 Edit(Action command, SequenceI[] seqs, int position, int number,
1059 this.command = command;
1061 this.position = position;
1062 this.number = number;
1063 this.gapChar = gapChar;
1066 Edit(Action command, SequenceI[] seqs, int position, int number,
1069 this.gapChar = al.getGapCharacter();
1070 this.command = command;
1072 this.position = position;
1073 this.number = number;
1076 alIndex = new int[seqs.length];
1077 for (int i = 0; i < seqs.length; i++)
1079 alIndex[i] = al.findIndex(seqs[i]);
1082 fullAlignmentHeight = (al.getHeight() == seqs.length);
1085 Edit(Action command, SequenceI[] seqs, int position, int number,
1086 AlignmentI al, String replace)
1088 this.command = command;
1090 this.position = position;
1091 this.number = number;
1093 this.gapChar = al.getGapCharacter();
1094 string = new char[seqs.length][];
1095 for (int i = 0; i < seqs.length; i++)
1097 string[i] = replace.toCharArray();
1100 fullAlignmentHeight = (al.getHeight() == seqs.length);