2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 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
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)
483 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
484 endRes + 1, showSequenceLogo);
485 if (consensus != null)
487 _updateConsensusRow(cnsns);
491 cs.setConsensus(cnsns);
493 if (cs instanceof ClustalxColourScheme)
495 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
499 if ((conservation != null)
500 || (cs != null && cs.conservationApplied()))
502 Conservation c = new Conservation(groupName,
503 ResidueProperties.propHash, 3, sequences, startRes,
506 c.verdict(false, consPercGaps);
507 if (conservation != null)
509 _updateConservationRow(c);
513 cs.setConservation(c);
515 if (cs instanceof ClustalxColourScheme)
517 ((ClustalxColourScheme) cs)
518 .resetClustalX(sequences, getWidth());
522 } catch (java.lang.OutOfMemoryError err)
525 System.out.println("Out of memory loading groups: " + err);
530 private void _updateConservationRow(Conservation c)
532 if (conservation == null)
537 conservation.label = "Conservation for " + getName();
538 conservation.description = "Conservation for group " + getName()
539 + " less than " + consPercGaps + "% gaps";
540 // preserve width if already set
541 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
544 conservation.annotations = null;
545 conservation.annotations = new Annotation[aWidth]; // should be alignment
547 c.completeAnnotations(conservation, null, startRes, endRes + 1);
550 public Hashtable[] consensusData = null;
552 private void _updateConsensusRow(Hashtable[] cnsns)
554 if (consensus == null)
558 consensus.label = "Consensus for " + getName();
559 consensus.description = "Percent Identity";
560 consensusData = cnsns;
561 // preserve width if already set
562 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
565 consensus.annotations = null;
566 consensus.annotations = new Annotation[aWidth]; // should be alignment width
568 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
569 ignoreGapsInConsensus, showSequenceLogo); // TODO: setting container
571 // ignoreGapsInConsensusCalculation);
576 * sequence to either add or remove from group
578 * flag passed to delete/addSequence to indicate if group properties
579 * should be recalculated
581 public void addOrRemove(SequenceI s, boolean recalc)
583 if (sequences.contains(s))
585 deleteSequence(s, recalc);
589 addSequence(s, recalc);
601 public void deleteSequence(SequenceI s, boolean recalc)
603 sequences.removeElement(s);
607 recalcConservation();
614 * @return DOCUMENT ME!
616 public int getStartRes()
624 * @return DOCUMENT ME!
626 public int getEndRes()
632 * Set the first column selected by this group. Runs from 0<=i<N_cols
636 public void setStartRes(int i)
642 * Set the groups last selected column. Runs from 0<=i<N_cols
646 public void setEndRes(int i)
654 * @return DOCUMENT ME!
658 return sequences.size();
667 * @return DOCUMENT ME!
669 public SequenceI getSequenceAt(int i)
671 return (SequenceI) sequences.elementAt(i);
680 public void setColourText(boolean state)
688 * @return DOCUMENT ME!
690 public boolean getColourText()
701 public void setDisplayText(boolean state)
709 * @return DOCUMENT ME!
711 public boolean getDisplayText()
722 public void setDisplayBoxes(boolean state)
724 displayBoxes = state;
730 * @return DOCUMENT ME!
732 public boolean getDisplayBoxes()
740 * @return DOCUMENT ME!
742 public int getWidth()
744 // MC This needs to get reset when characters are inserted and deleted
745 if (sequences.size() > 0)
747 width = ((SequenceI) sequences.elementAt(0)).getLength();
750 for (int i = 1; i < sequences.size(); i++)
752 SequenceI seq = (SequenceI) sequences.elementAt(i);
754 if (seq.getLength() > width)
756 width = seq.getLength();
769 public void setOutlineColour(Color c)
777 * @return DOCUMENT ME!
779 public Color getOutlineColour()
781 return outlineColour;
786 * returns the sequences in the group ordered by the ordering given by al.
787 * this used to return an array with null entries regardless, new behaviour is
788 * below. TODO: verify that this does not affect use in applet or application
792 * @return SequenceI[] intersection of sequences in group with al, ordered by
793 * al, or null if group does not intersect with al
795 public SequenceI[] getSequencesInOrder(AlignmentI al)
797 return getSequencesInOrder(al, true);
801 * return an array representing the intersection of the group with al,
802 * optionally returning an array the size of al.getHeight() where nulls mark
803 * the non-intersected sequences
807 * @return null or array
809 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
811 int sSize = sequences.size();
812 int alHeight = al.getHeight();
814 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
817 for (int i = 0; i < alHeight && index < sSize; i++)
819 if (sequences.contains(al.getSequenceAt(i)))
821 seqs[(trim) ? index : i] = al.getSequenceAt(i);
833 if (index < seqs.length)
835 SequenceI[] dummy = seqs;
836 seqs = new SequenceI[index];
839 seqs[index] = dummy[index];
847 * @return the idColour
849 public Color getIdColour()
856 * the idColour to set
858 public void setIdColour(Color idColour)
860 this.idColour = idColour;
864 * @return the representative sequence for this group
866 public SequenceI getSeqrep()
872 * set the representative sequence for this group. Note - this affects the
873 * interpretation of the Hidereps attribute.
876 * the seqrep to set (null means no sequence representative)
878 public void setSeqrep(SequenceI seqrep)
880 this.seqrep = seqrep;
885 * @return true if group has a sequence representative
887 public boolean hasSeqrep()
889 return seqrep != null;
893 * visibility of rows or represented rows covered by group
895 private boolean hidereps = false;
898 * set visibility of sequences covered by (if no sequence representative is
899 * defined) or represented by this group.
903 public void setHidereps(boolean visibility)
905 hidereps = visibility;
910 * @return true if sequences represented (or covered) by this group should be
913 public boolean isHidereps()
919 * visibility of columns intersecting this group
921 private boolean hidecols = false;
924 * set intended visibility of columns covered by this group
928 public void setHideCols(boolean visibility)
930 hidecols = visibility;
935 * @return true if columns covered by group should be hidden
937 public boolean isHideCols()
943 * create a new sequence group from the intersection of this group with an
944 * alignment Hashtable of hidden representatives
950 * @return new group containing sequences common to this group and alignment
952 public SequenceGroup intersect(AlignmentI alignment, Hashtable hashtable)
954 SequenceGroup sgroup = new SequenceGroup(this);
955 SequenceI[] insect = getSequencesInOrder(alignment);
956 sgroup.sequences = new Vector();
957 for (int s = 0; insect != null && s < insect.length; s++)
959 if (hashtable == null || hashtable.containsKey(insect[s]))
961 sgroup.sequences.addElement(insect[s]);
964 // Enumeration en =getSequences(hashtable).elements();
965 // while (en.hasMoreElements())
967 // SequenceI elem = (SequenceI) en.nextElement();
968 // if (alignment.getSequences().contains(elem))
970 // sgroup.addSequence(elem, false);
977 * @return the showUnconserved
979 public boolean getShowNonconserved()
981 return showNonconserved;
985 * @param showNonconserved
986 * the showUnconserved to set
988 public void setShowNonconserved(boolean displayNonconserved)
990 this.showNonconserved = displayNonconserved;
993 AlignmentAnnotation consensus = null, conservation = null;
996 * flag indicating if consensus histogram should be rendered
998 private boolean showConsensusHistogram;
1002 * @return automatically calculated consensus row
1004 public AlignmentAnnotation getConsensus()
1006 // TODO get or calculate and get consensus annotation row for this group
1007 int aWidth = this.getWidth();
1015 if (consensus == null)
1017 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1018 100f, AlignmentAnnotation.BAR_GRAPH);
1020 consensus.hasText = true;
1021 consensus.autoCalculated = true;
1022 consensus.groupRef = this;
1023 consensus.label = "Consensus for " + getName();
1024 consensus.description = "Percent Identity";
1029 * get the conservation annotation row for this group
1031 * @return autoCalculated annotation row
1033 public AlignmentAnnotation getConservationRow()
1035 if (conservation == null)
1037 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1038 11f, AlignmentAnnotation.BAR_GRAPH);
1041 conservation.hasText = true;
1042 conservation.autoCalculated = true;
1043 conservation.groupRef = this;
1044 conservation.label = "Conservation for " + getName();
1045 conservation.description = "Conservation for group " + getName()
1046 + " less than " + consPercGaps + "% gaps";
1047 return conservation;
1052 * @return true if annotation rows have been instantiated for this group
1054 public boolean hasAnnotationRows()
1056 return consensus != null || conservation != null;
1059 public SequenceI getConsensusSeq()
1062 StringBuffer seqs = new StringBuffer();
1063 for (int i = 0; i < consensus.annotations.length; i++)
1065 if (consensus.annotations[i] != null)
1067 if (consensus.annotations[i].description.charAt(0) == '[')
1069 seqs.append(consensus.annotations[i].description.charAt(1));
1073 seqs.append(consensus.annotations[i].displayCharacter);
1078 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1080 sq.setDescription("Percentage Identity Consensus "
1081 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1085 public void setIgnoreGapsConsensus(boolean state)
1087 if (this.ignoreGapsInConsensus != state && consensus != null)
1089 ignoreGapsInConsensus = state;
1090 recalcConservation();
1092 ignoreGapsInConsensus = state;
1095 public boolean getIgnoreGapsConsensus()
1097 return ignoreGapsInConsensus;
1101 * @param showSequenceLogo
1102 * indicates if a sequence logo is shown for consensus annotation
1104 public void setshowSequenceLogo(boolean showSequenceLogo)
1106 // TODO: decouple calculation from settings update
1107 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1109 this.showSequenceLogo = showSequenceLogo;
1110 recalcConservation();
1112 this.showSequenceLogo = showSequenceLogo;
1117 * @param showConsHist
1118 * flag indicating if the consensus histogram for this group should
1121 public void setShowConsensusHistogram(boolean showConsHist)
1124 if (showConsensusHistogram != showConsHist && consensus != null)
1126 this.showConsensusHistogram = showConsHist;
1127 recalcConservation();
1129 this.showConsensusHistogram = showConsHist;
1133 * @return the showConsensusHistogram
1135 public boolean isShowConsensusHistogram()
1137 return showConsensusHistogram;