2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
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.
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.
21 package jalview.datamodel;
24 import java.util.List;
28 import jalview.analysis.*;
29 import jalview.schemes.*;
32 * Collects a set contiguous ranges on a set of sequences
37 public class SequenceGroup implements AnnotatedCollectionI
43 Conservation conserve;
47 boolean displayBoxes = true;
49 boolean displayText = true;
51 boolean colourText = false;
54 * after Olivier's non-conserved only character display
56 boolean showNonconserved = false;
61 private Vector<SequenceI> sequences = new Vector<SequenceI>();
64 * representative sequence for this group (if any)
66 private SequenceI seqrep = null;
71 * Colourscheme applied to group if any
73 public ColourSchemeI cs;
79 public Color outlineColour = Color.black;
81 public Color idColour = null;
83 public int thresholdTextColour = 0;
85 public Color textColour = Color.black;
87 public Color textColour2 = Color.white;
90 * consensus calculation property
92 private boolean ignoreGapsInConsensus = true;
95 * consensus calculation property
97 private boolean showSequenceLogo = false;
100 * flag indicating if logo should be rendered normalised
102 private boolean normaliseSequenceLogo;
105 * @return the includeAllConsSymbols
107 public boolean isShowSequenceLogo()
109 return showSequenceLogo;
113 * Creates a new SequenceGroup object.
115 public SequenceGroup()
117 groupName = "JGroup:" + this.hashCode();
121 * Creates a new SequenceGroup object.
126 * @param displayBoxes
130 * first column of group
132 * last column of group
134 public SequenceGroup(Vector sequences, String groupName,
135 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
136 boolean colourText, int start, int end)
138 this.sequences = sequences;
139 this.groupName = groupName;
140 this.displayBoxes = displayBoxes;
141 this.displayText = displayText;
142 this.colourText = colourText;
146 recalcConservation();
154 public SequenceGroup(SequenceGroup seqsel)
158 sequences = new Vector();
159 Enumeration<SequenceI> sq = seqsel.sequences.elements();
160 while (sq.hasMoreElements())
162 sequences.addElement(sq.nextElement());
165 if (seqsel.groupName != null)
167 groupName = new String(seqsel.groupName);
169 displayBoxes = seqsel.displayBoxes;
170 displayText = seqsel.displayText;
171 colourText = seqsel.colourText;
172 startRes = seqsel.startRes;
173 endRes = seqsel.endRes;
175 if (seqsel.description != null)
176 description = new String(seqsel.description);
177 hidecols = seqsel.hidecols;
178 hidereps = seqsel.hidereps;
179 idColour = seqsel.idColour;
180 outlineColour = seqsel.outlineColour;
181 seqrep = seqsel.seqrep;
182 textColour = seqsel.textColour;
183 textColour2 = seqsel.textColour2;
184 thresholdTextColour = seqsel.thresholdTextColour;
185 width = seqsel.width;
186 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
187 if (seqsel.conserve != null)
189 recalcConservation(); // safer than
190 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
195 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
197 int iSize = sequences.size();
198 SequenceI[] seqs = new SequenceI[iSize];
199 SequenceI[] inorder = getSequencesInOrder(align);
201 for (int i = 0, ipos = 0; i < inorder.length; i++)
203 SequenceI seq = inorder[i];
205 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
206 if (seqs[ipos] != null)
208 seqs[ipos].setDescription(seq.getDescription());
209 seqs[ipos].setDBRef(seq.getDBRef());
210 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
211 if (seq.getDatasetSequence() != null)
213 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
216 if (seq.getAnnotation() != null)
218 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
219 // Only copy annotation that is either a score or referenced by the
220 // alignment's annotation vector
221 for (int a = 0; a < seq.getAnnotation().length; a++)
223 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
226 boolean found = false;
227 for (int pos = 0; pos < alann.length; pos++)
229 if (alann[pos] == tocopy)
238 AlignmentAnnotation newannot = new AlignmentAnnotation(
239 seq.getAnnotation()[a]);
240 newannot.restrict(startRes, endRes);
241 newannot.setSequenceRef(seqs[ipos]);
242 newannot.adjustForAlignment();
243 seqs[ipos].addAlignmentAnnotation(newannot);
253 if (iSize != inorder.length)
255 SequenceI[] nseqs = new SequenceI[iSize];
256 System.arraycopy(seqs, 0, nseqs, 0, iSize);
264 * If sequence ends in gaps, the end residue can be correctly calculated here
270 public int findEndRes(SequenceI seq)
275 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
277 ch = seq.getCharAt(j);
278 if (!jalview.util.Comparison.isGap((ch)))
286 eres += seq.getStart() - 1;
292 public List<SequenceI> getSequences()
297 public List<SequenceI> getSequences(
298 Map<SequenceI, SequenceCollectionI> hiddenReps)
300 if (hiddenReps == null)
306 Vector allSequences = new Vector();
308 for (int i = 0; i < sequences.size(); i++)
310 seq = (SequenceI) sequences.elementAt(i);
311 allSequences.addElement(seq);
312 if (hiddenReps.containsKey(seq))
314 SequenceCollectionI hsg = hiddenReps.get(seq);
315 for (SequenceI seq2 : hsg.getSequences())
317 if (seq2 != seq && !allSequences.contains(seq2))
319 allSequences.addElement(seq2);
329 public SequenceI[] getSequencesAsArray(
330 Map<SequenceI, SequenceCollectionI> map)
332 List<SequenceI> tmp = getSequences(map);
337 return tmp.toArray(new SequenceI[tmp.size()]);
346 * @return DOCUMENT ME!
348 public boolean adjustForRemoveLeft(int col)
350 // return value is true if the group still exists
353 startRes = startRes - col;
358 endRes = endRes - col;
360 if (startRes > endRes)
367 // must delete this group!!
380 * @return DOCUMENT ME!
382 public boolean adjustForRemoveRight(int col)
401 * @return DOCUMENT ME!
403 public String getName()
408 public String getDescription()
419 public void setName(String name)
422 // TODO: URGENT: update dependent objects (annotation row)
425 public void setDescription(String desc)
433 * @return DOCUMENT ME!
435 public Conservation getConservation()
446 public void setConservation(Conservation c)
452 * Add s to this sequence group. If aligment sequence is already contained in
453 * group, it will not be added again, but recalculation may happen if the flag
457 * alignment sequence to be added
459 * true means Group's conservation should be recalculated
461 public void addSequence(SequenceI s, boolean recalc)
463 if (s != null && !sequences.contains(s))
465 sequences.addElement(s);
470 recalcConservation();
475 * Max Gaps Threshold (percent) for performing a conservation calculation
477 private int consPercGaps = 25;
480 * @return Max Gaps Threshold for performing a conservation calculation
482 public int getConsPercGaps()
488 * set Max Gaps Threshold (percent) for performing a conservation calculation
489 * @param consPercGaps
491 public void setConsPercGaps(int consPercGaps)
493 this.consPercGaps = consPercGaps;
497 * calculate residue conservation for group - but only if necessary.
499 public void recalcConservation()
501 if (cs == null && consensus == null && conservation == null)
507 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
508 endRes + 1, showSequenceLogo);
509 if (consensus != null)
511 _updateConsensusRow(cnsns, sequences.size());
515 cs.setConsensus(cnsns);
518 if ((conservation != null)
519 || (cs != null && cs.conservationApplied()))
521 Conservation c = new Conservation(groupName,
522 ResidueProperties.propHash, 3, sequences, startRes,
525 c.verdict(false, consPercGaps);
526 if (conservation != null)
528 _updateConservationRow(c);
532 if (cs.conservationApplied())
534 cs.setConservation(c);
540 cs.alignmentChanged(context!=null ? context : this, null);
542 } catch (java.lang.OutOfMemoryError err)
545 System.out.println("Out of memory loading groups: " + err);
550 private void _updateConservationRow(Conservation c)
552 if (conservation == null)
557 conservation.label = "Conservation for " + getName();
558 conservation.description = "Conservation for group " + getName()
559 + " less than " + consPercGaps + "% gaps";
560 // preserve width if already set
561 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
564 conservation.annotations = null;
565 conservation.annotations = new Annotation[aWidth]; // should be alignment
567 c.completeAnnotations(conservation, null, startRes, endRes + 1);
570 public Hashtable[] consensusData = null;
572 private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
574 if (consensus == null)
578 consensus.label = "Consensus for " + getName();
579 consensus.description = "Percent Identity";
580 consensusData = cnsns;
581 // preserve width if already set
582 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
585 consensus.annotations = null;
586 consensus.annotations = new Annotation[aWidth]; // should be alignment width
588 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
589 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting container
591 // ignoreGapsInConsensusCalculation);
596 * sequence to either add or remove from group
598 * flag passed to delete/addSequence to indicate if group properties
599 * should be recalculated
601 public void addOrRemove(SequenceI s, boolean recalc)
603 if (sequences.contains(s))
605 deleteSequence(s, recalc);
609 addSequence(s, recalc);
621 public void deleteSequence(SequenceI s, boolean recalc)
623 sequences.removeElement(s);
627 recalcConservation();
634 * @return the first column selected by this group. Runs from 0<=i<N_cols
636 public int getStartRes()
643 * @return the groups last selected column. Runs from 0<=i<N_cols
645 public int getEndRes()
651 * Set the first column selected by this group. Runs from 0<=i<N_cols
655 public void setStartRes(int i)
661 * Set the groups last selected column. Runs from 0<=i<N_cols
665 public void setEndRes(int i)
673 * @return DOCUMENT ME!
677 return sequences.size();
686 * @return DOCUMENT ME!
688 public SequenceI getSequenceAt(int i)
690 return (SequenceI) sequences.elementAt(i);
699 public void setColourText(boolean state)
707 * @return DOCUMENT ME!
709 public boolean getColourText()
720 public void setDisplayText(boolean state)
728 * @return DOCUMENT ME!
730 public boolean getDisplayText()
741 public void setDisplayBoxes(boolean state)
743 displayBoxes = state;
749 * @return DOCUMENT ME!
751 public boolean getDisplayBoxes()
759 * @return DOCUMENT ME!
761 public int getWidth()
763 // MC This needs to get reset when characters are inserted and deleted
764 if (sequences.size() > 0)
766 width = ((SequenceI) sequences.elementAt(0)).getLength();
769 for (int i = 1; i < sequences.size(); i++)
771 SequenceI seq = (SequenceI) sequences.elementAt(i);
773 if (seq.getLength() > width)
775 width = seq.getLength();
788 public void setOutlineColour(Color c)
796 * @return DOCUMENT ME!
798 public Color getOutlineColour()
800 return outlineColour;
805 * returns the sequences in the group ordered by the ordering given by al.
806 * this used to return an array with null entries regardless, new behaviour is
807 * below. TODO: verify that this does not affect use in applet or application
811 * @return SequenceI[] intersection of sequences in group with al, ordered by
812 * al, or null if group does not intersect with al
814 public SequenceI[] getSequencesInOrder(AlignmentI al)
816 return getSequencesInOrder(al, true);
820 * return an array representing the intersection of the group with al,
821 * optionally returning an array the size of al.getHeight() where nulls mark
822 * the non-intersected sequences
826 * @return null or array
828 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
830 int sSize = sequences.size();
831 int alHeight = al.getHeight();
833 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
836 for (int i = 0; i < alHeight && index < sSize; i++)
838 if (sequences.contains(al.getSequenceAt(i)))
840 seqs[(trim) ? index : i] = al.getSequenceAt(i);
852 if (index < seqs.length)
854 SequenceI[] dummy = seqs;
855 seqs = new SequenceI[index];
858 seqs[index] = dummy[index];
866 * @return the idColour
868 public Color getIdColour()
875 * the idColour to set
877 public void setIdColour(Color idColour)
879 this.idColour = idColour;
883 * @return the representative sequence for this group
885 public SequenceI getSeqrep()
891 * set the representative sequence for this group. Note - this affects the
892 * interpretation of the Hidereps attribute.
895 * the seqrep to set (null means no sequence representative)
897 public void setSeqrep(SequenceI seqrep)
899 this.seqrep = seqrep;
904 * @return true if group has a sequence representative
906 public boolean hasSeqrep()
908 return seqrep != null;
912 * visibility of rows or represented rows covered by group
914 private boolean hidereps = false;
917 * set visibility of sequences covered by (if no sequence representative is
918 * defined) or represented by this group.
922 public void setHidereps(boolean visibility)
924 hidereps = visibility;
929 * @return true if sequences represented (or covered) by this group should be
932 public boolean isHidereps()
938 * visibility of columns intersecting this group
940 private boolean hidecols = false;
943 * set intended visibility of columns covered by this group
947 public void setHideCols(boolean visibility)
949 hidecols = visibility;
954 * @return true if columns covered by group should be hidden
956 public boolean isHideCols()
962 * create a new sequence group from the intersection of this group with an
963 * alignment Hashtable of hidden representatives
969 * @return new group containing sequences common to this group and alignment
971 public SequenceGroup intersect(AlignmentI alignment,
972 Map<SequenceI, SequenceCollectionI> map)
974 SequenceGroup sgroup = new SequenceGroup(this);
975 SequenceI[] insect = getSequencesInOrder(alignment);
976 sgroup.sequences = new Vector();
977 for (int s = 0; insect != null && s < insect.length; s++)
979 if (map == null || map.containsKey(insect[s]))
981 sgroup.sequences.addElement(insect[s]);
984 // Enumeration en =getSequences(hashtable).elements();
985 // while (en.hasMoreElements())
987 // SequenceI elem = (SequenceI) en.nextElement();
988 // if (alignment.getSequences().contains(elem))
990 // sgroup.addSequence(elem, false);
997 * @return the showUnconserved
999 public boolean getShowNonconserved()
1001 return showNonconserved;
1005 * @param showNonconserved
1006 * the showUnconserved to set
1008 public void setShowNonconserved(boolean displayNonconserved)
1010 this.showNonconserved = displayNonconserved;
1013 AlignmentAnnotation consensus = null, conservation = null;
1016 * flag indicating if consensus histogram should be rendered
1018 private boolean showConsensusHistogram;
1021 * set this alignmentAnnotation object as the one used to render consensus
1026 public void setConsensus(AlignmentAnnotation aan)
1028 if (consensus == null)
1036 * @return automatically calculated consensus row
1038 public AlignmentAnnotation getConsensus()
1040 // TODO get or calculate and get consensus annotation row for this group
1041 int aWidth = this.getWidth();
1049 if (consensus == null)
1051 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1052 100f, AlignmentAnnotation.BAR_GRAPH);
1053 consensus.hasText = true;
1054 consensus.autoCalculated = true;
1055 consensus.groupRef = this;
1056 consensus.label = "Consensus for " + getName();
1057 consensus.description = "Percent Identity";
1063 * set this alignmentAnnotation object as the one used to render consensus
1068 public void setConservationRow(AlignmentAnnotation aan)
1070 if (conservation == null)
1077 * get the conservation annotation row for this group
1079 * @return autoCalculated annotation row
1081 public AlignmentAnnotation getConservationRow()
1083 if (conservation == null)
1085 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1086 11f, AlignmentAnnotation.BAR_GRAPH);
1089 conservation.hasText = true;
1090 conservation.autoCalculated = true;
1091 conservation.groupRef = this;
1092 conservation.label = "Conservation for " + getName();
1093 conservation.description = "Conservation for group " + getName()
1094 + " less than " + consPercGaps + "% gaps";
1095 return conservation;
1100 * @return true if annotation rows have been instantiated for this group
1102 public boolean hasAnnotationRows()
1104 return consensus != null || conservation != null;
1107 public SequenceI getConsensusSeq()
1110 StringBuffer seqs = new StringBuffer();
1111 for (int i = 0; i < consensus.annotations.length; i++)
1113 if (consensus.annotations[i] != null)
1115 if (consensus.annotations[i].description.charAt(0) == '[')
1117 seqs.append(consensus.annotations[i].description.charAt(1));
1121 seqs.append(consensus.annotations[i].displayCharacter);
1126 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1128 sq.setDescription("Percentage Identity Consensus "
1129 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1133 public void setIgnoreGapsConsensus(boolean state)
1135 if (this.ignoreGapsInConsensus != state && consensus != null)
1137 ignoreGapsInConsensus = state;
1138 recalcConservation();
1140 ignoreGapsInConsensus = state;
1143 public boolean getIgnoreGapsConsensus()
1145 return ignoreGapsInConsensus;
1149 * @param showSequenceLogo
1150 * indicates if a sequence logo is shown for consensus annotation
1152 public void setshowSequenceLogo(boolean showSequenceLogo)
1154 // TODO: decouple calculation from settings update
1155 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1157 this.showSequenceLogo = showSequenceLogo;
1158 recalcConservation();
1160 this.showSequenceLogo = showSequenceLogo;
1165 * @param showConsHist
1166 * flag indicating if the consensus histogram for this group should
1169 public void setShowConsensusHistogram(boolean showConsHist)
1172 if (showConsensusHistogram != showConsHist && consensus != null)
1174 this.showConsensusHistogram = showConsHist;
1175 recalcConservation();
1177 this.showConsensusHistogram = showConsHist;
1181 * @return the showConsensusHistogram
1183 public boolean isShowConsensusHistogram()
1185 return showConsensusHistogram;
1189 * set flag indicating if logo should be normalised when rendered
1193 public void setNormaliseSequenceLogo(boolean norm)
1195 normaliseSequenceLogo = norm;
1198 public boolean isNormaliseSequenceLogo()
1200 return normaliseSequenceLogo;
1205 * returns a new array with all annotation involving this group
1207 public AlignmentAnnotation[] getAlignmentAnnotation()
1209 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1211 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1212 for (SequenceI seq : (Vector<SequenceI>) sequences)
1214 AlignmentAnnotation[] aa = seq.getAnnotation();
1217 for (AlignmentAnnotation al : aa)
1219 if (al.groupRef == this)
1226 if (consensus != null)
1228 annot.add(consensus);
1230 if (conservation != null)
1232 annot.add(conservation);
1234 return annot.toArray(new AlignmentAnnotation[0]);
1238 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1240 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1241 for (AlignmentAnnotation a : getAlignmentAnnotation())
1243 if (a.getCalcId() == calcId)
1255 private AnnotatedCollectionI context;
1257 * set the alignment or group context for this group
1260 public void setContext(AnnotatedCollectionI context)
1262 this.context = context;
1265 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1268 public AnnotatedCollectionI getContext()