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