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;
24 import java.util.List;
28 import jalview.analysis.*;
29 import jalview.schemes.*;
32 * Collects a set contiguous ranges on a set of sequences
37 public class SequenceGroup implements AnnotatedCollectionI
43 Conservation conserve;
47 boolean displayBoxes = true;
49 boolean displayText = true;
51 boolean colourText = false;
54 * after Olivier's non-conserved only character display
56 boolean showNonconserved = false;
61 private Vector<SequenceI> sequences = new Vector<SequenceI>();
64 * representative sequence for this group (if any)
66 private SequenceI seqrep = null;
71 * Colourscheme applied to group if any
73 public ColourSchemeI cs;
79 public Color outlineColour = Color.black;
81 public Color idColour = null;
83 public int thresholdTextColour = 0;
85 public Color textColour = Color.black;
87 public Color textColour2 = Color.white;
90 * consensus calculation property
92 private boolean ignoreGapsInConsensus = true;
95 * consensus calculation property
97 private boolean showSequenceLogo = false;
100 * flag indicating if logo should be rendered normalised
102 private boolean normaliseSequenceLogo;
105 * @return the includeAllConsSymbols
107 public boolean isShowSequenceLogo()
109 return showSequenceLogo;
113 * Creates a new SequenceGroup object.
115 public SequenceGroup()
117 groupName = "JGroup:" + this.hashCode();
121 * Creates a new SequenceGroup object.
126 * @param displayBoxes
130 * first column of group
132 * last column of group
134 public SequenceGroup(Vector sequences, String groupName,
135 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
136 boolean colourText, int start, int end)
138 this.sequences = sequences;
139 this.groupName = groupName;
140 this.displayBoxes = displayBoxes;
141 this.displayText = displayText;
142 this.colourText = colourText;
146 recalcConservation();
154 public SequenceGroup(SequenceGroup seqsel)
158 sequences = new Vector();
159 Enumeration<SequenceI> sq = seqsel.sequences.elements();
160 while (sq.hasMoreElements())
162 sequences.addElement(sq.nextElement());
165 if (seqsel.groupName != null)
167 groupName = new String(seqsel.groupName);
169 displayBoxes = seqsel.displayBoxes;
170 displayText = seqsel.displayText;
171 colourText = seqsel.colourText;
172 startRes = seqsel.startRes;
173 endRes = seqsel.endRes;
175 if (seqsel.description != null)
176 description = new String(seqsel.description);
177 hidecols = seqsel.hidecols;
178 hidereps = seqsel.hidereps;
179 idColour = seqsel.idColour;
180 outlineColour = seqsel.outlineColour;
181 seqrep = seqsel.seqrep;
182 textColour = seqsel.textColour;
183 textColour2 = seqsel.textColour2;
184 thresholdTextColour = seqsel.thresholdTextColour;
185 width = seqsel.width;
186 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
187 if (seqsel.conserve != null)
189 recalcConservation(); // safer than
190 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
195 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
197 int iSize = sequences.size();
198 SequenceI[] seqs = new SequenceI[iSize];
199 SequenceI[] inorder = getSequencesInOrder(align);
201 for (int i = 0, ipos = 0; i < inorder.length; i++)
203 SequenceI seq = inorder[i];
205 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
206 if (seqs[ipos] != null)
208 seqs[ipos].setDescription(seq.getDescription());
209 seqs[ipos].setDBRef(seq.getDBRef());
210 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
211 if (seq.getDatasetSequence() != null)
213 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
216 if (seq.getAnnotation() != null)
218 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
219 // Only copy annotation that is either a score or referenced by the
220 // alignment's annotation vector
221 for (int a = 0; a < seq.getAnnotation().length; a++)
223 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
226 boolean found = false;
227 for (int pos = 0; pos < alann.length; pos++)
229 if (alann[pos] == tocopy)
238 AlignmentAnnotation newannot = new AlignmentAnnotation(
239 seq.getAnnotation()[a]);
240 newannot.restrict(startRes, endRes);
241 newannot.setSequenceRef(seqs[ipos]);
242 newannot.adjustForAlignment();
243 seqs[ipos].addAlignmentAnnotation(newannot);
253 if (iSize != inorder.length)
255 SequenceI[] nseqs = new SequenceI[iSize];
256 System.arraycopy(seqs, 0, nseqs, 0, iSize);
264 * If sequence ends in gaps, the end residue can be correctly calculated here
270 public int findEndRes(SequenceI seq)
275 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
277 ch = seq.getCharAt(j);
278 if (!jalview.util.Comparison.isGap((ch)))
286 eres += seq.getStart() - 1;
292 public List<SequenceI> getSequences()
297 public List<SequenceI> getSequences(
298 Map<SequenceI, SequenceCollectionI> hiddenReps)
300 if (hiddenReps == null)
306 Vector allSequences = new Vector();
308 for (int i = 0; i < sequences.size(); i++)
310 seq = (SequenceI) sequences.elementAt(i);
311 allSequences.addElement(seq);
312 if (hiddenReps.containsKey(seq))
314 SequenceCollectionI hsg = hiddenReps.get(seq);
315 for (SequenceI seq2 : hsg.getSequences())
317 if (seq2 != seq && !allSequences.contains(seq2))
319 allSequences.addElement(seq2);
329 public SequenceI[] getSequencesAsArray(
330 Map<SequenceI, SequenceCollectionI> map)
332 List<SequenceI> tmp = getSequences(map);
337 return tmp.toArray(new SequenceI[tmp.size()]);
346 * @return DOCUMENT ME!
348 public boolean adjustForRemoveLeft(int col)
350 // return value is true if the group still exists
353 startRes = startRes - col;
358 endRes = endRes - col;
360 if (startRes > endRes)
367 // must delete this group!!
380 * @return DOCUMENT ME!
382 public boolean adjustForRemoveRight(int col)
401 * @return DOCUMENT ME!
403 public String getName()
408 public String getDescription()
419 public void setName(String name)
422 // TODO: URGENT: update dependent objects (annotation row)
425 public void setDescription(String desc)
433 * @return DOCUMENT ME!
435 public Conservation getConservation()
446 public void setConservation(Conservation c)
452 * Add s to this sequence group. If aligment sequence is already contained in
453 * group, it will not be added again, but recalculation may happen if the flag
457 * alignment sequence to be added
459 * true means Group's conservation should be recalculated
461 public void addSequence(SequenceI s, boolean recalc)
463 if (s != null && !sequences.contains(s))
465 sequences.addElement(s);
470 recalcConservation();
475 * Max Gaps Threshold (percent) for performing a conservation calculation
477 private int consPercGaps = 25;
480 * @return Max Gaps Threshold for performing a conservation calculation
482 public int getConsPercGaps()
488 * set Max Gaps Threshold (percent) for performing a conservation calculation
490 * @param consPercGaps
492 public void setConsPercGaps(int consPercGaps)
494 this.consPercGaps = consPercGaps;
498 * calculate residue conservation for group - but only if necessary.
500 public void recalcConservation()
502 if (cs == null && consensus == null && conservation == null)
508 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
509 endRes + 1, showSequenceLogo);
510 if (consensus != null)
512 _updateConsensusRow(cnsns, sequences.size());
516 cs.setConsensus(cnsns);
519 if ((conservation != null)
520 || (cs != null && cs.conservationApplied()))
522 Conservation c = new Conservation(groupName,
523 ResidueProperties.propHash, 3, sequences, startRes,
526 c.verdict(false, consPercGaps);
527 if (conservation != null)
529 _updateConservationRow(c);
533 if (cs.conservationApplied())
535 cs.setConservation(c);
541 cs.alignmentChanged(context != null ? context : this, null);
543 } catch (java.lang.OutOfMemoryError err)
546 System.out.println("Out of memory loading groups: " + err);
551 private void _updateConservationRow(Conservation c)
553 if (conservation == null)
558 conservation.label = "Conservation for " + getName();
559 conservation.description = "Conservation for group " + getName()
560 + " less than " + consPercGaps + "% gaps";
561 // preserve width if already set
562 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
565 conservation.annotations = null;
566 conservation.annotations = new Annotation[aWidth]; // should be alignment
568 c.completeAnnotations(conservation, null, startRes, endRes + 1);
571 public Hashtable[] consensusData = null;
573 private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
575 if (consensus == null)
579 consensus.label = "Consensus for " + getName();
580 consensus.description = "Percent Identity";
581 consensusData = cnsns;
582 // preserve width if already set
583 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
586 consensus.annotations = null;
587 consensus.annotations = new Annotation[aWidth]; // should be alignment width
589 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
590 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
593 // ignoreGapsInConsensusCalculation);
598 * sequence to either add or remove from group
600 * flag passed to delete/addSequence to indicate if group properties
601 * should be recalculated
603 public void addOrRemove(SequenceI s, boolean recalc)
605 if (sequences.contains(s))
607 deleteSequence(s, recalc);
611 addSequence(s, recalc);
623 public void deleteSequence(SequenceI s, boolean recalc)
625 sequences.removeElement(s);
629 recalcConservation();
636 * @return the first column selected by this group. Runs from 0<=i<N_cols
638 public int getStartRes()
645 * @return the groups last selected column. Runs from 0<=i<N_cols
647 public int getEndRes()
653 * Set the first column selected by this group. Runs from 0<=i<N_cols
657 public void setStartRes(int i)
663 * Set the groups last selected column. Runs from 0<=i<N_cols
667 public void setEndRes(int i)
675 * @return DOCUMENT ME!
679 return sequences.size();
688 * @return DOCUMENT ME!
690 public SequenceI getSequenceAt(int i)
692 return (SequenceI) sequences.elementAt(i);
701 public void setColourText(boolean state)
709 * @return DOCUMENT ME!
711 public boolean getColourText()
722 public void setDisplayText(boolean state)
730 * @return DOCUMENT ME!
732 public boolean getDisplayText()
743 public void setDisplayBoxes(boolean state)
745 displayBoxes = state;
751 * @return DOCUMENT ME!
753 public boolean getDisplayBoxes()
761 * @return DOCUMENT ME!
763 public int getWidth()
765 // MC This needs to get reset when characters are inserted and deleted
766 if (sequences.size() > 0)
768 width = ((SequenceI) sequences.elementAt(0)).getLength();
771 for (int i = 1; i < sequences.size(); i++)
773 SequenceI seq = (SequenceI) sequences.elementAt(i);
775 if (seq.getLength() > width)
777 width = seq.getLength();
790 public void setOutlineColour(Color c)
798 * @return DOCUMENT ME!
800 public Color getOutlineColour()
802 return outlineColour;
807 * returns the sequences in the group ordered by the ordering given by al.
808 * this used to return an array with null entries regardless, new behaviour is
809 * below. TODO: verify that this does not affect use in applet or application
813 * @return SequenceI[] intersection of sequences in group with al, ordered by
814 * al, or null if group does not intersect with al
816 public SequenceI[] getSequencesInOrder(AlignmentI al)
818 return getSequencesInOrder(al, true);
822 * return an array representing the intersection of the group with al,
823 * optionally returning an array the size of al.getHeight() where nulls mark
824 * the non-intersected sequences
828 * @return null or array
830 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
832 int sSize = sequences.size();
833 int alHeight = al.getHeight();
835 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
838 for (int i = 0; i < alHeight && index < sSize; i++)
840 if (sequences.contains(al.getSequenceAt(i)))
842 seqs[(trim) ? index : i] = al.getSequenceAt(i);
854 if (index < seqs.length)
856 SequenceI[] dummy = seqs;
857 seqs = new SequenceI[index];
860 seqs[index] = dummy[index];
868 * @return the idColour
870 public Color getIdColour()
877 * the idColour to set
879 public void setIdColour(Color idColour)
881 this.idColour = idColour;
885 * @return the representative sequence for this group
887 public SequenceI getSeqrep()
893 * set the representative sequence for this group. Note - this affects the
894 * interpretation of the Hidereps attribute.
897 * the seqrep to set (null means no sequence representative)
899 public void setSeqrep(SequenceI seqrep)
901 this.seqrep = seqrep;
906 * @return true if group has a sequence representative
908 public boolean hasSeqrep()
910 return seqrep != null;
914 * visibility of rows or represented rows covered by group
916 private boolean hidereps = false;
919 * set visibility of sequences covered by (if no sequence representative is
920 * defined) or represented by this group.
924 public void setHidereps(boolean visibility)
926 hidereps = visibility;
931 * @return true if sequences represented (or covered) by this group should be
934 public boolean isHidereps()
940 * visibility of columns intersecting this group
942 private boolean hidecols = false;
945 * set intended visibility of columns covered by this group
949 public void setHideCols(boolean visibility)
951 hidecols = visibility;
956 * @return true if columns covered by group should be hidden
958 public boolean isHideCols()
964 * create a new sequence group from the intersection of this group with an
965 * alignment Hashtable of hidden representatives
971 * @return new group containing sequences common to this group and alignment
973 public SequenceGroup intersect(AlignmentI alignment,
974 Map<SequenceI, SequenceCollectionI> map)
976 SequenceGroup sgroup = new SequenceGroup(this);
977 SequenceI[] insect = getSequencesInOrder(alignment);
978 sgroup.sequences = new Vector();
979 for (int s = 0; insect != null && s < insect.length; s++)
981 if (map == null || map.containsKey(insect[s]))
983 sgroup.sequences.addElement(insect[s]);
986 // Enumeration en =getSequences(hashtable).elements();
987 // while (en.hasMoreElements())
989 // SequenceI elem = (SequenceI) en.nextElement();
990 // if (alignment.getSequences().contains(elem))
992 // sgroup.addSequence(elem, false);
999 * @return the showUnconserved
1001 public boolean getShowNonconserved()
1003 return showNonconserved;
1007 * @param showNonconserved
1008 * the showUnconserved to set
1010 public void setShowNonconserved(boolean displayNonconserved)
1012 this.showNonconserved = displayNonconserved;
1015 AlignmentAnnotation consensus = null, conservation = null;
1018 * flag indicating if consensus histogram should be rendered
1020 private boolean showConsensusHistogram;
1023 * set this alignmentAnnotation object as the one used to render consensus
1028 public void setConsensus(AlignmentAnnotation aan)
1030 if (consensus == null)
1038 * @return automatically calculated consensus row
1040 public AlignmentAnnotation getConsensus()
1042 // TODO get or calculate and get consensus annotation row for this group
1043 int aWidth = this.getWidth();
1051 if (consensus == null)
1053 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1054 100f, AlignmentAnnotation.BAR_GRAPH);
1055 consensus.hasText = true;
1056 consensus.autoCalculated = true;
1057 consensus.groupRef = this;
1058 consensus.label = "Consensus for " + getName();
1059 consensus.description = "Percent Identity";
1065 * set this alignmentAnnotation object as the one used to render consensus
1070 public void setConservationRow(AlignmentAnnotation aan)
1072 if (conservation == null)
1079 * get the conservation annotation row for this group
1081 * @return autoCalculated annotation row
1083 public AlignmentAnnotation getConservationRow()
1085 if (conservation == null)
1087 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1088 11f, AlignmentAnnotation.BAR_GRAPH);
1091 conservation.hasText = true;
1092 conservation.autoCalculated = true;
1093 conservation.groupRef = this;
1094 conservation.label = "Conservation for " + getName();
1095 conservation.description = "Conservation for group " + getName()
1096 + " less than " + consPercGaps + "% gaps";
1097 return conservation;
1102 * @return true if annotation rows have been instantiated for this group
1104 public boolean hasAnnotationRows()
1106 return consensus != null || conservation != null;
1109 public SequenceI getConsensusSeq()
1112 StringBuffer seqs = new StringBuffer();
1113 for (int i = 0; i < consensus.annotations.length; i++)
1115 if (consensus.annotations[i] != null)
1117 if (consensus.annotations[i].description.charAt(0) == '[')
1119 seqs.append(consensus.annotations[i].description.charAt(1));
1123 seqs.append(consensus.annotations[i].displayCharacter);
1128 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1130 sq.setDescription("Percentage Identity Consensus "
1131 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1135 public void setIgnoreGapsConsensus(boolean state)
1137 if (this.ignoreGapsInConsensus != state && consensus != null)
1139 ignoreGapsInConsensus = state;
1140 recalcConservation();
1142 ignoreGapsInConsensus = state;
1145 public boolean getIgnoreGapsConsensus()
1147 return ignoreGapsInConsensus;
1151 * @param showSequenceLogo
1152 * indicates if a sequence logo is shown for consensus annotation
1154 public void setshowSequenceLogo(boolean showSequenceLogo)
1156 // TODO: decouple calculation from settings update
1157 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1159 this.showSequenceLogo = showSequenceLogo;
1160 recalcConservation();
1162 this.showSequenceLogo = showSequenceLogo;
1167 * @param showConsHist
1168 * flag indicating if the consensus histogram for this group should
1171 public void setShowConsensusHistogram(boolean showConsHist)
1174 if (showConsensusHistogram != showConsHist && consensus != null)
1176 this.showConsensusHistogram = showConsHist;
1177 recalcConservation();
1179 this.showConsensusHistogram = showConsHist;
1183 * @return the showConsensusHistogram
1185 public boolean isShowConsensusHistogram()
1187 return showConsensusHistogram;
1191 * set flag indicating if logo should be normalised when rendered
1195 public void setNormaliseSequenceLogo(boolean norm)
1197 normaliseSequenceLogo = norm;
1200 public boolean isNormaliseSequenceLogo()
1202 return normaliseSequenceLogo;
1207 * returns a new array with all annotation involving this group
1209 public AlignmentAnnotation[] getAlignmentAnnotation()
1211 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1213 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1214 for (SequenceI seq : (Vector<SequenceI>) 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);
1236 return annot.toArray(new AlignmentAnnotation[0]);
1240 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1242 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1243 for (AlignmentAnnotation a : getAlignmentAnnotation())
1245 if (a.getCalcId() == calcId)
1258 private AnnotatedCollectionI context;
1261 * set the alignment or group context for this group
1265 public void setContext(AnnotatedCollectionI context)
1267 this.context = context;
1273 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1276 public AnnotatedCollectionI getContext()