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;
55 public EditCommand(String description)
57 this.description = description;
60 public EditCommand(String description,
67 this.description = description;
68 if (command == CUT || command == PASTE)
72 new Edit(command, seqs, position, number, al)};
78 final public String getDescription()
85 return edits == null ? 0 : edits.length;
88 final public AlignmentI getAlignment()
93 final public void appendEdit(int command,
100 Edit edit = new Edit(command, seqs, position, number, al.getGapCharacter());
101 if (al.getHeight() == seqs.length)
104 edit.fullAlignmentHeight = true;
109 Edit[] temp = new Edit[edits.length + 1];
110 System.arraycopy(edits, 0, temp, 0, edits.length);
112 edits[edits.length - 1] = edit;
123 performEdit(edits.length - 1);
127 final void performEdit(int commandIndex)
129 int eSize = edits.length;
130 for (int e = commandIndex; e < eSize; e++)
132 if (edits[e].command == INSERT_GAP)
136 else if (edits[e].command == DELETE_GAP)
140 else if (edits[e].command == CUT)
144 else if (edits[e].command == PASTE)
151 final public void doCommand()
156 final public void undoCommand()
158 int e = 0, eSize = edits.length;
159 for (e = eSize - 1; e > -1; e--)
161 if (edits[e].command == INSERT_GAP)
165 else if (edits[e].command == DELETE_GAP)
169 else if (edits[e].command == CUT)
173 else if (edits[e].command == PASTE)
180 final void insertGap(Edit command)
182 for (int s = 0; s < command.seqs.length; s++)
184 command.seqs[s].insertCharAt(command.position,
189 adjustAnnotations(command, true, false);
192 final void deleteGap(Edit command)
194 for (int s = 0; s < command.seqs.length; s++)
196 command.seqs[s].deleteChars(command.position,
197 command.position + command.number);
200 adjustAnnotations(command, false, false);
203 void cut(Edit command)
205 boolean seqDeleted=false;
206 command.string = new char[command.seqs.length][];
208 for (int i = 0; i < command.seqs.length; i++)
210 if (command.seqs[i].getLength() > command.position)
212 command.string[i] = command.seqs[i].getSequence(command.position,
213 command.position + command.number);
215 if (command.seqs[i].getDatasetSequence() != null
216 || command.seqs[i].getSequenceFeatures() != null)
218 for (int s = command.position; s < command.position + command.number;
221 if (jalview.schemes.ResidueProperties
222 .aaIndex[command.seqs[i].getCharAt(s)] != 23)
224 adjustFeatures(command, i,
225 command.seqs[i].findPosition(command.position),
226 command.seqs[i].findPosition(command.position +
233 command.seqs[i].deleteChars(command.position,
234 command.position + command.number);
237 if (command.seqs[i].getLength() < 1)
239 command.al.deleteSequence(command.seqs[i]);
244 adjustAnnotations(command, false, seqDeleted);
247 void paste(Edit command)
251 boolean seqWasDeleted=false;
252 int start = 0, end = 0;
254 for (int i = 0; i < command.seqs.length; i++)
257 if (command.seqs[i].getLength() < 1)
259 // ie this sequence was deleted, we need to
260 // read it to the alignment
261 if (command.alIndex[i] < command.al.getHeight())
263 command.al.getSequences().insertElementAt(command.seqs[i],
268 command.al.addSequence(command.seqs[i]);
272 tmp = new StringBuffer();
273 tmp.append(command.seqs[i].getSequence());
275 if (command.string != null && command.string[i] != null)
277 if (command.position >= tmp.length())
279 //This occurs if padding is on, and residues
280 //are removed from end of alignment
281 int length = command.position - tmp.length();
284 tmp.append(command.gapChar);
288 tmp.insert(command.position, command.string[i]);
290 for (int s = 0; s < command.string[i].length; s++)
292 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] !=
296 start = command.seqs[i].findPosition(command.position);
297 end = command.seqs[i].findPosition(command.position +
302 command.string[i] = null;
305 command.seqs[i].setSequence(tmp.toString());
309 if (command.seqs[i].getDatasetSequence() != null)
310 { // use new ds mechanism here
311 Sequence ds = new Sequence(command.seqs[i].getName(),
312 jalview.analysis.AlignSeq.extractGaps(
313 jalview.util.Comparison.GapChars,
314 command.seqs[i].getSequenceAsString()
316 command.seqs[i].getStart(),
317 command.seqs[i].getEnd());
318 ds.setDescription(command.seqs[i].getDescription());
319 command.seqs[i].setDatasetSequence(ds);
322 adjustFeatures(command, i, start, end, true);
325 adjustAnnotations(command, true, seqWasDeleted);
327 command.string = null;
330 final void adjustAnnotations(Edit command, boolean insert, boolean modifyVisibility)
333 AlignmentAnnotation[] annotations = null;
335 if (modifyVisibility && !insert)
337 // only occurs if a sequence was added or deleted.
338 command.deletedAnnotationRows = new Hashtable();
340 if (command.fullAlignmentHeight)
342 annotations = command.al.getAlignmentAnnotation();
347 AlignmentAnnotation[] tmp;
348 for (int s = 0; s < command.seqs.length; s++)
350 if (modifyVisibility)
352 // Rows are only removed or added to sequence object.
355 tmp = command.seqs[s].getAnnotation();
357 command.deletedAnnotationRows.put(command.seqs[s], tmp);
358 for (int aa =0; aa<tmp.length; aa++)
360 command.al.deleteAnnotation(tmp[aa]);
362 command.seqs[s].setAlignmentAnnotation(null);
366 if (command.deletedAnnotationRows!=null && command.deletedAnnotationRows.containsKey(command.seqs[s]))
368 AlignmentAnnotation[] revealed = (AlignmentAnnotation[]) command.deletedAnnotationRows.get(command.seqs[s]);
369 command.seqs[s].setAlignmentAnnotation(revealed);
370 if (revealed!=null) {
371 for (int aa =0; aa<revealed.length; aa++)
373 command.al.addAnnotation(revealed[aa]);
375 for (int aa =0; aa<revealed.length; aa++)
377 command.al.setAnnotationIndex(revealed[aa], aa);
385 if (command.seqs[s].getAnnotation() == null)
392 annotations = command.seqs[s].getAnnotation();
396 tmp = new AlignmentAnnotation
397 [aSize + command.seqs[s].getAnnotation().length];
399 System.arraycopy(annotations, 0, tmp, 0, aSize);
401 System.arraycopy(command.seqs[s].getAnnotation(),
403 command.seqs[s].getAnnotation().length);
407 aSize = annotations.length;
411 if (annotations == null)
418 command.deletedAnnotations = new Hashtable();
423 for (int a = 0; a < annotations.length; a++)
425 if (annotations[a].autoCalculated)
432 aSize = annotations[a].annotations.length;
435 temp = new Annotation[aSize + command.number];
439 if (command.position < aSize)
441 if (command.position + command.number > aSize)
447 tSize = aSize - command.number + command.position;
459 temp = new Annotation[tSize];
465 if (command.position < annotations[a].annotations.length)
467 System.arraycopy(annotations[a].annotations,
468 0, temp, 0, command.position);
470 if (command.deletedAnnotations != null
472 command.deletedAnnotations.containsKey(annotations[a].
475 Annotation[] restore = (Annotation[])
476 command.deletedAnnotations.get(annotations[a].annotationId);
478 System.arraycopy(restore,
486 System.arraycopy(annotations[a].annotations,
487 command.position, temp,
488 command.position + command.number,
489 aSize - command.position);
493 if (command.deletedAnnotations != null
495 command.deletedAnnotations.containsKey(annotations[a].
498 Annotation[] restore = (Annotation[])
499 command.deletedAnnotations.get(annotations[a].annotationId);
501 temp = new Annotation[annotations[a].annotations.length +
503 System.arraycopy(annotations[a].annotations,
505 annotations[a].annotations.length);
506 System.arraycopy(restore, 0, temp,
507 annotations[a].annotations.length, restore.length);
511 temp = annotations[a].annotations;
517 if (tSize != aSize || command.position < 2)
519 int copylen = Math.min(command.position, annotations[a].annotations.length);
521 System.arraycopy(annotations[a].annotations,
522 0, temp, 0, copylen); //command.position);
524 Annotation[] deleted = new Annotation[command.number];
525 if (copylen>command.position) {
526 copylen = Math.min(command.number, annotations[a].annotations.length-command.position);
529 System.arraycopy(annotations[a].annotations,
530 command.position, deleted, 0, copylen); // command.number);
534 command.deletedAnnotations.put(annotations[a].annotationId,
536 if (annotations[a].annotations.length>command.position+command.number) {
537 System.arraycopy(annotations[a].annotations,
538 command.position + command.number,
539 temp, command.position,
540 annotations[a].annotations.length - command.position - command.number); // aSize
545 int dSize = aSize - command.position;
549 Annotation[] deleted = new Annotation[command.number];
550 System.arraycopy(annotations[a].annotations,
551 command.position, deleted, 0, dSize);
553 command.deletedAnnotations.put(annotations[a].annotationId,
556 tSize = Math.min(annotations[a].annotations.length,
558 temp = new Annotation[tSize];
559 System.arraycopy(annotations[a].annotations,
564 temp = annotations[a].annotations;
569 annotations[a].annotations = temp;
573 final void adjustFeatures(Edit command, int index, int i, int j,
576 SequenceI seq = command.seqs[index];
577 SequenceI sequence = seq.getDatasetSequence();
578 if (sequence == null)
585 if (command.editedFeatures != null
586 && command.editedFeatures.containsKey(seq))
588 sequence.setSequenceFeatures(
589 (SequenceFeature[]) command.editedFeatures.get(seq)
596 SequenceFeature[] sf = sequence.getSequenceFeatures();
603 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
607 for (int s = 0; s < sf.length; s++)
609 SequenceFeature copy = new SequenceFeature(sf[s]);
613 if (sf[s].getEnd() < i)
618 if (sf[s].getBegin() > j)
620 sf[s].setBegin(copy.getBegin() - cSize);
621 sf[s].setEnd(copy.getEnd() - cSize);
625 if (sf[s].getBegin() >= i)
630 if (sf[s].getEnd() < j)
635 sf[s].setEnd(sf[s].getEnd() - (cSize));
637 if (sf[s].getBegin() > sf[s].getEnd())
639 sequence.deleteFeature(sf[s]);
643 if (command.editedFeatures == null)
645 command.editedFeatures = new Hashtable();
648 command.editedFeatures.put(seq, oldsf);
654 boolean fullAlignmentHeight = false;
655 Hashtable deletedAnnotationRows;
656 Hashtable deletedAnnotations;
657 Hashtable editedFeatures;
663 int position, number;
672 this.command = command;
674 this.position = position;
675 this.number = number;
676 this.gapChar = gapChar;
685 this.gapChar = al.getGapCharacter();
686 this.command = command;
688 this.position = position;
689 this.number = number;
692 alIndex = new int[seqs.length];
693 for (int i = 0; i < seqs.length; i++)
695 alIndex[i] = al.findIndex(seqs[i]);
698 fullAlignmentHeight = (al.getHeight() == seqs.length);