a8b0ace9188402e8d4be1af115af124d48f459cb
[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     try
504     {
505       Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
506               endRes + 1, showSequenceLogo);
507       if (consensus != null)
508       {
509         _updateConsensusRow(cnsns, sequences.size());
510       }
511       if (cs != null)
512       {
513         cs.setConsensus(cnsns);
514       }
515
516       if ((conservation != null)
517               || (cs != null && cs.conservationApplied()))
518       {
519         Conservation c = new Conservation(groupName,
520                 ResidueProperties.propHash, 3, sequences, startRes,
521                 endRes + 1);
522         c.calculate();
523         c.verdict(false, consPercGaps);
524         if (conservation != null)
525         {
526           _updateConservationRow(c);
527         }
528         if (cs != null)
529         {
530           if (cs.conservationApplied())
531           {
532             cs.setConservation(c);
533           }
534         }
535       }
536       if (cs != null)
537       {
538         cs.alignmentChanged(context!=null ? context : this, null);
539       }
540     } catch (java.lang.OutOfMemoryError err)
541     {
542       // TODO: catch OOM
543       System.out.println("Out of memory loading groups: " + err);
544     }
545
546   }
547
548   private void _updateConservationRow(Conservation c)
549   {
550     if (conservation == null)
551     {
552       getConservation();
553     }
554     // update Labels
555     conservation.label = "Conservation for " + getName();
556     conservation.description = "Conservation for group " + getName()
557             + " less than " + consPercGaps + "% gaps";
558     // preserve width if already set
559     int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
560             : endRes + 1)
561             : endRes + 1;
562     conservation.annotations = null;
563     conservation.annotations = new Annotation[aWidth]; // should be alignment
564                                                        // width
565     c.completeAnnotations(conservation, null, startRes, endRes + 1);
566   }
567
568   public Hashtable[] consensusData = null;
569
570   private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
571   {
572     if (consensus == null)
573     {
574       getConsensus();
575     }
576     consensus.label = "Consensus for " + getName();
577     consensus.description = "Percent Identity";
578     consensusData = cnsns;
579     // preserve width if already set
580     int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
581             : endRes + 1)
582             : endRes + 1;
583     consensus.annotations = null;
584     consensus.annotations = new Annotation[aWidth]; // should be alignment width
585
586     AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
587             ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting container
588                                                       // for
589                                                       // ignoreGapsInConsensusCalculation);
590   }
591
592   /**
593    * @param s
594    *          sequence to either add or remove from group
595    * @param recalc
596    *          flag passed to delete/addSequence to indicate if group properties
597    *          should be recalculated
598    */
599   public void addOrRemove(SequenceI s, boolean recalc)
600   {
601     if (sequences.contains(s))
602     {
603       deleteSequence(s, recalc);
604     }
605     else
606     {
607       addSequence(s, recalc);
608     }
609   }
610
611   /**
612    * DOCUMENT ME!
613    * 
614    * @param s
615    *          DOCUMENT ME!
616    * @param recalc
617    *          DOCUMENT ME!
618    */
619   public void deleteSequence(SequenceI s, boolean recalc)
620   {
621     sequences.removeElement(s);
622
623     if (recalc)
624     {
625       recalcConservation();
626     }
627   }
628
629   /**
630    * 
631    * 
632    * @return the first column selected by this group. Runs from 0<=i<N_cols
633    */
634   public int getStartRes()
635   {
636     return startRes;
637   }
638
639   /**
640    * 
641    * @return the groups last selected column. Runs from 0<=i<N_cols
642    */
643   public int getEndRes()
644   {
645     return endRes;
646   }
647
648   /**
649    * Set the first column selected by this group. Runs from 0<=i<N_cols
650    * 
651    * @param i
652    */
653   public void setStartRes(int i)
654   {
655     startRes = i;
656   }
657
658   /**
659    * Set the groups last selected column. Runs from 0<=i<N_cols
660    * 
661    * @param i
662    */
663   public void setEndRes(int i)
664   {
665     endRes = i;
666   }
667
668   /**
669    * DOCUMENT ME!
670    * 
671    * @return DOCUMENT ME!
672    */
673   public int getSize()
674   {
675     return sequences.size();
676   }
677
678   /**
679    * DOCUMENT ME!
680    * 
681    * @param i
682    *          DOCUMENT ME!
683    * 
684    * @return DOCUMENT ME!
685    */
686   public SequenceI getSequenceAt(int i)
687   {
688     return (SequenceI) sequences.elementAt(i);
689   }
690
691   /**
692    * DOCUMENT ME!
693    * 
694    * @param state
695    *          DOCUMENT ME!
696    */
697   public void setColourText(boolean state)
698   {
699     colourText = state;
700   }
701
702   /**
703    * DOCUMENT ME!
704    * 
705    * @return DOCUMENT ME!
706    */
707   public boolean getColourText()
708   {
709     return colourText;
710   }
711
712   /**
713    * DOCUMENT ME!
714    * 
715    * @param state
716    *          DOCUMENT ME!
717    */
718   public void setDisplayText(boolean state)
719   {
720     displayText = state;
721   }
722
723   /**
724    * DOCUMENT ME!
725    * 
726    * @return DOCUMENT ME!
727    */
728   public boolean getDisplayText()
729   {
730     return displayText;
731   }
732
733   /**
734    * DOCUMENT ME!
735    * 
736    * @param state
737    *          DOCUMENT ME!
738    */
739   public void setDisplayBoxes(boolean state)
740   {
741     displayBoxes = state;
742   }
743
744   /**
745    * DOCUMENT ME!
746    * 
747    * @return DOCUMENT ME!
748    */
749   public boolean getDisplayBoxes()
750   {
751     return displayBoxes;
752   }
753
754   /**
755    * DOCUMENT ME!
756    * 
757    * @return DOCUMENT ME!
758    */
759   public int getWidth()
760   {
761     // MC This needs to get reset when characters are inserted and deleted
762     if (sequences.size() > 0)
763     {
764       width = ((SequenceI) sequences.elementAt(0)).getLength();
765     }
766
767     for (int i = 1; i < sequences.size(); i++)
768     {
769       SequenceI seq = (SequenceI) sequences.elementAt(i);
770
771       if (seq.getLength() > width)
772       {
773         width = seq.getLength();
774       }
775     }
776
777     return width;
778   }
779
780   /**
781    * DOCUMENT ME!
782    * 
783    * @param c
784    *          DOCUMENT ME!
785    */
786   public void setOutlineColour(Color c)
787   {
788     outlineColour = c;
789   }
790
791   /**
792    * DOCUMENT ME!
793    * 
794    * @return DOCUMENT ME!
795    */
796   public Color getOutlineColour()
797   {
798     return outlineColour;
799   }
800
801   /**
802    * 
803    * returns the sequences in the group ordered by the ordering given by al.
804    * this used to return an array with null entries regardless, new behaviour is
805    * below. TODO: verify that this does not affect use in applet or application
806    * 
807    * @param al
808    *          Alignment
809    * @return SequenceI[] intersection of sequences in group with al, ordered by
810    *         al, or null if group does not intersect with al
811    */
812   public SequenceI[] getSequencesInOrder(AlignmentI al)
813   {
814     return getSequencesInOrder(al, true);
815   }
816
817   /**
818    * return an array representing the intersection of the group with al,
819    * optionally returning an array the size of al.getHeight() where nulls mark
820    * the non-intersected sequences
821    * 
822    * @param al
823    * @param trim
824    * @return null or array
825    */
826   public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
827   {
828     int sSize = sequences.size();
829     int alHeight = al.getHeight();
830
831     SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
832
833     int index = 0;
834     for (int i = 0; i < alHeight && index < sSize; i++)
835     {
836       if (sequences.contains(al.getSequenceAt(i)))
837       {
838         seqs[(trim) ? index : i] = al.getSequenceAt(i);
839         index++;
840       }
841     }
842     if (index == 0)
843     {
844       return null;
845     }
846     if (!trim)
847     {
848       return seqs;
849     }
850     if (index < seqs.length)
851     {
852       SequenceI[] dummy = seqs;
853       seqs = new SequenceI[index];
854       while (--index >= 0)
855       {
856         seqs[index] = dummy[index];
857         dummy[index] = null;
858       }
859     }
860     return seqs;
861   }
862
863   /**
864    * @return the idColour
865    */
866   public Color getIdColour()
867   {
868     return idColour;
869   }
870
871   /**
872    * @param idColour
873    *          the idColour to set
874    */
875   public void setIdColour(Color idColour)
876   {
877     this.idColour = idColour;
878   }
879
880   /**
881    * @return the representative sequence for this group
882    */
883   public SequenceI getSeqrep()
884   {
885     return seqrep;
886   }
887
888   /**
889    * set the representative sequence for this group. Note - this affects the
890    * interpretation of the Hidereps attribute.
891    * 
892    * @param seqrep
893    *          the seqrep to set (null means no sequence representative)
894    */
895   public void setSeqrep(SequenceI seqrep)
896   {
897     this.seqrep = seqrep;
898   }
899
900   /**
901    * 
902    * @return true if group has a sequence representative
903    */
904   public boolean hasSeqrep()
905   {
906     return seqrep != null;
907   }
908
909   /**
910    * visibility of rows or represented rows covered by group
911    */
912   private boolean hidereps = false;
913
914   /**
915    * set visibility of sequences covered by (if no sequence representative is
916    * defined) or represented by this group.
917    * 
918    * @param visibility
919    */
920   public void setHidereps(boolean visibility)
921   {
922     hidereps = visibility;
923   }
924
925   /**
926    * 
927    * @return true if sequences represented (or covered) by this group should be
928    *         hidden
929    */
930   public boolean isHidereps()
931   {
932     return hidereps;
933   }
934
935   /**
936    * visibility of columns intersecting this group
937    */
938   private boolean hidecols = false;
939
940   /**
941    * set intended visibility of columns covered by this group
942    * 
943    * @param visibility
944    */
945   public void setHideCols(boolean visibility)
946   {
947     hidecols = visibility;
948   }
949
950   /**
951    * 
952    * @return true if columns covered by group should be hidden
953    */
954   public boolean isHideCols()
955   {
956     return hidecols;
957   }
958
959   /**
960    * create a new sequence group from the intersection of this group with an
961    * alignment Hashtable of hidden representatives
962    * 
963    * @param alignment
964    *          (may not be null)
965    * @param map
966    *          (may be null)
967    * @return new group containing sequences common to this group and alignment
968    */
969   public SequenceGroup intersect(AlignmentI alignment,
970           Map<SequenceI, SequenceCollectionI> map)
971   {
972     SequenceGroup sgroup = new SequenceGroup(this);
973     SequenceI[] insect = getSequencesInOrder(alignment);
974     sgroup.sequences = new Vector();
975     for (int s = 0; insect != null && s < insect.length; s++)
976     {
977       if (map == null || map.containsKey(insect[s]))
978       {
979         sgroup.sequences.addElement(insect[s]);
980       }
981     }
982     // Enumeration en =getSequences(hashtable).elements();
983     // while (en.hasMoreElements())
984     // {
985     // SequenceI elem = (SequenceI) en.nextElement();
986     // if (alignment.getSequences().contains(elem))
987     // {
988     // sgroup.addSequence(elem, false);
989     // }
990     // }
991     return sgroup;
992   }
993
994   /**
995    * @return the showUnconserved
996    */
997   public boolean getShowNonconserved()
998   {
999     return showNonconserved;
1000   }
1001
1002   /**
1003    * @param showNonconserved
1004    *          the showUnconserved to set
1005    */
1006   public void setShowNonconserved(boolean displayNonconserved)
1007   {
1008     this.showNonconserved = displayNonconserved;
1009   }
1010
1011   AlignmentAnnotation consensus = null, conservation = null;
1012
1013   /**
1014    * flag indicating if consensus histogram should be rendered
1015    */
1016   private boolean showConsensusHistogram;
1017
1018   /**
1019    * set this alignmentAnnotation object as the one used to render consensus
1020    * annotation
1021    * 
1022    * @param aan
1023    */
1024   public void setConsensus(AlignmentAnnotation aan)
1025   {
1026     if (consensus == null)
1027     {
1028       consensus = aan;
1029     }
1030   }
1031
1032   /**
1033    * 
1034    * @return automatically calculated consensus row
1035    */
1036   public AlignmentAnnotation getConsensus()
1037   {
1038     // TODO get or calculate and get consensus annotation row for this group
1039     int aWidth = this.getWidth();
1040     // pointer
1041     // possibility
1042     // here.
1043     if (aWidth < 0)
1044     {
1045       return null;
1046     }
1047     if (consensus == null)
1048     {
1049       consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1050               100f, AlignmentAnnotation.BAR_GRAPH);
1051       consensus.hasText = true;
1052       consensus.autoCalculated = true;
1053       consensus.groupRef = this;
1054       consensus.label = "Consensus for " + getName();
1055       consensus.description = "Percent Identity";
1056     }
1057     return consensus;
1058   }
1059
1060   /**
1061    * set this alignmentAnnotation object as the one used to render consensus
1062    * annotation
1063    * 
1064    * @param aan
1065    */
1066   public void setConservationRow(AlignmentAnnotation aan)
1067   {
1068     if (conservation == null)
1069     {
1070       conservation = aan;
1071     }
1072   }
1073
1074   /**
1075    * get the conservation annotation row for this group
1076    * 
1077    * @return autoCalculated annotation row
1078    */
1079   public AlignmentAnnotation getConservationRow()
1080   {
1081     if (conservation == null)
1082     {
1083       conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1084               11f, AlignmentAnnotation.BAR_GRAPH);
1085     }
1086
1087     conservation.hasText = true;
1088     conservation.autoCalculated = true;
1089     conservation.groupRef = this;
1090     conservation.label = "Conservation for " + getName();
1091     conservation.description = "Conservation for group " + getName()
1092             + " less than " + consPercGaps + "% gaps";
1093     return conservation;
1094   }
1095
1096   /**
1097    * 
1098    * @return true if annotation rows have been instantiated for this group
1099    */
1100   public boolean hasAnnotationRows()
1101   {
1102     return consensus != null || conservation != null;
1103   }
1104
1105   public SequenceI getConsensusSeq()
1106   {
1107     getConsensus();
1108     StringBuffer seqs = new StringBuffer();
1109     for (int i = 0; i < consensus.annotations.length; i++)
1110     {
1111       if (consensus.annotations[i] != null)
1112       {
1113         if (consensus.annotations[i].description.charAt(0) == '[')
1114         {
1115           seqs.append(consensus.annotations[i].description.charAt(1));
1116         }
1117         else
1118         {
1119           seqs.append(consensus.annotations[i].displayCharacter);
1120         }
1121       }
1122     }
1123
1124     SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1125             seqs.toString());
1126     sq.setDescription("Percentage Identity Consensus "
1127             + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1128     return sq;
1129   }
1130
1131   public void setIgnoreGapsConsensus(boolean state)
1132   {
1133     if (this.ignoreGapsInConsensus != state && consensus != null)
1134     {
1135       ignoreGapsInConsensus = state;
1136       recalcConservation();
1137     }
1138     ignoreGapsInConsensus = state;
1139   }
1140
1141   public boolean getIgnoreGapsConsensus()
1142   {
1143     return ignoreGapsInConsensus;
1144   }
1145
1146   /**
1147    * @param showSequenceLogo
1148    *          indicates if a sequence logo is shown for consensus annotation
1149    */
1150   public void setshowSequenceLogo(boolean showSequenceLogo)
1151   {
1152     // TODO: decouple calculation from settings update
1153     if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1154     {
1155       this.showSequenceLogo = showSequenceLogo;
1156       recalcConservation();
1157     }
1158     this.showSequenceLogo = showSequenceLogo;
1159   }
1160
1161   /**
1162    * 
1163    * @param showConsHist
1164    *          flag indicating if the consensus histogram for this group should
1165    *          be rendered
1166    */
1167   public void setShowConsensusHistogram(boolean showConsHist)
1168   {
1169
1170     if (showConsensusHistogram != showConsHist && consensus != null)
1171     {
1172       this.showConsensusHistogram = showConsHist;
1173       recalcConservation();
1174     }
1175     this.showConsensusHistogram = showConsHist;
1176   }
1177
1178   /**
1179    * @return the showConsensusHistogram
1180    */
1181   public boolean isShowConsensusHistogram()
1182   {
1183     return showConsensusHistogram;
1184   }
1185
1186   /**
1187    * set flag indicating if logo should be normalised when rendered
1188    * 
1189    * @param norm
1190    */
1191   public void setNormaliseSequenceLogo(boolean norm)
1192   {
1193     normaliseSequenceLogo = norm;
1194   }
1195
1196   public boolean isNormaliseSequenceLogo()
1197   {
1198     return normaliseSequenceLogo;
1199   }
1200
1201   @Override
1202   /**
1203    * returns a new array with all annotation involving this group
1204    */
1205   public AlignmentAnnotation[] getAlignmentAnnotation()
1206   {
1207     // TODO add in other methods like 'getAlignmentAnnotation(String label),
1208     // etc'
1209     ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1210     for (SequenceI seq : (Vector<SequenceI>) sequences)
1211     {
1212       AlignmentAnnotation[] aa = seq.getAnnotation();
1213       if (aa != null)
1214       {
1215         for (AlignmentAnnotation al : aa)
1216         {
1217           if (al.groupRef == this)
1218           {
1219             annot.add(al);
1220           }
1221         }
1222       }
1223     }
1224     if (consensus != null)
1225     {
1226       annot.add(consensus);
1227     }
1228     if (conservation != null)
1229     {
1230       annot.add(conservation);
1231     }
1232     return annot.toArray(new AlignmentAnnotation[0]);
1233   }
1234
1235   @Override
1236   public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1237   {
1238     ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1239     for (AlignmentAnnotation a : getAlignmentAnnotation())
1240     {
1241       if (a.getCalcId() == calcId)
1242       {
1243         aa.add(a);
1244       }
1245     }
1246     return aa;
1247   }
1248
1249   public void clear()
1250   {
1251     sequences.clear();
1252   }
1253   private AnnotatedCollectionI context;
1254   /**
1255    * set the alignment or group context for this group
1256    * @param context
1257    */
1258   public void setContext(AnnotatedCollectionI context)
1259   {
1260     this.context = context; 
1261   }
1262   /* (non-Javadoc)
1263    * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1264    */
1265   @Override
1266   public AnnotatedCollectionI getContext()
1267   {
1268     return context;
1269   }
1270 }