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)
377 AlignmentAnnotation[] annotations = null;
379 if (modifyVisibility && !insert)
381 // only occurs if a sequence was added or deleted.
382 command.deletedAnnotationRows = new Hashtable();
384 if (command.fullAlignmentHeight)
386 annotations = command.al.getAlignmentAnnotation();
391 AlignmentAnnotation[] tmp;
392 for (int s = 0; s < command.seqs.length; s++)
394 if (modifyVisibility)
396 // Rows are only removed or added to sequence object.
399 tmp = command.seqs[s].getAnnotation();
401 command.deletedAnnotationRows.put(command.seqs[s], tmp);
402 for (int aa =0; aa<tmp.length; aa++)
404 command.al.deleteAnnotation(tmp[aa]);
406 command.seqs[s].setAlignmentAnnotation(null);
410 if (command.deletedAnnotationRows!=null && command.deletedAnnotationRows.containsKey(command.seqs[s]))
412 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows.get(command.seqs[s]);
413 command.seqs[s].setAlignmentAnnotation(revealed);
414 if (revealed!=null) {
415 for (int aa =0; aa<revealed.length; aa++)
417 command.al.addAnnotation(revealed[aa]);
419 for (int aa =0; aa<revealed.length; aa++)
421 command.al.setAnnotationIndex(revealed[aa], aa);
429 if (command.seqs[s].getAnnotation() == null)
436 annotations = command.seqs[s].getAnnotation();
440 tmp = new AlignmentAnnotation
441 [aSize + command.seqs[s].getAnnotation().length];
443 System.arraycopy(annotations, 0, tmp, 0, aSize);
445 System.arraycopy(command.seqs[s].getAnnotation(),
447 command.seqs[s].getAnnotation().length);
451 aSize = annotations.length;
455 if (annotations == null)
462 command.deletedAnnotations = new Hashtable();
467 for (int a = 0; a < annotations.length; a++)
469 if (annotations[a].autoCalculated)
475 if (annotations[a].annotations == null)
477 // nothing to edit here ?
480 aSize = annotations[a].annotations.length;
483 temp = new Annotation[aSize + command.number];
484 if(annotations[a].padGaps)
485 for (int aa = 0; aa < temp.length; aa++)
487 temp[aa] = new Annotation(
494 if (command.position < aSize)
496 if (command.position + command.number > aSize)
502 tSize = aSize - command.number + command.position;
514 temp = new Annotation[tSize];
520 if (command.position < annotations[a].annotations.length)
522 System.arraycopy(annotations[a].annotations,
523 0, temp, 0, command.position);
525 if (command.deletedAnnotations != null
527 command.deletedAnnotations.containsKey(annotations[a].
530 Annotation[] restore = (Annotation[])
531 command.deletedAnnotations.get(annotations[a].annotationId);
533 System.arraycopy(restore,
541 System.arraycopy(annotations[a].annotations,
542 command.position, temp,
543 command.position + command.number,
544 aSize - command.position);
548 if (command.deletedAnnotations != null
550 command.deletedAnnotations.containsKey(annotations[a].
553 Annotation[] restore = (Annotation[])
554 command.deletedAnnotations.get(annotations[a].annotationId);
556 temp = new Annotation[annotations[a].annotations.length +
558 System.arraycopy(annotations[a].annotations,
560 annotations[a].annotations.length);
561 System.arraycopy(restore, 0, temp,
562 annotations[a].annotations.length, restore.length);
566 temp = annotations[a].annotations;
572 if (tSize != aSize || command.position < 2)
574 int copylen = Math.min(command.position, annotations[a].annotations.length);
576 System.arraycopy(annotations[a].annotations,
577 0, temp, 0, copylen); //command.position);
579 Annotation[] deleted = new Annotation[command.number];
580 if (copylen>command.position) {
581 copylen = Math.min(command.number, annotations[a].annotations.length-command.position);
584 System.arraycopy(annotations[a].annotations,
585 command.position, deleted, 0, copylen); // command.number);
589 command.deletedAnnotations.put(annotations[a].annotationId,
591 if (annotations[a].annotations.length>command.position+command.number) {
592 System.arraycopy(annotations[a].annotations,
593 command.position + command.number,
594 temp, command.position,
595 annotations[a].annotations.length - command.position - command.number); // aSize
600 int dSize = aSize - command.position;
604 Annotation[] deleted = new Annotation[command.number];
605 System.arraycopy(annotations[a].annotations,
606 command.position, deleted, 0, dSize);
608 command.deletedAnnotations.put(annotations[a].annotationId,
611 tSize = Math.min(annotations[a].annotations.length,
613 temp = new Annotation[tSize];
614 System.arraycopy(annotations[a].annotations,
619 temp = annotations[a].annotations;
624 annotations[a].annotations = temp;
628 final void adjustFeatures(Edit command, int index, int i, int j,
631 SequenceI seq = command.seqs[index];
632 SequenceI sequence = seq.getDatasetSequence();
633 if (sequence == null)
640 if (command.editedFeatures != null
641 && command.editedFeatures.containsKey(seq))
643 sequence.setSequenceFeatures(
644 (SequenceFeature[]) command.editedFeatures.get(seq)
651 SequenceFeature[] sf = sequence.getSequenceFeatures();
658 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
662 for (int s = 0; s < sf.length; s++)
664 SequenceFeature copy = new SequenceFeature(sf[s]);
668 if (sf[s].getEnd() < i)
673 if (sf[s].getBegin() > j)
675 sf[s].setBegin(copy.getBegin() - cSize);
676 sf[s].setEnd(copy.getEnd() - cSize);
680 if (sf[s].getBegin() >= i)
685 if (sf[s].getEnd() < j)
690 sf[s].setEnd(sf[s].getEnd() - (cSize));
692 if (sf[s].getBegin() > sf[s].getEnd())
694 sequence.deleteFeature(sf[s]);
698 if (command.editedFeatures == null)
700 command.editedFeatures = new Hashtable();
703 command.editedFeatures.put(seq, oldsf);
709 boolean fullAlignmentHeight = false;
710 Hashtable deletedAnnotationRows;
711 Hashtable deletedAnnotations;
712 Hashtable editedFeatures;
718 int position, number;
727 this.command = command;
729 this.position = position;
730 this.number = number;
731 this.gapChar = gapChar;
740 this.gapChar = al.getGapCharacter();
741 this.command = command;
743 this.position = position;
744 this.number = number;
747 alIndex = new int[seqs.length];
748 for (int i = 0; i < seqs.length; i++)
750 alIndex[i] = al.findIndex(seqs[i]);
753 fullAlignmentHeight = (al.getHeight() == seqs.length);
763 this.command = command;
765 this.position = position;
766 this.number = number;
768 this.gapChar = al.getGapCharacter();
769 string = new char[seqs.length][];
770 for (int i = 0; i < seqs.length; i++)
772 string[i] = replace.toCharArray();
775 fullAlignmentHeight = (al.getHeight() == seqs.length);