2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.commands;
23 import jalview.datamodel.*;
27 * <p>Title: EditCommmand</p>
29 * <p>Description: Essential information for performing
30 * undo and redo for cut/paste insert/delete gap
31 * which can be stored in the HistoryList </p>
33 * <p>Copyright: Copyright (c) 2006</p>
35 * <p>Company: Dundee University</p>
37 * @author not attributable
40 public class EditCommand
43 public static final int INSERT_GAP = 0;
44 public static final int DELETE_GAP = 1;
45 public static final int CUT = 2;
46 public static final int PASTE = 3;
47 public static final int REPLACE = 4;
56 public EditCommand(String description)
58 this.description = description;
61 public EditCommand(String description,
68 this.description = description;
69 if (command == CUT || command == PASTE)
73 new Edit(command, seqs, position, number, al)};
79 public EditCommand(String description,
87 this.description = description;
88 if (command == REPLACE)
91 { new Edit(command, seqs, position, number, al, replace)};
97 final public String getDescription()
104 return edits == null ? 0 : edits.length;
107 final public AlignmentI getAlignment()
113 * append a new editCommand
114 * Note. this shouldn't be called if the edit is an operation affects more alignment objects than the one referenced
115 * in al (for example, cut or pasting whole sequences). Use the form with an additional AlignmentI[] views parameter.
123 final public void appendEdit(int command,
130 appendEdit(command, seqs, position, number, al, performEdit, null);
133 * append a new edit command with a set of alignment views that may be operated on
142 final public void appendEdit(int command,
147 boolean performEdit, AlignmentI[] views)
149 Edit edit = new Edit(command, seqs, position, number, al.getGapCharacter());
150 if (al.getHeight() == seqs.length)
153 edit.fullAlignmentHeight = true;
158 Edit[] temp = new Edit[edits.length + 1];
159 System.arraycopy(edits, 0, temp, 0, edits.length);
161 edits[edits.length - 1] = edit;
172 performEdit(edits.length - 1, views);
176 final void performEdit(int commandIndex, AlignmentI[] views)
178 int eSize = edits.length;
179 for (int e = commandIndex; e < eSize; e++)
181 switch(edits[e].command)
190 cut(edits[e], views);
193 paste(edits[e], views);
202 final public void doCommand(AlignmentI[] views)
204 performEdit(0,views);
207 final public void undoCommand(AlignmentI[] views)
209 int e = 0, eSize = edits.length;
210 for (e = eSize - 1; e > -1; e--)
212 switch (edits[e].command)
221 paste(edits[e], views);
224 cut(edits[e], views);
233 final void insertGap(Edit command)
236 for (int s = 0; s < command.seqs.length; s++)
238 command.seqs[s].insertCharAt(command.position,
243 adjustAnnotations(command, true, false, null);
246 final void deleteGap(Edit command)
248 for (int s = 0; s < command.seqs.length; s++)
250 command.seqs[s].deleteChars(command.position,
251 command.position + command.number);
254 adjustAnnotations(command, false, false, null);
257 void cut(Edit command, AlignmentI[] views)
259 boolean seqDeleted=false;
260 command.string = new char[command.seqs.length][];
262 for (int i = 0; i < command.seqs.length; i++)
264 if (command.seqs[i].getLength() > command.position)
266 command.string[i] = command.seqs[i].getSequence(command.position,
267 command.position + command.number);
268 SequenceI oldds = command.seqs[i].getDatasetSequence();
269 if (command.oldds!=null && command.oldds[i]!=null)
271 // we are redoing an undone cut.
272 command.seqs[i].setDatasetSequence(null);
274 command.seqs[i].deleteChars(command.position,
275 command.position + command.number);
276 if (command.oldds!=null && command.oldds[i]!=null)
278 // oldds entry contains the cut dataset sequence.
279 command.seqs[i].setDatasetSequence(command.oldds[i]);
280 command.oldds[i] = oldds;
282 // modify the oldds if necessary
283 if (oldds!=command.seqs[i].getDatasetSequence()
284 || command.seqs[i].getSequenceFeatures() != null)
286 if (command.oldds==null)
288 command.oldds = new SequenceI[command.seqs.length];
290 command.oldds[i] = oldds;
291 adjustFeatures(command, i,
292 command.seqs[i].findPosition(command.position),
293 command.seqs[i].findPosition(command.position +
300 if (command.seqs[i].getLength() < 1)
302 command.al.deleteSequence(command.seqs[i]);
307 adjustAnnotations(command, false, seqDeleted, views);
310 void paste(Edit command, AlignmentI[] views)
314 boolean newDSWasNeeded;
316 boolean seqWasDeleted=false;
317 int start = 0, end = 0;
319 for (int i = 0; i < command.seqs.length; i++)
322 newDSWasNeeded = command.oldds!=null && command.oldds[i]!=null;
323 if (command.seqs[i].getLength() < 1)
325 // ie this sequence was deleted, we need to
326 // read it to the alignment
327 if (command.alIndex[i] < command.al.getHeight())
329 command.al.getSequences().insertElementAt(command.seqs[i],
334 command.al.addSequence(command.seqs[i]);
338 newstart = command.seqs[i].getStart();
339 newend = command.seqs[i].getEnd();
341 tmp = new StringBuffer();
342 tmp.append(command.seqs[i].getSequence());
343 //Undo of a delete does not replace original dataset sequence on to alignment sequence.
345 if (command.string != null && command.string[i] != null)
347 if (command.position >= tmp.length())
349 //This occurs if padding is on, and residues
350 //are removed from end of alignment
351 int length = command.position - tmp.length();
354 tmp.append(command.gapChar);
358 tmp.insert(command.position, command.string[i]);
359 for (int s = 0; s < command.string[i].length; s++)
361 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] !=
367 start = command.seqs[i].findPosition(command.position);
368 end = command.seqs[i].findPosition(command.position +
371 if (command.seqs[i].getStart()==start)
377 command.string[i] = null;
380 command.seqs[i].setSequence(tmp.toString());
381 command.seqs[i].setStart(newstart);
382 command.seqs[i].setEnd(newend);
385 if (command.seqs[i].getDatasetSequence() != null)
390 ds = command.oldds[i];
392 // make a new DS sequence
393 // use new ds mechanism here
394 ds= new Sequence(command.seqs[i].getName(),
395 jalview.analysis.AlignSeq.extractGaps(
396 jalview.util.Comparison.GapChars,
397 command.seqs[i].getSequenceAsString()
399 command.seqs[i].getStart(),
400 command.seqs[i].getEnd());
401 ds.setDescription(command.seqs[i].getDescription());
403 if (command.oldds==null)
405 command.oldds = new SequenceI[command.seqs.length];
407 command.oldds[i]=command.seqs[i].getDatasetSequence();
408 command.seqs[i].setDatasetSequence(ds);
410 adjustFeatures(command, i, start, end, true);
413 adjustAnnotations(command, true, seqWasDeleted, views);
415 command.string = null;
418 void replace(Edit command)
422 int start = command.position;
423 int end = command.number;
424 // TODO TUTORIAL - Fix for replacement with different length of sequence (or whole sequence)
425 // TODO Jalview 2.4 bugfix change to an aggregate command - original sequence string is cut, new string is pasted in.
426 command.number = start + command.string[0].length;
427 for (int i = 0; i < command.seqs.length; i++)
430 * addHistoryItem(new EditCommand("Cut Sequences",
434 sg.getEndRes()-sg.getStartRes()+1,
435 viewport.alignment));
439 * addHistoryItem(new EditCommand(
444 alignment.getWidth(),
449 oldstring = command.seqs[i].getSequenceAsString();
450 tmp = new StringBuffer(oldstring.substring(0, start));
451 tmp.append(command.string[i]);
452 tmp.append(oldstring.substring(end));
453 command.seqs[i].setSequence(tmp.toString());
454 command.string[i] = oldstring.substring(start, end).toCharArray();
460 final void adjustAnnotations(Edit command, boolean insert, boolean modifyVisibility, AlignmentI[] views)
462 AlignmentAnnotation[] annotations = null;
464 if (modifyVisibility && !insert)
466 // only occurs if a sequence was added or deleted.
467 command.deletedAnnotationRows = new Hashtable();
469 if (command.fullAlignmentHeight)
471 annotations = command.al.getAlignmentAnnotation();
476 AlignmentAnnotation[] tmp;
477 for (int s = 0; s < command.seqs.length; s++)
479 if (modifyVisibility)
481 // Rows are only removed or added to sequence object.
484 tmp = command.seqs[s].getAnnotation();
487 for (int aa =0; aa<tmp.length; aa++)
489 if (!command.al.deleteAnnotation(tmp[aa]))
491 // strip out annotation not in the current al (will be put back on insert in all views)
496 command.seqs[s].setAlignmentAnnotation(null);
497 if (alen!=tmp.length)
499 // save the non-null annotation references only
500 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
501 for (int aa=0,aapos=0;aa<tmp.length;aa++)
505 saved[aapos++] = tmp[aa];
510 command.deletedAnnotationRows.put(command.seqs[s], saved);
511 // and then remove any annotation in the other views
512 for (int alview=0; views!=null && alview<views.length; alview++)
514 if (views[alview]!=command.al)
516 AlignmentAnnotation[] toremove = views[alview].getAlignmentAnnotation();
517 if (toremove==null || toremove.length==0)
521 // remove any alignment annotation on this sequence that's on that alignment view.
522 for (int aa = 0; aa<toremove.length; aa++)
524 if (toremove[aa].sequenceRef==command.seqs[s])
526 views[alview].deleteAnnotation(toremove[aa]);
532 // save all the annotation
533 command.deletedAnnotationRows.put(command.seqs[s], tmp);
538 if (command.deletedAnnotationRows!=null && command.deletedAnnotationRows.containsKey(command.seqs[s]))
540 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows.get(command.seqs[s]);
541 command.seqs[s].setAlignmentAnnotation(revealed);
542 if (revealed!=null) {
543 for (int aa =0; aa<revealed.length; aa++)
545 // iterate through al adding original annotation
546 command.al.addAnnotation(revealed[aa]);
548 for (int aa =0; aa<revealed.length; aa++)
550 command.al.setAnnotationIndex(revealed[aa], aa);
552 // and then duplicate added annotation on every other alignment view
553 for (int vnum=0; views!=null && vnum<views.length; vnum++)
555 if (views[vnum]!=command.al)
557 int avwidth = views[vnum].getWidth()+1;
558 // duplicate in this view
559 for (int a=0; a<revealed.length; a++)
561 AlignmentAnnotation newann = new AlignmentAnnotation(revealed[a]);
562 command.seqs[s].addAlignmentAnnotation(newann);
563 newann.padAnnotation(avwidth);
564 views[vnum].addAnnotation(newann);
565 views[vnum].setAnnotationIndex(newann, a);
575 if (command.seqs[s].getAnnotation() == null)
582 annotations = command.seqs[s].getAnnotation();
586 tmp = new AlignmentAnnotation
587 [aSize + command.seqs[s].getAnnotation().length];
589 System.arraycopy(annotations, 0, tmp, 0, aSize);
591 System.arraycopy(command.seqs[s].getAnnotation(),
593 command.seqs[s].getAnnotation().length);
597 aSize = annotations.length;
601 if (annotations == null)
608 command.deletedAnnotations = new Hashtable();
613 for (int a = 0; a < annotations.length; a++)
615 if (annotations[a].autoCalculated || annotations[a].annotations == null)
622 aSize = annotations[a].annotations.length;
625 temp = new Annotation[aSize + command.number];
626 if(annotations[a].padGaps)
627 for (int aa = 0; aa < temp.length; aa++)
629 temp[aa] = new Annotation(
636 if (command.position < aSize)
638 if (command.position + command.number >= aSize)
644 tSize = aSize - command.number;
656 temp = new Annotation[tSize];
662 if (command.position < annotations[a].annotations.length)
664 System.arraycopy(annotations[a].annotations,
665 0, temp, 0, command.position);
667 if (command.deletedAnnotations != null
669 command.deletedAnnotations.containsKey(annotations[a].
672 Annotation[] restore = (Annotation[])
673 command.deletedAnnotations.get(annotations[a].annotationId);
675 System.arraycopy(restore,
683 System.arraycopy(annotations[a].annotations,
684 command.position, temp,
685 command.position + command.number,
686 aSize - command.position);
690 if (command.deletedAnnotations != null
692 command.deletedAnnotations.containsKey(annotations[a].
695 Annotation[] restore = (Annotation[])
696 command.deletedAnnotations.get(annotations[a].annotationId);
698 temp = new Annotation[annotations[a].annotations.length +
700 System.arraycopy(annotations[a].annotations,
702 annotations[a].annotations.length);
703 System.arraycopy(restore, 0, temp,
704 annotations[a].annotations.length, restore.length);
708 temp = annotations[a].annotations;
714 if (tSize != aSize || command.position < 2)
716 int copylen = Math.min(command.position, annotations[a].annotations.length);
718 System.arraycopy(annotations[a].annotations,
719 0, temp, 0, copylen); //command.position);
721 Annotation[] deleted = new Annotation[command.number];
722 if (copylen>=command.position) {
723 copylen = Math.min(command.number, annotations[a].annotations.length-command.position);
726 System.arraycopy(annotations[a].annotations,
727 command.position, deleted, 0, copylen); // command.number);
731 command.deletedAnnotations.put(annotations[a].annotationId,
733 if (annotations[a].annotations.length>command.position+command.number) {
734 System.arraycopy(annotations[a].annotations,
735 command.position + command.number,
736 temp, command.position,
737 annotations[a].annotations.length - command.position - command.number); // aSize
742 int dSize = aSize - command.position;
746 Annotation[] deleted = new Annotation[command.number];
747 System.arraycopy(annotations[a].annotations,
748 command.position, deleted, 0, dSize);
750 command.deletedAnnotations.put(annotations[a].annotationId,
753 tSize = Math.min(annotations[a].annotations.length,
755 temp = new Annotation[tSize];
756 System.arraycopy(annotations[a].annotations,
761 temp = annotations[a].annotations;
766 annotations[a].annotations = temp;
770 final void adjustFeatures(Edit command, int index, int i, int j,
773 SequenceI seq = command.seqs[index];
774 SequenceI sequence = seq.getDatasetSequence();
775 if (sequence == null)
782 if (command.editedFeatures != null
783 && command.editedFeatures.containsKey(seq))
785 sequence.setSequenceFeatures(
786 (SequenceFeature[]) command.editedFeatures.get(seq)
793 SequenceFeature[] sf = sequence.getSequenceFeatures();
800 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
804 for (int s = 0; s < sf.length; s++)
806 SequenceFeature copy = new SequenceFeature(sf[s]);
810 if (sf[s].getEnd() < i)
815 if (sf[s].getBegin() > j)
817 sf[s].setBegin(copy.getBegin() - cSize);
818 sf[s].setEnd(copy.getEnd() - cSize);
822 if (sf[s].getBegin() >= i)
827 if (sf[s].getEnd() < j)
832 sf[s].setEnd(sf[s].getEnd() - (cSize));
834 if (sf[s].getBegin() > sf[s].getEnd())
836 sequence.deleteFeature(sf[s]);
840 if (command.editedFeatures == null)
842 command.editedFeatures = new Hashtable();
845 command.editedFeatures.put(seq, oldsf);
851 public SequenceI[] oldds;
852 boolean fullAlignmentHeight = false;
853 Hashtable deletedAnnotationRows;
854 Hashtable deletedAnnotations;
855 Hashtable editedFeatures;
861 int position, number;
870 this.command = command;
872 this.position = position;
873 this.number = number;
874 this.gapChar = gapChar;
883 this.gapChar = al.getGapCharacter();
884 this.command = command;
886 this.position = position;
887 this.number = number;
890 alIndex = new int[seqs.length];
891 for (int i = 0; i < seqs.length; i++)
893 alIndex[i] = al.findIndex(seqs[i]);
896 fullAlignmentHeight = (al.getHeight() == seqs.length);
906 this.command = command;
908 this.position = position;
909 this.number = number;
911 this.gapChar = al.getGapCharacter();
912 string = new char[seqs.length][];
913 for (int i = 0; i < seqs.length; i++)
915 string[i] = replace.toCharArray();
918 fullAlignmentHeight = (al.getHeight() == seqs.length);