(JAL-969) removed TODOs
[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     return calculator.isWorking();
211   }
212
213   public boolean isCalculationInProgress(
214           AlignmentAnnotation alignmentAnnotation)
215   {
216     if (!alignmentAnnotation.autoCalculated)
217       return false;
218     if ((calculator.isWorking(consensusThread) && consensus==alignmentAnnotation)
219             || (calculator.isWorking(conservationThread) &&  (conservation==alignmentAnnotation || quality==alignmentAnnotation))
220             || (calculator.isWorking(strucConsensusThread) && strucConsensus==alignmentAnnotation)
221             )
222     {
223       return true;
224     }
225     return false;
226   }
227   @Override
228   public boolean isClosed()
229   {
230     // TODO: check that this isClosed is only true after panel is closed, not before it is fully constructed.
231     return alignment==null;
232   }
233
234   @Override
235   public AlignCalcManagerI getCalcManager()
236   {
237     return calculator;
238   }
239
240   /**
241    * should conservation rows be shown for groups
242    */
243   protected boolean showGroupConservation = false;
244
245   /**
246    * should consensus rows be shown for groups
247    */
248   protected boolean showGroupConsensus = false;
249
250   /**
251    * should consensus profile be rendered by default
252    */
253   protected boolean showSequenceLogo = false;
254   /**
255    * should consensus profile be rendered normalised to row height
256    */
257   protected boolean normaliseSequenceLogo = false;
258   /**
259    * should consensus histograms be rendered by default
260    */
261   protected boolean showConsensusHistogram = true;
262
263   /**
264    * @return the showConsensusProfile
265    */
266   public boolean isShowSequenceLogo()
267   {
268     return showSequenceLogo;
269   }
270
271   /**
272    * @param showSequenceLogo
273    *          the new value
274    */
275   public void setShowSequenceLogo(boolean showSequenceLogo)
276   {
277     if (showSequenceLogo != this.showSequenceLogo)
278     {
279       // TODO: decouple settings setting from calculation when refactoring
280       // annotation update method from alignframe to viewport
281       this.showSequenceLogo = showSequenceLogo;
282       if (consensusThread != null)
283       {
284         consensusThread.updateAnnotation();
285       }
286       if (strucConsensusThread != null)
287       {
288         strucConsensusThread.updateAnnotation();
289       }
290     }
291     this.showSequenceLogo = showSequenceLogo;
292   }
293
294   /**
295    * @param showConsensusHistogram
296    *          the showConsensusHistogram to set
297    */
298   public void setShowConsensusHistogram(boolean showConsensusHistogram)
299   {
300     this.showConsensusHistogram = showConsensusHistogram;
301   }
302
303   /**
304    * @return the showGroupConservation
305    */
306   public boolean isShowGroupConservation()
307   {
308     return showGroupConservation;
309   }
310
311   /**
312    * @param showGroupConservation
313    *          the showGroupConservation to set
314    */
315   public void setShowGroupConservation(boolean showGroupConservation)
316   {
317     this.showGroupConservation = showGroupConservation;
318   }
319
320   /**
321    * @return the showGroupConsensus
322    */
323   public boolean isShowGroupConsensus()
324   {
325     return showGroupConsensus;
326   }
327
328   /**
329    * @param showGroupConsensus
330    *          the showGroupConsensus to set
331    */
332   public void setShowGroupConsensus(boolean showGroupConsensus)
333   {
334     this.showGroupConsensus = showGroupConsensus;
335   }
336
337   /**
338    * 
339    * @return flag to indicate if the consensus histogram should be rendered by
340    *         default
341    */
342   public boolean isShowConsensusHistogram()
343   {
344     return this.showConsensusHistogram;
345   }
346
347   /**
348    * show non-conserved residues only
349    */
350   protected boolean showUnconserved = false;
351
352
353   /**
354    * when set, updateAlignment will always ensure sequences are of equal length
355    */
356   private boolean padGaps = false;
357
358   /**
359    * when set, alignment should be reordered according to a newly opened tree
360    */
361   public boolean sortByTree = false;
362
363   public boolean getShowUnconserved()
364   {
365     return showUnconserved;
366   }
367
368   public void setShowUnconserved(boolean showunconserved)
369   {
370     showUnconserved = showunconserved;
371   }
372
373   /**
374    * @param showNonconserved
375    *          the showUnconserved to set
376    */
377   public void setShowunconserved(boolean displayNonconserved)
378   {
379     this.showUnconserved = displayNonconserved;
380   }
381
382   /**
383    * 
384    * 
385    * @return null or the currently selected sequence region
386    */
387   public SequenceGroup getSelectionGroup()
388   {
389     return selectionGroup;
390   }
391
392   /**
393    * Set the selection group for this window.
394    * 
395    * @param sg
396    *          - group holding references to sequences in this alignment view
397    * 
398    */
399   public void setSelectionGroup(SequenceGroup sg)
400   {
401     selectionGroup = sg;
402   }
403
404   public void setHiddenColumns(ColumnSelection colsel)
405   {
406     this.colSel = colsel;
407     if (colSel.getHiddenColumns() != null)
408     {
409       hasHiddenColumns = true;
410     }
411   }
412
413   public ColumnSelection getColumnSelection()
414   {
415     return colSel;
416   }
417   public void setColumnSelection(ColumnSelection colSel)
418   {
419     this.colSel=colSel;
420   }
421   public Hashtable getHiddenRepSequences()
422   {
423     return hiddenRepSequences;
424   }
425   public void setHiddenRepSequences(Hashtable hiddenRepSequences)
426   {
427     this.hiddenRepSequences = hiddenRepSequences;
428   }
429   protected boolean hasHiddenColumns = false;
430
431   public void updateHiddenColumns()
432   {
433     hasHiddenColumns = colSel.getHiddenColumns() != null;  
434   }
435   
436   protected boolean hasHiddenRows = false;
437   
438   public boolean hasHiddenRows() {
439     return hasHiddenRows;
440   }
441
442   protected SequenceGroup selectionGroup;
443
444   public void setSequenceSetId(String newid)
445   {
446     if (sequenceSetID!=null)
447     {
448       System.err.println("Warning - overwriting a sequenceSetId for a viewport!");
449     }
450     sequenceSetID=new String(newid);
451   }
452   public String getSequenceSetId()
453   {
454     if (sequenceSetID == null)
455     {
456       sequenceSetID = alignment.hashCode() + "";
457     }
458
459     return sequenceSetID;
460   }
461   /**
462    * unique viewId for synchronizing state (e.g. with stored Jalview Project)
463    * 
464    */
465   protected String viewId = null;
466
467   public String getViewId()
468   {
469     if (viewId == null)
470     {
471       viewId = this.getSequenceSetId() + "." + this.hashCode() + "";
472     }
473     return viewId;
474   }
475   public void setIgnoreGapsConsensus(boolean b, AlignmentViewPanel ap)
476   {
477     ignoreGapsInConsensusCalculation = b;
478     if (ap!=null) {updateConsensus(ap);
479     if (globalColourScheme != null)
480     {
481       globalColourScheme.setThreshold(globalColourScheme.getThreshold(),
482               ignoreGapsInConsensusCalculation);
483     }}
484     
485   }
486   private long sgrouphash = -1, colselhash = -1;
487
488   /**
489    * checks current SelectionGroup against record of last hash value, and
490    * updates record.
491    * 
492    * @param b
493    *          update the record of last hash value
494    * 
495    * @return true if SelectionGroup changed since last call (when b is true)
496    */
497   public boolean isSelectionGroupChanged(boolean b)
498   {
499     int hc = (selectionGroup == null || selectionGroup.getSize() == 0) ? -1
500             : selectionGroup.hashCode();
501     if (hc != -1 && hc != sgrouphash)
502     {
503       if (b)
504       {
505         sgrouphash = hc;
506       }
507       return true;
508     }
509     return false;
510   }
511
512   /**
513    * checks current colsel against record of last hash value, and optionally
514    * updates record.
515    * 
516    * @param b
517    *          update the record of last hash value
518    * @return true if colsel changed since last call (when b is true)
519    */
520   public boolean isColSelChanged(boolean b)
521   {
522     int hc = (colSel == null || colSel.size() == 0) ? -1 : colSel
523             .hashCode();
524     if (hc != -1 && hc != colselhash)
525     {
526       if (b)
527       {
528         colselhash = hc;
529       }
530       return true;
531     }
532     return false;
533   }
534
535   public boolean getIgnoreGapsConsensus()
536   {
537     return ignoreGapsInConsensusCalculation;
538   }
539
540   /// property change stuff
541
542   // JBPNote Prolly only need this in the applet version.
543   private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
544           this);
545
546
547   /**
548    * Property change listener for changes in alignment
549    * 
550    * @param listener
551    *          DOCUMENT ME!
552    */
553   public void addPropertyChangeListener(
554           java.beans.PropertyChangeListener listener)
555   {
556     changeSupport.addPropertyChangeListener(listener);
557   }
558
559   /**
560    * DOCUMENT ME!
561    * 
562    * @param listener
563    *          DOCUMENT ME!
564    */
565   public void removePropertyChangeListener(
566           java.beans.PropertyChangeListener listener)
567   {
568     changeSupport.removePropertyChangeListener(listener);
569   }
570
571   /**
572    * Property change listener for changes in alignment
573    * 
574    * @param prop
575    *          DOCUMENT ME!
576    * @param oldvalue
577    *          DOCUMENT ME!
578    * @param newvalue
579    *          DOCUMENT ME!
580    */
581   public void firePropertyChange(String prop, Object oldvalue,
582           Object newvalue)
583   {
584     changeSupport.firePropertyChange(prop, oldvalue, newvalue);
585   }
586
587   // common hide/show column stuff
588   
589
590   public void hideSelectedColumns()
591   {
592     if (colSel.size() < 1)
593     {
594       return;
595     }
596
597     colSel.hideSelectedColumns();
598     setSelectionGroup(null);
599
600     hasHiddenColumns = true;
601   }
602
603   public void hideColumns(int start, int end)
604   {
605     if (start == end)
606     {
607       colSel.hideColumns(start);
608     }
609     else
610     {
611       colSel.hideColumns(start, end);
612     }
613
614     hasHiddenColumns = true;
615   }
616
617   public void showColumn(int col)
618   {
619     colSel.revealHiddenColumns(col);
620     if (colSel.getHiddenColumns() == null)
621     {
622       hasHiddenColumns = false;
623     }
624   }
625
626   public void showAllHiddenColumns()
627   {
628     colSel.revealAllHiddenColumns();
629     hasHiddenColumns = false;
630   }
631
632   
633   // common hide/show seq stuff
634   public void showAllHiddenSeqs()
635   {
636     if (alignment.getHiddenSequences().getSize() > 0)
637     {
638       if (selectionGroup == null)
639       {
640         selectionGroup = new SequenceGroup();
641         selectionGroup.setEndRes(alignment.getWidth() - 1);
642       }
643       Vector tmp = alignment.getHiddenSequences().showAll(
644               hiddenRepSequences);
645       for (int t = 0; t < tmp.size(); t++)
646       {
647         selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
648       }
649
650       hasHiddenRows = false;
651       hiddenRepSequences = null;
652
653       firePropertyChange("alignment", null, alignment.getSequences());
654       // used to set hasHiddenRows/hiddenRepSequences here, after the property changed event
655       sendSelection();
656     }
657   }
658
659   public void showSequence(int index)
660   {
661     Vector tmp = alignment.getHiddenSequences().showSequence(index,
662             hiddenRepSequences);
663     if (tmp.size() > 0)
664     {
665       if (selectionGroup == null)
666       {
667         selectionGroup = new SequenceGroup();
668         selectionGroup.setEndRes(alignment.getWidth() - 1);
669       }
670
671       for (int t = 0; t < tmp.size(); t++)
672       {
673         selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
674       }
675       // JBPNote: refactor: only update flag if we modified visiblity (used to do this regardless) 
676       if (alignment.getHiddenSequences().getSize() < 1)
677       {
678         hasHiddenRows = false;
679       }
680       firePropertyChange("alignment", null, alignment.getSequences());
681       sendSelection();
682     }
683   }
684
685
686   
687   public void hideAllSelectedSeqs()
688   {
689     if (selectionGroup == null || selectionGroup.getSize() < 1)
690     {
691       return;
692     }
693
694     SequenceI[] seqs = selectionGroup.getSequencesInOrder(alignment);
695
696     hideSequence(seqs);
697
698     setSelectionGroup(null);
699   }
700   
701
702   public void hideSequence(SequenceI[] seq)
703   {
704     if (seq != null)
705     {
706       for (int i = 0; i < seq.length; i++)
707       {
708         alignment.getHiddenSequences().hideSequence(seq[i]);
709       }
710       hasHiddenRows = true;
711       firePropertyChange("alignment", null, alignment.getSequences());
712     }
713   }
714
715   public void hideRepSequences(SequenceI repSequence, SequenceGroup sg)
716   {
717     int sSize = sg.getSize();
718     if (sSize < 2)
719     {
720       return;
721     }
722
723     if (hiddenRepSequences == null)
724     {
725       hiddenRepSequences = new Hashtable();
726     }
727
728     hiddenRepSequences.put(repSequence, sg);
729
730     // Hide all sequences except the repSequence
731     SequenceI[] seqs = new SequenceI[sSize - 1];
732     int index = 0;
733     for (int i = 0; i < sSize; i++)
734     {
735       if (sg.getSequenceAt(i) != repSequence)
736       {
737         if (index == sSize - 1)
738         {
739           return;
740         }
741
742         seqs[index++] = sg.getSequenceAt(i);
743       }
744     }
745     sg.setSeqrep(repSequence); // note: not done in 2.7applet 
746     sg.setHidereps(true); // note: not done in 2.7applet
747     hideSequence(seqs);
748
749   }
750
751   public boolean isHiddenRepSequence(SequenceI seq)
752   {
753     return hiddenRepSequences != null
754           && hiddenRepSequences.containsKey(seq);
755   }
756   public SequenceGroup getRepresentedSequences(SequenceI seq)
757   {
758     return (SequenceGroup) (hiddenRepSequences == null ? null : hiddenRepSequences.get(seq));
759   }
760
761   public int adjustForHiddenSeqs(int alignmentIndex)
762   {
763     return alignment.getHiddenSequences().adjustForHiddenSeqs(
764             alignmentIndex);
765   }
766
767   // Selection manipulation
768   /**
769    * broadcast selection to any interested parties
770    */
771   public abstract void sendSelection();
772   
773
774   public void invertColumnSelection()
775   {
776     colSel.invertColumnSelection(0, alignment.getWidth());
777   }
778
779
780   /**
781    * This method returns an array of new SequenceI objects derived from the
782    * whole alignment or just the current selection with start and end points
783    * adjusted
784    * 
785    * @note if you need references to the actual SequenceI objects in the
786    *       alignment or currently selected then use getSequenceSelection()
787    * @return selection as new sequenceI objects
788    */
789   public SequenceI[] getSelectionAsNewSequence()
790   {
791     SequenceI[] sequences;
792     // JBPNote: Need to test jalviewLite.getSelectedSequencesAsAlignmentFrom - this was the only caller in the applet for this method
793     // 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!)
794     if (selectionGroup == null)
795     {
796       sequences = alignment.getSequencesArray();
797       AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
798       for (int i = 0; i < sequences.length; i++)
799       {
800         sequences[i] = new Sequence(sequences[i], annots); // construct new
801         // sequence with
802         // subset of visible
803         // annotation
804       }
805     }
806     else
807     {
808       sequences = selectionGroup.getSelectionAsNewSequences(alignment);
809     }
810
811     return sequences;
812   }
813
814
815   /**
816    * get the currently selected sequence objects or all the sequences in the
817    * alignment.
818    * 
819    * @return array of references to sequence objects
820    */
821   public SequenceI[] getSequenceSelection()
822   {
823     SequenceI[] sequences = null;
824     if (selectionGroup != null)
825     {
826       sequences = selectionGroup.getSequencesInOrder(alignment);
827     }
828     if (sequences == null)
829     {
830       sequences = alignment.getSequencesArray();
831     }
832     return sequences;
833   }
834
835
836   /**
837    * This method returns the visible alignment as text, as seen on the GUI, ie
838    * if columns are hidden they will not be returned in the result. Use this for
839    * calculating trees, PCA, redundancy etc on views which contain hidden
840    * columns.
841    * 
842    * @return String[]
843    */
844   public jalview.datamodel.CigarArray getViewAsCigars(
845           boolean selectedRegionOnly)
846   {
847     return new jalview.datamodel.CigarArray(alignment,
848             (hasHiddenColumns ? colSel : null),
849             (selectedRegionOnly ? selectionGroup : null));
850   }
851
852   /**
853    * return a compact representation of the current alignment selection to pass
854    * to an analysis function
855    * 
856    * @param selectedOnly
857    *          boolean true to just return the selected view
858    * @return AlignmentView
859    */
860   public jalview.datamodel.AlignmentView getAlignmentView(
861           boolean selectedOnly)
862   {
863     return getAlignmentView(selectedOnly, false);
864   }
865
866   /**
867    * return a compact representation of the current alignment selection to pass
868    * to an analysis function
869    * 
870    * @param selectedOnly
871    *          boolean true to just return the selected view
872    * @param markGroups
873    *          boolean true to annotate the alignment view with groups on the
874    *          alignment (and intersecting with selected region if selectedOnly
875    *          is true)
876    * @return AlignmentView
877    */
878   public jalview.datamodel.AlignmentView getAlignmentView(
879           boolean selectedOnly, boolean markGroups)
880   {
881     return new AlignmentView(alignment, colSel, selectionGroup,
882             hasHiddenColumns, selectedOnly, markGroups);
883   }
884
885
886   /**
887    * This method returns the visible alignment as text, as seen on the GUI, ie
888    * if columns are hidden they will not be returned in the result. Use this for
889    * calculating trees, PCA, redundancy etc on views which contain hidden
890    * columns.
891    * 
892    * @return String[]
893    */
894   public String[] getViewAsString(boolean selectedRegionOnly)
895   {
896     String[] selection = null;
897     SequenceI[] seqs = null;
898     int i, iSize;
899     int start = 0, end = 0;
900     if (selectedRegionOnly && selectionGroup != null)
901     {
902       iSize = selectionGroup.getSize();
903       seqs = selectionGroup.getSequencesInOrder(alignment);
904       start = selectionGroup.getStartRes();
905       end = selectionGroup.getEndRes() + 1;
906     }
907     else
908     {
909       iSize = alignment.getHeight();
910       seqs = alignment.getSequencesArray();
911       end = alignment.getWidth();
912     }
913
914     selection = new String[iSize];
915     if (hasHiddenColumns)
916     {
917       selection = colSel.getVisibleSequenceStrings(start, end, seqs);
918     }
919     else
920     {
921       for (i = 0; i < iSize; i++)
922       {
923         selection[i] = seqs[i].getSequenceAsString(start, end);
924       }
925
926     }
927     return selection;
928   }
929
930
931   /**
932    * return visible region boundaries within given column range
933    * @param min first column (inclusive, from 0)
934    * @param max last column (exclusive)
935    * @return int[][] range of {start,end} visible positions
936    */
937   public int[][] getVisibleRegionBoundaries(int min, int max)
938   {
939     Vector regions = new Vector();
940     int start = min;
941     int end = max;
942
943     do
944     {
945       if (hasHiddenColumns)
946       {
947         if (start == 0)
948         {
949           start = colSel.adjustForHiddenColumns(start);
950         }
951
952         end = colSel.getHiddenBoundaryRight(start);
953         if (start == end)
954         {
955           end = max;
956         }
957         if (end > max)
958         {
959           end = max;
960         }
961       }
962
963       regions.addElement(new int[]
964       { start, end });
965
966       if (hasHiddenColumns)
967       {
968         start = colSel.adjustForHiddenColumns(end);
969         start = colSel.getHiddenBoundaryLeft(start) + 1;
970       }
971     } while (end < max);
972
973     int[][] startEnd = new int[regions.size()][2];
974
975     regions.copyInto(startEnd);
976
977     return startEnd;
978
979   }
980   /**
981    * @return the padGaps
982    */
983   public boolean isPadGaps()
984   {
985     return padGaps;
986   }
987   /**
988    * @param padGaps the padGaps to set
989    */
990   public void setPadGaps(boolean padGaps)
991   {
992     this.padGaps = padGaps;
993   }
994   /**
995    * apply any post-edit constraints and trigger any calculations needed after an edit has been performed on the alignment 
996    * @param ap
997    */
998   public void alignmentChanged(AlignmentViewPanel ap)
999   {
1000     if (isPadGaps())
1001     {
1002       alignment.padGaps();
1003     }
1004     if (autoCalculateConsensus)
1005     {
1006       updateConsensus(ap);
1007     }
1008     if (hconsensus != null && autoCalculateConsensus)
1009     {
1010       updateConservation(ap);
1011     }
1012     if (autoCalculateStrucConsensus)
1013     {
1014       updateStrucConsensus(ap);
1015     }
1016
1017     // Reset endRes of groups if beyond alignment width
1018     int alWidth = alignment.getWidth();
1019     Vector groups = alignment.getGroups();
1020     if (groups != null)
1021     {
1022       for (int i = 0; i < groups.size(); i++)
1023       {
1024         SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
1025         if (sg.getEndRes() > alWidth)
1026         {
1027           sg.setEndRes(alWidth - 1);
1028         }
1029       }
1030     }
1031
1032     if (selectionGroup != null && selectionGroup.getEndRes() > alWidth)
1033     {
1034       selectionGroup.setEndRes(alWidth - 1);
1035     }
1036
1037     resetAllColourSchemes();
1038
1039     // alignment.adjustSequenceAnnotations();
1040   }
1041
1042   
1043   /**
1044    * reset scope and do calculations for all applied colourschemes on alignment
1045    */
1046   void resetAllColourSchemes()
1047   {
1048     ColourSchemeI cs = globalColourScheme;
1049     if (cs != null)
1050     {
1051       if (cs instanceof ClustalxColourScheme)
1052       {
1053         ((ClustalxColourScheme) cs).resetClustalX(alignment.getSequences(),
1054                 alignment.getWidth());
1055       }
1056
1057       cs.setConsensus(hconsensus);
1058       if (cs.conservationApplied())
1059       {
1060         cs.setConservation(Conservation.calculateConservation("All",
1061                 ResidueProperties.propHash, 3, alignment.getSequences(), 0,
1062                 alignment.getWidth(), false, getConsPercGaps(), false));
1063       }
1064     }
1065
1066     int s, sSize = alignment.getGroups().size();
1067     for (s = 0; s < sSize; s++)
1068     {
1069       SequenceGroup sg = (SequenceGroup) alignment.getGroups().elementAt(s);
1070       if (sg.cs != null && sg.cs instanceof ClustalxColourScheme)
1071       {
1072         ((ClustalxColourScheme) sg.cs).resetClustalX(sg
1073                 .getSequences(hiddenRepSequences), sg.getWidth());
1074       }
1075       sg.recalcConservation();
1076     }
1077   }
1078
1079 }