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 implements AnnotatedCollectionI
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)
487 cs.alignmentChanged(this);
491 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
492 endRes + 1, showSequenceLogo);
493 if (consensus != null)
495 _updateConsensusRow(cnsns);
499 cs.setConsensus(cnsns);
501 if (cs instanceof ClustalxColourScheme)
503 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
507 if ((conservation != null)
508 || (cs != null && cs.conservationApplied()))
510 Conservation c = new Conservation(groupName,
511 ResidueProperties.propHash, 3, sequences, startRes,
514 c.verdict(false, consPercGaps);
515 if (conservation != null)
517 _updateConservationRow(c);
521 if (cs.conservationApplied())
523 cs.setConservation(c);
525 if (cs instanceof ClustalxColourScheme)
527 ((ClustalxColourScheme) cs).resetClustalX(sequences,
533 } catch (java.lang.OutOfMemoryError err)
536 System.out.println("Out of memory loading groups: " + err);
541 private void _updateConservationRow(Conservation c)
543 if (conservation == null)
548 conservation.label = "Conservation for " + getName();
549 conservation.description = "Conservation for group " + getName()
550 + " less than " + consPercGaps + "% gaps";
551 // preserve width if already set
552 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
555 conservation.annotations = null;
556 conservation.annotations = new Annotation[aWidth]; // should be alignment
558 c.completeAnnotations(conservation, null, startRes, endRes + 1);
561 public Hashtable[] consensusData = null;
563 private void _updateConsensusRow(Hashtable[] cnsns)
565 if (consensus == null)
569 consensus.label = "Consensus for " + getName();
570 consensus.description = "Percent Identity";
571 consensusData = cnsns;
572 // preserve width if already set
573 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
576 consensus.annotations = null;
577 consensus.annotations = new Annotation[aWidth]; // should be alignment width
579 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
580 ignoreGapsInConsensus, showSequenceLogo); // TODO: setting container
582 // ignoreGapsInConsensusCalculation);
587 * sequence to either add or remove from group
589 * flag passed to delete/addSequence to indicate if group properties
590 * should be recalculated
592 public void addOrRemove(SequenceI s, boolean recalc)
594 if (sequences.contains(s))
596 deleteSequence(s, recalc);
600 addSequence(s, recalc);
612 public void deleteSequence(SequenceI s, boolean recalc)
614 sequences.removeElement(s);
618 recalcConservation();
625 * @return DOCUMENT ME!
627 public int getStartRes()
635 * @return DOCUMENT ME!
637 public int getEndRes()
643 * Set the first column selected by this group. Runs from 0<=i<N_cols
647 public void setStartRes(int i)
653 * Set the groups last selected column. Runs from 0<=i<N_cols
657 public void setEndRes(int i)
665 * @return DOCUMENT ME!
669 return sequences.size();
678 * @return DOCUMENT ME!
680 public SequenceI getSequenceAt(int i)
682 return (SequenceI) sequences.elementAt(i);
691 public void setColourText(boolean state)
699 * @return DOCUMENT ME!
701 public boolean getColourText()
712 public void setDisplayText(boolean state)
720 * @return DOCUMENT ME!
722 public boolean getDisplayText()
733 public void setDisplayBoxes(boolean state)
735 displayBoxes = state;
741 * @return DOCUMENT ME!
743 public boolean getDisplayBoxes()
751 * @return DOCUMENT ME!
753 public int getWidth()
755 // MC This needs to get reset when characters are inserted and deleted
756 if (sequences.size() > 0)
758 width = ((SequenceI) sequences.elementAt(0)).getLength();
761 for (int i = 1; i < sequences.size(); i++)
763 SequenceI seq = (SequenceI) sequences.elementAt(i);
765 if (seq.getLength() > width)
767 width = seq.getLength();
780 public void setOutlineColour(Color c)
788 * @return DOCUMENT ME!
790 public Color getOutlineColour()
792 return outlineColour;
797 * returns the sequences in the group ordered by the ordering given by al.
798 * this used to return an array with null entries regardless, new behaviour is
799 * below. TODO: verify that this does not affect use in applet or application
803 * @return SequenceI[] intersection of sequences in group with al, ordered by
804 * al, or null if group does not intersect with al
806 public SequenceI[] getSequencesInOrder(AlignmentI al)
808 return getSequencesInOrder(al, true);
812 * return an array representing the intersection of the group with al,
813 * optionally returning an array the size of al.getHeight() where nulls mark
814 * the non-intersected sequences
818 * @return null or array
820 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
822 int sSize = sequences.size();
823 int alHeight = al.getHeight();
825 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
828 for (int i = 0; i < alHeight && index < sSize; i++)
830 if (sequences.contains(al.getSequenceAt(i)))
832 seqs[(trim) ? index : i] = al.getSequenceAt(i);
844 if (index < seqs.length)
846 SequenceI[] dummy = seqs;
847 seqs = new SequenceI[index];
850 seqs[index] = dummy[index];
858 * @return the idColour
860 public Color getIdColour()
867 * the idColour to set
869 public void setIdColour(Color idColour)
871 this.idColour = idColour;
875 * @return the representative sequence for this group
877 public SequenceI getSeqrep()
883 * set the representative sequence for this group. Note - this affects the
884 * interpretation of the Hidereps attribute.
887 * the seqrep to set (null means no sequence representative)
889 public void setSeqrep(SequenceI seqrep)
891 this.seqrep = seqrep;
896 * @return true if group has a sequence representative
898 public boolean hasSeqrep()
900 return seqrep != null;
904 * visibility of rows or represented rows covered by group
906 private boolean hidereps = false;
909 * set visibility of sequences covered by (if no sequence representative is
910 * defined) or represented by this group.
914 public void setHidereps(boolean visibility)
916 hidereps = visibility;
921 * @return true if sequences represented (or covered) by this group should be
924 public boolean isHidereps()
930 * visibility of columns intersecting this group
932 private boolean hidecols = false;
935 * set intended visibility of columns covered by this group
939 public void setHideCols(boolean visibility)
941 hidecols = visibility;
946 * @return true if columns covered by group should be hidden
948 public boolean isHideCols()
954 * create a new sequence group from the intersection of this group with an
955 * alignment Hashtable of hidden representatives
961 * @return new group containing sequences common to this group and alignment
963 public SequenceGroup intersect(AlignmentI alignment, Hashtable hashtable)
965 SequenceGroup sgroup = new SequenceGroup(this);
966 SequenceI[] insect = getSequencesInOrder(alignment);
967 sgroup.sequences = new Vector();
968 for (int s = 0; insect != null && s < insect.length; s++)
970 if (hashtable == null || hashtable.containsKey(insect[s]))
972 sgroup.sequences.addElement(insect[s]);
975 // Enumeration en =getSequences(hashtable).elements();
976 // while (en.hasMoreElements())
978 // SequenceI elem = (SequenceI) en.nextElement();
979 // if (alignment.getSequences().contains(elem))
981 // sgroup.addSequence(elem, false);
988 * @return the showUnconserved
990 public boolean getShowNonconserved()
992 return showNonconserved;
996 * @param showNonconserved
997 * the showUnconserved to set
999 public void setShowNonconserved(boolean displayNonconserved)
1001 this.showNonconserved = displayNonconserved;
1004 AlignmentAnnotation consensus = null, conservation = null;
1007 * flag indicating if consensus histogram should be rendered
1009 private boolean showConsensusHistogram;
1012 * set this alignmentAnnotation object as the one used to render consensus
1017 public void setConsensus(AlignmentAnnotation aan)
1019 if (consensus == null)
1027 * @return automatically calculated consensus row
1029 public AlignmentAnnotation getConsensus()
1031 // TODO get or calculate and get consensus annotation row for this group
1032 int aWidth = this.getWidth();
1040 if (consensus == null)
1042 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1043 100f, AlignmentAnnotation.BAR_GRAPH);
1045 consensus.hasText = true;
1046 consensus.autoCalculated = true;
1047 consensus.groupRef = this;
1048 consensus.label = "Consensus for " + getName();
1049 consensus.description = "Percent Identity";
1054 * set this alignmentAnnotation object as the one used to render consensus
1059 public void setConservationRow(AlignmentAnnotation aan)
1061 if (conservation == null)
1068 * get the conservation annotation row for this group
1070 * @return autoCalculated annotation row
1072 public AlignmentAnnotation getConservationRow()
1074 if (conservation == null)
1076 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1077 11f, AlignmentAnnotation.BAR_GRAPH);
1080 conservation.hasText = true;
1081 conservation.autoCalculated = true;
1082 conservation.groupRef = this;
1083 conservation.label = "Conservation for " + getName();
1084 conservation.description = "Conservation for group " + getName()
1085 + " less than " + consPercGaps + "% gaps";
1086 return conservation;
1091 * @return true if annotation rows have been instantiated for this group
1093 public boolean hasAnnotationRows()
1095 return consensus != null || conservation != null;
1098 public SequenceI getConsensusSeq()
1101 StringBuffer seqs = new StringBuffer();
1102 for (int i = 0; i < consensus.annotations.length; i++)
1104 if (consensus.annotations[i] != null)
1106 if (consensus.annotations[i].description.charAt(0) == '[')
1108 seqs.append(consensus.annotations[i].description.charAt(1));
1112 seqs.append(consensus.annotations[i].displayCharacter);
1117 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1119 sq.setDescription("Percentage Identity Consensus "
1120 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1124 public void setIgnoreGapsConsensus(boolean state)
1126 if (this.ignoreGapsInConsensus != state && consensus != null)
1128 ignoreGapsInConsensus = state;
1129 recalcConservation();
1131 ignoreGapsInConsensus = state;
1134 public boolean getIgnoreGapsConsensus()
1136 return ignoreGapsInConsensus;
1140 * @param showSequenceLogo
1141 * indicates if a sequence logo is shown for consensus annotation
1143 public void setshowSequenceLogo(boolean showSequenceLogo)
1145 // TODO: decouple calculation from settings update
1146 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1148 this.showSequenceLogo = showSequenceLogo;
1149 recalcConservation();
1151 this.showSequenceLogo = showSequenceLogo;
1156 * @param showConsHist
1157 * flag indicating if the consensus histogram for this group should
1160 public void setShowConsensusHistogram(boolean showConsHist)
1163 if (showConsensusHistogram != showConsHist && consensus != null)
1165 this.showConsensusHistogram = showConsHist;
1166 recalcConservation();
1168 this.showConsensusHistogram = showConsHist;
1172 * @return the showConsensusHistogram
1174 public boolean isShowConsensusHistogram()
1176 return showConsensusHistogram;
1180 * set flag indicating if logo should be normalised when rendered
1183 public void setNormaliseSequenceLogo(boolean norm)
1185 normaliseSequenceLogo=norm;
1187 public boolean isNormaliseSequenceLogo()
1189 return normaliseSequenceLogo;
1193 * returns a new array with all annotation involving this group
1195 public AlignmentAnnotation[] getAlignmentAnnotation()
1197 // TODO add in other methods like 'getAlignmentAnnotation(String label), etc'
1198 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1199 for (SequenceI seq:(Vector<SequenceI>)sequences)
1201 for (AlignmentAnnotation al: seq.getAnnotation())
1203 if (al.groupRef==this)
1209 if (consensus!=null)
1211 annot.add(consensus);
1213 if (conservation!=null)
1215 annot.add(conservation);
1217 return annot.toArray(new AlignmentAnnotation[0]);