Reset colour schemes and alignmentChanged moved to viewport
[jalview.git] / src / jalview / gui / AlignViewport.java
1  /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import jalview.analysis.*;
22
23 import jalview.bin.*;
24
25 import jalview.datamodel.*;
26
27 import jalview.schemes.*;
28
29 import java.awt.*;
30
31 import java.util.*;
32
33
34 /**
35  * DOCUMENT ME!
36  *
37  * @author $author$
38  * @version $Revision$
39  */
40 public class AlignViewport
41 {
42     int startRes;
43     int endRes;
44     int startSeq;
45     int endSeq;
46     boolean showJVSuffix = true;
47     boolean showText = true;
48     boolean showColourText = false;
49     boolean showBoxes = true;
50     boolean wrapAlignment = false;
51     boolean renderGaps = true;
52     boolean showSequenceFeatures = false;
53     boolean showAnnotation = true;
54     boolean colourAppliesToAllGroups = true;
55     ColourSchemeI globalColourScheme = null;
56     boolean conservationColourSelected = false;
57     boolean abovePIDThreshold = false;
58     SequenceGroup selectionGroup;
59     int charHeight;
60     int charWidth;
61     boolean validCharWidth;
62     int wrappedWidth;
63     Font font;
64     AlignmentI alignment;
65     ColumnSelection colSel = new ColumnSelection();
66     int threshold;
67     int increment;
68     NJTree currentTree = null;
69     boolean scaleAboveWrapped = false;
70     boolean scaleLeftWrapped = true;
71     boolean scaleRightWrapped = true;
72     boolean hasHiddenColumns = false;
73     boolean hasHiddenRows = false;
74     boolean showHiddenMarkers = true;
75
76     boolean cursorMode = false;
77
78     // The following vector holds the features which are
79     // currently visible, in the correct order or rendering
80     Hashtable featuresDisplayed = null;
81
82
83     /** DOCUMENT ME!! */
84     public Hashtable [] hconsensus;
85     AlignmentAnnotation consensus;
86     AlignmentAnnotation conservation;
87     AlignmentAnnotation quality;
88     boolean autoCalculateConsensus = true;
89
90     /** DOCUMENT ME!! */
91     public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!
92
93     // JBPNote Prolly only need this in the applet version.
94     private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(this);
95
96     boolean ignoreGapsInConsensusCalculation = false;
97
98     boolean isDataset = false;
99
100     boolean antiAlias = false;
101
102     boolean padGaps = false;
103
104     Rectangle explodedPosition;
105
106     String viewName;
107
108     String sequenceSetID;
109
110     boolean gatherViewsHere = false;
111
112     /**
113      * Creates a new AlignViewport object.
114      *
115      * @param al DOCUMENT ME!
116      */
117     public AlignViewport(AlignmentI al)
118     {
119         setAlignment(al);
120         init();
121     }
122     /**
123      * Create a new AlignViewport with hidden regions
124      * @param al AlignmentI
125      * @param hiddenColumns ColumnSelection
126      */
127     public AlignViewport(AlignmentI al, ColumnSelection hiddenColumns) {
128       setAlignment(al);
129       if (hiddenColumns!=null) {
130         this.colSel = hiddenColumns;
131         if (hiddenColumns.getHiddenColumns() != null)
132           hasHiddenColumns = true;
133       }
134       init();
135     }
136
137     void init()
138     {
139         this.startRes = 0;
140         this.endRes = alignment.getWidth() - 1;
141         this.startSeq = 0;
142         this.endSeq = alignment.getHeight() - 1;
143
144       antiAlias = Cache.getDefault("ANTI_ALIAS", false);
145
146       showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
147       showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
148
149       autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
150
151       padGaps = Cache.getDefault("PAD_GAPS", true);
152
153        String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
154        String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "") ;
155        String fontSize = Cache.getDefault("FONT_SIZE", "10");
156
157        int style = 0;
158
159        if (fontStyle.equals("bold"))
160        {
161          style = 1;
162        }
163        else if (fontStyle.equals("italic"))
164        {
165          style = 2;
166        }
167
168        setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
169
170        alignment.setGapCharacter( Cache.getDefault("GAP_SYMBOL", "-").charAt(0) );
171
172
173         // We must set conservation and consensus before setting colour,
174         // as Blosum and Clustal require this to be done
175         if(hconsensus==null && !isDataset)
176         {
177           if(!alignment.isNucleotide())
178           {
179             conservation = new AlignmentAnnotation("Conservation",
180                 "Conservation of total alignment less than " +
181                 ConsPercGaps + "% gaps",
182                 new Annotation[1], 0f,
183                 11f,
184                 AlignmentAnnotation.BAR_GRAPH);
185             conservation.hasText = true;
186
187
188             if (Cache.getDefault("SHOW_CONSERVATION", true))
189             {
190               alignment.addAnnotation(conservation);
191             }
192
193             if (Cache.getDefault("SHOW_QUALITY", true))
194             {
195               quality = new AlignmentAnnotation("Quality",
196                                                 "Alignment Quality based on Blosum62 scores",
197                                                 new Annotation[1],
198                                                 0f,
199                                                 11f,
200                                                 AlignmentAnnotation.BAR_GRAPH);
201               quality.hasText = true;
202
203               alignment.addAnnotation(quality);
204             }
205           }
206
207           consensus = new AlignmentAnnotation("Consensus", "PID",
208                                                new Annotation[1], 0f, 100f,
209                                                AlignmentAnnotation.BAR_GRAPH);
210           consensus.hasText = true;
211
212            if (Cache.getDefault("SHOW_IDENTITY", true))
213            {
214              alignment.addAnnotation(consensus);
215            }
216         }
217
218         if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
219         {
220           globalColourScheme = ColourSchemeProperty.getColour(alignment,
221               jalview.bin.Cache.getProperty("DEFAULT_COLOUR"));
222
223             if (globalColourScheme instanceof UserColourScheme)
224             {
225                 globalColourScheme = UserDefinedColours.loadDefaultColours();
226                 ((UserColourScheme)globalColourScheme).setThreshold(0, getIgnoreGapsConsensus());
227             }
228
229             if (globalColourScheme != null)
230             {
231                 globalColourScheme.setConsensus(hconsensus);
232             }
233         }
234     }
235
236
237
238     /**
239      * DOCUMENT ME!
240      *
241      * @param b DOCUMENT ME!
242      */
243     public void setShowSequenceFeatures(boolean b)
244     {
245         showSequenceFeatures = b;
246     }
247
248     public boolean getShowSequenceFeatures()
249     {
250       return showSequenceFeatures;
251     }
252
253
254
255     class ConservationThread extends Thread
256     {
257       AlignmentPanel ap;
258       public ConservationThread(AlignmentPanel ap)
259       {
260         this.ap = ap;
261       }
262
263       public void run()
264       {
265         try
266         {
267           int alWidth = alignment.getWidth();
268           if(alWidth<0)
269             return;
270
271           Conservation cons = new jalview.analysis.Conservation("All",
272               jalview.schemes.ResidueProperties.propHash, 3,
273               alignment.getSequences(), 0, alWidth -1);
274
275           cons.calculate();
276           cons.verdict(false, ConsPercGaps);
277
278           if (quality!=null)
279           {
280             cons.findQuality();
281           }
282
283           String sequence = cons.getConsSequence().getSequence();
284           float minR;
285           float minG;
286           float minB;
287           float maxR;
288           float maxG;
289           float maxB;
290           minR = 0.3f;
291           minG = 0.0f;
292           minB = 0f;
293           maxR = 1.0f - minR;
294           maxG = 0.9f - minG;
295           maxB = 0f - minB; // scalable range for colouring both Conservation and Quality
296
297           float min = 0f;
298           float max = 11f;
299           float qmin = 0f;
300           float qmax = 0f;
301
302           char c;
303
304           conservation.annotations = new Annotation[alWidth];
305
306           if (quality!=null)
307           {
308             quality.graphMax = cons.qualityRange[1].floatValue();
309             quality.annotations = new Annotation[alWidth];
310             qmin = cons.qualityRange[0].floatValue();
311             qmax = cons.qualityRange[1].floatValue();
312           }
313
314           for (int i = 0; i < alWidth; i++)
315           {
316             float value = 0;
317
318             c = sequence.charAt(i);
319
320             if (Character.isDigit(c))
321               value = (int) (c - '0');
322             else if (c == '*')
323               value = 11;
324             else if (c == '+')
325               value = 10;
326
327             float vprop = value - min;
328             vprop /= max;
329             conservation.annotations[i] =
330                 new Annotation(String.valueOf(c),
331                                String.valueOf(value), ' ', value,
332                                new Color(minR + (maxR * vprop),
333                                          minG + (maxG * vprop),
334                                          minB + (maxB * vprop)));
335
336             // Quality calc
337             if (quality!=null)
338             {
339               value = ( (Double) cons.quality.get(i)).floatValue();
340               vprop = value - qmin;
341               vprop /= qmax;
342               quality.annotations[i] = new Annotation(" ", String.valueOf(value), ' ',
343                                                value,
344                                                new Color(minR + (maxR * vprop),
345                   minG + (maxG * vprop),
346                   minB + (maxB * vprop)));
347             }
348           }
349         }
350         catch (OutOfMemoryError error)
351         {
352           javax.swing.SwingUtilities.invokeLater(new Runnable()
353           {
354
355
356             public void run()
357             {
358               javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
359                   "Out of memory calculating conservation!!"
360                   +
361                   "\nSee help files for increasing Java Virtual Machine memory."
362                   , "Out of memory",
363                   javax.swing.JOptionPane.WARNING_MESSAGE);
364             }
365           });
366
367           conservation = null;
368           quality = null;
369
370           System.out.println("Conservation calculation: " + error);
371           System.gc();
372
373         }
374
375         if(ap!=null)
376         {
377           ap.repaint();
378         }
379         updatingConservation = false;
380       }
381     }
382
383
384     static ConservationThread conservationThread;
385
386     static ConsensusThread consensusThread;
387
388     boolean consUpdateNeeded = false;
389
390     boolean updatingConsensus = false;
391
392     boolean updatingConservation = false;
393
394     /**
395      * DOCUMENT ME!
396      */
397     public void updateConservation(final AlignmentPanel ap)
398     {
399       if (alignment.isNucleotide() || conservation==null)
400         return;
401
402       updatingConservation = true;
403     // javax.swing.SwingUtilities.invokeLater(new Runnable()      {        public void run()  {
404           while (conservationThread != null && conservationThread.isAlive())
405           {
406             try
407             {
408            //   Thread.sleep(100);
409               ap.repaint();
410             }
411             catch (Exception ex)
412             {}
413           }
414           conservationThread = new ConservationThread(ap);
415           conservationThread.start();
416       //  }      });
417     }
418
419     /**
420      * DOCUMENT ME!
421      */
422     public void updateConsensus(final AlignmentPanel ap)
423     {
424       updatingConsensus = true;
425
426      // javax.swing.SwingUtilities.invokeLater(new Runnable()      {        public void run()   {
427           while (consensusThread != null && consensusThread.isAlive())
428           {
429             try
430             {
431           //    Thread.sleep(100);
432               ap.repaint();
433             }
434             catch (Exception ex)
435             {}
436           }
437           consensusThread = new ConsensusThread(ap);
438           consensusThread.start();
439       //  }      });
440     }
441
442
443     class ConsensusThread extends Thread
444     {
445       AlignmentPanel ap;
446       public ConsensusThread(AlignmentPanel ap)
447       {
448         this.ap = ap;
449       }
450       public void run()
451       {
452         try
453         {
454           int aWidth = alignment.getWidth();
455           if(aWidth<0)
456             return;
457
458           consensus.annotations = null;
459           consensus.annotations = new Annotation[aWidth];
460
461
462           hconsensus = new Hashtable[aWidth];
463           AAFrequency.calculate(alignment.getSequencesArray(),
464                                 0,
465                                 alignment.getWidth(),
466                                 hconsensus);
467
468           for (int i = 0; i < aWidth; i++)
469           {
470             float value = 0;
471             if (ignoreGapsInConsensusCalculation)
472               value = ( (Float) hconsensus[i].get(AAFrequency.PID_NOGAPS)).
473                   floatValue();
474             else
475               value = ( (Float) hconsensus[i].get(AAFrequency.PID_GAPS)).
476                   floatValue();
477
478             String maxRes = hconsensus[i].get(AAFrequency.MAXRESIDUE).toString();
479             String mouseOver = hconsensus[i].get(AAFrequency.MAXRESIDUE) + " ";
480
481             if (maxRes.length() > 1)
482             {
483               mouseOver = "[" + maxRes + "] ";
484               maxRes = "+";
485             }
486
487             mouseOver += ( (int) value + "%");
488             consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ', value);
489           }
490
491
492           if (globalColourScheme != null)
493             globalColourScheme.setConsensus(hconsensus);
494
495         }
496         catch (OutOfMemoryError error)
497         {
498           consensus = null;
499           hconsensus = null;
500           javax.swing.SwingUtilities.invokeLater(new Runnable()
501           {
502             public void run()
503             {
504               javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
505                   "Out of memory calculating consensus!!"
506                   +
507                   "\nSee help files for increasing Java Virtual Machine memory."
508                   , "Out of memory",
509                   javax.swing.JOptionPane.WARNING_MESSAGE);
510             }
511           });
512
513           System.out.println("Consensus calculation: " + error);
514           System.gc();
515         }
516
517         if (ap != null)
518         {
519           ap.repaint();
520         }
521
522
523         updatingConsensus = false;
524       }
525     }
526     /**
527      * get the consensus sequence as displayed under the PID consensus annotation row.
528      * @return consensus sequence as a new sequence object
529      */
530     public SequenceI getConsensusSeq() {
531       if (consensus==null)
532         updateConsensus(null);
533       if (consensus==null)
534         return null;
535       StringBuffer seqs=new StringBuffer();
536       for (int i=0; i<consensus.annotations.length; i++) {
537         if (consensus.annotations[i]!=null) {
538           if (consensus.annotations[i].description.charAt(0) == '[')
539             seqs.append(consensus.annotations[i].description.charAt(1));
540           else
541             seqs.append(consensus.annotations[i].displayCharacter);
542         }
543       }
544       SequenceI sq = new Sequence("Consensus", seqs.toString());
545       sq.setDescription("Percentage Identity Consensus "+((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
546       return sq;
547     }
548     /**
549      * DOCUMENT ME!
550      *
551      * @return DOCUMENT ME!
552      */
553     public SequenceGroup getSelectionGroup()
554     {
555         return selectionGroup;
556     }
557
558     /**
559      * DOCUMENT ME!
560      *
561      * @param sg DOCUMENT ME!
562      */
563     public void setSelectionGroup(SequenceGroup sg)
564     {
565         selectionGroup = sg;
566     }
567
568     /**
569      * DOCUMENT ME!
570      *
571      * @return DOCUMENT ME!
572      */
573     public boolean getConservationSelected()
574     {
575         return conservationColourSelected;
576     }
577
578     /**
579      * DOCUMENT ME!
580      *
581      * @param b DOCUMENT ME!
582      */
583     public void setConservationSelected(boolean b)
584     {
585         conservationColourSelected = b;
586     }
587
588     /**
589      * DOCUMENT ME!
590      *
591      * @return DOCUMENT ME!
592      */
593     public boolean getAbovePIDThreshold()
594     {
595         return abovePIDThreshold;
596     }
597
598     /**
599      * DOCUMENT ME!
600      *
601      * @param b DOCUMENT ME!
602      */
603     public void setAbovePIDThreshold(boolean b)
604     {
605         abovePIDThreshold = b;
606     }
607
608     /**
609      * DOCUMENT ME!
610      *
611      * @return DOCUMENT ME!
612      */
613     public int getStartRes()
614     {
615         return startRes;
616     }
617
618     /**
619      * DOCUMENT ME!
620      *
621      * @return DOCUMENT ME!
622      */
623     public int getEndRes()
624     {
625         return endRes;
626     }
627
628     /**
629      * DOCUMENT ME!
630      *
631      * @return DOCUMENT ME!
632      */
633     public int getStartSeq()
634     {
635         return startSeq;
636     }
637
638     /**
639      * DOCUMENT ME!
640      *
641      * @param cs DOCUMENT ME!
642      */
643     public void setGlobalColourScheme(ColourSchemeI cs)
644     {
645         globalColourScheme = cs;
646     }
647
648     /**
649      * DOCUMENT ME!
650      *
651      * @return DOCUMENT ME!
652      */
653     public ColourSchemeI getGlobalColourScheme()
654     {
655         return globalColourScheme;
656     }
657
658     /**
659      * DOCUMENT ME!
660      *
661      * @param res DOCUMENT ME!
662      */
663     public void setStartRes(int res)
664     {
665         this.startRes = res;
666     }
667
668     /**
669      * DOCUMENT ME!
670      *
671      * @param seq DOCUMENT ME!
672      */
673     public void setStartSeq(int seq)
674     {
675         this.startSeq = seq;
676     }
677
678     /**
679      * DOCUMENT ME!
680      *
681      * @param res DOCUMENT ME!
682      */
683     public void setEndRes(int res)
684     {
685         if (res > (alignment.getWidth() - 1))
686         {
687             // log.System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));
688             res = alignment.getWidth() - 1;
689         }
690
691         if (res < 0)
692         {
693             res = 0;
694         }
695
696         this.endRes = res;
697     }
698
699     /**
700      * DOCUMENT ME!
701      *
702      * @param seq DOCUMENT ME!
703      */
704     public void setEndSeq(int seq)
705     {
706         if (seq > alignment.getHeight())
707         {
708             seq = alignment.getHeight();
709         }
710
711         if (seq < 0)
712         {
713             seq = 0;
714         }
715
716         this.endSeq = seq;
717     }
718
719     /**
720      * DOCUMENT ME!
721      *
722      * @return DOCUMENT ME!
723      */
724     public int getEndSeq()
725     {
726         return endSeq;
727     }
728
729     /**
730      * DOCUMENT ME!
731      *
732      * @param f DOCUMENT ME!
733      */
734     public void setFont(Font f)
735     {
736         font = f;
737
738         Container c = new Container();
739
740         java.awt.FontMetrics fm = c.getFontMetrics(font);
741         setCharHeight(fm.getHeight());
742         setCharWidth(fm.charWidth('M'));
743         validCharWidth = true;
744     }
745
746     /**
747      * DOCUMENT ME!
748      *
749      * @return DOCUMENT ME!
750      */
751     public Font getFont()
752     {
753         return font;
754     }
755
756     /**
757      * DOCUMENT ME!
758      *
759      * @param w DOCUMENT ME!
760      */
761     public void setCharWidth(int w)
762     {
763         this.charWidth = w;
764     }
765
766     /**
767      * DOCUMENT ME!
768      *
769      * @return DOCUMENT ME!
770      */
771     public int getCharWidth()
772     {
773         return charWidth;
774     }
775
776     /**
777      * DOCUMENT ME!
778      *
779      * @param h DOCUMENT ME!
780      */
781     public void setCharHeight(int h)
782     {
783         this.charHeight = h;
784     }
785
786     /**
787      * DOCUMENT ME!
788      *
789      * @return DOCUMENT ME!
790      */
791     public int getCharHeight()
792     {
793         return charHeight;
794     }
795
796     /**
797      * DOCUMENT ME!
798      *
799      * @param w DOCUMENT ME!
800      */
801     public void setWrappedWidth(int w)
802     {
803         this.wrappedWidth = w;
804     }
805
806     /**
807      * DOCUMENT ME!
808      *
809      * @return DOCUMENT ME!
810      */
811     public int getWrappedWidth()
812     {
813         return wrappedWidth;
814     }
815
816
817     /**
818      * DOCUMENT ME!
819      *
820      * @return DOCUMENT ME!
821      */
822     public AlignmentI getAlignment()
823     {
824         return alignment;
825     }
826
827     /**
828      * DOCUMENT ME!
829      *
830      * @param align DOCUMENT ME!
831      */
832     public void setAlignment(AlignmentI align)
833     {
834         this.alignment = align;
835     }
836
837     /**
838      * DOCUMENT ME!
839      *
840      * @param state DOCUMENT ME!
841      */
842     public void setWrapAlignment(boolean state)
843     {
844         wrapAlignment = state;
845     }
846
847     /**
848      * DOCUMENT ME!
849      *
850      * @param state DOCUMENT ME!
851      */
852     public void setShowText(boolean state)
853     {
854         showText = state;
855     }
856
857     /**
858      * DOCUMENT ME!
859      *
860      * @param state DOCUMENT ME!
861      */
862     public void setRenderGaps(boolean state)
863     {
864         renderGaps = state;
865     }
866
867     /**
868      * DOCUMENT ME!
869      *
870      * @return DOCUMENT ME!
871      */
872     public boolean getColourText()
873     {
874         return showColourText;
875     }
876
877     /**
878      * DOCUMENT ME!
879      *
880      * @param state DOCUMENT ME!
881      */
882     public void setColourText(boolean state)
883     {
884         showColourText = state;
885     }
886
887     /**
888      * DOCUMENT ME!
889      *
890      * @param state DOCUMENT ME!
891      */
892     public void setShowBoxes(boolean state)
893     {
894         showBoxes = state;
895     }
896
897     /**
898      * DOCUMENT ME!
899      *
900      * @return DOCUMENT ME!
901      */
902     public boolean getWrapAlignment()
903     {
904         return wrapAlignment;
905     }
906
907     /**
908      * DOCUMENT ME!
909      *
910      * @return DOCUMENT ME!
911      */
912     public boolean getShowText()
913     {
914         return showText;
915     }
916
917     /**
918      * DOCUMENT ME!
919      *
920      * @return DOCUMENT ME!
921      */
922     public boolean getShowBoxes()
923     {
924         return showBoxes;
925     }
926
927     /**
928      * DOCUMENT ME!
929      *
930      * @return DOCUMENT ME!
931      */
932     public char getGapCharacter()
933     {
934         return getAlignment().getGapCharacter();
935     }
936
937     /**
938      * DOCUMENT ME!
939      *
940      * @param gap DOCUMENT ME!
941      */
942     public void setGapCharacter(char gap)
943     {
944         if (getAlignment() != null)
945         {
946             getAlignment().setGapCharacter(gap);
947         }
948     }
949
950     /**
951      * DOCUMENT ME!
952      *
953      * @param thresh DOCUMENT ME!
954      */
955     public void setThreshold(int thresh)
956     {
957         threshold = thresh;
958     }
959
960     /**
961      * DOCUMENT ME!
962      *
963      * @return DOCUMENT ME!
964      */
965     public int getThreshold()
966     {
967         return threshold;
968     }
969
970     /**
971      * DOCUMENT ME!
972      *
973      * @param inc DOCUMENT ME!
974      */
975     public void setIncrement(int inc)
976     {
977         increment = inc;
978     }
979
980     /**
981      * DOCUMENT ME!
982      *
983      * @return DOCUMENT ME!
984      */
985     public int getIncrement()
986     {
987         return increment;
988     }
989
990
991     /**
992      * DOCUMENT ME!
993      *
994      * @return DOCUMENT ME!
995      */
996     public ColumnSelection getColumnSelection()
997     {
998         return colSel;
999     }
1000
1001
1002     /**
1003      * DOCUMENT ME!
1004      *
1005      * @param tree DOCUMENT ME!
1006      */
1007     public void setCurrentTree(NJTree tree)
1008     {
1009         currentTree = tree;
1010     }
1011
1012     /**
1013      * DOCUMENT ME!
1014      *
1015      * @return DOCUMENT ME!
1016      */
1017     public NJTree getCurrentTree()
1018     {
1019         return currentTree;
1020     }
1021
1022     /**
1023      * DOCUMENT ME!
1024      *
1025      * @param b DOCUMENT ME!
1026      */
1027     public void setColourAppliesToAllGroups(boolean b)
1028     {
1029         colourAppliesToAllGroups = b;
1030     }
1031
1032     /**
1033      * DOCUMENT ME!
1034      *
1035      * @return DOCUMENT ME!
1036      */
1037     public boolean getColourAppliesToAllGroups()
1038     {
1039         return colourAppliesToAllGroups;
1040     }
1041
1042     /**
1043      * DOCUMENT ME!
1044      *
1045      * @return DOCUMENT ME!
1046      */
1047     public boolean getShowJVSuffix()
1048     {
1049         return showJVSuffix;
1050     }
1051
1052     /**
1053      * DOCUMENT ME!
1054      *
1055      * @param b DOCUMENT ME!
1056      */
1057     public void setShowJVSuffix(boolean b)
1058     {
1059         showJVSuffix = b;
1060     }
1061
1062
1063     /**
1064      * DOCUMENT ME!
1065      *
1066      * @return DOCUMENT ME!
1067      */
1068     public boolean getShowAnnotation()
1069     {
1070         return showAnnotation;
1071     }
1072
1073     /**
1074      * DOCUMENT ME!
1075      *
1076      * @param b DOCUMENT ME!
1077      */
1078     public void setShowAnnotation(boolean b)
1079     {
1080         showAnnotation = b;
1081     }
1082
1083     /**
1084      * DOCUMENT ME!
1085      *
1086      * @return DOCUMENT ME!
1087      */
1088     public boolean getScaleAboveWrapped()
1089     {
1090         return scaleAboveWrapped;
1091     }
1092
1093     /**
1094      * DOCUMENT ME!
1095      *
1096      * @return DOCUMENT ME!
1097      */
1098     public boolean getScaleLeftWrapped()
1099     {
1100         return scaleLeftWrapped;
1101     }
1102
1103     /**
1104      * DOCUMENT ME!
1105      *
1106      * @return DOCUMENT ME!
1107      */
1108     public boolean getScaleRightWrapped()
1109     {
1110         return scaleRightWrapped;
1111     }
1112
1113     /**
1114      * DOCUMENT ME!
1115      *
1116      * @param b DOCUMENT ME!
1117      */
1118     public void setScaleAboveWrapped(boolean b)
1119     {
1120         scaleAboveWrapped = b;
1121     }
1122
1123     /**
1124      * DOCUMENT ME!
1125      *
1126      * @param b DOCUMENT ME!
1127      */
1128     public void setScaleLeftWrapped(boolean b)
1129     {
1130         scaleLeftWrapped = b;
1131     }
1132
1133     /**
1134      * DOCUMENT ME!
1135      *
1136      * @param b DOCUMENT ME!
1137      */
1138     public void setScaleRightWrapped(boolean b)
1139     {
1140         scaleRightWrapped = b;
1141     }
1142
1143     /**
1144      * Property change listener for changes in alignment
1145      *
1146      * @param listener DOCUMENT ME!
1147      */
1148     public void addPropertyChangeListener(
1149         java.beans.PropertyChangeListener listener)
1150     {
1151         changeSupport.addPropertyChangeListener(listener);
1152     }
1153
1154     /**
1155      * DOCUMENT ME!
1156      *
1157      * @param listener DOCUMENT ME!
1158      */
1159     public void removePropertyChangeListener(
1160         java.beans.PropertyChangeListener listener)
1161     {
1162         changeSupport.removePropertyChangeListener(listener);
1163     }
1164
1165     /**
1166      * Property change listener for changes in alignment
1167      *
1168      * @param prop DOCUMENT ME!
1169      * @param oldvalue DOCUMENT ME!
1170      * @param newvalue DOCUMENT ME!
1171      */
1172     public void firePropertyChange(String prop, Object oldvalue, Object newvalue)
1173     {
1174         changeSupport.firePropertyChange(prop, oldvalue, newvalue);
1175     }
1176
1177     public void setIgnoreGapsConsensus(boolean b, AlignmentPanel ap)
1178     {
1179       ignoreGapsInConsensusCalculation = b;
1180       updateConsensus(ap);
1181       if(globalColourScheme!=null)
1182       {
1183         globalColourScheme.setThreshold(globalColourScheme.getThreshold(), ignoreGapsInConsensusCalculation);
1184       }
1185     }
1186
1187     public boolean getIgnoreGapsConsensus()
1188     {
1189      return ignoreGapsInConsensusCalculation;
1190     }
1191
1192     public void setDataset(boolean b)
1193     {
1194       isDataset = b;
1195     }
1196
1197     public boolean isDataset()
1198     {
1199       return isDataset;
1200     }
1201
1202
1203     public void hideSelectedColumns()
1204     {
1205       if (colSel.size() < 1)
1206         return;
1207
1208       colSel.hideSelectedColumns();
1209       setSelectionGroup(null);
1210
1211       hasHiddenColumns = true;
1212     }
1213
1214
1215     public void hideColumns(int start, int end)
1216     {
1217       if(start==end)
1218         colSel.hideColumns(start);
1219       else
1220         colSel.hideColumns(start, end);
1221
1222       hasHiddenColumns = true;
1223     }
1224
1225     public void hideAllSelectedSeqs()
1226     {
1227       if (selectionGroup == null)
1228         return;
1229
1230       SequenceI[] seqs = selectionGroup.getSequencesInOrder(alignment);
1231
1232       hideSequence(seqs);
1233
1234       setSelectionGroup(null);
1235     }
1236
1237     public void hideSequence(SequenceI [] seq)
1238     {
1239       if(seq!=null)
1240       {
1241         for (int i = 0; i < seq.length; i++)
1242           alignment.getHiddenSequences().hideSequence(seq[i]);
1243
1244         hasHiddenRows = true;
1245         firePropertyChange("alignment", null, alignment.getSequences());
1246       }
1247     }
1248
1249     public void showSequence(int index)
1250     {
1251       Vector tmp = alignment.getHiddenSequences().showSequence(index);
1252       if(tmp.size()>0)
1253       {
1254         if(selectionGroup==null)
1255         {
1256           selectionGroup = new SequenceGroup();
1257           selectionGroup.setEndRes(alignment.getWidth()-1);
1258         }
1259
1260         for (int t = 0; t < tmp.size(); t++)
1261         {
1262           selectionGroup.addSequence(
1263               (SequenceI) tmp.elementAt(t), false
1264               );
1265         }
1266         firePropertyChange("alignment", null, alignment.getSequences());
1267       }
1268
1269       if(alignment.getHiddenSequences().getSize()<1)
1270         hasHiddenRows = false;
1271     }
1272
1273     public void showColumn(int col)
1274     {
1275       colSel.revealHiddenColumns(col);
1276       if(colSel.getHiddenColumns()==null)
1277         hasHiddenColumns = false;
1278     }
1279
1280     public void showAllHiddenColumns()
1281     {
1282       colSel.revealAllHiddenColumns();
1283       hasHiddenColumns = false;
1284     }
1285
1286     public void showAllHiddenSeqs()
1287     {
1288       if(alignment.getHiddenSequences().getSize()>0)
1289       {
1290         if(selectionGroup==null)
1291         {
1292           selectionGroup = new SequenceGroup();
1293           selectionGroup.setEndRes(alignment.getWidth()-1);
1294         }
1295         Vector tmp = alignment.getHiddenSequences().showAll();
1296         for(int t=0; t<tmp.size(); t++)
1297         {
1298           selectionGroup.addSequence(
1299               (SequenceI)tmp.elementAt(t), false
1300               );
1301         }
1302         firePropertyChange("alignment", null, alignment.getSequences());
1303         hasHiddenRows = false;
1304       }
1305     }
1306
1307     public void invertColumnSelection()
1308     {
1309       int column;
1310       for(int i=0; i<alignment.getWidth(); i++)
1311       {
1312         column = i;
1313
1314         if(colSel.contains(column))
1315           colSel.removeElement(column);
1316         else
1317           colSel.addElement(column);
1318
1319       }
1320
1321     }
1322
1323     public int adjustForHiddenSeqs(int alignmentIndex)
1324     {
1325       return alignment.getHiddenSequences().adjustForHiddenSeqs(alignmentIndex);
1326     }
1327
1328     /**
1329      * This method returns the a new SequenceI [] with
1330      * the selection sequence and start and end points adjusted
1331      * @return String[]
1332      */
1333     public SequenceI[] getSelectionAsNewSequence()
1334     {
1335       SequenceI[] sequences;
1336
1337       if (selectionGroup == null)
1338         sequences = alignment.getSequencesArray();
1339       else
1340         sequences = selectionGroup.getSelectionAsNewSequences(alignment);
1341
1342       return sequences;
1343     }
1344
1345     /**
1346      * This method returns the visible alignment as text, as
1347      * seen on the GUI, ie if columns are hidden they will not
1348      * be returned in the result.
1349      * Use this for calculating trees, PCA, redundancy etc on views
1350      * which contain hidden columns.
1351      * @return String[]
1352      */
1353     public jalview.datamodel.CigarArray getViewAsCigars(boolean selectedRegionOnly)
1354     {
1355       CigarArray selection=null;
1356       SequenceI [] seqs= null;
1357       int i, iSize;
1358       int start = 0, end = 0;
1359       if(selectedRegionOnly && selectionGroup!=null)
1360       {
1361         iSize = selectionGroup.getSize(false);
1362         seqs = selectionGroup.getSequencesInOrder(alignment);
1363         start = selectionGroup.getStartRes();
1364         end = selectionGroup.getEndRes(); // inclusive for start and end in SeqCigar constructor
1365       }
1366       else
1367       {
1368         iSize = alignment.getHeight();
1369         seqs = alignment.getSequencesArray();
1370         end = alignment.getWidth()-1;
1371       }
1372       SeqCigar[] selseqs = new SeqCigar[iSize];
1373       for(i=0; i<iSize; i++)
1374       {
1375         selseqs[i] = new SeqCigar(seqs[i], start, end);
1376       }
1377       selection=new CigarArray(selseqs);
1378       // now construct the CigarArray operations
1379       if (hasHiddenColumns) {
1380         Vector regions = colSel.getHiddenColumns();
1381         int [] region;
1382         int hideStart, hideEnd;
1383         int last=start;
1384         for (int j = 0; last<end & j < regions.size(); j++)
1385         {
1386           region = (int[]) regions.elementAt(j);
1387           hideStart = region[0];
1388           hideEnd = region[1];
1389           // edit hidden regions to selection range
1390           if(hideStart<last) {
1391             if (hideEnd > last)
1392             {
1393               hideStart = last;
1394             } else
1395               continue;
1396           }
1397
1398           if (hideStart>end)
1399             break;
1400
1401           if (hideEnd>end)
1402             hideEnd=end;
1403
1404           if (hideStart>hideEnd)
1405             break;
1406           /**
1407            * form operations...
1408            */
1409           if (last<hideStart)
1410             selection.addOperation(CigarArray.M, hideStart-last);
1411           selection.addOperation(CigarArray.D, 1+hideEnd-hideStart);
1412           last = hideEnd+1;
1413         }
1414         // Final match if necessary.
1415         if (last<end)
1416           selection.addOperation(CigarArray.M, end-last+1);
1417       } else {
1418         selection.addOperation(CigarArray.M, end-start+1);
1419       }
1420       return selection;
1421     }
1422     /**
1423      * return a compact representation of the current alignment selection to
1424      * pass to an analysis function
1425      * @param selectedOnly boolean true to just return the selected view
1426      * @return AlignmentView
1427      */
1428     jalview.datamodel.AlignmentView getAlignmentView(boolean selectedOnly) {
1429       // JBPNote:
1430       // this is here because the AlignmentView constructor modifies the CigarArray
1431       // object. Refactoring of Cigar and alignment view representation should
1432       // be done to remove redundancy.
1433       CigarArray aligview = getViewAsCigars(selectedOnly);
1434       if (aligview!=null) {
1435         return new AlignmentView(aligview,
1436             (selectedOnly && selectionGroup!=null) ? selectionGroup.getStartRes() : 0);
1437       }
1438       return null;
1439     }
1440     /**
1441      * This method returns the visible alignment as text, as
1442      * seen on the GUI, ie if columns are hidden they will not
1443      * be returned in the result.
1444      * Use this for calculating trees, PCA, redundancy etc on views
1445      * which contain hidden columns.
1446      * @return String[]
1447      */
1448     public String [] getViewAsString(boolean selectedRegionOnly)
1449     {
1450       String [] selection = null;
1451       SequenceI [] seqs= null;
1452       int i, iSize;
1453       int start = 0, end = 0;
1454       if(selectedRegionOnly && selectionGroup!=null)
1455       {
1456         iSize = selectionGroup.getSize(false);
1457         seqs = selectionGroup.getSequencesInOrder(alignment);
1458         start = selectionGroup.getStartRes();
1459         end = selectionGroup.getEndRes()+1;
1460       }
1461       else
1462       {
1463         iSize = alignment.getHeight();
1464         seqs = alignment.getSequencesArray();
1465         end = alignment.getWidth();
1466       }
1467
1468       selection = new String[iSize];
1469       if (hasHiddenColumns) {
1470         selection = colSel.getVisibleSequenceStrings(start, end, seqs);
1471       } else {
1472         for(i=0; i<iSize; i++)
1473         {
1474           selection[i] = seqs[i].getSequence(start, end);
1475         }
1476
1477       }
1478       return selection;
1479     }
1480
1481     public boolean getShowHiddenMarkers()
1482     {
1483       return showHiddenMarkers;
1484     }
1485
1486     public void setShowHiddenMarkers(boolean show)
1487     {
1488       showHiddenMarkers = show;
1489     }
1490
1491     public String getSequenceSetId()
1492     {
1493       if(sequenceSetID==null)
1494         sequenceSetID =  alignment.hashCode()+"";
1495
1496       return sequenceSetID;
1497     }
1498
1499     public void alignmentChanged(AlignmentPanel ap)
1500     {
1501         if (padGaps)
1502           alignment.padGaps();
1503
1504         if (hconsensus != null && autoCalculateConsensus)
1505         {
1506           updateConsensus(ap);
1507           updateConservation(ap);
1508         }
1509
1510         resetAllColourSchemes();
1511
1512         alignment.adjustSequenceAnnotations();
1513     }
1514
1515
1516     void resetAllColourSchemes()
1517     {
1518       ColourSchemeI cs = globalColourScheme;
1519       if(cs!=null)
1520       {
1521         if (cs instanceof ClustalxColourScheme)
1522         {
1523           ( (ClustalxColourScheme) cs).
1524               resetClustalX(alignment.getSequences(),
1525                             alignment.getWidth());
1526         }
1527
1528         cs.setConsensus(hconsensus);
1529         if (cs.conservationApplied())
1530         {
1531           Alignment al = (Alignment) alignment;
1532           Conservation c = new Conservation("All",
1533                                             ResidueProperties.propHash, 3,
1534                                             al.getSequences(), 0,
1535                                             al.getWidth() - 1);
1536           c.calculate();
1537           c.verdict(false, ConsPercGaps);
1538
1539           cs.setConservation(c);
1540         }
1541       }
1542
1543       int s, sSize = alignment.getGroups().size();
1544       for(s=0; s<sSize; s++)
1545       {
1546         SequenceGroup sg = (SequenceGroup)alignment.getGroups().elementAt(s);
1547         if(sg.cs!=null && sg.cs instanceof ClustalxColourScheme)
1548         {
1549           ((ClustalxColourScheme)sg.cs).resetClustalX(
1550               sg.getSequences(true), sg.getWidth());
1551         }
1552         sg.recalcConservation();
1553       }
1554     }
1555
1556
1557
1558 }