2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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);
254 // final void insertNuc(Edit command)
257 // for (int s = 0; s < command.seqs.length; s++)
259 // System.out.println("pos: "+command.position+" number: "+command.number);
260 // command.seqs[s].insertCharAt(command.position, command.number,'A');
263 // adjustAnnotations(command, true, false, null);
266 final void deleteGap(Edit command)
268 for (int s = 0; s < command.seqs.length; s++)
270 command.seqs[s].deleteChars(command.position, command.position
274 adjustAnnotations(command, false, false, null);
277 void cut(Edit command, AlignmentI[] views)
279 boolean seqDeleted = false;
280 command.string = new char[command.seqs.length][];
282 for (int i = 0; i < command.seqs.length; i++)
284 if (command.seqs[i].getLength() > command.position)
286 command.string[i] = command.seqs[i].getSequence(command.position,
287 command.position + command.number);
288 SequenceI oldds = command.seqs[i].getDatasetSequence();
289 if (command.oldds != null && command.oldds[i] != null)
291 // we are redoing an undone cut.
292 command.seqs[i].setDatasetSequence(null);
294 command.seqs[i].deleteChars(command.position, command.position
296 if (command.oldds != null && command.oldds[i] != null)
298 // oldds entry contains the cut dataset sequence.
299 command.seqs[i].setDatasetSequence(command.oldds[i]);
300 command.oldds[i] = oldds;
304 // modify the oldds if necessary
305 if (oldds != command.seqs[i].getDatasetSequence()
306 || command.seqs[i].getSequenceFeatures() != null)
308 if (command.oldds == null)
310 command.oldds = new SequenceI[command.seqs.length];
312 command.oldds[i] = oldds;
316 command.seqs[i].findPosition(command.position),
317 command.seqs[i].findPosition(command.position
318 + command.number), false);
323 if (command.seqs[i].getLength() < 1)
325 command.al.deleteSequence(command.seqs[i]);
330 adjustAnnotations(command, false, seqDeleted, views);
333 void paste(Edit command, AlignmentI[] views)
337 boolean newDSWasNeeded;
338 int newstart, newend;
339 boolean seqWasDeleted = false;
340 int start = 0, end = 0;
342 for (int i = 0; i < command.seqs.length; i++)
345 newDSWasNeeded = command.oldds != null && command.oldds[i] != null;
346 if (command.seqs[i].getLength() < 1)
348 // ie this sequence was deleted, we need to
349 // read it to the alignment
350 if (command.alIndex[i] < command.al.getHeight())
352 List<SequenceI> sequences;
353 synchronized (sequences = command.al.getSequences())
355 sequences.add(command.alIndex[i], command.seqs[i]);
360 command.al.addSequence(command.seqs[i]);
362 seqWasDeleted = true;
364 newstart = command.seqs[i].getStart();
365 newend = command.seqs[i].getEnd();
367 tmp = new StringBuffer();
368 tmp.append(command.seqs[i].getSequence());
369 // Undo of a delete does not replace original dataset sequence on to
370 // alignment sequence.
372 if (command.string != null && command.string[i] != null)
374 if (command.position >= tmp.length())
376 // This occurs if padding is on, and residues
377 // are removed from end of alignment
378 int length = command.position - tmp.length();
381 tmp.append(command.gapChar);
385 tmp.insert(command.position, command.string[i]);
386 for (int s = 0; s < command.string[i].length; s++)
388 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
393 start = command.seqs[i].findPosition(command.position);
394 end = command.seqs[i].findPosition(command.position
397 if (command.seqs[i].getStart() == start)
403 command.string[i] = null;
406 command.seqs[i].setSequence(tmp.toString());
407 command.seqs[i].setStart(newstart);
408 command.seqs[i].setEnd(newend);
411 if (command.seqs[i].getDatasetSequence() != null)
416 ds = command.oldds[i];
420 // make a new DS sequence
421 // use new ds mechanism here
422 ds = new Sequence(command.seqs[i].getName(),
423 jalview.analysis.AlignSeq.extractGaps(
424 jalview.util.Comparison.GapChars,
425 command.seqs[i].getSequenceAsString()),
426 command.seqs[i].getStart(), command.seqs[i].getEnd());
427 ds.setDescription(command.seqs[i].getDescription());
429 if (command.oldds == null)
431 command.oldds = new SequenceI[command.seqs.length];
433 command.oldds[i] = command.seqs[i].getDatasetSequence();
434 command.seqs[i].setDatasetSequence(ds);
436 adjustFeatures(command, i, start, end, true);
439 adjustAnnotations(command, true, seqWasDeleted, views);
441 command.string = null;
444 void replace(Edit command)
448 int start = command.position;
449 int end = command.number;
450 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
452 // TODO Jalview 2.4 bugfix change to an aggregate command - original
453 // sequence string is cut, new string is pasted in.
454 command.number = start + command.string[0].length;
455 for (int i = 0; i < command.seqs.length; i++)
457 boolean newDSWasNeeded = command.oldds != null
458 && command.oldds[i] != null;
461 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
462 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
463 * viewport.alignment));
467 * then addHistoryItem(new EditCommand( "Add sequences",
468 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
471 oldstring = command.seqs[i].getSequenceAsString();
472 tmp = new StringBuffer(oldstring.substring(0, start));
473 tmp.append(command.string[i]);
474 String nogaprep = jalview.analysis.AlignSeq.extractGaps(
475 jalview.util.Comparison.GapChars, new String(
477 int ipos = command.seqs[i].findPosition(start)
478 - command.seqs[i].getStart();
479 tmp.append(oldstring.substring(end));
480 command.seqs[i].setSequence(tmp.toString());
481 command.string[i] = oldstring.substring(start, end).toCharArray();
482 String nogapold = jalview.analysis.AlignSeq.extractGaps(
483 jalview.util.Comparison.GapChars, new String(
485 if (!nogaprep.toLowerCase().equals(nogapold.toLowerCase()))
489 SequenceI oldds = command.seqs[i].getDatasetSequence();
490 command.seqs[i].setDatasetSequence(command.oldds[i]);
491 command.oldds[i] = oldds;
495 if (command.oldds == null)
497 command.oldds = new SequenceI[command.seqs.length];
499 command.oldds[i] = command.seqs[i].getDatasetSequence();
500 SequenceI newds = new Sequence(
501 command.seqs[i].getDatasetSequence());
502 String fullseq, osp = newds.getSequenceAsString();
503 fullseq = osp.substring(0, ipos) + nogaprep
504 + osp.substring(ipos + nogaprep.length());
505 newds.setSequence(fullseq.toUpperCase());
506 // TODO: JAL-1131 ensure newly created dataset sequence is added to
508 // dataset sequences associated with the alignment.
509 // TODO: JAL-1131 fix up any annotation associated with new dataset
510 // sequence to ensure that original sequence/annotation relationships
512 command.seqs[i].setDatasetSequence(newds);
521 final void adjustAnnotations(Edit command, boolean insert,
522 boolean modifyVisibility, AlignmentI[] views)
524 AlignmentAnnotation[] annotations = null;
526 if (modifyVisibility && !insert)
528 // only occurs if a sequence was added or deleted.
529 command.deletedAnnotationRows = new Hashtable();
531 if (command.fullAlignmentHeight)
533 annotations = command.al.getAlignmentAnnotation();
538 AlignmentAnnotation[] tmp;
539 for (int s = 0; s < command.seqs.length; s++)
541 if (modifyVisibility)
543 // Rows are only removed or added to sequence object.
547 tmp = command.seqs[s].getAnnotation();
550 int alen = tmp.length;
551 for (int aa = 0; aa < tmp.length; aa++)
553 if (!command.al.deleteAnnotation(tmp[aa]))
555 // strip out annotation not in the current al (will be put
556 // back on insert in all views)
561 command.seqs[s].setAlignmentAnnotation(null);
562 if (alen != tmp.length)
564 // save the non-null annotation references only
565 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
566 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
570 saved[aapos++] = tmp[aa];
575 command.deletedAnnotationRows.put(command.seqs[s], saved);
576 // and then remove any annotation in the other views
577 for (int alview = 0; views != null && alview < views.length; alview++)
579 if (views[alview] != command.al)
581 AlignmentAnnotation[] toremove = views[alview]
582 .getAlignmentAnnotation();
583 if (toremove == null || toremove.length == 0)
587 // remove any alignment annotation on this sequence that's
588 // on that alignment view.
589 for (int aa = 0; aa < toremove.length; aa++)
591 if (toremove[aa].sequenceRef == command.seqs[s])
593 views[alview].deleteAnnotation(toremove[aa]);
601 // save all the annotation
602 command.deletedAnnotationRows.put(command.seqs[s], tmp);
609 if (command.deletedAnnotationRows != null
610 && command.deletedAnnotationRows
611 .containsKey(command.seqs[s]))
613 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
614 .get(command.seqs[s]);
615 command.seqs[s].setAlignmentAnnotation(revealed);
616 if (revealed != null)
618 for (int aa = 0; aa < revealed.length; aa++)
620 // iterate through al adding original annotation
621 command.al.addAnnotation(revealed[aa]);
623 for (int aa = 0; aa < revealed.length; aa++)
625 command.al.setAnnotationIndex(revealed[aa], aa);
627 // and then duplicate added annotation on every other alignment
629 for (int vnum = 0; views != null && vnum < views.length; vnum++)
631 if (views[vnum] != command.al)
633 int avwidth = views[vnum].getWidth() + 1;
634 // duplicate in this view
635 for (int a = 0; a < revealed.length; a++)
637 AlignmentAnnotation newann = new AlignmentAnnotation(
639 command.seqs[s].addAlignmentAnnotation(newann);
640 newann.padAnnotation(avwidth);
641 views[vnum].addAnnotation(newann);
642 views[vnum].setAnnotationIndex(newann, a);
652 if (command.seqs[s].getAnnotation() == null)
659 annotations = command.seqs[s].getAnnotation();
663 tmp = new AlignmentAnnotation[aSize
664 + command.seqs[s].getAnnotation().length];
666 System.arraycopy(annotations, 0, tmp, 0, aSize);
668 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
669 command.seqs[s].getAnnotation().length);
673 aSize = annotations.length;
677 if (annotations == null)
684 command.deletedAnnotations = new Hashtable();
689 for (int a = 0; a < annotations.length; a++)
691 if (annotations[a].autoCalculated
692 || annotations[a].annotations == null)
699 aSize = annotations[a].annotations.length;
702 temp = new Annotation[aSize + command.number];
703 if (annotations[a].padGaps)
704 for (int aa = 0; aa < temp.length; aa++)
706 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
711 if (command.position < aSize)
713 if (command.position + command.number >= aSize)
719 tSize = aSize - command.number;
731 temp = new Annotation[tSize];
736 if (command.position < annotations[a].annotations.length)
738 System.arraycopy(annotations[a].annotations, 0, temp, 0,
741 if (command.deletedAnnotations != null
742 && command.deletedAnnotations
743 .containsKey(annotations[a].annotationId))
745 Annotation[] restore = (Annotation[]) command.deletedAnnotations
746 .get(annotations[a].annotationId);
748 System.arraycopy(restore, 0, temp, command.position,
753 System.arraycopy(annotations[a].annotations, command.position,
754 temp, command.position + command.number, aSize
759 if (command.deletedAnnotations != null
760 && command.deletedAnnotations
761 .containsKey(annotations[a].annotationId))
763 Annotation[] restore = (Annotation[]) command.deletedAnnotations
764 .get(annotations[a].annotationId);
766 temp = new Annotation[annotations[a].annotations.length
768 System.arraycopy(annotations[a].annotations, 0, temp, 0,
769 annotations[a].annotations.length);
770 System.arraycopy(restore, 0, temp,
771 annotations[a].annotations.length, restore.length);
775 temp = annotations[a].annotations;
781 if (tSize != aSize || command.position < 2)
783 int copylen = Math.min(command.position,
784 annotations[a].annotations.length);
786 System.arraycopy(annotations[a].annotations, 0, temp, 0,
787 copylen); // command.position);
789 Annotation[] deleted = new Annotation[command.number];
790 if (copylen >= command.position)
792 copylen = Math.min(command.number,
793 annotations[a].annotations.length - command.position);
796 System.arraycopy(annotations[a].annotations,
797 command.position, deleted, 0, copylen); // command.number);
801 command.deletedAnnotations.put(annotations[a].annotationId,
803 if (annotations[a].annotations.length > command.position
806 System.arraycopy(annotations[a].annotations, command.position
807 + command.number, temp, command.position,
808 annotations[a].annotations.length - command.position
809 - command.number); // aSize
814 int dSize = aSize - command.position;
818 Annotation[] deleted = new Annotation[command.number];
819 System.arraycopy(annotations[a].annotations, command.position,
822 command.deletedAnnotations.put(annotations[a].annotationId,
825 tSize = Math.min(annotations[a].annotations.length,
827 temp = new Annotation[tSize];
828 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
832 temp = annotations[a].annotations;
837 annotations[a].annotations = temp;
841 final void adjustFeatures(Edit command, int index, int i, int j,
844 SequenceI seq = command.seqs[index];
845 SequenceI sequence = seq.getDatasetSequence();
846 if (sequence == null)
853 if (command.editedFeatures != null
854 && command.editedFeatures.containsKey(seq))
856 sequence.setSequenceFeatures((SequenceFeature[]) command.editedFeatures
863 SequenceFeature[] sf = sequence.getSequenceFeatures();
870 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
874 for (int s = 0; s < sf.length; s++)
876 SequenceFeature copy = new SequenceFeature(sf[s]);
880 if (sf[s].getEnd() < i)
885 if (sf[s].getBegin() > j)
887 sf[s].setBegin(copy.getBegin() - cSize);
888 sf[s].setEnd(copy.getEnd() - cSize);
892 if (sf[s].getBegin() >= i)
897 if (sf[s].getEnd() < j)
902 sf[s].setEnd(sf[s].getEnd() - (cSize));
904 if (sf[s].getBegin() > sf[s].getEnd())
906 sequence.deleteFeature(sf[s]);
910 if (command.editedFeatures == null)
912 command.editedFeatures = new Hashtable();
915 command.editedFeatures.put(seq, oldsf);
921 public SequenceI[] oldds;
923 boolean fullAlignmentHeight = false;
925 Hashtable deletedAnnotationRows;
927 Hashtable deletedAnnotations;
929 Hashtable editedFeatures;
941 int position, number;
945 Edit(int command, SequenceI[] seqs, int position, int number,
948 this.command = command;
950 this.position = position;
951 this.number = number;
952 this.gapChar = gapChar;
955 Edit(int command, SequenceI[] seqs, int position, int number,
958 this.gapChar = al.getGapCharacter();
959 this.command = command;
961 this.position = position;
962 this.number = number;
965 alIndex = new int[seqs.length];
966 for (int i = 0; i < seqs.length; i++)
968 alIndex[i] = al.findIndex(seqs[i]);
971 fullAlignmentHeight = (al.getHeight() == seqs.length);
974 Edit(int command, SequenceI[] seqs, int position, int number,
975 AlignmentI al, String replace)
977 this.command = command;
979 this.position = position;
980 this.number = number;
982 this.gapChar = al.getGapCharacter();
983 string = new char[seqs.length][];
984 for (int i = 0; i < seqs.length; i++)
986 string[i] = replace.toCharArray();
989 fullAlignmentHeight = (al.getHeight() == seqs.length);