JAL-2822 JAL-2684 only create dataset seq for an edit as needed, adjust start/end...
[jalview.git] / src / jalview / commands / EditCommand.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.commands;
22
23 import jalview.analysis.AlignSeq;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.Range;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
31 import jalview.datamodel.features.SequenceFeaturesI;
32 import jalview.util.Comparison;
33 import jalview.util.ReverseListIterator;
34 import jalview.util.StringUtils;
35
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.Hashtable;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.ListIterator;
42 import java.util.Map;
43
44 /**
45  * 
46  * <p>
47  * Title: EditCommmand
48  * </p>
49  * 
50  * <p>
51  * Description: Essential information for performing undo and redo for cut/paste
52  * insert/delete gap which can be stored in the HistoryList
53  * </p>
54  * 
55  * <p>
56  * Copyright: Copyright (c) 2006
57  * </p>
58  * 
59  * <p>
60  * Company: Dundee University
61  * </p>
62  * 
63  * @author not attributable
64  * @version 1.0
65  */
66 public class EditCommand implements CommandI
67 {
68   public enum Action
69   {
70     INSERT_GAP
71     {
72       @Override
73       public Action getUndoAction()
74       {
75         return DELETE_GAP;
76       }
77     },
78     DELETE_GAP
79     {
80       @Override
81       public Action getUndoAction()
82       {
83         return INSERT_GAP;
84       }
85     },
86     CUT
87     {
88       @Override
89       public Action getUndoAction()
90       {
91         return PASTE;
92       }
93     },
94     PASTE
95     {
96       @Override
97       public Action getUndoAction()
98       {
99         return CUT;
100       }
101     },
102     REPLACE
103     {
104       @Override
105       public Action getUndoAction()
106       {
107         return REPLACE;
108       }
109     },
110     INSERT_NUC
111     {
112       @Override
113       public Action getUndoAction()
114       {
115         return null;
116       }
117     };
118     public abstract Action getUndoAction();
119   };
120
121   private List<Edit> edits = new ArrayList<Edit>();
122
123   String description;
124
125   public EditCommand()
126   {
127   }
128
129   public EditCommand(String desc)
130   {
131     this.description = desc;
132   }
133
134   public EditCommand(String desc, Action command, SequenceI[] seqs,
135           int position, int number, AlignmentI al)
136   {
137     this.description = desc;
138     if (command == Action.CUT || command == Action.PASTE)
139     {
140       setEdit(new Edit(command, seqs, position, number, al));
141     }
142
143     performEdit(0, null);
144   }
145
146   public EditCommand(String desc, Action command, String replace,
147           SequenceI[] seqs, int position, int number, AlignmentI al)
148   {
149     this.description = desc;
150     if (command == Action.REPLACE)
151     {
152       setEdit(new Edit(command, seqs, position, number, al, replace));
153     }
154
155     performEdit(0, null);
156   }
157
158   /**
159    * Set the list of edits to the specified item (only).
160    * 
161    * @param e
162    */
163   protected void setEdit(Edit e)
164   {
165     edits.clear();
166     edits.add(e);
167   }
168
169   /**
170    * Add the given edit command to the stored list of commands. If simply
171    * expanding the range of the last command added, then modify it instead of
172    * adding a new command.
173    * 
174    * @param e
175    */
176   public void addEdit(Edit e)
177   {
178     if (!expandEdit(edits, e))
179     {
180       edits.add(e);
181     }
182   }
183
184   /**
185    * Returns true if the new edit is incorporated by updating (expanding the
186    * range of) the last edit on the list, else false. We can 'expand' the last
187    * edit if the new one is the same action, on the same sequences, and acts on
188    * a contiguous range. This is the case where a mouse drag generates a series
189    * of contiguous gap insertions or deletions.
190    * 
191    * @param edits
192    * @param e
193    * @return
194    */
195   protected static boolean expandEdit(List<Edit> edits, Edit e)
196   {
197     if (edits == null || edits.isEmpty())
198     {
199       return false;
200     }
201     Edit lastEdit = edits.get(edits.size() - 1);
202     Action action = e.command;
203     if (lastEdit.command != action)
204     {
205       return false;
206     }
207
208     /*
209      * Both commands must act on the same sequences - compare the underlying
210      * dataset sequences, rather than the aligned sequences, which change as
211      * they are edited.
212      */
213     if (lastEdit.seqs.length != e.seqs.length)
214     {
215       return false;
216     }
217     for (int i = 0; i < e.seqs.length; i++)
218     {
219       if (lastEdit.seqs[i].getDatasetSequence() != e.seqs[i]
220               .getDatasetSequence())
221       {
222         return false;
223       }
224     }
225
226     /**
227      * Check a contiguous edit; either
228      * <ul>
229      * <li>a new Insert <n> positions to the right of the last <insert n>,
230      * or</li>
231      * <li>a new Delete <n> gaps which is <n> positions to the left of the last
232      * delete.</li>
233      * </ul>
234      */
235     boolean contiguous = (action == Action.INSERT_GAP
236             && e.position == lastEdit.position + lastEdit.number)
237             || (action == Action.DELETE_GAP
238                     && e.position + e.number == lastEdit.position);
239     if (contiguous)
240     {
241       /*
242        * We are just expanding the range of the last edit. For delete gap, also
243        * moving the start position left.
244        */
245       lastEdit.number += e.number;
246       lastEdit.seqs = e.seqs;
247       if (action == Action.DELETE_GAP)
248       {
249         lastEdit.position--;
250       }
251       return true;
252     }
253     return false;
254   }
255
256   /**
257    * Clear the list of stored edit commands.
258    * 
259    */
260   protected void clearEdits()
261   {
262     edits.clear();
263   }
264
265   /**
266    * Returns the i'th stored Edit command.
267    * 
268    * @param i
269    * @return
270    */
271   protected Edit getEdit(int i)
272   {
273     if (i >= 0 && i < edits.size())
274     {
275       return edits.get(i);
276     }
277     return null;
278   }
279
280   @Override
281   final public String getDescription()
282   {
283     return description;
284   }
285
286   @Override
287   public int getSize()
288   {
289     return edits.size();
290   }
291
292   /**
293    * Return the alignment for the first edit (or null if no edit).
294    * 
295    * @return
296    */
297   final public AlignmentI getAlignment()
298   {
299     return (edits.isEmpty() ? null : edits.get(0).al);
300   }
301
302   /**
303    * append a new editCommand Note. this shouldn't be called if the edit is an
304    * operation affects more alignment objects than the one referenced in al (for
305    * example, cut or pasting whole sequences). Use the form with an additional
306    * AlignmentI[] views parameter.
307    * 
308    * @param command
309    * @param seqs
310    * @param position
311    * @param number
312    * @param al
313    * @param performEdit
314    */
315   final public void appendEdit(Action command, SequenceI[] seqs,
316           int position, int number, AlignmentI al, boolean performEdit)
317   {
318     appendEdit(command, seqs, position, number, al, performEdit, null);
319   }
320
321   /**
322    * append a new edit command with a set of alignment views that may be
323    * operated on
324    * 
325    * @param command
326    * @param seqs
327    * @param position
328    * @param number
329    * @param al
330    * @param performEdit
331    * @param views
332    */
333   final public void appendEdit(Action command, SequenceI[] seqs,
334           int position, int number, AlignmentI al, boolean performEdit,
335           AlignmentI[] views)
336   {
337     Edit edit = new Edit(command, seqs, position, number, al);
338     appendEdit(edit, al, performEdit, views);
339   }
340
341   /**
342    * Overloaded method that accepts an Edit object with additional parameters.
343    * 
344    * @param edit
345    * @param al
346    * @param performEdit
347    * @param views
348    */
349   final public void appendEdit(Edit edit, AlignmentI al,
350           boolean performEdit, AlignmentI[] views)
351   {
352     if (al.getHeight() == edit.seqs.length)
353     {
354       edit.al = al;
355       edit.fullAlignmentHeight = true;
356     }
357
358     addEdit(edit);
359
360     if (performEdit)
361     {
362       performEdit(edit, views);
363     }
364   }
365
366   /**
367    * Execute all the edit commands, starting at the given commandIndex
368    * 
369    * @param commandIndex
370    * @param views
371    */
372   public final void performEdit(int commandIndex, AlignmentI[] views)
373   {
374     ListIterator<Edit> iterator = edits.listIterator(commandIndex);
375     while (iterator.hasNext())
376     {
377       Edit edit = iterator.next();
378       performEdit(edit, views);
379     }
380   }
381
382   /**
383    * Execute one edit command in all the specified alignment views
384    * 
385    * @param edit
386    * @param views
387    */
388   protected static void performEdit(Edit edit, AlignmentI[] views)
389   {
390     switch (edit.command)
391     {
392     case INSERT_GAP:
393       insertGap(edit);
394       break;
395     case DELETE_GAP:
396       deleteGap(edit);
397       break;
398     case CUT:
399       cut(edit, views);
400       break;
401     case PASTE:
402       paste(edit, views);
403       break;
404     case REPLACE:
405       replace(edit);
406       break;
407     case INSERT_NUC:
408       // TODO:add deleteNuc for UNDO
409       // case INSERT_NUC:
410       // insertNuc(edits[e]);
411       break;
412     default:
413       break;
414     }
415   }
416
417   @Override
418   final public void doCommand(AlignmentI[] views)
419   {
420     performEdit(0, views);
421   }
422
423   /**
424    * Undo the stored list of commands, in reverse order.
425    */
426   @Override
427   final public void undoCommand(AlignmentI[] views)
428   {
429     ListIterator<Edit> iterator = edits.listIterator(edits.size());
430     while (iterator.hasPrevious())
431     {
432       Edit e = iterator.previous();
433       switch (e.command)
434       {
435       case INSERT_GAP:
436         deleteGap(e);
437         break;
438       case DELETE_GAP:
439         insertGap(e);
440         break;
441       case CUT:
442         paste(e, views);
443         break;
444       case PASTE:
445         cut(e, views);
446         break;
447       case REPLACE:
448         replace(e);
449         break;
450       case INSERT_NUC:
451         // not implemented
452         break;
453       default:
454         break;
455       }
456     }
457   }
458
459   /**
460    * Insert gap(s) in sequences as specified by the command, and adjust
461    * annotations.
462    * 
463    * @param command
464    */
465   final private static void insertGap(Edit command)
466   {
467
468     for (int s = 0; s < command.seqs.length; s++)
469     {
470       command.seqs[s].insertCharAt(command.position, command.number,
471               command.gapChar);
472       // System.out.println("pos: "+command.position+" number:
473       // "+command.number);
474     }
475
476     adjustAnnotations(command, true, false, null);
477   }
478
479   //
480   // final void insertNuc(Edit command)
481   // {
482   //
483   // for (int s = 0; s < command.seqs.length; s++)
484   // {
485   // System.out.println("pos: "+command.position+" number: "+command.number);
486   // command.seqs[s].insertCharAt(command.position, command.number,'A');
487   // }
488   //
489   // adjustAnnotations(command, true, false, null);
490   // }
491
492   /**
493    * Delete gap(s) in sequences as specified by the command, and adjust
494    * annotations.
495    * 
496    * @param command
497    */
498   final static private void deleteGap(Edit command)
499   {
500     for (int s = 0; s < command.seqs.length; s++)
501     {
502       command.seqs[s].deleteChars(command.position,
503               command.position + command.number);
504     }
505
506     adjustAnnotations(command, false, false, null);
507   }
508
509   /**
510    * Carry out a Cut action. The cut characters are saved in case Undo is
511    * requested.
512    * 
513    * @param command
514    * @param views
515    */
516   static void cut(Edit command, AlignmentI[] views)
517   {
518     boolean seqDeleted = false;
519     command.string = new char[command.seqs.length][];
520
521     for (int i = 0; i < command.seqs.length; i++)
522     {
523       final SequenceI sequence = command.seqs[i];
524       if (sequence.getLength() > command.position)
525       {
526         command.string[i] = sequence.getSequence(command.position,
527                 command.position + command.number);
528         SequenceI oldds = sequence.getDatasetSequence();
529         if (command.oldds != null && command.oldds[i] != null)
530         {
531           // we are redoing an undone cut.
532           sequence.setDatasetSequence(null);
533         }
534         Range cutPositions = sequence.findPositions(command.position + 1,
535                 command.position + command.number);
536         boolean cutIsInternal = cutPositions != null
537                 && sequence.getStart() != cutPositions
538                 .getBegin() && sequence.getEnd() != cutPositions.getEnd();
539         sequence.deleteChars(command.position, command.position
540                 + command.number);
541
542         if (command.oldds != null && command.oldds[i] != null)
543         {
544           // Undoing previous Paste - so
545           // oldds entry contains the cut dataset sequence,
546           // with sequence features in expected place.
547           sequence.setDatasetSequence(command.oldds[i]);
548           command.oldds[i] = oldds;
549         }
550         else
551         {
552           // New cut operation
553           // We always keep track of the dataset sequence so we can safely
554           // restore it during the Undo
555           if (command.oldds == null)
556           {
557             command.oldds = new SequenceI[command.seqs.length];
558           }
559           command.oldds[i] = oldds;
560
561           // do we need to edit sequence features for new sequence ?
562           if (oldds != sequence.getDatasetSequence()
563                   || (cutIsInternal
564                           && sequence.getFeatures().hasFeatures()))
565           {
566             if (cutPositions != null)
567             {
568               cutFeatures(command, sequence, cutPositions.getBegin(),
569                               cutPositions.getEnd(), cutIsInternal);
570             }
571           }
572         }
573       }
574
575       if (sequence.getLength() < 1)
576       {
577         command.al.deleteSequence(sequence);
578         seqDeleted = true;
579       }
580     }
581
582     adjustAnnotations(command, false, seqDeleted, views);
583   }
584
585   /**
586    * Perform the given Paste command. This may be to add cut or copied sequences
587    * to an alignment, or to undo a 'Cut' action on a region of the alignment.
588    * 
589    * @param command
590    * @param views
591    */
592   static void paste(Edit command, AlignmentI[] views)
593   {
594     boolean seqWasDeleted = false;
595
596     for (int i = 0; i < command.seqs.length; i++)
597     {
598       boolean newDSNeeded = false;
599       boolean newDSWasNeeded = command.oldds != null
600               && command.oldds[i] != null;
601       SequenceI sequence = command.seqs[i];
602       if (sequence.getLength() < 1)
603       {
604         /*
605          * sequence was deleted; re-add it to the alignment
606          */
607         if (command.alIndex[i] < command.al.getHeight())
608         {
609           List<SequenceI> sequences;
610           synchronized (sequences = command.al.getSequences())
611           {
612             if (!(command.alIndex[i] < 0))
613             {
614               sequences.add(command.alIndex[i], sequence);
615             }
616           }
617         }
618         else
619         {
620           command.al.addSequence(sequence);
621         }
622         seqWasDeleted = true;
623       }
624       int newStart = sequence.getStart();
625       int newEnd = sequence.getEnd();
626
627       StringBuilder tmp = new StringBuilder();
628       tmp.append(sequence.getSequence());
629       // Undo of a delete does not replace original dataset sequence on to
630       // alignment sequence.
631
632       int start = 0;
633       int length = 0;
634
635       if (command.string != null && command.string[i] != null)
636       {
637         if (command.position >= tmp.length())
638         {
639           // This occurs if padding is on, and residues
640           // are removed from end of alignment
641           int len = command.position - tmp.length();
642           while (len > 0)
643           {
644             tmp.append(command.gapChar);
645             len--;
646           }
647         }
648         tmp.insert(command.position, command.string[i]);
649         for (int s = 0; s < command.string[i].length; s++)
650         {
651           if (!Comparison.isGap(command.string[i][s]))
652           {
653             length++;
654             if (!newDSNeeded)
655             {
656               newDSNeeded = true;
657               start = sequence.findPosition(command.position);
658               // end = sequence
659               // .findPosition(command.position + command.number);
660             }
661             if (sequence.getStart() == start)
662             {
663               newStart--;
664             }
665             else
666             {
667               newEnd++;
668             }
669           }
670         }
671         command.string[i] = null;
672       }
673
674       sequence.setSequence(tmp.toString());
675       sequence.setStart(newStart);
676       sequence.setEnd(newEnd);
677
678       /*
679        * command and Undo share the same dataset sequence if cut was
680        * at start or end of sequence
681        */
682       boolean sameDatasetSequence = false;
683       if (newDSNeeded)
684       {
685         if (sequence.getDatasetSequence() != null)
686         {
687           SequenceI ds;
688           if (newDSWasNeeded)
689           {
690             ds = command.oldds[i];
691           }
692           else
693           {
694             // make a new DS sequence
695             // use new ds mechanism here
696             String ungapped = AlignSeq.extractGaps(Comparison.GapChars,
697                     sequence.getSequenceAsString());
698             ds = new Sequence(sequence.getName(), ungapped,
699                     sequence.getStart(), sequence.getEnd());
700             ds.setDescription(sequence.getDescription());
701           }
702           if (command.oldds == null)
703           {
704             command.oldds = new SequenceI[command.seqs.length];
705           }
706           command.oldds[i] = sequence.getDatasetSequence();
707           sameDatasetSequence = ds == sequence.getDatasetSequence();
708           ds.setSequenceFeatures(sequence.getSequenceFeatures());
709           sequence.setDatasetSequence(ds);
710         }
711         undoCutFeatures(command, command.seqs[i], start, length,
712                 sameDatasetSequence);
713       }
714     }
715     adjustAnnotations(command, true, seqWasDeleted, views);
716
717     command.string = null;
718   }
719
720   static void replace(Edit command)
721   {
722     StringBuffer tmp;
723     String oldstring;
724     int start = command.position;
725     int end = command.number;
726     // TODO TUTORIAL - Fix for replacement with different length of sequence (or
727     // whole sequence)
728     // TODO Jalview 2.4 bugfix change to an aggregate command - original
729     // sequence string is cut, new string is pasted in.
730     command.number = start + command.string[0].length;
731     for (int i = 0; i < command.seqs.length; i++)
732     {
733       boolean newDSWasNeeded = command.oldds != null
734               && command.oldds[i] != null;
735       boolean newStartEndWasNeeded = command.oldStartEnd!=null && command.oldStartEnd[i]!=null;
736
737       /**
738        * cut addHistoryItem(new EditCommand("Cut Sequences", EditCommand.CUT,
739        * cut, sg.getStartRes(), sg.getEndRes()-sg.getStartRes()+1,
740        * viewport.alignment));
741        * 
742        */
743       /**
744        * then addHistoryItem(new EditCommand( "Add sequences",
745        * EditCommand.PASTE, sequences, 0, alignment.getWidth(), alignment) );
746        * 
747        */
748
749       Range beforeEditedPositions = command.seqs[i].findPositions(1, start);
750       Range afterEditedPositions = command.seqs[i]
751               .findPositions(start + end + 1, command.seqs[i].getLength());
752       
753       oldstring = command.seqs[i].getSequenceAsString();
754       tmp = new StringBuffer(oldstring.substring(0, start));
755       tmp.append(command.string[i]);
756       String nogaprep = jalview.analysis.AlignSeq.extractGaps(
757               jalview.util.Comparison.GapChars,
758               new String(command.string[i]));
759       int ipos = command.seqs[i].findPosition(start)
760               - command.seqs[i].getStart();
761       tmp.append(oldstring.substring(end));
762       command.seqs[i].setSequence(tmp.toString());
763       command.string[i] = oldstring.substring(start, end).toCharArray();
764       String nogapold = AlignSeq.extractGaps(Comparison.GapChars,
765               new String(command.string[i]));
766
767       if (!nogaprep.toLowerCase().equals(nogapold.toLowerCase()))
768       {
769         // probably need a new dataset sequence
770         if (newDSWasNeeded)
771         {
772           // then just switch the dataset sequence
773           SequenceI oldds = command.seqs[i].getDatasetSequence();
774           command.seqs[i].setDatasetSequence(command.oldds[i]);
775           command.oldds[i] = oldds;
776         }
777         else
778         if (newStartEndWasNeeded)
779         {
780           Range newStart = command.oldStartEnd[i];
781           command.oldStartEnd[i] = new Range(command.seqs[i].getStart(),
782                   command.seqs[i].getEnd());
783           command.seqs[i].setStart(newStart.getBegin());
784           command.seqs[i].setEnd(newStart.getEnd());
785         }
786         else         
787         {
788           // first edit the original dataset sequence string
789           SequenceI oldds = command.seqs[i].getDatasetSequence();
790           String fullseq, osp = oldds.getSequenceAsString();
791
792           fullseq = osp.substring(0, ipos) + nogaprep
793                   + osp.substring(ipos + nogaprep.length());
794
795           // and check if new sequence data is different..
796           if (!fullseq.equalsIgnoreCase(osp))
797           {
798             // old ds and edited ds are different, so
799             // create the new dataset sequence
800             SequenceI newds = new Sequence(oldds);
801             newds.setSequence(fullseq.toUpperCase());
802
803             if (command.oldds == null)
804             {
805               command.oldds = new SequenceI[command.seqs.length];
806             }
807             command.oldds[i] = command.seqs[i].getDatasetSequence();
808             // TODO: JAL-1131 ensure newly created dataset sequence is added to
809             // the set of
810             // dataset sequences associated with the alignment.
811             // TODO: JAL-1131 fix up any annotation associated with new dataset
812             // sequence to ensure that original sequence/annotation
813             // relationships
814             // are preserved.
815             command.seqs[i].setDatasetSequence(newds);
816           }
817           else
818           {
819             if (command.oldStartEnd == null)
820             {
821               command.oldStartEnd = new Range[command.seqs.length];
822             }
823             command.oldStartEnd[i] = new Range(command.seqs[i].getStart(),
824                     command.seqs[i].getEnd());
825             if (beforeEditedPositions != null
826                     && afterEditedPositions == null)
827             {
828               // modification at end
829               command.seqs[i].setEnd(
830                       beforeEditedPositions.getEnd() + nogaprep.length());
831             }
832             else if (afterEditedPositions != null
833                     && beforeEditedPositions == null)
834             {
835               // modification at start
836               command.seqs[i].setStart(
837                       afterEditedPositions.getBegin() - nogaprep.length());
838             }
839             else
840             {
841               // edit covered both start and end. Here we can only guess the
842               // new
843               // start/end
844               String nogapalseq = jalview.analysis.AlignSeq.extractGaps(
845                       jalview.util.Comparison.GapChars,
846                       command.seqs[i].getSequenceAsString().toUpperCase());
847               int newStart = command.seqs[i].getDatasetSequence()
848                       .getSequenceAsString().indexOf(nogapalseq);
849               if (newStart == -1)
850               {
851                 throw new Error(
852                         "Implementation Error: could not locate start/end "
853                                 + "in dataset sequence after an edit of the sequence string");
854               }
855               int newEnd = newStart + nogapalseq.length() - 1;
856               command.seqs[i].setStart(newStart);
857               command.seqs[i].setEnd(newEnd);
858             }
859           }
860         }
861       }
862       tmp = null;
863       oldstring = null;
864     }
865   }
866
867   final static void adjustAnnotations(Edit command, boolean insert,
868           boolean modifyVisibility, AlignmentI[] views)
869   {
870     AlignmentAnnotation[] annotations = null;
871
872     if (modifyVisibility && !insert)
873     {
874       // only occurs if a sequence was added or deleted.
875       command.deletedAnnotationRows = new Hashtable<SequenceI, AlignmentAnnotation[]>();
876     }
877     if (command.fullAlignmentHeight)
878     {
879       annotations = command.al.getAlignmentAnnotation();
880     }
881     else
882     {
883       int aSize = 0;
884       AlignmentAnnotation[] tmp;
885       for (int s = 0; s < command.seqs.length; s++)
886       {
887         command.seqs[s].sequenceChanged();
888
889         if (modifyVisibility)
890         {
891           // Rows are only removed or added to sequence object.
892           if (!insert)
893           {
894             // remove rows
895             tmp = command.seqs[s].getAnnotation();
896             if (tmp != null)
897             {
898               int alen = tmp.length;
899               for (int aa = 0; aa < tmp.length; aa++)
900               {
901                 if (!command.al.deleteAnnotation(tmp[aa]))
902                 {
903                   // strip out annotation not in the current al (will be put
904                   // back on insert in all views)
905                   tmp[aa] = null;
906                   alen--;
907                 }
908               }
909               command.seqs[s].setAlignmentAnnotation(null);
910               if (alen != tmp.length)
911               {
912                 // save the non-null annotation references only
913                 AlignmentAnnotation[] saved = new AlignmentAnnotation[alen];
914                 for (int aa = 0, aapos = 0; aa < tmp.length; aa++)
915                 {
916                   if (tmp[aa] != null)
917                   {
918                     saved[aapos++] = tmp[aa];
919                     tmp[aa] = null;
920                   }
921                 }
922                 tmp = saved;
923                 command.deletedAnnotationRows.put(command.seqs[s], saved);
924                 // and then remove any annotation in the other views
925                 for (int alview = 0; views != null
926                         && alview < views.length; alview++)
927                 {
928                   if (views[alview] != command.al)
929                   {
930                     AlignmentAnnotation[] toremove = views[alview]
931                             .getAlignmentAnnotation();
932                     if (toremove == null || toremove.length == 0)
933                     {
934                       continue;
935                     }
936                     // remove any alignment annotation on this sequence that's
937                     // on that alignment view.
938                     for (int aa = 0; aa < toremove.length; aa++)
939                     {
940                       if (toremove[aa].sequenceRef == command.seqs[s])
941                       {
942                         views[alview].deleteAnnotation(toremove[aa]);
943                       }
944                     }
945                   }
946                 }
947               }
948               else
949               {
950                 // save all the annotation
951                 command.deletedAnnotationRows.put(command.seqs[s], tmp);
952               }
953             }
954           }
955           else
956           {
957             // recover rows
958             if (command.deletedAnnotationRows != null
959                     && command.deletedAnnotationRows
960                             .containsKey(command.seqs[s]))
961             {
962               AlignmentAnnotation[] revealed = command.deletedAnnotationRows
963                       .get(command.seqs[s]);
964               command.seqs[s].setAlignmentAnnotation(revealed);
965               if (revealed != null)
966               {
967                 for (int aa = 0; aa < revealed.length; aa++)
968                 {
969                   // iterate through al adding original annotation
970                   command.al.addAnnotation(revealed[aa]);
971                 }
972                 for (int aa = 0; aa < revealed.length; aa++)
973                 {
974                   command.al.setAnnotationIndex(revealed[aa], aa);
975                 }
976                 // and then duplicate added annotation on every other alignment
977                 // view
978                 for (int vnum = 0; views != null && vnum < views.length; vnum++)
979                 {
980                   if (views[vnum] != command.al)
981                   {
982                     int avwidth = views[vnum].getWidth() + 1;
983                     // duplicate in this view
984                     for (int a = 0; a < revealed.length; a++)
985                     {
986                       AlignmentAnnotation newann = new AlignmentAnnotation(
987                               revealed[a]);
988                       command.seqs[s].addAlignmentAnnotation(newann);
989                       newann.padAnnotation(avwidth);
990                       views[vnum].addAnnotation(newann);
991                       views[vnum].setAnnotationIndex(newann, a);
992                     }
993                   }
994                 }
995               }
996             }
997           }
998           continue;
999         }
1000
1001         if (command.seqs[s].getAnnotation() == null)
1002         {
1003           continue;
1004         }
1005
1006         if (aSize == 0)
1007         {
1008           annotations = command.seqs[s].getAnnotation();
1009         }
1010         else
1011         {
1012           tmp = new AlignmentAnnotation[aSize
1013                   + command.seqs[s].getAnnotation().length];
1014
1015           System.arraycopy(annotations, 0, tmp, 0, aSize);
1016
1017           System.arraycopy(command.seqs[s].getAnnotation(), 0, tmp, aSize,
1018                   command.seqs[s].getAnnotation().length);
1019
1020           annotations = tmp;
1021         }
1022         aSize = annotations.length;
1023       }
1024     }
1025
1026     if (annotations == null)
1027     {
1028       return;
1029     }
1030
1031     if (!insert)
1032     {
1033       command.deletedAnnotations = new Hashtable<String, Annotation[]>();
1034     }
1035
1036     int aSize;
1037     Annotation[] temp;
1038     for (int a = 0; a < annotations.length; a++)
1039     {
1040       if (annotations[a].autoCalculated
1041               || annotations[a].annotations == null)
1042       {
1043         continue;
1044       }
1045
1046       int tSize = 0;
1047
1048       aSize = annotations[a].annotations.length;
1049       if (insert)
1050       {
1051         temp = new Annotation[aSize + command.number];
1052         if (annotations[a].padGaps)
1053         {
1054           for (int aa = 0; aa < temp.length; aa++)
1055           {
1056             temp[aa] = new Annotation(command.gapChar + "", null, ' ', 0);
1057           }
1058         }
1059       }
1060       else
1061       {
1062         if (command.position < aSize)
1063         {
1064           if (command.position + command.number >= aSize)
1065           {
1066             tSize = aSize;
1067           }
1068           else
1069           {
1070             tSize = aSize - command.number;
1071           }
1072         }
1073         else
1074         {
1075           tSize = aSize;
1076         }
1077
1078         if (tSize < 0)
1079         {
1080           tSize = aSize;
1081         }
1082         temp = new Annotation[tSize];
1083       }
1084
1085       if (insert)
1086       {
1087         if (command.position < annotations[a].annotations.length)
1088         {
1089           System.arraycopy(annotations[a].annotations, 0, temp, 0,
1090                   command.position);
1091
1092           if (command.deletedAnnotations != null
1093                   && command.deletedAnnotations
1094                           .containsKey(annotations[a].annotationId))
1095           {
1096             Annotation[] restore = command.deletedAnnotations
1097                     .get(annotations[a].annotationId);
1098
1099             System.arraycopy(restore, 0, temp, command.position,
1100                     command.number);
1101
1102           }
1103
1104           System.arraycopy(annotations[a].annotations, command.position,
1105                   temp, command.position + command.number,
1106                   aSize - command.position);
1107         }
1108         else
1109         {
1110           if (command.deletedAnnotations != null
1111                   && command.deletedAnnotations
1112                           .containsKey(annotations[a].annotationId))
1113           {
1114             Annotation[] restore = command.deletedAnnotations
1115                     .get(annotations[a].annotationId);
1116
1117             temp = new Annotation[annotations[a].annotations.length
1118                     + restore.length];
1119             System.arraycopy(annotations[a].annotations, 0, temp, 0,
1120                     annotations[a].annotations.length);
1121             System.arraycopy(restore, 0, temp,
1122                     annotations[a].annotations.length, restore.length);
1123           }
1124           else
1125           {
1126             temp = annotations[a].annotations;
1127           }
1128         }
1129       }
1130       else
1131       {
1132         if (tSize != aSize || command.position < 2)
1133         {
1134           int copylen = Math.min(command.position,
1135                   annotations[a].annotations.length);
1136           if (copylen > 0)
1137           {
1138             System.arraycopy(annotations[a].annotations, 0, temp, 0,
1139                     copylen); // command.position);
1140           }
1141
1142           Annotation[] deleted = new Annotation[command.number];
1143           if (copylen >= command.position)
1144           {
1145             copylen = Math.min(command.number,
1146                     annotations[a].annotations.length - command.position);
1147             if (copylen > 0)
1148             {
1149               System.arraycopy(annotations[a].annotations, command.position,
1150                       deleted, 0, copylen); // command.number);
1151             }
1152           }
1153
1154           command.deletedAnnotations.put(annotations[a].annotationId,
1155                   deleted);
1156           if (annotations[a].annotations.length > command.position
1157                   + command.number)
1158           {
1159             System.arraycopy(annotations[a].annotations,
1160                     command.position + command.number, temp,
1161                     command.position, annotations[a].annotations.length
1162                             - command.position - command.number); // aSize
1163           }
1164         }
1165         else
1166         {
1167           int dSize = aSize - command.position;
1168
1169           if (dSize > 0)
1170           {
1171             Annotation[] deleted = new Annotation[command.number];
1172             System.arraycopy(annotations[a].annotations, command.position,
1173                     deleted, 0, dSize);
1174
1175             command.deletedAnnotations.put(annotations[a].annotationId,
1176                     deleted);
1177
1178             tSize = Math.min(annotations[a].annotations.length,
1179                     command.position);
1180             temp = new Annotation[tSize];
1181             System.arraycopy(annotations[a].annotations, 0, temp, 0, tSize);
1182           }
1183           else
1184           {
1185             temp = annotations[a].annotations;
1186           }
1187         }
1188       }
1189
1190       annotations[a].annotations = temp;
1191     }
1192   }
1193
1194   /**
1195    * Restores features to the state before a Cut.
1196    * <ul>
1197    * <li>re-add any features deleted by the cut</li>
1198    * <li>remove any truncated features created by the cut</li>
1199    * <li>shift right any features to the right of the cut</li>
1200    * </ul>
1201    * 
1202    * @param command
1203    *          the Cut command
1204    * @param seq
1205    *          the sequence the Cut applied to
1206    * @param start
1207    *          the start residue position of the cut
1208    * @param length
1209    *          the number of residues cut
1210    * @param sameDatasetSequence
1211    *          true if dataset sequence and frame of reference were left
1212    *          unchanged by the Cut
1213    */
1214   final static void undoCutFeatures(Edit command, SequenceI seq,
1215           final int start, final int length, boolean sameDatasetSequence)
1216   {
1217     SequenceI sequence = seq.getDatasetSequence();
1218     if (sequence == null)
1219     {
1220       sequence = seq;
1221     }
1222
1223     /*
1224      * shift right features that lie to the right of the restored cut (but not 
1225      * if dataset sequence unchanged - so coordinates were changed by Cut)
1226      */
1227     if (!sameDatasetSequence)
1228     {
1229       /*
1230        * shift right all features right of and not 
1231        * contiguous with the cut position
1232        */
1233       seq.getFeatures().shiftFeatures(start + 1, length);
1234
1235       /*
1236        * shift right any features that start at the cut position,
1237        * unless they were truncated
1238        */
1239       List<SequenceFeature> sfs = seq.getFeatures().findFeatures(start,
1240               start);
1241       for (SequenceFeature sf : sfs)
1242       {
1243         if (sf.getBegin() == start)
1244         {
1245           if (!command.truncatedFeatures.containsKey(seq)
1246                   || !command.truncatedFeatures.get(seq).contains(sf))
1247           {
1248             /*
1249              * feature was shifted left to cut position (not truncated),
1250              * so shift it back right
1251              */
1252             SequenceFeature shifted = new SequenceFeature(sf, sf.getBegin()
1253                     + length, sf.getEnd() + length, sf.getFeatureGroup(),
1254                     sf.getScore());
1255             seq.addSequenceFeature(shifted);
1256             seq.deleteFeature(sf);
1257           }
1258         }
1259       }
1260     }
1261
1262     /*
1263      * restore any features that were deleted or truncated
1264      */
1265     if (command.deletedFeatures != null
1266             && command.deletedFeatures.containsKey(seq))
1267     {
1268       for (SequenceFeature deleted : command.deletedFeatures.get(seq))
1269       {
1270         sequence.addSequenceFeature(deleted);
1271       }
1272     }
1273
1274     /*
1275      * delete any truncated features
1276      */
1277     if (command.truncatedFeatures != null
1278             && command.truncatedFeatures.containsKey(seq))
1279     {
1280       for (SequenceFeature amended : command.truncatedFeatures.get(seq))
1281       {
1282         sequence.deleteFeature(amended);
1283       }
1284     }
1285   }
1286
1287   /**
1288    * Returns the list of edit commands wrapped by this object.
1289    * 
1290    * @return
1291    */
1292   public List<Edit> getEdits()
1293   {
1294     return this.edits;
1295   }
1296
1297   /**
1298    * Returns a map whose keys are the dataset sequences, and values their
1299    * aligned sequences before the command edit list was applied. The aligned
1300    * sequences are copies, which may be updated without affecting the originals.
1301    * 
1302    * The command holds references to the aligned sequences (after editing). If
1303    * the command is an 'undo',then the prior state is simply the aligned state.
1304    * Otherwise, we have to derive the prior state by working backwards through
1305    * the edit list to infer the aligned sequences before editing.
1306    * 
1307    * Note: an alternative solution would be to cache the 'before' state of each
1308    * edit, but this would be expensive in space in the common case that the
1309    * original is never needed (edits are not mirrored).
1310    * 
1311    * @return
1312    * @throws IllegalStateException
1313    *           on detecting an edit command of a type that can't be unwound
1314    */
1315   public Map<SequenceI, SequenceI> priorState(boolean forUndo)
1316   {
1317     Map<SequenceI, SequenceI> result = new HashMap<SequenceI, SequenceI>();
1318     if (getEdits() == null)
1319     {
1320       return result;
1321     }
1322     if (forUndo)
1323     {
1324       for (Edit e : getEdits())
1325       {
1326         for (SequenceI seq : e.getSequences())
1327         {
1328           SequenceI ds = seq.getDatasetSequence();
1329           // SequenceI preEdit = result.get(ds);
1330           if (!result.containsKey(ds))
1331           {
1332             /*
1333              * copy sequence including start/end (but don't use copy constructor
1334              * as we don't need annotations)
1335              */
1336             SequenceI preEdit = new Sequence("", seq.getSequenceAsString(),
1337                     seq.getStart(), seq.getEnd());
1338             preEdit.setDatasetSequence(ds);
1339             result.put(ds, preEdit);
1340           }
1341         }
1342       }
1343       return result;
1344     }
1345
1346     /*
1347      * Work backwards through the edit list, deriving the sequences before each
1348      * was applied. The final result is the sequence set before any edits.
1349      */
1350     Iterator<Edit> editList = new ReverseListIterator<Edit>(getEdits());
1351     while (editList.hasNext())
1352     {
1353       Edit oldEdit = editList.next();
1354       Action action = oldEdit.getAction();
1355       int position = oldEdit.getPosition();
1356       int number = oldEdit.getNumber();
1357       final char gap = oldEdit.getGapCharacter();
1358       for (SequenceI seq : oldEdit.getSequences())
1359       {
1360         SequenceI ds = seq.getDatasetSequence();
1361         SequenceI preEdit = result.get(ds);
1362         if (preEdit == null)
1363         {
1364           preEdit = new Sequence("", seq.getSequenceAsString(),
1365                   seq.getStart(), seq.getEnd());
1366           preEdit.setDatasetSequence(ds);
1367           result.put(ds, preEdit);
1368         }
1369         /*
1370          * 'Undo' this edit action on the sequence (updating the value in the
1371          * map).
1372          */
1373         if (ds != null)
1374         {
1375           if (action == Action.DELETE_GAP)
1376           {
1377             preEdit.setSequence(new String(StringUtils.insertCharAt(
1378                     preEdit.getSequence(), position, number, gap)));
1379           }
1380           else if (action == Action.INSERT_GAP)
1381           {
1382             preEdit.setSequence(new String(StringUtils.deleteChars(
1383                     preEdit.getSequence(), position, position + number)));
1384           }
1385           else
1386           {
1387             System.err.println("Can't undo edit action " + action);
1388             // throw new IllegalStateException("Can't undo edit action " +
1389             // action);
1390           }
1391         }
1392       }
1393     }
1394     return result;
1395   }
1396
1397   public class Edit
1398   {
1399     public SequenceI[] oldds;
1400
1401     /**
1402      * start and end of sequence prior to edit
1403      */
1404     public Range[] oldStartEnd;
1405
1406     boolean fullAlignmentHeight = false;
1407
1408     Map<SequenceI, AlignmentAnnotation[]> deletedAnnotationRows;
1409
1410     Map<String, Annotation[]> deletedAnnotations;
1411
1412     /*
1413      * features deleted by the cut (re-add on Undo)
1414      * (including the original of any shortened features)
1415      */
1416     Map<SequenceI, List<SequenceFeature>> deletedFeatures;
1417
1418     /*
1419      * shortened features added by the cut (delete on Undo)
1420      */
1421     Map<SequenceI, List<SequenceFeature>> truncatedFeatures;
1422
1423     AlignmentI al;
1424
1425     Action command;
1426
1427     char[][] string;
1428
1429     SequenceI[] seqs;
1430
1431     int[] alIndex;
1432
1433     int position, number;
1434
1435     char gapChar;
1436
1437     public Edit(Action cmd, SequenceI[] sqs, int pos, int count,
1438             char gap)
1439     {
1440       this.command = cmd;
1441       this.seqs = sqs;
1442       this.position = pos;
1443       this.number = count;
1444       this.gapChar = gap;
1445     }
1446
1447     Edit(Action cmd, SequenceI[] sqs, int pos, int count,
1448             AlignmentI align)
1449     {
1450       this(cmd, sqs, pos, count, align.getGapCharacter());
1451
1452       this.al = align;
1453
1454       alIndex = new int[sqs.length];
1455       for (int i = 0; i < sqs.length; i++)
1456       {
1457         alIndex[i] = align.findIndex(sqs[i]);
1458       }
1459
1460       fullAlignmentHeight = (align.getHeight() == sqs.length);
1461     }
1462
1463     Edit(Action cmd, SequenceI[] sqs, int pos, int count,
1464             AlignmentI align, String replace)
1465     {
1466       this(cmd, sqs, pos, count, align);
1467
1468       string = new char[sqs.length][];
1469       for (int i = 0; i < sqs.length; i++)
1470       {
1471         string[i] = replace.toCharArray();
1472       }
1473     }
1474
1475     public SequenceI[] getSequences()
1476     {
1477       return seqs;
1478     }
1479
1480     public int getPosition()
1481     {
1482       return position;
1483     }
1484
1485     public Action getAction()
1486     {
1487       return command;
1488     }
1489
1490     public int getNumber()
1491     {
1492       return number;
1493     }
1494
1495     public char getGapCharacter()
1496     {
1497       return gapChar;
1498     }
1499   }
1500
1501   /**
1502    * Returns an iterator over the list of edit commands which traverses the list
1503    * either forwards or backwards.
1504    * 
1505    * @param forwards
1506    * @return
1507    */
1508   public Iterator<Edit> getEditIterator(boolean forwards)
1509   {
1510     if (forwards)
1511     {
1512       return getEdits().iterator();
1513     }
1514     else
1515     {
1516       return new ReverseListIterator<Edit>(getEdits());
1517     }
1518   }
1519
1520   /**
1521    * Adjusts features for Cut, and saves details of changes made to allow Undo
1522    * <ul>
1523    * <li>features left of the cut are unchanged</li>
1524    * <li>features right of the cut are shifted left</li>
1525    * <li>features internal to the cut region are deleted</li>
1526    * <li>features that overlap or span the cut are shortened</li>
1527    * <li>the originals of any deleted or shorted features are saved, to re-add
1528    * on Undo</li>
1529    * <li>any added (shortened) features are saved, to delete on Undo</li>
1530    * </ul>
1531    * 
1532    * @param command
1533    * @param seq
1534    * @param fromPosition
1535    * @param toPosition
1536    * @param cutIsInternal
1537    */
1538   protected static void cutFeatures(Edit command, SequenceI seq,
1539           int fromPosition, int toPosition, boolean cutIsInternal)
1540   {
1541     /* if the cut is at start or end of sequence
1542      * then we don't modify the seuqence feature store
1543      */
1544     if (!cutIsInternal)
1545     {
1546       return;
1547     }
1548     List<SequenceFeature> added = new ArrayList<>();
1549     List<SequenceFeature> removed = new ArrayList<>();
1550   
1551     SequenceFeaturesI featureStore = seq.getFeatures();
1552     if (toPosition < fromPosition || featureStore == null)
1553     {
1554       return;
1555     }
1556   
1557     int cutStartPos = fromPosition;
1558     int cutEndPos = toPosition;
1559     int cutWidth = cutEndPos - cutStartPos + 1;
1560   
1561     synchronized (featureStore)
1562     {
1563       /*
1564        * get features that overlap the cut region
1565        */
1566       List<SequenceFeature> toAmend = featureStore.findFeatures(
1567               cutStartPos, cutEndPos);
1568   
1569       /*
1570        * add any contact features that span the cut region
1571        * (not returned by findFeatures)
1572        */
1573       for (SequenceFeature contact : featureStore.getContactFeatures())
1574       {
1575         if (contact.getBegin() < cutStartPos
1576                 && contact.getEnd() > cutEndPos)
1577         {
1578           toAmend.add(contact);
1579         }
1580       }
1581
1582       /*
1583        * adjust start-end of overlapping features;
1584        * delete features enclosed by the cut;
1585        * delete partially overlapping contact features
1586        */
1587       for (SequenceFeature sf : toAmend)
1588       {
1589         int sfBegin = sf.getBegin();
1590         int sfEnd = sf.getEnd();
1591         int newBegin = sfBegin;
1592         int newEnd = sfEnd;
1593         boolean toDelete = false;
1594         boolean follows = false;
1595         
1596         if (sfBegin >= cutStartPos && sfEnd <= cutEndPos)
1597         {
1598           /*
1599            * feature lies within cut region - delete it
1600            */
1601           toDelete = true;
1602         }
1603         else if (sfBegin < cutStartPos && sfEnd > cutEndPos)
1604         {
1605           /*
1606            * feature spans cut region - left-shift the end
1607            */
1608           newEnd -= cutWidth;
1609         }
1610         else if (sfEnd <= cutEndPos)
1611         {
1612           /*
1613            * feature overlaps left of cut region - truncate right
1614            */
1615           newEnd = cutStartPos - 1;
1616           if (sf.isContactFeature())
1617           {
1618             toDelete = true;
1619           }
1620         }
1621         else if (sfBegin >= cutStartPos)
1622         {
1623           /*
1624            * remaining case - feature overlaps right
1625            * truncate left, adjust end of feature
1626            */
1627           newBegin = cutIsInternal ? cutStartPos : cutEndPos + 1;
1628           newEnd = newBegin + sfEnd - cutEndPos - 1;
1629           if (sf.isContactFeature())
1630           {
1631             toDelete = true;
1632           }
1633         }
1634   
1635         seq.deleteFeature(sf);
1636         if (!follows)
1637         {
1638           removed.add(sf);
1639         }
1640         if (!toDelete)
1641         {
1642           SequenceFeature copy = new SequenceFeature(sf, newBegin, newEnd,
1643                   sf.getFeatureGroup(), sf.getScore());
1644           seq.addSequenceFeature(copy);
1645           if (!follows)
1646           {
1647             added.add(copy);
1648           }
1649         }
1650       }
1651   
1652       /*
1653        * and left shift any features lying to the right of the cut region
1654        */
1655
1656       featureStore.shiftFeatures(cutEndPos + 1, -cutWidth);
1657     }
1658
1659     /*
1660      * save deleted and amended features, so that Undo can 
1661      * re-add or delete them respectively
1662      */
1663     if (command.deletedFeatures == null)
1664     {
1665       command.deletedFeatures = new HashMap<>();
1666     }
1667     if (command.truncatedFeatures == null)
1668     {
1669       command.truncatedFeatures = new HashMap<>();
1670     }
1671     command.deletedFeatures.put(seq, removed);
1672     command.truncatedFeatures.put(seq, added);
1673   }
1674 }