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: ensure newly created dataset sequence is added to the set of
504 // dataset sequences associated with the alignment.
505 command.seqs[i].setDatasetSequence(newds);
514 final void adjustAnnotations(Edit command, boolean insert,
515 boolean modifyVisibility, AlignmentI[] views)
517 AlignmentAnnotation[] annotations = null;
519 if (modifyVisibility && !insert)
521 // only occurs if a sequence was added or deleted.
522 command.deletedAnnotationRows = new Hashtable();
524 if (command.fullAlignmentHeight)
526 annotations = command.al.getAlignmentAnnotation();
531 AlignmentAnnotation[] tmp;
532 for (int s = 0; s < command.seqs.length; s++)
534 if (modifyVisibility)
536 // Rows are only removed or added to sequence object.
540 tmp = command.seqs[s].getAnnotation();
543 int alen = tmp.length;
544 for (int aa = 0; aa < tmp.length; aa++)
546 if (!command.al.deleteAnnotation(tmp[aa]))
548 // strip out annotation not in the current al (will be put
549 // back on insert in all views)
554 command.seqs[s].setAlignmentAnnotation(null);
555 if (alen != tmp.length)
557 // save the non-null annotation references only
558 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
559 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
563 saved[aapos++] = tmp[aa];
568 command.deletedAnnotationRows.put(command.seqs[s], saved);
569 // and then remove any annotation in the other views
570 for (int alview = 0; views != null && alview < views.length; alview++)
572 if (views[alview] != command.al)
574 AlignmentAnnotation[] toremove = views[alview]
575 .getAlignmentAnnotation();
576 if (toremove == null || toremove.length == 0)
580 // remove any alignment annotation on this sequence that's
581 // on that alignment view.
582 for (int aa = 0; aa < toremove.length; aa++)
584 if (toremove[aa].sequenceRef == command.seqs[s])
586 views[alview].deleteAnnotation(toremove[aa]);
594 // save all the annotation
595 command.deletedAnnotationRows.put(command.seqs[s], tmp);
602 if (command.deletedAnnotationRows != null
603 && command.deletedAnnotationRows
604 .containsKey(command.seqs[s]))
606 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
607 .get(command.seqs[s]);
608 command.seqs[s].setAlignmentAnnotation(revealed);
609 if (revealed != null)
611 for (int aa = 0; aa < revealed.length; aa++)
613 // iterate through al adding original annotation
614 command.al.addAnnotation(revealed[aa]);
616 for (int aa = 0; aa < revealed.length; aa++)
618 command.al.setAnnotationIndex(revealed[aa], aa);
620 // and then duplicate added annotation on every other alignment
622 for (int vnum = 0; views != null && vnum < views.length; vnum++)
624 if (views[vnum] != command.al)
626 int avwidth = views[vnum].getWidth() + 1;
627 // duplicate in this view
628 for (int a = 0; a < revealed.length; a++)
630 AlignmentAnnotation newann = new AlignmentAnnotation(
632 command.seqs[s].addAlignmentAnnotation(newann);
633 newann.padAnnotation(avwidth);
634 views[vnum].addAnnotation(newann);
635 views[vnum].setAnnotationIndex(newann, a);
645 if (command.seqs[s].getAnnotation() == null)
652 annotations = command.seqs[s].getAnnotation();
656 tmp = new AlignmentAnnotation[aSize
657 + command.seqs[s].getAnnotation().length];
659 System.arraycopy(annotations, 0, tmp, 0, aSize);
661 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
662 command.seqs[s].getAnnotation().length);
666 aSize = annotations.length;
670 if (annotations == null)
677 command.deletedAnnotations = new Hashtable();
682 for (int a = 0; a < annotations.length; a++)
684 if (annotations[a].autoCalculated
685 || annotations[a].annotations == null)
692 aSize = annotations[a].annotations.length;
695 temp = new Annotation[aSize + command.number];
696 if (annotations[a].padGaps)
697 for (int aa = 0; aa < temp.length; aa++)
699 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
704 if (command.position < aSize)
706 if (command.position + command.number >= aSize)
712 tSize = aSize - command.number;
724 temp = new Annotation[tSize];
729 if (command.position < annotations[a].annotations.length)
731 System.arraycopy(annotations[a].annotations, 0, temp, 0,
734 if (command.deletedAnnotations != null
735 && command.deletedAnnotations
736 .containsKey(annotations[a].annotationId))
738 Annotation[] restore = (Annotation[]) command.deletedAnnotations
739 .get(annotations[a].annotationId);
741 System.arraycopy(restore, 0, temp, command.position,
746 System.arraycopy(annotations[a].annotations, command.position,
747 temp, command.position + command.number, aSize
752 if (command.deletedAnnotations != null
753 && command.deletedAnnotations
754 .containsKey(annotations[a].annotationId))
756 Annotation[] restore = (Annotation[]) command.deletedAnnotations
757 .get(annotations[a].annotationId);
759 temp = new Annotation[annotations[a].annotations.length
761 System.arraycopy(annotations[a].annotations, 0, temp, 0,
762 annotations[a].annotations.length);
763 System.arraycopy(restore, 0, temp,
764 annotations[a].annotations.length, restore.length);
768 temp = annotations[a].annotations;
774 if (tSize != aSize || command.position < 2)
776 int copylen = Math.min(command.position,
777 annotations[a].annotations.length);
779 System.arraycopy(annotations[a].annotations, 0, temp, 0,
780 copylen); // command.position);
782 Annotation[] deleted = new Annotation[command.number];
783 if (copylen >= command.position)
785 copylen = Math.min(command.number,
786 annotations[a].annotations.length - command.position);
789 System.arraycopy(annotations[a].annotations,
790 command.position, deleted, 0, copylen); // command.number);
794 command.deletedAnnotations.put(annotations[a].annotationId,
796 if (annotations[a].annotations.length > command.position
799 System.arraycopy(annotations[a].annotations, command.position
800 + command.number, temp, command.position,
801 annotations[a].annotations.length - command.position
802 - command.number); // aSize
807 int dSize = aSize - command.position;
811 Annotation[] deleted = new Annotation[command.number];
812 System.arraycopy(annotations[a].annotations, command.position,
815 command.deletedAnnotations.put(annotations[a].annotationId,
818 tSize = Math.min(annotations[a].annotations.length,
820 temp = new Annotation[tSize];
821 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
825 temp = annotations[a].annotations;
830 annotations[a].annotations = temp;
834 final void adjustFeatures(Edit command, int index, int i, int j,
837 SequenceI seq = command.seqs[index];
838 SequenceI sequence = seq.getDatasetSequence();
839 if (sequence == null)
846 if (command.editedFeatures != null
847 && command.editedFeatures.containsKey(seq))
849 sequence.setSequenceFeatures((SequenceFeature[]) command.editedFeatures
856 SequenceFeature[] sf = sequence.getSequenceFeatures();
863 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
867 for (int s = 0; s < sf.length; s++)
869 SequenceFeature copy = new SequenceFeature(sf[s]);
873 if (sf[s].getEnd() < i)
878 if (sf[s].getBegin() > j)
880 sf[s].setBegin(copy.getBegin() - cSize);
881 sf[s].setEnd(copy.getEnd() - cSize);
885 if (sf[s].getBegin() >= i)
890 if (sf[s].getEnd() < j)
895 sf[s].setEnd(sf[s].getEnd() - (cSize));
897 if (sf[s].getBegin() > sf[s].getEnd())
899 sequence.deleteFeature(sf[s]);
903 if (command.editedFeatures == null)
905 command.editedFeatures = new Hashtable();
908 command.editedFeatures.put(seq, oldsf);
914 public SequenceI[] oldds;
916 boolean fullAlignmentHeight = false;
918 Hashtable deletedAnnotationRows;
920 Hashtable deletedAnnotations;
922 Hashtable editedFeatures;
934 int position, number;
938 Edit(int command, SequenceI[] seqs, int position, int number,
941 this.command = command;
943 this.position = position;
944 this.number = number;
945 this.gapChar = gapChar;
948 Edit(int command, SequenceI[] seqs, int position, int number,
951 this.gapChar = al.getGapCharacter();
952 this.command = command;
954 this.position = position;
955 this.number = number;
958 alIndex = new int[seqs.length];
959 for (int i = 0; i < seqs.length; i++)
961 alIndex[i] = al.findIndex(seqs[i]);
964 fullAlignmentHeight = (al.getHeight() == seqs.length);
967 Edit(int command, SequenceI[] seqs, int position, int number,
968 AlignmentI al, String replace)
970 this.command = command;
972 this.position = position;
973 this.number = number;
975 this.gapChar = al.getGapCharacter();
976 string = new char[seqs.length][];
977 for (int i = 0; i < seqs.length; i++)
979 string[i] = replace.toCharArray();
982 fullAlignmentHeight = (al.getHeight() == seqs.length);