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)
235 for (int s = 0; s < command.seqs.length; s++)
237 command.seqs[s].insertCharAt(command.position,
242 adjustAnnotations(command, true, false, null);
245 final void deleteGap(Edit command)
247 for (int s = 0; s < command.seqs.length; s++)
249 command.seqs[s].deleteChars(command.position,
250 command.position + command.number);
253 adjustAnnotations(command, false, false, null);
256 void cut(Edit command, AlignmentI[] views)
258 boolean seqDeleted=false;
259 command.string = new char[command.seqs.length][];
261 for (int i = 0; i < command.seqs.length; i++)
263 if (command.seqs[i].getLength() > command.position)
265 command.string[i] = command.seqs[i].getSequence(command.position,
266 command.position + command.number);
268 if (command.seqs[i].getDatasetSequence() != null
269 || command.seqs[i].getSequenceFeatures() != null)
271 for (int s = command.position; s < command.position + command.number;
274 if (jalview.schemes.ResidueProperties
275 .aaIndex[command.seqs[i].getCharAt(s)] != 23)
277 adjustFeatures(command, i,
278 command.seqs[i].findPosition(command.position),
279 command.seqs[i].findPosition(command.position +
286 command.seqs[i].deleteChars(command.position,
287 command.position + command.number);
290 if (command.seqs[i].getLength() < 1)
292 command.al.deleteSequence(command.seqs[i]);
297 adjustAnnotations(command, false, seqDeleted, views);
300 void paste(Edit command, AlignmentI[] views)
304 boolean seqWasDeleted=false;
305 int start = 0, end = 0;
307 for (int i = 0; i < command.seqs.length; i++)
310 if (command.seqs[i].getLength() < 1)
312 // ie this sequence was deleted, we need to
313 // read it to the alignment
314 if (command.alIndex[i] < command.al.getHeight())
316 command.al.getSequences().insertElementAt(command.seqs[i],
321 command.al.addSequence(command.seqs[i]);
325 tmp = new StringBuffer();
326 tmp.append(command.seqs[i].getSequence());
328 if (command.string != null && command.string[i] != null)
330 if (command.position >= tmp.length())
332 //This occurs if padding is on, and residues
333 //are removed from end of alignment
334 int length = command.position - tmp.length();
337 tmp.append(command.gapChar);
341 tmp.insert(command.position, command.string[i]);
343 for (int s = 0; s < command.string[i].length; s++)
345 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] !=
349 start = command.seqs[i].findPosition(command.position);
350 end = command.seqs[i].findPosition(command.position +
355 command.string[i] = null;
358 command.seqs[i].setSequence(tmp.toString());
362 if (command.seqs[i].getDatasetSequence() != null)
363 { // use new ds mechanism here
364 Sequence ds = new Sequence(command.seqs[i].getName(),
365 jalview.analysis.AlignSeq.extractGaps(
366 jalview.util.Comparison.GapChars,
367 command.seqs[i].getSequenceAsString()
369 command.seqs[i].getStart(),
370 command.seqs[i].getEnd());
371 ds.setDescription(command.seqs[i].getDescription());
372 command.seqs[i].setDatasetSequence(ds);
375 adjustFeatures(command, i, start, end, true);
378 adjustAnnotations(command, true, seqWasDeleted, views);
380 command.string = null;
383 void replace(Edit command)
387 int start = command.position;
388 int end = command.number;
390 command.number = start + command.string[0].length;
391 for (int i = 0; i < command.seqs.length; i++)
393 oldstring = command.seqs[i].getSequenceAsString();
394 tmp = new StringBuffer(oldstring.substring(0, start));
395 tmp.append(command.string[i]);
396 tmp.append(oldstring.substring(end));
397 command.seqs[i].setSequence(tmp.toString());
398 command.string[i] = oldstring.substring(start, end).toCharArray();
404 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)
566 if (annotations[a].annotations == null)
568 // nothing to edit here ?
571 aSize = annotations[a].annotations.length;
574 temp = new Annotation[aSize + command.number];
575 if(annotations[a].padGaps)
576 for (int aa = 0; aa < temp.length; aa++)
578 temp[aa] = new Annotation(
585 if (command.position < aSize)
587 if (command.position + command.number > aSize)
593 tSize = aSize - command.number + command.position;
605 temp = new Annotation[tSize];
611 if (command.position < annotations[a].annotations.length)
613 System.arraycopy(annotations[a].annotations,
614 0, temp, 0, command.position);
616 if (command.deletedAnnotations != null
618 command.deletedAnnotations.containsKey(annotations[a].
621 Annotation[] restore = (Annotation[])
622 command.deletedAnnotations.get(annotations[a].annotationId);
624 System.arraycopy(restore,
632 System.arraycopy(annotations[a].annotations,
633 command.position, temp,
634 command.position + command.number,
635 aSize - command.position);
639 if (command.deletedAnnotations != null
641 command.deletedAnnotations.containsKey(annotations[a].
644 Annotation[] restore = (Annotation[])
645 command.deletedAnnotations.get(annotations[a].annotationId);
647 temp = new Annotation[annotations[a].annotations.length +
649 System.arraycopy(annotations[a].annotations,
651 annotations[a].annotations.length);
652 System.arraycopy(restore, 0, temp,
653 annotations[a].annotations.length, restore.length);
657 temp = annotations[a].annotations;
663 if (tSize != aSize || command.position < 2)
665 int copylen = Math.min(command.position, annotations[a].annotations.length);
667 System.arraycopy(annotations[a].annotations,
668 0, temp, 0, copylen); //command.position);
670 Annotation[] deleted = new Annotation[command.number];
671 if (copylen>=command.position) {
672 copylen = Math.min(command.number, annotations[a].annotations.length-command.position);
675 System.arraycopy(annotations[a].annotations,
676 command.position, deleted, 0, copylen); // command.number);
680 command.deletedAnnotations.put(annotations[a].annotationId,
682 if (annotations[a].annotations.length>command.position+command.number) {
683 System.arraycopy(annotations[a].annotations,
684 command.position + command.number,
685 temp, command.position,
686 annotations[a].annotations.length - command.position - command.number); // aSize
691 int dSize = aSize - command.position;
695 Annotation[] deleted = new Annotation[command.number];
696 System.arraycopy(annotations[a].annotations,
697 command.position, deleted, 0, dSize);
699 command.deletedAnnotations.put(annotations[a].annotationId,
702 tSize = Math.min(annotations[a].annotations.length,
704 temp = new Annotation[tSize];
705 System.arraycopy(annotations[a].annotations,
710 temp = annotations[a].annotations;
715 annotations[a].annotations = temp;
719 final void adjustFeatures(Edit command, int index, int i, int j,
722 SequenceI seq = command.seqs[index];
723 SequenceI sequence = seq.getDatasetSequence();
724 if (sequence == null)
731 if (command.editedFeatures != null
732 && command.editedFeatures.containsKey(seq))
734 sequence.setSequenceFeatures(
735 (SequenceFeature[]) command.editedFeatures.get(seq)
742 SequenceFeature[] sf = sequence.getSequenceFeatures();
749 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
753 for (int s = 0; s < sf.length; s++)
755 SequenceFeature copy = new SequenceFeature(sf[s]);
759 if (sf[s].getEnd() < i)
764 if (sf[s].getBegin() > j)
766 sf[s].setBegin(copy.getBegin() - cSize);
767 sf[s].setEnd(copy.getEnd() - cSize);
771 if (sf[s].getBegin() >= i)
776 if (sf[s].getEnd() < j)
781 sf[s].setEnd(sf[s].getEnd() - (cSize));
783 if (sf[s].getBegin() > sf[s].getEnd())
785 sequence.deleteFeature(sf[s]);
789 if (command.editedFeatures == null)
791 command.editedFeatures = new Hashtable();
794 command.editedFeatures.put(seq, oldsf);
800 boolean fullAlignmentHeight = false;
801 Hashtable deletedAnnotationRows;
802 Hashtable deletedAnnotations;
803 Hashtable editedFeatures;
809 int position, number;
818 this.command = command;
820 this.position = position;
821 this.number = number;
822 this.gapChar = gapChar;
831 this.gapChar = al.getGapCharacter();
832 this.command = command;
834 this.position = position;
835 this.number = number;
838 alIndex = new int[seqs.length];
839 for (int i = 0; i < seqs.length; i++)
841 alIndex[i] = al.findIndex(seqs[i]);
844 fullAlignmentHeight = (al.getHeight() == seqs.length);
854 this.command = command;
856 this.position = position;
857 this.number = number;
859 this.gapChar = al.getGapCharacter();
860 string = new char[seqs.length][];
861 for (int i = 0; i < seqs.length; i++)
863 string[i] = replace.toCharArray();
866 fullAlignmentHeight = (al.getHeight() == seqs.length);