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.renderer.ResidueShader;
26 import jalview.renderer.ResidueShaderI;
27 import jalview.schemes.ColourSchemeI;
29 import java.awt.Color;
30 import java.util.ArrayList;
31 import java.util.List;
35 * Collects a set contiguous ranges on a set of sequences
40 public class SequenceGroup implements AnnotatedCollectionI
46 Conservation conserve;
48 boolean displayBoxes = true;
50 boolean displayText = true;
52 boolean colourText = false;
54 HiddenMarkovModel hmm;
57 * True if the group is defined as a group on the alignment, false if it is
60 boolean isDefined = false;
63 * after Olivier's non-conserved only character display
65 boolean showNonconserved = false;
70 private List<SequenceI> sequences = new ArrayList<>();
73 * representative sequence for this group (if any)
75 private SequenceI seqrep = null;
80 * Colourscheme applied to group if any
82 public ResidueShaderI cs;
84 // start column (base 0)
87 // end column (base 0)
90 public Color outlineColour = Color.black;
92 public Color idColour = null;
94 public int thresholdTextColour = 0;
96 public Color textColour = Color.black;
98 public Color textColour2 = Color.white;
101 * consensus calculation property
103 private boolean ignoreGapsInConsensus = true;
106 * consensus calculation property
108 private boolean showSequenceLogo = false;
111 * flag indicating if logo should be rendered normalised
113 private boolean normaliseSequenceLogo;
116 * visibility of rows or represented rows covered by group
118 private boolean hidereps = false;
121 * visibility of columns intersecting this group
123 private boolean hidecols = false;
125 AlignmentAnnotation consensus = null;
127 AlignmentAnnotation conservation = null;
129 private boolean showConsensusHistogram;
131 private AnnotatedCollectionI context;
134 * Creates a new SequenceGroup object.
136 public SequenceGroup()
138 groupName = "JGroup:" + this.hashCode();
139 cs = new ResidueShader();
143 * Creates a new SequenceGroup object.
148 * @param displayBoxes
152 * first column of group
154 * last column of group
156 public SequenceGroup(List<SequenceI> sequences, String groupName,
157 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
158 boolean colourText, int start, int end)
161 this.sequences = sequences;
162 this.groupName = groupName;
163 this.displayBoxes = displayBoxes;
164 this.displayText = displayText;
165 this.colourText = colourText;
166 this.cs = new ResidueShader(scheme);
169 recalcConservation();
177 public SequenceGroup(SequenceGroup seqsel)
182 sequences = new ArrayList<>();
183 sequences.addAll(seqsel.sequences);
184 if (seqsel.groupName != null)
186 groupName = new String(seqsel.groupName);
188 displayBoxes = seqsel.displayBoxes;
189 displayText = seqsel.displayText;
190 colourText = seqsel.colourText;
191 startRes = seqsel.startRes;
192 endRes = seqsel.endRes;
193 cs = new ResidueShader(seqsel.getColourScheme());
194 if (seqsel.description != null)
196 description = new String(seqsel.description);
198 hidecols = seqsel.hidecols;
199 hidereps = seqsel.hidereps;
200 showNonconserved = seqsel.showNonconserved;
201 showSequenceLogo = seqsel.showSequenceLogo;
202 normaliseSequenceLogo = seqsel.normaliseSequenceLogo;
203 showConsensusHistogram = seqsel.showConsensusHistogram;
204 idColour = seqsel.idColour;
205 outlineColour = seqsel.outlineColour;
206 seqrep = seqsel.seqrep;
207 textColour = seqsel.textColour;
208 textColour2 = seqsel.textColour2;
209 thresholdTextColour = seqsel.thresholdTextColour;
210 width = seqsel.width;
211 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
212 if (seqsel.conserve != null)
214 recalcConservation(); // safer than
215 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
220 public boolean isShowSequenceLogo()
222 return showSequenceLogo;
225 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
227 int iSize = sequences.size();
228 SequenceI[] seqs = new SequenceI[iSize];
229 SequenceI[] inorder = getSequencesInOrder(align);
231 for (int i = 0, ipos = 0; i < inorder.length; i++)
233 SequenceI seq = inorder[i];
235 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
236 if (seqs[ipos] != null)
238 seqs[ipos].setDescription(seq.getDescription());
239 seqs[ipos].setDBRefs(seq.getDBRefs());
240 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
241 if (seq.getDatasetSequence() != null)
243 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
246 if (seq.getAnnotation() != null)
248 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
249 // Only copy annotation that is either a score or referenced by the
250 // alignment's annotation vector
251 for (int a = 0; a < seq.getAnnotation().length; a++)
253 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
256 boolean found = false;
257 for (int pos = 0; pos < alann.length; pos++)
259 if (alann[pos] == tocopy)
270 AlignmentAnnotation newannot = new AlignmentAnnotation(
271 seq.getAnnotation()[a]);
272 newannot.restrict(startRes, endRes);
273 newannot.setSequenceRef(seqs[ipos]);
274 newannot.adjustForAlignment();
275 seqs[ipos].addAlignmentAnnotation(newannot);
285 if (iSize != inorder.length)
287 SequenceI[] nseqs = new SequenceI[iSize];
288 System.arraycopy(seqs, 0, nseqs, 0, iSize);
296 * If sequence ends in gaps, the end residue can be correctly calculated here
302 public int findEndRes(SequenceI seq)
307 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
309 ch = seq.getCharAt(j);
310 if (!jalview.util.Comparison.isGap((ch)))
318 eres += seq.getStart() - 1;
325 public List<SequenceI> getSequences()
331 public List<SequenceI> getSequences(
332 Map<SequenceI, SequenceCollectionI> hiddenReps)
334 if (hiddenReps == null)
336 // TODO: need a synchronizedCollection here ?
341 List<SequenceI> allSequences = new ArrayList<>();
342 for (SequenceI seq : sequences)
344 allSequences.add(seq);
345 if (hiddenReps.containsKey(seq))
347 SequenceCollectionI hsg = hiddenReps.get(seq);
348 for (SequenceI seq2 : hsg.getSequences())
350 if (seq2 != seq && !allSequences.contains(seq2))
352 allSequences.add(seq2);
362 public SequenceI[] getSequencesAsArray(
363 Map<SequenceI, SequenceCollectionI> map)
365 List<SequenceI> tmp = getSequences(map);
370 return tmp.toArray(new SequenceI[tmp.size()]);
379 * @return DOCUMENT ME!
381 public boolean adjustForRemoveLeft(int col)
383 // return value is true if the group still exists
386 startRes = startRes - col;
391 endRes = endRes - col;
393 if (startRes > endRes)
400 // must delete this group!!
413 * @return DOCUMENT ME!
415 public boolean adjustForRemoveRight(int col)
434 * @return DOCUMENT ME!
436 public String getName()
441 public String getDescription()
452 public void setName(String name)
455 // TODO: URGENT: update dependent objects (annotation row)
458 public void setDescription(String desc)
466 * @return DOCUMENT ME!
468 public Conservation getConservation()
479 public void setConservation(Conservation c)
485 * Add s to this sequence group. If aligment sequence is already contained in
486 * group, it will not be added again, but recalculation may happen if the flag
490 * alignment sequence to be added
492 * true means Group's conservation should be recalculated
494 public void addSequence(SequenceI s, boolean recalc)
496 synchronized (sequences)
498 if (s != null && !sequences.contains(s))
505 recalcConservation();
511 * Max Gaps Threshold (percent) for performing a conservation calculation
513 private int consPercGaps = 25;
516 * @return Max Gaps Threshold for performing a conservation calculation
518 public int getConsPercGaps()
524 * set Max Gaps Threshold (percent) for performing a conservation calculation
526 * @param consPercGaps
528 public void setConsPercGaps(int consPercGaps)
530 this.consPercGaps = consPercGaps;
534 * calculate residue conservation and colourschemes for group - but only if
535 * necessary. returns true if the calculation resulted in a visible change to
538 public boolean recalcConservation()
540 return recalcConservation(false);
544 * calculate residue conservation for group - but only if necessary. returns
545 * true if the calculation resulted in a visible change to group
548 * when set, colourschemes for this group are not refreshed after
551 public boolean recalcConservation(boolean defer)
553 if (cs == null && consensus == null && conservation == null)
557 // TODO: try harder to detect changes in state in order to minimise
558 // recalculation effort
562 ProfilesI cnsns = AAFrequency.calculate(sequences, startRes,
563 endRes + 1, showSequenceLogo);
564 if (consensus != null)
566 _updateConsensusRow(cnsns, sequences.size());
571 cs.setConsensus(cnsns);
575 if ((conservation != null)
576 || (cs != null && cs.conservationApplied()))
578 Conservation c = new Conservation(groupName, sequences, startRes,
581 c.verdict(false, consPercGaps);
582 if (conservation != null)
584 _updateConservationRow(c);
588 if (cs.conservationApplied())
590 cs.setConservation(c);
593 // eager update - will cause a refresh of overview regardless
596 if (cs != null && !defer)
598 // TODO: JAL-2034 should cs.alignmentChanged modify return state
599 cs.alignmentChanged(context != null ? context : this, null);
606 } catch (java.lang.OutOfMemoryError err)
609 System.out.println("Out of memory loading groups: " + err);
614 private void _updateConservationRow(Conservation c)
616 if (conservation == null)
621 conservation.label = "Conservation for " + getName();
622 conservation.description = "Conservation for group " + getName()
623 + " less than " + consPercGaps + "% gaps";
624 // preserve width if already set
625 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
628 conservation.annotations = null;
629 conservation.annotations = new Annotation[aWidth]; // should be alignment
631 c.completeAnnotations(conservation, null, startRes, endRes + 1);
634 public ProfilesI consensusData = null;
636 private void _updateConsensusRow(ProfilesI cnsns, long nseq)
638 if (consensus == null)
642 consensus.label = "Consensus for " + getName();
643 consensus.description = "Percent Identity";
644 consensusData = cnsns;
645 // preserve width if already set
646 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
649 consensus.annotations = null;
650 consensus.annotations = new Annotation[aWidth]; // should be alignment width
652 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
653 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
656 // ignoreGapsInConsensusCalculation);
661 * sequence to either add or remove from group
663 * flag passed to delete/addSequence to indicate if group properties
664 * should be recalculated
666 public void addOrRemove(SequenceI s, boolean recalc)
668 synchronized (sequences)
670 if (sequences.contains(s))
672 deleteSequence(s, recalc);
676 addSequence(s, recalc);
687 * true means recalculate conservation
689 public void deleteSequence(SequenceI s, boolean recalc)
691 synchronized (sequences)
697 recalcConservation();
705 * @return the first column selected by this group. Runs from 0<=i<N_cols
708 public int getStartRes()
715 * @return the groups last selected column. Runs from 0<=i<N_cols
718 public int getEndRes()
724 * Set the first column selected by this group. Runs from 0<=i<N_cols
728 public void setStartRes(int i)
734 * Set the groups last selected column. Runs from 0<=i<N_cols
738 public void setEndRes(int i)
744 * @return number of sequences in group
748 return sequences.size();
753 * @return the ith sequence
755 public SequenceI getSequenceAt(int i)
757 return sequences.get(i);
764 public void setColourText(boolean state)
772 * @return DOCUMENT ME!
774 public boolean getColourText()
785 public void setDisplayText(boolean state)
793 * @return DOCUMENT ME!
795 public boolean getDisplayText()
806 public void setDisplayBoxes(boolean state)
808 displayBoxes = state;
814 * @return DOCUMENT ME!
816 public boolean getDisplayBoxes()
822 * computes the width of current set of sequences and returns it
824 * @return DOCUMENT ME!
827 public int getWidth()
829 synchronized (sequences)
831 // MC This needs to get reset when characters are inserted and deleted
832 boolean first = true;
833 for (SequenceI seq : sequences)
835 if (first || seq.getLength() > width)
837 width = seq.getLength();
851 public void setOutlineColour(Color c)
859 * @return DOCUMENT ME!
861 public Color getOutlineColour()
863 return outlineColour;
868 * returns the sequences in the group ordered by the ordering given by al.
869 * this used to return an array with null entries regardless, new behaviour is
870 * below. TODO: verify that this does not affect use in applet or application
874 * @return SequenceI[] intersection of sequences in group with al, ordered by
875 * al, or null if group does not intersect with al
877 public SequenceI[] getSequencesInOrder(AlignmentI al)
879 return getSequencesInOrder(al, true);
883 * return an array representing the intersection of the group with al,
884 * optionally returning an array the size of al.getHeight() where nulls mark
885 * the non-intersected sequences
889 * @return null or array
891 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
893 synchronized (sequences)
895 int sSize = sequences.size();
896 int alHeight = al.getHeight();
898 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
901 for (int i = 0; i < alHeight && index < sSize; i++)
903 if (sequences.contains(al.getSequenceAt(i)))
905 seqs[(trim) ? index : i] = al.getSequenceAt(i);
917 if (index < seqs.length)
919 SequenceI[] dummy = seqs;
920 seqs = new SequenceI[index];
923 seqs[index] = dummy[index];
932 * @return the idColour
934 public Color getIdColour()
941 * the idColour to set
943 public void setIdColour(Color idColour)
945 this.idColour = idColour;
949 * @return the representative sequence for this group
952 public SequenceI getSeqrep()
958 * set the representative sequence for this group. Note - this affects the
959 * interpretation of the Hidereps attribute.
962 * the seqrep to set (null means no sequence representative)
965 public void setSeqrep(SequenceI seqrep)
967 this.seqrep = seqrep;
972 * @return true if group has a sequence representative
975 public boolean hasSeqrep()
977 return seqrep != null;
981 * set visibility of sequences covered by (if no sequence representative is
982 * defined) or represented by this group.
986 public void setHidereps(boolean visibility)
988 hidereps = visibility;
993 * @return true if sequences represented (or covered) by this group should be
996 public boolean isHidereps()
1002 * set intended visibility of columns covered by this group
1006 public void setHideCols(boolean visibility)
1008 hidecols = visibility;
1013 * @return true if columns covered by group should be hidden
1015 public boolean isHideCols()
1021 * create a new sequence group from the intersection of this group with an
1022 * alignment Hashtable of hidden representatives
1028 * @return new group containing sequences common to this group and alignment
1030 public SequenceGroup intersect(AlignmentI alignment,
1031 Map<SequenceI, SequenceCollectionI> map)
1033 SequenceGroup sgroup = new SequenceGroup(this);
1034 SequenceI[] insect = getSequencesInOrder(alignment);
1035 sgroup.sequences = new ArrayList<>();
1036 for (int s = 0; insect != null && s < insect.length; s++)
1038 if (map == null || map.containsKey(insect[s]))
1040 sgroup.sequences.add(insect[s]);
1047 * @return the showUnconserved
1049 public boolean getShowNonconserved()
1051 return showNonconserved;
1055 * @param showNonconserved
1056 * the showUnconserved to set
1058 public void setShowNonconserved(boolean displayNonconserved)
1060 this.showNonconserved = displayNonconserved;
1064 * set this alignmentAnnotation object as the one used to render consensus
1069 public void setConsensus(AlignmentAnnotation aan)
1071 if (consensus == null)
1079 * @return automatically calculated consensus row note: the row is a stub if a
1080 * consensus calculation has not yet been performed on the group
1082 public AlignmentAnnotation getConsensus()
1084 // TODO get or calculate and get consensus annotation row for this group
1085 int aWidth = this.getWidth();
1093 if (consensus == null)
1095 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1096 100f, AlignmentAnnotation.BAR_GRAPH);
1097 consensus.hasText = true;
1098 consensus.autoCalculated = true;
1099 consensus.groupRef = this;
1100 consensus.label = "Consensus for " + getName();
1101 consensus.description = "Percent Identity";
1107 * set this alignmentAnnotation object as the one used to render consensus
1112 public void setConservationRow(AlignmentAnnotation aan)
1114 if (conservation == null)
1121 * get the conservation annotation row for this group
1123 * @return autoCalculated annotation row
1125 public AlignmentAnnotation getConservationRow()
1127 if (conservation == null)
1129 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1130 11f, AlignmentAnnotation.BAR_GRAPH);
1133 conservation.hasText = true;
1134 conservation.autoCalculated = true;
1135 conservation.groupRef = this;
1136 conservation.label = "Conservation for " + getName();
1137 conservation.description = "Conservation for group " + getName()
1138 + " less than " + consPercGaps + "% gaps";
1139 return conservation;
1144 * @return true if annotation rows have been instantiated for this group
1146 public boolean hasAnnotationRows()
1148 return consensus != null || conservation != null;
1151 public SequenceI getConsensusSeq()
1154 StringBuffer seqs = new StringBuffer();
1155 for (int i = 0; i < consensus.annotations.length; i++)
1157 if (consensus.annotations[i] != null)
1159 if (consensus.annotations[i].description.charAt(0) == '[')
1161 seqs.append(consensus.annotations[i].description.charAt(1));
1165 seqs.append(consensus.annotations[i].displayCharacter);
1170 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1172 sq.setDescription("Percentage Identity Consensus "
1173 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1177 public void setIgnoreGapsConsensus(boolean state)
1179 if (this.ignoreGapsInConsensus != state && consensus != null)
1181 ignoreGapsInConsensus = state;
1182 recalcConservation();
1184 ignoreGapsInConsensus = state;
1187 public boolean getIgnoreGapsConsensus()
1189 return ignoreGapsInConsensus;
1193 * @param showSequenceLogo
1194 * indicates if a sequence logo is shown for consensus annotation
1196 public void setshowSequenceLogo(boolean showSequenceLogo)
1198 // TODO: decouple calculation from settings update
1199 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1201 this.showSequenceLogo = showSequenceLogo;
1202 recalcConservation();
1204 this.showSequenceLogo = showSequenceLogo;
1209 * @param showConsHist
1210 * flag indicating if the consensus histogram for this group should
1213 public void setShowConsensusHistogram(boolean showConsHist)
1216 if (showConsensusHistogram != showConsHist && consensus != null)
1218 this.showConsensusHistogram = showConsHist;
1219 recalcConservation();
1221 this.showConsensusHistogram = showConsHist;
1225 * @return the showConsensusHistogram
1227 public boolean isShowConsensusHistogram()
1229 return showConsensusHistogram;
1233 * set flag indicating if logo should be normalised when rendered
1237 public void setNormaliseSequenceLogo(boolean norm)
1239 normaliseSequenceLogo = norm;
1242 public boolean isNormaliseSequenceLogo()
1244 return normaliseSequenceLogo;
1249 * returns a new array with all annotation involving this group
1251 public AlignmentAnnotation[] getAlignmentAnnotation()
1253 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1255 ArrayList<AlignmentAnnotation> annot = new ArrayList<>();
1256 synchronized (sequences)
1258 for (SequenceI seq : sequences)
1260 AlignmentAnnotation[] aa = seq.getAnnotation();
1263 for (AlignmentAnnotation al : aa)
1265 if (al.groupRef == this)
1272 if (consensus != null)
1274 annot.add(consensus);
1276 if (conservation != null)
1278 annot.add(conservation);
1281 return annot.toArray(new AlignmentAnnotation[0]);
1285 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1287 List<AlignmentAnnotation> aa = new ArrayList<>();
1292 for (AlignmentAnnotation a : getAlignmentAnnotation())
1294 if (calcId.equals(a.getCalcId()))
1303 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1304 String calcId, String label)
1306 ArrayList<AlignmentAnnotation> aa = new ArrayList<>();
1307 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1309 if ((calcId == null || (ann.getCalcId() != null && ann.getCalcId()
1311 && (seq == null || (ann.sequenceRef != null && ann.sequenceRef == seq))
1312 && (label == null || (ann.label != null && ann.label
1322 * Answer true if any annotation matches the calcId passed in (if not null).
1327 public boolean hasAnnotation(String calcId)
1329 if (calcId != null && !"".equals(calcId))
1331 for (AlignmentAnnotation a : getAlignmentAnnotation())
1333 if (a.getCalcId() == calcId)
1343 * Remove all sequences from the group (leaving other properties unchanged).
1347 synchronized (sequences)
1354 * Sets the alignment or group context for this group, and whether it is
1355 * defined as a group
1358 * the context for the group
1360 * whether the group is defined on the alignment or is just a
1362 * @throws IllegalArgumentException
1363 * if setting the context would result in a circular reference chain
1365 public void setContext(AnnotatedCollectionI ctx, boolean defined)
1368 this.isDefined = defined;
1372 * Sets the alignment or group context for this group
1375 * the context for the group
1376 * @throws IllegalArgumentException
1377 * if setting the context would result in a circular reference chain
1379 public void setContext(AnnotatedCollectionI ctx)
1381 AnnotatedCollectionI ref = ctx;
1384 if (ref == this || ref.getContext() == ctx)
1386 throw new IllegalArgumentException(
1387 "Circular reference in SequenceGroup.context");
1389 ref = ref.getContext();
1397 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1400 public AnnotatedCollectionI getContext()
1405 public boolean isDefined()
1410 public void setColourScheme(ColourSchemeI scheme)
1414 cs = new ResidueShader();
1416 cs.setColourScheme(scheme);
1419 public void setGroupColourScheme(ResidueShaderI scheme)
1424 public ColourSchemeI getColourScheme()
1426 return cs == null ? null : cs.getColourScheme();
1429 public ResidueShaderI getGroupColourScheme()
1435 public boolean isNucleotide()
1437 if (context != null) {
1438 return context.isNucleotide();
1445 * @return true if seq is a member of the group
1448 public boolean contains(SequenceI seq1)
1450 return sequences.contains(seq1);
1456 * @return true if startRes<=apos and endRes>=apos and seq is in the group
1458 public boolean contains(SequenceI seq, int apos)
1460 return (startRes <= apos && endRes >= apos) && sequences.contains(seq);