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;
425 command.number = start + command.string[0].length;
426 for (int i = 0; i < command.seqs.length; i++)
428 oldstring = command.seqs[i].getSequenceAsString();
429 tmp = new StringBuffer(oldstring.substring(0, start));
430 tmp.append(command.string[i]);
431 tmp.append(oldstring.substring(end));
432 command.seqs[i].setSequence(tmp.toString());
433 command.string[i] = oldstring.substring(start, end).toCharArray();
439 final void adjustAnnotations(Edit command, boolean insert, boolean modifyVisibility, AlignmentI[] views)
441 AlignmentAnnotation[] annotations = null;
443 if (modifyVisibility && !insert)
445 // only occurs if a sequence was added or deleted.
446 command.deletedAnnotationRows = new Hashtable();
448 if (command.fullAlignmentHeight)
450 annotations = command.al.getAlignmentAnnotation();
455 AlignmentAnnotation[] tmp;
456 for (int s = 0; s < command.seqs.length; s++)
458 if (modifyVisibility)
460 // Rows are only removed or added to sequence object.
463 tmp = command.seqs[s].getAnnotation();
466 for (int aa =0; aa<tmp.length; aa++)
468 if (!command.al.deleteAnnotation(tmp[aa]))
470 // strip out annotation not in the current al (will be put back on insert in all views)
475 command.seqs[s].setAlignmentAnnotation(null);
476 if (alen!=tmp.length)
478 // save the non-null annotation references only
479 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
480 for (int aa=0,aapos=0;aa<tmp.length;aa++)
484 saved[aapos++] = tmp[aa];
489 command.deletedAnnotationRows.put(command.seqs[s], saved);
490 // and then remove any annotation in the other views
491 for (int alview=0; views!=null && alview<views.length; alview++)
493 if (views[alview]!=command.al)
495 AlignmentAnnotation[] toremove = views[alview].getAlignmentAnnotation();
496 if (toremove==null || toremove.length==0)
500 // remove any alignment annotation on this sequence that's on that alignment view.
501 for (int aa = 0; aa<toremove.length; aa++)
503 if (toremove[aa].sequenceRef==command.seqs[s])
505 views[alview].deleteAnnotation(toremove[aa]);
511 // save all the annotation
512 command.deletedAnnotationRows.put(command.seqs[s], tmp);
517 if (command.deletedAnnotationRows!=null && command.deletedAnnotationRows.containsKey(command.seqs[s]))
519 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows.get(command.seqs[s]);
520 command.seqs[s].setAlignmentAnnotation(revealed);
521 if (revealed!=null) {
522 for (int aa =0; aa<revealed.length; aa++)
524 // iterate through al adding original annotation
525 command.al.addAnnotation(revealed[aa]);
527 for (int aa =0; aa<revealed.length; aa++)
529 command.al.setAnnotationIndex(revealed[aa], aa);
531 // and then duplicate added annotation on every other alignment view
532 for (int vnum=0; views!=null && vnum<views.length; vnum++)
534 if (views[vnum]!=command.al)
536 int avwidth = views[vnum].getWidth()+1;
537 // duplicate in this view
538 for (int a=0; a<revealed.length; a++)
540 AlignmentAnnotation newann = new AlignmentAnnotation(revealed[a]);
541 command.seqs[s].addAlignmentAnnotation(newann);
542 newann.padAnnotation(avwidth);
543 views[vnum].addAnnotation(newann);
544 views[vnum].setAnnotationIndex(newann, a);
554 if (command.seqs[s].getAnnotation() == null)
561 annotations = command.seqs[s].getAnnotation();
565 tmp = new AlignmentAnnotation
566 [aSize + command.seqs[s].getAnnotation().length];
568 System.arraycopy(annotations, 0, tmp, 0, aSize);
570 System.arraycopy(command.seqs[s].getAnnotation(),
572 command.seqs[s].getAnnotation().length);
576 aSize = annotations.length;
580 if (annotations == null)
587 command.deletedAnnotations = new Hashtable();
592 for (int a = 0; a < annotations.length; a++)
594 if (annotations[a].autoCalculated || annotations[a].annotations == null)
601 aSize = annotations[a].annotations.length;
604 temp = new Annotation[aSize + command.number];
605 if(annotations[a].padGaps)
606 for (int aa = 0; aa < temp.length; aa++)
608 temp[aa] = new Annotation(
615 if (command.position < aSize)
617 if (command.position + command.number >= aSize)
623 tSize = aSize - command.number;
635 temp = new Annotation[tSize];
641 if (command.position < annotations[a].annotations.length)
643 System.arraycopy(annotations[a].annotations,
644 0, temp, 0, command.position);
646 if (command.deletedAnnotations != null
648 command.deletedAnnotations.containsKey(annotations[a].
651 Annotation[] restore = (Annotation[])
652 command.deletedAnnotations.get(annotations[a].annotationId);
654 System.arraycopy(restore,
662 System.arraycopy(annotations[a].annotations,
663 command.position, temp,
664 command.position + command.number,
665 aSize - command.position);
669 if (command.deletedAnnotations != null
671 command.deletedAnnotations.containsKey(annotations[a].
674 Annotation[] restore = (Annotation[])
675 command.deletedAnnotations.get(annotations[a].annotationId);
677 temp = new Annotation[annotations[a].annotations.length +
679 System.arraycopy(annotations[a].annotations,
681 annotations[a].annotations.length);
682 System.arraycopy(restore, 0, temp,
683 annotations[a].annotations.length, restore.length);
687 temp = annotations[a].annotations;
693 if (tSize != aSize || command.position < 2)
695 int copylen = Math.min(command.position, annotations[a].annotations.length);
697 System.arraycopy(annotations[a].annotations,
698 0, temp, 0, copylen); //command.position);
700 Annotation[] deleted = new Annotation[command.number];
701 if (copylen>=command.position) {
702 copylen = Math.min(command.number, annotations[a].annotations.length-command.position);
705 System.arraycopy(annotations[a].annotations,
706 command.position, deleted, 0, copylen); // command.number);
710 command.deletedAnnotations.put(annotations[a].annotationId,
712 if (annotations[a].annotations.length>command.position+command.number) {
713 System.arraycopy(annotations[a].annotations,
714 command.position + command.number,
715 temp, command.position,
716 annotations[a].annotations.length - command.position - command.number); // aSize
721 int dSize = aSize - command.position;
725 Annotation[] deleted = new Annotation[command.number];
726 System.arraycopy(annotations[a].annotations,
727 command.position, deleted, 0, dSize);
729 command.deletedAnnotations.put(annotations[a].annotationId,
732 tSize = Math.min(annotations[a].annotations.length,
734 temp = new Annotation[tSize];
735 System.arraycopy(annotations[a].annotations,
740 temp = annotations[a].annotations;
745 annotations[a].annotations = temp;
749 final void adjustFeatures(Edit command, int index, int i, int j,
752 SequenceI seq = command.seqs[index];
753 SequenceI sequence = seq.getDatasetSequence();
754 if (sequence == null)
761 if (command.editedFeatures != null
762 && command.editedFeatures.containsKey(seq))
764 sequence.setSequenceFeatures(
765 (SequenceFeature[]) command.editedFeatures.get(seq)
772 SequenceFeature[] sf = sequence.getSequenceFeatures();
779 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
783 for (int s = 0; s < sf.length; s++)
785 SequenceFeature copy = new SequenceFeature(sf[s]);
789 if (sf[s].getEnd() < i)
794 if (sf[s].getBegin() > j)
796 sf[s].setBegin(copy.getBegin() - cSize);
797 sf[s].setEnd(copy.getEnd() - cSize);
801 if (sf[s].getBegin() >= i)
806 if (sf[s].getEnd() < j)
811 sf[s].setEnd(sf[s].getEnd() - (cSize));
813 if (sf[s].getBegin() > sf[s].getEnd())
815 sequence.deleteFeature(sf[s]);
819 if (command.editedFeatures == null)
821 command.editedFeatures = new Hashtable();
824 command.editedFeatures.put(seq, oldsf);
830 public SequenceI[] oldds;
831 boolean fullAlignmentHeight = false;
832 Hashtable deletedAnnotationRows;
833 Hashtable deletedAnnotations;
834 Hashtable editedFeatures;
840 int position, number;
849 this.command = command;
851 this.position = position;
852 this.number = number;
853 this.gapChar = gapChar;
862 this.gapChar = al.getGapCharacter();
863 this.command = command;
865 this.position = position;
866 this.number = number;
869 alIndex = new int[seqs.length];
870 for (int i = 0; i < seqs.length; i++)
872 alIndex[i] = al.findIndex(seqs[i]);
875 fullAlignmentHeight = (al.getHeight() == seqs.length);
885 this.command = command;
887 this.position = position;
888 this.number = number;
890 this.gapChar = al.getGapCharacter();
891 string = new char[seqs.length][];
892 for (int i = 0; i < seqs.length; i++)
894 string[i] = replace.toCharArray();
897 fullAlignmentHeight = (al.getHeight() == seqs.length);