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.Hashtable;
31 import java.util.List;
40 * Description: Essential information for performing undo and redo for cut/paste
41 * insert/delete gap which can be stored in the HistoryList
45 * Copyright: Copyright (c) 2006
49 * Company: Dundee University
52 * @author not attributable
55 public class EditCommand implements CommandI
57 public static final int INSERT_GAP = 0;
59 public static final int DELETE_GAP = 1;
61 public static final int CUT = 2;
63 public static final int PASTE = 3;
65 public static final int REPLACE = 4;
67 public static final int INSERT_NUC = 5;
77 public EditCommand(String description)
79 this.description = description;
82 public EditCommand(String description, int command, SequenceI[] seqs,
83 int position, int number, AlignmentI al)
85 this.description = description;
86 if (command == CUT || command == PASTE)
89 { new Edit(command, seqs, position, number, al) };
95 public EditCommand(String description, int command, String replace,
96 SequenceI[] seqs, int position, int number, AlignmentI al)
98 this.description = description;
99 if (command == REPLACE)
102 { new Edit(command, seqs, position, number, al, replace) };
105 performEdit(0, null);
109 final public String getDescription()
117 return edits == null ? 0 : edits.length;
120 final public AlignmentI getAlignment()
126 * append a new editCommand Note. this shouldn't be called if the edit is an
127 * operation affects more alignment objects than the one referenced in al (for
128 * example, cut or pasting whole sequences). Use the form with an additional
129 * AlignmentI[] views parameter.
138 final public void appendEdit(int command, SequenceI[] seqs, int position,
139 int number, AlignmentI al, boolean performEdit)
141 appendEdit(command, seqs, position, number, al, performEdit, null);
145 * append a new edit command with a set of alignment views that may be
156 final public void appendEdit(int command, SequenceI[] seqs, int position,
157 int number, AlignmentI al, boolean performEdit, AlignmentI[] views)
159 Edit edit = new Edit(command, seqs, position, number,
160 al.getGapCharacter());
161 if (al.getHeight() == seqs.length)
164 edit.fullAlignmentHeight = true;
169 Edit[] temp = new Edit[edits.length + 1];
170 System.arraycopy(edits, 0, temp, 0, edits.length);
172 edits[edits.length - 1] = edit;
182 performEdit(edits.length - 1, views);
186 final void performEdit(int commandIndex, AlignmentI[] views)
188 int eSize = edits.length;
189 for (int e = commandIndex; e < eSize; e++)
191 switch (edits[e].command)
200 cut(edits[e], views);
203 paste(edits[e], views);
208 // TODO:add deleteNuc for UNDO
210 // insertNuc(edits[e]);
217 final public void doCommand(AlignmentI[] views)
219 performEdit(0, views);
223 final public void undoCommand(AlignmentI[] views)
247 final void insertGap(Edit command)
250 for (int s = 0; s < command.seqs.length; s++)
252 command.seqs[s].insertCharAt(command.position, command.number,
254 // System.out.println("pos: "+command.position+" number: "+command.number);
257 adjustAnnotations(command, true, false, null);
261 // final void insertNuc(Edit command)
264 // for (int s = 0; s < command.seqs.length; s++)
266 // System.out.println("pos: "+command.position+" number: "+command.number);
267 // command.seqs[s].insertCharAt(command.position, command.number,'A');
270 // adjustAnnotations(command, true, false, null);
273 final void deleteGap(Edit command)
275 for (int s = 0; s < command.seqs.length; s++)
277 command.seqs[s].deleteChars(command.position, command.position
281 adjustAnnotations(command, false, false, null);
284 void cut(Edit command, AlignmentI[] views)
286 boolean seqDeleted = false;
287 command.string = new char[command.seqs.length][];
289 for (int i = 0; i < command.seqs.length; i++)
291 if (command.seqs[i].getLength() > command.position)
293 command.string[i] = command.seqs[i].getSequence(command.position,
294 command.position + command.number);
295 SequenceI oldds = command.seqs[i].getDatasetSequence();
296 if (command.oldds != null && command.oldds[i] != null)
298 // we are redoing an undone cut.
299 command.seqs[i].setDatasetSequence(null);
301 command.seqs[i].deleteChars(command.position, command.position
303 if (command.oldds != null && command.oldds[i] != null)
305 // oldds entry contains the cut dataset sequence.
306 command.seqs[i].setDatasetSequence(command.oldds[i]);
307 command.oldds[i] = oldds;
311 // modify the oldds if necessary
312 if (oldds != command.seqs[i].getDatasetSequence()
313 || command.seqs[i].getSequenceFeatures() != null)
315 if (command.oldds == null)
317 command.oldds = new SequenceI[command.seqs.length];
319 command.oldds[i] = oldds;
323 command.seqs[i].findPosition(command.position),
324 command.seqs[i].findPosition(command.position
325 + command.number), false);
330 if (command.seqs[i].getLength() < 1)
332 command.al.deleteSequence(command.seqs[i]);
337 adjustAnnotations(command, false, seqDeleted, views);
340 void paste(Edit command, AlignmentI[] views)
344 boolean newDSWasNeeded;
345 int newstart, newend;
346 boolean seqWasDeleted = false;
347 int start = 0, end = 0;
349 for (int i = 0; i < command.seqs.length; i++)
352 newDSWasNeeded = command.oldds != null && command.oldds[i] != null;
353 if (command.seqs[i].getLength() < 1)
355 // ie this sequence was deleted, we need to
356 // read it to the alignment
357 if (command.alIndex[i] < command.al.getHeight())
359 List<SequenceI> sequences;
360 synchronized (sequences = command.al.getSequences())
362 if (!(command.alIndex[i] < 0))
364 sequences.add(command.alIndex[i], command.seqs[i]);
370 command.al.addSequence(command.seqs[i]);
372 seqWasDeleted = true;
374 newstart = command.seqs[i].getStart();
375 newend = command.seqs[i].getEnd();
377 tmp = new StringBuffer();
378 tmp.append(command.seqs[i].getSequence());
379 // Undo of a delete does not replace original dataset sequence on to
380 // alignment sequence.
382 if (command.string != null && command.string[i] != null)
384 if (command.position >= tmp.length())
386 // This occurs if padding is on, and residues
387 // are removed from end of alignment
388 int length = command.position - tmp.length();
391 tmp.append(command.gapChar);
395 tmp.insert(command.position, command.string[i]);
396 for (int s = 0; s < command.string[i].length; s++)
398 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
403 start = command.seqs[i].findPosition(command.position);
404 end = command.seqs[i].findPosition(command.position
407 if (command.seqs[i].getStart() == start)
417 command.string[i] = null;
420 command.seqs[i].setSequence(tmp.toString());
421 command.seqs[i].setStart(newstart);
422 command.seqs[i].setEnd(newend);
425 if (command.seqs[i].getDatasetSequence() != null)
430 ds = command.oldds[i];
434 // make a new DS sequence
435 // use new ds mechanism here
436 ds = new Sequence(command.seqs[i].getName(),
437 jalview.analysis.AlignSeq.extractGaps(
438 jalview.util.Comparison.GapChars,
439 command.seqs[i].getSequenceAsString()),
440 command.seqs[i].getStart(), command.seqs[i].getEnd());
441 ds.setDescription(command.seqs[i].getDescription());
443 if (command.oldds == null)
445 command.oldds = new SequenceI[command.seqs.length];
447 command.oldds[i] = command.seqs[i].getDatasetSequence();
448 command.seqs[i].setDatasetSequence(ds);
450 adjustFeatures(command, i, start, end, true);
453 adjustAnnotations(command, true, seqWasDeleted, views);
455 command.string = null;
458 void replace(Edit command)
462 int start = command.position;
463 int end = command.number;
464 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
466 // TODO Jalview 2.4 bugfix change to an aggregate command - original
467 // sequence string is cut, new string is pasted in.
468 command.number = start + command.string[0].length;
469 for (int i = 0; i < command.seqs.length; i++)
471 boolean newDSWasNeeded = command.oldds != null
472 && command.oldds[i] != null;
475 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
476 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
477 * viewport.alignment));
481 * then addHistoryItem(new EditCommand( "Add sequences",
482 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
485 oldstring = command.seqs[i].getSequenceAsString();
486 tmp = new StringBuffer(oldstring.substring(0, start));
487 tmp.append(command.string[i]);
488 String nogaprep = jalview.analysis.AlignSeq.extractGaps(
489 jalview.util.Comparison.GapChars, new String(
491 int ipos = command.seqs[i].findPosition(start)
492 - command.seqs[i].getStart();
493 tmp.append(oldstring.substring(end));
494 command.seqs[i].setSequence(tmp.toString());
495 command.string[i] = oldstring.substring(start, end).toCharArray();
496 String nogapold = jalview.analysis.AlignSeq.extractGaps(
497 jalview.util.Comparison.GapChars, new String(
499 if (!nogaprep.toLowerCase().equals(nogapold.toLowerCase()))
503 SequenceI oldds = command.seqs[i].getDatasetSequence();
504 command.seqs[i].setDatasetSequence(command.oldds[i]);
505 command.oldds[i] = oldds;
509 if (command.oldds == null)
511 command.oldds = new SequenceI[command.seqs.length];
513 command.oldds[i] = command.seqs[i].getDatasetSequence();
514 SequenceI newds = new Sequence(
515 command.seqs[i].getDatasetSequence());
516 String fullseq, osp = newds.getSequenceAsString();
517 fullseq = osp.substring(0, ipos) + nogaprep
518 + osp.substring(ipos + nogaprep.length());
519 newds.setSequence(fullseq.toUpperCase());
520 // TODO: JAL-1131 ensure newly created dataset sequence is added to
522 // dataset sequences associated with the alignment.
523 // TODO: JAL-1131 fix up any annotation associated with new dataset
524 // sequence to ensure that original sequence/annotation relationships
526 command.seqs[i].setDatasetSequence(newds);
535 final void adjustAnnotations(Edit command, boolean insert,
536 boolean modifyVisibility, AlignmentI[] views)
538 AlignmentAnnotation[] annotations = null;
540 if (modifyVisibility && !insert)
542 // only occurs if a sequence was added or deleted.
543 command.deletedAnnotationRows = new Hashtable();
545 if (command.fullAlignmentHeight)
547 annotations = command.al.getAlignmentAnnotation();
552 AlignmentAnnotation[] tmp;
553 for (int s = 0; s < command.seqs.length; s++)
555 if (modifyVisibility)
557 // Rows are only removed or added to sequence object.
561 tmp = command.seqs[s].getAnnotation();
564 int alen = tmp.length;
565 for (int aa = 0; aa < tmp.length; aa++)
567 if (!command.al.deleteAnnotation(tmp[aa]))
569 // strip out annotation not in the current al (will be put
570 // back on insert in all views)
575 command.seqs[s].setAlignmentAnnotation(null);
576 if (alen != tmp.length)
578 // save the non-null annotation references only
579 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
580 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
584 saved[aapos++] = tmp[aa];
589 command.deletedAnnotationRows.put(command.seqs[s], saved);
590 // and then remove any annotation in the other views
591 for (int alview = 0; views != null && alview < views.length; alview++)
593 if (views[alview] != command.al)
595 AlignmentAnnotation[] toremove = views[alview]
596 .getAlignmentAnnotation();
597 if (toremove == null || toremove.length == 0)
601 // remove any alignment annotation on this sequence that's
602 // on that alignment view.
603 for (int aa = 0; aa < toremove.length; aa++)
605 if (toremove[aa].sequenceRef == command.seqs[s])
607 views[alview].deleteAnnotation(toremove[aa]);
615 // save all the annotation
616 command.deletedAnnotationRows.put(command.seqs[s], tmp);
623 if (command.deletedAnnotationRows != null
624 && command.deletedAnnotationRows
625 .containsKey(command.seqs[s]))
627 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
628 .get(command.seqs[s]);
629 command.seqs[s].setAlignmentAnnotation(revealed);
630 if (revealed != null)
632 for (int aa = 0; aa < revealed.length; aa++)
634 // iterate through al adding original annotation
635 command.al.addAnnotation(revealed[aa]);
637 for (int aa = 0; aa < revealed.length; aa++)
639 command.al.setAnnotationIndex(revealed[aa], aa);
641 // and then duplicate added annotation on every other alignment
643 for (int vnum = 0; views != null && vnum < views.length; vnum++)
645 if (views[vnum] != command.al)
647 int avwidth = views[vnum].getWidth() + 1;
648 // duplicate in this view
649 for (int a = 0; a < revealed.length; a++)
651 AlignmentAnnotation newann = new AlignmentAnnotation(
653 command.seqs[s].addAlignmentAnnotation(newann);
654 newann.padAnnotation(avwidth);
655 views[vnum].addAnnotation(newann);
656 views[vnum].setAnnotationIndex(newann, a);
666 if (command.seqs[s].getAnnotation() == null)
673 annotations = command.seqs[s].getAnnotation();
677 tmp = new AlignmentAnnotation[aSize
678 + command.seqs[s].getAnnotation().length];
680 System.arraycopy(annotations, 0, tmp, 0, aSize);
682 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
683 command.seqs[s].getAnnotation().length);
687 aSize = annotations.length;
691 if (annotations == null)
698 command.deletedAnnotations = new Hashtable();
703 for (int a = 0; a < annotations.length; a++)
705 if (annotations[a].autoCalculated
706 || annotations[a].annotations == null)
713 aSize = annotations[a].annotations.length;
716 temp = new Annotation[aSize + command.number];
717 if (annotations[a].padGaps)
719 for (int aa = 0; aa < temp.length; aa++)
721 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
727 if (command.position < aSize)
729 if (command.position + command.number >= aSize)
735 tSize = aSize - command.number;
747 temp = new Annotation[tSize];
752 if (command.position < annotations[a].annotations.length)
754 System.arraycopy(annotations[a].annotations, 0, temp, 0,
757 if (command.deletedAnnotations != null
758 && command.deletedAnnotations
759 .containsKey(annotations[a].annotationId))
761 Annotation[] restore = (Annotation[]) command.deletedAnnotations
762 .get(annotations[a].annotationId);
764 System.arraycopy(restore, 0, temp, command.position,
769 System.arraycopy(annotations[a].annotations, command.position,
770 temp, command.position + command.number, aSize
775 if (command.deletedAnnotations != null
776 && command.deletedAnnotations
777 .containsKey(annotations[a].annotationId))
779 Annotation[] restore = (Annotation[]) command.deletedAnnotations
780 .get(annotations[a].annotationId);
782 temp = new Annotation[annotations[a].annotations.length
784 System.arraycopy(annotations[a].annotations, 0, temp, 0,
785 annotations[a].annotations.length);
786 System.arraycopy(restore, 0, temp,
787 annotations[a].annotations.length, restore.length);
791 temp = annotations[a].annotations;
797 if (tSize != aSize || command.position < 2)
799 int copylen = Math.min(command.position,
800 annotations[a].annotations.length);
803 System.arraycopy(annotations[a].annotations, 0, temp, 0,
804 copylen); // command.position);
807 Annotation[] deleted = new Annotation[command.number];
808 if (copylen >= command.position)
810 copylen = Math.min(command.number,
811 annotations[a].annotations.length - command.position);
814 System.arraycopy(annotations[a].annotations,
815 command.position, deleted, 0, copylen); // command.number);
819 command.deletedAnnotations.put(annotations[a].annotationId,
821 if (annotations[a].annotations.length > command.position
824 System.arraycopy(annotations[a].annotations, command.position
825 + command.number, temp, command.position,
826 annotations[a].annotations.length - command.position
827 - command.number); // aSize
832 int dSize = aSize - command.position;
836 Annotation[] deleted = new Annotation[command.number];
837 System.arraycopy(annotations[a].annotations, command.position,
840 command.deletedAnnotations.put(annotations[a].annotationId,
843 tSize = Math.min(annotations[a].annotations.length,
845 temp = new Annotation[tSize];
846 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
850 temp = annotations[a].annotations;
855 annotations[a].annotations = temp;
859 final void adjustFeatures(Edit command, int index, int i, int j,
862 SequenceI seq = command.seqs[index];
863 SequenceI sequence = seq.getDatasetSequence();
864 if (sequence == null)
871 if (command.editedFeatures != null
872 && command.editedFeatures.containsKey(seq))
874 sequence.setSequenceFeatures((SequenceFeature[]) command.editedFeatures
881 SequenceFeature[] sf = sequence.getSequenceFeatures();
888 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
892 for (int s = 0; s < sf.length; s++)
894 SequenceFeature copy = new SequenceFeature(sf[s]);
898 if (sf[s].getEnd() < i)
903 if (sf[s].getBegin() > j)
905 sf[s].setBegin(copy.getBegin() - cSize);
906 sf[s].setEnd(copy.getEnd() - cSize);
910 if (sf[s].getBegin() >= i)
915 if (sf[s].getEnd() < j)
920 sf[s].setEnd(sf[s].getEnd() - (cSize));
922 if (sf[s].getBegin() > sf[s].getEnd())
924 sequence.deleteFeature(sf[s]);
928 if (command.editedFeatures == null)
930 command.editedFeatures = new Hashtable();
933 command.editedFeatures.put(seq, oldsf);
939 public SequenceI[] oldds;
941 boolean fullAlignmentHeight = false;
943 Hashtable deletedAnnotationRows;
945 Hashtable deletedAnnotations;
947 Hashtable editedFeatures;
959 int position, number;
963 Edit(int command, SequenceI[] seqs, int position, int number,
966 this.command = command;
968 this.position = position;
969 this.number = number;
970 this.gapChar = gapChar;
973 Edit(int command, SequenceI[] seqs, int position, int number,
976 this.gapChar = al.getGapCharacter();
977 this.command = command;
979 this.position = position;
980 this.number = number;
983 alIndex = new int[seqs.length];
984 for (int i = 0; i < seqs.length; i++)
986 alIndex[i] = al.findIndex(seqs[i]);
989 fullAlignmentHeight = (al.getHeight() == seqs.length);
992 Edit(int command, SequenceI[] seqs, int position, int number,
993 AlignmentI al, String replace)
995 this.command = command;
997 this.position = position;
998 this.number = number;
1000 this.gapChar = al.getGapCharacter();
1001 string = new char[seqs.length][];
1002 for (int i = 0; i < seqs.length; i++)
1004 string[i] = replace.toCharArray();
1007 fullAlignmentHeight = (al.getHeight() == seqs.length);