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