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