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;
55 * True if the group is defined as a group on the alignment, false if it is
58 boolean isDefined = false;
61 * after Olivier's non-conserved only character display
63 boolean showNonconserved = false;
68 private List<SequenceI> sequences = new ArrayList<>();
71 * representative sequence for this group (if any)
73 private SequenceI seqrep = null;
78 * Colourscheme applied to group if any
80 public ResidueShaderI cs;
82 // start column (base 0)
85 // end column (base 0)
88 public Color outlineColour = Color.black;
90 public Color idColour = null;
92 public int thresholdTextColour = 0;
94 public Color textColour = Color.black;
96 public Color textColour2 = Color.white;
99 * consensus calculation property
101 private boolean ignoreGapsInConsensus = true;
104 * consensus calculation property
106 private boolean showSequenceLogo = false;
109 * flag indicating if logo should be rendered normalised
111 private boolean normaliseSequenceLogo;
114 * visibility of rows or represented rows covered by group
116 private boolean hidereps = false;
119 * visibility of columns intersecting this group
121 private boolean hidecols = false;
123 AlignmentAnnotation consensus = null;
125 AlignmentAnnotation conservation = null;
127 private boolean showConsensusHistogram;
129 private AnnotatedCollectionI context;
132 * Creates a new SequenceGroup object.
134 public SequenceGroup()
136 groupName = "JGroup:" + this.hashCode();
137 cs = new ResidueShader();
141 * Creates a new SequenceGroup object.
146 * @param displayBoxes
150 * first column of group
152 * last column of group
154 public SequenceGroup(List<SequenceI> sequences, String groupName,
155 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
156 boolean colourText, int start, int end)
159 this.sequences = sequences;
160 this.groupName = groupName;
161 this.displayBoxes = displayBoxes;
162 this.displayText = displayText;
163 this.colourText = colourText;
164 this.cs = new ResidueShader(scheme);
167 recalcConservation();
175 public SequenceGroup(SequenceGroup seqsel)
180 sequences = new ArrayList<>();
181 sequences.addAll(seqsel.sequences);
182 if (seqsel.groupName != null)
184 groupName = new String(seqsel.groupName);
186 displayBoxes = seqsel.displayBoxes;
187 displayText = seqsel.displayText;
188 colourText = seqsel.colourText;
189 startRes = seqsel.startRes;
190 endRes = seqsel.endRes;
191 cs = new ResidueShader((ResidueShader) seqsel.cs);
192 if (seqsel.description != null)
194 description = new String(seqsel.description);
196 hidecols = seqsel.hidecols;
197 hidereps = seqsel.hidereps;
198 showNonconserved = seqsel.showNonconserved;
199 showSequenceLogo = seqsel.showSequenceLogo;
200 normaliseSequenceLogo = seqsel.normaliseSequenceLogo;
201 showConsensusHistogram = seqsel.showConsensusHistogram;
202 idColour = seqsel.idColour;
203 outlineColour = seqsel.outlineColour;
204 seqrep = seqsel.seqrep;
205 textColour = seqsel.textColour;
206 textColour2 = seqsel.textColour2;
207 thresholdTextColour = seqsel.thresholdTextColour;
208 width = seqsel.width;
209 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
210 if (seqsel.conserve != null)
212 recalcConservation(); // safer than
213 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
218 public boolean isShowSequenceLogo()
220 return showSequenceLogo;
223 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
225 int iSize = sequences.size();
226 SequenceI[] seqs = new SequenceI[iSize];
227 SequenceI[] inorder = getSequencesInOrder(align);
229 for (int i = 0, ipos = 0; i < inorder.length; i++)
231 SequenceI seq = inorder[i];
233 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
234 if (seqs[ipos] != null)
236 seqs[ipos].setDescription(seq.getDescription());
237 seqs[ipos].setDBRefs(seq.getDBRefs());
238 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
239 if (seq.getDatasetSequence() != null)
241 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
244 if (seq.getAnnotation() != null)
246 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
247 // Only copy annotation that is either a score or referenced by the
248 // alignment's annotation vector
249 for (int a = 0; a < seq.getAnnotation().length; a++)
251 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
254 boolean found = false;
255 for (int pos = 0; pos < alann.length; pos++)
257 if (alann[pos] == tocopy)
268 AlignmentAnnotation newannot = new AlignmentAnnotation(
269 seq.getAnnotation()[a]);
270 newannot.restrict(startRes, endRes);
271 newannot.setSequenceRef(seqs[ipos]);
272 newannot.adjustForAlignment();
273 seqs[ipos].addAlignmentAnnotation(newannot);
283 if (iSize != inorder.length)
285 SequenceI[] nseqs = new SequenceI[iSize];
286 System.arraycopy(seqs, 0, nseqs, 0, iSize);
294 * If sequence ends in gaps, the end residue can be correctly calculated here
300 public int findEndRes(SequenceI seq)
305 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
307 ch = seq.getCharAt(j);
308 if (!jalview.util.Comparison.isGap((ch)))
316 eres += seq.getStart() - 1;
323 public List<SequenceI> getSequences()
329 public List<SequenceI> getSequences(
330 Map<SequenceI, SequenceCollectionI> hiddenReps)
332 if (hiddenReps == null)
334 // TODO: need a synchronizedCollection here ?
339 List<SequenceI> allSequences = new ArrayList<>();
340 for (SequenceI seq : sequences)
342 allSequences.add(seq);
343 if (hiddenReps.containsKey(seq))
345 SequenceCollectionI hsg = hiddenReps.get(seq);
346 for (SequenceI seq2 : hsg.getSequences())
348 if (seq2 != seq && !allSequences.contains(seq2))
350 allSequences.add(seq2);
360 public SequenceI[] getSequencesAsArray(
361 Map<SequenceI, SequenceCollectionI> map)
363 List<SequenceI> tmp = getSequences(map);
368 return tmp.toArray(new SequenceI[tmp.size()]);
377 * @return DOCUMENT ME!
379 public boolean adjustForRemoveLeft(int col)
381 // return value is true if the group still exists
384 startRes = startRes - col;
389 endRes = endRes - col;
391 if (startRes > endRes)
398 // must delete this group!!
411 * @return DOCUMENT ME!
413 public boolean adjustForRemoveRight(int col)
432 * @return DOCUMENT ME!
434 public String getName()
439 public String getDescription()
450 public void setName(String name)
453 // TODO: URGENT: update dependent objects (annotation row)
456 public void setDescription(String desc)
464 * @return DOCUMENT ME!
466 public Conservation getConservation()
477 public void setConservation(Conservation c)
483 * Add s to this sequence group. If aligment sequence is already contained in
484 * group, it will not be added again, but recalculation may happen if the flag
488 * alignment sequence to be added
490 * true means Group's conservation should be recalculated
492 public void addSequence(SequenceI s, boolean recalc)
494 synchronized (sequences)
496 if (s != null && !sequences.contains(s))
503 recalcConservation();
509 * Max Gaps Threshold (percent) for performing a conservation calculation
511 private int consPercGaps = 25;
514 * @return Max Gaps Threshold for performing a conservation calculation
516 public int getConsPercGaps()
522 * set Max Gaps Threshold (percent) for performing a conservation calculation
524 * @param consPercGaps
526 public void setConsPercGaps(int consPercGaps)
528 this.consPercGaps = consPercGaps;
532 * calculate residue conservation and colourschemes for group - but only if
533 * necessary. returns true if the calculation resulted in a visible change to
536 public boolean recalcConservation()
538 return recalcConservation(false);
542 * calculate residue conservation for group - but only if necessary. returns
543 * true if the calculation resulted in a visible change to group
546 * when set, colourschemes for this group are not refreshed after
549 public boolean recalcConservation(boolean defer)
551 if (cs == null && consensus == null && conservation == null)
555 // TODO: try harder to detect changes in state in order to minimise
556 // recalculation effort
560 ProfilesI cnsns = AAFrequency.calculate(sequences, startRes,
561 endRes + 1, showSequenceLogo);
562 if (consensus != null)
564 _updateConsensusRow(cnsns, sequences.size());
569 cs.setConsensus(cnsns);
573 if ((conservation != null)
574 || (cs != null && cs.conservationApplied()))
576 Conservation c = new Conservation(groupName, sequences, startRes,
579 c.verdict(false, consPercGaps);
580 if (conservation != null)
582 _updateConservationRow(c);
586 if (cs.conservationApplied())
588 cs.setConservation(c);
591 // eager update - will cause a refresh of overview regardless
594 if (cs != null && !defer)
596 // TODO: JAL-2034 should cs.alignmentChanged modify return state
597 cs.alignmentChanged(context != null ? context : this, null);
604 } catch (java.lang.OutOfMemoryError err)
607 System.out.println("Out of memory loading groups: " + err);
612 private void _updateConservationRow(Conservation c)
614 if (conservation == null)
619 conservation.label = "Conservation for " + getName();
620 conservation.description = "Conservation for group " + getName()
621 + " less than " + consPercGaps + "% gaps";
622 // preserve width if already set
623 int aWidth = (conservation.annotations != null)
624 ? (endRes < conservation.annotations.length
625 ? 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)
647 ? (endRes < consensus.annotations.length
648 ? consensus.annotations.length
651 consensus.annotations = null;
652 consensus.annotations = new Annotation[aWidth]; // should be alignment width
654 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
655 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
658 // ignoreGapsInConsensusCalculation);
663 * sequence to either add or remove from group
665 * flag passed to delete/addSequence to indicate if group properties
666 * should be recalculated
668 public void addOrRemove(SequenceI s, boolean recalc)
670 synchronized (sequences)
672 if (sequences.contains(s))
674 deleteSequence(s, recalc);
678 addSequence(s, recalc);
689 * true means recalculate conservation
691 public void deleteSequence(SequenceI s, boolean recalc)
693 synchronized (sequences)
699 recalcConservation();
707 * @return the first column selected by this group. Runs from 0<=i<N_cols
710 public int getStartRes()
717 * @return the groups last selected column. Runs from 0<=i<N_cols
720 public int getEndRes()
726 * Set the first column selected by this group. Runs from 0<=i<N_cols
730 public void setStartRes(int i)
736 * Set the groups last selected column. Runs from 0<=i<N_cols
740 public void setEndRes(int i)
746 * @return number of sequences in group
750 return sequences.size();
755 * @return the ith sequence
757 public SequenceI getSequenceAt(int i)
759 return sequences.get(i);
766 public void setColourText(boolean state)
774 * @return DOCUMENT ME!
776 public boolean getColourText()
787 public void setDisplayText(boolean state)
795 * @return DOCUMENT ME!
797 public boolean getDisplayText()
808 public void setDisplayBoxes(boolean state)
810 displayBoxes = state;
816 * @return DOCUMENT ME!
818 public boolean getDisplayBoxes()
824 * computes the width of current set of sequences and returns it
826 * @return DOCUMENT ME!
829 public int getWidth()
831 synchronized (sequences)
833 // MC This needs to get reset when characters are inserted and deleted
834 boolean first = true;
835 for (SequenceI seq : sequences)
837 if (first || seq.getLength() > width)
839 width = seq.getLength();
853 public void setOutlineColour(Color c)
861 * @return DOCUMENT ME!
863 public Color getOutlineColour()
865 return outlineColour;
870 * returns the sequences in the group ordered by the ordering given by al.
871 * this used to return an array with null entries regardless, new behaviour is
872 * below. TODO: verify that this does not affect use in applet or application
876 * @return SequenceI[] intersection of sequences in group with al, ordered by
877 * al, or null if group does not intersect with al
879 public SequenceI[] getSequencesInOrder(AlignmentI al)
881 return getSequencesInOrder(al, true);
885 * return an array representing the intersection of the group with al,
886 * optionally returning an array the size of al.getHeight() where nulls mark
887 * the non-intersected sequences
891 * @return null or array
893 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
895 synchronized (sequences)
897 int sSize = sequences.size();
898 int alHeight = al.getHeight();
900 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
903 for (int i = 0; i < alHeight && index < sSize; i++)
905 if (sequences.contains(al.getSequenceAt(i)))
907 seqs[(trim) ? index : i] = al.getSequenceAt(i);
919 if (index < seqs.length)
921 SequenceI[] dummy = seqs;
922 seqs = new SequenceI[index];
925 seqs[index] = dummy[index];
934 * @return the idColour
936 public Color getIdColour()
943 * the idColour to set
945 public void setIdColour(Color idColour)
947 this.idColour = idColour;
951 * @return the representative sequence for this group
954 public SequenceI getSeqrep()
960 * set the representative sequence for this group. Note - this affects the
961 * interpretation of the Hidereps attribute.
964 * the seqrep to set (null means no sequence representative)
967 public void setSeqrep(SequenceI seqrep)
969 this.seqrep = seqrep;
974 * @return true if group has a sequence representative
977 public boolean hasSeqrep()
979 return seqrep != null;
983 * set visibility of sequences covered by (if no sequence representative is
984 * defined) or represented by this group.
988 public void setHidereps(boolean visibility)
990 hidereps = visibility;
995 * @return true if sequences represented (or covered) by this group should be
998 public boolean isHidereps()
1004 * set intended visibility of columns covered by this group
1008 public void setHideCols(boolean visibility)
1010 hidecols = visibility;
1015 * @return true if columns covered by group should be hidden
1017 public boolean isHideCols()
1023 * create a new sequence group from the intersection of this group with an
1024 * alignment Hashtable of hidden representatives
1030 * @return new group containing sequences common to this group and alignment
1032 public SequenceGroup intersect(AlignmentI alignment,
1033 Map<SequenceI, SequenceCollectionI> map)
1035 SequenceGroup sgroup = new SequenceGroup(this);
1036 SequenceI[] insect = getSequencesInOrder(alignment);
1037 sgroup.sequences = new ArrayList<>();
1038 for (int s = 0; insect != null && s < insect.length; s++)
1040 if (map == null || map.containsKey(insect[s]))
1042 sgroup.sequences.add(insect[s]);
1049 * @return the showUnconserved
1051 public boolean getShowNonconserved()
1053 return showNonconserved;
1057 * @param showNonconserved
1058 * the showUnconserved to set
1060 public void setShowNonconserved(boolean displayNonconserved)
1062 this.showNonconserved = displayNonconserved;
1066 * set this alignmentAnnotation object as the one used to render consensus
1071 public void setConsensus(AlignmentAnnotation aan)
1073 if (consensus == null)
1081 * @return automatically calculated consensus row note: the row is a stub if a
1082 * consensus calculation has not yet been performed on the group
1084 public AlignmentAnnotation getConsensus()
1086 // TODO get or calculate and get consensus annotation row for this group
1087 int aWidth = this.getWidth();
1095 if (consensus == null)
1097 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1098 100f, AlignmentAnnotation.BAR_GRAPH);
1099 consensus.hasText = true;
1100 consensus.autoCalculated = true;
1101 consensus.groupRef = this;
1102 consensus.label = "Consensus for " + getName();
1103 consensus.description = "Percent Identity";
1109 * set this alignmentAnnotation object as the one used to render consensus
1114 public void setConservationRow(AlignmentAnnotation aan)
1116 if (conservation == null)
1123 * get the conservation annotation row for this group
1125 * @return autoCalculated annotation row
1127 public AlignmentAnnotation getConservationRow()
1129 if (conservation == null)
1131 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1132 11f, AlignmentAnnotation.BAR_GRAPH);
1135 conservation.hasText = true;
1136 conservation.autoCalculated = true;
1137 conservation.groupRef = this;
1138 conservation.label = "Conservation for " + getName();
1139 conservation.description = "Conservation for group " + getName()
1140 + " less than " + consPercGaps + "% gaps";
1141 return conservation;
1146 * @return true if annotation rows have been instantiated for this group
1148 public boolean hasAnnotationRows()
1150 return consensus != null || conservation != null;
1153 public SequenceI getConsensusSeq()
1156 StringBuffer seqs = new StringBuffer();
1157 for (int i = 0; i < consensus.annotations.length; i++)
1159 if (consensus.annotations[i] != null)
1161 if (consensus.annotations[i].description.charAt(0) == '[')
1163 seqs.append(consensus.annotations[i].description.charAt(1));
1167 seqs.append(consensus.annotations[i].displayCharacter);
1172 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1174 sq.setDescription("Percentage Identity Consensus "
1175 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1179 public void setIgnoreGapsConsensus(boolean state)
1181 if (this.ignoreGapsInConsensus != state && consensus != null)
1183 ignoreGapsInConsensus = state;
1184 recalcConservation();
1186 ignoreGapsInConsensus = state;
1189 public boolean getIgnoreGapsConsensus()
1191 return ignoreGapsInConsensus;
1195 * @param showSequenceLogo
1196 * indicates if a sequence logo is shown for consensus annotation
1198 public void setshowSequenceLogo(boolean showSequenceLogo)
1200 // TODO: decouple calculation from settings update
1201 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1203 this.showSequenceLogo = showSequenceLogo;
1204 recalcConservation();
1206 this.showSequenceLogo = showSequenceLogo;
1211 * @param showConsHist
1212 * flag indicating if the consensus histogram for this group should
1215 public void setShowConsensusHistogram(boolean showConsHist)
1218 if (showConsensusHistogram != showConsHist && consensus != null)
1220 this.showConsensusHistogram = showConsHist;
1221 recalcConservation();
1223 this.showConsensusHistogram = showConsHist;
1227 * @return the showConsensusHistogram
1229 public boolean isShowConsensusHistogram()
1231 return showConsensusHistogram;
1235 * set flag indicating if logo should be normalised when rendered
1239 public void setNormaliseSequenceLogo(boolean norm)
1241 normaliseSequenceLogo = norm;
1244 public boolean isNormaliseSequenceLogo()
1246 return normaliseSequenceLogo;
1251 * returns a new array with all annotation involving this group
1253 public AlignmentAnnotation[] getAlignmentAnnotation()
1255 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1257 ArrayList<AlignmentAnnotation> annot = new ArrayList<>();
1258 synchronized (sequences)
1260 for (SequenceI seq : sequences)
1262 AlignmentAnnotation[] aa = seq.getAnnotation();
1265 for (AlignmentAnnotation al : aa)
1267 if (al.groupRef == this)
1274 if (consensus != null)
1276 annot.add(consensus);
1278 if (conservation != null)
1280 annot.add(conservation);
1283 return annot.toArray(new AlignmentAnnotation[0]);
1287 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1289 List<AlignmentAnnotation> aa = new ArrayList<>();
1294 for (AlignmentAnnotation a : getAlignmentAnnotation())
1296 if (calcId.equals(a.getCalcId()))
1305 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1306 String calcId, String label)
1308 ArrayList<AlignmentAnnotation> aa = new ArrayList<>();
1309 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1311 if ((calcId == null || (ann.getCalcId() != null
1312 && ann.getCalcId().equals(calcId)))
1313 && (seq == null || (ann.sequenceRef != null
1314 && ann.sequenceRef == seq))
1316 || (ann.label != null && ann.label.equals(label))))
1325 * Answer true if any annotation matches the calcId passed in (if not null).
1330 public boolean hasAnnotation(String calcId)
1332 if (calcId != null && !"".equals(calcId))
1334 for (AlignmentAnnotation a : getAlignmentAnnotation())
1336 if (a.getCalcId() == calcId)
1346 * Remove all sequences from the group (leaving other properties unchanged).
1350 synchronized (sequences)
1357 * Sets the alignment or group context for this group, and whether it is
1358 * defined as a group
1361 * the context for the group
1363 * whether the group is defined on the alignment or is just a
1365 * @throws IllegalArgumentException
1366 * if setting the context would result in a circular reference chain
1368 public void setContext(AnnotatedCollectionI ctx, boolean defined)
1371 this.isDefined = defined;
1375 * Sets the alignment or group context for this group
1378 * the context for the group
1379 * @throws IllegalArgumentException
1380 * if setting the context would result in a circular reference chain
1382 public void setContext(AnnotatedCollectionI ctx)
1384 AnnotatedCollectionI ref = ctx;
1387 if (ref == this || ref.getContext() == ctx)
1389 throw new IllegalArgumentException(
1390 "Circular reference in SequenceGroup.context");
1392 ref = ref.getContext();
1400 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1403 public AnnotatedCollectionI getContext()
1408 public boolean isDefined()
1413 public void setColourScheme(ColourSchemeI scheme)
1417 cs = new ResidueShader();
1419 cs.setColourScheme(scheme);
1422 public void setGroupColourScheme(ResidueShaderI scheme)
1427 public ColourSchemeI getColourScheme()
1429 return cs == null ? null : cs.getColourScheme();
1432 public ResidueShaderI getGroupColourScheme()
1438 public boolean isNucleotide()
1440 if (context != null)
1442 return context.isNucleotide();
1449 * @return true if seq is a member of the group
1452 public boolean contains(SequenceI seq1)
1454 return sequences.contains(seq1);
1460 * @return true if startRes<=apos and endRes>=apos and seq is in the group
1462 public boolean contains(SequenceI seq, int apos)
1464 return (startRes <= apos && endRes >= apos) && sequences.contains(seq);