f3cf291d1371d0d9299fd267f72d587086100777
[jalview.git] / src / jalview / datamodel / SequenceGroup.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 import java.util.*;
24 import java.util.List;
25
26 import java.awt.*;
27
28 import jalview.analysis.*;
29 import jalview.schemes.*;
30
31 /**
32  * Collects a set contiguous ranges on a set of sequences
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37 public class SequenceGroup implements AnnotatedCollectionI
38 {
39   String groupName;
40
41   String description;
42
43   Conservation conserve;
44
45   Vector aaFrequency;
46
47   boolean displayBoxes = true;
48
49   boolean displayText = true;
50
51   boolean colourText = false;
52
53   /**
54    * after Olivier's non-conserved only character display
55    */
56   boolean showNonconserved = false;
57
58   /**
59    * group members
60    */
61   private Vector<SequenceI> sequences = new Vector<SequenceI>();
62
63   /**
64    * representative sequence for this group (if any)
65    */
66   private SequenceI seqrep = null;
67
68   int width = -1;
69
70   /**
71    * Colourscheme applied to group if any
72    */
73   public ColourSchemeI cs;
74
75   int startRes = 0;
76
77   int endRes = 0;
78
79   public Color outlineColour = Color.black;
80
81   public Color idColour = null;
82
83   public int thresholdTextColour = 0;
84
85   public Color textColour = Color.black;
86
87   public Color textColour2 = Color.white;
88
89   /**
90    * consensus calculation property
91    */
92   private boolean ignoreGapsInConsensus = true;
93
94   /**
95    * consensus calculation property
96    */
97   private boolean showSequenceLogo = false;
98
99   /**
100    * flag indicating if logo should be rendered normalised
101    */
102   private boolean normaliseSequenceLogo;
103
104   /**
105    * @return the includeAllConsSymbols
106    */
107   public boolean isShowSequenceLogo()
108   {
109     return showSequenceLogo;
110   }
111
112   /**
113    * Creates a new SequenceGroup object.
114    */
115   public SequenceGroup()
116   {
117     groupName = "JGroup:" + this.hashCode();
118   }
119
120   /**
121    * Creates a new SequenceGroup object.
122    * 
123    * @param sequences
124    * @param groupName
125    * @param scheme
126    * @param displayBoxes
127    * @param displayText
128    * @param colourText
129    * @param start
130    *          first column of group
131    * @param end
132    *          last column of group
133    */
134   public SequenceGroup(Vector sequences, String groupName,
135           ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
136           boolean colourText, int start, int end)
137   {
138     this.sequences = sequences;
139     this.groupName = groupName;
140     this.displayBoxes = displayBoxes;
141     this.displayText = displayText;
142     this.colourText = colourText;
143     this.cs = scheme;
144     startRes = start;
145     endRes = end;
146     recalcConservation();
147   }
148
149   /**
150    * copy constructor
151    * 
152    * @param seqsel
153    */
154   public SequenceGroup(SequenceGroup seqsel)
155   {
156     if (seqsel != null)
157     {
158       sequences = new Vector();
159       Enumeration<SequenceI> sq = seqsel.sequences.elements();
160       while (sq.hasMoreElements())
161       {
162         sequences.addElement(sq.nextElement());
163       }
164       ;
165       if (seqsel.groupName != null)
166       {
167         groupName = new String(seqsel.groupName);
168       }
169       displayBoxes = seqsel.displayBoxes;
170       displayText = seqsel.displayText;
171       colourText = seqsel.colourText;
172       startRes = seqsel.startRes;
173       endRes = seqsel.endRes;
174       cs = seqsel.cs;
175       if (seqsel.description != null)
176         description = new String(seqsel.description);
177       hidecols = seqsel.hidecols;
178       hidereps = seqsel.hidereps;
179       idColour = seqsel.idColour;
180       outlineColour = seqsel.outlineColour;
181       seqrep = seqsel.seqrep;
182       textColour = seqsel.textColour;
183       textColour2 = seqsel.textColour2;
184       thresholdTextColour = seqsel.thresholdTextColour;
185       width = seqsel.width;
186       ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
187       if (seqsel.conserve != null)
188       {
189         recalcConservation(); // safer than
190         // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
191       }
192     }
193   }
194
195   public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
196   {
197     int iSize = sequences.size();
198     SequenceI[] seqs = new SequenceI[iSize];
199     SequenceI[] inorder = getSequencesInOrder(align);
200
201     for (int i = 0, ipos = 0; i < inorder.length; i++)
202     {
203       SequenceI seq = inorder[i];
204
205       seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
206       if (seqs[ipos] != null)
207       {
208         seqs[ipos].setDescription(seq.getDescription());
209         seqs[ipos].setDBRef(seq.getDBRef());
210         seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
211         if (seq.getDatasetSequence() != null)
212         {
213           seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
214         }
215
216         if (seq.getAnnotation() != null)
217         {
218           AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
219           // Only copy annotation that is either a score or referenced by the
220           // alignment's annotation vector
221           for (int a = 0; a < seq.getAnnotation().length; a++)
222           {
223             AlignmentAnnotation tocopy = seq.getAnnotation()[a];
224             if (alann != null)
225             {
226               boolean found = false;
227               for (int pos = 0; pos < alann.length; pos++)
228               {
229                 if (alann[pos] == tocopy)
230                 {
231                   found = true;
232                   break;
233                 }
234               }
235               if (!found)
236                 continue;
237             }
238             AlignmentAnnotation newannot = new AlignmentAnnotation(
239                     seq.getAnnotation()[a]);
240             newannot.restrict(startRes, endRes);
241             newannot.setSequenceRef(seqs[ipos]);
242             newannot.adjustForAlignment();
243             seqs[ipos].addAlignmentAnnotation(newannot);
244           }
245         }
246         ipos++;
247       }
248       else
249       {
250         iSize--;
251       }
252     }
253     if (iSize != inorder.length)
254     {
255       SequenceI[] nseqs = new SequenceI[iSize];
256       System.arraycopy(seqs, 0, nseqs, 0, iSize);
257       seqs = nseqs;
258     }
259     return seqs;
260
261   }
262
263   /**
264    * If sequence ends in gaps, the end residue can be correctly calculated here
265    * 
266    * @param seq
267    *          SequenceI
268    * @return int
269    */
270   public int findEndRes(SequenceI seq)
271   {
272     int eres = 0;
273     char ch;
274
275     for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
276     {
277       ch = seq.getCharAt(j);
278       if (!jalview.util.Comparison.isGap((ch)))
279       {
280         eres++;
281       }
282     }
283
284     if (eres > 0)
285     {
286       eres += seq.getStart() - 1;
287     }
288
289     return eres;
290   }
291
292   public List<SequenceI> getSequences()
293   {
294     return sequences;
295   }
296
297   public List<SequenceI> getSequences(
298           Map<SequenceI, SequenceCollectionI> hiddenReps)
299   {
300     if (hiddenReps == null)
301     {
302       return sequences;
303     }
304     else
305     {
306       Vector allSequences = new Vector();
307       SequenceI seq;
308       for (int i = 0; i < sequences.size(); i++)
309       {
310         seq = (SequenceI) sequences.elementAt(i);
311         allSequences.addElement(seq);
312         if (hiddenReps.containsKey(seq))
313         {
314           SequenceCollectionI hsg = hiddenReps.get(seq);
315           for (SequenceI seq2 : hsg.getSequences())
316           {
317             if (seq2 != seq && !allSequences.contains(seq2))
318             {
319               allSequences.addElement(seq2);
320             }
321           }
322         }
323       }
324
325       return allSequences;
326     }
327   }
328
329   public SequenceI[] getSequencesAsArray(
330           Map<SequenceI, SequenceCollectionI> map)
331   {
332     List<SequenceI> tmp = getSequences(map);
333     if (tmp == null)
334     {
335       return null;
336     }
337     return tmp.toArray(new SequenceI[tmp.size()]);
338   }
339
340   /**
341    * DOCUMENT ME!
342    * 
343    * @param col
344    *          DOCUMENT ME!
345    * 
346    * @return DOCUMENT ME!
347    */
348   public boolean adjustForRemoveLeft(int col)
349   {
350     // return value is true if the group still exists
351     if (startRes >= col)
352     {
353       startRes = startRes - col;
354     }
355
356     if (endRes >= col)
357     {
358       endRes = endRes - col;
359
360       if (startRes > endRes)
361       {
362         startRes = 0;
363       }
364     }
365     else
366     {
367       // must delete this group!!
368       return false;
369     }
370
371     return true;
372   }
373
374   /**
375    * DOCUMENT ME!
376    * 
377    * @param col
378    *          DOCUMENT ME!
379    * 
380    * @return DOCUMENT ME!
381    */
382   public boolean adjustForRemoveRight(int col)
383   {
384     if (startRes > col)
385     {
386       // delete this group
387       return false;
388     }
389
390     if (endRes >= col)
391     {
392       endRes = col;
393     }
394
395     return true;
396   }
397
398   /**
399    * DOCUMENT ME!
400    * 
401    * @return DOCUMENT ME!
402    */
403   public String getName()
404   {
405     return groupName;
406   }
407
408   public String getDescription()
409   {
410     return description;
411   }
412
413   /**
414    * DOCUMENT ME!
415    * 
416    * @param name
417    *          DOCUMENT ME!
418    */
419   public void setName(String name)
420   {
421     groupName = name;
422     // TODO: URGENT: update dependent objects (annotation row)
423   }
424
425   public void setDescription(String desc)
426   {
427     description = desc;
428   }
429
430   /**
431    * DOCUMENT ME!
432    * 
433    * @return DOCUMENT ME!
434    */
435   public Conservation getConservation()
436   {
437     return conserve;
438   }
439
440   /**
441    * DOCUMENT ME!
442    * 
443    * @param c
444    *          DOCUMENT ME!
445    */
446   public void setConservation(Conservation c)
447   {
448     conserve = c;
449   }
450
451   /**
452    * Add s to this sequence group. If aligment sequence is already contained in
453    * group, it will not be added again, but recalculation may happen if the flag
454    * is set.
455    * 
456    * @param s
457    *          alignment sequence to be added
458    * @param recalc
459    *          true means Group's conservation should be recalculated
460    */
461   public void addSequence(SequenceI s, boolean recalc)
462   {
463     if (s != null && !sequences.contains(s))
464     {
465       sequences.addElement(s);
466     }
467
468     if (recalc)
469     {
470       recalcConservation();
471     }
472   }
473
474   /**
475    * Max Gaps Threshold (percent) for performing a conservation calculation 
476    */
477   private int consPercGaps = 25;
478
479   /**
480    * @return Max Gaps Threshold for performing a conservation calculation
481    */
482   public int getConsPercGaps()
483   {
484     return consPercGaps;
485   }
486
487   /**
488    * set Max Gaps Threshold (percent) for performing a conservation calculation
489    * @param consPercGaps 
490    */
491   public void setConsPercGaps(int consPercGaps)
492   {
493     this.consPercGaps = consPercGaps;
494   }
495
496   /**
497    * calculate residue conservation for group - but only if necessary.
498    */
499   public void recalcConservation()
500   {
501     if (cs == null && consensus == null && conservation == null)
502     {
503       return;
504     }
505     try
506     {
507       Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
508               endRes + 1, showSequenceLogo);
509       if (consensus != null)
510       {
511         _updateConsensusRow(cnsns, sequences.size());
512       }
513       if (cs != null)
514       {
515         cs.setConsensus(cnsns);
516       }
517
518       if ((conservation != null)
519               || (cs != null && cs.conservationApplied()))
520       {
521         Conservation c = new Conservation(groupName,
522                 ResidueProperties.propHash, 3, sequences, startRes,
523                 endRes + 1);
524         c.calculate();
525         c.verdict(false, consPercGaps);
526         if (conservation != null)
527         {
528           _updateConservationRow(c);
529         }
530         if (cs != null)
531         {
532           if (cs.conservationApplied())
533           {
534             cs.setConservation(c);
535           }
536         }
537       }
538       if (cs != null)
539       {
540         cs.alignmentChanged(context!=null ? context : this, null);
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       AlignmentAnnotation[] aa = seq.getAnnotation();
1215       if (aa != null)
1216       {
1217         for (AlignmentAnnotation al : aa)
1218         {
1219           if (al.groupRef == this)
1220           {
1221             annot.add(al);
1222           }
1223         }
1224       }
1225     }
1226     if (consensus != null)
1227     {
1228       annot.add(consensus);
1229     }
1230     if (conservation != null)
1231     {
1232       annot.add(conservation);
1233     }
1234     return annot.toArray(new AlignmentAnnotation[0]);
1235   }
1236
1237   @Override
1238   public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1239   {
1240     ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1241     for (AlignmentAnnotation a : getAlignmentAnnotation())
1242     {
1243       if (a.getCalcId() == calcId)
1244       {
1245         aa.add(a);
1246       }
1247     }
1248     return aa;
1249   }
1250
1251   public void clear()
1252   {
1253     sequences.clear();
1254   }
1255   private AnnotatedCollectionI context;
1256   /**
1257    * set the alignment or group context for this group
1258    * @param context
1259    */
1260   public void setContext(AnnotatedCollectionI context)
1261   {
1262     this.context = context; 
1263   }
1264   /* (non-Javadoc)
1265    * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1266    */
1267   @Override
1268   public AnnotatedCollectionI getContext()
1269   {
1270     return context;
1271   }
1272 }