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;
23 import jalview.analysis.AAFrequency;
24 import jalview.analysis.Conservation;
25 import jalview.schemes.ColourSchemeI;
26 import jalview.schemes.ResidueProperties;
28 import java.awt.Color;
29 import java.util.ArrayList;
30 import java.util.Enumeration;
31 import java.util.Hashtable;
32 import java.util.List;
34 import java.util.Vector;
37 * Collects a set contiguous ranges on a set of sequences
42 public class SequenceGroup implements AnnotatedCollectionI
48 Conservation conserve;
52 boolean displayBoxes = true;
54 boolean displayText = true;
56 boolean colourText = false;
59 * after Olivier's non-conserved only character display
61 boolean showNonconserved = false;
66 private Vector<SequenceI> sequences = new Vector<SequenceI>();
69 * representative sequence for this group (if any)
71 private SequenceI seqrep = null;
76 * Colourscheme applied to group if any
78 public ColourSchemeI cs;
84 public Color outlineColour = Color.black;
86 public Color idColour = null;
88 public int thresholdTextColour = 0;
90 public Color textColour = Color.black;
92 public Color textColour2 = Color.white;
95 * consensus calculation property
97 private boolean ignoreGapsInConsensus = true;
100 * consensus calculation property
102 private boolean showSequenceLogo = false;
105 * flag indicating if logo should be rendered normalised
107 private boolean normaliseSequenceLogo;
110 * @return the includeAllConsSymbols
112 public boolean isShowSequenceLogo()
114 return showSequenceLogo;
118 * Creates a new SequenceGroup object.
120 public SequenceGroup()
122 groupName = "JGroup:" + this.hashCode();
126 * Creates a new SequenceGroup object.
131 * @param displayBoxes
135 * first column of group
137 * last column of group
139 public SequenceGroup(Vector sequences, String groupName,
140 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
141 boolean colourText, int start, int end)
143 this.sequences = sequences;
144 this.groupName = groupName;
145 this.displayBoxes = displayBoxes;
146 this.displayText = displayText;
147 this.colourText = colourText;
151 recalcConservation();
159 public SequenceGroup(SequenceGroup seqsel)
163 sequences = new Vector();
164 Enumeration<SequenceI> sq = seqsel.sequences.elements();
165 while (sq.hasMoreElements())
167 sequences.addElement(sq.nextElement());
170 if (seqsel.groupName != null)
172 groupName = new String(seqsel.groupName);
174 displayBoxes = seqsel.displayBoxes;
175 displayText = seqsel.displayText;
176 colourText = seqsel.colourText;
177 startRes = seqsel.startRes;
178 endRes = seqsel.endRes;
180 if (seqsel.description != null)
181 description = new String(seqsel.description);
182 hidecols = seqsel.hidecols;
183 hidereps = seqsel.hidereps;
184 idColour = seqsel.idColour;
185 outlineColour = seqsel.outlineColour;
186 seqrep = seqsel.seqrep;
187 textColour = seqsel.textColour;
188 textColour2 = seqsel.textColour2;
189 thresholdTextColour = seqsel.thresholdTextColour;
190 width = seqsel.width;
191 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
192 if (seqsel.conserve != null)
194 recalcConservation(); // safer than
195 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
200 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
202 int iSize = sequences.size();
203 SequenceI[] seqs = new SequenceI[iSize];
204 SequenceI[] inorder = getSequencesInOrder(align);
206 for (int i = 0, ipos = 0; i < inorder.length; i++)
208 SequenceI seq = inorder[i];
210 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
211 if (seqs[ipos] != null)
213 seqs[ipos].setDescription(seq.getDescription());
214 seqs[ipos].setDBRef(seq.getDBRef());
215 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
216 if (seq.getDatasetSequence() != null)
218 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
221 if (seq.getAnnotation() != null)
223 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
224 // Only copy annotation that is either a score or referenced by the
225 // alignment's annotation vector
226 for (int a = 0; a < seq.getAnnotation().length; a++)
228 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
231 boolean found = false;
232 for (int pos = 0; pos < alann.length; pos++)
234 if (alann[pos] == tocopy)
243 AlignmentAnnotation newannot = new AlignmentAnnotation(
244 seq.getAnnotation()[a]);
245 newannot.restrict(startRes, endRes);
246 newannot.setSequenceRef(seqs[ipos]);
247 newannot.adjustForAlignment();
248 seqs[ipos].addAlignmentAnnotation(newannot);
258 if (iSize != inorder.length)
260 SequenceI[] nseqs = new SequenceI[iSize];
261 System.arraycopy(seqs, 0, nseqs, 0, iSize);
269 * If sequence ends in gaps, the end residue can be correctly calculated here
275 public int findEndRes(SequenceI seq)
280 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
282 ch = seq.getCharAt(j);
283 if (!jalview.util.Comparison.isGap((ch)))
291 eres += seq.getStart() - 1;
298 public List<SequenceI> getSequences()
304 public List<SequenceI> getSequences(
305 Map<SequenceI, SequenceCollectionI> hiddenReps)
307 if (hiddenReps == null)
313 Vector allSequences = new Vector();
315 for (int i = 0; i < sequences.size(); i++)
317 seq = sequences.elementAt(i);
318 allSequences.addElement(seq);
319 if (hiddenReps.containsKey(seq))
321 SequenceCollectionI hsg = hiddenReps.get(seq);
322 for (SequenceI seq2 : hsg.getSequences())
324 if (seq2 != seq && !allSequences.contains(seq2))
326 allSequences.addElement(seq2);
336 public SequenceI[] getSequencesAsArray(
337 Map<SequenceI, SequenceCollectionI> map)
339 List<SequenceI> tmp = getSequences(map);
344 return tmp.toArray(new SequenceI[tmp.size()]);
353 * @return DOCUMENT ME!
355 public boolean adjustForRemoveLeft(int col)
357 // return value is true if the group still exists
360 startRes = startRes - col;
365 endRes = endRes - col;
367 if (startRes > endRes)
374 // must delete this group!!
387 * @return DOCUMENT ME!
389 public boolean adjustForRemoveRight(int col)
408 * @return DOCUMENT ME!
410 public String getName()
415 public String getDescription()
426 public void setName(String name)
429 // TODO: URGENT: update dependent objects (annotation row)
432 public void setDescription(String desc)
440 * @return DOCUMENT ME!
442 public Conservation getConservation()
453 public void setConservation(Conservation c)
459 * Add s to this sequence group. If aligment sequence is already contained in
460 * group, it will not be added again, but recalculation may happen if the flag
464 * alignment sequence to be added
466 * true means Group's conservation should be recalculated
468 public void addSequence(SequenceI s, boolean recalc)
470 if (s != null && !sequences.contains(s))
472 sequences.addElement(s);
477 recalcConservation();
482 * Max Gaps Threshold (percent) for performing a conservation calculation
484 private int consPercGaps = 25;
487 * @return Max Gaps Threshold for performing a conservation calculation
489 public int getConsPercGaps()
495 * set Max Gaps Threshold (percent) for performing a conservation calculation
497 * @param consPercGaps
499 public void setConsPercGaps(int consPercGaps)
501 this.consPercGaps = consPercGaps;
505 * calculate residue conservation for group - but only if necessary.
507 public void recalcConservation()
509 if (cs == null && consensus == null && conservation == null)
515 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
516 endRes + 1, showSequenceLogo);
517 if (consensus != null)
519 _updateConsensusRow(cnsns, sequences.size());
523 cs.setConsensus(cnsns);
526 if ((conservation != null)
527 || (cs != null && cs.conservationApplied()))
529 Conservation c = new Conservation(groupName,
530 ResidueProperties.propHash, 3, sequences, startRes,
533 c.verdict(false, consPercGaps);
534 if (conservation != null)
536 _updateConservationRow(c);
540 if (cs.conservationApplied())
542 cs.setConservation(c);
548 cs.alignmentChanged(context != null ? context : this, null);
550 } catch (java.lang.OutOfMemoryError err)
553 System.out.println("Out of memory loading groups: " + err);
558 private void _updateConservationRow(Conservation c)
560 if (conservation == null)
565 conservation.label = "Conservation for " + getName();
566 conservation.description = "Conservation for group " + getName()
567 + " less than " + consPercGaps + "% gaps";
568 // preserve width if already set
569 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
572 conservation.annotations = null;
573 conservation.annotations = new Annotation[aWidth]; // should be alignment
575 c.completeAnnotations(conservation, null, startRes, endRes + 1);
578 public Hashtable[] consensusData = null;
580 private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
582 if (consensus == null)
586 consensus.label = "Consensus for " + getName();
587 consensus.description = "Percent Identity";
588 consensusData = cnsns;
589 // preserve width if already set
590 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
593 consensus.annotations = null;
594 consensus.annotations = new Annotation[aWidth]; // should be alignment width
596 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
597 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
600 // ignoreGapsInConsensusCalculation);
605 * sequence to either add or remove from group
607 * flag passed to delete/addSequence to indicate if group properties
608 * should be recalculated
610 public void addOrRemove(SequenceI s, boolean recalc)
612 if (sequences.contains(s))
614 deleteSequence(s, recalc);
618 addSequence(s, recalc);
630 public void deleteSequence(SequenceI s, boolean recalc)
632 sequences.removeElement(s);
636 recalcConservation();
643 * @return the first column selected by this group. Runs from 0<=i<N_cols
646 public int getStartRes()
653 * @return the groups last selected column. Runs from 0<=i<N_cols
656 public int getEndRes()
662 * Set the first column selected by this group. Runs from 0<=i<N_cols
666 public void setStartRes(int i)
672 * Set the groups last selected column. Runs from 0<=i<N_cols
676 public void setEndRes(int i)
684 * @return DOCUMENT ME!
688 return sequences.size();
697 * @return DOCUMENT ME!
699 public SequenceI getSequenceAt(int i)
701 return sequences.elementAt(i);
710 public void setColourText(boolean state)
718 * @return DOCUMENT ME!
720 public boolean getColourText()
731 public void setDisplayText(boolean state)
739 * @return DOCUMENT ME!
741 public boolean getDisplayText()
752 public void setDisplayBoxes(boolean state)
754 displayBoxes = state;
760 * @return DOCUMENT ME!
762 public boolean getDisplayBoxes()
770 * @return DOCUMENT ME!
773 public int getWidth()
775 // MC This needs to get reset when characters are inserted and deleted
776 if (sequences.size() > 0)
778 width = sequences.elementAt(0).getLength();
781 for (int i = 1; i < sequences.size(); i++)
783 SequenceI seq = sequences.elementAt(i);
785 if (seq.getLength() > width)
787 width = seq.getLength();
800 public void setOutlineColour(Color c)
808 * @return DOCUMENT ME!
810 public Color getOutlineColour()
812 return outlineColour;
817 * returns the sequences in the group ordered by the ordering given by al.
818 * this used to return an array with null entries regardless, new behaviour is
819 * below. TODO: verify that this does not affect use in applet or application
823 * @return SequenceI[] intersection of sequences in group with al, ordered by
824 * al, or null if group does not intersect with al
826 public SequenceI[] getSequencesInOrder(AlignmentI al)
828 return getSequencesInOrder(al, true);
832 * return an array representing the intersection of the group with al,
833 * optionally returning an array the size of al.getHeight() where nulls mark
834 * the non-intersected sequences
838 * @return null or array
840 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
842 int sSize = sequences.size();
843 int alHeight = al.getHeight();
845 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
848 for (int i = 0; i < alHeight && index < sSize; i++)
850 if (sequences.contains(al.getSequenceAt(i)))
852 seqs[(trim) ? index : i] = al.getSequenceAt(i);
864 if (index < seqs.length)
866 SequenceI[] dummy = seqs;
867 seqs = new SequenceI[index];
870 seqs[index] = dummy[index];
878 * @return the idColour
880 public Color getIdColour()
887 * the idColour to set
889 public void setIdColour(Color idColour)
891 this.idColour = idColour;
895 * @return the representative sequence for this group
897 public SequenceI getSeqrep()
903 * set the representative sequence for this group. Note - this affects the
904 * interpretation of the Hidereps attribute.
907 * the seqrep to set (null means no sequence representative)
909 public void setSeqrep(SequenceI seqrep)
911 this.seqrep = seqrep;
916 * @return true if group has a sequence representative
918 public boolean hasSeqrep()
920 return seqrep != null;
924 * visibility of rows or represented rows covered by group
926 private boolean hidereps = false;
929 * set visibility of sequences covered by (if no sequence representative is
930 * defined) or represented by this group.
934 public void setHidereps(boolean visibility)
936 hidereps = visibility;
941 * @return true if sequences represented (or covered) by this group should be
944 public boolean isHidereps()
950 * visibility of columns intersecting this group
952 private boolean hidecols = false;
955 * set intended visibility of columns covered by this group
959 public void setHideCols(boolean visibility)
961 hidecols = visibility;
966 * @return true if columns covered by group should be hidden
968 public boolean isHideCols()
974 * create a new sequence group from the intersection of this group with an
975 * alignment Hashtable of hidden representatives
981 * @return new group containing sequences common to this group and alignment
983 public SequenceGroup intersect(AlignmentI alignment,
984 Map<SequenceI, SequenceCollectionI> map)
986 SequenceGroup sgroup = new SequenceGroup(this);
987 SequenceI[] insect = getSequencesInOrder(alignment);
988 sgroup.sequences = new Vector();
989 for (int s = 0; insect != null && s < insect.length; s++)
991 if (map == null || map.containsKey(insect[s]))
993 sgroup.sequences.addElement(insect[s]);
996 // Enumeration en =getSequences(hashtable).elements();
997 // while (en.hasMoreElements())
999 // SequenceI elem = (SequenceI) en.nextElement();
1000 // if (alignment.getSequences().contains(elem))
1002 // sgroup.addSequence(elem, false);
1009 * @return the showUnconserved
1011 public boolean getShowNonconserved()
1013 return showNonconserved;
1017 * @param showNonconserved
1018 * the showUnconserved to set
1020 public void setShowNonconserved(boolean displayNonconserved)
1022 this.showNonconserved = displayNonconserved;
1025 AlignmentAnnotation consensus = null, conservation = null;
1028 * flag indicating if consensus histogram should be rendered
1030 private boolean showConsensusHistogram;
1033 * set this alignmentAnnotation object as the one used to render consensus
1038 public void setConsensus(AlignmentAnnotation aan)
1040 if (consensus == null)
1048 * @return automatically calculated consensus row
1050 public AlignmentAnnotation getConsensus()
1052 // TODO get or calculate and get consensus annotation row for this group
1053 int aWidth = this.getWidth();
1061 if (consensus == null)
1063 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1064 100f, AlignmentAnnotation.BAR_GRAPH);
1065 consensus.hasText = true;
1066 consensus.autoCalculated = true;
1067 consensus.groupRef = this;
1068 consensus.label = "Consensus for " + getName();
1069 consensus.description = "Percent Identity";
1075 * set this alignmentAnnotation object as the one used to render consensus
1080 public void setConservationRow(AlignmentAnnotation aan)
1082 if (conservation == null)
1089 * get the conservation annotation row for this group
1091 * @return autoCalculated annotation row
1093 public AlignmentAnnotation getConservationRow()
1095 if (conservation == null)
1097 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1098 11f, AlignmentAnnotation.BAR_GRAPH);
1101 conservation.hasText = true;
1102 conservation.autoCalculated = true;
1103 conservation.groupRef = this;
1104 conservation.label = "Conservation for " + getName();
1105 conservation.description = "Conservation for group " + getName()
1106 + " less than " + consPercGaps + "% gaps";
1107 return conservation;
1112 * @return true if annotation rows have been instantiated for this group
1114 public boolean hasAnnotationRows()
1116 return consensus != null || conservation != null;
1119 public SequenceI getConsensusSeq()
1122 StringBuffer seqs = new StringBuffer();
1123 for (int i = 0; i < consensus.annotations.length; i++)
1125 if (consensus.annotations[i] != null)
1127 if (consensus.annotations[i].description.charAt(0) == '[')
1129 seqs.append(consensus.annotations[i].description.charAt(1));
1133 seqs.append(consensus.annotations[i].displayCharacter);
1138 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1140 sq.setDescription("Percentage Identity Consensus "
1141 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1145 public void setIgnoreGapsConsensus(boolean state)
1147 if (this.ignoreGapsInConsensus != state && consensus != null)
1149 ignoreGapsInConsensus = state;
1150 recalcConservation();
1152 ignoreGapsInConsensus = state;
1155 public boolean getIgnoreGapsConsensus()
1157 return ignoreGapsInConsensus;
1161 * @param showSequenceLogo
1162 * indicates if a sequence logo is shown for consensus annotation
1164 public void setshowSequenceLogo(boolean showSequenceLogo)
1166 // TODO: decouple calculation from settings update
1167 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1169 this.showSequenceLogo = showSequenceLogo;
1170 recalcConservation();
1172 this.showSequenceLogo = showSequenceLogo;
1177 * @param showConsHist
1178 * flag indicating if the consensus histogram for this group should
1181 public void setShowConsensusHistogram(boolean showConsHist)
1184 if (showConsensusHistogram != showConsHist && consensus != null)
1186 this.showConsensusHistogram = showConsHist;
1187 recalcConservation();
1189 this.showConsensusHistogram = showConsHist;
1193 * @return the showConsensusHistogram
1195 public boolean isShowConsensusHistogram()
1197 return showConsensusHistogram;
1201 * set flag indicating if logo should be normalised when rendered
1205 public void setNormaliseSequenceLogo(boolean norm)
1207 normaliseSequenceLogo = norm;
1210 public boolean isNormaliseSequenceLogo()
1212 return normaliseSequenceLogo;
1217 * returns a new array with all annotation involving this group
1219 public AlignmentAnnotation[] getAlignmentAnnotation()
1221 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1223 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1224 for (SequenceI seq : sequences)
1226 AlignmentAnnotation[] aa = seq.getAnnotation();
1229 for (AlignmentAnnotation al : aa)
1231 if (al.groupRef == this)
1238 if (consensus != null)
1240 annot.add(consensus);
1242 if (conservation != null)
1244 annot.add(conservation);
1246 return annot.toArray(new AlignmentAnnotation[0]);
1250 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1252 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1253 for (AlignmentAnnotation a : getAlignmentAnnotation())
1255 if (a.getCalcId() == calcId)
1264 * Answer true if any annotation matches the calcId passed in (if not null).
1269 public boolean hasAnnotation(String calcId)
1271 if (calcId != null && !"".equals(calcId))
1273 for (AlignmentAnnotation a : getAlignmentAnnotation())
1275 if (a.getCalcId() == calcId)
1289 private AnnotatedCollectionI context;
1292 * set the alignment or group context for this group
1296 public void setContext(AnnotatedCollectionI context)
1298 this.context = context;
1304 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1307 public AnnotatedCollectionI getContext()