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);
269 if (command.seqs[i].getDatasetSequence() != null
270 || command.seqs[i].getSequenceFeatures() != null)
272 for (int s = command.position; s < command.position + command.number;
275 if (jalview.schemes.ResidueProperties
276 .aaIndex[command.seqs[i].getCharAt(s)] != 23)
278 adjustFeatures(command, i,
279 command.seqs[i].findPosition(command.position),
280 command.seqs[i].findPosition(command.position +
287 command.seqs[i].deleteChars(command.position,
288 command.position + command.number);
291 if (command.seqs[i].getLength() < 1)
293 command.al.deleteSequence(command.seqs[i]);
298 adjustAnnotations(command, false, seqDeleted, views);
301 void paste(Edit command, AlignmentI[] views)
305 boolean seqWasDeleted=false;
306 int start = 0, end = 0;
308 for (int i = 0; i < command.seqs.length; i++)
311 if (command.seqs[i].getLength() < 1)
313 // ie this sequence was deleted, we need to
314 // read it to the alignment
315 if (command.alIndex[i] < command.al.getHeight())
317 command.al.getSequences().insertElementAt(command.seqs[i],
322 command.al.addSequence(command.seqs[i]);
326 tmp = new StringBuffer();
327 tmp.append(command.seqs[i].getSequence());
329 if (command.string != null && command.string[i] != null)
331 if (command.position >= tmp.length())
333 //This occurs if padding is on, and residues
334 //are removed from end of alignment
335 int length = command.position - tmp.length();
338 tmp.append(command.gapChar);
342 tmp.insert(command.position, command.string[i]);
344 for (int s = 0; s < command.string[i].length; s++)
346 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] !=
350 start = command.seqs[i].findPosition(command.position);
351 end = command.seqs[i].findPosition(command.position +
356 command.string[i] = null;
359 command.seqs[i].setSequence(tmp.toString());
363 if (command.seqs[i].getDatasetSequence() != null)
364 { // use new ds mechanism here
365 Sequence ds = new Sequence(command.seqs[i].getName(),
366 jalview.analysis.AlignSeq.extractGaps(
367 jalview.util.Comparison.GapChars,
368 command.seqs[i].getSequenceAsString()
370 command.seqs[i].getStart(),
371 command.seqs[i].getEnd());
372 ds.setDescription(command.seqs[i].getDescription());
373 command.seqs[i].setDatasetSequence(ds);
376 adjustFeatures(command, i, start, end, true);
379 adjustAnnotations(command, true, seqWasDeleted, views);
381 command.string = null;
384 void replace(Edit command)
388 int start = command.position;
389 int end = command.number;
391 command.number = start + command.string[0].length;
392 for (int i = 0; i < command.seqs.length; i++)
394 oldstring = command.seqs[i].getSequenceAsString();
395 tmp = new StringBuffer(oldstring.substring(0, start));
396 tmp.append(command.string[i]);
397 tmp.append(oldstring.substring(end));
398 command.seqs[i].setSequence(tmp.toString());
399 command.string[i] = oldstring.substring(start, end).toCharArray();
405 final void adjustAnnotations(Edit command, boolean insert, boolean modifyVisibility, AlignmentI[] views)
407 AlignmentAnnotation[] annotations = null;
409 if (modifyVisibility && !insert)
411 // only occurs if a sequence was added or deleted.
412 command.deletedAnnotationRows = new Hashtable();
414 if (command.fullAlignmentHeight)
416 annotations = command.al.getAlignmentAnnotation();
421 AlignmentAnnotation[] tmp;
422 for (int s = 0; s < command.seqs.length; s++)
424 if (modifyVisibility)
426 // Rows are only removed or added to sequence object.
429 tmp = command.seqs[s].getAnnotation();
432 for (int aa =0; aa<tmp.length; aa++)
434 if (!command.al.deleteAnnotation(tmp[aa]))
436 // strip out annotation not in the current al (will be put back on insert in all views)
441 command.seqs[s].setAlignmentAnnotation(null);
442 if (alen!=tmp.length)
444 // save the non-null annotation references only
445 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
446 for (int aa=0,aapos=0;aa<tmp.length;aa++)
450 saved[aapos++] = tmp[aa];
455 command.deletedAnnotationRows.put(command.seqs[s], saved);
456 // and then remove any annotation in the other views
457 for (int alview=0; views!=null && alview<views.length; alview++)
459 if (views[alview]!=command.al)
461 AlignmentAnnotation[] toremove = views[alview].getAlignmentAnnotation();
462 if (toremove==null || toremove.length==0)
466 // remove any alignment annotation on this sequence that's on that alignment view.
467 for (int aa = 0; aa<toremove.length; aa++)
469 if (toremove[aa].sequenceRef==command.seqs[s])
471 views[alview].deleteAnnotation(toremove[aa]);
477 // save all the annotation
478 command.deletedAnnotationRows.put(command.seqs[s], tmp);
483 if (command.deletedAnnotationRows!=null && command.deletedAnnotationRows.containsKey(command.seqs[s]))
485 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows.get(command.seqs[s]);
486 command.seqs[s].setAlignmentAnnotation(revealed);
487 if (revealed!=null) {
488 for (int aa =0; aa<revealed.length; aa++)
490 // iterate through al adding original annotation
491 command.al.addAnnotation(revealed[aa]);
493 for (int aa =0; aa<revealed.length; aa++)
495 command.al.setAnnotationIndex(revealed[aa], aa);
497 // and then duplicate added annotation on every other alignment view
498 for (int vnum=0; views!=null && vnum<views.length; vnum++)
500 if (views[vnum]!=command.al)
502 int avwidth = views[vnum].getWidth()+1;
503 // duplicate in this view
504 for (int a=0; a<revealed.length; a++)
506 AlignmentAnnotation newann = new AlignmentAnnotation(revealed[a]);
507 command.seqs[s].addAlignmentAnnotation(newann);
508 newann.padAnnotation(avwidth);
509 views[vnum].addAnnotation(newann);
510 views[vnum].setAnnotationIndex(newann, a);
520 if (command.seqs[s].getAnnotation() == null)
527 annotations = command.seqs[s].getAnnotation();
531 tmp = new AlignmentAnnotation
532 [aSize + command.seqs[s].getAnnotation().length];
534 System.arraycopy(annotations, 0, tmp, 0, aSize);
536 System.arraycopy(command.seqs[s].getAnnotation(),
538 command.seqs[s].getAnnotation().length);
542 aSize = annotations.length;
546 if (annotations == null)
553 command.deletedAnnotations = new Hashtable();
558 for (int a = 0; a < annotations.length; a++)
560 if (annotations[a].autoCalculated || annotations[a].annotations == null)
567 aSize = annotations[a].annotations.length;
570 temp = new Annotation[aSize + command.number];
571 if(annotations[a].padGaps)
572 for (int aa = 0; aa < temp.length; aa++)
574 temp[aa] = new Annotation(
581 if (command.position < aSize)
583 if (command.position + command.number >= aSize)
589 tSize = aSize - command.number;
601 temp = new Annotation[tSize];
607 if (command.position < annotations[a].annotations.length)
609 System.arraycopy(annotations[a].annotations,
610 0, temp, 0, command.position);
612 if (command.deletedAnnotations != null
614 command.deletedAnnotations.containsKey(annotations[a].
617 Annotation[] restore = (Annotation[])
618 command.deletedAnnotations.get(annotations[a].annotationId);
620 System.arraycopy(restore,
628 System.arraycopy(annotations[a].annotations,
629 command.position, temp,
630 command.position + command.number,
631 aSize - command.position);
635 if (command.deletedAnnotations != null
637 command.deletedAnnotations.containsKey(annotations[a].
640 Annotation[] restore = (Annotation[])
641 command.deletedAnnotations.get(annotations[a].annotationId);
643 temp = new Annotation[annotations[a].annotations.length +
645 System.arraycopy(annotations[a].annotations,
647 annotations[a].annotations.length);
648 System.arraycopy(restore, 0, temp,
649 annotations[a].annotations.length, restore.length);
653 temp = annotations[a].annotations;
659 if (tSize != aSize || command.position < 2)
661 int copylen = Math.min(command.position, annotations[a].annotations.length);
663 System.arraycopy(annotations[a].annotations,
664 0, temp, 0, copylen); //command.position);
666 Annotation[] deleted = new Annotation[command.number];
667 if (copylen>=command.position) {
668 copylen = Math.min(command.number, annotations[a].annotations.length-command.position);
671 System.arraycopy(annotations[a].annotations,
672 command.position, deleted, 0, copylen); // command.number);
676 command.deletedAnnotations.put(annotations[a].annotationId,
678 if (annotations[a].annotations.length>command.position+command.number) {
679 System.arraycopy(annotations[a].annotations,
680 command.position + command.number,
681 temp, command.position,
682 annotations[a].annotations.length - command.position - command.number); // aSize
687 int dSize = aSize - command.position;
691 Annotation[] deleted = new Annotation[command.number];
692 System.arraycopy(annotations[a].annotations,
693 command.position, deleted, 0, dSize);
695 command.deletedAnnotations.put(annotations[a].annotationId,
698 tSize = Math.min(annotations[a].annotations.length,
700 temp = new Annotation[tSize];
701 System.arraycopy(annotations[a].annotations,
706 temp = annotations[a].annotations;
711 annotations[a].annotations = temp;
715 final void adjustFeatures(Edit command, int index, int i, int j,
718 SequenceI seq = command.seqs[index];
719 SequenceI sequence = seq.getDatasetSequence();
720 if (sequence == null)
727 if (command.editedFeatures != null
728 && command.editedFeatures.containsKey(seq))
730 sequence.setSequenceFeatures(
731 (SequenceFeature[]) command.editedFeatures.get(seq)
738 SequenceFeature[] sf = sequence.getSequenceFeatures();
745 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
749 for (int s = 0; s < sf.length; s++)
751 SequenceFeature copy = new SequenceFeature(sf[s]);
755 if (sf[s].getEnd() < i)
760 if (sf[s].getBegin() > j)
762 sf[s].setBegin(copy.getBegin() - cSize);
763 sf[s].setEnd(copy.getEnd() - cSize);
767 if (sf[s].getBegin() >= i)
772 if (sf[s].getEnd() < j)
777 sf[s].setEnd(sf[s].getEnd() - (cSize));
779 if (sf[s].getBegin() > sf[s].getEnd())
781 sequence.deleteFeature(sf[s]);
785 if (command.editedFeatures == null)
787 command.editedFeatures = new Hashtable();
790 command.editedFeatures.put(seq, oldsf);
796 boolean fullAlignmentHeight = false;
797 Hashtable deletedAnnotationRows;
798 Hashtable deletedAnnotations;
799 Hashtable editedFeatures;
805 int position, number;
814 this.command = command;
816 this.position = position;
817 this.number = number;
818 this.gapChar = gapChar;
827 this.gapChar = al.getGapCharacter();
828 this.command = command;
830 this.position = position;
831 this.number = number;
834 alIndex = new int[seqs.length];
835 for (int i = 0; i < seqs.length; i++)
837 alIndex[i] = al.findIndex(seqs[i]);
840 fullAlignmentHeight = (al.getHeight() == seqs.length);
850 this.command = command;
852 this.position = position;
853 this.number = number;
855 this.gapChar = al.getGapCharacter();
856 string = new char[seqs.length][];
857 for (int i = 0; i < seqs.length; i++)
859 string[i] = replace.toCharArray();
862 fullAlignmentHeight = (al.getHeight() == seqs.length);