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 // int index = command.al.findIndex(command.seqs[i]);
363 // sequences.add(index, command.seqs[i]);
364 sequences.add(command.alIndex[i] < 0 ? 0 : command.alIndex[i], command.seqs[i]);
369 command.al.addSequence(command.seqs[i]);
371 seqWasDeleted = true;
373 newstart = command.seqs[i].getStart();
374 newend = command.seqs[i].getEnd();
376 tmp = new StringBuffer();
377 tmp.append(command.seqs[i].getSequence());
378 // Undo of a delete does not replace original dataset sequence on to
379 // alignment sequence.
381 if (command.string != null && command.string[i] != null)
383 if (command.position >= tmp.length())
385 // This occurs if padding is on, and residues
386 // are removed from end of alignment
387 int length = command.position - tmp.length();
390 tmp.append(command.gapChar);
394 tmp.insert(command.position, command.string[i]);
395 for (int s = 0; s < command.string[i].length; s++)
397 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
402 start = command.seqs[i].findPosition(command.position);
403 end = command.seqs[i].findPosition(command.position
406 if (command.seqs[i].getStart() == start)
416 command.string[i] = null;
419 command.seqs[i].setSequence(tmp.toString());
420 command.seqs[i].setStart(newstart);
421 command.seqs[i].setEnd(newend);
424 if (command.seqs[i].getDatasetSequence() != null)
429 ds = command.oldds[i];
433 // make a new DS sequence
434 // use new ds mechanism here
435 ds = new Sequence(command.seqs[i].getName(),
436 jalview.analysis.AlignSeq.extractGaps(
437 jalview.util.Comparison.GapChars,
438 command.seqs[i].getSequenceAsString()),
439 command.seqs[i].getStart(), command.seqs[i].getEnd());
440 ds.setDescription(command.seqs[i].getDescription());
442 if (command.oldds == null)
444 command.oldds = new SequenceI[command.seqs.length];
446 command.oldds[i] = command.seqs[i].getDatasetSequence();
447 command.seqs[i].setDatasetSequence(ds);
449 adjustFeatures(command, i, start, end, true);
452 adjustAnnotations(command, true, seqWasDeleted, views);
454 command.string = null;
457 void replace(Edit command)
461 int start = command.position;
462 int end = command.number;
463 // TODO TUTORIAL - Fix for replacement with different length of sequence (or
465 // TODO Jalview 2.4 bugfix change to an aggregate command - original
466 // sequence string is cut, new string is pasted in.
467 command.number = start + command.string[0].length;
468 for (int i = 0; i < command.seqs.length; i++)
470 boolean newDSWasNeeded = command.oldds != null
471 && command.oldds[i] != null;
474 * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
475 * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
476 * viewport.alignment));
480 * then addHistoryItem(new EditCommand( "Add sequences",
481 * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
484 oldstring = command.seqs[i].getSequenceAsString();
485 tmp = new StringBuffer(oldstring.substring(0, start));
486 tmp.append(command.string[i]);
487 String nogaprep = jalview.analysis.AlignSeq.extractGaps(
488 jalview.util.Comparison.GapChars, new String(
490 int ipos = command.seqs[i].findPosition(start)
491 - command.seqs[i].getStart();
492 tmp.append(oldstring.substring(end));
493 command.seqs[i].setSequence(tmp.toString());
494 command.string[i] = oldstring.substring(start, end).toCharArray();
495 String nogapold = jalview.analysis.AlignSeq.extractGaps(
496 jalview.util.Comparison.GapChars, new String(
498 if (!nogaprep.toLowerCase().equals(nogapold.toLowerCase()))
502 SequenceI oldds = command.seqs[i].getDatasetSequence();
503 command.seqs[i].setDatasetSequence(command.oldds[i]);
504 command.oldds[i] = oldds;
508 if (command.oldds == null)
510 command.oldds = new SequenceI[command.seqs.length];
512 command.oldds[i] = command.seqs[i].getDatasetSequence();
513 SequenceI newds = new Sequence(
514 command.seqs[i].getDatasetSequence());
515 String fullseq, osp = newds.getSequenceAsString();
516 fullseq = osp.substring(0, ipos) + nogaprep
517 + osp.substring(ipos + nogaprep.length());
518 newds.setSequence(fullseq.toUpperCase());
519 // TODO: JAL-1131 ensure newly created dataset sequence is added to
521 // dataset sequences associated with the alignment.
522 // TODO: JAL-1131 fix up any annotation associated with new dataset
523 // sequence to ensure that original sequence/annotation relationships
525 command.seqs[i].setDatasetSequence(newds);
534 final void adjustAnnotations(Edit command, boolean insert,
535 boolean modifyVisibility, AlignmentI[] views)
537 AlignmentAnnotation[] annotations = null;
539 if (modifyVisibility && !insert)
541 // only occurs if a sequence was added or deleted.
542 command.deletedAnnotationRows = new Hashtable();
544 if (command.fullAlignmentHeight)
546 annotations = command.al.getAlignmentAnnotation();
551 AlignmentAnnotation[] tmp;
552 for (int s = 0; s < command.seqs.length; s++)
554 if (modifyVisibility)
556 // Rows are only removed or added to sequence object.
560 tmp = command.seqs[s].getAnnotation();
563 int alen = tmp.length;
564 for (int aa = 0; aa < tmp.length; aa++)
566 if (!command.al.deleteAnnotation(tmp[aa]))
568 // strip out annotation not in the current al (will be put
569 // back on insert in all views)
574 command.seqs[s].setAlignmentAnnotation(null);
575 if (alen != tmp.length)
577 // save the non-null annotation references only
578 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
579 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
583 saved[aapos++] = tmp[aa];
588 command.deletedAnnotationRows.put(command.seqs[s], saved);
589 // and then remove any annotation in the other views
590 for (int alview = 0; views != null && alview < views.length; alview++)
592 if (views[alview] != command.al)
594 AlignmentAnnotation[] toremove = views[alview]
595 .getAlignmentAnnotation();
596 if (toremove == null || toremove.length == 0)
600 // remove any alignment annotation on this sequence that's
601 // on that alignment view.
602 for (int aa = 0; aa < toremove.length; aa++)
604 if (toremove[aa].sequenceRef == command.seqs[s])
606 views[alview].deleteAnnotation(toremove[aa]);
614 // save all the annotation
615 command.deletedAnnotationRows.put(command.seqs[s], tmp);
622 if (command.deletedAnnotationRows != null
623 && command.deletedAnnotationRows
624 .containsKey(command.seqs[s]))
626 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows
627 .get(command.seqs[s]);
628 command.seqs[s].setAlignmentAnnotation(revealed);
629 if (revealed != null)
631 for (int aa = 0; aa < revealed.length; aa++)
633 // iterate through al adding original annotation
634 command.al.addAnnotation(revealed[aa]);
636 for (int aa = 0; aa < revealed.length; aa++)
638 command.al.setAnnotationIndex(revealed[aa], aa);
640 // and then duplicate added annotation on every other alignment
642 for (int vnum = 0; views != null && vnum < views.length; vnum++)
644 if (views[vnum] != command.al)
646 int avwidth = views[vnum].getWidth() + 1;
647 // duplicate in this view
648 for (int a = 0; a < revealed.length; a++)
650 AlignmentAnnotation newann = new AlignmentAnnotation(
652 command.seqs[s].addAlignmentAnnotation(newann);
653 newann.padAnnotation(avwidth);
654 views[vnum].addAnnotation(newann);
655 views[vnum].setAnnotationIndex(newann, a);
665 if (command.seqs[s].getAnnotation() == null)
672 annotations = command.seqs[s].getAnnotation();
676 tmp = new AlignmentAnnotation[aSize
677 + command.seqs[s].getAnnotation().length];
679 System.arraycopy(annotations, 0, tmp, 0, aSize);
681 System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
682 command.seqs[s].getAnnotation().length);
686 aSize = annotations.length;
690 if (annotations == null)
697 command.deletedAnnotations = new Hashtable();
702 for (int a = 0; a < annotations.length; a++)
704 if (annotations[a].autoCalculated
705 || annotations[a].annotations == null)
712 aSize = annotations[a].annotations.length;
715 temp = new Annotation[aSize + command.number];
716 if (annotations[a].padGaps)
718 for (int aa = 0; aa < temp.length; aa++)
720 temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
726 if (command.position < aSize)
728 if (command.position + command.number >= aSize)
734 tSize = aSize - command.number;
746 temp = new Annotation[tSize];
751 if (command.position < annotations[a].annotations.length)
753 System.arraycopy(annotations[a].annotations, 0, temp, 0,
756 if (command.deletedAnnotations != null
757 && command.deletedAnnotations
758 .containsKey(annotations[a].annotationId))
760 Annotation[] restore = (Annotation[]) command.deletedAnnotations
761 .get(annotations[a].annotationId);
763 System.arraycopy(restore, 0, temp, command.position,
768 System.arraycopy(annotations[a].annotations, command.position,
769 temp, command.position + command.number, aSize
774 if (command.deletedAnnotations != null
775 && command.deletedAnnotations
776 .containsKey(annotations[a].annotationId))
778 Annotation[] restore = (Annotation[]) command.deletedAnnotations
779 .get(annotations[a].annotationId);
781 temp = new Annotation[annotations[a].annotations.length
783 System.arraycopy(annotations[a].annotations, 0, temp, 0,
784 annotations[a].annotations.length);
785 System.arraycopy(restore, 0, temp,
786 annotations[a].annotations.length, restore.length);
790 temp = annotations[a].annotations;
796 if (tSize != aSize || command.position < 2)
798 int copylen = Math.min(command.position,
799 annotations[a].annotations.length);
802 System.arraycopy(annotations[a].annotations, 0, temp, 0,
803 copylen); // command.position);
806 Annotation[] deleted = new Annotation[command.number];
807 if (copylen >= command.position)
809 copylen = Math.min(command.number,
810 annotations[a].annotations.length - command.position);
813 System.arraycopy(annotations[a].annotations,
814 command.position, deleted, 0, copylen); // command.number);
818 command.deletedAnnotations.put(annotations[a].annotationId,
820 if (annotations[a].annotations.length > command.position
823 System.arraycopy(annotations[a].annotations, command.position
824 + command.number, temp, command.position,
825 annotations[a].annotations.length - command.position
826 - command.number); // aSize
831 int dSize = aSize - command.position;
835 Annotation[] deleted = new Annotation[command.number];
836 System.arraycopy(annotations[a].annotations, command.position,
839 command.deletedAnnotations.put(annotations[a].annotationId,
842 tSize = Math.min(annotations[a].annotations.length,
844 temp = new Annotation[tSize];
845 System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
849 temp = annotations[a].annotations;
854 annotations[a].annotations = temp;
858 final void adjustFeatures(Edit command, int index, int i, int j,
861 SequenceI seq = command.seqs[index];
862 SequenceI sequence = seq.getDatasetSequence();
863 if (sequence == null)
870 if (command.editedFeatures != null
871 && command.editedFeatures.containsKey(seq))
873 sequence.setSequenceFeatures((SequenceFeature[]) command.editedFeatures
880 SequenceFeature[] sf = sequence.getSequenceFeatures();
887 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
891 for (int s = 0; s < sf.length; s++)
893 SequenceFeature copy = new SequenceFeature(sf[s]);
897 if (sf[s].getEnd() < i)
902 if (sf[s].getBegin() > j)
904 sf[s].setBegin(copy.getBegin() - cSize);
905 sf[s].setEnd(copy.getEnd() - cSize);
909 if (sf[s].getBegin() >= i)
914 if (sf[s].getEnd() < j)
919 sf[s].setEnd(sf[s].getEnd() - (cSize));
921 if (sf[s].getBegin() > sf[s].getEnd())
923 sequence.deleteFeature(sf[s]);
927 if (command.editedFeatures == null)
929 command.editedFeatures = new Hashtable();
932 command.editedFeatures.put(seq, oldsf);
938 public SequenceI[] oldds;
940 boolean fullAlignmentHeight = false;
942 Hashtable deletedAnnotationRows;
944 Hashtable deletedAnnotations;
946 Hashtable editedFeatures;
958 int position, number;
962 Edit(int command, SequenceI[] seqs, int position, int number,
965 this.command = command;
967 this.position = position;
968 this.number = number;
969 this.gapChar = gapChar;
972 Edit(int command, SequenceI[] seqs, int position, int number,
975 this.gapChar = al.getGapCharacter();
976 this.command = command;
978 this.position = position;
979 this.number = number;
982 alIndex = new int[seqs.length];
983 for (int i = 0; i < seqs.length; i++)
985 alIndex[i] = al.findIndex(seqs[i]);
988 fullAlignmentHeight = (al.getHeight() == seqs.length);
991 Edit(int command, SequenceI[] seqs, int position, int number,
992 AlignmentI al, String replace)
994 this.command = command;
996 this.position = position;
997 this.number = number;
999 this.gapChar = al.getGapCharacter();
1000 string = new char[seqs.length][];
1001 for (int i = 0; i < seqs.length; i++)
1003 string[i] = replace.toCharArray();
1006 fullAlignmentHeight = (al.getHeight() == seqs.length);