2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.commands;
23 import jalview.datamodel.*;
32 * Description: Essential information for performing undo and redo for cut/paste
33 * insert/delete gap which can be stored in the HistoryList
37 * Copyright: Copyright (c) 2006
41 * Company: Dundee University
44 * @author not attributable
47 public class EditCommand implements CommandI
49 public static final int INSERT_GAP = 0;
51 public static final int DELETE_GAP = 1;
53 public static final int CUT = 2;
55 public static final int PASTE = 3;
57 public static final int REPLACE = 4;
59 public static final int INSERT_NUC = 5;
69 public EditCommand(String description)
71 this.description = description;
74 public EditCommand(String description, int command, SequenceI[] seqs,
75 int position, int number, AlignmentI al)
77 this.description = description;
78 if (command == CUT || command == PASTE)
81 { new Edit(command, seqs, position, number, al) };
87 public EditCommand(String description, int command, String replace,
88 SequenceI[] seqs, int position, int number, AlignmentI al)
90 this.description = description;
91 if (command == REPLACE)
94 { new Edit(command, seqs, position, number, al, replace) };
101 final public String getDescription()
109 return edits == null ? 0 : edits.length;
112 final public AlignmentI getAlignment()
118 * append a new editCommand Note. this shouldn't be called if the edit is an
119 * operation affects more alignment objects than the one referenced in al (for
120 * example, cut or pasting whole sequences). Use the form with an additional
121 * AlignmentI[] views parameter.
130 final public void appendEdit(int command, SequenceI[] seqs, int position,
131 int number, AlignmentI al, boolean performEdit)
133 appendEdit(command, seqs, position, number, al, performEdit, null);
137 * append a new edit command with a set of alignment views that may be
148 final public void appendEdit(int command, SequenceI[] seqs, int position,
149 int number, AlignmentI al, boolean performEdit, AlignmentI[] views)
151 Edit edit = new Edit(command, seqs, position, number,
152 al.getGapCharacter());
153 if (al.getHeight() == seqs.length)
156 edit.fullAlignmentHeight = true;
161 Edit[] temp = new Edit[edits.length + 1];
162 System.arraycopy(edits, 0, temp, 0, edits.length);
164 edits[edits.length - 1] = edit;
174 performEdit(edits.length - 1, views);
178 final void performEdit(int commandIndex, AlignmentI[] views)
180 int eSize = edits.length;
181 for (int e = commandIndex; e < eSize; e++)
183 switch (edits[e].command)
192 cut(edits[e], views);
195 paste(edits[e], views);
200 // TODO:add deleteNuc for UNDO
202 // insertNuc(edits[e]);
209 final public void doCommand(AlignmentI[] views)
211 performEdit(0, views);
215 final public void undoCommand(AlignmentI[] views)
217 int e = 0, eSize = edits.length;
218 for (e = eSize - 1; e > -1; e--)
220 switch (edits[e].command)
229 paste(edits[e], views);
232 cut(edits[e], views);
241 final void insertGap(Edit command)
244 for (int s = 0; s < command.seqs.length; s++)
246 command.seqs[s].insertCharAt(command.position, command.number,
248 // System.out.println("pos: "+command.position+" number: "+command.number);
251 adjustAnnotations(command, true, false, null);
255 // final void insertNuc(Edit command)
258 // for (int s = 0; s < command.seqs.length; s++)
260 // System.out.println("pos: "+command.position+" number: "+command.number);
261 // command.seqs[s].insertCharAt(command.position, command.number,'A');
264 // adjustAnnotations(command, true, false, null);
267 final void deleteGap(Edit command)
269 for (int s = 0; s < command.seqs.length; s++)
271 command.seqs[s].deleteChars(command.position, command.position
275 adjustAnnotations(command, false, false, null);
278 void cut(Edit command, AlignmentI[] views)
280 boolean seqDeleted = false;
281 command.string = new char[command.seqs.length][];
283 for (int i = 0; i < command.seqs.length; i++)
285 if (command.seqs[i].getLength() > command.position)
287 command.string[i] = command.seqs[i].getSequence(command.position,
288 command.position + command.number);
289 SequenceI oldds = command.seqs[i].getDatasetSequence();
290 if (command.oldds != null && command.oldds[i] != null)
292 // we are redoing an undone cut.
293 command.seqs[i].setDatasetSequence(null);
295 command.seqs[i].deleteChars(command.position, command.position
297 if (command.oldds != null && command.oldds[i] != null)
299 // oldds entry contains the cut dataset sequence.
300 command.seqs[i].setDatasetSequence(command.oldds[i]);
301 command.oldds[i] = oldds;
305 // modify the oldds if necessary
306 if (oldds != command.seqs[i].getDatasetSequence()
307 || command.seqs[i].getSequenceFeatures() != null)
309 if (command.oldds == null)
311 command.oldds = new SequenceI[command.seqs.length];
313 command.oldds[i] = oldds;
317 command.seqs[i].findPosition(command.position),
318 command.seqs[i].findPosition(command.position
319 + command.number), false);
324 if (command.seqs[i].getLength() < 1)
326 command.al.deleteSequence(command.seqs[i]);
331 adjustAnnotations(command, false, seqDeleted, views);
334 void paste(Edit command, AlignmentI[] views)
338 boolean newDSWasNeeded;
339 int newstart, newend;
340 boolean seqWasDeleted = false;
341 int start = 0, end = 0;
343 for (int i = 0; i < command.seqs.length; i++)
346 newDSWasNeeded = command.oldds != null && command.oldds[i] != null;
347 if (command.seqs[i].getLength() < 1)
349 // ie this sequence was deleted, we need to
350 // read it to the alignment
351 if (command.alIndex[i] < command.al.getHeight())
353 List<SequenceI> sequences;
354 synchronized (sequences = command.al.getSequences())
356 sequences.add(command.alIndex[i], command.seqs[i]);
361 command.al.addSequence(command.seqs[i]);
363 seqWasDeleted = true;
365 newstart = command.seqs[i].getStart();
366 newend = command.seqs[i].getEnd();
368 tmp = new StringBuffer();
369 tmp.append(command.seqs[i].getSequence());
370 // Undo of a delete does not replace original dataset sequence on to
371 // alignment sequence.
373 if (command.string != null && command.string[i] != null)
375 if (command.position >= tmp.length())
377 // This occurs if padding is on, and residues
378 // are removed from end of alignment
379 int length = command.position - tmp.length();
382 tmp.append(command.gapChar);
386 tmp.insert(command.position, command.string[i]);
387 for (int s = 0; s < command.string[i].length; s++)
389 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
394 start = command.seqs[i].findPosition(command.position);
395 end = command.seqs[i].findPosition(command.position
398 if (command.seqs[i].getStart() == start)
404 command.string[i] = null;
407 command.seqs[i].setSequence(tmp.toString());
408 command.seqs[i].setStart(newstart);
409 command.seqs[i].setEnd(newend);
412 if (command.seqs[i].getDatasetSequence() != null)
417 ds = command.oldds[i];
421 // make a new DS sequence
422 // use new ds mechanism here
423 ds = new Sequence(command.seqs[i].getName(),
424 jalview.analysis.AlignSeq.extractGaps(
425 jalview.util.Comparison.GapChars,
426 command.seqs[i].getSequenceAsString()),
427 command.seqs[i].getStart(), command.seqs[i].getEnd());
428 ds.setDescription(command.seqs[i].getDescription());
430 if (command.oldds == null)
432 command.oldds = new SequenceI[command.seqs.length];
434 command.oldds[i] = command.seqs[i].getDatasetSequence();
435 command.seqs[i].setDatasetSequence(ds);
437 adjustFeatures(command, i, start, end, true);
440 adjustAnnotations(command, true, seqWasDeleted, views);
442 command.string = null;
445 void replace(Edit command)
449 int start = command.position;
450 int end = command.number;
451 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
453 // TODO Jalview 2.4 bugfix change to an aggregate command - original
454 // sequence string is cut, new string is pasted in.
455 command.number = start + command.string[0].length;
456 for (int i = 0; i < command.seqs.length; i++)
458 boolean newDSWasNeeded = command.oldds != null
459 && command.oldds[i] != null;
462 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
463 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
464 * viewport.alignment));
468 * then addHistoryItem(new EditCommand( "Add sequences",
469 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
472 oldstring = command.seqs[i].getSequenceAsString();
473 tmp = new StringBuffer(oldstring.substring(0, start));
474 tmp.append(command.string[i]);
475 String nogaprep = jalview.analysis.AlignSeq.extractGaps(
476 jalview.util.Comparison.GapChars, new String(
478 int ipos = command.seqs[i].findPosition(start)
479 - command.seqs[i].getStart();
480 tmp.append(oldstring.substring(end));
481 command.seqs[i].setSequence(tmp.toString());
482 command.string[i] = oldstring.substring(start, end).toCharArray();
483 String nogapold = jalview.analysis.AlignSeq.extractGaps(
484 jalview.util.Comparison.GapChars, new String(
486 if (!nogaprep.toLowerCase().equals(nogapold.toLowerCase()))
490 SequenceI oldds = command.seqs[i].getDatasetSequence();
491 command.seqs[i].setDatasetSequence(command.oldds[i]);
492 command.oldds[i] = oldds;
496 if (command.oldds == null)
498 command.oldds = new SequenceI[command.seqs.length];
500 command.oldds[i] = command.seqs[i].getDatasetSequence();
501 SequenceI newds = new Sequence(
502 command.seqs[i].getDatasetSequence());
503 String fullseq, osp = newds.getSequenceAsString();
504 fullseq = osp.substring(0, ipos) + nogaprep
505 + osp.substring(ipos + nogaprep.length());
506 newds.setSequence(fullseq.toUpperCase());
507 // TODO: JAL-1131 ensure newly created dataset sequence is added to
509 // dataset sequences associated with the alignment.
510 // TODO: JAL-1131 fix up any annotation associated with new dataset
511 // sequence to ensure that original sequence/annotation relationships
513 command.seqs[i].setDatasetSequence(newds);
522 final void adjustAnnotations(Edit command, boolean insert,
523 boolean modifyVisibility, AlignmentI[] views)
525 AlignmentAnnotation[] annotations = null;
527 if (modifyVisibility && !insert)
529 // only occurs if a sequence was added or deleted.
530 command.deletedAnnotationRows = new Hashtable();
532 if (command.fullAlignmentHeight)
534 annotations = command.al.getAlignmentAnnotation();
539 AlignmentAnnotation[] tmp;
540 for (int s = 0; s < command.seqs.length; s++)
542 if (modifyVisibility)
544 // Rows are only removed or added to sequence object.
548 tmp = command.seqs[s].getAnnotation();
551 int alen = tmp.length;
552 for (int aa = 0; aa < tmp.length; aa++)
554 if (!command.al.deleteAnnotation(tmp[aa]))
556 // strip out annotation not in the current al (will be put
557 // back on insert in all views)
562 command.seqs[s].setAlignmentAnnotation(null);
563 if (alen != tmp.length)
565 // save the non-null annotation references only
566 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
567 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
571 saved[aapos++] = tmp[aa];
576 command.deletedAnnotationRows.put(command.seqs[s], saved);
577 // and then remove any annotation in the other views
578 for (int alview = 0; views != null && alview < views.length; alview++)
580 if (views[alview] != command.al)
582 AlignmentAnnotation[] toremove = views[alview]
583 .getAlignmentAnnotation();
584 if (toremove == null || toremove.length == 0)
588 // remove any alignment annotation on this sequence that's
589 // on that alignment view.
590 for (int aa = 0; aa < toremove.length; aa++)
592 if (toremove[aa].sequenceRef == command.seqs[s])
594 views[alview].deleteAnnotation(toremove[aa]);
602 // save all the annotation
603 command.deletedAnnotationRows.put(command.seqs[s], tmp);
610 if (command.deletedAnnotationRows != null
611 && command.deletedAnnotationRows
612 .containsKey(command.seqs[s]))
614 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
615 .get(command.seqs[s]);
616 command.seqs[s].setAlignmentAnnotation(revealed);
617 if (revealed != null)
619 for (int aa = 0; aa < revealed.length; aa++)
621 // iterate through al adding original annotation
622 command.al.addAnnotation(revealed[aa]);
624 for (int aa = 0; aa < revealed.length; aa++)
626 command.al.setAnnotationIndex(revealed[aa], aa);
628 // and then duplicate added annotation on every other alignment
630 for (int vnum = 0; views != null && vnum < views.length; vnum++)
632 if (views[vnum] != command.al)
634 int avwidth = views[vnum].getWidth() + 1;
635 // duplicate in this view
636 for (int a = 0; a < revealed.length; a++)
638 AlignmentAnnotation newann = new AlignmentAnnotation(
640 command.seqs[s].addAlignmentAnnotation(newann);
641 newann.padAnnotation(avwidth);
642 views[vnum].addAnnotation(newann);
643 views[vnum].setAnnotationIndex(newann, a);
653 if (command.seqs[s].getAnnotation() == null)
660 annotations = command.seqs[s].getAnnotation();
664 tmp = new AlignmentAnnotation[aSize
665 + command.seqs[s].getAnnotation().length];
667 System.arraycopy(annotations, 0, tmp, 0, aSize);
669 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
670 command.seqs[s].getAnnotation().length);
674 aSize = annotations.length;
678 if (annotations == null)
685 command.deletedAnnotations = new Hashtable();
690 for (int a = 0; a < annotations.length; a++)
692 if (annotations[a].autoCalculated
693 || annotations[a].annotations == null)
700 aSize = annotations[a].annotations.length;
703 temp = new Annotation[aSize + command.number];
704 if (annotations[a].padGaps)
705 for (int aa = 0; aa < temp.length; aa++)
707 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
712 if (command.position < aSize)
714 if (command.position + command.number >= aSize)
720 tSize = aSize - command.number;
732 temp = new Annotation[tSize];
737 if (command.position < annotations[a].annotations.length)
739 System.arraycopy(annotations[a].annotations, 0, temp, 0,
742 if (command.deletedAnnotations != null
743 && command.deletedAnnotations
744 .containsKey(annotations[a].annotationId))
746 Annotation[] restore = (Annotation[]) command.deletedAnnotations
747 .get(annotations[a].annotationId);
749 System.arraycopy(restore, 0, temp, command.position,
754 System.arraycopy(annotations[a].annotations, command.position,
755 temp, command.position + command.number, aSize
760 if (command.deletedAnnotations != null
761 && command.deletedAnnotations
762 .containsKey(annotations[a].annotationId))
764 Annotation[] restore = (Annotation[]) command.deletedAnnotations
765 .get(annotations[a].annotationId);
767 temp = new Annotation[annotations[a].annotations.length
769 System.arraycopy(annotations[a].annotations, 0, temp, 0,
770 annotations[a].annotations.length);
771 System.arraycopy(restore, 0, temp,
772 annotations[a].annotations.length, restore.length);
776 temp = annotations[a].annotations;
782 if (tSize != aSize || command.position < 2)
784 int copylen = Math.min(command.position,
785 annotations[a].annotations.length);
787 System.arraycopy(annotations[a].annotations, 0, temp, 0,
788 copylen); // command.position);
790 Annotation[] deleted = new Annotation[command.number];
791 if (copylen >= command.position)
793 copylen = Math.min(command.number,
794 annotations[a].annotations.length - command.position);
797 System.arraycopy(annotations[a].annotations,
798 command.position, deleted, 0, copylen); // command.number);
802 command.deletedAnnotations.put(annotations[a].annotationId,
804 if (annotations[a].annotations.length > command.position
807 System.arraycopy(annotations[a].annotations, command.position
808 + command.number, temp, command.position,
809 annotations[a].annotations.length - command.position
810 - command.number); // aSize
815 int dSize = aSize - command.position;
819 Annotation[] deleted = new Annotation[command.number];
820 System.arraycopy(annotations[a].annotations, command.position,
823 command.deletedAnnotations.put(annotations[a].annotationId,
826 tSize = Math.min(annotations[a].annotations.length,
828 temp = new Annotation[tSize];
829 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
833 temp = annotations[a].annotations;
838 annotations[a].annotations = temp;
842 final void adjustFeatures(Edit command, int index, int i, int j,
845 SequenceI seq = command.seqs[index];
846 SequenceI sequence = seq.getDatasetSequence();
847 if (sequence == null)
854 if (command.editedFeatures != null
855 && command.editedFeatures.containsKey(seq))
857 sequence.setSequenceFeatures((SequenceFeature[]) command.editedFeatures
864 SequenceFeature[] sf = sequence.getSequenceFeatures();
871 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
875 for (int s = 0; s < sf.length; s++)
877 SequenceFeature copy = new SequenceFeature(sf[s]);
881 if (sf[s].getEnd() < i)
886 if (sf[s].getBegin() > j)
888 sf[s].setBegin(copy.getBegin() - cSize);
889 sf[s].setEnd(copy.getEnd() - cSize);
893 if (sf[s].getBegin() >= i)
898 if (sf[s].getEnd() < j)
903 sf[s].setEnd(sf[s].getEnd() - (cSize));
905 if (sf[s].getBegin() > sf[s].getEnd())
907 sequence.deleteFeature(sf[s]);
911 if (command.editedFeatures == null)
913 command.editedFeatures = new Hashtable();
916 command.editedFeatures.put(seq, oldsf);
922 public SequenceI[] oldds;
924 boolean fullAlignmentHeight = false;
926 Hashtable deletedAnnotationRows;
928 Hashtable deletedAnnotations;
930 Hashtable editedFeatures;
942 int position, number;
946 Edit(int command, SequenceI[] seqs, int position, int number,
949 this.command = command;
951 this.position = position;
952 this.number = number;
953 this.gapChar = gapChar;
956 Edit(int command, SequenceI[] seqs, int position, int number,
959 this.gapChar = al.getGapCharacter();
960 this.command = command;
962 this.position = position;
963 this.number = number;
966 alIndex = new int[seqs.length];
967 for (int i = 0; i < seqs.length; i++)
969 alIndex[i] = al.findIndex(seqs[i]);
972 fullAlignmentHeight = (al.getHeight() == seqs.length);
975 Edit(int command, SequenceI[] seqs, int position, int number,
976 AlignmentI al, String replace)
978 this.command = command;
980 this.position = position;
981 this.number = number;
983 this.gapChar = al.getGapCharacter();
984 string = new char[seqs.length][];
985 for (int i = 0; i < seqs.length; i++)
987 string[i] = replace.toCharArray();
990 fullAlignmentHeight = (al.getHeight() == seqs.length);