2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
28 import java.awt.Color;
29 import java.util.ArrayList;
30 import java.util.Hashtable;
31 import java.util.List;
33 import java.util.Vector;
36 * Collects a set contiguous ranges on a set of sequences
41 public class SequenceGroup implements AnnotatedCollectionI
47 Conservation conserve;
51 boolean displayBoxes = true;
53 boolean displayText = true;
55 boolean colourText = false;
58 * after Olivier's non-conserved only character display
60 boolean showNonconserved = false;
65 private List<SequenceI> sequences = new ArrayList<SequenceI>();
68 * representative sequence for this group (if any)
70 private SequenceI seqrep = null;
75 * Colourscheme applied to group if any
77 public ColourSchemeI cs;
83 public Color outlineColour = Color.black;
85 public Color idColour = null;
87 public int thresholdTextColour = 0;
89 public Color textColour = Color.black;
91 public Color textColour2 = Color.white;
94 * consensus calculation property
96 private boolean ignoreGapsInConsensus = true;
99 * consensus calculation property
101 private boolean showSequenceLogo = false;
104 * flag indicating if logo should be rendered normalised
106 private boolean normaliseSequenceLogo;
109 * @return the includeAllConsSymbols
111 public boolean isShowSequenceLogo()
113 return showSequenceLogo;
117 * Creates a new SequenceGroup object.
119 public SequenceGroup()
121 groupName = "JGroup:" + this.hashCode();
125 * Creates a new SequenceGroup object.
130 * @param displayBoxes
134 * first column of group
136 * last column of group
138 public SequenceGroup(List<SequenceI> sequences, String groupName,
139 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
140 boolean colourText, int start, int end)
142 this.sequences = sequences;
143 this.groupName = groupName;
144 this.displayBoxes = displayBoxes;
145 this.displayText = displayText;
146 this.colourText = colourText;
150 recalcConservation();
158 public SequenceGroup(SequenceGroup seqsel)
162 sequences = new ArrayList<SequenceI>();
163 sequences.addAll(seqsel.sequences);
164 if (seqsel.groupName != null)
166 groupName = new String(seqsel.groupName);
168 displayBoxes = seqsel.displayBoxes;
169 displayText = seqsel.displayText;
170 colourText = seqsel.colourText;
171 startRes = seqsel.startRes;
172 endRes = seqsel.endRes;
174 if (seqsel.description != null)
176 description = new String(seqsel.description);
178 hidecols = seqsel.hidecols;
179 hidereps = seqsel.hidereps;
180 idColour = seqsel.idColour;
181 outlineColour = seqsel.outlineColour;
182 seqrep = seqsel.seqrep;
183 textColour = seqsel.textColour;
184 textColour2 = seqsel.textColour2;
185 thresholdTextColour = seqsel.thresholdTextColour;
186 width = seqsel.width;
187 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
188 if (seqsel.conserve != null)
190 recalcConservation(); // safer than
191 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
196 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
198 int iSize = sequences.size();
199 SequenceI[] seqs = new SequenceI[iSize];
200 SequenceI[] inorder = getSequencesInOrder(align);
202 for (int i = 0, ipos = 0; i < inorder.length; i++)
204 SequenceI seq = inorder[i];
206 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
207 if (seqs[ipos] != null)
209 seqs[ipos].setDescription(seq.getDescription());
210 seqs[ipos].setDBRef(seq.getDBRef());
211 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
212 if (seq.getDatasetSequence() != null)
214 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
217 if (seq.getAnnotation() != null)
219 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
220 // Only copy annotation that is either a score or referenced by the
221 // alignment's annotation vector
222 for (int a = 0; a < seq.getAnnotation().length; a++)
224 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
227 boolean found = false;
228 for (int pos = 0; pos < alann.length; pos++)
230 if (alann[pos] == tocopy)
241 AlignmentAnnotation newannot = new AlignmentAnnotation(
242 seq.getAnnotation()[a]);
243 newannot.restrict(startRes, endRes);
244 newannot.setSequenceRef(seqs[ipos]);
245 newannot.adjustForAlignment();
246 seqs[ipos].addAlignmentAnnotation(newannot);
256 if (iSize != inorder.length)
258 SequenceI[] nseqs = new SequenceI[iSize];
259 System.arraycopy(seqs, 0, nseqs, 0, iSize);
267 * If sequence ends in gaps, the end residue can be correctly calculated here
273 public int findEndRes(SequenceI seq)
278 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
280 ch = seq.getCharAt(j);
281 if (!jalview.util.Comparison.isGap((ch)))
289 eres += seq.getStart() - 1;
296 public List<SequenceI> getSequences()
302 public List<SequenceI> getSequences(
303 Map<SequenceI, SequenceCollectionI> hiddenReps)
305 if (hiddenReps == null)
307 // TODO: need a synchronizedCollection here ?
312 List<SequenceI> allSequences = new ArrayList<SequenceI>();
313 for (SequenceI seq : sequences)
315 allSequences.add(seq);
316 if (hiddenReps.containsKey(seq))
318 SequenceCollectionI hsg = hiddenReps.get(seq);
319 for (SequenceI seq2 : hsg.getSequences())
321 if (seq2 != seq && !allSequences.contains(seq2))
323 allSequences.add(seq2);
333 public SequenceI[] getSequencesAsArray(
334 Map<SequenceI, SequenceCollectionI> map)
336 List<SequenceI> tmp = getSequences(map);
341 return tmp.toArray(new SequenceI[tmp.size()]);
350 * @return DOCUMENT ME!
352 public boolean adjustForRemoveLeft(int col)
354 // return value is true if the group still exists
357 startRes = startRes - col;
362 endRes = endRes - col;
364 if (startRes > endRes)
371 // must delete this group!!
384 * @return DOCUMENT ME!
386 public boolean adjustForRemoveRight(int col)
405 * @return DOCUMENT ME!
407 public String getName()
412 public String getDescription()
423 public void setName(String name)
426 // TODO: URGENT: update dependent objects (annotation row)
429 public void setDescription(String desc)
437 * @return DOCUMENT ME!
439 public Conservation getConservation()
450 public void setConservation(Conservation c)
456 * Add s to this sequence group. If aligment sequence is already contained in
457 * group, it will not be added again, but recalculation may happen if the flag
461 * alignment sequence to be added
463 * true means Group's conservation should be recalculated
465 public void addSequence(SequenceI s, boolean recalc)
467 synchronized (sequences)
469 if (s != null && !sequences.contains(s))
476 recalcConservation();
482 * Max Gaps Threshold (percent) for performing a conservation calculation
484 private int consPercGaps = 25;
487 * @return Max Gaps Threshold for performing a conservation calculation
489 public int getConsPercGaps()
495 * set Max Gaps Threshold (percent) for performing a conservation calculation
497 * @param consPercGaps
499 public void setConsPercGaps(int consPercGaps)
501 this.consPercGaps = consPercGaps;
505 * calculate residue conservation for group - but only if necessary.
507 public void recalcConservation()
509 if (cs == null && consensus == null && conservation == null)
515 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
516 endRes + 1, showSequenceLogo);
517 if (consensus != null)
519 _updateConsensusRow(cnsns, sequences.size());
523 cs.setConsensus(cnsns);
526 if ((conservation != null)
527 || (cs != null && cs.conservationApplied()))
529 Conservation c = new Conservation(groupName,
530 ResidueProperties.propHash, 3, sequences, startRes,
533 c.verdict(false, consPercGaps);
534 if (conservation != null)
536 _updateConservationRow(c);
540 if (cs.conservationApplied())
542 cs.setConservation(c);
548 cs.alignmentChanged(context != null ? context : this, null);
550 } catch (java.lang.OutOfMemoryError err)
553 System.out.println("Out of memory loading groups: " + err);
558 private void _updateConservationRow(Conservation c)
560 if (conservation == null)
565 conservation.label = "Conservation for " + getName();
566 conservation.description = "Conservation for group " + getName()
567 + " less than " + consPercGaps + "% gaps";
568 // preserve width if already set
569 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
572 conservation.annotations = null;
573 conservation.annotations = new Annotation[aWidth]; // should be alignment
575 c.completeAnnotations(conservation, null, startRes, endRes + 1);
578 public Hashtable[] consensusData = null;
580 private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
582 if (consensus == null)
586 consensus.label = "Consensus for " + getName();
587 consensus.description = "Percent Identity";
588 consensusData = cnsns;
589 // preserve width if already set
590 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
593 consensus.annotations = null;
594 consensus.annotations = new Annotation[aWidth]; // should be alignment width
596 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
597 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
600 // ignoreGapsInConsensusCalculation);
605 * sequence to either add or remove from group
607 * flag passed to delete/addSequence to indicate if group properties
608 * should be recalculated
610 public void addOrRemove(SequenceI s, boolean recalc)
612 synchronized (sequences)
614 if (sequences.contains(s))
616 deleteSequence(s, recalc);
620 addSequence(s, recalc);
631 * true means recalculate conservation
633 public void deleteSequence(SequenceI s, boolean recalc)
635 synchronized (sequences)
641 recalcConservation();
649 * @return the first column selected by this group. Runs from 0<=i<N_cols
652 public int getStartRes()
659 * @return the groups last selected column. Runs from 0<=i<N_cols
662 public int getEndRes()
668 * Set the first column selected by this group. Runs from 0<=i<N_cols
672 public void setStartRes(int i)
678 * Set the groups last selected column. Runs from 0<=i<N_cols
682 public void setEndRes(int i)
688 * @return number of sequences in group
692 return sequences.size();
697 * @return the ith sequence
699 public SequenceI getSequenceAt(int i)
701 return sequences.get(i);
708 public void setColourText(boolean state)
716 * @return DOCUMENT ME!
718 public boolean getColourText()
729 public void setDisplayText(boolean state)
737 * @return DOCUMENT ME!
739 public boolean getDisplayText()
750 public void setDisplayBoxes(boolean state)
752 displayBoxes = state;
758 * @return DOCUMENT ME!
760 public boolean getDisplayBoxes()
766 * computes the width of current set of sequences and returns it
768 * @return DOCUMENT ME!
771 public int getWidth()
773 synchronized (sequences)
775 // MC This needs to get reset when characters are inserted and deleted
777 for (SequenceI seq:sequences) {
778 if (first || seq.getLength() > width)
780 width = seq.getLength();
794 public void setOutlineColour(Color c)
802 * @return DOCUMENT ME!
804 public Color getOutlineColour()
806 return outlineColour;
811 * returns the sequences in the group ordered by the ordering given by al.
812 * this used to return an array with null entries regardless, new behaviour is
813 * below. TODO: verify that this does not affect use in applet or application
817 * @return SequenceI[] intersection of sequences in group with al, ordered by
818 * al, or null if group does not intersect with al
820 public SequenceI[] getSequencesInOrder(AlignmentI al)
822 return getSequencesInOrder(al, true);
826 * return an array representing the intersection of the group with al,
827 * optionally returning an array the size of al.getHeight() where nulls mark
828 * the non-intersected sequences
832 * @return null or array
834 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
836 synchronized (sequences)
838 int sSize = sequences.size();
839 int alHeight = al.getHeight();
841 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
844 for (int i = 0; i < alHeight && index < sSize; i++)
846 if (sequences.contains(al.getSequenceAt(i)))
848 seqs[(trim) ? index : i] = al.getSequenceAt(i);
860 if (index < seqs.length)
862 SequenceI[] dummy = seqs;
863 seqs = new SequenceI[index];
866 seqs[index] = dummy[index];
875 * @return the idColour
877 public Color getIdColour()
884 * the idColour to set
886 public void setIdColour(Color idColour)
888 this.idColour = idColour;
892 * @return the representative sequence for this group
894 public SequenceI getSeqrep()
900 * set the representative sequence for this group. Note - this affects the
901 * interpretation of the Hidereps attribute.
904 * the seqrep to set (null means no sequence representative)
906 public void setSeqrep(SequenceI seqrep)
908 this.seqrep = seqrep;
913 * @return true if group has a sequence representative
915 public boolean hasSeqrep()
917 return seqrep != null;
921 * visibility of rows or represented rows covered by group
923 private boolean hidereps = false;
926 * set visibility of sequences covered by (if no sequence representative is
927 * defined) or represented by this group.
931 public void setHidereps(boolean visibility)
933 hidereps = visibility;
938 * @return true if sequences represented (or covered) by this group should be
941 public boolean isHidereps()
947 * visibility of columns intersecting this group
949 private boolean hidecols = false;
952 * set intended visibility of columns covered by this group
956 public void setHideCols(boolean visibility)
958 hidecols = visibility;
963 * @return true if columns covered by group should be hidden
965 public boolean isHideCols()
971 * create a new sequence group from the intersection of this group with an
972 * alignment Hashtable of hidden representatives
978 * @return new group containing sequences common to this group and alignment
980 public SequenceGroup intersect(AlignmentI alignment,
981 Map<SequenceI, SequenceCollectionI> map)
983 SequenceGroup sgroup = new SequenceGroup(this);
984 SequenceI[] insect = getSequencesInOrder(alignment);
985 sgroup.sequences = new ArrayList<SequenceI>();
986 for (int s = 0; insect != null && s < insect.length; s++)
988 if (map == null || map.containsKey(insect[s]))
990 sgroup.sequences.add(insect[s]);
997 * @return the showUnconserved
999 public boolean getShowNonconserved()
1001 return showNonconserved;
1005 * @param showNonconserved
1006 * the showUnconserved to set
1008 public void setShowNonconserved(boolean displayNonconserved)
1010 this.showNonconserved = displayNonconserved;
1013 AlignmentAnnotation consensus = null, conservation = null;
1016 * flag indicating if consensus histogram should be rendered
1018 private boolean showConsensusHistogram;
1021 * set this alignmentAnnotation object as the one used to render consensus
1026 public void setConsensus(AlignmentAnnotation aan)
1028 if (consensus == null)
1036 * @return automatically calculated consensus row
1038 public AlignmentAnnotation getConsensus()
1040 // TODO get or calculate and get consensus annotation row for this group
1041 int aWidth = this.getWidth();
1049 if (consensus == null)
1051 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1052 100f, AlignmentAnnotation.BAR_GRAPH);
1053 consensus.hasText = true;
1054 consensus.autoCalculated = true;
1055 consensus.groupRef = this;
1056 consensus.label = "Consensus for " + getName();
1057 consensus.description = "Percent Identity";
1063 * set this alignmentAnnotation object as the one used to render consensus
1068 public void setConservationRow(AlignmentAnnotation aan)
1070 if (conservation == null)
1077 * get the conservation annotation row for this group
1079 * @return autoCalculated annotation row
1081 public AlignmentAnnotation getConservationRow()
1083 if (conservation == null)
1085 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1086 11f, AlignmentAnnotation.BAR_GRAPH);
1089 conservation.hasText = true;
1090 conservation.autoCalculated = true;
1091 conservation.groupRef = this;
1092 conservation.label = "Conservation for " + getName();
1093 conservation.description = "Conservation for group " + getName()
1094 + " less than " + consPercGaps + "% gaps";
1095 return conservation;
1100 * @return true if annotation rows have been instantiated for this group
1102 public boolean hasAnnotationRows()
1104 return consensus != null || conservation != null;
1107 public SequenceI getConsensusSeq()
1110 StringBuffer seqs = new StringBuffer();
1111 for (int i = 0; i < consensus.annotations.length; i++)
1113 if (consensus.annotations[i] != null)
1115 if (consensus.annotations[i].description.charAt(0) == '[')
1117 seqs.append(consensus.annotations[i].description.charAt(1));
1121 seqs.append(consensus.annotations[i].displayCharacter);
1126 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1128 sq.setDescription("Percentage Identity Consensus "
1129 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1133 public void setIgnoreGapsConsensus(boolean state)
1135 if (this.ignoreGapsInConsensus != state && consensus != null)
1137 ignoreGapsInConsensus = state;
1138 recalcConservation();
1140 ignoreGapsInConsensus = state;
1143 public boolean getIgnoreGapsConsensus()
1145 return ignoreGapsInConsensus;
1149 * @param showSequenceLogo
1150 * indicates if a sequence logo is shown for consensus annotation
1152 public void setshowSequenceLogo(boolean showSequenceLogo)
1154 // TODO: decouple calculation from settings update
1155 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1157 this.showSequenceLogo = showSequenceLogo;
1158 recalcConservation();
1160 this.showSequenceLogo = showSequenceLogo;
1165 * @param showConsHist
1166 * flag indicating if the consensus histogram for this group should
1169 public void setShowConsensusHistogram(boolean showConsHist)
1172 if (showConsensusHistogram != showConsHist && consensus != null)
1174 this.showConsensusHistogram = showConsHist;
1175 recalcConservation();
1177 this.showConsensusHistogram = showConsHist;
1181 * @return the showConsensusHistogram
1183 public boolean isShowConsensusHistogram()
1185 return showConsensusHistogram;
1189 * set flag indicating if logo should be normalised when rendered
1193 public void setNormaliseSequenceLogo(boolean norm)
1195 normaliseSequenceLogo = norm;
1198 public boolean isNormaliseSequenceLogo()
1200 return normaliseSequenceLogo;
1205 * returns a new array with all annotation involving this group
1207 public AlignmentAnnotation[] getAlignmentAnnotation()
1209 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1211 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1212 synchronized (sequences)
1214 for (SequenceI seq : sequences)
1216 AlignmentAnnotation[] aa = seq.getAnnotation();
1219 for (AlignmentAnnotation al : aa)
1221 if (al.groupRef == this)
1228 if (consensus != null)
1230 annot.add(consensus);
1232 if (conservation != null)
1234 annot.add(conservation);
1237 return annot.toArray(new AlignmentAnnotation[0]);
1241 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1243 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1244 for (AlignmentAnnotation a : getAlignmentAnnotation())
1246 if (a.getCalcId() == calcId)
1255 * Returns a list of annotations that match the specified sequenceRef, calcId
1256 * and label, ignoring null values.
1258 * @return list of AlignmentAnnotation objects
1261 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1262 String calcId, String label)
1264 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1265 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1267 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1268 && ann.sequenceRef != null && ann.sequenceRef == seq
1269 && ann.label != null && ann.label.equals(label))
1278 * Answer true if any annotation matches the calcId passed in (if not null).
1283 public boolean hasAnnotation(String calcId)
1285 if (calcId != null && !"".equals(calcId))
1287 for (AlignmentAnnotation a : getAlignmentAnnotation())
1289 if (a.getCalcId() == calcId)
1299 * Remove all sequences from the group (leaving other properties unchanged).
1303 synchronized (sequences)
1309 private AnnotatedCollectionI context;
1312 * set the alignment or group context for this group
1316 public void setContext(AnnotatedCollectionI context)
1318 this.context = context;
1324 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1327 public AnnotatedCollectionI getContext()