2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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);
253 // final void insertNuc(Edit command)
256 // for (int s = 0; s < command.seqs.length; s++)
258 // System.out.println("pos: "+command.position+" number: "+command.number);
259 // command.seqs[s].insertCharAt(command.position, command.number,'A');
262 // adjustAnnotations(command, true, false, null);
265 final void deleteGap(Edit command)
267 for (int s = 0; s < command.seqs.length; s++)
269 command.seqs[s].deleteChars(command.position, command.position
273 adjustAnnotations(command, false, false, null);
276 void cut(Edit command, AlignmentI[] views)
278 boolean seqDeleted = false;
279 command.string = new char[command.seqs.length][];
281 for (int i = 0; i < command.seqs.length; i++)
283 if (command.seqs[i].getLength() > command.position)
285 command.string[i] = command.seqs[i].getSequence(command.position,
286 command.position + command.number);
287 SequenceI oldds = command.seqs[i].getDatasetSequence();
288 if (command.oldds != null && command.oldds[i] != null)
290 // we are redoing an undone cut.
291 command.seqs[i].setDatasetSequence(null);
293 command.seqs[i].deleteChars(command.position, command.position
295 if (command.oldds != null && command.oldds[i] != null)
297 // oldds entry contains the cut dataset sequence.
298 command.seqs[i].setDatasetSequence(command.oldds[i]);
299 command.oldds[i] = oldds;
303 // modify the oldds if necessary
304 if (oldds != command.seqs[i].getDatasetSequence()
305 || command.seqs[i].getSequenceFeatures() != null)
307 if (command.oldds == null)
309 command.oldds = new SequenceI[command.seqs.length];
311 command.oldds[i] = oldds;
315 command.seqs[i].findPosition(command.position),
316 command.seqs[i].findPosition(command.position
317 + command.number), false);
322 if (command.seqs[i].getLength() < 1)
324 command.al.deleteSequence(command.seqs[i]);
329 adjustAnnotations(command, false, seqDeleted, views);
332 void paste(Edit command, AlignmentI[] views)
336 boolean newDSWasNeeded;
337 int newstart, newend;
338 boolean seqWasDeleted = false;
339 int start = 0, end = 0;
341 for (int i = 0; i < command.seqs.length; i++)
344 newDSWasNeeded = command.oldds != null && command.oldds[i] != null;
345 if (command.seqs[i].getLength() < 1)
347 // ie this sequence was deleted, we need to
348 // read it to the alignment
349 if (command.alIndex[i] < command.al.getHeight())
351 List<SequenceI> sequences;
352 synchronized (sequences=command.al.getSequences()) {
353 sequences.add(command.alIndex[i], command.seqs[i]);
358 command.al.addSequence(command.seqs[i]);
360 seqWasDeleted = true;
362 newstart = command.seqs[i].getStart();
363 newend = command.seqs[i].getEnd();
365 tmp = new StringBuffer();
366 tmp.append(command.seqs[i].getSequence());
367 // Undo of a delete does not replace original dataset sequence on to
368 // alignment sequence.
370 if (command.string != null && command.string[i] != null)
372 if (command.position >= tmp.length())
374 // This occurs if padding is on, and residues
375 // are removed from end of alignment
376 int length = command.position - tmp.length();
379 tmp.append(command.gapChar);
383 tmp.insert(command.position, command.string[i]);
384 for (int s = 0; s < command.string[i].length; s++)
386 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
391 start = command.seqs[i].findPosition(command.position);
392 end = command.seqs[i].findPosition(command.position
395 if (command.seqs[i].getStart() == start)
401 command.string[i] = null;
404 command.seqs[i].setSequence(tmp.toString());
405 command.seqs[i].setStart(newstart);
406 command.seqs[i].setEnd(newend);
409 if (command.seqs[i].getDatasetSequence() != null)
414 ds = command.oldds[i];
418 // make a new DS sequence
419 // use new ds mechanism here
420 ds = new Sequence(command.seqs[i].getName(),
421 jalview.analysis.AlignSeq.extractGaps(
422 jalview.util.Comparison.GapChars,
423 command.seqs[i].getSequenceAsString()),
424 command.seqs[i].getStart(), command.seqs[i].getEnd());
425 ds.setDescription(command.seqs[i].getDescription());
427 if (command.oldds == null)
429 command.oldds = new SequenceI[command.seqs.length];
431 command.oldds[i] = command.seqs[i].getDatasetSequence();
432 command.seqs[i].setDatasetSequence(ds);
434 adjustFeatures(command, i, start, end, true);
437 adjustAnnotations(command, true, seqWasDeleted, views);
439 command.string = null;
442 void replace(Edit command)
446 int start = command.position;
447 int end = command.number;
448 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
450 // TODO Jalview 2.4 bugfix change to an aggregate command - original
451 // sequence string is cut, new string is pasted in.
452 command.number = start + command.string[0].length;
453 for (int i = 0; i < command.seqs.length; i++)
455 boolean newDSWasNeeded = command.oldds != null && command.oldds[i] != null;
458 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
459 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
460 * viewport.alignment));
464 * then addHistoryItem(new EditCommand( "Add sequences",
465 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
468 oldstring = command.seqs[i].getSequenceAsString();
469 tmp = new StringBuffer(oldstring.substring(0, start));
470 tmp.append(command.string[i]);
471 String nogaprep = jalview.analysis.AlignSeq.extractGaps(
472 jalview.util.Comparison.GapChars, new String(
474 int ipos = command.seqs[i].findPosition(start)
475 - command.seqs[i].getStart();
476 tmp.append(oldstring.substring(end));
477 command.seqs[i].setSequence(tmp.toString());
478 command.string[i] = oldstring.substring(start, end).toCharArray();
479 String nogapold = jalview.analysis.AlignSeq.extractGaps(
480 jalview.util.Comparison.GapChars, new String(
482 if (!nogaprep.toLowerCase().equals(nogapold.toLowerCase()))
486 SequenceI oldds = command.seqs[i].getDatasetSequence();
487 command.seqs[i].setDatasetSequence(command.oldds[i]);
488 command.oldds[i] = oldds;
492 if (command.oldds == null)
494 command.oldds = new SequenceI[command.seqs.length];
496 command.oldds[i] = command.seqs[i].getDatasetSequence();
497 SequenceI newds = new Sequence(
498 command.seqs[i].getDatasetSequence());
499 String fullseq, osp = newds.getSequenceAsString();
500 fullseq = osp.substring(0, ipos) + nogaprep
501 + osp.substring(ipos + nogaprep.length());
502 newds.setSequence(fullseq.toUpperCase());
503 // TODO: JAL-1131 ensure newly created dataset sequence is added to the set of
504 // dataset sequences associated with the alignment.
505 // TODO: JAL-1131 fix up any annotation associated with new dataset
506 // sequence to ensure that original sequence/annotation relationships
508 command.seqs[i].setDatasetSequence(newds);
517 final void adjustAnnotations(Edit command, boolean insert,
518 boolean modifyVisibility, AlignmentI[] views)
520 AlignmentAnnotation[] annotations = null;
522 if (modifyVisibility && !insert)
524 // only occurs if a sequence was added or deleted.
525 command.deletedAnnotationRows = new Hashtable();
527 if (command.fullAlignmentHeight)
529 annotations = command.al.getAlignmentAnnotation();
534 AlignmentAnnotation[] tmp;
535 for (int s = 0; s < command.seqs.length; s++)
537 if (modifyVisibility)
539 // Rows are only removed or added to sequence object.
543 tmp = command.seqs[s].getAnnotation();
546 int alen = tmp.length;
547 for (int aa = 0; aa < tmp.length; aa++)
549 if (!command.al.deleteAnnotation(tmp[aa]))
551 // strip out annotation not in the current al (will be put
552 // back on insert in all views)
557 command.seqs[s].setAlignmentAnnotation(null);
558 if (alen != tmp.length)
560 // save the non-null annotation references only
561 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
562 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
566 saved[aapos++] = tmp[aa];
571 command.deletedAnnotationRows.put(command.seqs[s], saved);
572 // and then remove any annotation in the other views
573 for (int alview = 0; views != null && alview < views.length; alview++)
575 if (views[alview] != command.al)
577 AlignmentAnnotation[] toremove = views[alview]
578 .getAlignmentAnnotation();
579 if (toremove == null || toremove.length == 0)
583 // remove any alignment annotation on this sequence that's
584 // on that alignment view.
585 for (int aa = 0; aa < toremove.length; aa++)
587 if (toremove[aa].sequenceRef == command.seqs[s])
589 views[alview].deleteAnnotation(toremove[aa]);
597 // save all the annotation
598 command.deletedAnnotationRows.put(command.seqs[s], tmp);
605 if (command.deletedAnnotationRows != null
606 && command.deletedAnnotationRows
607 .containsKey(command.seqs[s]))
609 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
610 .get(command.seqs[s]);
611 command.seqs[s].setAlignmentAnnotation(revealed);
612 if (revealed != null)
614 for (int aa = 0; aa < revealed.length; aa++)
616 // iterate through al adding original annotation
617 command.al.addAnnotation(revealed[aa]);
619 for (int aa = 0; aa < revealed.length; aa++)
621 command.al.setAnnotationIndex(revealed[aa], aa);
623 // and then duplicate added annotation on every other alignment
625 for (int vnum = 0; views != null && vnum < views.length; vnum++)
627 if (views[vnum] != command.al)
629 int avwidth = views[vnum].getWidth() + 1;
630 // duplicate in this view
631 for (int a = 0; a < revealed.length; a++)
633 AlignmentAnnotation newann = new AlignmentAnnotation(
635 command.seqs[s].addAlignmentAnnotation(newann);
636 newann.padAnnotation(avwidth);
637 views[vnum].addAnnotation(newann);
638 views[vnum].setAnnotationIndex(newann, a);
648 if (command.seqs[s].getAnnotation() == null)
655 annotations = command.seqs[s].getAnnotation();
659 tmp = new AlignmentAnnotation[aSize
660 + command.seqs[s].getAnnotation().length];
662 System.arraycopy(annotations, 0, tmp, 0, aSize);
664 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
665 command.seqs[s].getAnnotation().length);
669 aSize = annotations.length;
673 if (annotations == null)
680 command.deletedAnnotations = new Hashtable();
685 for (int a = 0; a < annotations.length; a++)
687 if (annotations[a].autoCalculated
688 || annotations[a].annotations == null)
695 aSize = annotations[a].annotations.length;
698 temp = new Annotation[aSize + command.number];
699 if (annotations[a].padGaps)
700 for (int aa = 0; aa < temp.length; aa++)
702 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
707 if (command.position < aSize)
709 if (command.position + command.number >= aSize)
715 tSize = aSize - command.number;
727 temp = new Annotation[tSize];
732 if (command.position < annotations[a].annotations.length)
734 System.arraycopy(annotations[a].annotations, 0, temp, 0,
737 if (command.deletedAnnotations != null
738 && command.deletedAnnotations
739 .containsKey(annotations[a].annotationId))
741 Annotation[] restore = (Annotation[]) command.deletedAnnotations
742 .get(annotations[a].annotationId);
744 System.arraycopy(restore, 0, temp, command.position,
749 System.arraycopy(annotations[a].annotations, command.position,
750 temp, command.position + command.number, aSize
755 if (command.deletedAnnotations != null
756 && command.deletedAnnotations
757 .containsKey(annotations[a].annotationId))
759 Annotation[] restore = (Annotation[]) command.deletedAnnotations
760 .get(annotations[a].annotationId);
762 temp = new Annotation[annotations[a].annotations.length
764 System.arraycopy(annotations[a].annotations, 0, temp, 0,
765 annotations[a].annotations.length);
766 System.arraycopy(restore, 0, temp,
767 annotations[a].annotations.length, restore.length);
771 temp = annotations[a].annotations;
777 if (tSize != aSize || command.position < 2)
779 int copylen = Math.min(command.position,
780 annotations[a].annotations.length);
782 System.arraycopy(annotations[a].annotations, 0, temp, 0,
783 copylen); // command.position);
785 Annotation[] deleted = new Annotation[command.number];
786 if (copylen >= command.position)
788 copylen = Math.min(command.number,
789 annotations[a].annotations.length - command.position);
792 System.arraycopy(annotations[a].annotations,
793 command.position, deleted, 0, copylen); // command.number);
797 command.deletedAnnotations.put(annotations[a].annotationId,
799 if (annotations[a].annotations.length > command.position
802 System.arraycopy(annotations[a].annotations, command.position
803 + command.number, temp, command.position,
804 annotations[a].annotations.length - command.position
805 - command.number); // aSize
810 int dSize = aSize - command.position;
814 Annotation[] deleted = new Annotation[command.number];
815 System.arraycopy(annotations[a].annotations, command.position,
818 command.deletedAnnotations.put(annotations[a].annotationId,
821 tSize = Math.min(annotations[a].annotations.length,
823 temp = new Annotation[tSize];
824 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
828 temp = annotations[a].annotations;
833 annotations[a].annotations = temp;
837 final void adjustFeatures(Edit command, int index, int i, int j,
840 SequenceI seq = command.seqs[index];
841 SequenceI sequence = seq.getDatasetSequence();
842 if (sequence == null)
849 if (command.editedFeatures != null
850 && command.editedFeatures.containsKey(seq))
852 sequence.setSequenceFeatures((SequenceFeature[]) command.editedFeatures
859 SequenceFeature[] sf = sequence.getSequenceFeatures();
866 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
870 for (int s = 0; s < sf.length; s++)
872 SequenceFeature copy = new SequenceFeature(sf[s]);
876 if (sf[s].getEnd() < i)
881 if (sf[s].getBegin() > j)
883 sf[s].setBegin(copy.getBegin() - cSize);
884 sf[s].setEnd(copy.getEnd() - cSize);
888 if (sf[s].getBegin() >= i)
893 if (sf[s].getEnd() < j)
898 sf[s].setEnd(sf[s].getEnd() - (cSize));
900 if (sf[s].getBegin() > sf[s].getEnd())
902 sequence.deleteFeature(sf[s]);
906 if (command.editedFeatures == null)
908 command.editedFeatures = new Hashtable();
911 command.editedFeatures.put(seq, oldsf);
917 public SequenceI[] oldds;
919 boolean fullAlignmentHeight = false;
921 Hashtable deletedAnnotationRows;
923 Hashtable deletedAnnotations;
925 Hashtable editedFeatures;
937 int position, number;
941 Edit(int command, SequenceI[] seqs, int position, int number,
944 this.command = command;
946 this.position = position;
947 this.number = number;
948 this.gapChar = gapChar;
951 Edit(int command, SequenceI[] seqs, int position, int number,
954 this.gapChar = al.getGapCharacter();
955 this.command = command;
957 this.position = position;
958 this.number = number;
961 alIndex = new int[seqs.length];
962 for (int i = 0; i < seqs.length; i++)
964 alIndex[i] = al.findIndex(seqs[i]);
967 fullAlignmentHeight = (al.getHeight() == seqs.length);
970 Edit(int command, SequenceI[] seqs, int position, int number,
971 AlignmentI al, String replace)
973 this.command = command;
975 this.position = position;
976 this.number = number;
978 this.gapChar = al.getGapCharacter();
979 string = new char[seqs.length][];
980 for (int i = 0; i < seqs.length; i++)
982 string[i] = replace.toCharArray();
985 fullAlignmentHeight = (al.getHeight() == seqs.length);