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