2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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.
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.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.datamodel;
24 import jalview.analysis.*;
25 import jalview.schemes.*;
28 * Collects a set contiguous ranges on a set of sequences
33 public class SequenceGroup
39 Conservation conserve;
43 boolean displayBoxes = true;
45 boolean displayText = true;
47 boolean colourText = false;
50 * after Olivier's non-conserved only character display
52 boolean showNonconserved = false;
57 private Vector sequences = new Vector();
60 * representative sequence for this group (if any)
62 private SequenceI seqrep = null;
67 * Colourscheme applied to group if any
69 public ColourSchemeI cs;
75 public Color outlineColour = Color.black;
77 public Color idColour = null;
79 public int thresholdTextColour = 0;
81 public Color textColour = Color.black;
83 public Color textColour2 = Color.white;
86 * consensus calculation property
88 private boolean ignoreGapsInConsensus = true;
91 * consensus calculation property
93 private boolean showSequenceLogo = false;
95 * flag indicating if logo should be rendered normalised
97 private boolean normaliseSequenceLogo;
101 * @return the includeAllConsSymbols
103 public boolean isShowSequenceLogo()
105 return showSequenceLogo;
109 * Creates a new SequenceGroup object.
111 public SequenceGroup()
113 groupName = "JGroup:" + this.hashCode();
117 * Creates a new SequenceGroup object.
122 * @param displayBoxes
126 * first column of group
128 * last column of group
130 public SequenceGroup(Vector sequences, String groupName,
131 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
132 boolean colourText, int start, int end)
134 this.sequences = sequences;
135 this.groupName = groupName;
136 this.displayBoxes = displayBoxes;
137 this.displayText = displayText;
138 this.colourText = colourText;
142 recalcConservation();
150 public SequenceGroup(SequenceGroup seqsel)
154 sequences = new Vector();
155 Enumeration sq = seqsel.sequences.elements();
156 while (sq.hasMoreElements())
158 sequences.addElement(sq.nextElement());
161 if (seqsel.groupName != null)
163 groupName = new String(seqsel.groupName);
165 displayBoxes = seqsel.displayBoxes;
166 displayText = seqsel.displayText;
167 colourText = seqsel.colourText;
168 startRes = seqsel.startRes;
169 endRes = seqsel.endRes;
171 if (seqsel.description != null)
172 description = new String(seqsel.description);
173 hidecols = seqsel.hidecols;
174 hidereps = seqsel.hidereps;
175 idColour = seqsel.idColour;
176 outlineColour = seqsel.outlineColour;
177 seqrep = seqsel.seqrep;
178 textColour = seqsel.textColour;
179 textColour2 = seqsel.textColour2;
180 thresholdTextColour = seqsel.thresholdTextColour;
181 width = seqsel.width;
182 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
183 if (seqsel.conserve != null)
185 recalcConservation(); // safer than
186 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
191 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
193 int iSize = sequences.size();
194 SequenceI[] seqs = new SequenceI[iSize];
195 SequenceI[] inorder = getSequencesInOrder(align);
197 for (int i = 0, ipos = 0; i < inorder.length; i++)
199 SequenceI seq = inorder[i];
201 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
202 if (seqs[ipos] != null)
204 seqs[ipos].setDescription(seq.getDescription());
205 seqs[ipos].setDBRef(seq.getDBRef());
206 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
207 if (seq.getDatasetSequence() != null)
209 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
212 if (seq.getAnnotation() != null)
214 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
215 // Only copy annotation that is either a score or referenced by the
216 // alignment's annotation vector
217 for (int a = 0; a < seq.getAnnotation().length; a++)
219 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
222 boolean found = false;
223 for (int pos = 0; pos < alann.length; pos++)
225 if (alann[pos] == tocopy)
234 AlignmentAnnotation newannot = new AlignmentAnnotation(
235 seq.getAnnotation()[a]);
236 newannot.restrict(startRes, endRes);
237 newannot.setSequenceRef(seqs[ipos]);
238 newannot.adjustForAlignment();
239 seqs[ipos].addAlignmentAnnotation(newannot);
249 if (iSize != inorder.length)
251 SequenceI[] nseqs = new SequenceI[iSize];
252 System.arraycopy(seqs, 0, nseqs, 0, iSize);
260 * If sequence ends in gaps, the end residue can be correctly calculated here
266 public int findEndRes(SequenceI seq)
271 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
273 ch = seq.getCharAt(j);
274 if (!jalview.util.Comparison.isGap((ch)))
282 eres += seq.getStart() - 1;
288 public Vector getSequences(Hashtable hiddenReps)
290 if (hiddenReps == null)
296 Vector allSequences = new Vector();
298 for (int i = 0; i < sequences.size(); i++)
300 seq = (SequenceI) sequences.elementAt(i);
301 allSequences.addElement(seq);
302 if (hiddenReps.containsKey(seq))
304 SequenceGroup hsg = (SequenceGroup) hiddenReps.get(seq);
305 for (int h = 0; h < hsg.getSize(); h++)
307 seq2 = hsg.getSequenceAt(h);
308 if (seq2 != seq && !allSequences.contains(seq2))
310 allSequences.addElement(seq2);
320 public SequenceI[] getSequencesAsArray(Hashtable hiddenReps)
322 Vector tmp = getSequences(hiddenReps);
327 SequenceI[] result = new SequenceI[tmp.size()];
328 for (int i = 0; i < result.length; i++)
330 result[i] = (SequenceI) tmp.elementAt(i);
342 * @return DOCUMENT ME!
344 public boolean adjustForRemoveLeft(int col)
346 // return value is true if the group still exists
349 startRes = startRes - col;
354 endRes = endRes - col;
356 if (startRes > endRes)
363 // must delete this group!!
376 * @return DOCUMENT ME!
378 public boolean adjustForRemoveRight(int col)
397 * @return DOCUMENT ME!
399 public String getName()
404 public String getDescription()
415 public void setName(String name)
418 // TODO: URGENT: update dependent objects (annotation row)
421 public void setDescription(String desc)
429 * @return DOCUMENT ME!
431 public Conservation getConservation()
442 public void setConservation(Conservation c)
448 * Add s to this sequence group. If aligment sequence is already contained in
449 * group, it will not be added again, but recalculation may happen if the flag
453 * alignment sequence to be added
455 * true means Group's conservation should be recalculated
457 public void addSequence(SequenceI s, boolean recalc)
459 if (s != null && !sequences.contains(s))
461 sequences.addElement(s);
466 recalcConservation();
471 * Max Gaps Threshold for performing a conservation calculation TODO: make
472 * this a configurable property - or global to an alignment view
474 private int consPercGaps = 25;
477 * calculate residue conservation for group - but only if necessary.
479 public void recalcConservation()
481 if (cs == null && consensus == null && conservation == null)
488 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
489 endRes + 1, showSequenceLogo);
490 if (consensus != null)
492 _updateConsensusRow(cnsns);
496 cs.setConsensus(cnsns);
498 if (cs instanceof ClustalxColourScheme)
500 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
504 if ((conservation != null)
505 || (cs != null && cs.conservationApplied()))
507 Conservation c = new Conservation(groupName,
508 ResidueProperties.propHash, 3, sequences, startRes,
511 c.verdict(false, consPercGaps);
512 if (conservation != null)
514 _updateConservationRow(c);
518 if (cs.conservationApplied())
520 cs.setConservation(c);
522 if (cs instanceof ClustalxColourScheme)
524 ((ClustalxColourScheme) cs).resetClustalX(sequences,
530 } catch (java.lang.OutOfMemoryError err)
533 System.out.println("Out of memory loading groups: " + err);
538 private void _updateConservationRow(Conservation c)
540 if (conservation == null)
545 conservation.label = "Conservation for " + getName();
546 conservation.description = "Conservation for group " + getName()
547 + " less than " + consPercGaps + "% gaps";
548 // preserve width if already set
549 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
552 conservation.annotations = null;
553 conservation.annotations = new Annotation[aWidth]; // should be alignment
555 c.completeAnnotations(conservation, null, startRes, endRes + 1);
558 public Hashtable[] consensusData = null;
560 private void _updateConsensusRow(Hashtable[] cnsns)
562 if (consensus == null)
566 consensus.label = "Consensus for " + getName();
567 consensus.description = "Percent Identity";
568 consensusData = cnsns;
569 // preserve width if already set
570 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
573 consensus.annotations = null;
574 consensus.annotations = new Annotation[aWidth]; // should be alignment width
576 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
577 ignoreGapsInConsensus, showSequenceLogo); // TODO: setting container
579 // ignoreGapsInConsensusCalculation);
584 * sequence to either add or remove from group
586 * flag passed to delete/addSequence to indicate if group properties
587 * should be recalculated
589 public void addOrRemove(SequenceI s, boolean recalc)
591 if (sequences.contains(s))
593 deleteSequence(s, recalc);
597 addSequence(s, recalc);
609 public void deleteSequence(SequenceI s, boolean recalc)
611 sequences.removeElement(s);
615 recalcConservation();
622 * @return DOCUMENT ME!
624 public int getStartRes()
632 * @return DOCUMENT ME!
634 public int getEndRes()
640 * Set the first column selected by this group. Runs from 0<=i<N_cols
644 public void setStartRes(int i)
650 * Set the groups last selected column. Runs from 0<=i<N_cols
654 public void setEndRes(int i)
662 * @return DOCUMENT ME!
666 return sequences.size();
675 * @return DOCUMENT ME!
677 public SequenceI getSequenceAt(int i)
679 return (SequenceI) sequences.elementAt(i);
688 public void setColourText(boolean state)
696 * @return DOCUMENT ME!
698 public boolean getColourText()
709 public void setDisplayText(boolean state)
717 * @return DOCUMENT ME!
719 public boolean getDisplayText()
730 public void setDisplayBoxes(boolean state)
732 displayBoxes = state;
738 * @return DOCUMENT ME!
740 public boolean getDisplayBoxes()
748 * @return DOCUMENT ME!
750 public int getWidth()
752 // MC This needs to get reset when characters are inserted and deleted
753 if (sequences.size() > 0)
755 width = ((SequenceI) sequences.elementAt(0)).getLength();
758 for (int i = 1; i < sequences.size(); i++)
760 SequenceI seq = (SequenceI) sequences.elementAt(i);
762 if (seq.getLength() > width)
764 width = seq.getLength();
777 public void setOutlineColour(Color c)
785 * @return DOCUMENT ME!
787 public Color getOutlineColour()
789 return outlineColour;
794 * returns the sequences in the group ordered by the ordering given by al.
795 * this used to return an array with null entries regardless, new behaviour is
796 * below. TODO: verify that this does not affect use in applet or application
800 * @return SequenceI[] intersection of sequences in group with al, ordered by
801 * al, or null if group does not intersect with al
803 public SequenceI[] getSequencesInOrder(AlignmentI al)
805 return getSequencesInOrder(al, true);
809 * return an array representing the intersection of the group with al,
810 * optionally returning an array the size of al.getHeight() where nulls mark
811 * the non-intersected sequences
815 * @return null or array
817 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
819 int sSize = sequences.size();
820 int alHeight = al.getHeight();
822 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
825 for (int i = 0; i < alHeight && index < sSize; i++)
827 if (sequences.contains(al.getSequenceAt(i)))
829 seqs[(trim) ? index : i] = al.getSequenceAt(i);
841 if (index < seqs.length)
843 SequenceI[] dummy = seqs;
844 seqs = new SequenceI[index];
847 seqs[index] = dummy[index];
855 * @return the idColour
857 public Color getIdColour()
864 * the idColour to set
866 public void setIdColour(Color idColour)
868 this.idColour = idColour;
872 * @return the representative sequence for this group
874 public SequenceI getSeqrep()
880 * set the representative sequence for this group. Note - this affects the
881 * interpretation of the Hidereps attribute.
884 * the seqrep to set (null means no sequence representative)
886 public void setSeqrep(SequenceI seqrep)
888 this.seqrep = seqrep;
893 * @return true if group has a sequence representative
895 public boolean hasSeqrep()
897 return seqrep != null;
901 * visibility of rows or represented rows covered by group
903 private boolean hidereps = false;
906 * set visibility of sequences covered by (if no sequence representative is
907 * defined) or represented by this group.
911 public void setHidereps(boolean visibility)
913 hidereps = visibility;
918 * @return true if sequences represented (or covered) by this group should be
921 public boolean isHidereps()
927 * visibility of columns intersecting this group
929 private boolean hidecols = false;
932 * set intended visibility of columns covered by this group
936 public void setHideCols(boolean visibility)
938 hidecols = visibility;
943 * @return true if columns covered by group should be hidden
945 public boolean isHideCols()
951 * create a new sequence group from the intersection of this group with an
952 * alignment Hashtable of hidden representatives
958 * @return new group containing sequences common to this group and alignment
960 public SequenceGroup intersect(AlignmentI alignment, Hashtable hashtable)
962 SequenceGroup sgroup = new SequenceGroup(this);
963 SequenceI[] insect = getSequencesInOrder(alignment);
964 sgroup.sequences = new Vector();
965 for (int s = 0; insect != null && s < insect.length; s++)
967 if (hashtable == null || hashtable.containsKey(insect[s]))
969 sgroup.sequences.addElement(insect[s]);
972 // Enumeration en =getSequences(hashtable).elements();
973 // while (en.hasMoreElements())
975 // SequenceI elem = (SequenceI) en.nextElement();
976 // if (alignment.getSequences().contains(elem))
978 // sgroup.addSequence(elem, false);
985 * @return the showUnconserved
987 public boolean getShowNonconserved()
989 return showNonconserved;
993 * @param showNonconserved
994 * the showUnconserved to set
996 public void setShowNonconserved(boolean displayNonconserved)
998 this.showNonconserved = displayNonconserved;
1001 AlignmentAnnotation consensus = null, conservation = null;
1004 * flag indicating if consensus histogram should be rendered
1006 private boolean showConsensusHistogram;
1009 * set this alignmentAnnotation object as the one used to render consensus
1014 public void setConsensus(AlignmentAnnotation aan)
1016 if (consensus == null)
1024 * @return automatically calculated consensus row
1026 public AlignmentAnnotation getConsensus()
1028 // TODO get or calculate and get consensus annotation row for this group
1029 int aWidth = this.getWidth();
1037 if (consensus == null)
1039 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1040 100f, AlignmentAnnotation.BAR_GRAPH);
1042 consensus.hasText = true;
1043 consensus.autoCalculated = true;
1044 consensus.groupRef = this;
1045 consensus.label = "Consensus for " + getName();
1046 consensus.description = "Percent Identity";
1051 * set this alignmentAnnotation object as the one used to render consensus
1056 public void setConservationRow(AlignmentAnnotation aan)
1058 if (conservation == null)
1065 * get the conservation annotation row for this group
1067 * @return autoCalculated annotation row
1069 public AlignmentAnnotation getConservationRow()
1071 if (conservation == null)
1073 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1074 11f, AlignmentAnnotation.BAR_GRAPH);
1077 conservation.hasText = true;
1078 conservation.autoCalculated = true;
1079 conservation.groupRef = this;
1080 conservation.label = "Conservation for " + getName();
1081 conservation.description = "Conservation for group " + getName()
1082 + " less than " + consPercGaps + "% gaps";
1083 return conservation;
1088 * @return true if annotation rows have been instantiated for this group
1090 public boolean hasAnnotationRows()
1092 return consensus != null || conservation != null;
1095 public SequenceI getConsensusSeq()
1098 StringBuffer seqs = new StringBuffer();
1099 for (int i = 0; i < consensus.annotations.length; i++)
1101 if (consensus.annotations[i] != null)
1103 if (consensus.annotations[i].description.charAt(0) == '[')
1105 seqs.append(consensus.annotations[i].description.charAt(1));
1109 seqs.append(consensus.annotations[i].displayCharacter);
1114 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1116 sq.setDescription("Percentage Identity Consensus "
1117 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1121 public void setIgnoreGapsConsensus(boolean state)
1123 if (this.ignoreGapsInConsensus != state && consensus != null)
1125 ignoreGapsInConsensus = state;
1126 recalcConservation();
1128 ignoreGapsInConsensus = state;
1131 public boolean getIgnoreGapsConsensus()
1133 return ignoreGapsInConsensus;
1137 * @param showSequenceLogo
1138 * indicates if a sequence logo is shown for consensus annotation
1140 public void setshowSequenceLogo(boolean showSequenceLogo)
1142 // TODO: decouple calculation from settings update
1143 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1145 this.showSequenceLogo = showSequenceLogo;
1146 recalcConservation();
1148 this.showSequenceLogo = showSequenceLogo;
1153 * @param showConsHist
1154 * flag indicating if the consensus histogram for this group should
1157 public void setShowConsensusHistogram(boolean showConsHist)
1160 if (showConsensusHistogram != showConsHist && consensus != null)
1162 this.showConsensusHistogram = showConsHist;
1163 recalcConservation();
1165 this.showConsensusHistogram = showConsHist;
1169 * @return the showConsensusHistogram
1171 public boolean isShowConsensusHistogram()
1173 return showConsensusHistogram;
1177 * set flag indicating if logo should be normalised when rendered
1180 public void setNormaliseSequenceLogo(boolean norm)
1182 normaliseSequenceLogo=norm;
1184 public boolean isNormaliseSequenceLogo()
1186 return normaliseSequenceLogo;