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()
112 final public void appendEdit(int command,
119 Edit edit = new Edit(command, seqs, position, number, al.getGapCharacter());
120 if (al.getHeight() == seqs.length)
123 edit.fullAlignmentHeight = true;
128 Edit[] temp = new Edit[edits.length + 1];
129 System.arraycopy(edits, 0, temp, 0, edits.length);
131 edits[edits.length - 1] = edit;
142 performEdit(edits.length - 1);
146 final void performEdit(int commandIndex)
148 int eSize = edits.length;
149 for (int e = commandIndex; e < eSize; e++)
151 switch(edits[e].command)
172 final public void doCommand()
177 final public void undoCommand()
179 int e = 0, eSize = edits.length;
180 for (e = eSize - 1; e > -1; e--)
182 switch (edits[e].command)
203 final void insertGap(Edit command)
205 for (int s = 0; s < command.seqs.length; s++)
207 command.seqs[s].insertCharAt(command.position,
212 adjustAnnotations(command, true, false);
215 final void deleteGap(Edit command)
217 for (int s = 0; s < command.seqs.length; s++)
219 command.seqs[s].deleteChars(command.position,
220 command.position + command.number);
223 adjustAnnotations(command, false, false);
226 void cut(Edit command)
228 boolean seqDeleted=false;
229 command.string = new char[command.seqs.length][];
231 for (int i = 0; i < command.seqs.length; i++)
233 if (command.seqs[i].getLength() > command.position)
235 command.string[i] = command.seqs[i].getSequence(command.position,
236 command.position + command.number);
238 if (command.seqs[i].getDatasetSequence() != null
239 || command.seqs[i].getSequenceFeatures() != null)
241 for (int s = command.position; s < command.position + command.number;
244 if (jalview.schemes.ResidueProperties
245 .aaIndex[command.seqs[i].getCharAt(s)] != 23)
247 adjustFeatures(command, i,
248 command.seqs[i].findPosition(command.position),
249 command.seqs[i].findPosition(command.position +
256 command.seqs[i].deleteChars(command.position,
257 command.position + command.number);
260 if (command.seqs[i].getLength() < 1)
262 command.al.deleteSequence(command.seqs[i]);
267 adjustAnnotations(command, false, seqDeleted);
270 void paste(Edit command)
274 boolean seqWasDeleted=false;
275 int start = 0, end = 0;
277 for (int i = 0; i < command.seqs.length; i++)
280 if (command.seqs[i].getLength() < 1)
282 // ie this sequence was deleted, we need to
283 // read it to the alignment
284 if (command.alIndex[i] < command.al.getHeight())
286 command.al.getSequences().insertElementAt(command.seqs[i],
291 command.al.addSequence(command.seqs[i]);
295 tmp = new StringBuffer();
296 tmp.append(command.seqs[i].getSequence());
298 if (command.string != null && command.string[i] != null)
300 if (command.position >= tmp.length())
302 //This occurs if padding is on, and residues
303 //are removed from end of alignment
304 int length = command.position - tmp.length();
307 tmp.append(command.gapChar);
311 tmp.insert(command.position, command.string[i]);
313 for (int s = 0; s < command.string[i].length; s++)
315 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] !=
319 start = command.seqs[i].findPosition(command.position);
320 end = command.seqs[i].findPosition(command.position +
325 command.string[i] = null;
328 command.seqs[i].setSequence(tmp.toString());
332 if (command.seqs[i].getDatasetSequence() != null)
333 { // use new ds mechanism here
334 Sequence ds = new Sequence(command.seqs[i].getName(),
335 jalview.analysis.AlignSeq.extractGaps(
336 jalview.util.Comparison.GapChars,
337 command.seqs[i].getSequenceAsString()
339 command.seqs[i].getStart(),
340 command.seqs[i].getEnd());
341 ds.setDescription(command.seqs[i].getDescription());
342 command.seqs[i].setDatasetSequence(ds);
345 adjustFeatures(command, i, start, end, true);
348 adjustAnnotations(command, true, seqWasDeleted);
350 command.string = null;
353 void replace(Edit command)
357 int start = command.position;
358 int end = command.number;
360 command.number = start + command.string[0].length;
361 for (int i = 0; i < command.seqs.length; i++)
363 oldstring = command.seqs[i].getSequenceAsString();
364 tmp = new StringBuffer(oldstring.substring(0, start));
365 tmp.append(command.string[i]);
366 tmp.append(oldstring.substring(end));
367 command.seqs[i].setSequence(tmp.toString());
368 command.string[i] = oldstring.substring(start, end).toCharArray();
374 final void adjustAnnotations(Edit command, boolean insert, boolean modifyVisibility)
376 AlignmentAnnotation[] annotations = null;
378 if (modifyVisibility && !insert)
380 // only occurs if a sequence was added or deleted.
381 command.deletedAnnotationRows = new Hashtable();
383 if (command.fullAlignmentHeight)
385 annotations = command.al.getAlignmentAnnotation();
390 AlignmentAnnotation[] tmp;
391 for (int s = 0; s < command.seqs.length; s++)
393 if (modifyVisibility)
395 // Rows are only removed or added to sequence object.
398 tmp = command.seqs[s].getAnnotation();
400 command.deletedAnnotationRows.put(command.seqs[s], tmp);
401 for (int aa =0; aa<tmp.length; aa++)
403 command.al.deleteAnnotation(tmp[aa]);
405 command.seqs[s].setAlignmentAnnotation(null);
409 if (command.deletedAnnotationRows!=null && command.deletedAnnotationRows.containsKey(command.seqs[s]))
411 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows.get(command.seqs[s]);
412 command.seqs[s].setAlignmentAnnotation(revealed);
413 if (revealed!=null) {
414 for (int aa =0; aa<revealed.length; aa++)
416 command.al.addAnnotation(revealed[aa]);
418 for (int aa =0; aa<revealed.length; aa++)
420 command.al.setAnnotationIndex(revealed[aa], aa);
428 if (command.seqs[s].getAnnotation() == null)
435 annotations = command.seqs[s].getAnnotation();
439 tmp = new AlignmentAnnotation
440 [aSize + command.seqs[s].getAnnotation().length];
442 System.arraycopy(annotations, 0, tmp, 0, aSize);
444 System.arraycopy(command.seqs[s].getAnnotation(),
446 command.seqs[s].getAnnotation().length);
450 aSize = annotations.length;
454 if (annotations == null)
461 command.deletedAnnotations = new Hashtable();
466 for (int a = 0; a < annotations.length; a++)
468 if (annotations[a].autoCalculated)
475 aSize = annotations[a].annotations.length;
478 temp = new Annotation[aSize + command.number];
479 if(annotations[a].padGaps)
480 for (int aa = 0; aa < temp.length; aa++)
482 temp[aa] = new Annotation(
483 command.al.getGapCharacter()+"",
489 if (command.position < aSize)
491 if (command.position + command.number > aSize)
497 tSize = aSize - command.number + command.position;
509 temp = new Annotation[tSize];
515 if (command.position < annotations[a].annotations.length)
517 System.arraycopy(annotations[a].annotations,
518 0, temp, 0, command.position);
520 if (command.deletedAnnotations != null
522 command.deletedAnnotations.containsKey(annotations[a].
525 Annotation[] restore = (Annotation[])
526 command.deletedAnnotations.get(annotations[a].annotationId);
528 System.arraycopy(restore,
536 System.arraycopy(annotations[a].annotations,
537 command.position, temp,
538 command.position + command.number,
539 aSize - command.position);
543 if (command.deletedAnnotations != null
545 command.deletedAnnotations.containsKey(annotations[a].
548 Annotation[] restore = (Annotation[])
549 command.deletedAnnotations.get(annotations[a].annotationId);
551 temp = new Annotation[annotations[a].annotations.length +
553 System.arraycopy(annotations[a].annotations,
555 annotations[a].annotations.length);
556 System.arraycopy(restore, 0, temp,
557 annotations[a].annotations.length, restore.length);
561 temp = annotations[a].annotations;
567 if (tSize != aSize || command.position < 2)
569 int copylen = Math.min(command.position, annotations[a].annotations.length);
571 System.arraycopy(annotations[a].annotations,
572 0, temp, 0, copylen); //command.position);
574 Annotation[] deleted = new Annotation[command.number];
575 if (copylen>command.position) {
576 copylen = Math.min(command.number, annotations[a].annotations.length-command.position);
579 System.arraycopy(annotations[a].annotations,
580 command.position, deleted, 0, copylen); // command.number);
584 command.deletedAnnotations.put(annotations[a].annotationId,
586 if (annotations[a].annotations.length>command.position+command.number) {
587 System.arraycopy(annotations[a].annotations,
588 command.position + command.number,
589 temp, command.position,
590 annotations[a].annotations.length - command.position - command.number); // aSize
595 int dSize = aSize - command.position;
599 Annotation[] deleted = new Annotation[command.number];
600 System.arraycopy(annotations[a].annotations,
601 command.position, deleted, 0, dSize);
603 command.deletedAnnotations.put(annotations[a].annotationId,
606 tSize = Math.min(annotations[a].annotations.length,
608 temp = new Annotation[tSize];
609 System.arraycopy(annotations[a].annotations,
614 temp = annotations[a].annotations;
619 annotations[a].annotations = temp;
623 final void adjustFeatures(Edit command, int index, int i, int j,
626 SequenceI seq = command.seqs[index];
627 SequenceI sequence = seq.getDatasetSequence();
628 if (sequence == null)
635 if (command.editedFeatures != null
636 && command.editedFeatures.containsKey(seq))
638 sequence.setSequenceFeatures(
639 (SequenceFeature[]) command.editedFeatures.get(seq)
646 SequenceFeature[] sf = sequence.getSequenceFeatures();
653 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
657 for (int s = 0; s < sf.length; s++)
659 SequenceFeature copy = new SequenceFeature(sf[s]);
663 if (sf[s].getEnd() < i)
668 if (sf[s].getBegin() > j)
670 sf[s].setBegin(copy.getBegin() - cSize);
671 sf[s].setEnd(copy.getEnd() - cSize);
675 if (sf[s].getBegin() >= i)
680 if (sf[s].getEnd() < j)
685 sf[s].setEnd(sf[s].getEnd() - (cSize));
687 if (sf[s].getBegin() > sf[s].getEnd())
689 sequence.deleteFeature(sf[s]);
693 if (command.editedFeatures == null)
695 command.editedFeatures = new Hashtable();
698 command.editedFeatures.put(seq, oldsf);
704 boolean fullAlignmentHeight = false;
705 Hashtable deletedAnnotationRows;
706 Hashtable deletedAnnotations;
707 Hashtable editedFeatures;
713 int position, number;
722 this.command = command;
724 this.position = position;
725 this.number = number;
726 this.gapChar = gapChar;
735 this.gapChar = al.getGapCharacter();
736 this.command = command;
738 this.position = position;
739 this.number = number;
742 alIndex = new int[seqs.length];
743 for (int i = 0; i < seqs.length; i++)
745 alIndex[i] = al.findIndex(seqs[i]);
748 fullAlignmentHeight = (al.getHeight() == seqs.length);
758 this.command = command;
760 this.position = position;
761 this.number = number;
763 string = new char[seqs.length][];
764 for (int i = 0; i < seqs.length; i++)
766 string[i] = replace.toCharArray();
769 fullAlignmentHeight = (al.getHeight() == seqs.length);