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.api.ColorI;
26 import jalview.schemes.Colour;
27 import jalview.schemes.ColourSchemeI;
28 import jalview.schemes.ResidueProperties;
30 import java.awt.Color;
31 import java.util.ArrayList;
32 import java.util.Hashtable;
33 import java.util.List;
35 import java.util.Vector;
38 * Collects a set contiguous ranges on a set of sequences
43 public class SequenceGroup implements AnnotatedCollectionI
49 Conservation conserve;
53 boolean displayBoxes = true;
55 boolean displayText = true;
57 boolean colourText = false;
60 * after Olivier's non-conserved only character display
62 boolean showNonconserved = false;
67 private List<SequenceI> sequences = new ArrayList<SequenceI>();
70 * representative sequence for this group (if any)
72 private SequenceI seqrep = null;
77 * Colourscheme applied to group if any
79 public ColourSchemeI cs;
81 // start column (base 0)
84 // end column (base 0)
87 public ColorI outlineColour = Colour.black;
89 public ColorI idColour = null;
91 public int thresholdTextColour = 0;
93 public ColorI textColour = Colour.black;
95 public ColorI textColour2 = Colour.white;
98 * consensus calculation property
100 private boolean ignoreGapsInConsensus = true;
103 * consensus calculation property
105 private boolean showSequenceLogo = false;
108 * flag indicating if logo should be rendered normalised
110 private boolean normaliseSequenceLogo;
113 * @return the includeAllConsSymbols
115 public boolean isShowSequenceLogo()
117 return showSequenceLogo;
121 * Creates a new SequenceGroup object.
123 public SequenceGroup()
125 groupName = "JGroup:" + this.hashCode();
129 * Creates a new SequenceGroup object.
134 * @param displayBoxes
138 * first column of group
140 * last column of group
142 public SequenceGroup(List<SequenceI> sequences, String groupName,
143 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
144 boolean colourText, int start, int end)
146 this.sequences = sequences;
147 this.groupName = groupName;
148 this.displayBoxes = displayBoxes;
149 this.displayText = displayText;
150 this.colourText = colourText;
154 recalcConservation();
162 public SequenceGroup(SequenceGroup seqsel)
166 sequences = new ArrayList<SequenceI>();
167 sequences.addAll(seqsel.sequences);
168 if (seqsel.groupName != null)
170 groupName = new String(seqsel.groupName);
172 displayBoxes = seqsel.displayBoxes;
173 displayText = seqsel.displayText;
174 colourText = seqsel.colourText;
175 startRes = seqsel.startRes;
176 endRes = seqsel.endRes;
178 if (seqsel.description != null)
180 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].setDBRefs(seq.getDBRefs());
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)
245 AlignmentAnnotation newannot = new AlignmentAnnotation(
246 seq.getAnnotation()[a]);
247 newannot.restrict(startRes, endRes);
248 newannot.setSequenceRef(seqs[ipos]);
249 newannot.adjustForAlignment();
250 seqs[ipos].addAlignmentAnnotation(newannot);
260 if (iSize != inorder.length)
262 SequenceI[] nseqs = new SequenceI[iSize];
263 System.arraycopy(seqs, 0, nseqs, 0, iSize);
271 * If sequence ends in gaps, the end residue can be correctly calculated here
277 public int findEndRes(SequenceI seq)
282 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
284 ch = seq.getCharAt(j);
285 if (!jalview.util.Comparison.isGap((ch)))
293 eres += seq.getStart() - 1;
300 public List<SequenceI> getSequences()
306 public List<SequenceI> getSequences(
307 Map<SequenceI, SequenceCollectionI> hiddenReps)
309 if (hiddenReps == null)
311 // TODO: need a synchronizedCollection here ?
316 List<SequenceI> allSequences = new ArrayList<SequenceI>();
317 for (SequenceI seq : sequences)
319 allSequences.add(seq);
320 if (hiddenReps.containsKey(seq))
322 SequenceCollectionI hsg = hiddenReps.get(seq);
323 for (SequenceI seq2 : hsg.getSequences())
325 if (seq2 != seq && !allSequences.contains(seq2))
327 allSequences.add(seq2);
337 public SequenceI[] getSequencesAsArray(
338 Map<SequenceI, SequenceCollectionI> map)
340 List<SequenceI> tmp = getSequences(map);
345 return tmp.toArray(new SequenceI[tmp.size()]);
354 * @return DOCUMENT ME!
356 public boolean adjustForRemoveLeft(int col)
358 // return value is true if the group still exists
361 startRes = startRes - col;
366 endRes = endRes - col;
368 if (startRes > endRes)
375 // must delete this group!!
388 * @return DOCUMENT ME!
390 public boolean adjustForRemoveRight(int col)
409 * @return DOCUMENT ME!
411 public String getName()
416 public String getDescription()
427 public void setName(String name)
430 // TODO: URGENT: update dependent objects (annotation row)
433 public void setDescription(String desc)
441 * @return DOCUMENT ME!
443 public Conservation getConservation()
454 public void setConservation(Conservation c)
460 * Add s to this sequence group. If aligment sequence is already contained in
461 * group, it will not be added again, but recalculation may happen if the flag
465 * alignment sequence to be added
467 * true means Group's conservation should be recalculated
469 public void addSequence(SequenceI s, boolean recalc)
471 synchronized (sequences)
473 if (s != null && !sequences.contains(s))
480 recalcConservation();
486 * Max Gaps Threshold (percent) for performing a conservation calculation
488 private int consPercGaps = 25;
491 * @return Max Gaps Threshold for performing a conservation calculation
493 public int getConsPercGaps()
499 * set Max Gaps Threshold (percent) for performing a conservation calculation
501 * @param consPercGaps
503 public void setConsPercGaps(int consPercGaps)
505 this.consPercGaps = consPercGaps;
509 * calculate residue conservation and colourschemes for group - but only if
510 * necessary. returns true if the calculation resulted in a visible change to
513 public boolean recalcConservation()
515 return recalcConservation(false);
519 * calculate residue conservation for group - but only if necessary. returns
520 * true if the calculation resulted in a visible change to group
523 * when set, colourschemes for this group are not refreshed after
526 public boolean recalcConservation(boolean defer)
528 if (cs == null && consensus == null && conservation == null)
532 // TODO: try harder to detect changes in state in order to minimise
533 // recalculation effort
537 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
538 endRes + 1, showSequenceLogo);
539 if (consensus != null)
541 _updateConsensusRow(cnsns, sequences.size());
546 cs.setConsensus(cnsns);
550 if ((conservation != null)
551 || (cs != null && cs.conservationApplied()))
553 Conservation c = new Conservation(groupName,
554 ResidueProperties.propHash, 3, sequences, startRes,
557 c.verdict(false, consPercGaps);
558 if (conservation != null)
560 _updateConservationRow(c);
564 if (cs.conservationApplied())
566 cs.setConservation(c);
569 // eager update - will cause a refresh of overview regardless
572 if (cs != null && !defer)
574 // TODO: JAL-2034 should cs.alignmentChanged modify return state
575 cs.alignmentChanged(context != null ? context : this, null);
582 } catch (java.lang.OutOfMemoryError err)
585 System.out.println("Out of memory loading groups: " + err);
590 private void _updateConservationRow(Conservation c)
592 if (conservation == null)
597 conservation.label = "Conservation for " + getName();
598 conservation.description = "Conservation for group " + getName()
599 + " less than " + consPercGaps + "% gaps";
600 // preserve width if already set
601 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
604 conservation.annotations = null;
605 conservation.annotations = new Annotation[aWidth]; // should be alignment
607 c.completeAnnotations(conservation, null, startRes, endRes + 1);
610 public Hashtable[] consensusData = null;
612 private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
614 if (consensus == null)
618 consensus.label = "Consensus for " + getName();
619 consensus.description = "Percent Identity";
620 consensusData = cnsns;
621 // preserve width if already set
622 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
625 consensus.annotations = null;
626 consensus.annotations = new Annotation[aWidth]; // should be alignment width
628 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
629 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
632 // ignoreGapsInConsensusCalculation);
637 * sequence to either add or remove from group
639 * flag passed to delete/addSequence to indicate if group properties
640 * should be recalculated
642 public void addOrRemove(SequenceI s, boolean recalc)
644 synchronized (sequences)
646 if (sequences.contains(s))
648 deleteSequence(s, recalc);
652 addSequence(s, recalc);
663 * true means recalculate conservation
665 public void deleteSequence(SequenceI s, boolean recalc)
667 synchronized (sequences)
673 recalcConservation();
681 * @return the first column selected by this group. Runs from 0<=i<N_cols
684 public int getStartRes()
691 * @return the groups last selected column. Runs from 0<=i<N_cols
694 public int getEndRes()
700 * Set the first column selected by this group. Runs from 0<=i<N_cols
704 public void setStartRes(int i)
710 * Set the groups last selected column. Runs from 0<=i<N_cols
714 public void setEndRes(int i)
720 * @return number of sequences in group
724 return sequences.size();
729 * @return the ith sequence
731 public SequenceI getSequenceAt(int i)
733 return sequences.get(i);
740 public void setColourText(boolean state)
748 * @return DOCUMENT ME!
750 public boolean getColourText()
761 public void setDisplayText(boolean state)
769 * @return DOCUMENT ME!
771 public boolean getDisplayText()
782 public void setDisplayBoxes(boolean state)
784 displayBoxes = state;
790 * @return DOCUMENT ME!
792 public boolean getDisplayBoxes()
798 * computes the width of current set of sequences and returns it
800 * @return DOCUMENT ME!
803 public int getWidth()
805 synchronized (sequences)
807 // MC This needs to get reset when characters are inserted and deleted
808 boolean first = true;
809 for (SequenceI seq : sequences)
811 if (first || seq.getLength() > width)
813 width = seq.getLength();
827 public void setOutlineColour(Color c)
829 outlineColour = new Colour(c);
835 * @return DOCUMENT ME!
837 public ColorI getOutlineColour()
839 return outlineColour;
844 * returns the sequences in the group ordered by the ordering given by al.
845 * this used to return an array with null entries regardless, new behaviour is
846 * below. TODO: verify that this does not affect use in applet or application
850 * @return SequenceI[] intersection of sequences in group with al, ordered by
851 * al, or null if group does not intersect with al
853 public SequenceI[] getSequencesInOrder(AlignmentI al)
855 return getSequencesInOrder(al, true);
859 * return an array representing the intersection of the group with al,
860 * optionally returning an array the size of al.getHeight() where nulls mark
861 * the non-intersected sequences
865 * @return null or array
867 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
869 synchronized (sequences)
871 int sSize = sequences.size();
872 int alHeight = al.getHeight();
874 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
877 for (int i = 0; i < alHeight && index < sSize; i++)
879 if (sequences.contains(al.getSequenceAt(i)))
881 seqs[(trim) ? index : i] = al.getSequenceAt(i);
893 if (index < seqs.length)
895 SequenceI[] dummy = seqs;
896 seqs = new SequenceI[index];
899 seqs[index] = dummy[index];
908 * @return the idColour
910 public ColorI getIdColour()
917 * the idColour to set
919 public void setIdColour(Color idColour)
921 this.idColour = new Colour(idColour);
925 * @return the representative sequence for this group
928 public SequenceI getSeqrep()
934 * set the representative sequence for this group. Note - this affects the
935 * interpretation of the Hidereps attribute.
938 * the seqrep to set (null means no sequence representative)
941 public void setSeqrep(SequenceI seqrep)
943 this.seqrep = seqrep;
948 * @return true if group has a sequence representative
951 public boolean hasSeqrep()
953 return seqrep != null;
957 * visibility of rows or represented rows covered by group
959 private boolean hidereps = false;
962 * set visibility of sequences covered by (if no sequence representative is
963 * defined) or represented by this group.
967 public void setHidereps(boolean visibility)
969 hidereps = visibility;
974 * @return true if sequences represented (or covered) by this group should be
977 public boolean isHidereps()
983 * visibility of columns intersecting this group
985 private boolean hidecols = false;
988 * set intended visibility of columns covered by this group
992 public void setHideCols(boolean visibility)
994 hidecols = visibility;
999 * @return true if columns covered by group should be hidden
1001 public boolean isHideCols()
1007 * create a new sequence group from the intersection of this group with an
1008 * alignment Hashtable of hidden representatives
1014 * @return new group containing sequences common to this group and alignment
1016 public SequenceGroup intersect(AlignmentI alignment,
1017 Map<SequenceI, SequenceCollectionI> map)
1019 SequenceGroup sgroup = new SequenceGroup(this);
1020 SequenceI[] insect = getSequencesInOrder(alignment);
1021 sgroup.sequences = new ArrayList<SequenceI>();
1022 for (int s = 0; insect != null && s < insect.length; s++)
1024 if (map == null || map.containsKey(insect[s]))
1026 sgroup.sequences.add(insect[s]);
1033 * @return the showUnconserved
1035 public boolean getShowNonconserved()
1037 return showNonconserved;
1041 * @param showNonconserved
1042 * the showUnconserved to set
1044 public void setShowNonconserved(boolean displayNonconserved)
1046 this.showNonconserved = displayNonconserved;
1049 AlignmentAnnotation consensus = null, conservation = null;
1052 * flag indicating if consensus histogram should be rendered
1054 private boolean showConsensusHistogram;
1057 * set this alignmentAnnotation object as the one used to render consensus
1062 public void setConsensus(AlignmentAnnotation aan)
1064 if (consensus == null)
1072 * @return automatically calculated consensus row note: the row is a stub if a
1073 * consensus calculation has not yet been performed on the group
1075 public AlignmentAnnotation getConsensus()
1077 // TODO get or calculate and get consensus annotation row for this group
1078 int aWidth = this.getWidth();
1086 if (consensus == null)
1088 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1089 100f, AlignmentAnnotation.BAR_GRAPH);
1090 consensus.hasText = true;
1091 consensus.autoCalculated = true;
1092 consensus.groupRef = this;
1093 consensus.label = "Consensus for " + getName();
1094 consensus.description = "Percent Identity";
1100 * set this alignmentAnnotation object as the one used to render consensus
1105 public void setConservationRow(AlignmentAnnotation aan)
1107 if (conservation == null)
1114 * get the conservation annotation row for this group
1116 * @return autoCalculated annotation row
1118 public AlignmentAnnotation getConservationRow()
1120 if (conservation == null)
1122 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1123 11f, AlignmentAnnotation.BAR_GRAPH);
1126 conservation.hasText = true;
1127 conservation.autoCalculated = true;
1128 conservation.groupRef = this;
1129 conservation.label = "Conservation for " + getName();
1130 conservation.description = "Conservation for group " + getName()
1131 + " less than " + consPercGaps + "% gaps";
1132 return conservation;
1137 * @return true if annotation rows have been instantiated for this group
1139 public boolean hasAnnotationRows()
1141 return consensus != null || conservation != null;
1144 public SequenceI getConsensusSeq()
1147 StringBuffer seqs = new StringBuffer();
1148 for (int i = 0; i < consensus.annotations.length; i++)
1150 if (consensus.annotations[i] != null)
1152 if (consensus.annotations[i].description.charAt(0) == '[')
1154 seqs.append(consensus.annotations[i].description.charAt(1));
1158 seqs.append(consensus.annotations[i].displayCharacter);
1163 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1165 sq.setDescription("Percentage Identity Consensus "
1166 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1170 public void setIgnoreGapsConsensus(boolean state)
1172 if (this.ignoreGapsInConsensus != state && consensus != null)
1174 ignoreGapsInConsensus = state;
1175 recalcConservation();
1177 ignoreGapsInConsensus = state;
1180 public boolean getIgnoreGapsConsensus()
1182 return ignoreGapsInConsensus;
1186 * @param showSequenceLogo
1187 * indicates if a sequence logo is shown for consensus annotation
1189 public void setshowSequenceLogo(boolean showSequenceLogo)
1191 // TODO: decouple calculation from settings update
1192 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1194 this.showSequenceLogo = showSequenceLogo;
1195 recalcConservation();
1197 this.showSequenceLogo = showSequenceLogo;
1202 * @param showConsHist
1203 * flag indicating if the consensus histogram for this group should
1206 public void setShowConsensusHistogram(boolean showConsHist)
1209 if (showConsensusHistogram != showConsHist && consensus != null)
1211 this.showConsensusHistogram = showConsHist;
1212 recalcConservation();
1214 this.showConsensusHistogram = showConsHist;
1218 * @return the showConsensusHistogram
1220 public boolean isShowConsensusHistogram()
1222 return showConsensusHistogram;
1226 * set flag indicating if logo should be normalised when rendered
1230 public void setNormaliseSequenceLogo(boolean norm)
1232 normaliseSequenceLogo = norm;
1235 public boolean isNormaliseSequenceLogo()
1237 return normaliseSequenceLogo;
1242 * returns a new array with all annotation involving this group
1244 public AlignmentAnnotation[] getAlignmentAnnotation()
1246 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1248 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1249 synchronized (sequences)
1251 for (SequenceI seq : sequences)
1253 AlignmentAnnotation[] aa = seq.getAnnotation();
1256 for (AlignmentAnnotation al : aa)
1258 if (al.groupRef == this)
1265 if (consensus != null)
1267 annot.add(consensus);
1269 if (conservation != null)
1271 annot.add(conservation);
1274 return annot.toArray(new AlignmentAnnotation[0]);
1278 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1280 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1281 for (AlignmentAnnotation a : getAlignmentAnnotation())
1283 if (a.getCalcId() == calcId)
1292 * Returns a list of annotations that match the specified sequenceRef, calcId
1293 * and label, ignoring null values.
1295 * @return list of AlignmentAnnotation objects
1298 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1299 String calcId, String label)
1301 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1302 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1304 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1305 && ann.sequenceRef != null && ann.sequenceRef == seq
1306 && ann.label != null && ann.label.equals(label))
1315 * Answer true if any annotation matches the calcId passed in (if not null).
1320 public boolean hasAnnotation(String calcId)
1322 if (calcId != null && !"".equals(calcId))
1324 for (AlignmentAnnotation a : getAlignmentAnnotation())
1326 if (a.getCalcId() == calcId)
1336 * Remove all sequences from the group (leaving other properties unchanged).
1340 synchronized (sequences)
1346 private AnnotatedCollectionI context;
1349 * set the alignment or group context for this group
1353 public void setContext(AnnotatedCollectionI context)
1355 this.context = context;
1361 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1364 public AnnotatedCollectionI getContext()