(JAL-969) refactored alignmentViewport base class to own package and pulled up most...
[jalview.git] / src / jalview / viewmodel / AlignmentViewport.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.viewmodel;
19
20 import jalview.analysis.Conservation;
21 import jalview.api.AlignCalcManagerI;
22 import jalview.api.AlignViewportI;
23 import jalview.api.AlignmentViewPanel;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.AlignmentView;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.schemes.ClustalxColourScheme;
32 import jalview.schemes.ColourSchemeI;
33 import jalview.schemes.ResidueProperties;
34 import jalview.workers.AlignCalcManager;
35 import jalview.workers.ConsensusThread;
36 import jalview.workers.ConservationThread;
37 import jalview.workers.StrucConsensusThread;
38
39 import java.util.Hashtable;
40 import java.util.Vector;
41
42 /**
43  * base class holding visualization and analysis attributes and common logic for an active alignment view displayed in the GUI
44  * @author jimp
45  *
46  */
47 public abstract class AlignmentViewport implements AlignViewportI
48 {
49   /**
50    * alignment displayed in the viewport. Please use get/setter
51    */
52   protected AlignmentI alignment;
53
54   protected String sequenceSetID;
55
56   private Hashtable hiddenRepSequences;
57
58   protected ColumnSelection colSel = new ColumnSelection();
59
60
61   public boolean autoCalculateConsensus = true;
62
63   protected boolean autoCalculateStrucConsensus = true;
64
65   protected boolean ignoreGapsInConsensusCalculation = false;
66   
67
68   protected ColourSchemeI globalColourScheme = null;
69
70
71   public void setGlobalColourScheme(ColourSchemeI cs)
72   {
73     globalColourScheme = cs;
74   }
75
76   public ColourSchemeI getGlobalColourScheme()
77   {
78     return globalColourScheme;
79   }
80
81
82   protected AlignmentAnnotation consensus;
83
84   protected AlignmentAnnotation strucConsensus;
85
86   protected AlignmentAnnotation conservation;
87
88   protected AlignmentAnnotation quality;
89
90   protected AlignmentAnnotation[] groupConsensus;
91
92   protected AlignmentAnnotation[] groupConservation;
93
94   /** 
95    * results of alignment consensus analysis for visible portion of view 
96    */
97   protected Hashtable[] hconsensus=null;
98
99   /**
100    * results of secondary structure base pair consensus for visible portion of view
101    */
102   protected Hashtable[] hStrucConsensus=null;
103
104   /**
105    * percentage gaps allowed in a column before all amino acid properties should be considered unconserved
106    */
107   int ConsPercGaps = 25; // JBPNote : This should be a scalable property!
108
109
110   public int getConsPercGaps()
111   {
112     return ConsPercGaps;
113   }
114   @Override
115   public void setSequenceConsensusHash(Hashtable[] hconsensus)
116   {
117     this.hconsensus=hconsensus;
118     
119   }
120
121   @Override
122   public Hashtable[] getSequenceConsensusHash()
123   {
124     return hconsensus;
125   }
126
127   @Override
128   public Hashtable[] getRnaStructureConsensusHash()
129   {
130     return hStrucConsensus;
131   }
132   @Override
133   public void setRnaStructureConsensusHash(Hashtable[] hStrucConsensus)
134   {
135     this.hStrucConsensus=hStrucConsensus;
136     
137   }
138   @Override
139   public AlignmentAnnotation getAlignmentQualityAnnot()
140   {
141     return quality;
142   }
143
144   @Override
145   public AlignmentAnnotation getAlignmentConservationAnnotation()
146   {
147     return conservation;
148   }
149   @Override
150   public AlignmentAnnotation getAlignmentConsensusAnnotation()
151   {
152     return consensus;
153   }
154   @Override
155   public AlignmentAnnotation getAlignmentStrucConsensusAnnotation()
156   {
157     return strucConsensus;
158   }
159   
160   protected AlignCalcManagerI calculator=new AlignCalcManager();
161
162   jalview.workers.ConsensusThread consensusThread;
163
164   StrucConsensusThread strucConsensusThread;
165
166
167   private ConservationThread conservationThread;
168
169   /**
170    * trigger update of conservation annotation
171    */
172   public void updateConservation(final AlignmentViewPanel ap)
173   {
174     // see note in mantis : issue number 8585
175     if (alignment.isNucleotide() || conservation == null
176             || !autoCalculateConsensus)
177     {
178       return;
179     }
180     
181     calculator.startWorker(conservationThread=new jalview.workers.ConservationThread(this, ap));
182   }
183
184   /**
185    * trigger update of consensus annotation
186    */
187   public void updateConsensus(final AlignmentViewPanel ap)
188   {
189     // see note in mantis : issue number 8585
190     if (consensus == null || !autoCalculateConsensus)
191     {
192       return;
193     }
194     calculator.startWorker(consensusThread = new ConsensusThread(this, ap));
195   }
196
197   // --------START Structure Conservation
198   public void updateStrucConsensus(final AlignmentViewPanel ap)
199   {
200     // see note in mantis : issue number 8585
201     if (strucConsensus == null || !autoCalculateStrucConsensus)
202     {
203       return;
204     }
205     calculator.startWorker(strucConsensusThread = new StrucConsensusThread(this,ap));
206   }
207
208   public boolean isCalcInProgress()
209   {
210     // TODO generalise to iterate over all calculators associated with av
211     return calculator.isWorking();
212   }
213
214   public boolean isCalculationInProgress(
215           AlignmentAnnotation alignmentAnnotation)
216   {
217     if (!alignmentAnnotation.autoCalculated)
218       return false;
219     if ((calculator.isWorking(consensusThread) && consensus==alignmentAnnotation)
220             || (calculator.isWorking(conservationThread) &&  (conservation==alignmentAnnotation || quality==alignmentAnnotation))
221             || (calculator.isWorking(strucConsensusThread) && strucConsensus==alignmentAnnotation)
222             )
223     {
224       return true;
225     }
226     return false;
227   }
228   @Override
229   public boolean isClosed()
230   {
231     // TODO: check that this isClosed is only true after panel is closed, not before it is fully constructed.
232     return alignment==null;
233   }
234
235   @Override
236   public AlignCalcManagerI getCalcManager()
237   {
238     return calculator;
239   }
240
241   /**
242    * should conservation rows be shown for groups
243    */
244   protected boolean showGroupConservation = false;
245
246   /**
247    * should consensus rows be shown for groups
248    */
249   protected boolean showGroupConsensus = false;
250
251   /**
252    * should consensus profile be rendered by default
253    */
254   protected boolean showSequenceLogo = false;
255   /**
256    * should consensus profile be rendered normalised to row height
257    */
258   protected boolean normaliseSequenceLogo = false;
259   /**
260    * should consensus histograms be rendered by default
261    */
262   protected boolean showConsensusHistogram = true;
263
264   /**
265    * @return the showConsensusProfile
266    */
267   public boolean isShowSequenceLogo()
268   {
269     return showSequenceLogo;
270   }
271
272   /**
273    * @param showSequenceLogo
274    *          the new value
275    */
276   public void setShowSequenceLogo(boolean showSequenceLogo)
277   {
278     if (showSequenceLogo != this.showSequenceLogo)
279     {
280       // TODO: decouple settings setting from calculation when refactoring
281       // annotation update method from alignframe to viewport
282       this.showSequenceLogo = showSequenceLogo;
283       if (consensusThread != null)
284       {
285         consensusThread.updateAnnotation();
286       }
287       if (strucConsensusThread != null)
288       {
289         strucConsensusThread.updateAnnotation();
290       }
291     }
292     this.showSequenceLogo = showSequenceLogo;
293   }
294
295   /**
296    * @param showConsensusHistogram
297    *          the showConsensusHistogram to set
298    */
299   public void setShowConsensusHistogram(boolean showConsensusHistogram)
300   {
301     this.showConsensusHistogram = showConsensusHistogram;
302   }
303
304   /**
305    * @return the showGroupConservation
306    */
307   public boolean isShowGroupConservation()
308   {
309     return showGroupConservation;
310   }
311
312   /**
313    * @param showGroupConservation
314    *          the showGroupConservation to set
315    */
316   public void setShowGroupConservation(boolean showGroupConservation)
317   {
318     this.showGroupConservation = showGroupConservation;
319   }
320
321   /**
322    * @return the showGroupConsensus
323    */
324   public boolean isShowGroupConsensus()
325   {
326     return showGroupConsensus;
327   }
328
329   /**
330    * @param showGroupConsensus
331    *          the showGroupConsensus to set
332    */
333   public void setShowGroupConsensus(boolean showGroupConsensus)
334   {
335     this.showGroupConsensus = showGroupConsensus;
336   }
337
338   /**
339    * 
340    * @return flag to indicate if the consensus histogram should be rendered by
341    *         default
342    */
343   public boolean isShowConsensusHistogram()
344   {
345     return this.showConsensusHistogram;
346   }
347
348   /**
349    * show non-conserved residues only
350    */
351   protected boolean showUnconserved = false;
352
353
354   /**
355    * when set, updateAlignment will always ensure sequences are of equal length
356    */
357   private boolean padGaps = false;
358
359   /**
360    * when set, alignment should be reordered according to a newly opened tree
361    */
362   public boolean sortByTree = false;
363
364   public boolean getShowUnconserved()
365   {
366     return showUnconserved;
367   }
368
369   public void setShowUnconserved(boolean showunconserved)
370   {
371     showUnconserved = showunconserved;
372   }
373
374   /**
375    * @param showNonconserved
376    *          the showUnconserved to set
377    */
378   public void setShowunconserved(boolean displayNonconserved)
379   {
380     this.showUnconserved = displayNonconserved;
381   }
382
383   /**
384    * 
385    * 
386    * @return null or the currently selected sequence region
387    */
388   public SequenceGroup getSelectionGroup()
389   {
390     return selectionGroup;
391   }
392
393   /**
394    * Set the selection group for this window.
395    * 
396    * @param sg
397    *          - group holding references to sequences in this alignment view
398    * 
399    */
400   public void setSelectionGroup(SequenceGroup sg)
401   {
402     selectionGroup = sg;
403   }
404
405   public void setHiddenColumns(ColumnSelection colsel)
406   {
407     this.colSel = colsel;
408     if (colSel.getHiddenColumns() != null)
409     {
410       hasHiddenColumns = true;
411     }
412   }
413
414   public ColumnSelection getColumnSelection()
415   {
416     return colSel;
417   }
418   public void setColumnSelection(ColumnSelection colSel)
419   {
420     this.colSel=colSel;
421   }
422   public Hashtable getHiddenRepSequences()
423   {
424     return hiddenRepSequences;
425   }
426   public void setHiddenRepSequences(Hashtable hiddenRepSequences)
427   {
428     this.hiddenRepSequences = hiddenRepSequences;
429   }
430   protected boolean hasHiddenColumns = false;
431
432   public void updateHiddenColumns()
433   {
434     hasHiddenColumns = colSel.getHiddenColumns() != null;  
435   }
436   
437   protected boolean hasHiddenRows = false;
438   
439   public boolean hasHiddenRows() {
440     return hasHiddenRows;
441   }
442
443   protected SequenceGroup selectionGroup;
444
445   public void setSequenceSetId(String newid)
446   {
447     if (sequenceSetID!=null)
448     {
449       System.err.println("Warning - overwriting a sequenceSetId for a viewport!");
450     }
451     sequenceSetID=new String(newid);
452   }
453   public String getSequenceSetId()
454   {
455     if (sequenceSetID == null)
456     {
457       sequenceSetID = alignment.hashCode() + "";
458     }
459
460     return sequenceSetID;
461   }
462   /**
463    * unique viewId for synchronizing state (e.g. with stored Jalview Project)
464    * 
465    */
466   protected String viewId = null;
467
468   public String getViewId()
469   {
470     if (viewId == null)
471     {
472       viewId = this.getSequenceSetId() + "." + this.hashCode() + "";
473     }
474     return viewId;
475   }
476   public void setIgnoreGapsConsensus(boolean b, AlignmentViewPanel ap)
477   {
478     ignoreGapsInConsensusCalculation = b;
479     if (ap!=null) {updateConsensus(ap);
480     if (globalColourScheme != null)
481     {
482       globalColourScheme.setThreshold(globalColourScheme.getThreshold(),
483               ignoreGapsInConsensusCalculation);
484     }}
485     
486   }
487   private long sgrouphash = -1, colselhash = -1;
488
489   /**
490    * checks current SelectionGroup against record of last hash value, and
491    * updates record.
492    * 
493    * @param b
494    *          update the record of last hash value
495    * 
496    * @return true if SelectionGroup changed since last call (when b is true)
497    */
498   public boolean isSelectionGroupChanged(boolean b)
499   {
500     int hc = (selectionGroup == null || selectionGroup.getSize() == 0) ? -1
501             : selectionGroup.hashCode();
502     if (hc != -1 && hc != sgrouphash)
503     {
504       if (b)
505       {
506         sgrouphash = hc;
507       }
508       return true;
509     }
510     return false;
511   }
512
513   /**
514    * checks current colsel against record of last hash value, and optionally
515    * updates record.
516    * 
517    * @param b
518    *          update the record of last hash value
519    * @return true if colsel changed since last call (when b is true)
520    */
521   public boolean isColSelChanged(boolean b)
522   {
523     int hc = (colSel == null || colSel.size() == 0) ? -1 : colSel
524             .hashCode();
525     if (hc != -1 && hc != colselhash)
526     {
527       if (b)
528       {
529         colselhash = hc;
530       }
531       return true;
532     }
533     return false;
534   }
535
536   public boolean getIgnoreGapsConsensus()
537   {
538     return ignoreGapsInConsensusCalculation;
539   }
540
541   /// property change stuff
542
543   // JBPNote Prolly only need this in the applet version.
544   private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
545           this);
546
547
548   /**
549    * Property change listener for changes in alignment
550    * 
551    * @param listener
552    *          DOCUMENT ME!
553    */
554   public void addPropertyChangeListener(
555           java.beans.PropertyChangeListener listener)
556   {
557     changeSupport.addPropertyChangeListener(listener);
558   }
559
560   /**
561    * DOCUMENT ME!
562    * 
563    * @param listener
564    *          DOCUMENT ME!
565    */
566   public void removePropertyChangeListener(
567           java.beans.PropertyChangeListener listener)
568   {
569     changeSupport.removePropertyChangeListener(listener);
570   }
571
572   /**
573    * Property change listener for changes in alignment
574    * 
575    * @param prop
576    *          DOCUMENT ME!
577    * @param oldvalue
578    *          DOCUMENT ME!
579    * @param newvalue
580    *          DOCUMENT ME!
581    */
582   public void firePropertyChange(String prop, Object oldvalue,
583           Object newvalue)
584   {
585     changeSupport.firePropertyChange(prop, oldvalue, newvalue);
586   }
587
588   // common hide/show column stuff
589   
590
591   public void hideSelectedColumns()
592   {
593     if (colSel.size() < 1)
594     {
595       return;
596     }
597
598     colSel.hideSelectedColumns();
599     setSelectionGroup(null);
600
601     hasHiddenColumns = true;
602   }
603
604   public void hideColumns(int start, int end)
605   {
606     if (start == end)
607     {
608       colSel.hideColumns(start);
609     }
610     else
611     {
612       colSel.hideColumns(start, end);
613     }
614
615     hasHiddenColumns = true;
616   }
617
618   public void showColumn(int col)
619   {
620     colSel.revealHiddenColumns(col);
621     if (colSel.getHiddenColumns() == null)
622     {
623       hasHiddenColumns = false;
624     }
625   }
626
627   public void showAllHiddenColumns()
628   {
629     colSel.revealAllHiddenColumns();
630     hasHiddenColumns = false;
631   }
632
633   
634   // common hide/show seq stuff
635   public void showAllHiddenSeqs()
636   {
637     if (alignment.getHiddenSequences().getSize() > 0)
638     {
639       if (selectionGroup == null)
640       {
641         selectionGroup = new SequenceGroup();
642         selectionGroup.setEndRes(alignment.getWidth() - 1);
643       }
644       Vector tmp = alignment.getHiddenSequences().showAll(
645               hiddenRepSequences);
646       for (int t = 0; t < tmp.size(); t++)
647       {
648         selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
649       }
650
651       hasHiddenRows = false;
652       hiddenRepSequences = null;
653
654       firePropertyChange("alignment", null, alignment.getSequences());
655       // used to set hasHiddenRows/hiddenRepSequences here, after the property changed event
656       sendSelection();
657     }
658   }
659
660   public void showSequence(int index)
661   {
662     Vector tmp = alignment.getHiddenSequences().showSequence(index,
663             hiddenRepSequences);
664     if (tmp.size() > 0)
665     {
666       if (selectionGroup == null)
667       {
668         selectionGroup = new SequenceGroup();
669         selectionGroup.setEndRes(alignment.getWidth() - 1);
670       }
671
672       for (int t = 0; t < tmp.size(); t++)
673       {
674         selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
675       }
676       // JBPNote: refactor: only update flag if we modified visiblity (used to do this regardless) 
677       if (alignment.getHiddenSequences().getSize() < 1)
678       {
679         hasHiddenRows = false;
680       }
681       firePropertyChange("alignment", null, alignment.getSequences());
682       sendSelection();
683     }
684   }
685
686
687   
688   public void hideAllSelectedSeqs()
689   {
690     if (selectionGroup == null || selectionGroup.getSize() < 1)
691     {
692       return;
693     }
694
695     SequenceI[] seqs = selectionGroup.getSequencesInOrder(alignment);
696
697     hideSequence(seqs);
698
699     setSelectionGroup(null);
700   }
701   
702
703   public void hideSequence(SequenceI[] seq)
704   {
705     if (seq != null)
706     {
707       for (int i = 0; i < seq.length; i++)
708       {
709         alignment.getHiddenSequences().hideSequence(seq[i]);
710       }
711       hasHiddenRows = true;
712       firePropertyChange("alignment", null, alignment.getSequences());
713     }
714   }
715
716   public void hideRepSequences(SequenceI repSequence, SequenceGroup sg)
717   {
718     int sSize = sg.getSize();
719     if (sSize < 2)
720     {
721       return;
722     }
723
724     if (hiddenRepSequences == null)
725     {
726       hiddenRepSequences = new Hashtable();
727     }
728
729     hiddenRepSequences.put(repSequence, sg);
730
731     // Hide all sequences except the repSequence
732     SequenceI[] seqs = new SequenceI[sSize - 1];
733     int index = 0;
734     for (int i = 0; i < sSize; i++)
735     {
736       if (sg.getSequenceAt(i) != repSequence)
737       {
738         if (index == sSize - 1)
739         {
740           return;
741         }
742
743         seqs[index++] = sg.getSequenceAt(i);
744       }
745     }
746     sg.setSeqrep(repSequence); // note: not done in 2.7applet 
747     sg.setHidereps(true); // note: not done in 2.7applet
748     hideSequence(seqs);
749
750   }
751
752   public boolean isHiddenRepSequence(SequenceI seq)
753   {
754     return hiddenRepSequences != null
755           && hiddenRepSequences.containsKey(seq);
756   }
757   public SequenceGroup getRepresentedSequences(SequenceI seq)
758   {
759     return (SequenceGroup) (hiddenRepSequences == null ? null : hiddenRepSequences.get(seq));
760   }
761
762   public int adjustForHiddenSeqs(int alignmentIndex)
763   {
764     return alignment.getHiddenSequences().adjustForHiddenSeqs(
765             alignmentIndex);
766   }
767
768   // Selection manipulation
769   /**
770    * broadcast selection to any interested parties
771    */
772   public abstract void sendSelection();
773   
774
775   public void invertColumnSelection()
776   {
777     colSel.invertColumnSelection(0, alignment.getWidth());
778   }
779
780
781   /**
782    * This method returns an array of new SequenceI objects derived from the
783    * whole alignment or just the current selection with start and end points
784    * adjusted
785    * 
786    * @note if you need references to the actual SequenceI objects in the
787    *       alignment or currently selected then use getSequenceSelection()
788    * @return selection as new sequenceI objects
789    */
790   public SequenceI[] getSelectionAsNewSequence()
791   {
792     SequenceI[] sequences;
793     // JBPNote: Need to test jalviewLite.getSelectedSequencesAsAlignmentFrom - this was the only caller in the applet for this method
794     // JBPNote: in applet, this method returned references to the alignment sequences, and it did not honour the presence/absence of annotation attached to the alignment (probably!)
795     if (selectionGroup == null)
796     {
797       sequences = alignment.getSequencesArray();
798       AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
799       for (int i = 0; i < sequences.length; i++)
800       {
801         sequences[i] = new Sequence(sequences[i], annots); // construct new
802         // sequence with
803         // subset of visible
804         // annotation
805       }
806     }
807     else
808     {
809       sequences = selectionGroup.getSelectionAsNewSequences(alignment);
810     }
811
812     return sequences;
813   }
814
815
816   /**
817    * get the currently selected sequence objects or all the sequences in the
818    * alignment.
819    * 
820    * @return array of references to sequence objects
821    */
822   public SequenceI[] getSequenceSelection()
823   {
824     SequenceI[] sequences = null;
825     if (selectionGroup != null)
826     {
827       sequences = selectionGroup.getSequencesInOrder(alignment);
828     }
829     if (sequences == null)
830     {
831       sequences = alignment.getSequencesArray();
832     }
833     return sequences;
834   }
835
836
837   /**
838    * This method returns the visible alignment as text, as seen on the GUI, ie
839    * if columns are hidden they will not be returned in the result. Use this for
840    * calculating trees, PCA, redundancy etc on views which contain hidden
841    * columns.
842    * 
843    * @return String[]
844    */
845   public jalview.datamodel.CigarArray getViewAsCigars(
846           boolean selectedRegionOnly)
847   {
848     return new jalview.datamodel.CigarArray(alignment,
849             (hasHiddenColumns ? colSel : null),
850             (selectedRegionOnly ? selectionGroup : null));
851   }
852
853   /**
854    * return a compact representation of the current alignment selection to pass
855    * to an analysis function
856    * 
857    * @param selectedOnly
858    *          boolean true to just return the selected view
859    * @return AlignmentView
860    */
861   public jalview.datamodel.AlignmentView getAlignmentView(
862           boolean selectedOnly)
863   {
864     return getAlignmentView(selectedOnly, false);
865   }
866
867   /**
868    * return a compact representation of the current alignment selection to pass
869    * to an analysis function
870    * 
871    * @param selectedOnly
872    *          boolean true to just return the selected view
873    * @param markGroups
874    *          boolean true to annotate the alignment view with groups on the
875    *          alignment (and intersecting with selected region if selectedOnly
876    *          is true)
877    * @return AlignmentView
878    */
879   public jalview.datamodel.AlignmentView getAlignmentView(
880           boolean selectedOnly, boolean markGroups)
881   {
882     return new AlignmentView(alignment, colSel, selectionGroup,
883             hasHiddenColumns, selectedOnly, markGroups);
884   }
885
886
887   /**
888    * This method returns the visible alignment as text, as seen on the GUI, ie
889    * if columns are hidden they will not be returned in the result. Use this for
890    * calculating trees, PCA, redundancy etc on views which contain hidden
891    * columns.
892    * 
893    * @return String[]
894    */
895   public String[] getViewAsString(boolean selectedRegionOnly)
896   {
897     String[] selection = null;
898     SequenceI[] seqs = null;
899     int i, iSize;
900     int start = 0, end = 0;
901     if (selectedRegionOnly && selectionGroup != null)
902     {
903       iSize = selectionGroup.getSize();
904       seqs = selectionGroup.getSequencesInOrder(alignment);
905       start = selectionGroup.getStartRes();
906       end = selectionGroup.getEndRes() + 1;
907     }
908     else
909     {
910       iSize = alignment.getHeight();
911       seqs = alignment.getSequencesArray();
912       end = alignment.getWidth();
913     }
914
915     selection = new String[iSize];
916     if (hasHiddenColumns)
917     {
918       selection = colSel.getVisibleSequenceStrings(start, end, seqs);
919     }
920     else
921     {
922       for (i = 0; i < iSize; i++)
923       {
924         selection[i] = seqs[i].getSequenceAsString(start, end);
925       }
926
927     }
928     return selection;
929   }
930
931
932   /**
933    * return visible region boundaries within given column range
934    * @param min first column (inclusive, from 0)
935    * @param max last column (exclusive)
936    * @return int[][] range of {start,end} visible positions
937    */
938   public int[][] getVisibleRegionBoundaries(int min, int max)
939   {
940     Vector regions = new Vector();
941     int start = min;
942     int end = max;
943
944     do
945     {
946       if (hasHiddenColumns)
947       {
948         if (start == 0)
949         {
950           start = colSel.adjustForHiddenColumns(start);
951         }
952
953         end = colSel.getHiddenBoundaryRight(start);
954         if (start == end)
955         {
956           end = max;
957         }
958         if (end > max)
959         {
960           end = max;
961         }
962       }
963
964       regions.addElement(new int[]
965       { start, end });
966
967       if (hasHiddenColumns)
968       {
969         start = colSel.adjustForHiddenColumns(end);
970         start = colSel.getHiddenBoundaryLeft(start) + 1;
971       }
972     } while (end < max);
973
974     int[][] startEnd = new int[regions.size()][2];
975
976     regions.copyInto(startEnd);
977
978     return startEnd;
979
980   }
981   /**
982    * @return the padGaps
983    */
984   public boolean isPadGaps()
985   {
986     return padGaps;
987   }
988   /**
989    * @param padGaps the padGaps to set
990    */
991   public void setPadGaps(boolean padGaps)
992   {
993     this.padGaps = padGaps;
994   }
995   /**
996    * apply any post-edit constraints and trigger any calculations needed after an edit has been performed on the alignment 
997    * @param ap
998    */
999   public void alignmentChanged(AlignmentViewPanel ap)
1000   {
1001     // TODO: test jvLite always pads, and that JVD has configurable padding.
1002     if (isPadGaps())
1003     {
1004       alignment.padGaps();
1005     }
1006     if (autoCalculateConsensus)
1007     {
1008       updateConsensus(ap);
1009     }
1010     if (hconsensus != null && autoCalculateConsensus)
1011     {
1012       updateConservation(ap);
1013     }
1014     if (autoCalculateStrucConsensus)
1015     {
1016       updateStrucConsensus(ap);
1017     }
1018
1019     // Reset endRes of groups if beyond alignment width
1020     int alWidth = alignment.getWidth();
1021     Vector groups = alignment.getGroups();
1022     if (groups != null)
1023     {
1024       for (int i = 0; i < groups.size(); i++)
1025       {
1026         SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
1027         if (sg.getEndRes() > alWidth)
1028         {
1029           sg.setEndRes(alWidth - 1);
1030         }
1031       }
1032     }
1033
1034     if (selectionGroup != null && selectionGroup.getEndRes() > alWidth)
1035     {
1036       selectionGroup.setEndRes(alWidth - 1);
1037     }
1038
1039     resetAllColourSchemes();
1040
1041     // alignment.adjustSequenceAnnotations();
1042   }
1043
1044   
1045   /**
1046    * reset scope and do calculations for all applied colourschemes on alignment
1047    */
1048   void resetAllColourSchemes()
1049   {
1050     ColourSchemeI cs = globalColourScheme;
1051     if (cs != null)
1052     {
1053       if (cs instanceof ClustalxColourScheme)
1054       {
1055         ((ClustalxColourScheme) cs).resetClustalX(alignment.getSequences(),
1056                 alignment.getWidth());
1057       }
1058
1059       cs.setConsensus(hconsensus);
1060       if (cs.conservationApplied())
1061       {
1062         cs.setConservation(Conservation.calculateConservation("All",
1063                 ResidueProperties.propHash, 3, alignment.getSequences(), 0,
1064                 alignment.getWidth(), false, getConsPercGaps(), false));
1065       }
1066     }
1067
1068     int s, sSize = alignment.getGroups().size();
1069     for (s = 0; s < sSize; s++)
1070     {
1071       SequenceGroup sg = (SequenceGroup) alignment.getGroups().elementAt(s);
1072       if (sg.cs != null && sg.cs instanceof ClustalxColourScheme)
1073       {
1074         ((ClustalxColourScheme) sg.cs).resetClustalX(sg
1075                 .getSequences(hiddenRepSequences), sg.getWidth());
1076       }
1077       sg.recalcConservation();
1078     }
1079   }
1080
1081 }