2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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
21 import jalview.datamodel.*;
\r
23 import java.util.Hashtable;
\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 implements CommandI
\r
42 public static final int INSERT_GAP = 0;
\r
43 public static final int DELETE_GAP = 1;
\r
44 public static final int CUT = 2;
\r
45 public static final int PASTE = 3;
\r
51 public EditCommand()
\r
54 public EditCommand(String description)
\r
56 this.description = description;
\r
59 public EditCommand( String description,
\r
66 this.description = description;
\r
67 if ( command==CUT || command==PASTE)
\r
69 edits = new Edit[]{new Edit(command, seqs, position, number, al)};
\r
76 final public String getDescription()
\r
81 public int getSize()
\r
83 return edits==null?0:edits.length;
\r
86 final public AlignmentI getAlignment()
\r
92 final public void appendEdit(int command,
\r
97 boolean performEdit)
\r
99 Edit edit = new Edit(command, seqs, position, number, al.getGapCharacter());
\r
100 if(al.getHeight()==seqs.length)
\r
103 edit.fullAlignmentHeight = true;
\r
108 Edit[] temp = new Edit[edits.length + 1];
\r
109 System.arraycopy(edits, 0, temp, 0, edits.length);
\r
111 edits[edits.length - 1] = edit;
\r
114 edits = new Edit[] { edit };
\r
117 performEdit(edits.length - 1);
\r
120 final void performEdit(int commandIndex)
\r
122 int eSize = edits.length;
\r
123 for (int e = commandIndex; e < eSize; e++)
\r
125 if (edits[e].command==INSERT_GAP)
\r
127 insertGap(edits[e]);
\r
129 else if (edits[e].command==DELETE_GAP)
\r
131 deleteGap(edits[e]);
\r
133 else if(edits[e].command==CUT)
\r
137 else if(edits[e].command==PASTE)
\r
144 final public void doCommand()
\r
149 final public void undoCommand()
\r
151 int e = 0, eSize = edits.length;
\r
152 for (e = eSize-1; e > -1; e--)
\r
154 if (edits[e].command==INSERT_GAP)
\r
156 deleteGap(edits[e]);
\r
158 else if (edits[e].command==DELETE_GAP)
\r
160 insertGap(edits[e]);
\r
162 else if (edits[e].command==CUT)
\r
166 else if (edits[e].command==PASTE)
\r
173 final void insertGap(Edit command)
\r
175 for(int s=0; s<command.seqs.length; s++)
\r
177 command.seqs[s].insertCharAt(command.position,
\r
182 adjustAnnotations(command, true);
\r
185 final void deleteGap(Edit command)
\r
187 for (int s = 0; s < command.seqs.length; s++)
\r
189 command.seqs[s].deleteChars(command.position, command.position+command.number);
\r
192 adjustAnnotations(command, false);
\r
195 void cut(Edit command)
\r
197 command.string = new char [command.seqs.length][];
\r
199 for(int i=0; i<command.seqs.length; i++)
\r
201 if(command.seqs[i].getLength()>command.position)
\r
203 command.string[i] = command.seqs[i].getSequence(command.position,
\r
204 command.position + command.number);
\r
206 if(command.seqs[i].getDatasetSequence()!=null
\r
207 || command.seqs[i].getSequenceFeatures()!=null)
\r
209 for (int s = command.position; s < command.position + command.number; s++)
\r
211 if (jalview.schemes.ResidueProperties
\r
212 .aaIndex[command.seqs[i].getCharAt(s)] != 23)
\r
214 adjustFeatures(command, i,
\r
215 command.seqs[i].findPosition(command.position),
\r
216 command.seqs[i].findPosition(command.position +
\r
223 command.seqs[i].deleteChars(command.position,
\r
224 command.position + command.number);
\r
227 if(command.seqs[i].getLength()<1)
\r
229 command.al.deleteSequence(command.seqs[i]);
\r
233 adjustAnnotations(command, false);
\r
236 void paste(Edit command)
\r
239 boolean newDSNeeded;
\r
240 int start=0, end=0;
\r
242 for(int i=0; i<command.seqs.length; i++)
\r
244 newDSNeeded = false;
\r
245 if(command.seqs[i].getLength()<1)
\r
247 // ie this sequence was deleted, we need to
\r
248 // read it to the alignment
\r
249 if (command.alIndex[i] < command.al.getHeight())
\r
250 command.al.getSequences().insertElementAt(command.seqs[i],
\r
251 command.alIndex[i]);
\r
253 command.al.addSequence(command.seqs[i]);
\r
255 tmp = new StringBuffer();
\r
256 tmp.append(command.seqs[i].getSequence());
\r
258 if(command.string!=null && command.string[i]!=null)
\r
260 if(command.position>=tmp.length())
\r
262 //This occurs if padding is on, and residues
\r
263 //are removed from end of alignment
\r
264 int length = command.position-tmp.length();
\r
267 tmp.append(command.gapChar);
\r
271 tmp.insert(command.position, command.string[i]);
\r
273 for (int s = 0; s < command.string[i].length; s++)
\r
275 if (jalview.schemes.ResidueProperties.aaIndex[command.string[i][s]] != 23)
\r
277 newDSNeeded = true;
\r
278 start = command.seqs[i].findPosition(command.position);
\r
279 end = command.seqs[i].findPosition(command.position+command.number);
\r
283 command.string[i] = null;
\r
287 command.seqs[i].setSequence(tmp.toString());
\r
291 if (command.seqs[i].getDatasetSequence() != null)
\r
293 Sequence ds = new Sequence(command.seqs[i].getName(),
\r
294 jalview.analysis.AlignSeq.extractGaps(
\r
295 jalview.util.Comparison.GapChars,
\r
296 command.seqs[i].getSequenceAsString()
\r
298 command.seqs[i].getStart(),
\r
299 command.seqs[i].getEnd());
\r
300 ds.setDescription(command.seqs[i].getDescription());
\r
301 command.seqs[i].setDatasetSequence(ds);
\r
304 adjustFeatures(command, i, start, end, true);
\r
309 adjustAnnotations(command, true);
\r
311 command.string = null;
\r
315 final void adjustAnnotations(Edit command, boolean insert)
\r
318 AlignmentAnnotation [] annotations = null;
\r
320 if (command.fullAlignmentHeight)
\r
322 annotations = command.al.getAlignmentAnnotation();
\r
327 AlignmentAnnotation [] tmp;
\r
328 for(int s=0; s<command.seqs.length; s++)
\r
330 if(command.seqs[s].getAnnotation()==null)
\r
334 annotations = command.seqs[s].getAnnotation();
\r
337 tmp = new AlignmentAnnotation
\r
338 [aSize + command.seqs[s].getAnnotation().length];
\r
340 System.arraycopy(annotations,0,tmp,0,aSize);
\r
342 System.arraycopy(command.seqs[s].getAnnotation(),
\r
343 0,tmp,aSize,command.seqs[s].getAnnotation().length);
\r
349 aSize = annotations.length;
\r
353 if(annotations==null)
\r
358 command.deletedAnnotations = new Hashtable();
\r
361 Annotation [] temp;
\r
362 for (int a = 0; a < annotations.length; a++)
\r
364 if(annotations[a].autoCalculated)
\r
369 aSize = annotations[a].annotations.length;
\r
371 tSize = aSize + command.number;
\r
373 tSize = aSize - command.number;
\r
375 temp = new Annotation[tSize];
\r
379 if(command.position < annotations[a].annotations.length)
\r
381 System.arraycopy(annotations[a].annotations,
\r
382 0, temp, 0, command.position);
\r
384 if (command.deletedAnnotations != null
\r
386 command.deletedAnnotations.containsKey(annotations[a].annotationId))
\r
388 Annotation[] restore = (Annotation[])
\r
389 command.deletedAnnotations.get(annotations[a].annotationId);
\r
391 System.arraycopy(restore,
\r
399 System.arraycopy(annotations[a].annotations,
\r
400 command.position, temp,
\r
401 command.position + command.number,
\r
402 aSize - command.position);
\r
405 temp = annotations[a].annotations;
\r
409 if(command.position < annotations[a].annotations.length)
\r
411 System.arraycopy(annotations[a].annotations,
\r
412 0, temp, 0, command.position);
\r
414 Annotation[] deleted = new Annotation[command.number];
\r
415 System.arraycopy(annotations[a].annotations,
\r
416 command.position, deleted, 0, command.number);
\r
418 command.deletedAnnotations.put(annotations[a].annotationId,
\r
421 System.arraycopy(annotations[a].annotations,
\r
422 command.position + command.number,
\r
423 temp, command.position,
\r
424 aSize - command.position - command.number);
\r
427 temp = annotations[a].annotations;
\r
430 annotations[a].annotations = temp;
\r
434 final void adjustFeatures(Edit command, int index, int i, int j, boolean insert)
\r
436 SequenceI seq = command.seqs[index];
\r
437 SequenceI sequence = seq.getDatasetSequence();
\r
443 if (command.editedFeatures != null
\r
444 && command.editedFeatures.containsKey(seq))
\r
445 sequence.setSequenceFeatures(
\r
446 (SequenceFeature[]) command.editedFeatures.get(seq)
\r
453 SequenceFeature [] sf = sequence.getSequenceFeatures();
\r
461 SequenceFeature [] oldsf = new SequenceFeature[sf.length];
\r
465 for (int s = 0; s < sf.length; s++)
\r
467 SequenceFeature copy = new SequenceFeature(sf[s]);
\r
471 if (sf[s].getEnd() < i)
\r
474 if (sf[s].getBegin() > j)
\r
476 sf[s].setBegin(copy.getBegin() - cSize);
\r
477 sf[s].setEnd(copy.getEnd() - cSize);
\r
481 if (sf[s].getBegin() >= i)
\r
484 if (sf[s].getEnd() < j)
\r
485 sf[s].setEnd(j - 1);
\r
487 sf[s].setEnd(sf[s].getEnd() - (cSize));
\r
489 if (sf[s].getBegin() > sf[s].getEnd())
\r
490 sequence.deleteFeature(sf[s]);
\r
493 if (command.editedFeatures == null)
\r
494 command.editedFeatures = new Hashtable();
\r
496 command.editedFeatures.put(seq, oldsf);
\r
503 boolean fullAlignmentHeight = false;
\r
504 Hashtable deletedAnnotations;
\r
505 Hashtable editedFeatures;
\r
511 int position, number;
\r
520 this.command = command;
\r
522 this.position = position;
\r
523 this.number = number;
\r
524 this.gapChar = gapChar;
\r
534 this.gapChar = al.getGapCharacter();
\r
535 this.command = command;
\r
537 this.position = position;
\r
538 this.number = number;
\r
541 alIndex = new int[seqs.length];
\r
542 for(int i=0; i<seqs.length; i++)
\r
544 alIndex[i] = al.findIndex(seqs[i]);
\r
547 fullAlignmentHeight = (al.getHeight()==seqs.length);
\r