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