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;
26 import jalview.schemes.ResidueProperties;
27 import jalview.util.Comparison;
29 import java.awt.Color;
30 import java.util.ArrayList;
31 import java.util.Hashtable;
32 import java.util.List;
34 import java.util.Vector;
37 * Collects a set contiguous ranges on a set of sequences
42 public class SequenceGroup implements AnnotatedCollectionI
48 Conservation conserve;
52 boolean displayBoxes = true;
54 boolean displayText = true;
56 boolean colourText = false;
59 * after Olivier's non-conserved only character display
61 boolean showNonconserved = false;
66 private List<SequenceI> sequences = new ArrayList<SequenceI>();
69 * representative sequence for this group (if any)
71 private SequenceI seqrep = null;
76 * Colourscheme applied to group if any
78 public ColourSchemeI cs;
80 // start column (base 0)
83 // end column (base 0)
86 public Color outlineColour = Color.black;
88 public Color idColour = null;
90 public int thresholdTextColour = 0;
92 public Color textColour = Color.black;
94 public Color textColour2 = Color.white;
97 * consensus calculation property
99 private boolean ignoreGapsInConsensus = true;
102 * consensus calculation property
104 private boolean showSequenceLogo = false;
107 * flag indicating if logo should be rendered normalised
109 private boolean normaliseSequenceLogo;
112 * @return the includeAllConsSymbols
114 public boolean isShowSequenceLogo()
116 return showSequenceLogo;
120 * Creates a new SequenceGroup object.
122 public SequenceGroup()
124 groupName = "JGroup:" + this.hashCode();
128 * Creates a new SequenceGroup object.
133 * @param displayBoxes
137 * first column of group
139 * last column of group
141 public SequenceGroup(List<SequenceI> sequences, String groupName,
142 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
143 boolean colourText, int start, int end)
145 this.sequences = sequences;
146 this.groupName = groupName;
147 this.displayBoxes = displayBoxes;
148 this.displayText = displayText;
149 this.colourText = colourText;
153 recalcConservation();
161 public SequenceGroup(SequenceGroup seqsel)
165 sequences = new ArrayList<SequenceI>();
166 sequences.addAll(seqsel.sequences);
167 if (seqsel.groupName != null)
169 groupName = new String(seqsel.groupName);
171 displayBoxes = seqsel.displayBoxes;
172 displayText = seqsel.displayText;
173 colourText = seqsel.colourText;
174 startRes = seqsel.startRes;
175 endRes = seqsel.endRes;
177 if (seqsel.description != null)
179 description = new String(seqsel.description);
181 hidecols = seqsel.hidecols;
182 hidereps = seqsel.hidereps;
183 idColour = seqsel.idColour;
184 outlineColour = seqsel.outlineColour;
185 seqrep = seqsel.seqrep;
186 textColour = seqsel.textColour;
187 textColour2 = seqsel.textColour2;
188 thresholdTextColour = seqsel.thresholdTextColour;
189 width = seqsel.width;
190 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
191 if (seqsel.conserve != null)
193 recalcConservation(); // safer than
194 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
199 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
201 int iSize = sequences.size();
202 SequenceI[] seqs = new SequenceI[iSize];
203 SequenceI[] inorder = getSequencesInOrder(align);
205 for (int i = 0, ipos = 0; i < inorder.length; i++)
207 SequenceI seq = inorder[i];
209 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
210 if (seqs[ipos] != null)
212 seqs[ipos].setDescription(seq.getDescription());
213 seqs[ipos].setDBRef(seq.getDBRef());
214 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
215 if (seq.getDatasetSequence() != null)
217 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
220 if (seq.getAnnotation() != null)
222 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
223 // Only copy annotation that is either a score or referenced by the
224 // alignment's annotation vector
225 for (int a = 0; a < seq.getAnnotation().length; a++)
227 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
230 boolean found = false;
231 for (int pos = 0; pos < alann.length; pos++)
233 if (alann[pos] == tocopy)
244 AlignmentAnnotation newannot = new AlignmentAnnotation(
245 seq.getAnnotation()[a]);
246 newannot.restrict(startRes, endRes);
247 newannot.setSequenceRef(seqs[ipos]);
248 newannot.adjustForAlignment();
249 seqs[ipos].addAlignmentAnnotation(newannot);
259 if (iSize != inorder.length)
261 SequenceI[] nseqs = new SequenceI[iSize];
262 System.arraycopy(seqs, 0, nseqs, 0, iSize);
270 * If sequence ends in gaps, the end residue can be correctly calculated here
276 public int findEndRes(SequenceI seq)
281 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
283 ch = seq.getCharAt(j);
284 if (!Comparison.isGap((ch)))
292 eres += seq.getStart() - 1;
299 public List<SequenceI> getSequences()
305 public List<SequenceI> getSequences(
306 Map<SequenceI, SequenceCollectionI> hiddenReps)
308 if (hiddenReps == null)
310 // TODO: need a synchronizedCollection here ?
315 List<SequenceI> allSequences = new ArrayList<SequenceI>();
316 for (SequenceI seq : sequences)
318 allSequences.add(seq);
319 if (hiddenReps.containsKey(seq))
321 SequenceCollectionI hsg = hiddenReps.get(seq);
322 for (SequenceI seq2 : hsg.getSequences())
324 if (seq2 != seq && !allSequences.contains(seq2))
326 allSequences.add(seq2);
336 public SequenceI[] getSequencesAsArray(
337 Map<SequenceI, SequenceCollectionI> map)
339 List<SequenceI> tmp = getSequences(map);
344 return tmp.toArray(new SequenceI[tmp.size()]);
353 * @return DOCUMENT ME!
355 public boolean adjustForRemoveLeft(int col)
357 // return value is true if the group still exists
360 startRes = startRes - col;
365 endRes = endRes - col;
367 if (startRes > endRes)
374 // must delete this group!!
387 * @return DOCUMENT ME!
389 public boolean adjustForRemoveRight(int col)
408 * @return DOCUMENT ME!
410 public String getName()
415 public String getDescription()
426 public void setName(String name)
429 // TODO: URGENT: update dependent objects (annotation row)
432 public void setDescription(String desc)
440 * @return DOCUMENT ME!
442 public Conservation getConservation()
453 public void setConservation(Conservation c)
459 * Add s to this sequence group. If aligment sequence is already contained in
460 * group, it will not be added again, but recalculation may happen if the flag
464 * alignment sequence to be added
466 * true means Group's conservation should be recalculated
468 public void addSequence(SequenceI s, boolean recalc)
470 synchronized (sequences)
472 if (s != null && !sequences.contains(s))
479 recalcConservation();
485 * Max Gaps Threshold (percent) for performing a conservation calculation
487 private int consPercGaps = 25;
490 * @return Max Gaps Threshold for performing a conservation calculation
492 public int getConsPercGaps()
498 * set Max Gaps Threshold (percent) for performing a conservation calculation
500 * @param consPercGaps
502 public void setConsPercGaps(int consPercGaps)
504 this.consPercGaps = consPercGaps;
508 * calculate residue conservation for group - but only if necessary.
510 public void recalcConservation()
512 if (cs == null && consensus == null && conservation == null)
518 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
519 endRes + 1, showSequenceLogo);
520 if (consensus != null)
522 _updateConsensusRow(cnsns, sequences.size());
526 cs.setConsensus(cnsns);
529 if ((conservation != null)
530 || (cs != null && cs.conservationApplied()))
532 Conservation c = new Conservation(groupName,
533 ResidueProperties.propHash, 3, sequences, startRes,
536 c.verdict(false, consPercGaps);
537 if (conservation != null)
539 _updateConservationRow(c);
543 if (cs.conservationApplied())
545 cs.setConservation(c);
551 cs.alignmentChanged(context != null ? context : this, null);
553 } catch (java.lang.OutOfMemoryError err)
556 System.out.println("Out of memory loading groups: " + err);
561 private void _updateConservationRow(Conservation c)
563 if (conservation == null)
568 conservation.label = "Conservation for " + getName();
569 conservation.description = "Conservation for group " + getName()
570 + " less than " + consPercGaps + "% gaps";
571 // preserve width if already set
572 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
575 conservation.annotations = null;
576 conservation.annotations = new Annotation[aWidth]; // should be alignment
578 c.completeAnnotations(conservation, null, startRes, endRes + 1);
581 public Hashtable[] consensusData = null;
583 private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
585 if (consensus == null)
589 consensus.label = "Consensus for " + getName();
590 consensus.description = "Percent Identity";
591 consensusData = cnsns;
592 // preserve width if already set
593 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
596 consensus.annotations = null;
597 consensus.annotations = new Annotation[aWidth]; // should be alignment width
599 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
600 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
603 // ignoreGapsInConsensusCalculation);
608 * sequence to either add or remove from group
610 * flag passed to delete/addSequence to indicate if group properties
611 * should be recalculated
613 public void addOrRemove(SequenceI s, boolean recalc)
615 synchronized (sequences)
617 if (sequences.contains(s))
619 deleteSequence(s, recalc);
623 addSequence(s, recalc);
634 * true means recalculate conservation
636 public void deleteSequence(SequenceI s, boolean recalc)
638 synchronized (sequences)
644 recalcConservation();
652 * @return the first column selected by this group. Runs from 0<=i<N_cols
655 public int getStartRes()
662 * @return the groups last selected column. Runs from 0<=i<N_cols
665 public int getEndRes()
671 * Set the first column selected by this group. Runs from 0<=i<N_cols
675 public void setStartRes(int i)
681 * Set the groups last selected column. Runs from 0<=i<N_cols
685 public void setEndRes(int i)
691 * @return number of sequences in group
695 return sequences.size();
700 * @return the ith sequence
702 public SequenceI getSequenceAt(int i)
704 return sequences.get(i);
711 public void setColourText(boolean state)
719 * @return DOCUMENT ME!
721 public boolean getColourText()
732 public void setDisplayText(boolean state)
740 * @return DOCUMENT ME!
742 public boolean getDisplayText()
753 public void setDisplayBoxes(boolean state)
755 displayBoxes = state;
761 * @return DOCUMENT ME!
763 public boolean getDisplayBoxes()
769 * computes the width of current set of sequences and returns it
771 * @return DOCUMENT ME!
774 public int getWidth()
776 synchronized (sequences)
778 // MC This needs to get reset when characters are inserted and deleted
780 for (SequenceI seq:sequences) {
781 if (first || seq.getLength() > width)
783 width = seq.getLength();
797 public void setOutlineColour(Color c)
805 * @return DOCUMENT ME!
807 public Color getOutlineColour()
809 return outlineColour;
814 * returns the sequences in the group ordered by the ordering given by al.
815 * this used to return an array with null entries regardless, new behaviour is
816 * below. TODO: verify that this does not affect use in applet or application
820 * @return SequenceI[] intersection of sequences in group with al, ordered by
821 * al, or null if group does not intersect with al
823 public SequenceI[] getSequencesInOrder(AlignmentI al)
825 return getSequencesInOrder(al, true);
829 * return an array representing the intersection of the group with al,
830 * optionally returning an array the size of al.getHeight() where nulls mark
831 * the non-intersected sequences
835 * @return null or array
837 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
839 synchronized (sequences)
841 int sSize = sequences.size();
842 int alHeight = al.getHeight();
844 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
847 for (int i = 0; i < alHeight && index < sSize; i++)
849 if (sequences.contains(al.getSequenceAt(i)))
851 seqs[(trim) ? index : i] = al.getSequenceAt(i);
863 if (index < seqs.length)
865 SequenceI[] dummy = seqs;
866 seqs = new SequenceI[index];
869 seqs[index] = dummy[index];
878 * @return the idColour
880 public Color getIdColour()
887 * the idColour to set
889 public void setIdColour(Color idColour)
891 this.idColour = idColour;
895 * @return the representative sequence for this group
897 public SequenceI getSeqrep()
903 * set the representative sequence for this group. Note - this affects the
904 * interpretation of the Hidereps attribute.
907 * the seqrep to set (null means no sequence representative)
909 public void setSeqrep(SequenceI seqrep)
911 this.seqrep = seqrep;
916 * @return true if group has a sequence representative
918 public boolean hasSeqrep()
920 return seqrep != null;
924 * visibility of rows or represented rows covered by group
926 private boolean hidereps = false;
929 * set visibility of sequences covered by (if no sequence representative is
930 * defined) or represented by this group.
934 public void setHidereps(boolean visibility)
936 hidereps = visibility;
941 * @return true if sequences represented (or covered) by this group should be
944 public boolean isHidereps()
950 * visibility of columns intersecting this group
952 private boolean hidecols = false;
955 * set intended visibility of columns covered by this group
959 public void setHideCols(boolean visibility)
961 hidecols = visibility;
966 * @return true if columns covered by group should be hidden
968 public boolean isHideCols()
974 * create a new sequence group from the intersection of this group with an
975 * alignment Hashtable of hidden representatives
981 * @return new group containing sequences common to this group and alignment
983 public SequenceGroup intersect(AlignmentI alignment,
984 Map<SequenceI, SequenceCollectionI> map)
986 SequenceGroup sgroup = new SequenceGroup(this);
987 SequenceI[] insect = getSequencesInOrder(alignment);
988 sgroup.sequences = new ArrayList<SequenceI>();
989 for (int s = 0; insect != null && s < insect.length; s++)
991 if (map == null || map.containsKey(insect[s]))
993 sgroup.sequences.add(insect[s]);
1000 * @return the showUnconserved
1002 public boolean getShowNonconserved()
1004 return showNonconserved;
1008 * @param showNonconserved
1009 * the showUnconserved to set
1011 public void setShowNonconserved(boolean displayNonconserved)
1013 this.showNonconserved = displayNonconserved;
1016 AlignmentAnnotation consensus = null, conservation = null;
1019 * flag indicating if consensus histogram should be rendered
1021 private boolean showConsensusHistogram;
1024 * set this alignmentAnnotation object as the one used to render consensus
1029 public void setConsensus(AlignmentAnnotation aan)
1031 if (consensus == null)
1039 * @return automatically calculated consensus row
1041 public AlignmentAnnotation getConsensus()
1043 // TODO get or calculate and get consensus annotation row for this group
1044 int aWidth = this.getWidth();
1052 if (consensus == null)
1054 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1055 100f, AlignmentAnnotation.BAR_GRAPH);
1056 consensus.hasText = true;
1057 consensus.autoCalculated = true;
1058 consensus.groupRef = this;
1059 consensus.label = "Consensus for " + getName();
1060 consensus.description = "Percent Identity";
1066 * set this alignmentAnnotation object as the one used to render consensus
1071 public void setConservationRow(AlignmentAnnotation aan)
1073 if (conservation == null)
1080 * get the conservation annotation row for this group
1082 * @return autoCalculated annotation row
1084 public AlignmentAnnotation getConservationRow()
1086 if (conservation == null)
1088 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1089 11f, AlignmentAnnotation.BAR_GRAPH);
1092 conservation.hasText = true;
1093 conservation.autoCalculated = true;
1094 conservation.groupRef = this;
1095 conservation.label = "Conservation for " + getName();
1096 conservation.description = "Conservation for group " + getName()
1097 + " less than " + consPercGaps + "% gaps";
1098 return conservation;
1103 * @return true if annotation rows have been instantiated for this group
1105 public boolean hasAnnotationRows()
1107 return consensus != null || conservation != null;
1110 public SequenceI getConsensusSeq()
1113 StringBuffer seqs = new StringBuffer();
1114 for (int i = 0; i < consensus.annotations.length; i++)
1116 if (consensus.annotations[i] != null)
1118 if (consensus.annotations[i].description.charAt(0) == '[')
1120 seqs.append(consensus.annotations[i].description.charAt(1));
1124 seqs.append(consensus.annotations[i].displayCharacter);
1129 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1131 sq.setDescription("Percentage Identity Consensus "
1132 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1136 public void setIgnoreGapsConsensus(boolean state)
1138 if (this.ignoreGapsInConsensus != state && consensus != null)
1140 ignoreGapsInConsensus = state;
1141 recalcConservation();
1143 ignoreGapsInConsensus = state;
1146 public boolean getIgnoreGapsConsensus()
1148 return ignoreGapsInConsensus;
1152 * @param showSequenceLogo
1153 * indicates if a sequence logo is shown for consensus annotation
1155 public void setshowSequenceLogo(boolean showSequenceLogo)
1157 // TODO: decouple calculation from settings update
1158 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1160 this.showSequenceLogo = showSequenceLogo;
1161 recalcConservation();
1163 this.showSequenceLogo = showSequenceLogo;
1168 * @param showConsHist
1169 * flag indicating if the consensus histogram for this group should
1172 public void setShowConsensusHistogram(boolean showConsHist)
1175 if (showConsensusHistogram != showConsHist && consensus != null)
1177 this.showConsensusHistogram = showConsHist;
1178 recalcConservation();
1180 this.showConsensusHistogram = showConsHist;
1184 * @return the showConsensusHistogram
1186 public boolean isShowConsensusHistogram()
1188 return showConsensusHistogram;
1192 * set flag indicating if logo should be normalised when rendered
1196 public void setNormaliseSequenceLogo(boolean norm)
1198 normaliseSequenceLogo = norm;
1201 public boolean isNormaliseSequenceLogo()
1203 return normaliseSequenceLogo;
1208 * returns a new array with all annotation involving this group
1210 public AlignmentAnnotation[] getAlignmentAnnotation()
1212 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1214 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1215 synchronized (sequences)
1217 for (SequenceI seq : sequences)
1219 AlignmentAnnotation[] aa = seq.getAnnotation();
1222 for (AlignmentAnnotation al : aa)
1224 if (al.groupRef == this)
1231 if (consensus != null)
1233 annot.add(consensus);
1235 if (conservation != null)
1237 annot.add(conservation);
1240 return annot.toArray(new AlignmentAnnotation[0]);
1244 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1246 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1247 for (AlignmentAnnotation a : getAlignmentAnnotation())
1249 if (a.getCalcId() == calcId)
1258 * Returns a list of annotations that match the specified sequenceRef, calcId
1259 * and label, ignoring null values.
1261 * @return list of AlignmentAnnotation objects
1264 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1265 String calcId, String label)
1267 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1268 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1270 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1271 && ann.sequenceRef != null && ann.sequenceRef == seq
1272 && ann.label != null && ann.label.equals(label))
1281 * Answer true if any annotation matches the calcId passed in (if not null).
1286 public boolean hasAnnotation(String calcId)
1288 if (calcId != null && !"".equals(calcId))
1290 for (AlignmentAnnotation a : getAlignmentAnnotation())
1292 if (a.getCalcId() == calcId)
1302 * Remove all sequences from the group (leaving other properties unchanged).
1306 synchronized (sequences)
1312 private AnnotatedCollectionI context;
1315 * set the alignment or group context for this group
1319 public void setContext(AnnotatedCollectionI context)
1321 this.context = context;
1327 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1330 public AnnotatedCollectionI getContext()