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;
25 import jalview.datamodel.*;
34 * Description: Essential information for performing undo and redo for cut/paste
35 * insert/delete gap which can be stored in the HistoryList
39 * Copyright: Copyright (c) 2006
43 * Company: Dundee University
46 * @author not attributable
49 public class EditCommand implements CommandI
51 public static final int INSERT_GAP = 0;
53 public static final int DELETE_GAP = 1;
55 public static final int CUT = 2;
57 public static final int PASTE = 3;
59 public static final int REPLACE = 4;
61 public static final int INSERT_NUC = 5;
71 public EditCommand(String description)
73 this.description = description;
76 public EditCommand(String description, int command, SequenceI[] seqs,
77 int position, int number, AlignmentI al)
79 this.description = description;
80 if (command == CUT || command == PASTE)
83 { new Edit(command, seqs, position, number, al) };
89 public EditCommand(String description, int command, String replace,
90 SequenceI[] seqs, int position, int number, AlignmentI al)
92 this.description = description;
93 if (command == REPLACE)
96 { new Edit(command, seqs, position, number, al, replace) };
103 final public String getDescription()
111 return edits == null ? 0 : edits.length;
114 final public AlignmentI getAlignment()
120 * append a new editCommand Note. this shouldn't be called if the edit is an
121 * operation affects more alignment objects than the one referenced in al (for
122 * example, cut or pasting whole sequences). Use the form with an additional
123 * AlignmentI[] views parameter.
132 final public void appendEdit(int command, SequenceI[] seqs, int position,
133 int number, AlignmentI al, boolean performEdit)
135 appendEdit(command, seqs, position, number, al, performEdit, null);
139 * append a new edit command with a set of alignment views that may be
150 final public void appendEdit(int command, SequenceI[] seqs, int position,
151 int number, AlignmentI al, boolean performEdit, AlignmentI[] views)
153 Edit edit = new Edit(command, seqs, position, number,
154 al.getGapCharacter());
155 if (al.getHeight() == seqs.length)
158 edit.fullAlignmentHeight = true;
163 Edit[] temp = new Edit[edits.length + 1];
164 System.arraycopy(edits, 0, temp, 0, edits.length);
166 edits[edits.length - 1] = edit;
176 performEdit(edits.length - 1, views);
180 final void performEdit(int commandIndex, AlignmentI[] views)
182 int eSize = edits.length;
183 for (int e = commandIndex; e < eSize; e++)
185 switch (edits[e].command)
194 cut(edits[e], views);
197 paste(edits[e], views);
202 // TODO:add deleteNuc for UNDO
204 // insertNuc(edits[e]);
211 final public void doCommand(AlignmentI[] views)
213 performEdit(0, views);
217 final public void undoCommand(AlignmentI[] views)
219 int e = 0, eSize = edits.length;
220 for (e = eSize - 1; e > -1; e--)
222 switch (edits[e].command)
231 paste(edits[e], views);
234 cut(edits[e], views);
243 final void insertGap(Edit command)
246 for (int s = 0; s < command.seqs.length; s++)
248 command.seqs[s].insertCharAt(command.position, command.number,
250 // System.out.println("pos: "+command.position+" number: "+command.number);
253 adjustAnnotations(command, true, false, null);
257 // final void insertNuc(Edit command)
260 // for (int s = 0; s < command.seqs.length; s++)
262 // System.out.println("pos: "+command.position+" number: "+command.number);
263 // command.seqs[s].insertCharAt(command.position, command.number,'A');
266 // adjustAnnotations(command, true, false, null);
269 final void deleteGap(Edit command)
271 for (int s = 0; s < command.seqs.length; s++)
273 command.seqs[s].deleteChars(command.position, command.position
277 adjustAnnotations(command, false, false, null);
280 void cut(Edit command, AlignmentI[] views)
282 boolean seqDeleted = false;
283 command.string = new char[command.seqs.length][];
285 for (int i = 0; i < command.seqs.length; i++)
287 if (command.seqs[i].getLength() > command.position)
289 command.string[i] = command.seqs[i].getSequence(command.position,
290 command.position + command.number);
291 SequenceI oldds = command.seqs[i].getDatasetSequence();
292 if (command.oldds != null && command.oldds[i] != null)
294 // we are redoing an undone cut.
295 command.seqs[i].setDatasetSequence(null);
297 command.seqs[i].deleteChars(command.position, command.position
299 if (command.oldds != null && command.oldds[i] != null)
301 // oldds entry contains the cut dataset sequence.
302 command.seqs[i].setDatasetSequence(command.oldds[i]);
303 command.oldds[i] = oldds;
307 // modify the oldds if necessary
308 if (oldds != command.seqs[i].getDatasetSequence()
309 || command.seqs[i].getSequenceFeatures() != null)
311 if (command.oldds == null)
313 command.oldds = new SequenceI[command.seqs.length];
315 command.oldds[i] = oldds;
319 command.seqs[i].findPosition(command.position),
320 command.seqs[i].findPosition(command.position
321 + command.number), false);
326 if (command.seqs[i].getLength() < 1)
328 command.al.deleteSequence(command.seqs[i]);
333 adjustAnnotations(command, false, seqDeleted, views);
336 void paste(Edit command, AlignmentI[] views)
340 boolean newDSWasNeeded;
341 int newstart, newend;
342 boolean seqWasDeleted = false;
343 int start = 0, end = 0;
345 for (int i = 0; i < command.seqs.length; i++)
348 newDSWasNeeded = command.oldds != null && command.oldds[i] != null;
349 if (command.seqs[i].getLength() < 1)
351 // ie this sequence was deleted, we need to
352 // read it to the alignment
353 if (command.alIndex[i] < command.al.getHeight())
355 List<SequenceI> sequences;
356 synchronized (sequences = command.al.getSequences())
358 sequences.add(command.alIndex[i], command.seqs[i]);
363 command.al.addSequence(command.seqs[i]);
365 seqWasDeleted = true;
367 newstart = command.seqs[i].getStart();
368 newend = command.seqs[i].getEnd();
370 tmp = new StringBuffer();
371 tmp.append(command.seqs[i].getSequence());
372 // Undo of a delete does not replace original dataset sequence on to
373 // alignment sequence.
375 if (command.string != null && command.string[i] != null)
377 if (command.position >= tmp.length())
379 // This occurs if padding is on, and residues
380 // are removed from end of alignment
381 int length = command.position - tmp.length();
384 tmp.append(command.gapChar);
388 tmp.insert(command.position, command.string[i]);
389 for (int s = 0; s < command.string[i].length; s++)
391 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
396 start = command.seqs[i].findPosition(command.position);
397 end = command.seqs[i].findPosition(command.position
400 if (command.seqs[i].getStart() == start)
406 command.string[i] = null;
409 command.seqs[i].setSequence(tmp.toString());
410 command.seqs[i].setStart(newstart);
411 command.seqs[i].setEnd(newend);
414 if (command.seqs[i].getDatasetSequence() != null)
419 ds = command.oldds[i];
423 // make a new DS sequence
424 // use new ds mechanism here
425 ds = new Sequence(command.seqs[i].getName(),
426 jalview.analysis.AlignSeq.extractGaps(
427 jalview.util.Comparison.GapChars,
428 command.seqs[i].getSequenceAsString()),
429 command.seqs[i].getStart(), command.seqs[i].getEnd());
430 ds.setDescription(command.seqs[i].getDescription());
432 if (command.oldds == null)
434 command.oldds = new SequenceI[command.seqs.length];
436 command.oldds[i] = command.seqs[i].getDatasetSequence();
437 command.seqs[i].setDatasetSequence(ds);
439 adjustFeatures(command, i, start, end, true);
442 adjustAnnotations(command, true, seqWasDeleted, views);
444 command.string = null;
447 void replace(Edit command)
451 int start = command.position;
452 int end = command.number;
453 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
455 // TODO Jalview 2.4 bugfix change to an aggregate command - original
456 // sequence string is cut, new string is pasted in.
457 command.number = start + command.string[0].length;
458 for (int i = 0; i < command.seqs.length; i++)
460 boolean newDSWasNeeded = command.oldds != null
461 && command.oldds[i] != null;
464 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
465 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
466 * viewport.alignment));
470 * then addHistoryItem(new EditCommand( "Add sequences",
471 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
474 oldstring = command.seqs[i].getSequenceAsString();
475 tmp = new StringBuffer(oldstring.substring(0, start));
476 tmp.append(command.string[i]);
477 String nogaprep = jalview.analysis.AlignSeq.extractGaps(
478 jalview.util.Comparison.GapChars, new String(
480 int ipos = command.seqs[i].findPosition(start)
481 - command.seqs[i].getStart();
482 tmp.append(oldstring.substring(end));
483 command.seqs[i].setSequence(tmp.toString());
484 command.string[i] = oldstring.substring(start, end).toCharArray();
485 String nogapold = jalview.analysis.AlignSeq.extractGaps(
486 jalview.util.Comparison.GapChars, new String(
488 if (!nogaprep.toLowerCase().equals(nogapold.toLowerCase()))
492 SequenceI oldds = command.seqs[i].getDatasetSequence();
493 command.seqs[i].setDatasetSequence(command.oldds[i]);
494 command.oldds[i] = oldds;
498 if (command.oldds == null)
500 command.oldds = new SequenceI[command.seqs.length];
502 command.oldds[i] = command.seqs[i].getDatasetSequence();
503 SequenceI newds = new Sequence(
504 command.seqs[i].getDatasetSequence());
505 String fullseq, osp = newds.getSequenceAsString();
506 fullseq = osp.substring(0, ipos) + nogaprep
507 + osp.substring(ipos + nogaprep.length());
508 newds.setSequence(fullseq.toUpperCase());
509 // TODO: JAL-1131 ensure newly created dataset sequence is added to
511 // dataset sequences associated with the alignment.
512 // TODO: JAL-1131 fix up any annotation associated with new dataset
513 // sequence to ensure that original sequence/annotation relationships
515 command.seqs[i].setDatasetSequence(newds);
524 final void adjustAnnotations(Edit command, boolean insert,
525 boolean modifyVisibility, AlignmentI[] views)
527 AlignmentAnnotation[] annotations = null;
529 if (modifyVisibility && !insert)
531 // only occurs if a sequence was added or deleted.
532 command.deletedAnnotationRows = new Hashtable();
534 if (command.fullAlignmentHeight)
536 annotations = command.al.getAlignmentAnnotation();
541 AlignmentAnnotation[] tmp;
542 for (int s = 0; s < command.seqs.length; s++)
544 if (modifyVisibility)
546 // Rows are only removed or added to sequence object.
550 tmp = command.seqs[s].getAnnotation();
553 int alen = tmp.length;
554 for (int aa = 0; aa < tmp.length; aa++)
556 if (!command.al.deleteAnnotation(tmp[aa]))
558 // strip out annotation not in the current al (will be put
559 // back on insert in all views)
564 command.seqs[s].setAlignmentAnnotation(null);
565 if (alen != tmp.length)
567 // save the non-null annotation references only
568 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
569 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
573 saved[aapos++] = tmp[aa];
578 command.deletedAnnotationRows.put(command.seqs[s], saved);
579 // and then remove any annotation in the other views
580 for (int alview = 0; views != null && alview < views.length; alview++)
582 if (views[alview] != command.al)
584 AlignmentAnnotation[] toremove = views[alview]
585 .getAlignmentAnnotation();
586 if (toremove == null || toremove.length == 0)
590 // remove any alignment annotation on this sequence that's
591 // on that alignment view.
592 for (int aa = 0; aa < toremove.length; aa++)
594 if (toremove[aa].sequenceRef == command.seqs[s])
596 views[alview].deleteAnnotation(toremove[aa]);
604 // save all the annotation
605 command.deletedAnnotationRows.put(command.seqs[s], tmp);
612 if (command.deletedAnnotationRows != null
613 && command.deletedAnnotationRows
614 .containsKey(command.seqs[s]))
616 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
617 .get(command.seqs[s]);
618 command.seqs[s].setAlignmentAnnotation(revealed);
619 if (revealed != null)
621 for (int aa = 0; aa < revealed.length; aa++)
623 // iterate through al adding original annotation
624 command.al.addAnnotation(revealed[aa]);
626 for (int aa = 0; aa < revealed.length; aa++)
628 command.al.setAnnotationIndex(revealed[aa], aa);
630 // and then duplicate added annotation on every other alignment
632 for (int vnum = 0; views != null && vnum < views.length; vnum++)
634 if (views[vnum] != command.al)
636 int avwidth = views[vnum].getWidth() + 1;
637 // duplicate in this view
638 for (int a = 0; a < revealed.length; a++)
640 AlignmentAnnotation newann = new AlignmentAnnotation(
642 command.seqs[s].addAlignmentAnnotation(newann);
643 newann.padAnnotation(avwidth);
644 views[vnum].addAnnotation(newann);
645 views[vnum].setAnnotationIndex(newann, a);
655 if (command.seqs[s].getAnnotation() == null)
662 annotations = command.seqs[s].getAnnotation();
666 tmp = new AlignmentAnnotation[aSize
667 + command.seqs[s].getAnnotation().length];
669 System.arraycopy(annotations, 0, tmp, 0, aSize);
671 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
672 command.seqs[s].getAnnotation().length);
676 aSize = annotations.length;
680 if (annotations == null)
687 command.deletedAnnotations = new Hashtable();
692 for (int a = 0; a < annotations.length; a++)
694 if (annotations[a].autoCalculated
695 || annotations[a].annotations == null)
702 aSize = annotations[a].annotations.length;
705 temp = new Annotation[aSize + command.number];
706 if (annotations[a].padGaps)
707 for (int aa = 0; aa < temp.length; aa++)
709 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
714 if (command.position < aSize)
716 if (command.position + command.number >= aSize)
722 tSize = aSize - command.number;
734 temp = new Annotation[tSize];
739 if (command.position < annotations[a].annotations.length)
741 System.arraycopy(annotations[a].annotations, 0, temp, 0,
744 if (command.deletedAnnotations != null
745 && command.deletedAnnotations
746 .containsKey(annotations[a].annotationId))
748 Annotation[] restore = (Annotation[]) command.deletedAnnotations
749 .get(annotations[a].annotationId);
751 System.arraycopy(restore, 0, temp, command.position,
756 System.arraycopy(annotations[a].annotations, command.position,
757 temp, command.position + command.number, aSize
762 if (command.deletedAnnotations != null
763 && command.deletedAnnotations
764 .containsKey(annotations[a].annotationId))
766 Annotation[] restore = (Annotation[]) command.deletedAnnotations
767 .get(annotations[a].annotationId);
769 temp = new Annotation[annotations[a].annotations.length
771 System.arraycopy(annotations[a].annotations, 0, temp, 0,
772 annotations[a].annotations.length);
773 System.arraycopy(restore, 0, temp,
774 annotations[a].annotations.length, restore.length);
778 temp = annotations[a].annotations;
784 if (tSize != aSize || command.position < 2)
786 int copylen = Math.min(command.position,
787 annotations[a].annotations.length);
789 System.arraycopy(annotations[a].annotations, 0, temp, 0,
790 copylen); // command.position);
792 Annotation[] deleted = new Annotation[command.number];
793 if (copylen >= command.position)
795 copylen = Math.min(command.number,
796 annotations[a].annotations.length - command.position);
799 System.arraycopy(annotations[a].annotations,
800 command.position, deleted, 0, copylen); // command.number);
804 command.deletedAnnotations.put(annotations[a].annotationId,
806 if (annotations[a].annotations.length > command.position
809 System.arraycopy(annotations[a].annotations, command.position
810 + command.number, temp, command.position,
811 annotations[a].annotations.length - command.position
812 - command.number); // aSize
817 int dSize = aSize - command.position;
821 Annotation[] deleted = new Annotation[command.number];
822 System.arraycopy(annotations[a].annotations, command.position,
825 command.deletedAnnotations.put(annotations[a].annotationId,
828 tSize = Math.min(annotations[a].annotations.length,
830 temp = new Annotation[tSize];
831 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
835 temp = annotations[a].annotations;
840 annotations[a].annotations = temp;
844 final void adjustFeatures(Edit command, int index, int i, int j,
847 SequenceI seq = command.seqs[index];
848 SequenceI sequence = seq.getDatasetSequence();
849 if (sequence == null)
856 if (command.editedFeatures != null
857 && command.editedFeatures.containsKey(seq))
859 sequence.setSequenceFeatures((SequenceFeature[]) command.editedFeatures
866 SequenceFeature[] sf = sequence.getSequenceFeatures();
873 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
877 for (int s = 0; s < sf.length; s++)
879 SequenceFeature copy = new SequenceFeature(sf[s]);
883 if (sf[s].getEnd() < i)
888 if (sf[s].getBegin() > j)
890 sf[s].setBegin(copy.getBegin() - cSize);
891 sf[s].setEnd(copy.getEnd() - cSize);
895 if (sf[s].getBegin() >= i)
900 if (sf[s].getEnd() < j)
905 sf[s].setEnd(sf[s].getEnd() - (cSize));
907 if (sf[s].getBegin() > sf[s].getEnd())
909 sequence.deleteFeature(sf[s]);
913 if (command.editedFeatures == null)
915 command.editedFeatures = new Hashtable();
918 command.editedFeatures.put(seq, oldsf);
924 public SequenceI[] oldds;
926 boolean fullAlignmentHeight = false;
928 Hashtable deletedAnnotationRows;
930 Hashtable deletedAnnotations;
932 Hashtable editedFeatures;
944 int position, number;
948 Edit(int command, SequenceI[] seqs, int position, int number,
951 this.command = command;
953 this.position = position;
954 this.number = number;
955 this.gapChar = gapChar;
958 Edit(int command, SequenceI[] seqs, int position, int number,
961 this.gapChar = al.getGapCharacter();
962 this.command = command;
964 this.position = position;
965 this.number = number;
968 alIndex = new int[seqs.length];
969 for (int i = 0; i < seqs.length; i++)
971 alIndex[i] = al.findIndex(seqs[i]);
974 fullAlignmentHeight = (al.getHeight() == seqs.length);
977 Edit(int command, SequenceI[] seqs, int position, int number,
978 AlignmentI al, String replace)
980 this.command = command;
982 this.position = position;
983 this.number = number;
985 this.gapChar = al.getGapCharacter();
986 string = new char[seqs.length][];
987 for (int i = 0; i < seqs.length; i++)
989 string[i] = replace.toCharArray();
992 fullAlignmentHeight = (al.getHeight() == seqs.length);