update author list in license for (JAL-826)
[jalview.git] / src / jalview / datamodel / SequenceGroup.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
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.datamodel;
19
20 import java.util.*;
21
22 import java.awt.*;
23
24 import jalview.analysis.*;
25 import jalview.schemes.*;
26
27 /**
28  * Collects a set contiguous ranges on a set of sequences
29  * 
30  * @author $author$
31  * @version $Revision$
32  */
33 public class SequenceGroup
34 {
35   String groupName;
36
37   String description;
38
39   Conservation conserve;
40
41   Vector aaFrequency;
42
43   boolean displayBoxes = true;
44
45   boolean displayText = true;
46
47   boolean colourText = false;
48
49   /**
50    * after Olivier's non-conserved only character display
51    */
52   boolean showNonconserved = false;
53
54   /**
55    * group members
56    */
57   private Vector sequences = new Vector();
58
59   /**
60    * representative sequence for this group (if any)
61    */
62   private SequenceI seqrep = null;
63
64   int width = -1;
65
66   /**
67    * Colourscheme applied to group if any
68    */
69   public ColourSchemeI cs;
70
71   int startRes = 0;
72
73   int endRes = 0;
74
75   public Color outlineColour = Color.black;
76
77   public Color idColour = null;
78
79   public int thresholdTextColour = 0;
80
81   public Color textColour = Color.black;
82
83   public Color textColour2 = Color.white;
84
85   /**
86    * consensus calculation property
87    */
88   private boolean ignoreGapsInConsensus = true;
89
90   /**
91    * consensus calculation property
92    */
93   private boolean showSequenceLogo = false;
94
95   /**
96    * @return the includeAllConsSymbols
97    */
98   public boolean isShowSequenceLogo()
99   {
100     return showSequenceLogo;
101   }
102
103   /**
104    * Creates a new SequenceGroup object.
105    */
106   public SequenceGroup()
107   {
108     groupName = "JGroup:" + this.hashCode();
109   }
110
111   /**
112    * Creates a new SequenceGroup object.
113    * 
114    * @param sequences
115    * @param groupName
116    * @param scheme
117    * @param displayBoxes
118    * @param displayText
119    * @param colourText
120    * @param start
121    *          first column of group
122    * @param end
123    *          last column of group
124    */
125   public SequenceGroup(Vector sequences, String groupName,
126           ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
127           boolean colourText, int start, int end)
128   {
129     this.sequences = sequences;
130     this.groupName = groupName;
131     this.displayBoxes = displayBoxes;
132     this.displayText = displayText;
133     this.colourText = colourText;
134     this.cs = scheme;
135     startRes = start;
136     endRes = end;
137     recalcConservation();
138   }
139
140   /**
141    * copy constructor
142    * 
143    * @param seqsel
144    */
145   public SequenceGroup(SequenceGroup seqsel)
146   {
147     if (seqsel != null)
148     {
149       sequences = new Vector();
150       Enumeration sq = seqsel.sequences.elements();
151       while (sq.hasMoreElements())
152       {
153         sequences.addElement(sq.nextElement());
154       }
155       ;
156       if (seqsel.groupName != null)
157       {
158         groupName = new String(seqsel.groupName);
159       }
160       displayBoxes = seqsel.displayBoxes;
161       displayText = seqsel.displayText;
162       colourText = seqsel.colourText;
163       startRes = seqsel.startRes;
164       endRes = seqsel.endRes;
165       cs = seqsel.cs;
166       if (seqsel.description != null)
167         description = new String(seqsel.description);
168       hidecols = seqsel.hidecols;
169       hidereps = seqsel.hidereps;
170       idColour = seqsel.idColour;
171       outlineColour = seqsel.outlineColour;
172       seqrep = seqsel.seqrep;
173       textColour = seqsel.textColour;
174       textColour2 = seqsel.textColour2;
175       thresholdTextColour = seqsel.thresholdTextColour;
176       width = seqsel.width;
177       ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
178       if (seqsel.conserve != null)
179       {
180         recalcConservation(); // safer than
181         // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
182       }
183     }
184   }
185
186   public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
187   {
188     int iSize = sequences.size();
189     SequenceI[] seqs = new SequenceI[iSize];
190     SequenceI[] inorder = getSequencesInOrder(align);
191
192     for (int i = 0, ipos = 0; i < inorder.length; i++)
193     {
194       SequenceI seq = inorder[i];
195
196       seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
197       if (seqs[ipos] != null)
198       {
199         seqs[ipos].setDescription(seq.getDescription());
200         seqs[ipos].setDBRef(seq.getDBRef());
201         seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
202         if (seq.getDatasetSequence() != null)
203         {
204           seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
205         }
206
207         if (seq.getAnnotation() != null)
208         {
209           AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
210           // Only copy annotation that is either a score or referenced by the
211           // alignment's annotation vector
212           for (int a = 0; a < seq.getAnnotation().length; a++)
213           {
214             AlignmentAnnotation tocopy = seq.getAnnotation()[a];
215             if (alann != null)
216             {
217               boolean found = false;
218               for (int pos = 0; pos < alann.length; pos++)
219               {
220                 if (alann[pos] == tocopy)
221                 {
222                   found = true;
223                   break;
224                 }
225               }
226               if (!found)
227                 continue;
228             }
229             AlignmentAnnotation newannot = new AlignmentAnnotation(
230                     seq.getAnnotation()[a]);
231             newannot.restrict(startRes, endRes);
232             newannot.setSequenceRef(seqs[ipos]);
233             newannot.adjustForAlignment();
234             seqs[ipos].addAlignmentAnnotation(newannot);
235           }
236         }
237         ipos++;
238       }
239       else
240       {
241         iSize--;
242       }
243     }
244     if (iSize != inorder.length)
245     {
246       SequenceI[] nseqs = new SequenceI[iSize];
247       System.arraycopy(seqs, 0, nseqs, 0, iSize);
248       seqs = nseqs;
249     }
250     return seqs;
251
252   }
253
254   /**
255    * If sequence ends in gaps, the end residue can be correctly calculated here
256    * 
257    * @param seq
258    *          SequenceI
259    * @return int
260    */
261   public int findEndRes(SequenceI seq)
262   {
263     int eres = 0;
264     char ch;
265
266     for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
267     {
268       ch = seq.getCharAt(j);
269       if (!jalview.util.Comparison.isGap((ch)))
270       {
271         eres++;
272       }
273     }
274
275     if (eres > 0)
276     {
277       eres += seq.getStart() - 1;
278     }
279
280     return eres;
281   }
282
283   public Vector getSequences(Hashtable hiddenReps)
284   {
285     if (hiddenReps == null)
286     {
287       return sequences;
288     }
289     else
290     {
291       Vector allSequences = new Vector();
292       SequenceI seq, seq2;
293       for (int i = 0; i < sequences.size(); i++)
294       {
295         seq = (SequenceI) sequences.elementAt(i);
296         allSequences.addElement(seq);
297         if (hiddenReps.containsKey(seq))
298         {
299           SequenceGroup hsg = (SequenceGroup) hiddenReps.get(seq);
300           for (int h = 0; h < hsg.getSize(); h++)
301           {
302             seq2 = hsg.getSequenceAt(h);
303             if (seq2 != seq && !allSequences.contains(seq2))
304             {
305               allSequences.addElement(seq2);
306             }
307           }
308         }
309       }
310
311       return allSequences;
312     }
313   }
314
315   public SequenceI[] getSequencesAsArray(Hashtable hiddenReps)
316   {
317     Vector tmp = getSequences(hiddenReps);
318     if (tmp == null)
319     {
320       return null;
321     }
322     SequenceI[] result = new SequenceI[tmp.size()];
323     for (int i = 0; i < result.length; i++)
324     {
325       result[i] = (SequenceI) tmp.elementAt(i);
326     }
327
328     return result;
329   }
330
331   /**
332    * DOCUMENT ME!
333    * 
334    * @param col
335    *          DOCUMENT ME!
336    * 
337    * @return DOCUMENT ME!
338    */
339   public boolean adjustForRemoveLeft(int col)
340   {
341     // return value is true if the group still exists
342     if (startRes >= col)
343     {
344       startRes = startRes - col;
345     }
346
347     if (endRes >= col)
348     {
349       endRes = endRes - col;
350
351       if (startRes > endRes)
352       {
353         startRes = 0;
354       }
355     }
356     else
357     {
358       // must delete this group!!
359       return false;
360     }
361
362     return true;
363   }
364
365   /**
366    * DOCUMENT ME!
367    * 
368    * @param col
369    *          DOCUMENT ME!
370    * 
371    * @return DOCUMENT ME!
372    */
373   public boolean adjustForRemoveRight(int col)
374   {
375     if (startRes > col)
376     {
377       // delete this group
378       return false;
379     }
380
381     if (endRes >= col)
382     {
383       endRes = col;
384     }
385
386     return true;
387   }
388
389   /**
390    * DOCUMENT ME!
391    * 
392    * @return DOCUMENT ME!
393    */
394   public String getName()
395   {
396     return groupName;
397   }
398
399   public String getDescription()
400   {
401     return description;
402   }
403
404   /**
405    * DOCUMENT ME!
406    * 
407    * @param name
408    *          DOCUMENT ME!
409    */
410   public void setName(String name)
411   {
412     groupName = name;
413     // TODO: URGENT: update dependent objects (annotation row)
414   }
415
416   public void setDescription(String desc)
417   {
418     description = desc;
419   }
420
421   /**
422    * DOCUMENT ME!
423    * 
424    * @return DOCUMENT ME!
425    */
426   public Conservation getConservation()
427   {
428     return conserve;
429   }
430
431   /**
432    * DOCUMENT ME!
433    * 
434    * @param c
435    *          DOCUMENT ME!
436    */
437   public void setConservation(Conservation c)
438   {
439     conserve = c;
440   }
441
442   /**
443    * Add s to this sequence group. If aligment sequence is already contained in
444    * group, it will not be added again, but recalculation may happen if the flag
445    * is set.
446    * 
447    * @param s
448    *          alignment sequence to be added
449    * @param recalc
450    *          true means Group's conservation should be recalculated
451    */
452   public void addSequence(SequenceI s, boolean recalc)
453   {
454     if (s != null && !sequences.contains(s))
455     {
456       sequences.addElement(s);
457     }
458
459     if (recalc)
460     {
461       recalcConservation();
462     }
463   }
464
465   /**
466    * Max Gaps Threshold for performing a conservation calculation TODO: make
467    * this a configurable property - or global to an alignment view
468    */
469   private int consPercGaps = 25;
470
471   /**
472    * calculate residue conservation for group - but only if necessary.
473    */
474   public void recalcConservation()
475   {
476     if (cs == null && consensus == null && conservation == null)
477     {
478       return;
479     }
480
481     try
482     {
483       Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
484               endRes + 1, showSequenceLogo);
485       if (consensus != null)
486       {
487         _updateConsensusRow(cnsns);
488       }
489       if (cs != null)
490       {
491         cs.setConsensus(cnsns);
492
493         if (cs instanceof ClustalxColourScheme)
494         {
495           ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
496         }
497       }
498
499       if ((conservation != null)
500               || (cs != null && cs.conservationApplied()))
501       {
502         Conservation c = new Conservation(groupName,
503                 ResidueProperties.propHash, 3, sequences, startRes,
504                 endRes + 1);
505         c.calculate();
506         c.verdict(false, consPercGaps);
507         if (conservation != null)
508         {
509           _updateConservationRow(c);
510         }
511         if (cs != null)
512         {
513           if (cs.conservationApplied())
514           {
515             cs.setConservation(c);
516
517             if (cs instanceof ClustalxColourScheme)
518             {
519               ((ClustalxColourScheme) cs).resetClustalX(sequences,
520                       getWidth());
521             }
522           }
523         }
524       }
525     } catch (java.lang.OutOfMemoryError err)
526     {
527       // TODO: catch OOM
528       System.out.println("Out of memory loading groups: " + err);
529     }
530
531   }
532
533   private void _updateConservationRow(Conservation c)
534   {
535     if (conservation == null)
536     {
537       getConservation();
538     }
539     // update Labels
540     conservation.label = "Conservation for " + getName();
541     conservation.description = "Conservation for group " + getName()
542             + " less than " + consPercGaps + "% gaps";
543     // preserve width if already set
544     int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
545             : endRes + 1)
546             : endRes + 1;
547     conservation.annotations = null;
548     conservation.annotations = new Annotation[aWidth]; // should be alignment
549                                                        // width
550     c.completeAnnotations(conservation, null, startRes, endRes + 1);
551   }
552
553   public Hashtable[] consensusData = null;
554
555   private void _updateConsensusRow(Hashtable[] cnsns)
556   {
557     if (consensus == null)
558     {
559       getConsensus();
560     }
561     consensus.label = "Consensus for " + getName();
562     consensus.description = "Percent Identity";
563     consensusData = cnsns;
564     // preserve width if already set
565     int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
566             : endRes + 1)
567             : endRes + 1;
568     consensus.annotations = null;
569     consensus.annotations = new Annotation[aWidth]; // should be alignment width
570
571     AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
572             ignoreGapsInConsensus, showSequenceLogo); // TODO: setting container
573                                                       // for
574                                                       // ignoreGapsInConsensusCalculation);
575   }
576
577   /**
578    * @param s
579    *          sequence to either add or remove from group
580    * @param recalc
581    *          flag passed to delete/addSequence to indicate if group properties
582    *          should be recalculated
583    */
584   public void addOrRemove(SequenceI s, boolean recalc)
585   {
586     if (sequences.contains(s))
587     {
588       deleteSequence(s, recalc);
589     }
590     else
591     {
592       addSequence(s, recalc);
593     }
594   }
595
596   /**
597    * DOCUMENT ME!
598    * 
599    * @param s
600    *          DOCUMENT ME!
601    * @param recalc
602    *          DOCUMENT ME!
603    */
604   public void deleteSequence(SequenceI s, boolean recalc)
605   {
606     sequences.removeElement(s);
607
608     if (recalc)
609     {
610       recalcConservation();
611     }
612   }
613
614   /**
615    * DOCUMENT ME!
616    * 
617    * @return DOCUMENT ME!
618    */
619   public int getStartRes()
620   {
621     return startRes;
622   }
623
624   /**
625    * DOCUMENT ME!
626    * 
627    * @return DOCUMENT ME!
628    */
629   public int getEndRes()
630   {
631     return endRes;
632   }
633
634   /**
635    * Set the first column selected by this group. Runs from 0<=i<N_cols
636    * 
637    * @param i
638    */
639   public void setStartRes(int i)
640   {
641     startRes = i;
642   }
643
644   /**
645    * Set the groups last selected column. Runs from 0<=i<N_cols
646    * 
647    * @param i
648    */
649   public void setEndRes(int i)
650   {
651     endRes = i;
652   }
653
654   /**
655    * DOCUMENT ME!
656    * 
657    * @return DOCUMENT ME!
658    */
659   public int getSize()
660   {
661     return sequences.size();
662   }
663
664   /**
665    * DOCUMENT ME!
666    * 
667    * @param i
668    *          DOCUMENT ME!
669    * 
670    * @return DOCUMENT ME!
671    */
672   public SequenceI getSequenceAt(int i)
673   {
674     return (SequenceI) sequences.elementAt(i);
675   }
676
677   /**
678    * DOCUMENT ME!
679    * 
680    * @param state
681    *          DOCUMENT ME!
682    */
683   public void setColourText(boolean state)
684   {
685     colourText = state;
686   }
687
688   /**
689    * DOCUMENT ME!
690    * 
691    * @return DOCUMENT ME!
692    */
693   public boolean getColourText()
694   {
695     return colourText;
696   }
697
698   /**
699    * DOCUMENT ME!
700    * 
701    * @param state
702    *          DOCUMENT ME!
703    */
704   public void setDisplayText(boolean state)
705   {
706     displayText = state;
707   }
708
709   /**
710    * DOCUMENT ME!
711    * 
712    * @return DOCUMENT ME!
713    */
714   public boolean getDisplayText()
715   {
716     return displayText;
717   }
718
719   /**
720    * DOCUMENT ME!
721    * 
722    * @param state
723    *          DOCUMENT ME!
724    */
725   public void setDisplayBoxes(boolean state)
726   {
727     displayBoxes = state;
728   }
729
730   /**
731    * DOCUMENT ME!
732    * 
733    * @return DOCUMENT ME!
734    */
735   public boolean getDisplayBoxes()
736   {
737     return displayBoxes;
738   }
739
740   /**
741    * DOCUMENT ME!
742    * 
743    * @return DOCUMENT ME!
744    */
745   public int getWidth()
746   {
747     // MC This needs to get reset when characters are inserted and deleted
748     if (sequences.size() > 0)
749     {
750       width = ((SequenceI) sequences.elementAt(0)).getLength();
751     }
752
753     for (int i = 1; i < sequences.size(); i++)
754     {
755       SequenceI seq = (SequenceI) sequences.elementAt(i);
756
757       if (seq.getLength() > width)
758       {
759         width = seq.getLength();
760       }
761     }
762
763     return width;
764   }
765
766   /**
767    * DOCUMENT ME!
768    * 
769    * @param c
770    *          DOCUMENT ME!
771    */
772   public void setOutlineColour(Color c)
773   {
774     outlineColour = c;
775   }
776
777   /**
778    * DOCUMENT ME!
779    * 
780    * @return DOCUMENT ME!
781    */
782   public Color getOutlineColour()
783   {
784     return outlineColour;
785   }
786
787   /**
788    * 
789    * returns the sequences in the group ordered by the ordering given by al.
790    * this used to return an array with null entries regardless, new behaviour is
791    * below. TODO: verify that this does not affect use in applet or application
792    * 
793    * @param al
794    *          Alignment
795    * @return SequenceI[] intersection of sequences in group with al, ordered by
796    *         al, or null if group does not intersect with al
797    */
798   public SequenceI[] getSequencesInOrder(AlignmentI al)
799   {
800     return getSequencesInOrder(al, true);
801   }
802
803   /**
804    * return an array representing the intersection of the group with al,
805    * optionally returning an array the size of al.getHeight() where nulls mark
806    * the non-intersected sequences
807    * 
808    * @param al
809    * @param trim
810    * @return null or array
811    */
812   public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
813   {
814     int sSize = sequences.size();
815     int alHeight = al.getHeight();
816
817     SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
818
819     int index = 0;
820     for (int i = 0; i < alHeight && index < sSize; i++)
821     {
822       if (sequences.contains(al.getSequenceAt(i)))
823       {
824         seqs[(trim) ? index : i] = al.getSequenceAt(i);
825         index++;
826       }
827     }
828     if (index == 0)
829     {
830       return null;
831     }
832     if (!trim)
833     {
834       return seqs;
835     }
836     if (index < seqs.length)
837     {
838       SequenceI[] dummy = seqs;
839       seqs = new SequenceI[index];
840       while (--index >= 0)
841       {
842         seqs[index] = dummy[index];
843         dummy[index] = null;
844       }
845     }
846     return seqs;
847   }
848
849   /**
850    * @return the idColour
851    */
852   public Color getIdColour()
853   {
854     return idColour;
855   }
856
857   /**
858    * @param idColour
859    *          the idColour to set
860    */
861   public void setIdColour(Color idColour)
862   {
863     this.idColour = idColour;
864   }
865
866   /**
867    * @return the representative sequence for this group
868    */
869   public SequenceI getSeqrep()
870   {
871     return seqrep;
872   }
873
874   /**
875    * set the representative sequence for this group. Note - this affects the
876    * interpretation of the Hidereps attribute.
877    * 
878    * @param seqrep
879    *          the seqrep to set (null means no sequence representative)
880    */
881   public void setSeqrep(SequenceI seqrep)
882   {
883     this.seqrep = seqrep;
884   }
885
886   /**
887    * 
888    * @return true if group has a sequence representative
889    */
890   public boolean hasSeqrep()
891   {
892     return seqrep != null;
893   }
894
895   /**
896    * visibility of rows or represented rows covered by group
897    */
898   private boolean hidereps = false;
899
900   /**
901    * set visibility of sequences covered by (if no sequence representative is
902    * defined) or represented by this group.
903    * 
904    * @param visibility
905    */
906   public void setHidereps(boolean visibility)
907   {
908     hidereps = visibility;
909   }
910
911   /**
912    * 
913    * @return true if sequences represented (or covered) by this group should be
914    *         hidden
915    */
916   public boolean isHidereps()
917   {
918     return hidereps;
919   }
920
921   /**
922    * visibility of columns intersecting this group
923    */
924   private boolean hidecols = false;
925
926   /**
927    * set intended visibility of columns covered by this group
928    * 
929    * @param visibility
930    */
931   public void setHideCols(boolean visibility)
932   {
933     hidecols = visibility;
934   }
935
936   /**
937    * 
938    * @return true if columns covered by group should be hidden
939    */
940   public boolean isHideCols()
941   {
942     return hidecols;
943   }
944
945   /**
946    * create a new sequence group from the intersection of this group with an
947    * alignment Hashtable of hidden representatives
948    * 
949    * @param alignment
950    *          (may not be null)
951    * @param hashtable
952    *          (may be null)
953    * @return new group containing sequences common to this group and alignment
954    */
955   public SequenceGroup intersect(AlignmentI alignment, Hashtable hashtable)
956   {
957     SequenceGroup sgroup = new SequenceGroup(this);
958     SequenceI[] insect = getSequencesInOrder(alignment);
959     sgroup.sequences = new Vector();
960     for (int s = 0; insect != null && s < insect.length; s++)
961     {
962       if (hashtable == null || hashtable.containsKey(insect[s]))
963       {
964         sgroup.sequences.addElement(insect[s]);
965       }
966     }
967     // Enumeration en =getSequences(hashtable).elements();
968     // while (en.hasMoreElements())
969     // {
970     // SequenceI elem = (SequenceI) en.nextElement();
971     // if (alignment.getSequences().contains(elem))
972     // {
973     // sgroup.addSequence(elem, false);
974     // }
975     // }
976     return sgroup;
977   }
978
979   /**
980    * @return the showUnconserved
981    */
982   public boolean getShowNonconserved()
983   {
984     return showNonconserved;
985   }
986
987   /**
988    * @param showNonconserved
989    *          the showUnconserved to set
990    */
991   public void setShowNonconserved(boolean displayNonconserved)
992   {
993     this.showNonconserved = displayNonconserved;
994   }
995
996   AlignmentAnnotation consensus = null, conservation = null;
997
998   /**
999    * flag indicating if consensus histogram should be rendered
1000    */
1001   private boolean showConsensusHistogram;
1002
1003   /**
1004    * set this alignmentAnnotation object as the one used to render consensus
1005    * annotation
1006    * 
1007    * @param aan
1008    */
1009   public void setConsensus(AlignmentAnnotation aan)
1010   {
1011     if (consensus == null)
1012     {
1013       consensus = aan;
1014     }
1015   }
1016
1017   /**
1018    * 
1019    * @return automatically calculated consensus row
1020    */
1021   public AlignmentAnnotation getConsensus()
1022   {
1023     // TODO get or calculate and get consensus annotation row for this group
1024     int aWidth = this.getWidth();
1025     // pointer
1026     // possibility
1027     // here.
1028     if (aWidth < 0)
1029     {
1030       return null;
1031     }
1032     if (consensus == null)
1033     {
1034       consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1035               100f, AlignmentAnnotation.BAR_GRAPH);
1036     }
1037     consensus.hasText = true;
1038     consensus.autoCalculated = true;
1039     consensus.groupRef = this;
1040     consensus.label = "Consensus for " + getName();
1041     consensus.description = "Percent Identity";
1042     return consensus;
1043   }
1044
1045   /**
1046    * set this alignmentAnnotation object as the one used to render consensus
1047    * annotation
1048    * 
1049    * @param aan
1050    */
1051   public void setConservationRow(AlignmentAnnotation aan)
1052   {
1053     if (conservation == null)
1054     {
1055       conservation = aan;
1056     }
1057   }
1058
1059   /**
1060    * get the conservation annotation row for this group
1061    * 
1062    * @return autoCalculated annotation row
1063    */
1064   public AlignmentAnnotation getConservationRow()
1065   {
1066     if (conservation == null)
1067     {
1068       conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1069               11f, AlignmentAnnotation.BAR_GRAPH);
1070     }
1071
1072     conservation.hasText = true;
1073     conservation.autoCalculated = true;
1074     conservation.groupRef = this;
1075     conservation.label = "Conservation for " + getName();
1076     conservation.description = "Conservation for group " + getName()
1077             + " less than " + consPercGaps + "% gaps";
1078     return conservation;
1079   }
1080
1081   /**
1082    * 
1083    * @return true if annotation rows have been instantiated for this group
1084    */
1085   public boolean hasAnnotationRows()
1086   {
1087     return consensus != null || conservation != null;
1088   }
1089
1090   public SequenceI getConsensusSeq()
1091   {
1092     getConsensus();
1093     StringBuffer seqs = new StringBuffer();
1094     for (int i = 0; i < consensus.annotations.length; i++)
1095     {
1096       if (consensus.annotations[i] != null)
1097       {
1098         if (consensus.annotations[i].description.charAt(0) == '[')
1099         {
1100           seqs.append(consensus.annotations[i].description.charAt(1));
1101         }
1102         else
1103         {
1104           seqs.append(consensus.annotations[i].displayCharacter);
1105         }
1106       }
1107     }
1108
1109     SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1110             seqs.toString());
1111     sq.setDescription("Percentage Identity Consensus "
1112             + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1113     return sq;
1114   }
1115
1116   public void setIgnoreGapsConsensus(boolean state)
1117   {
1118     if (this.ignoreGapsInConsensus != state && consensus != null)
1119     {
1120       ignoreGapsInConsensus = state;
1121       recalcConservation();
1122     }
1123     ignoreGapsInConsensus = state;
1124   }
1125
1126   public boolean getIgnoreGapsConsensus()
1127   {
1128     return ignoreGapsInConsensus;
1129   }
1130
1131   /**
1132    * @param showSequenceLogo
1133    *          indicates if a sequence logo is shown for consensus annotation
1134    */
1135   public void setshowSequenceLogo(boolean showSequenceLogo)
1136   {
1137     // TODO: decouple calculation from settings update
1138     if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1139     {
1140       this.showSequenceLogo = showSequenceLogo;
1141       recalcConservation();
1142     }
1143     this.showSequenceLogo = showSequenceLogo;
1144   }
1145
1146   /**
1147    * 
1148    * @param showConsHist
1149    *          flag indicating if the consensus histogram for this group should
1150    *          be rendered
1151    */
1152   public void setShowConsensusHistogram(boolean showConsHist)
1153   {
1154
1155     if (showConsensusHistogram != showConsHist && consensus != null)
1156     {
1157       this.showConsensusHistogram = showConsHist;
1158       recalcConservation();
1159     }
1160     this.showConsensusHistogram = showConsHist;
1161   }
1162
1163   /**
1164    * @return the showConsensusHistogram
1165    */
1166   public boolean isShowConsensusHistogram()
1167   {
1168     return showConsensusHistogram;
1169   }
1170 }