2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.commands;
\r
23 import jalview.datamodel.*;
\r
27 * <p>Title: EditCommmand</p>
\r
29 * <p>Description: Essential information for performing
\r
30 * undo and redo for cut/paste insert/delete gap
\r
31 * which can be stored in the HistoryList </p>
\r
33 * <p>Copyright: Copyright (c) 2006</p>
\r
35 * <p>Company: Dundee University</p>
\r
37 * @author not attributable
\r
40 public class EditCommand
\r
43 public static final int INSERT_GAP = 0;
\r
44 public static final int DELETE_GAP = 1;
\r
45 public static final int CUT = 2;
\r
46 public static final int PASTE = 3;
\r
52 public EditCommand()
\r
55 public EditCommand(String description)
\r
57 this.description = description;
\r
60 public EditCommand(String description,
\r
67 this.description = description;
\r
68 if (command == CUT || command == PASTE)
\r
72 new Edit(command, seqs, position, number, al)};
\r
78 final public String getDescription()
\r
83 public int getSize()
\r
85 return edits == null ? 0 : edits.length;
\r
88 final public AlignmentI getAlignment()
\r
93 final public void appendEdit(int command,
\r
98 boolean performEdit)
\r
100 Edit edit = new Edit(command, seqs, position, number, al.getGapCharacter());
\r
101 if (al.getHeight() == seqs.length)
\r
104 edit.fullAlignmentHeight = true;
\r
109 Edit[] temp = new Edit[edits.length + 1];
\r
110 System.arraycopy(edits, 0, temp, 0, edits.length);
\r
112 edits[edits.length - 1] = edit;
\r
123 performEdit(edits.length - 1);
\r
127 final void performEdit(int commandIndex)
\r
129 int eSize = edits.length;
\r
130 for (int e = commandIndex; e < eSize; e++)
\r
132 if (edits[e].command == INSERT_GAP)
\r
134 insertGap(edits[e]);
\r
136 else if (edits[e].command == DELETE_GAP)
\r
138 deleteGap(edits[e]);
\r
140 else if (edits[e].command == CUT)
\r
144 else if (edits[e].command == PASTE)
\r
151 final public void doCommand()
\r
156 final public void undoCommand()
\r
158 int e = 0, eSize = edits.length;
\r
159 for (e = eSize - 1; e > -1; e--)
\r
161 if (edits[e].command == INSERT_GAP)
\r
163 deleteGap(edits[e]);
\r
165 else if (edits[e].command == DELETE_GAP)
\r
167 insertGap(edits[e]);
\r
169 else if (edits[e].command == CUT)
\r
173 else if (edits[e].command == PASTE)
\r
180 final void insertGap(Edit command)
\r
182 for (int s = 0; s < command.seqs.length; s++)
\r
184 command.seqs[s].insertCharAt(command.position,
\r
189 adjustAnnotations(command, true);
\r
192 final void deleteGap(Edit command)
\r
194 for (int s = 0; s < command.seqs.length; s++)
\r
196 command.seqs[s].deleteChars(command.position,
\r
197 command.position + command.number);
\r
200 adjustAnnotations(command, false);
\r
203 void cut(Edit command)
\r
205 command.string = new char[command.seqs.length][];
\r
207 for (int i = 0; i < command.seqs.length; i++)
\r
209 if (command.seqs[i].getLength() > command.position)
\r
211 command.string[i] = command.seqs[i].getSequence(command.position,
\r
212 command.position + command.number);
\r
214 if (command.seqs[i].getDatasetSequence() != null
\r
215 || command.seqs[i].getSequenceFeatures() != null)
\r
217 for (int s = command.position; s < command.position + command.number;
\r
220 if (jalview.schemes.ResidueProperties
\r
221 .aaIndex[command.seqs[i].getCharAt(s)] != 23)
\r
223 adjustFeatures(command, i,
\r
224 command.seqs[i].findPosition(command.position),
\r
225 command.seqs[i].findPosition(command.position +
\r
232 command.seqs[i].deleteChars(command.position,
\r
233 command.position + command.number);
\r
236 if (command.seqs[i].getLength() < 1)
\r
238 command.al.deleteSequence(command.seqs[i]);
\r
242 adjustAnnotations(command, false);
\r
245 void paste(Edit command)
\r
248 boolean newDSNeeded;
\r
249 int start = 0, end = 0;
\r
251 for (int i = 0; i < command.seqs.length; i++)
\r
253 newDSNeeded = false;
\r
254 if (command.seqs[i].getLength() < 1)
\r
256 // ie this sequence was deleted, we need to
\r
257 // read it to the alignment
\r
258 if (command.alIndex[i] < command.al.getHeight())
\r
260 command.al.getSequences().insertElementAt(command.seqs[i],
\r
261 command.alIndex[i]);
\r
265 command.al.addSequence(command.seqs[i]);
\r
268 tmp = new StringBuffer();
\r
269 tmp.append(command.seqs[i].getSequence());
\r
271 if (command.string != null && command.string[i] != null)
\r
273 if (command.position >= tmp.length())
\r
275 //This occurs if padding is on, and residues
\r
276 //are removed from end of alignment
\r
277 int length = command.position - tmp.length();
\r
280 tmp.append(command.gapChar);
\r
284 tmp.insert(command.position, command.string[i]);
\r
286 for (int s = 0; s < command.string[i].length; s++)
\r
288 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] !=
\r
291 newDSNeeded = true;
\r
292 start = command.seqs[i].findPosition(command.position);
\r
293 end = command.seqs[i].findPosition(command.position +
\r
298 command.string[i] = null;
\r
301 command.seqs[i].setSequence(tmp.toString());
\r
305 if (command.seqs[i].getDatasetSequence() != null)
\r
307 Sequence ds = new Sequence(command.seqs[i].getName(),
\r
308 jalview.analysis.AlignSeq.extractGaps(
\r
309 jalview.util.Comparison.GapChars,
\r
310 command.seqs[i].getSequenceAsString()
\r
312 command.seqs[i].getStart(),
\r
313 command.seqs[i].getEnd());
\r
314 ds.setDescription(command.seqs[i].getDescription());
\r
315 command.seqs[i].setDatasetSequence(ds);
\r
318 adjustFeatures(command, i, start, end, true);
\r
322 adjustAnnotations(command, true);
\r
324 command.string = null;
\r
327 final void adjustAnnotations(Edit command, boolean insert)
\r
330 AlignmentAnnotation[] annotations = null;
\r
332 if (command.fullAlignmentHeight)
\r
334 annotations = command.al.getAlignmentAnnotation();
\r
339 AlignmentAnnotation[] tmp;
\r
340 for (int s = 0; s < command.seqs.length; s++)
\r
342 if (command.seqs[s].getAnnotation() == null)
\r
349 annotations = command.seqs[s].getAnnotation();
\r
353 tmp = new AlignmentAnnotation
\r
354 [aSize + command.seqs[s].getAnnotation().length];
\r
356 System.arraycopy(annotations, 0, tmp, 0, aSize);
\r
358 System.arraycopy(command.seqs[s].getAnnotation(),
\r
360 command.seqs[s].getAnnotation().length);
\r
365 aSize = annotations.length;
\r
369 if (annotations == null)
\r
376 command.deletedAnnotations = new Hashtable();
\r
381 for (int a = 0; a < annotations.length; a++)
\r
383 if (annotations[a].autoCalculated)
\r
390 aSize = annotations[a].annotations.length;
\r
393 temp = new Annotation[aSize + command.number];
\r
397 if (command.position < aSize)
\r
399 if (command.position + command.number > aSize)
\r
405 tSize = aSize - command.number + command.position;
\r
417 temp = new Annotation[tSize];
\r
423 if (command.position < annotations[a].annotations.length)
\r
425 System.arraycopy(annotations[a].annotations,
\r
426 0, temp, 0, command.position);
\r
428 if (command.deletedAnnotations != null
\r
430 command.deletedAnnotations.containsKey(annotations[a].
\r
433 Annotation[] restore = (Annotation[])
\r
434 command.deletedAnnotations.get(annotations[a].annotationId);
\r
436 System.arraycopy(restore,
\r
444 System.arraycopy(annotations[a].annotations,
\r
445 command.position, temp,
\r
446 command.position + command.number,
\r
447 aSize - command.position);
\r
451 if (command.deletedAnnotations != null
\r
453 command.deletedAnnotations.containsKey(annotations[a].
\r
456 Annotation[] restore = (Annotation[])
\r
457 command.deletedAnnotations.get(annotations[a].annotationId);
\r
459 temp = new Annotation[annotations[a].annotations.length +
\r
461 System.arraycopy(annotations[a].annotations,
\r
463 annotations[a].annotations.length);
\r
464 System.arraycopy(restore, 0, temp,
\r
465 annotations[a].annotations.length, restore.length);
\r
469 temp = annotations[a].annotations;
\r
475 if (tSize != aSize || command.position < 2)
\r
477 System.arraycopy(annotations[a].annotations,
\r
478 0, temp, 0, command.position);
\r
480 Annotation[] deleted = new Annotation[command.number];
\r
481 System.arraycopy(annotations[a].annotations,
\r
482 command.position, deleted, 0, command.number);
\r
484 command.deletedAnnotations.put(annotations[a].annotationId,
\r
487 System.arraycopy(annotations[a].annotations,
\r
488 command.position + command.number,
\r
489 temp, command.position,
\r
490 aSize - command.position - command.number);
\r
494 int dSize = aSize - command.position;
\r
498 Annotation[] deleted = new Annotation[command.number];
\r
499 System.arraycopy(annotations[a].annotations,
\r
500 command.position, deleted, 0, dSize);
\r
502 command.deletedAnnotations.put(annotations[a].annotationId,
\r
505 tSize = Math.min(annotations[a].annotations.length,
\r
507 temp = new Annotation[tSize];
\r
508 System.arraycopy(annotations[a].annotations,
\r
509 0, temp, 0, tSize);
\r
513 temp = annotations[a].annotations;
\r
518 annotations[a].annotations = temp;
\r
522 final void adjustFeatures(Edit command, int index, int i, int j,
\r
525 SequenceI seq = command.seqs[index];
\r
526 SequenceI sequence = seq.getDatasetSequence();
\r
527 if (sequence == null)
\r
534 if (command.editedFeatures != null
\r
535 && command.editedFeatures.containsKey(seq))
\r
537 sequence.setSequenceFeatures(
\r
538 (SequenceFeature[]) command.editedFeatures.get(seq)
\r
545 SequenceFeature[] sf = sequence.getSequenceFeatures();
\r
552 SequenceFeature[] oldsf = new SequenceFeature[sf.length];
\r
556 for (int s = 0; s < sf.length; s++)
\r
558 SequenceFeature copy = new SequenceFeature(sf[s]);
\r
562 if (sf[s].getEnd() < i)
\r
567 if (sf[s].getBegin() > j)
\r
569 sf[s].setBegin(copy.getBegin() - cSize);
\r
570 sf[s].setEnd(copy.getEnd() - cSize);
\r
574 if (sf[s].getBegin() >= i)
\r
579 if (sf[s].getEnd() < j)
\r
581 sf[s].setEnd(j - 1);
\r
584 sf[s].setEnd(sf[s].getEnd() - (cSize));
\r
586 if (sf[s].getBegin() > sf[s].getEnd())
\r
588 sequence.deleteFeature(sf[s]);
\r
592 if (command.editedFeatures == null)
\r
594 command.editedFeatures = new Hashtable();
\r
597 command.editedFeatures.put(seq, oldsf);
\r
603 boolean fullAlignmentHeight = false;
\r
604 Hashtable deletedAnnotations;
\r
605 Hashtable editedFeatures;
\r
611 int position, number;
\r
620 this.command = command;
\r
622 this.position = position;
\r
623 this.number = number;
\r
624 this.gapChar = gapChar;
\r
633 this.gapChar = al.getGapCharacter();
\r
634 this.command = command;
\r
636 this.position = position;
\r
637 this.number = number;
\r
640 alIndex = new int[seqs.length];
\r
641 for (int i = 0; i < seqs.length; i++)
\r
643 alIndex[i] = al.findIndex(seqs[i]);
\r
646 fullAlignmentHeight = (al.getHeight() == seqs.length);
\r