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