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