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