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