b62c61c9cffe688064c0fce22f56256aafadf286
[jalview.git] / src / jalview / viewmodel / AlignmentViewport.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.viewmodel;
22
23 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
24 import jalview.analysis.Conservation;
25 import jalview.api.AlignCalcManagerI;
26 import jalview.api.AlignViewportI;
27 import jalview.api.AlignmentViewPanel;
28 import jalview.api.FeaturesDisplayedI;
29 import jalview.api.ViewStyleI;
30 import jalview.commands.CommandI;
31 import jalview.datamodel.AlignedCodonFrame;
32 import jalview.datamodel.AlignmentAnnotation;
33 import jalview.datamodel.AlignmentI;
34 import jalview.datamodel.AlignmentView;
35 import jalview.datamodel.Annotation;
36 import jalview.datamodel.CigarArray;
37 import jalview.datamodel.ColumnSelection;
38 import jalview.datamodel.HiddenSequences;
39 import jalview.datamodel.ProfilesI;
40 import jalview.datamodel.SearchResultsI;
41 import jalview.datamodel.Sequence;
42 import jalview.datamodel.SequenceCollectionI;
43 import jalview.datamodel.SequenceGroup;
44 import jalview.datamodel.SequenceI;
45 import jalview.renderer.ResidueShader;
46 import jalview.renderer.ResidueShaderI;
47 import jalview.schemes.ColourSchemeI;
48 import jalview.structure.CommandListener;
49 import jalview.structure.StructureSelectionManager;
50 import jalview.structure.VamsasSource;
51 import jalview.util.Comparison;
52 import jalview.util.MapList;
53 import jalview.util.MappingUtils;
54 import jalview.viewmodel.styles.ViewStyle;
55 import jalview.workers.AlignCalcManager;
56 import jalview.workers.ComplementConsensusThread;
57 import jalview.workers.ConsensusThread;
58 import jalview.workers.StrucConsensusThread;
59
60 import java.awt.Color;
61 import java.beans.PropertyChangeSupport;
62 import java.util.ArrayDeque;
63 import java.util.ArrayList;
64 import java.util.BitSet;
65 import java.util.Deque;
66 import java.util.HashMap;
67 import java.util.Hashtable;
68 import java.util.List;
69 import java.util.Map;
70
71 /**
72  * base class holding visualization and analysis attributes and common logic for
73  * an active alignment view displayed in the GUI
74  * 
75  * @author jimp
76  * 
77  */
78 public abstract class AlignmentViewport implements AlignViewportI,
79         CommandListener, VamsasSource
80 {
81   protected ViewportPositionProps posProps;
82
83   protected ViewStyleI viewStyle = new ViewStyle();
84
85   /**
86    * A viewport that hosts the cDna view of this (protein), or vice versa (if
87    * set).
88    */
89   AlignViewportI codingComplement = null;
90
91   FeaturesDisplayedI featuresDisplayed = null;
92
93   protected Deque<CommandI> historyList = new ArrayDeque<CommandI>();
94
95   protected Deque<CommandI> redoList = new ArrayDeque<CommandI>();
96
97   /**
98    * @param name
99    * @see jalview.api.ViewStyleI#setFontName(java.lang.String)
100    */
101   @Override
102   public void setFontName(String name)
103   {
104     viewStyle.setFontName(name);
105   }
106
107   /**
108    * @param style
109    * @see jalview.api.ViewStyleI#setFontStyle(int)
110    */
111   @Override
112   public void setFontStyle(int style)
113   {
114     viewStyle.setFontStyle(style);
115   }
116
117   /**
118    * @param size
119    * @see jalview.api.ViewStyleI#setFontSize(int)
120    */
121   @Override
122   public void setFontSize(int size)
123   {
124     viewStyle.setFontSize(size);
125   }
126
127   /**
128    * @return
129    * @see jalview.api.ViewStyleI#getFontStyle()
130    */
131   @Override
132   public int getFontStyle()
133   {
134     return viewStyle.getFontStyle();
135   }
136
137   /**
138    * @return
139    * @see jalview.api.ViewStyleI#getFontName()
140    */
141   @Override
142   public String getFontName()
143   {
144     return viewStyle.getFontName();
145   }
146
147   /**
148    * @return
149    * @see jalview.api.ViewStyleI#getFontSize()
150    */
151   @Override
152   public int getFontSize()
153   {
154     return viewStyle.getFontSize();
155   }
156
157   /**
158    * @param upperCasebold
159    * @see jalview.api.ViewStyleI#setUpperCasebold(boolean)
160    */
161   @Override
162   public void setUpperCasebold(boolean upperCasebold)
163   {
164     viewStyle.setUpperCasebold(upperCasebold);
165   }
166
167   /**
168    * @return
169    * @see jalview.api.ViewStyleI#isUpperCasebold()
170    */
171   @Override
172   public boolean isUpperCasebold()
173   {
174     return viewStyle.isUpperCasebold();
175   }
176
177   /**
178    * @return
179    * @see jalview.api.ViewStyleI#isSeqNameItalics()
180    */
181   @Override
182   public boolean isSeqNameItalics()
183   {
184     return viewStyle.isSeqNameItalics();
185   }
186
187   /**
188    * @param colourByReferenceSeq
189    * @see jalview.api.ViewStyleI#setColourByReferenceSeq(boolean)
190    */
191   @Override
192   public void setColourByReferenceSeq(boolean colourByReferenceSeq)
193   {
194     viewStyle.setColourByReferenceSeq(colourByReferenceSeq);
195   }
196
197   /**
198    * @param b
199    * @see jalview.api.ViewStyleI#setColourAppliesToAllGroups(boolean)
200    */
201   @Override
202   public void setColourAppliesToAllGroups(boolean b)
203   {
204     viewStyle.setColourAppliesToAllGroups(b);
205   }
206
207   /**
208    * @return
209    * @see jalview.api.ViewStyleI#getColourAppliesToAllGroups()
210    */
211   @Override
212   public boolean getColourAppliesToAllGroups()
213   {
214     return viewStyle.getColourAppliesToAllGroups();
215   }
216
217   /**
218    * @return
219    * @see jalview.api.ViewStyleI#getAbovePIDThreshold()
220    */
221   @Override
222   public boolean getAbovePIDThreshold()
223   {
224     return viewStyle.getAbovePIDThreshold();
225   }
226
227   /**
228    * @param inc
229    * @see jalview.api.ViewStyleI#setIncrement(int)
230    */
231   @Override
232   public void setIncrement(int inc)
233   {
234     viewStyle.setIncrement(inc);
235   }
236
237   /**
238    * @return
239    * @see jalview.api.ViewStyleI#getIncrement()
240    */
241   @Override
242   public int getIncrement()
243   {
244     return viewStyle.getIncrement();
245   }
246
247   /**
248    * @param b
249    * @see jalview.api.ViewStyleI#setConservationSelected(boolean)
250    */
251   @Override
252   public void setConservationSelected(boolean b)
253   {
254     viewStyle.setConservationSelected(b);
255   }
256
257   /**
258    * @param show
259    * @see jalview.api.ViewStyleI#setShowHiddenMarkers(boolean)
260    */
261   @Override
262   public void setShowHiddenMarkers(boolean show)
263   {
264     viewStyle.setShowHiddenMarkers(show);
265   }
266
267   /**
268    * @return
269    * @see jalview.api.ViewStyleI#getShowHiddenMarkers()
270    */
271   @Override
272   public boolean getShowHiddenMarkers()
273   {
274     return viewStyle.getShowHiddenMarkers();
275   }
276
277   /**
278    * @param b
279    * @see jalview.api.ViewStyleI#setScaleRightWrapped(boolean)
280    */
281   @Override
282   public void setScaleRightWrapped(boolean b)
283   {
284     viewStyle.setScaleRightWrapped(b);
285   }
286
287   /**
288    * @param b
289    * @see jalview.api.ViewStyleI#setScaleLeftWrapped(boolean)
290    */
291   @Override
292   public void setScaleLeftWrapped(boolean b)
293   {
294     viewStyle.setScaleLeftWrapped(b);
295   }
296
297   /**
298    * @param b
299    * @see jalview.api.ViewStyleI#setScaleAboveWrapped(boolean)
300    */
301   @Override
302   public void setScaleAboveWrapped(boolean b)
303   {
304     viewStyle.setScaleAboveWrapped(b);
305   }
306
307   /**
308    * @return
309    * @see jalview.api.ViewStyleI#getScaleLeftWrapped()
310    */
311   @Override
312   public boolean getScaleLeftWrapped()
313   {
314     return viewStyle.getScaleLeftWrapped();
315   }
316
317   /**
318    * @return
319    * @see jalview.api.ViewStyleI#getScaleAboveWrapped()
320    */
321   @Override
322   public boolean getScaleAboveWrapped()
323   {
324     return viewStyle.getScaleAboveWrapped();
325   }
326
327   /**
328    * @return
329    * @see jalview.api.ViewStyleI#getScaleRightWrapped()
330    */
331   @Override
332   public boolean getScaleRightWrapped()
333   {
334     return viewStyle.getScaleRightWrapped();
335   }
336
337   /**
338    * @param b
339    * @see jalview.api.ViewStyleI#setAbovePIDThreshold(boolean)
340    */
341   @Override
342   public void setAbovePIDThreshold(boolean b)
343   {
344     viewStyle.setAbovePIDThreshold(b);
345   }
346
347   /**
348    * @param thresh
349    * @see jalview.api.ViewStyleI#setThreshold(int)
350    */
351   @Override
352   public void setThreshold(int thresh)
353   {
354     viewStyle.setThreshold(thresh);
355   }
356
357   /**
358    * @return
359    * @see jalview.api.ViewStyleI#getThreshold()
360    */
361   @Override
362   public int getThreshold()
363   {
364     return viewStyle.getThreshold();
365   }
366
367   /**
368    * @return
369    * @see jalview.api.ViewStyleI#getShowJVSuffix()
370    */
371   @Override
372   public boolean getShowJVSuffix()
373   {
374     return viewStyle.getShowJVSuffix();
375   }
376
377   /**
378    * @param b
379    * @see jalview.api.ViewStyleI#setShowJVSuffix(boolean)
380    */
381   @Override
382   public void setShowJVSuffix(boolean b)
383   {
384     viewStyle.setShowJVSuffix(b);
385   }
386
387   /**
388    * @param state
389    * @see jalview.api.ViewStyleI#setWrapAlignment(boolean)
390    */
391   @Override
392   public void setWrapAlignment(boolean state)
393   {
394     viewStyle.setWrapAlignment(state);
395   }
396
397   /**
398    * @param state
399    * @see jalview.api.ViewStyleI#setShowText(boolean)
400    */
401   @Override
402   public void setShowText(boolean state)
403   {
404     viewStyle.setShowText(state);
405   }
406
407   /**
408    * @param state
409    * @see jalview.api.ViewStyleI#setRenderGaps(boolean)
410    */
411   @Override
412   public void setRenderGaps(boolean state)
413   {
414     viewStyle.setRenderGaps(state);
415   }
416
417   /**
418    * @return
419    * @see jalview.api.ViewStyleI#getColourText()
420    */
421   @Override
422   public boolean getColourText()
423   {
424     return viewStyle.getColourText();
425   }
426
427   /**
428    * @param state
429    * @see jalview.api.ViewStyleI#setColourText(boolean)
430    */
431   @Override
432   public void setColourText(boolean state)
433   {
434     viewStyle.setColourText(state);
435   }
436
437   /**
438    * @return
439    * @see jalview.api.ViewStyleI#getWrapAlignment()
440    */
441   @Override
442   public boolean getWrapAlignment()
443   {
444     return viewStyle.getWrapAlignment();
445   }
446
447   /**
448    * @return
449    * @see jalview.api.ViewStyleI#getShowText()
450    */
451   @Override
452   public boolean getShowText()
453   {
454     return viewStyle.getShowText();
455   }
456
457   /**
458    * @return
459    * @see jalview.api.ViewStyleI#getWrappedWidth()
460    */
461   @Override
462   public int getWrappedWidth()
463   {
464     return viewStyle.getWrappedWidth();
465   }
466
467   /**
468    * @param w
469    * @see jalview.api.ViewStyleI#setWrappedWidth(int)
470    */
471   @Override
472   public void setWrappedWidth(int w)
473   {
474     viewStyle.setWrappedWidth(w);
475   }
476
477   /**
478    * @return
479    * @see jalview.api.ViewStyleI#getCharHeight()
480    */
481   @Override
482   public int getCharHeight()
483   {
484     return viewStyle.getCharHeight();
485   }
486
487   /**
488    * @param h
489    * @see jalview.api.ViewStyleI#setCharHeight(int)
490    */
491   @Override
492   public void setCharHeight(int h)
493   {
494     viewStyle.setCharHeight(h);
495   }
496
497   /**
498    * @return
499    * @see jalview.api.ViewStyleI#getCharWidth()
500    */
501   @Override
502   public int getCharWidth()
503   {
504     return viewStyle.getCharWidth();
505   }
506
507   /**
508    * @param w
509    * @see jalview.api.ViewStyleI#setCharWidth(int)
510    */
511   @Override
512   public void setCharWidth(int w)
513   {
514     viewStyle.setCharWidth(w);
515   }
516
517   /**
518    * @return
519    * @see jalview.api.ViewStyleI#getShowBoxes()
520    */
521   @Override
522   public boolean getShowBoxes()
523   {
524     return viewStyle.getShowBoxes();
525   }
526
527   /**
528    * @return
529    * @see jalview.api.ViewStyleI#getShowUnconserved()
530    */
531   @Override
532   public boolean getShowUnconserved()
533   {
534     return viewStyle.getShowUnconserved();
535   }
536
537   /**
538    * @param showunconserved
539    * @see jalview.api.ViewStyleI#setShowUnconserved(boolean)
540    */
541   @Override
542   public void setShowUnconserved(boolean showunconserved)
543   {
544     viewStyle.setShowUnconserved(showunconserved);
545   }
546
547   /**
548    * @param default1
549    * @see jalview.api.ViewStyleI#setSeqNameItalics(boolean)
550    */
551   @Override
552   public void setSeqNameItalics(boolean default1)
553   {
554     viewStyle.setSeqNameItalics(default1);
555   }
556
557   /**
558    * alignment displayed in the viewport. Please use get/setter
559    */
560   protected AlignmentI alignment;
561
562   @Override
563   public AlignmentI getAlignment()
564   {
565     return alignment;
566   }
567
568   @Override
569   public char getGapCharacter()
570   {
571     return alignment.getGapCharacter();
572   }
573
574   protected String sequenceSetID;
575
576   /**
577    * probably unused indicator that view is of a dataset rather than an
578    * alignment
579    */
580   protected boolean isDataset = false;
581
582   public void setDataset(boolean b)
583   {
584     isDataset = b;
585   }
586
587   public boolean isDataset()
588   {
589     return isDataset;
590   }
591
592
593
594   // protected ColumnSelection colSel = new ColumnSelection();
595
596   public boolean autoCalculateConsensus = true;
597
598   protected boolean autoCalculateStrucConsensus = true;
599
600   protected boolean ignoreGapsInConsensusCalculation = false;
601
602   protected ResidueShaderI residueShading;
603
604   @Override
605   public void setGlobalColourScheme(ColourSchemeI cs)
606   {
607     // TODO: logic refactored from AlignFrame changeColour -
608     // TODO: autorecalc stuff should be changed to rely on the worker system
609     // check to see if we should implement a changeColour(cs) method rather than
610     // put the logic in here
611     // - means that caller decides if they want to just modify state and defer
612     // calculation till later or to do all calculations in thread.
613     // via changecolour
614
615     /*
616      * only instantiate alignment colouring once, thereafter update it;
617      * this means that any conservation or PID threshold settings
618      * persist when the alignment colour scheme is changed
619      */
620     if (residueShading == null)
621     {
622       residueShading = new ResidueShader(viewStyle);
623     }
624     residueShading.setColourScheme(cs);
625
626     // TODO: do threshold and increment belong in ViewStyle or ResidueShader?
627     // ...problem: groups need these, but do not currently have a ViewStyle
628
629     if (cs != null)
630     {
631       if (getConservationSelected())
632       {
633         residueShading.setConservation(hconservation);
634       }
635       residueShading.alignmentChanged(alignment, getHiddenRepSequences());
636     }
637
638     /*
639      * if 'apply colour to all groups' is selected... do so
640      * (but don't transfer any colour threshold settings to groups)
641      */
642     if (getColourAppliesToAllGroups())
643     {
644       for (SequenceGroup sg : getAlignment().getGroups())
645       {
646         /*
647          * retain any colour thresholds per group while
648          * changing choice of colour scheme (JAL-2386)
649          */
650         sg.setColourScheme(cs);
651         if (cs != null)
652         {
653           sg.getGroupColourScheme()
654 .alignmentChanged(sg,
655                   getHiddenRepSequences());
656         }
657       }
658     }
659   }
660
661   @Override
662   public ColourSchemeI getGlobalColourScheme()
663   {
664     return residueShading == null ? null : residueShading
665             .getColourScheme();
666   }
667
668   @Override
669   public ResidueShaderI getResidueShading()
670   {
671     return residueShading;
672   }
673
674   protected AlignmentAnnotation consensus;
675
676   protected AlignmentAnnotation complementConsensus;
677
678   protected AlignmentAnnotation strucConsensus;
679
680   protected AlignmentAnnotation conservation;
681
682   protected AlignmentAnnotation quality;
683
684   protected AlignmentAnnotation[] groupConsensus;
685
686   protected AlignmentAnnotation[] groupConservation;
687
688   /**
689    * results of alignment consensus analysis for visible portion of view
690    */
691   protected ProfilesI hconsensus = null;
692
693   /**
694    * results of cDNA complement consensus visible portion of view
695    */
696   protected Hashtable[] hcomplementConsensus = null;
697
698   /**
699    * results of secondary structure base pair consensus for visible portion of
700    * view
701    */
702   protected Hashtable[] hStrucConsensus = null;
703
704   protected Conservation hconservation = null;
705
706   @Override
707   public void setConservation(Conservation cons)
708   {
709     hconservation = cons;
710   }
711
712   /**
713    * percentage gaps allowed in a column before all amino acid properties should
714    * be considered unconserved
715    */
716   int ConsPercGaps = 25; // JBPNote : This should be a scalable property!
717
718   @Override
719   public int getConsPercGaps()
720   {
721     return ConsPercGaps;
722   }
723
724   @Override
725   public void setSequenceConsensusHash(ProfilesI hconsensus)
726   {
727     this.hconsensus = hconsensus;
728   }
729
730   @Override
731   public void setComplementConsensusHash(Hashtable[] hconsensus)
732   {
733     this.hcomplementConsensus = hconsensus;
734   }
735
736   @Override
737   public ProfilesI getSequenceConsensusHash()
738   {
739     return hconsensus;
740   }
741
742   @Override
743   public Hashtable[] getComplementConsensusHash()
744   {
745     return hcomplementConsensus;
746   }
747
748   @Override
749   public Hashtable[] getRnaStructureConsensusHash()
750   {
751     return hStrucConsensus;
752   }
753
754   @Override
755   public void setRnaStructureConsensusHash(Hashtable[] hStrucConsensus)
756   {
757     this.hStrucConsensus = hStrucConsensus;
758
759   }
760
761   @Override
762   public AlignmentAnnotation getAlignmentQualityAnnot()
763   {
764     return quality;
765   }
766
767   @Override
768   public AlignmentAnnotation getAlignmentConservationAnnotation()
769   {
770     return conservation;
771   }
772
773   @Override
774   public AlignmentAnnotation getAlignmentConsensusAnnotation()
775   {
776     return consensus;
777   }
778
779   @Override
780   public AlignmentAnnotation getComplementConsensusAnnotation()
781   {
782     return complementConsensus;
783   }
784
785   @Override
786   public AlignmentAnnotation getAlignmentStrucConsensusAnnotation()
787   {
788     return strucConsensus;
789   }
790
791   protected AlignCalcManagerI calculator = new AlignCalcManager();
792
793   /**
794    * trigger update of conservation annotation
795    */
796   public void updateConservation(final AlignmentViewPanel ap)
797   {
798     // see note in mantis : issue number 8585
799     if (alignment.isNucleotide()
800             || (conservation == null && quality == null)
801             || !autoCalculateConsensus)
802     {
803       return;
804     }
805     if (calculator
806             .getRegisteredWorkersOfClass(jalview.workers.ConservationThread.class) == null)
807     {
808       calculator.registerWorker(new jalview.workers.ConservationThread(
809               this, ap));
810     }
811   }
812
813   /**
814    * trigger update of consensus annotation
815    */
816   public void updateConsensus(final AlignmentViewPanel ap)
817   {
818     // see note in mantis : issue number 8585
819     if (consensus == null || !autoCalculateConsensus)
820     {
821       return;
822     }
823     if (calculator.getRegisteredWorkersOfClass(ConsensusThread.class) == null)
824     {
825       calculator.registerWorker(new ConsensusThread(this, ap));
826     }
827
828     /*
829      * A separate thread to compute cDNA consensus for a protein alignment
830      * which has mapping to cDNA
831      */
832     final AlignmentI al = this.getAlignment();
833     if (!al.isNucleotide() && al.getCodonFrames() != null
834             && !al.getCodonFrames().isEmpty())
835     {
836       /*
837        * fudge - check first for protein-to-nucleotide mappings
838        * (we don't want to do this for protein-to-protein)
839        */
840       boolean doConsensus = false;
841       for (AlignedCodonFrame mapping : al.getCodonFrames())
842       {
843         // TODO hold mapping type e.g. dna-to-protein in AlignedCodonFrame?
844         MapList[] mapLists = mapping.getdnaToProt();
845         // mapLists can be empty if project load has not finished resolving seqs
846         if (mapLists.length > 0 && mapLists[0].getFromRatio() == 3)
847         {
848           doConsensus = true;
849           break;
850         }
851       }
852       if (doConsensus)
853       {
854         if (calculator
855                 .getRegisteredWorkersOfClass(ComplementConsensusThread.class) == null)
856         {
857           calculator
858                   .registerWorker(new ComplementConsensusThread(this, ap));
859         }
860       }
861     }
862   }
863
864   // --------START Structure Conservation
865   public void updateStrucConsensus(final AlignmentViewPanel ap)
866   {
867     if (autoCalculateStrucConsensus && strucConsensus == null
868             && alignment.isNucleotide() && alignment.hasRNAStructure())
869     {
870       // secondary structure has been added - so init the consensus line
871       initRNAStructure();
872     }
873
874     // see note in mantis : issue number 8585
875     if (strucConsensus == null || !autoCalculateStrucConsensus)
876     {
877       return;
878     }
879     if (calculator.getRegisteredWorkersOfClass(StrucConsensusThread.class) == null)
880     {
881       calculator.registerWorker(new StrucConsensusThread(this, ap));
882     }
883   }
884
885   public boolean isCalcInProgress()
886   {
887     return calculator.isWorking();
888   }
889
890   @Override
891   public boolean isCalculationInProgress(
892           AlignmentAnnotation alignmentAnnotation)
893   {
894     if (!alignmentAnnotation.autoCalculated)
895     {
896       return false;
897     }
898     if (calculator.workingInvolvedWith(alignmentAnnotation))
899     {
900       // System.err.println("grey out ("+alignmentAnnotation.label+")");
901       return true;
902     }
903     return false;
904   }
905
906   public void setAlignment(AlignmentI align)
907   {
908     this.alignment = align;
909   }
910
911   /**
912    * Clean up references when this viewport is closed
913    */
914   @Override
915   public void dispose()
916   {
917     /*
918      * defensively null out references to large objects in case
919      * this object is not garbage collected (as if!)
920      */
921     consensus = null;
922     complementConsensus = null;
923     strucConsensus = null;
924     conservation = null;
925     quality = null;
926     groupConsensus = null;
927     groupConservation = null;
928     hconsensus = null;
929     hcomplementConsensus = null;
930     // colour scheme may hold reference to consensus
931     residueShading = null;
932     // TODO remove listeners from changeSupport?
933     changeSupport = null;
934     setAlignment(null);
935   }
936
937   @Override
938   public boolean isClosed()
939   {
940     // TODO: check that this isClosed is only true after panel is closed, not
941     // before it is fully constructed.
942     return alignment == null;
943   }
944
945   @Override
946   public AlignCalcManagerI getCalcManager()
947   {
948     return calculator;
949   }
950
951   /**
952    * should conservation rows be shown for groups
953    */
954   protected boolean showGroupConservation = false;
955
956   /**
957    * should consensus rows be shown for groups
958    */
959   protected boolean showGroupConsensus = false;
960
961   /**
962    * should consensus profile be rendered by default
963    */
964   protected boolean showSequenceLogo = false;
965
966   /**
967    * should consensus profile be rendered normalised to row height
968    */
969   protected boolean normaliseSequenceLogo = false;
970
971   /**
972    * should consensus histograms be rendered by default
973    */
974   protected boolean showConsensusHistogram = true;
975
976   /**
977    * @return the showConsensusProfile
978    */
979   @Override
980   public boolean isShowSequenceLogo()
981   {
982     return showSequenceLogo;
983   }
984
985   /**
986    * @param showSequenceLogo
987    *          the new value
988    */
989   public void setShowSequenceLogo(boolean showSequenceLogo)
990   {
991     if (showSequenceLogo != this.showSequenceLogo)
992     {
993       // TODO: decouple settings setting from calculation when refactoring
994       // annotation update method from alignframe to viewport
995       this.showSequenceLogo = showSequenceLogo;
996       calculator.updateAnnotationFor(ConsensusThread.class);
997       calculator.updateAnnotationFor(ComplementConsensusThread.class);
998       calculator.updateAnnotationFor(StrucConsensusThread.class);
999     }
1000     this.showSequenceLogo = showSequenceLogo;
1001   }
1002
1003   /**
1004    * @param showConsensusHistogram
1005    *          the showConsensusHistogram to set
1006    */
1007   public void setShowConsensusHistogram(boolean showConsensusHistogram)
1008   {
1009     this.showConsensusHistogram = showConsensusHistogram;
1010   }
1011
1012   /**
1013    * @return the showGroupConservation
1014    */
1015   public boolean isShowGroupConservation()
1016   {
1017     return showGroupConservation;
1018   }
1019
1020   /**
1021    * @param showGroupConservation
1022    *          the showGroupConservation to set
1023    */
1024   public void setShowGroupConservation(boolean showGroupConservation)
1025   {
1026     this.showGroupConservation = showGroupConservation;
1027   }
1028
1029   /**
1030    * @return the showGroupConsensus
1031    */
1032   public boolean isShowGroupConsensus()
1033   {
1034     return showGroupConsensus;
1035   }
1036
1037   /**
1038    * @param showGroupConsensus
1039    *          the showGroupConsensus to set
1040    */
1041   public void setShowGroupConsensus(boolean showGroupConsensus)
1042   {
1043     this.showGroupConsensus = showGroupConsensus;
1044   }
1045
1046   /**
1047    * 
1048    * @return flag to indicate if the consensus histogram should be rendered by
1049    *         default
1050    */
1051   @Override
1052   public boolean isShowConsensusHistogram()
1053   {
1054     return this.showConsensusHistogram;
1055   }
1056
1057   /**
1058    * when set, updateAlignment will always ensure sequences are of equal length
1059    */
1060   private boolean padGaps = false;
1061
1062   /**
1063    * when set, alignment should be reordered according to a newly opened tree
1064    */
1065   public boolean sortByTree = false;
1066
1067   /**
1068    * 
1069    * 
1070    * @return null or the currently selected sequence region
1071    */
1072   @Override
1073   public SequenceGroup getSelectionGroup()
1074   {
1075     return selectionGroup;
1076   }
1077
1078   /**
1079    * Set the selection group for this window. Also sets the current alignment as
1080    * the context for the group, if it does not already have one.
1081    * 
1082    * @param sg
1083    *          - group holding references to sequences in this alignment view
1084    * 
1085    */
1086   @Override
1087   public void setSelectionGroup(SequenceGroup sg)
1088   {
1089     selectionGroup = sg;
1090     if (sg != null && sg.getContext() == null)
1091     {
1092       sg.setContext(alignment);
1093     }
1094   }
1095
1096   public void setHiddenColumns(ColumnSelection colsel)
1097   {
1098     posProps.setHiddenColumns(colsel);
1099     // this.colSel = colsel;
1100   }
1101
1102   @Override
1103   public ColumnSelection getColumnSelection()
1104   {
1105     return posProps.getColumnSelection();
1106     // return colSel;
1107   }
1108
1109   @Override
1110   public void setColumnSelection(ColumnSelection colSel)
1111   {
1112     posProps.setColumnSelection(colSel);
1113     /*this.colSel = colSel;
1114     if (colSel != null)
1115     {
1116       updateHiddenColumns();
1117     }
1118     isColSelChanged(true);*/
1119   }
1120
1121   /**
1122    * 
1123    * @return
1124    */
1125   @Override
1126   public Map<SequenceI, SequenceCollectionI> getHiddenRepSequences()
1127   {
1128     return posProps.getHiddenRepSequences();
1129   }
1130
1131   @Override
1132   public void setHiddenRepSequences(
1133           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
1134   {
1135     posProps.getHiddenRepSequences();
1136   }
1137
1138   @Override
1139   public boolean hasSelectedColumns()
1140   {
1141     ColumnSelection columnSelection = getColumnSelection();
1142     return columnSelection != null && columnSelection.hasSelectedColumns();
1143   }
1144
1145   @Override
1146   public boolean hasHiddenColumns()
1147   {
1148     return posProps.hasHiddenColumns();
1149     // return colSel != null && colSel.hasHiddenColumns();
1150   }
1151
1152   public void updateHiddenColumns()
1153   {
1154     // this method doesn't really do anything now. But - it could, since a
1155     // column Selection could be in the process of modification
1156     // hasHiddenColumns = colSel.hasHiddenColumns();
1157   }
1158
1159   @Override
1160   public boolean hasHiddenRows()
1161   {
1162     return alignment.getHiddenSequences().getSize() > 0;
1163   }
1164
1165   protected SequenceGroup selectionGroup;
1166
1167   public void setSequenceSetId(String newid)
1168   {
1169     if (sequenceSetID != null)
1170     {
1171       System.err
1172               .println("Warning - overwriting a sequenceSetId for a viewport!");
1173     }
1174     sequenceSetID = new String(newid);
1175   }
1176
1177   @Override
1178   public String getSequenceSetId()
1179   {
1180     if (sequenceSetID == null)
1181     {
1182       sequenceSetID = alignment.hashCode() + "";
1183     }
1184
1185     return sequenceSetID;
1186   }
1187
1188   /**
1189    * unique viewId for synchronizing state (e.g. with stored Jalview Project)
1190    * 
1191    */
1192   protected String viewId = null;
1193
1194   @Override
1195   public String getViewId()
1196   {
1197     if (viewId == null)
1198     {
1199       viewId = this.getSequenceSetId() + "." + this.hashCode() + "";
1200     }
1201     return viewId;
1202   }
1203
1204   public void setIgnoreGapsConsensus(boolean b, AlignmentViewPanel ap)
1205   {
1206     ignoreGapsInConsensusCalculation = b;
1207     if (ap != null)
1208     {
1209       updateConsensus(ap);
1210       if (residueShading != null)
1211       {
1212         residueShading.setThreshold(residueShading.getThreshold(),
1213                 ignoreGapsInConsensusCalculation);
1214       }
1215     }
1216
1217   }
1218
1219   private long sgrouphash = -1;
1220
1221   /**
1222    * checks current SelectionGroup against record of last hash value, and
1223    * updates record.
1224    * 
1225    * @param b
1226    *          update the record of last hash value
1227    * 
1228    * @return true if SelectionGroup changed since last call (when b is true)
1229    */
1230   public boolean isSelectionGroupChanged(boolean b)
1231   {
1232     int hc = (selectionGroup == null || selectionGroup.getSize() == 0) ? -1
1233             : selectionGroup.hashCode();
1234     if (hc != -1 && hc != sgrouphash)
1235     {
1236       if (b)
1237       {
1238         sgrouphash = hc;
1239       }
1240       return true;
1241     }
1242     return false;
1243   }
1244
1245   /**
1246    * checks current colsel against record of last hash value, and optionally
1247    * updates record.
1248    * 
1249    * @param b
1250    *          update the record of last hash value
1251    * @return true if colsel changed since last call (when b is true)
1252    */
1253   public boolean isColSelChanged(boolean b)
1254   {
1255     return posProps.isColSelChanged(b);
1256     /*int hc = (colSel == null || colSel.isEmpty()) ? -1 : colSel.hashCode();
1257     if (hc != -1 && hc != colselhash)
1258     {
1259       if (b)
1260       {
1261         colselhash = hc;
1262       }
1263       return true;
1264     }
1265     return false;*/
1266   }
1267
1268   @Override
1269   public boolean isIgnoreGapsConsensus()
1270   {
1271     return ignoreGapsInConsensusCalculation;
1272   }
1273
1274   // property change stuff
1275   // JBPNote Prolly only need this in the applet version.
1276   private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
1277           this);
1278
1279   protected boolean showConservation = true;
1280
1281   protected boolean showQuality = true;
1282
1283   protected boolean showConsensus = true;
1284
1285   private Map<SequenceI, Color> sequenceColours = new HashMap<SequenceI, Color>();
1286
1287   protected SequenceAnnotationOrder sortAnnotationsBy = null;
1288
1289   protected boolean showAutocalculatedAbove;
1290
1291   /**
1292    * when set, view will scroll to show the highlighted position
1293    */
1294   private boolean followHighlight = true;
1295
1296   /*private int startRes;
1297
1298   private int endRes;
1299
1300   private int startSeq;
1301
1302   private int endSeq;*/
1303
1304   /**
1305    * Property change listener for changes in alignment
1306    * 
1307    * @param listener
1308    *          DOCUMENT ME!
1309    */
1310   public void addPropertyChangeListener(
1311           java.beans.PropertyChangeListener listener)
1312   {
1313     changeSupport.addPropertyChangeListener(listener);
1314   }
1315
1316   /**
1317    * DOCUMENT ME!
1318    * 
1319    * @param listener
1320    *          DOCUMENT ME!
1321    */
1322   public void removePropertyChangeListener(
1323           java.beans.PropertyChangeListener listener)
1324   {
1325     changeSupport.removePropertyChangeListener(listener);
1326   }
1327
1328   /**
1329    * Property change listener for changes in alignment
1330    * 
1331    * @param prop
1332    *          DOCUMENT ME!
1333    * @param oldvalue
1334    *          DOCUMENT ME!
1335    * @param newvalue
1336    *          DOCUMENT ME!
1337    */
1338   public void firePropertyChange(String prop, Object oldvalue,
1339           Object newvalue)
1340   {
1341     changeSupport.firePropertyChange(prop, oldvalue, newvalue);
1342   }
1343
1344   // common hide/show column stuff
1345
1346   public void hideColumns(int start, int end)
1347   {
1348     posProps.hideColumns(start, end);
1349   }
1350
1351   public void showColumn(int col)
1352   {
1353     posProps.showColumn(col);
1354   }
1355
1356   public void showAllHiddenColumns()
1357   {
1358     posProps.showAllHiddenColumns();
1359   }
1360
1361   public void hideSelectedColumns()
1362   {
1363     if (colSel.isEmpty())
1364     {
1365       return;
1366     }
1367
1368     colSel.hideSelectedColumns();
1369     setSelectionGroup(null);
1370     isColSelChanged(true);
1371   }
1372
1373
1374
1375   // common hide/show seq stuff
1376   public void showAllHiddenSeqs()
1377   {
1378     selectionGroup = posProps.showAllHiddenSeqs(selectionGroup);
1379
1380     /*    if (alignment.getHiddenSequences().getSize() > 0)
1381         {
1382           if (selectionGroup == null)
1383           {
1384             selectionGroup = new SequenceGroup();
1385             selectionGroup.setEndRes(alignment.getWidth() - 1);
1386           }
1387           List<SequenceI> tmp = alignment.getHiddenSequences().showAll(
1388                   hiddenRepSequences);
1389           for (SequenceI seq : tmp)
1390           {
1391             selectionGroup.addSequence(seq, false);
1392             setSequenceAnnotationsVisible(seq, true);
1393           }
1394
1395           hiddenRepSequences = null;
1396
1397           firePropertyChange("alignment", null, alignment.getSequences());
1398           // used to set hasHiddenRows/hiddenRepSequences here, after the property
1399           // changed event
1400           sendSelection();
1401         }*/
1402   }
1403
1404   public void showSequence(int index)
1405   {
1406     posProps.showSequence(index, selectionGroup);
1407     /*List<SequenceI> tmp = alignment.getHiddenSequences().showSequence(
1408             index, hiddenRepSequences);
1409     if (tmp.size() > 0)
1410     {
1411       if (selectionGroup == null)
1412       {
1413         selectionGroup = new SequenceGroup();
1414         selectionGroup.setEndRes(alignment.getWidth() - 1);
1415       }
1416
1417       for (SequenceI seq : tmp)
1418       {
1419         selectionGroup.addSequence(seq, false);
1420         setSequenceAnnotationsVisible(seq, true);
1421       }
1422       firePropertyChange("alignment", null, alignment.getSequences());
1423       sendSelection();
1424     }*/
1425   }
1426
1427   public void hideAllSelectedSeqs()
1428   {
1429     posProps.hideAllSelectedSeqs(selectionGroup);
1430     /*if (selectionGroup == null || selectionGroup.getSize() < 1)
1431     {
1432       return;
1433     }
1434
1435     SequenceI[] seqs = selectionGroup.getSequencesInOrder(alignment);
1436
1437     hideSequence(seqs);
1438
1439     setSelectionGroup(null);*/
1440   }
1441
1442   public void hideSequence(SequenceI[] seq)
1443   {
1444     posProps.hideSequence(seq);
1445     /*if (seq != null)
1446     {
1447       for (int i = 0; i < seq.length; i++)
1448       {
1449         alignment.getHiddenSequences().hideSequence(seq[i]);
1450         setSequenceAnnotationsVisible(seq[i], false);
1451       }
1452       firePropertyChange("alignment", null, alignment.getSequences());
1453     }*/
1454   }
1455
1456   /**
1457    * Hides the specified sequence, or the sequences it represents
1458    * 
1459    * @param sequence
1460    *          the sequence to hide, or keep as representative
1461    * @param representGroup
1462    *          if true, hide the current selection group except for the
1463    *          representative sequence
1464    */
1465   public void hideSequences(SequenceI sequence, boolean representGroup)
1466   {
1467     posProps.hideSequences(sequence, representGroup, selectionGroup);
1468     /*if (selectionGroup == null || selectionGroup.getSize() < 1)
1469     {
1470       hideSequence(new SequenceI[] { sequence });
1471       return;
1472     }
1473
1474     if (representGroup)
1475     {
1476       hideRepSequences(sequence, selectionGroup);
1477       setSelectionGroup(null);
1478       return;
1479     }
1480
1481     int gsize = selectionGroup.getSize();
1482     SequenceI[] hseqs = selectionGroup.getSequences().toArray(
1483             new SequenceI[gsize]);
1484
1485     hideSequence(hseqs);
1486     setSelectionGroup(null);
1487     sendSelection();*/
1488   }
1489
1490
1491   public void hideRepSequences(SequenceI repSequence, SequenceGroup sg)
1492   {
1493     posProps.hideRepSequences(repSequence, sg);
1494     /*int sSize = sg.getSize();
1495     if (sSize < 2)
1496     {
1497       return;
1498     }
1499
1500     if (hiddenRepSequences == null)
1501     {
1502       hiddenRepSequences = new Hashtable<SequenceI, SequenceCollectionI>();
1503     }
1504
1505     hiddenRepSequences.put(repSequence, sg);
1506
1507     // Hide all sequences except the repSequence
1508     SequenceI[] seqs = new SequenceI[sSize - 1];
1509     int index = 0;
1510     for (int i = 0; i < sSize; i++)
1511     {
1512       if (sg.getSequenceAt(i) != repSequence)
1513       {
1514         if (index == sSize - 1)
1515         {
1516           return;
1517         }
1518
1519         seqs[index++] = sg.getSequenceAt(i);
1520       }
1521     }
1522     sg.setSeqrep(repSequence); // note: not done in 2.7applet
1523     sg.setHidereps(true); // note: not done in 2.7applet
1524     hideSequence(seqs);*/
1525
1526   }
1527
1528   /**
1529    * 
1530    * @return null or the current reference sequence
1531    */
1532   public SequenceI getReferenceSeq()
1533   {
1534     return alignment.getSeqrep();
1535   }
1536
1537   /**
1538    * @param seq
1539    * @return true iff seq is the reference for the alignment
1540    */
1541   public boolean isReferenceSeq(SequenceI seq)
1542   {
1543     return alignment.getSeqrep() == seq;
1544   }
1545
1546   /**
1547    * 
1548    * @param seq
1549    * @return true if there are sequences represented by this sequence that are
1550    *         currently hidden
1551    */
1552   public boolean isHiddenRepSequence(SequenceI seq)
1553   {
1554     return posProps.isHiddenRepSequence(seq);
1555     // return (hiddenRepSequences != null && hiddenRepSequences
1556     // .containsKey(seq));
1557   }
1558
1559   /**
1560    * 
1561    * @param seq
1562    * @return null or a sequence group containing the sequences that seq
1563    *         represents
1564    */
1565   public SequenceGroup getRepresentedSequences(SequenceI seq)
1566   {
1567     return posProps.getRepresentedSequences(seq);
1568     // return (SequenceGroup) (hiddenRepSequences == null ? null
1569     // : hiddenRepSequences.get(seq));
1570   }
1571
1572   @Override
1573   public int adjustForHiddenSeqs(int alignmentIndex)
1574   {
1575     return posProps.adjustForHiddenSeqs(alignmentIndex);
1576     // return alignment.getHiddenSequences().adjustForHiddenSeqs(
1577     // alignmentIndex);
1578   }
1579
1580   @Override
1581   public void invertColumnSelection()
1582   {
1583     posProps.invertColumnSelection();
1584     // colSel.invertColumnSelection(0, alignment.getWidth());
1585   }
1586
1587   @Override
1588   public SequenceI[] getSelectionAsNewSequence()
1589   {
1590     SequenceI[] sequences;
1591     // JBPNote: Need to test jalviewLite.getSelectedSequencesAsAlignmentFrom -
1592     // this was the only caller in the applet for this method
1593     // JBPNote: in applet, this method returned references to the alignment
1594     // sequences, and it did not honour the presence/absence of annotation
1595     // attached to the alignment (probably!)
1596     if (selectionGroup == null || selectionGroup.getSize() == 0)
1597     {
1598       sequences = alignment.getSequencesArray();
1599       AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
1600       for (int i = 0; i < sequences.length; i++)
1601       {
1602         // construct new sequence with subset of visible annotation
1603         sequences[i] = new Sequence(sequences[i], annots);
1604       }
1605     }
1606     else
1607     {
1608       sequences = selectionGroup.getSelectionAsNewSequences(alignment);
1609     }
1610
1611     return sequences;
1612   }
1613
1614   @Override
1615   public SequenceI[] getSequenceSelection()
1616   {
1617     SequenceI[] sequences = null;
1618     if (selectionGroup != null)
1619     {
1620       sequences = selectionGroup.getSequencesInOrder(alignment);
1621     }
1622     if (sequences == null)
1623     {
1624       sequences = alignment.getSequencesArray();
1625     }
1626     return sequences;
1627   }
1628
1629   @Override
1630   public CigarArray getViewAsCigars(boolean selectedRegionOnly)
1631   {
1632     return new CigarArray(alignment, posProps.getColumnSelection(),
1633             (selectedRegionOnly ? selectionGroup : null));
1634   }
1635
1636   @Override
1637   public jalview.datamodel.AlignmentView getAlignmentView(
1638           boolean selectedOnly)
1639   {
1640     return getAlignmentView(selectedOnly, false);
1641   }
1642
1643   @Override
1644   public jalview.datamodel.AlignmentView getAlignmentView(
1645           boolean selectedOnly, boolean markGroups)
1646   {
1647     return new AlignmentView(alignment, posProps.getColumnSelection(),
1648             selectionGroup, posProps.hasHiddenColumns(), selectedOnly,
1649             markGroups);
1650   }
1651
1652   @Override
1653   public String[] getViewAsString(boolean selectedRegionOnly)
1654   {
1655     return getViewAsString(selectedRegionOnly, true);
1656   }
1657
1658   @Override
1659   public String[] getViewAsString(boolean selectedRegionOnly,
1660           boolean exportHiddenSeqs)
1661   {
1662     String[] selection = null;
1663     SequenceI[] seqs = null;
1664     int i, iSize;
1665     int start = 0, end = 0;
1666     if (selectedRegionOnly && selectionGroup != null)
1667     {
1668       iSize = selectionGroup.getSize();
1669       seqs = selectionGroup.getSequencesInOrder(alignment);
1670       start = selectionGroup.getStartRes();
1671       end = selectionGroup.getEndRes() + 1;
1672     }
1673     else
1674     {
1675       if (hasHiddenRows() && exportHiddenSeqs)
1676       {
1677         AlignmentI fullAlignment = alignment.getHiddenSequences()
1678                 .getFullAlignment();
1679         iSize = fullAlignment.getHeight();
1680         seqs = fullAlignment.getSequencesArray();
1681         end = fullAlignment.getWidth();
1682       }
1683       else
1684       {
1685         iSize = alignment.getHeight();
1686         seqs = alignment.getSequencesArray();
1687         end = alignment.getWidth();
1688       }
1689     }
1690
1691     selection = new String[iSize];
1692     if (posProps.hasHiddenColumns())
1693     {
1694       selection = posProps.getVisibleSequenceStrings(start, end, seqs);
1695     }
1696     else
1697     {
1698       for (i = 0; i < iSize; i++)
1699       {
1700         selection[i] = seqs[i].getSequenceAsString(start, end);
1701       }
1702
1703     }
1704     return selection;
1705   }
1706
1707   @Override
1708   public List<int[]> getVisibleRegionBoundaries(int min, int max)
1709   {
1710     return posProps.getVisibleRegionBoundaries(min, max);
1711     /*    ArrayList<int[]> regions = new ArrayList<int[]>();
1712         int start = min;
1713         int end = max;
1714
1715         do
1716         {
1717           if (colSel != null && colSel.hasHiddenColumns())
1718           {
1719             if (start == 0)
1720             {
1721               start = colSel.adjustForHiddenColumns(start);
1722             }
1723
1724             end = colSel.getHiddenBoundaryRight(start);
1725             if (start == end)
1726             {
1727               end = max;
1728             }
1729             if (end > max)
1730             {
1731               end = max;
1732             }
1733           }
1734
1735           regions.add(new int[] { start, end });
1736
1737           if (posProps.hasHiddenColumns())
1738           {
1739             start = colSel.adjustForHiddenColumns(end);
1740             start = colSel.getHiddenBoundaryLeft(start) + 1;
1741           }
1742         } while (end < max);
1743
1744         int[][] startEnd = new int[regions.size()][2];
1745
1746         return regions;*/
1747   }
1748
1749   @Override
1750   public List<AlignmentAnnotation> getVisibleAlignmentAnnotation(
1751           boolean selectedOnly)
1752   {
1753     return posProps.getVisibleAlignmentAnnotation(selectedOnly,
1754             selectionGroup);
1755     /*ArrayList<AlignmentAnnotation> ala = new ArrayList<AlignmentAnnotation>();
1756     AlignmentAnnotation[] aa;
1757     if ((aa = alignment.getAlignmentAnnotation()) != null)
1758     {
1759       for (AlignmentAnnotation annot : aa)
1760       {
1761         AlignmentAnnotation clone = new AlignmentAnnotation(annot);
1762         if (selectedOnly && selectionGroup != null)
1763         {
1764           colSel.makeVisibleAnnotation(selectionGroup.getStartRes(),
1765                   selectionGroup.getEndRes(), clone);
1766         }
1767         else
1768         {
1769           colSel.makeVisibleAnnotation(clone);
1770         }
1771         ala.add(clone);
1772       }
1773     }
1774     return ala;*/
1775   }
1776
1777   @Override
1778   public boolean isPadGaps()
1779   {
1780     return padGaps;
1781   }
1782
1783   @Override
1784   public void setPadGaps(boolean padGaps)
1785   {
1786     this.padGaps = padGaps;
1787   }
1788
1789   /**
1790    * apply any post-edit constraints and trigger any calculations needed after
1791    * an edit has been performed on the alignment
1792    * 
1793    * @param ap
1794    */
1795   @Override
1796   public void alignmentChanged(AlignmentViewPanel ap)
1797   {
1798     if (isPadGaps())
1799     {
1800       alignment.padGaps();
1801     }
1802     if (autoCalculateConsensus)
1803     {
1804       updateConsensus(ap);
1805     }
1806     if (hconsensus != null && autoCalculateConsensus)
1807     {
1808       updateConservation(ap);
1809     }
1810     if (autoCalculateStrucConsensus)
1811     {
1812       updateStrucConsensus(ap);
1813     }
1814
1815     // Reset endRes of groups if beyond alignment width
1816     int alWidth = alignment.getWidth();
1817     List<SequenceGroup> groups = alignment.getGroups();
1818     if (groups != null)
1819     {
1820       for (SequenceGroup sg : groups)
1821       {
1822         if (sg.getEndRes() > alWidth)
1823         {
1824           sg.setEndRes(alWidth - 1);
1825         }
1826       }
1827     }
1828
1829     if (selectionGroup != null && selectionGroup.getEndRes() > alWidth)
1830     {
1831       selectionGroup.setEndRes(alWidth - 1);
1832     }
1833
1834     updateAllColourSchemes();
1835     calculator.restartWorkers();
1836     // alignment.adjustSequenceAnnotations();
1837   }
1838
1839   /**
1840    * reset scope and do calculations for all applied colourschemes on alignment
1841    */
1842   void updateAllColourSchemes()
1843   {
1844     ResidueShaderI rs = residueShading;
1845     if (rs != null)
1846     {
1847       rs.alignmentChanged(alignment, posProps.getHiddenRepSequences());
1848
1849       rs.setConsensus(hconsensus);
1850       if (rs.conservationApplied())
1851       {
1852         rs.setConservation(Conservation.calculateConservation("All",
1853                 alignment.getSequences(), 0, alignment.getWidth(), false,
1854                 getConsPercGaps(), false));
1855       }
1856     }
1857
1858     for (SequenceGroup sg : alignment.getGroups())
1859     {
1860       if (sg.cs != null)
1861       {
1862         sg.cs.alignmentChanged(sg, posProps.getHiddenRepSequences());
1863       }
1864       sg.recalcConservation();
1865     }
1866   }
1867
1868   protected void initAutoAnnotation()
1869   {
1870     // TODO: add menu option action that nulls or creates consensus object
1871     // depending on if the user wants to see the annotation or not in a
1872     // specific alignment
1873
1874     if (hconsensus == null && !isDataset)
1875     {
1876       if (!alignment.isNucleotide())
1877       {
1878         initConservation();
1879         initQuality();
1880       }
1881       else
1882       {
1883         initRNAStructure();
1884       }
1885       consensus = new AlignmentAnnotation("Consensus", "PID",
1886               new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
1887       initConsensus(consensus);
1888
1889       initComplementConsensus();
1890     }
1891   }
1892
1893   /**
1894    * If this is a protein alignment and there are mappings to cDNA, add the cDNA
1895    * consensus annotation.
1896    */
1897   public void initComplementConsensus()
1898   {
1899     if (!alignment.isNucleotide())
1900     {
1901       final List<AlignedCodonFrame> codonMappings = alignment
1902               .getCodonFrames();
1903       if (codonMappings != null && !codonMappings.isEmpty())
1904       {
1905         boolean doConsensus = false;
1906         for (AlignedCodonFrame mapping : codonMappings)
1907         {
1908           // TODO hold mapping type e.g. dna-to-protein in AlignedCodonFrame?
1909           MapList[] mapLists = mapping.getdnaToProt();
1910           // mapLists can be empty if project load has not finished resolving
1911           // seqs
1912           if (mapLists.length > 0 && mapLists[0].getFromRatio() == 3)
1913           {
1914             doConsensus = true;
1915             break;
1916           }
1917         }
1918         if (doConsensus)
1919         {
1920           complementConsensus = new AlignmentAnnotation("cDNA Consensus",
1921                   "PID for cDNA", new Annotation[1], 0f, 100f,
1922                   AlignmentAnnotation.BAR_GRAPH);
1923           initConsensus(complementConsensus);
1924         }
1925       }
1926     }
1927   }
1928
1929   private void initConsensus(AlignmentAnnotation aa)
1930   {
1931     aa.hasText = true;
1932     aa.autoCalculated = true;
1933
1934     if (showConsensus)
1935     {
1936       alignment.addAnnotation(aa);
1937     }
1938   }
1939
1940   private void initConservation()
1941   {
1942     if (showConservation)
1943     {
1944       if (conservation == null)
1945       {
1946         conservation = new AlignmentAnnotation("Conservation",
1947                 "Conservation of total alignment less than "
1948                         + getConsPercGaps() + "% gaps", new Annotation[1],
1949                 0f, 11f, AlignmentAnnotation.BAR_GRAPH);
1950         conservation.hasText = true;
1951         conservation.autoCalculated = true;
1952         alignment.addAnnotation(conservation);
1953       }
1954     }
1955   }
1956
1957   private void initQuality()
1958   {
1959     if (showQuality)
1960     {
1961       if (quality == null)
1962       {
1963         quality = new AlignmentAnnotation("Quality",
1964                 "Alignment Quality based on Blosum62 scores",
1965                 new Annotation[1], 0f, 11f, AlignmentAnnotation.BAR_GRAPH);
1966         quality.hasText = true;
1967         quality.autoCalculated = true;
1968         alignment.addAnnotation(quality);
1969       }
1970     }
1971   }
1972
1973   private void initRNAStructure()
1974   {
1975     if (alignment.hasRNAStructure() && strucConsensus == null)
1976     {
1977       strucConsensus = new AlignmentAnnotation("StrucConsensus", "PID",
1978               new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
1979       strucConsensus.hasText = true;
1980       strucConsensus.autoCalculated = true;
1981
1982       if (showConsensus)
1983       {
1984         alignment.addAnnotation(strucConsensus);
1985       }
1986     }
1987   }
1988
1989   /*
1990    * (non-Javadoc)
1991    * 
1992    * @see jalview.api.AlignViewportI#calcPanelHeight()
1993    */
1994   @Override
1995   public int calcPanelHeight()
1996   {
1997     // setHeight of panels
1998     AlignmentAnnotation[] anns = getAlignment().getAlignmentAnnotation();
1999     int height = 0;
2000     int charHeight = getCharHeight();
2001     if (anns != null)
2002     {
2003       BitSet graphgrp = new BitSet();
2004       for (AlignmentAnnotation aa : anns)
2005       {
2006         if (aa == null)
2007         {
2008           System.err.println("Null annotation row: ignoring.");
2009           continue;
2010         }
2011         if (!aa.visible)
2012         {
2013           continue;
2014         }
2015         if (aa.graphGroup > -1)
2016         {
2017           if (graphgrp.get(aa.graphGroup))
2018           {
2019             continue;
2020           }
2021           else
2022           {
2023             graphgrp.set(aa.graphGroup);
2024           }
2025         }
2026         aa.height = 0;
2027
2028         if (aa.hasText)
2029         {
2030           aa.height += charHeight;
2031         }
2032
2033         if (aa.hasIcons)
2034         {
2035           aa.height += 16;
2036         }
2037
2038         if (aa.graph > 0)
2039         {
2040           aa.height += aa.graphHeight;
2041         }
2042
2043         if (aa.height == 0)
2044         {
2045           aa.height = 20;
2046         }
2047
2048         height += aa.height;
2049       }
2050     }
2051     if (height == 0)
2052     {
2053       // set minimum
2054       height = 20;
2055     }
2056     return height;
2057   }
2058
2059   @Override
2060   public void updateGroupAnnotationSettings(boolean applyGlobalSettings,
2061           boolean preserveNewGroupSettings)
2062   {
2063     boolean updateCalcs = false;
2064     boolean conv = isShowGroupConservation();
2065     boolean cons = isShowGroupConsensus();
2066     boolean showprf = isShowSequenceLogo();
2067     boolean showConsHist = isShowConsensusHistogram();
2068     boolean normLogo = isNormaliseSequenceLogo();
2069
2070     /**
2071      * TODO reorder the annotation rows according to group/sequence ordering on
2072      * alignment
2073      */
2074     boolean sortg = true;
2075
2076     // remove old automatic annotation
2077     // add any new annotation
2078
2079     // intersect alignment annotation with alignment groups
2080
2081     AlignmentAnnotation[] aan = alignment.getAlignmentAnnotation();
2082     List<SequenceGroup> oldrfs = new ArrayList<SequenceGroup>();
2083     if (aan != null)
2084     {
2085       for (int an = 0; an < aan.length; an++)
2086       {
2087         if (aan[an].autoCalculated && aan[an].groupRef != null)
2088         {
2089           oldrfs.add(aan[an].groupRef);
2090           alignment.deleteAnnotation(aan[an], false);
2091         }
2092       }
2093     }
2094     if (alignment.getGroups() != null)
2095     {
2096       for (SequenceGroup sg : alignment.getGroups())
2097       {
2098         updateCalcs = false;
2099         if (applyGlobalSettings
2100                 || (!preserveNewGroupSettings && !oldrfs.contains(sg)))
2101         {
2102           // set defaults for this group's conservation/consensus
2103           sg.setshowSequenceLogo(showprf);
2104           sg.setShowConsensusHistogram(showConsHist);
2105           sg.setNormaliseSequenceLogo(normLogo);
2106         }
2107         if (conv)
2108         {
2109           updateCalcs = true;
2110           alignment.addAnnotation(sg.getConservationRow(), 0);
2111         }
2112         if (cons)
2113         {
2114           updateCalcs = true;
2115           alignment.addAnnotation(sg.getConsensus(), 0);
2116         }
2117         // refresh the annotation rows
2118         if (updateCalcs)
2119         {
2120           sg.recalcConservation();
2121         }
2122       }
2123     }
2124     oldrfs.clear();
2125   }
2126
2127   @Override
2128   public boolean isDisplayReferenceSeq()
2129   {
2130     return alignment.hasSeqrep() && viewStyle.isDisplayReferenceSeq();
2131   }
2132
2133   @Override
2134   public void setDisplayReferenceSeq(boolean displayReferenceSeq)
2135   {
2136     viewStyle.setDisplayReferenceSeq(displayReferenceSeq);
2137   }
2138
2139   @Override
2140   public boolean isColourByReferenceSeq()
2141   {
2142     return alignment.hasSeqrep() && viewStyle.isColourByReferenceSeq();
2143   }
2144
2145   @Override
2146   public Color getSequenceColour(SequenceI seq)
2147   {
2148     Color sqc = sequenceColours.get(seq);
2149     return (sqc == null ? Color.white : sqc);
2150   }
2151
2152   @Override
2153   public void setSequenceColour(SequenceI seq, Color col)
2154   {
2155     if (col == null)
2156     {
2157       sequenceColours.remove(seq);
2158     }
2159     else
2160     {
2161       sequenceColours.put(seq, col);
2162     }
2163   }
2164
2165   @Override
2166   public void updateSequenceIdColours()
2167   {
2168     for (SequenceGroup sg : alignment.getGroups())
2169     {
2170       if (sg.idColour != null)
2171       {
2172         for (SequenceI s : sg.getSequences(getHiddenRepSequences()))
2173         {
2174           sequenceColours.put(s, sg.idColour);
2175         }
2176       }
2177     }
2178   }
2179
2180   @Override
2181   public void clearSequenceColours()
2182   {
2183     sequenceColours.clear();
2184   };
2185
2186   @Override
2187   public AlignViewportI getCodingComplement()
2188   {
2189     return this.codingComplement;
2190   }
2191
2192   /**
2193    * Set this as the (cDna/protein) complement of the given viewport. Also
2194    * ensures the reverse relationship is set on the given viewport.
2195    */
2196   @Override
2197   public void setCodingComplement(AlignViewportI av)
2198   {
2199     if (this == av)
2200     {
2201       System.err.println("Ignoring recursive setCodingComplement request");
2202     }
2203     else
2204     {
2205       this.codingComplement = av;
2206       // avoid infinite recursion!
2207       if (av.getCodingComplement() != this)
2208       {
2209         av.setCodingComplement(this);
2210       }
2211     }
2212   }
2213
2214   @Override
2215   public boolean isNucleotide()
2216   {
2217     return getAlignment() == null ? false : getAlignment().isNucleotide();
2218   }
2219
2220   @Override
2221   public FeaturesDisplayedI getFeaturesDisplayed()
2222   {
2223     return featuresDisplayed;
2224   }
2225
2226   @Override
2227   public void setFeaturesDisplayed(FeaturesDisplayedI featuresDisplayedI)
2228   {
2229     featuresDisplayed = featuresDisplayedI;
2230   }
2231
2232   @Override
2233   public boolean areFeaturesDisplayed()
2234   {
2235     return featuresDisplayed != null
2236             && featuresDisplayed.getRegisteredFeaturesCount() > 0;
2237   }
2238
2239   /**
2240    * set the flag
2241    * 
2242    * @param b
2243    *          features are displayed if true
2244    */
2245   @Override
2246   public void setShowSequenceFeatures(boolean b)
2247   {
2248     viewStyle.setShowSequenceFeatures(b);
2249   }
2250
2251   @Override
2252   public boolean isShowSequenceFeatures()
2253   {
2254     return viewStyle.isShowSequenceFeatures();
2255   }
2256
2257   @Override
2258   public void setShowSequenceFeaturesHeight(boolean selected)
2259   {
2260     viewStyle.setShowSequenceFeaturesHeight(selected);
2261   }
2262
2263   @Override
2264   public boolean isShowSequenceFeaturesHeight()
2265   {
2266     return viewStyle.isShowSequenceFeaturesHeight();
2267   }
2268
2269   @Override
2270   public void setShowAnnotation(boolean b)
2271   {
2272     viewStyle.setShowAnnotation(b);
2273   }
2274
2275   @Override
2276   public boolean isShowAnnotation()
2277   {
2278     return viewStyle.isShowAnnotation();
2279   }
2280
2281   @Override
2282   public boolean isRightAlignIds()
2283   {
2284     return viewStyle.isRightAlignIds();
2285   }
2286
2287   @Override
2288   public void setRightAlignIds(boolean rightAlignIds)
2289   {
2290     viewStyle.setRightAlignIds(rightAlignIds);
2291   }
2292
2293   @Override
2294   public boolean getConservationSelected()
2295   {
2296     return viewStyle.getConservationSelected();
2297   }
2298
2299   @Override
2300   public void setShowBoxes(boolean state)
2301   {
2302     viewStyle.setShowBoxes(state);
2303   }
2304
2305   /**
2306    * @return
2307    * @see jalview.api.ViewStyleI#getTextColour()
2308    */
2309   @Override
2310   public Color getTextColour()
2311   {
2312     return viewStyle.getTextColour();
2313   }
2314
2315   /**
2316    * @return
2317    * @see jalview.api.ViewStyleI#getTextColour2()
2318    */
2319   @Override
2320   public Color getTextColour2()
2321   {
2322     return viewStyle.getTextColour2();
2323   }
2324
2325   /**
2326    * @return
2327    * @see jalview.api.ViewStyleI#getThresholdTextColour()
2328    */
2329   @Override
2330   public int getThresholdTextColour()
2331   {
2332     return viewStyle.getThresholdTextColour();
2333   }
2334
2335   /**
2336    * @return
2337    * @see jalview.api.ViewStyleI#isConservationColourSelected()
2338    */
2339   @Override
2340   public boolean isConservationColourSelected()
2341   {
2342     return viewStyle.isConservationColourSelected();
2343   }
2344
2345   /**
2346    * @return
2347    * @see jalview.api.ViewStyleI#isRenderGaps()
2348    */
2349   @Override
2350   public boolean isRenderGaps()
2351   {
2352     return viewStyle.isRenderGaps();
2353   }
2354
2355   /**
2356    * @return
2357    * @see jalview.api.ViewStyleI#isShowColourText()
2358    */
2359   @Override
2360   public boolean isShowColourText()
2361   {
2362     return viewStyle.isShowColourText();
2363   }
2364
2365   /**
2366    * @param conservationColourSelected
2367    * @see jalview.api.ViewStyleI#setConservationColourSelected(boolean)
2368    */
2369   @Override
2370   public void setConservationColourSelected(
2371           boolean conservationColourSelected)
2372   {
2373     viewStyle.setConservationColourSelected(conservationColourSelected);
2374   }
2375
2376   /**
2377    * @param showColourText
2378    * @see jalview.api.ViewStyleI#setShowColourText(boolean)
2379    */
2380   @Override
2381   public void setShowColourText(boolean showColourText)
2382   {
2383     viewStyle.setShowColourText(showColourText);
2384   }
2385
2386   /**
2387    * @param textColour
2388    * @see jalview.api.ViewStyleI#setTextColour(java.awt.Color)
2389    */
2390   @Override
2391   public void setTextColour(Color textColour)
2392   {
2393     viewStyle.setTextColour(textColour);
2394   }
2395
2396   /**
2397    * @param thresholdTextColour
2398    * @see jalview.api.ViewStyleI#setThresholdTextColour(int)
2399    */
2400   @Override
2401   public void setThresholdTextColour(int thresholdTextColour)
2402   {
2403     viewStyle.setThresholdTextColour(thresholdTextColour);
2404   }
2405
2406   /**
2407    * @param textColour2
2408    * @see jalview.api.ViewStyleI#setTextColour2(java.awt.Color)
2409    */
2410   @Override
2411   public void setTextColour2(Color textColour2)
2412   {
2413     viewStyle.setTextColour2(textColour2);
2414   }
2415
2416   @Override
2417   public ViewStyleI getViewStyle()
2418   {
2419     return new ViewStyle(viewStyle);
2420   }
2421
2422   @Override
2423   public void setViewStyle(ViewStyleI settingsForView)
2424   {
2425     viewStyle = new ViewStyle(settingsForView);
2426     if (residueShading != null)
2427     {
2428       residueShading.setConservationApplied(settingsForView
2429               .isConservationColourSelected());
2430     }
2431   }
2432
2433   @Override
2434   public boolean sameStyle(ViewStyleI them)
2435   {
2436     return viewStyle.sameStyle(them);
2437   }
2438
2439   /**
2440    * @return
2441    * @see jalview.api.ViewStyleI#getIdWidth()
2442    */
2443   @Override
2444   public int getIdWidth()
2445   {
2446     return viewStyle.getIdWidth();
2447   }
2448
2449   /**
2450    * @param i
2451    * @see jalview.api.ViewStyleI#setIdWidth(int)
2452    */
2453   @Override
2454   public void setIdWidth(int i)
2455   {
2456     viewStyle.setIdWidth(i);
2457   }
2458
2459   /**
2460    * @return
2461    * @see jalview.api.ViewStyleI#isCentreColumnLabels()
2462    */
2463   @Override
2464   public boolean isCentreColumnLabels()
2465   {
2466     return viewStyle.isCentreColumnLabels();
2467   }
2468
2469   /**
2470    * @param centreColumnLabels
2471    * @see jalview.api.ViewStyleI#setCentreColumnLabels(boolean)
2472    */
2473   @Override
2474   public void setCentreColumnLabels(boolean centreColumnLabels)
2475   {
2476     viewStyle.setCentreColumnLabels(centreColumnLabels);
2477   }
2478
2479   /**
2480    * @param showdbrefs
2481    * @see jalview.api.ViewStyleI#setShowDBRefs(boolean)
2482    */
2483   @Override
2484   public void setShowDBRefs(boolean showdbrefs)
2485   {
2486     viewStyle.setShowDBRefs(showdbrefs);
2487   }
2488
2489   /**
2490    * @return
2491    * @see jalview.api.ViewStyleI#isShowDBRefs()
2492    */
2493   @Override
2494   public boolean isShowDBRefs()
2495   {
2496     return viewStyle.isShowDBRefs();
2497   }
2498
2499   /**
2500    * @return
2501    * @see jalview.api.ViewStyleI#isShowNPFeats()
2502    */
2503   @Override
2504   public boolean isShowNPFeats()
2505   {
2506     return viewStyle.isShowNPFeats();
2507   }
2508
2509   /**
2510    * @param shownpfeats
2511    * @see jalview.api.ViewStyleI#setShowNPFeats(boolean)
2512    */
2513   @Override
2514   public void setShowNPFeats(boolean shownpfeats)
2515   {
2516     viewStyle.setShowNPFeats(shownpfeats);
2517   }
2518
2519   public abstract StructureSelectionManager getStructureSelectionManager();
2520
2521   /**
2522    * Add one command to the command history list.
2523    * 
2524    * @param command
2525    */
2526   public void addToHistoryList(CommandI command)
2527   {
2528     if (this.historyList != null)
2529     {
2530       this.historyList.push(command);
2531       broadcastCommand(command, false);
2532     }
2533   }
2534
2535   protected void broadcastCommand(CommandI command, boolean undo)
2536   {
2537     getStructureSelectionManager().commandPerformed(command, undo,
2538             getVamsasSource());
2539   }
2540
2541   /**
2542    * Add one command to the command redo list.
2543    * 
2544    * @param command
2545    */
2546   public void addToRedoList(CommandI command)
2547   {
2548     if (this.redoList != null)
2549     {
2550       this.redoList.push(command);
2551     }
2552     broadcastCommand(command, true);
2553   }
2554
2555   /**
2556    * Clear the command redo list.
2557    */
2558   public void clearRedoList()
2559   {
2560     if (this.redoList != null)
2561     {
2562       this.redoList.clear();
2563     }
2564   }
2565
2566   public void setHistoryList(Deque<CommandI> list)
2567   {
2568     this.historyList = list;
2569   }
2570
2571   public Deque<CommandI> getHistoryList()
2572   {
2573     return this.historyList;
2574   }
2575
2576   public void setRedoList(Deque<CommandI> list)
2577   {
2578     this.redoList = list;
2579   }
2580
2581   public Deque<CommandI> getRedoList()
2582   {
2583     return this.redoList;
2584   }
2585
2586   @Override
2587   public VamsasSource getVamsasSource()
2588   {
2589     return this;
2590   }
2591
2592   public SequenceAnnotationOrder getSortAnnotationsBy()
2593   {
2594     return sortAnnotationsBy;
2595   }
2596
2597   public void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
2598   {
2599     this.sortAnnotationsBy = sortAnnotationsBy;
2600   }
2601
2602   public boolean isShowAutocalculatedAbove()
2603   {
2604     return showAutocalculatedAbove;
2605   }
2606
2607   public void setShowAutocalculatedAbove(boolean showAutocalculatedAbove)
2608   {
2609     this.showAutocalculatedAbove = showAutocalculatedAbove;
2610   }
2611
2612   @Override
2613   public boolean isScaleProteinAsCdna()
2614   {
2615     return viewStyle.isScaleProteinAsCdna();
2616   }
2617
2618   @Override
2619   public void setScaleProteinAsCdna(boolean b)
2620   {
2621     viewStyle.setScaleProteinAsCdna(b);
2622   }
2623
2624   /**
2625    * @return true if view should scroll to show the highlighted region of a
2626    *         sequence
2627    * @return
2628    */
2629   @Override
2630   public final boolean isFollowHighlight()
2631   {
2632     return followHighlight;
2633   }
2634
2635   @Override
2636   public final void setFollowHighlight(boolean b)
2637   {
2638     this.followHighlight = b;
2639   }
2640
2641   @Override
2642   public ViewportPositionProps getPosProps()
2643   {
2644     return posProps;
2645   }
2646
2647   @Override
2648   public int getStartRes()
2649   {
2650     return posProps.getStartRes();
2651   }
2652
2653   @Override
2654   public int getEndRes()
2655   {
2656     return posProps.getEndRes();
2657   }
2658
2659   @Override
2660   public int getStartSeq()
2661   {
2662     return posProps.getStartSeq();
2663   }
2664
2665   public void setStartRes(int res)
2666   {
2667     posProps.setStartRes(res);
2668     // this.startRes = res;
2669   }
2670
2671   public void setStartSeq(int seq)
2672   {
2673     posProps.setStartSeq(seq);
2674     // this.startSeq = seq;
2675   }
2676
2677   public void setEndRes(int res)
2678   {
2679     posProps.setEndRes(res);
2680     /*if (res > alignment.getWidth() - 1)
2681     {
2682       // log.System.out.println(" Corrected res from " + res + " to maximum " +
2683       // (alignment.getWidth()-1));
2684       res = alignment.getWidth() - 1;
2685     }
2686     if (res < 0)
2687     {
2688       res = 0;
2689     }
2690     this.endRes = res;*/
2691   }
2692
2693   public void setEndSeq(int seq)
2694   {
2695     posProps.setEndSeq(seq);
2696     /*if (seq > alignment.getHeight())
2697     {
2698       seq = alignment.getHeight();
2699     }
2700     if (seq < 0)
2701     {
2702       seq = 0;
2703     }
2704     this.endSeq = seq;*/
2705   }
2706
2707   @Override
2708   public int getEndSeq()
2709   {
2710     return posProps.getEndSeq();
2711     // return endSeq;
2712   }
2713
2714   /**
2715    * Helper method to populate the SearchResults with the location in the
2716    * complementary alignment to scroll to, in order to match this one.
2717    * 
2718    * @param sr
2719    *          the SearchResults to add to
2720    * @return the offset (below top of visible region) of the matched sequence
2721    */
2722   protected int findComplementScrollTarget(SearchResultsI sr)
2723   {
2724     final AlignViewportI complement = getCodingComplement();
2725     if (complement == null || !complement.isFollowHighlight())
2726     {
2727       return 0;
2728     }
2729     boolean iAmProtein = !getAlignment().isNucleotide();
2730     AlignmentI proteinAlignment = iAmProtein ? getAlignment() : complement
2731             .getAlignment();
2732     if (proteinAlignment == null)
2733     {
2734       return 0;
2735     }
2736     final List<AlignedCodonFrame> mappings = proteinAlignment
2737             .getCodonFrames();
2738
2739     /*
2740      * Heuristic: find the first mapped sequence (if any) with a non-gapped
2741      * residue in the middle column of the visible region. Scroll the
2742      * complementary alignment to line up the corresponding residue.
2743      */
2744     int seqOffset = 0;
2745     SequenceI sequence = null;
2746
2747     /*
2748      * locate 'middle' column (true middle if an odd number visible, left of
2749      * middle if an even number visible)
2750      */
2751     int middleColumn = getStartRes() + (getEndRes() - getStartRes()) / 2;
2752     final HiddenSequences hiddenSequences = getAlignment()
2753             .getHiddenSequences();
2754
2755     /*
2756      * searching to the bottom of the alignment gives smoother scrolling across
2757      * all gapped visible regions
2758      */
2759     int lastSeq = alignment.getHeight() - 1;
2760     List<AlignedCodonFrame> seqMappings = null;
2761     for (int seqNo = getStartSeq(); seqNo < lastSeq; seqNo++, seqOffset++)
2762     {
2763       sequence = getAlignment().getSequenceAt(seqNo);
2764       if (hiddenSequences != null && hiddenSequences.isHidden(sequence))
2765       {
2766         continue;
2767       }
2768       if (Comparison.isGap(sequence.getCharAt(middleColumn)))
2769       {
2770         continue;
2771       }
2772       seqMappings = MappingUtils
2773               .findMappingsForSequenceAndOthers(sequence, mappings,
2774                       getCodingComplement().getAlignment().getSequences());
2775       if (!seqMappings.isEmpty())
2776       {
2777         break;
2778       }
2779     }
2780
2781     if (sequence == null || seqMappings == null || seqMappings.isEmpty())
2782     {
2783       /*
2784        * No ungapped mapped sequence in middle column - do nothing
2785        */
2786       return 0;
2787     }
2788     MappingUtils.addSearchResults(sr, sequence,
2789             sequence.findPosition(middleColumn), seqMappings);
2790     return seqOffset;
2791   }
2792
2793   /**
2794    * synthesize a column selection if none exists so it covers the given
2795    * selection group. if wholewidth is false, no column selection is made if the
2796    * selection group covers the whole alignment width.
2797    * 
2798    * @param sg
2799    * @param wholewidth
2800    */
2801   public void expandColSelection(SequenceGroup sg, boolean wholewidth)
2802   {
2803     if (!this.hasSelectedColumns())
2804     {
2805       posProps.expandColSelection(sg, wholewidth);
2806     }
2807     /*int sgs, sge;
2808     if (sg != null && (sgs = sg.getStartRes()) >= 0
2809             && sg.getStartRes() <= (sge = sg.getEndRes())
2810             && !this.hasSelectedColumns())
2811     {
2812       if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
2813       {
2814         // do nothing
2815         return;
2816       }
2817       if (colSel == null)
2818       {
2819         colSel = new ColumnSelection();
2820       }
2821       for (int cspos = sg.getStartRes(); cspos <= sg.getEndRes(); cspos++)
2822       {
2823         colSel.addElement(cspos);
2824       }
2825     }*/
2826   }
2827
2828   /**
2829    * hold status of current selection group - defined on alignment or not.
2830    */
2831   private boolean selectionIsDefinedGroup = false;
2832
2833
2834   @Override
2835   public boolean isSelectionDefinedGroup()
2836   {
2837     if (selectionGroup == null)
2838     {
2839       return false;
2840     }
2841     if (isSelectionGroupChanged(true))
2842     {
2843       selectionIsDefinedGroup = false;
2844       List<SequenceGroup> gps = alignment.getGroups();
2845       if (gps == null || gps.size() == 0)
2846       {
2847         selectionIsDefinedGroup = false;
2848       }
2849       else
2850       {
2851         selectionIsDefinedGroup = gps.contains(selectionGroup);
2852       }
2853     }
2854     return selectionGroup.getContext() == alignment
2855             || selectionIsDefinedGroup;
2856   }
2857
2858   /**
2859    * null, or currently highlighted results on this view
2860    */
2861   private SearchResultsI searchResults = null;
2862
2863   @Override
2864   public boolean hasSearchResults()
2865   {
2866     return searchResults != null;
2867   }
2868
2869   @Override
2870   public void setSearchResults(SearchResultsI results)
2871   {
2872     searchResults = results;
2873   }
2874
2875   @Override
2876   public SearchResultsI getSearchResults()
2877   {
2878     return searchResults;
2879   }
2880 }