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