2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, 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;
96 * @return the includeAllConsSymbols
98 public boolean isShowSequenceLogo()
100 return showSequenceLogo;
104 * Creates a new SequenceGroup object.
106 public SequenceGroup()
108 groupName = "JGroup:" + this.hashCode();
112 * Creates a new SequenceGroup object.
117 * @param displayBoxes
121 * first column of group
123 * last column of group
125 public SequenceGroup(Vector sequences, String groupName,
126 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
127 boolean colourText, int start, int end)
129 this.sequences = sequences;
130 this.groupName = groupName;
131 this.displayBoxes = displayBoxes;
132 this.displayText = displayText;
133 this.colourText = colourText;
137 recalcConservation();
145 public SequenceGroup(SequenceGroup seqsel)
149 sequences = new Vector();
150 Enumeration sq = seqsel.sequences.elements();
151 while (sq.hasMoreElements())
153 sequences.addElement(sq.nextElement());
156 if (seqsel.groupName != null)
158 groupName = new String(seqsel.groupName);
160 displayBoxes = seqsel.displayBoxes;
161 displayText = seqsel.displayText;
162 colourText = seqsel.colourText;
163 startRes = seqsel.startRes;
164 endRes = seqsel.endRes;
166 if (seqsel.description != null)
167 description = new String(seqsel.description);
168 hidecols = seqsel.hidecols;
169 hidereps = seqsel.hidereps;
170 idColour = seqsel.idColour;
171 outlineColour = seqsel.outlineColour;
172 seqrep = seqsel.seqrep;
173 textColour = seqsel.textColour;
174 textColour2 = seqsel.textColour2;
175 thresholdTextColour = seqsel.thresholdTextColour;
176 width = seqsel.width;
177 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
178 if (seqsel.conserve != null)
180 recalcConservation(); // safer than
181 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
186 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
188 int iSize = sequences.size();
189 SequenceI[] seqs = new SequenceI[iSize];
190 SequenceI[] inorder = getSequencesInOrder(align);
192 for (int i = 0, ipos = 0; i < inorder.length; i++)
194 SequenceI seq = inorder[i];
196 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
197 if (seqs[ipos] != null)
199 seqs[ipos].setDescription(seq.getDescription());
200 seqs[ipos].setDBRef(seq.getDBRef());
201 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
202 if (seq.getDatasetSequence() != null)
204 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
207 if (seq.getAnnotation() != null)
209 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
210 // Only copy annotation that is either a score or referenced by the
211 // alignment's annotation vector
212 for (int a = 0; a < seq.getAnnotation().length; a++)
214 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
217 boolean found = false;
218 for (int pos = 0; pos < alann.length; pos++)
220 if (alann[pos] == tocopy)
229 AlignmentAnnotation newannot = new AlignmentAnnotation(
230 seq.getAnnotation()[a]);
231 newannot.restrict(startRes, endRes);
232 newannot.setSequenceRef(seqs[ipos]);
233 newannot.adjustForAlignment();
234 seqs[ipos].addAlignmentAnnotation(newannot);
244 if (iSize != inorder.length)
246 SequenceI[] nseqs = new SequenceI[iSize];
247 System.arraycopy(seqs, 0, nseqs, 0, iSize);
255 * If sequence ends in gaps, the end residue can be correctly calculated here
261 public int findEndRes(SequenceI seq)
266 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
268 ch = seq.getCharAt(j);
269 if (!jalview.util.Comparison.isGap((ch)))
277 eres += seq.getStart() - 1;
283 public Vector getSequences(Hashtable hiddenReps)
285 if (hiddenReps == null)
291 Vector allSequences = new Vector();
293 for (int i = 0; i < sequences.size(); i++)
295 seq = (SequenceI) sequences.elementAt(i);
296 allSequences.addElement(seq);
297 if (hiddenReps.containsKey(seq))
299 SequenceGroup hsg = (SequenceGroup) hiddenReps.get(seq);
300 for (int h = 0; h < hsg.getSize(); h++)
302 seq2 = hsg.getSequenceAt(h);
303 if (seq2 != seq && !allSequences.contains(seq2))
305 allSequences.addElement(seq2);
315 public SequenceI[] getSequencesAsArray(Hashtable hiddenReps)
317 Vector tmp = getSequences(hiddenReps);
322 SequenceI[] result = new SequenceI[tmp.size()];
323 for (int i = 0; i < result.length; i++)
325 result[i] = (SequenceI) tmp.elementAt(i);
337 * @return DOCUMENT ME!
339 public boolean adjustForRemoveLeft(int col)
341 // return value is true if the group still exists
344 startRes = startRes - col;
349 endRes = endRes - col;
351 if (startRes > endRes)
358 // must delete this group!!
371 * @return DOCUMENT ME!
373 public boolean adjustForRemoveRight(int col)
392 * @return DOCUMENT ME!
394 public String getName()
399 public String getDescription()
410 public void setName(String name)
413 // TODO: URGENT: update dependent objects (annotation row)
416 public void setDescription(String desc)
424 * @return DOCUMENT ME!
426 public Conservation getConservation()
437 public void setConservation(Conservation c)
443 * Add s to this sequence group. If aligment sequence is already contained in
444 * group, it will not be added again, but recalculation may happen if the flag
448 * alignment sequence to be added
450 * true means Group's conservation should be recalculated
452 public void addSequence(SequenceI s, boolean recalc)
454 if (s != null && !sequences.contains(s))
456 sequences.addElement(s);
461 recalcConservation();
466 * Max Gaps Threshold for performing a conservation calculation TODO: make
467 * this a configurable property - or global to an alignment view
469 private int consPercGaps = 25;
472 * calculate residue conservation for group - but only if necessary.
474 public void recalcConservation()
476 if (cs == null && consensus == null && conservation == null)
482 cs.alignmentChanged(this);
486 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
487 endRes + 1, showSequenceLogo);
488 if (consensus != null)
490 _updateConsensusRow(cnsns);
494 cs.setConsensus(cnsns);
496 if (cs instanceof ClustalxColourScheme)
498 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
502 if ((conservation != null)
503 || (cs != null && cs.conservationApplied()))
505 Conservation c = new Conservation(groupName,
506 ResidueProperties.propHash, 3, sequences, startRes,
509 c.verdict(false, consPercGaps);
510 if (conservation != null)
512 _updateConservationRow(c);
516 if (cs.conservationApplied())
518 cs.setConservation(c);
520 if (cs instanceof ClustalxColourScheme)
522 ((ClustalxColourScheme) cs).resetClustalX(sequences,
528 } catch (java.lang.OutOfMemoryError err)
531 System.out.println("Out of memory loading groups: " + err);
536 private void _updateConservationRow(Conservation c)
538 if (conservation == null)
543 conservation.label = "Conservation for " + getName();
544 conservation.description = "Conservation for group " + getName()
545 + " less than " + consPercGaps + "% gaps";
546 // preserve width if already set
547 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
550 conservation.annotations = null;
551 conservation.annotations = new Annotation[aWidth]; // should be alignment
553 c.completeAnnotations(conservation, null, startRes, endRes + 1);
556 public Hashtable[] consensusData = null;
558 private void _updateConsensusRow(Hashtable[] cnsns)
560 if (consensus == null)
564 consensus.label = "Consensus for " + getName();
565 consensus.description = "Percent Identity";
566 consensusData = cnsns;
567 // preserve width if already set
568 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
571 consensus.annotations = null;
572 consensus.annotations = new Annotation[aWidth]; // should be alignment width
574 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
575 ignoreGapsInConsensus, showSequenceLogo); // TODO: setting container
577 // ignoreGapsInConsensusCalculation);
582 * sequence to either add or remove from group
584 * flag passed to delete/addSequence to indicate if group properties
585 * should be recalculated
587 public void addOrRemove(SequenceI s, boolean recalc)
589 if (sequences.contains(s))
591 deleteSequence(s, recalc);
595 addSequence(s, recalc);
607 public void deleteSequence(SequenceI s, boolean recalc)
609 sequences.removeElement(s);
613 recalcConservation();
620 * @return DOCUMENT ME!
622 public int getStartRes()
630 * @return DOCUMENT ME!
632 public int getEndRes()
638 * Set the first column selected by this group. Runs from 0<=i<N_cols
642 public void setStartRes(int i)
648 * Set the groups last selected column. Runs from 0<=i<N_cols
652 public void setEndRes(int i)
660 * @return DOCUMENT ME!
664 return sequences.size();
673 * @return DOCUMENT ME!
675 public SequenceI getSequenceAt(int i)
677 return (SequenceI) sequences.elementAt(i);
686 public void setColourText(boolean state)
694 * @return DOCUMENT ME!
696 public boolean getColourText()
707 public void setDisplayText(boolean state)
715 * @return DOCUMENT ME!
717 public boolean getDisplayText()
728 public void setDisplayBoxes(boolean state)
730 displayBoxes = state;
736 * @return DOCUMENT ME!
738 public boolean getDisplayBoxes()
746 * @return DOCUMENT ME!
748 public int getWidth()
750 // MC This needs to get reset when characters are inserted and deleted
751 if (sequences.size() > 0)
753 width = ((SequenceI) sequences.elementAt(0)).getLength();
756 for (int i = 1; i < sequences.size(); i++)
758 SequenceI seq = (SequenceI) sequences.elementAt(i);
760 if (seq.getLength() > width)
762 width = seq.getLength();
775 public void setOutlineColour(Color c)
783 * @return DOCUMENT ME!
785 public Color getOutlineColour()
787 return outlineColour;
792 * returns the sequences in the group ordered by the ordering given by al.
793 * this used to return an array with null entries regardless, new behaviour is
794 * below. TODO: verify that this does not affect use in applet or application
798 * @return SequenceI[] intersection of sequences in group with al, ordered by
799 * al, or null if group does not intersect with al
801 public SequenceI[] getSequencesInOrder(AlignmentI al)
803 return getSequencesInOrder(al, true);
807 * return an array representing the intersection of the group with al,
808 * optionally returning an array the size of al.getHeight() where nulls mark
809 * the non-intersected sequences
813 * @return null or array
815 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
817 int sSize = sequences.size();
818 int alHeight = al.getHeight();
820 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
823 for (int i = 0; i < alHeight && index < sSize; i++)
825 if (sequences.contains(al.getSequenceAt(i)))
827 seqs[(trim) ? index : i] = al.getSequenceAt(i);
839 if (index < seqs.length)
841 SequenceI[] dummy = seqs;
842 seqs = new SequenceI[index];
845 seqs[index] = dummy[index];
853 * @return the idColour
855 public Color getIdColour()
862 * the idColour to set
864 public void setIdColour(Color idColour)
866 this.idColour = idColour;
870 * @return the representative sequence for this group
872 public SequenceI getSeqrep()
878 * set the representative sequence for this group. Note - this affects the
879 * interpretation of the Hidereps attribute.
882 * the seqrep to set (null means no sequence representative)
884 public void setSeqrep(SequenceI seqrep)
886 this.seqrep = seqrep;
891 * @return true if group has a sequence representative
893 public boolean hasSeqrep()
895 return seqrep != null;
899 * visibility of rows or represented rows covered by group
901 private boolean hidereps = false;
904 * set visibility of sequences covered by (if no sequence representative is
905 * defined) or represented by this group.
909 public void setHidereps(boolean visibility)
911 hidereps = visibility;
916 * @return true if sequences represented (or covered) by this group should be
919 public boolean isHidereps()
925 * visibility of columns intersecting this group
927 private boolean hidecols = false;
930 * set intended visibility of columns covered by this group
934 public void setHideCols(boolean visibility)
936 hidecols = visibility;
941 * @return true if columns covered by group should be hidden
943 public boolean isHideCols()
949 * create a new sequence group from the intersection of this group with an
950 * alignment Hashtable of hidden representatives
956 * @return new group containing sequences common to this group and alignment
958 public SequenceGroup intersect(AlignmentI alignment, Hashtable hashtable)
960 SequenceGroup sgroup = new SequenceGroup(this);
961 SequenceI[] insect = getSequencesInOrder(alignment);
962 sgroup.sequences = new Vector();
963 for (int s = 0; insect != null && s < insect.length; s++)
965 if (hashtable == null || hashtable.containsKey(insect[s]))
967 sgroup.sequences.addElement(insect[s]);
970 // Enumeration en =getSequences(hashtable).elements();
971 // while (en.hasMoreElements())
973 // SequenceI elem = (SequenceI) en.nextElement();
974 // if (alignment.getSequences().contains(elem))
976 // sgroup.addSequence(elem, false);
983 * @return the showUnconserved
985 public boolean getShowNonconserved()
987 return showNonconserved;
991 * @param showNonconserved
992 * the showUnconserved to set
994 public void setShowNonconserved(boolean displayNonconserved)
996 this.showNonconserved = displayNonconserved;
999 AlignmentAnnotation consensus = null, conservation = null;
1002 * flag indicating if consensus histogram should be rendered
1004 private boolean showConsensusHistogram;
1007 * set this alignmentAnnotation object as the one used to render consensus
1012 public void setConsensus(AlignmentAnnotation aan)
1014 if (consensus == null)
1022 * @return automatically calculated consensus row
1024 public AlignmentAnnotation getConsensus()
1026 // TODO get or calculate and get consensus annotation row for this group
1027 int aWidth = this.getWidth();
1035 if (consensus == null)
1037 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1038 100f, AlignmentAnnotation.BAR_GRAPH);
1040 consensus.hasText = true;
1041 consensus.autoCalculated = true;
1042 consensus.groupRef = this;
1043 consensus.label = "Consensus for " + getName();
1044 consensus.description = "Percent Identity";
1049 * set this alignmentAnnotation object as the one used to render consensus
1054 public void setConservationRow(AlignmentAnnotation aan)
1056 if (conservation == null)
1063 * get the conservation annotation row for this group
1065 * @return autoCalculated annotation row
1067 public AlignmentAnnotation getConservationRow()
1069 if (conservation == null)
1071 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1072 11f, AlignmentAnnotation.BAR_GRAPH);
1075 conservation.hasText = true;
1076 conservation.autoCalculated = true;
1077 conservation.groupRef = this;
1078 conservation.label = "Conservation for " + getName();
1079 conservation.description = "Conservation for group " + getName()
1080 + " less than " + consPercGaps + "% gaps";
1081 return conservation;
1086 * @return true if annotation rows have been instantiated for this group
1088 public boolean hasAnnotationRows()
1090 return consensus != null || conservation != null;
1093 public SequenceI getConsensusSeq()
1096 StringBuffer seqs = new StringBuffer();
1097 for (int i = 0; i < consensus.annotations.length; i++)
1099 if (consensus.annotations[i] != null)
1101 if (consensus.annotations[i].description.charAt(0) == '[')
1103 seqs.append(consensus.annotations[i].description.charAt(1));
1107 seqs.append(consensus.annotations[i].displayCharacter);
1112 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1114 sq.setDescription("Percentage Identity Consensus "
1115 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1119 public void setIgnoreGapsConsensus(boolean state)
1121 if (this.ignoreGapsInConsensus != state && consensus != null)
1123 ignoreGapsInConsensus = state;
1124 recalcConservation();
1126 ignoreGapsInConsensus = state;
1129 public boolean getIgnoreGapsConsensus()
1131 return ignoreGapsInConsensus;
1135 * @param showSequenceLogo
1136 * indicates if a sequence logo is shown for consensus annotation
1138 public void setshowSequenceLogo(boolean showSequenceLogo)
1140 // TODO: decouple calculation from settings update
1141 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1143 this.showSequenceLogo = showSequenceLogo;
1144 recalcConservation();
1146 this.showSequenceLogo = showSequenceLogo;
1151 * @param showConsHist
1152 * flag indicating if the consensus histogram for this group should
1155 public void setShowConsensusHistogram(boolean showConsHist)
1158 if (showConsensusHistogram != showConsHist && consensus != null)
1160 this.showConsensusHistogram = showConsHist;
1161 recalcConservation();
1163 this.showConsensusHistogram = showConsHist;
1167 * @return the showConsensusHistogram
1169 public boolean isShowConsensusHistogram()
1171 return showConsensusHistogram;
1176 * returns a new array with all annotation involving this group
1178 public AlignmentAnnotation[] getAlignmentAnnotation()
1180 // TODO add in other methods like 'getAlignmentAnnotation(String label), etc'
1181 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1182 for (SequenceI seq:(Vector<SequenceI>)sequences)
1184 for (AlignmentAnnotation al: seq.getAnnotation())
1186 if (al.groupRef==this)
1192 if (consensus!=null)
1194 annot.add(consensus);
1196 if (conservation!=null)
1198 annot.add(conservation);
1200 return annot.toArray(new AlignmentAnnotation[0]);