2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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;
27 import java.awt.Color;
28 import java.util.ArrayList;
29 import java.util.List;
33 * Collects a set contiguous ranges on a set of sequences
38 public class SequenceGroup implements AnnotatedCollectionI
44 Conservation conserve;
46 boolean displayBoxes = true;
48 boolean displayText = true;
50 boolean colourText = false;
53 * after Olivier's non-conserved only character display
55 boolean showNonconserved = false;
60 private List<SequenceI> sequences = new ArrayList<SequenceI>();
63 * representative sequence for this group (if any)
65 private SequenceI seqrep = null;
70 * Colourscheme applied to group if any
72 public ColourSchemeI cs;
74 // start column (base 0)
77 // end column (base 0)
80 public Color outlineColour = Color.black;
82 public Color idColour = null;
84 public int thresholdTextColour = 0;
86 public Color textColour = Color.black;
88 public Color textColour2 = Color.white;
91 * consensus calculation property
93 private boolean ignoreGapsInConsensus = true;
96 * consensus calculation property
98 private boolean showSequenceLogo = false;
101 * flag indicating if logo should be rendered normalised
103 private boolean normaliseSequenceLogo;
106 * @return the includeAllConsSymbols
108 public boolean isShowSequenceLogo()
110 return showSequenceLogo;
114 * Creates a new SequenceGroup object.
116 public SequenceGroup()
118 groupName = "JGroup:" + this.hashCode();
122 * Creates a new SequenceGroup object.
127 * @param displayBoxes
131 * first column of group
133 * last column of group
135 public SequenceGroup(List<SequenceI> sequences, String groupName,
136 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
137 boolean colourText, int start, int end)
139 this.sequences = sequences;
140 this.groupName = groupName;
141 this.displayBoxes = displayBoxes;
142 this.displayText = displayText;
143 this.colourText = colourText;
147 recalcConservation();
155 public SequenceGroup(SequenceGroup seqsel)
159 sequences = new ArrayList<SequenceI>();
160 sequences.addAll(seqsel.sequences);
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)
173 description = new String(seqsel.description);
175 hidecols = seqsel.hidecols;
176 hidereps = seqsel.hidereps;
177 idColour = seqsel.idColour;
178 outlineColour = seqsel.outlineColour;
179 seqrep = seqsel.seqrep;
180 textColour = seqsel.textColour;
181 textColour2 = seqsel.textColour2;
182 thresholdTextColour = seqsel.thresholdTextColour;
183 width = seqsel.width;
184 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
185 if (seqsel.conserve != null)
187 recalcConservation(); // safer than
188 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
193 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
195 int iSize = sequences.size();
196 SequenceI[] seqs = new SequenceI[iSize];
197 SequenceI[] inorder = getSequencesInOrder(align);
199 for (int i = 0, ipos = 0; i < inorder.length; i++)
201 SequenceI seq = inorder[i];
203 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
204 if (seqs[ipos] != null)
206 seqs[ipos].setDescription(seq.getDescription());
207 seqs[ipos].setDBRefs(seq.getDBRefs());
208 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
209 if (seq.getDatasetSequence() != null)
211 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
214 if (seq.getAnnotation() != null)
216 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
217 // Only copy annotation that is either a score or referenced by the
218 // alignment's annotation vector
219 for (int a = 0; a < seq.getAnnotation().length; a++)
221 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
224 boolean found = false;
225 for (int pos = 0; pos < alann.length; pos++)
227 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;
293 public List<SequenceI> getSequences()
299 public List<SequenceI> getSequences(
300 Map<SequenceI, SequenceCollectionI> hiddenReps)
302 if (hiddenReps == null)
304 // TODO: need a synchronizedCollection here ?
309 List<SequenceI> allSequences = new ArrayList<SequenceI>();
310 for (SequenceI seq : sequences)
312 allSequences.add(seq);
313 if (hiddenReps.containsKey(seq))
315 SequenceCollectionI hsg = hiddenReps.get(seq);
316 for (SequenceI seq2 : hsg.getSequences())
318 if (seq2 != seq && !allSequences.contains(seq2))
320 allSequences.add(seq2);
330 public SequenceI[] getSequencesAsArray(
331 Map<SequenceI, SequenceCollectionI> map)
333 List<SequenceI> tmp = getSequences(map);
338 return tmp.toArray(new SequenceI[tmp.size()]);
347 * @return DOCUMENT ME!
349 public boolean adjustForRemoveLeft(int col)
351 // return value is true if the group still exists
354 startRes = startRes - col;
359 endRes = endRes - col;
361 if (startRes > endRes)
368 // must delete this group!!
381 * @return DOCUMENT ME!
383 public boolean adjustForRemoveRight(int col)
402 * @return DOCUMENT ME!
404 public String getName()
409 public String getDescription()
420 public void setName(String name)
423 // TODO: URGENT: update dependent objects (annotation row)
426 public void setDescription(String desc)
434 * @return DOCUMENT ME!
436 public Conservation getConservation()
447 public void setConservation(Conservation c)
453 * Add s to this sequence group. If aligment sequence is already contained in
454 * group, it will not be added again, but recalculation may happen if the flag
458 * alignment sequence to be added
460 * true means Group's conservation should be recalculated
462 public void addSequence(SequenceI s, boolean recalc)
464 synchronized (sequences)
466 if (s != null && !sequences.contains(s))
473 recalcConservation();
479 * Max Gaps Threshold (percent) for performing a conservation calculation
481 private int consPercGaps = 25;
484 * @return Max Gaps Threshold for performing a conservation calculation
486 public int getConsPercGaps()
492 * set Max Gaps Threshold (percent) for performing a conservation calculation
494 * @param consPercGaps
496 public void setConsPercGaps(int consPercGaps)
498 this.consPercGaps = consPercGaps;
502 * calculate residue conservation and colourschemes for group - but only if
503 * necessary. returns true if the calculation resulted in a visible change to
506 public boolean recalcConservation()
508 return recalcConservation(false);
512 * calculate residue conservation for group - but only if necessary. returns
513 * true if the calculation resulted in a visible change to group
516 * when set, colourschemes for this group are not refreshed after
519 public boolean recalcConservation(boolean defer)
521 if (cs == null && consensus == null && conservation == null)
525 // TODO: try harder to detect changes in state in order to minimise
526 // recalculation effort
530 ProfilesI cnsns = AAFrequency.calculate(sequences, startRes,
531 endRes + 1, showSequenceLogo);
532 if (consensus != null)
534 _updateConsensusRow(cnsns, sequences.size());
539 cs.setConsensus(cnsns);
543 if ((conservation != null)
544 || (cs != null && cs.conservationApplied()))
546 Conservation c = new Conservation(groupName, sequences, startRes,
549 c.verdict(false, consPercGaps);
550 if (conservation != null)
552 _updateConservationRow(c);
556 if (cs.conservationApplied())
558 cs.setConservation(c);
561 // eager update - will cause a refresh of overview regardless
564 if (cs != null && !defer)
566 // TODO: JAL-2034 should cs.alignmentChanged modify return state
567 cs.alignmentChanged(context != null ? context : this, null);
574 } catch (java.lang.OutOfMemoryError err)
577 System.out.println("Out of memory loading groups: " + err);
582 private void _updateConservationRow(Conservation c)
584 if (conservation == null)
589 conservation.label = "Conservation for " + getName();
590 conservation.description = "Conservation for group " + getName()
591 + " less than " + consPercGaps + "% gaps";
592 // preserve width if already set
593 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
596 conservation.annotations = null;
597 conservation.annotations = new Annotation[aWidth]; // should be alignment
599 c.completeAnnotations(conservation, null, startRes, endRes + 1);
602 public ProfilesI consensusData = null;
604 private void _updateConsensusRow(ProfilesI cnsns, long nseq)
606 if (consensus == null)
610 consensus.label = "Consensus for " + getName();
611 consensus.description = "Percent Identity";
612 consensusData = cnsns;
613 // preserve width if already set
614 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
617 consensus.annotations = null;
618 consensus.annotations = new Annotation[aWidth]; // should be alignment width
620 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
621 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
624 // ignoreGapsInConsensusCalculation);
629 * sequence to either add or remove from group
631 * flag passed to delete/addSequence to indicate if group properties
632 * should be recalculated
634 public void addOrRemove(SequenceI s, boolean recalc)
636 synchronized (sequences)
638 if (sequences.contains(s))
640 deleteSequence(s, recalc);
644 addSequence(s, recalc);
655 * true means recalculate conservation
657 public void deleteSequence(SequenceI s, boolean recalc)
659 synchronized (sequences)
665 recalcConservation();
673 * @return the first column selected by this group. Runs from 0<=i<N_cols
676 public int getStartRes()
683 * @return the groups last selected column. Runs from 0<=i<N_cols
686 public int getEndRes()
692 * Set the first column selected by this group. Runs from 0<=i<N_cols
696 public void setStartRes(int i)
702 * Set the groups last selected column. Runs from 0<=i<N_cols
706 public void setEndRes(int i)
712 * @return number of sequences in group
716 return sequences.size();
721 * @return the ith sequence
723 public SequenceI getSequenceAt(int i)
725 return sequences.get(i);
732 public void setColourText(boolean state)
740 * @return DOCUMENT ME!
742 public boolean getColourText()
753 public void setDisplayText(boolean state)
761 * @return DOCUMENT ME!
763 public boolean getDisplayText()
774 public void setDisplayBoxes(boolean state)
776 displayBoxes = state;
782 * @return DOCUMENT ME!
784 public boolean getDisplayBoxes()
790 * computes the width of current set of sequences and returns it
792 * @return DOCUMENT ME!
795 public int getWidth()
797 synchronized (sequences)
799 // MC This needs to get reset when characters are inserted and deleted
800 boolean first = true;
801 for (SequenceI seq : sequences)
803 if (first || seq.getLength() > width)
805 width = seq.getLength();
819 public void setOutlineColour(Color c)
827 * @return DOCUMENT ME!
829 public Color getOutlineColour()
831 return outlineColour;
836 * returns the sequences in the group ordered by the ordering given by al.
837 * this used to return an array with null entries regardless, new behaviour is
838 * below. TODO: verify that this does not affect use in applet or application
842 * @return SequenceI[] intersection of sequences in group with al, ordered by
843 * al, or null if group does not intersect with al
845 public SequenceI[] getSequencesInOrder(AlignmentI al)
847 return getSequencesInOrder(al, true);
851 * return an array representing the intersection of the group with al,
852 * optionally returning an array the size of al.getHeight() where nulls mark
853 * the non-intersected sequences
857 * @return null or array
859 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
861 synchronized (sequences)
863 int sSize = sequences.size();
864 int alHeight = al.getHeight();
866 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
869 for (int i = 0; i < alHeight && index < sSize; i++)
871 if (sequences.contains(al.getSequenceAt(i)))
873 seqs[(trim) ? index : i] = al.getSequenceAt(i);
885 if (index < seqs.length)
887 SequenceI[] dummy = seqs;
888 seqs = new SequenceI[index];
891 seqs[index] = dummy[index];
900 * @return the idColour
902 public Color getIdColour()
909 * the idColour to set
911 public void setIdColour(Color idColour)
913 this.idColour = idColour;
917 * @return the representative sequence for this group
920 public SequenceI getSeqrep()
926 * set the representative sequence for this group. Note - this affects the
927 * interpretation of the Hidereps attribute.
930 * the seqrep to set (null means no sequence representative)
933 public void setSeqrep(SequenceI seqrep)
935 this.seqrep = seqrep;
940 * @return true if group has a sequence representative
943 public boolean hasSeqrep()
945 return seqrep != null;
949 * visibility of rows or represented rows covered by group
951 private boolean hidereps = false;
954 * set visibility of sequences covered by (if no sequence representative is
955 * defined) or represented by this group.
959 public void setHidereps(boolean visibility)
961 hidereps = visibility;
966 * @return true if sequences represented (or covered) by this group should be
969 public boolean isHidereps()
975 * visibility of columns intersecting this group
977 private boolean hidecols = false;
980 * set intended visibility of columns covered by this group
984 public void setHideCols(boolean visibility)
986 hidecols = visibility;
991 * @return true if columns covered by group should be hidden
993 public boolean isHideCols()
999 * create a new sequence group from the intersection of this group with an
1000 * alignment Hashtable of hidden representatives
1006 * @return new group containing sequences common to this group and alignment
1008 public SequenceGroup intersect(AlignmentI alignment,
1009 Map<SequenceI, SequenceCollectionI> map)
1011 SequenceGroup sgroup = new SequenceGroup(this);
1012 SequenceI[] insect = getSequencesInOrder(alignment);
1013 sgroup.sequences = new ArrayList<SequenceI>();
1014 for (int s = 0; insect != null && s < insect.length; s++)
1016 if (map == null || map.containsKey(insect[s]))
1018 sgroup.sequences.add(insect[s]);
1025 * @return the showUnconserved
1027 public boolean getShowNonconserved()
1029 return showNonconserved;
1033 * @param showNonconserved
1034 * the showUnconserved to set
1036 public void setShowNonconserved(boolean displayNonconserved)
1038 this.showNonconserved = displayNonconserved;
1041 AlignmentAnnotation consensus = null, conservation = null;
1044 * flag indicating if consensus histogram should be rendered
1046 private boolean showConsensusHistogram;
1049 * set this alignmentAnnotation object as the one used to render consensus
1054 public void setConsensus(AlignmentAnnotation aan)
1056 if (consensus == null)
1064 * @return automatically calculated consensus row note: the row is a stub if a
1065 * consensus calculation has not yet been performed on the group
1067 public AlignmentAnnotation getConsensus()
1069 // TODO get or calculate and get consensus annotation row for this group
1070 int aWidth = this.getWidth();
1078 if (consensus == null)
1080 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1081 100f, AlignmentAnnotation.BAR_GRAPH);
1082 consensus.hasText = true;
1083 consensus.autoCalculated = true;
1084 consensus.groupRef = this;
1085 consensus.label = "Consensus for " + getName();
1086 consensus.description = "Percent Identity";
1092 * set this alignmentAnnotation object as the one used to render consensus
1097 public void setConservationRow(AlignmentAnnotation aan)
1099 if (conservation == null)
1106 * get the conservation annotation row for this group
1108 * @return autoCalculated annotation row
1110 public AlignmentAnnotation getConservationRow()
1112 if (conservation == null)
1114 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1115 11f, AlignmentAnnotation.BAR_GRAPH);
1118 conservation.hasText = true;
1119 conservation.autoCalculated = true;
1120 conservation.groupRef = this;
1121 conservation.label = "Conservation for " + getName();
1122 conservation.description = "Conservation for group " + getName()
1123 + " less than " + consPercGaps + "% gaps";
1124 return conservation;
1129 * @return true if annotation rows have been instantiated for this group
1131 public boolean hasAnnotationRows()
1133 return consensus != null || conservation != null;
1136 public SequenceI getConsensusSeq()
1139 StringBuffer seqs = new StringBuffer();
1140 for (int i = 0; i < consensus.annotations.length; i++)
1142 if (consensus.annotations[i] != null)
1144 if (consensus.annotations[i].description.charAt(0) == '[')
1146 seqs.append(consensus.annotations[i].description.charAt(1));
1150 seqs.append(consensus.annotations[i].displayCharacter);
1155 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1157 sq.setDescription("Percentage Identity Consensus "
1158 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1162 public void setIgnoreGapsConsensus(boolean state)
1164 if (this.ignoreGapsInConsensus != state && consensus != null)
1166 ignoreGapsInConsensus = state;
1167 recalcConservation();
1169 ignoreGapsInConsensus = state;
1172 public boolean getIgnoreGapsConsensus()
1174 return ignoreGapsInConsensus;
1178 * @param showSequenceLogo
1179 * indicates if a sequence logo is shown for consensus annotation
1181 public void setshowSequenceLogo(boolean showSequenceLogo)
1183 // TODO: decouple calculation from settings update
1184 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1186 this.showSequenceLogo = showSequenceLogo;
1187 recalcConservation();
1189 this.showSequenceLogo = showSequenceLogo;
1194 * @param showConsHist
1195 * flag indicating if the consensus histogram for this group should
1198 public void setShowConsensusHistogram(boolean showConsHist)
1201 if (showConsensusHistogram != showConsHist && consensus != null)
1203 this.showConsensusHistogram = showConsHist;
1204 recalcConservation();
1206 this.showConsensusHistogram = showConsHist;
1210 * @return the showConsensusHistogram
1212 public boolean isShowConsensusHistogram()
1214 return showConsensusHistogram;
1218 * set flag indicating if logo should be normalised when rendered
1222 public void setNormaliseSequenceLogo(boolean norm)
1224 normaliseSequenceLogo = norm;
1227 public boolean isNormaliseSequenceLogo()
1229 return normaliseSequenceLogo;
1234 * returns a new array with all annotation involving this group
1236 public AlignmentAnnotation[] getAlignmentAnnotation()
1238 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1240 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1241 synchronized (sequences)
1243 for (SequenceI seq : sequences)
1245 AlignmentAnnotation[] aa = seq.getAnnotation();
1248 for (AlignmentAnnotation al : aa)
1250 if (al.groupRef == this)
1257 if (consensus != null)
1259 annot.add(consensus);
1261 if (conservation != null)
1263 annot.add(conservation);
1266 return annot.toArray(new AlignmentAnnotation[0]);
1270 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1272 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1273 for (AlignmentAnnotation a : getAlignmentAnnotation())
1275 if (a.getCalcId() == calcId)
1284 * Returns a list of annotations that match the specified sequenceRef, calcId
1285 * and label, ignoring null values.
1287 * @return list of AlignmentAnnotation objects
1290 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1291 String calcId, String label)
1293 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1294 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1296 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1297 && ann.sequenceRef != null && ann.sequenceRef == seq
1298 && ann.label != null && ann.label.equals(label))
1307 * Answer true if any annotation matches the calcId passed in (if not null).
1312 public boolean hasAnnotation(String calcId)
1314 if (calcId != null && !"".equals(calcId))
1316 for (AlignmentAnnotation a : getAlignmentAnnotation())
1318 if (a.getCalcId() == calcId)
1328 * Remove all sequences from the group (leaving other properties unchanged).
1332 synchronized (sequences)
1338 private AnnotatedCollectionI context;
1341 * set the alignment or group context for this group
1345 public void setContext(AnnotatedCollectionI context)
1347 this.context = context;
1353 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1356 public AnnotatedCollectionI getContext()