2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.datamodel;
22 import java.util.List;
26 import jalview.analysis.*;
27 import jalview.schemes.*;
30 * Collects a set contiguous ranges on a set of sequences
35 public class SequenceGroup implements AnnotatedCollectionI
41 Conservation conserve;
45 boolean displayBoxes = true;
47 boolean displayText = true;
49 boolean colourText = false;
52 * after Olivier's non-conserved only character display
54 boolean showNonconserved = false;
59 private Vector<SequenceI> sequences = new Vector<SequenceI>();
62 * representative sequence for this group (if any)
64 private SequenceI seqrep = null;
69 * Colourscheme applied to group if any
71 public ColourSchemeI cs;
77 public Color outlineColour = Color.black;
79 public Color idColour = null;
81 public int thresholdTextColour = 0;
83 public Color textColour = Color.black;
85 public Color textColour2 = Color.white;
88 * consensus calculation property
90 private boolean ignoreGapsInConsensus = true;
93 * consensus calculation property
95 private boolean showSequenceLogo = false;
98 * flag indicating if logo should be rendered normalised
100 private boolean normaliseSequenceLogo;
103 * @return the includeAllConsSymbols
105 public boolean isShowSequenceLogo()
107 return showSequenceLogo;
111 * Creates a new SequenceGroup object.
113 public SequenceGroup()
115 groupName = "JGroup:" + this.hashCode();
119 * Creates a new SequenceGroup object.
124 * @param displayBoxes
128 * first column of group
130 * last column of group
132 public SequenceGroup(Vector sequences, String groupName,
133 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
134 boolean colourText, int start, int end)
136 this.sequences = sequences;
137 this.groupName = groupName;
138 this.displayBoxes = displayBoxes;
139 this.displayText = displayText;
140 this.colourText = colourText;
144 recalcConservation();
152 public SequenceGroup(SequenceGroup seqsel)
156 sequences = new Vector();
157 Enumeration<SequenceI> sq = seqsel.sequences.elements();
158 while (sq.hasMoreElements())
160 sequences.addElement(sq.nextElement());
163 if (seqsel.groupName != null)
165 groupName = new String(seqsel.groupName);
167 displayBoxes = seqsel.displayBoxes;
168 displayText = seqsel.displayText;
169 colourText = seqsel.colourText;
170 startRes = seqsel.startRes;
171 endRes = seqsel.endRes;
173 if (seqsel.description != null)
174 description = new String(seqsel.description);
175 hidecols = seqsel.hidecols;
176 hidereps = seqsel.hidereps;
177 idColour = seqsel.idColour;
178 outlineColour = seqsel.outlineColour;
179 seqrep = seqsel.seqrep;
180 textColour = seqsel.textColour;
181 textColour2 = seqsel.textColour2;
182 thresholdTextColour = seqsel.thresholdTextColour;
183 width = seqsel.width;
184 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
185 if (seqsel.conserve != null)
187 recalcConservation(); // safer than
188 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
193 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
195 int iSize = sequences.size();
196 SequenceI[] seqs = new SequenceI[iSize];
197 SequenceI[] inorder = getSequencesInOrder(align);
199 for (int i = 0, ipos = 0; i < inorder.length; i++)
201 SequenceI seq = inorder[i];
203 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
204 if (seqs[ipos] != null)
206 seqs[ipos].setDescription(seq.getDescription());
207 seqs[ipos].setDBRef(seq.getDBRef());
208 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
209 if (seq.getDatasetSequence() != null)
211 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
214 if (seq.getAnnotation() != null)
216 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
217 // Only copy annotation that is either a score or referenced by the
218 // alignment's annotation vector
219 for (int a = 0; a < seq.getAnnotation().length; a++)
221 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
224 boolean found = false;
225 for (int pos = 0; pos < alann.length; pos++)
227 if (alann[pos] == tocopy)
236 AlignmentAnnotation newannot = new AlignmentAnnotation(
237 seq.getAnnotation()[a]);
238 newannot.restrict(startRes, endRes);
239 newannot.setSequenceRef(seqs[ipos]);
240 newannot.adjustForAlignment();
241 seqs[ipos].addAlignmentAnnotation(newannot);
251 if (iSize != inorder.length)
253 SequenceI[] nseqs = new SequenceI[iSize];
254 System.arraycopy(seqs, 0, nseqs, 0, iSize);
262 * If sequence ends in gaps, the end residue can be correctly calculated here
268 public int findEndRes(SequenceI seq)
273 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
275 ch = seq.getCharAt(j);
276 if (!jalview.util.Comparison.isGap((ch)))
284 eres += seq.getStart() - 1;
290 public List<SequenceI> getSequences()
295 public List<SequenceI> getSequences(
296 Map<SequenceI, SequenceCollectionI> hiddenReps)
298 if (hiddenReps == null)
304 Vector allSequences = new Vector();
306 for (int i = 0; i < sequences.size(); i++)
308 seq = (SequenceI) sequences.elementAt(i);
309 allSequences.addElement(seq);
310 if (hiddenReps.containsKey(seq))
312 SequenceCollectionI hsg = hiddenReps.get(seq);
313 for (SequenceI seq2 : hsg.getSequences())
315 if (seq2 != seq && !allSequences.contains(seq2))
317 allSequences.addElement(seq2);
327 public SequenceI[] getSequencesAsArray(
328 Map<SequenceI, SequenceCollectionI> map)
330 List<SequenceI> tmp = getSequences(map);
335 return tmp.toArray(new SequenceI[tmp.size()]);
344 * @return DOCUMENT ME!
346 public boolean adjustForRemoveLeft(int col)
348 // return value is true if the group still exists
351 startRes = startRes - col;
356 endRes = endRes - col;
358 if (startRes > endRes)
365 // must delete this group!!
378 * @return DOCUMENT ME!
380 public boolean adjustForRemoveRight(int col)
399 * @return DOCUMENT ME!
401 public String getName()
406 public String getDescription()
417 public void setName(String name)
420 // TODO: URGENT: update dependent objects (annotation row)
423 public void setDescription(String desc)
431 * @return DOCUMENT ME!
433 public Conservation getConservation()
444 public void setConservation(Conservation c)
450 * Add s to this sequence group. If aligment sequence is already contained in
451 * group, it will not be added again, but recalculation may happen if the flag
455 * alignment sequence to be added
457 * true means Group's conservation should be recalculated
459 public void addSequence(SequenceI s, boolean recalc)
461 if (s != null && !sequences.contains(s))
463 sequences.addElement(s);
468 recalcConservation();
473 * Max Gaps Threshold (percent) for performing a conservation calculation
475 private int consPercGaps = 25;
478 * @return Max Gaps Threshold for performing a conservation calculation
480 public int getConsPercGaps()
486 * set Max Gaps Threshold (percent) for performing a conservation calculation
487 * @param consPercGaps
489 public void setConsPercGaps(int consPercGaps)
491 this.consPercGaps = consPercGaps;
495 * calculate residue conservation for group - but only if necessary.
497 public void recalcConservation()
499 if (cs == null && consensus == null && conservation == null)
505 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
506 endRes + 1, showSequenceLogo);
507 if (consensus != null)
509 _updateConsensusRow(cnsns, sequences.size());
513 cs.setConsensus(cnsns);
516 if ((conservation != null)
517 || (cs != null && cs.conservationApplied()))
519 Conservation c = new Conservation(groupName,
520 ResidueProperties.propHash, 3, sequences, startRes,
523 c.verdict(false, consPercGaps);
524 if (conservation != null)
526 _updateConservationRow(c);
530 if (cs.conservationApplied())
532 cs.setConservation(c);
538 cs.alignmentChanged(context!=null ? context : this, null);
540 } catch (java.lang.OutOfMemoryError err)
543 System.out.println("Out of memory loading groups: " + err);
548 private void _updateConservationRow(Conservation c)
550 if (conservation == null)
555 conservation.label = "Conservation for " + getName();
556 conservation.description = "Conservation for group " + getName()
557 + " less than " + consPercGaps + "% gaps";
558 // preserve width if already set
559 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
562 conservation.annotations = null;
563 conservation.annotations = new Annotation[aWidth]; // should be alignment
565 c.completeAnnotations(conservation, null, startRes, endRes + 1);
568 public Hashtable[] consensusData = null;
570 private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
572 if (consensus == null)
576 consensus.label = "Consensus for " + getName();
577 consensus.description = "Percent Identity";
578 consensusData = cnsns;
579 // preserve width if already set
580 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
583 consensus.annotations = null;
584 consensus.annotations = new Annotation[aWidth]; // should be alignment width
586 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
587 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting container
589 // ignoreGapsInConsensusCalculation);
594 * sequence to either add or remove from group
596 * flag passed to delete/addSequence to indicate if group properties
597 * should be recalculated
599 public void addOrRemove(SequenceI s, boolean recalc)
601 if (sequences.contains(s))
603 deleteSequence(s, recalc);
607 addSequence(s, recalc);
619 public void deleteSequence(SequenceI s, boolean recalc)
621 sequences.removeElement(s);
625 recalcConservation();
632 * @return the first column selected by this group. Runs from 0<=i<N_cols
634 public int getStartRes()
641 * @return the groups last selected column. Runs from 0<=i<N_cols
643 public int getEndRes()
649 * Set the first column selected by this group. Runs from 0<=i<N_cols
653 public void setStartRes(int i)
659 * Set the groups last selected column. Runs from 0<=i<N_cols
663 public void setEndRes(int i)
671 * @return DOCUMENT ME!
675 return sequences.size();
684 * @return DOCUMENT ME!
686 public SequenceI getSequenceAt(int i)
688 return (SequenceI) sequences.elementAt(i);
697 public void setColourText(boolean state)
705 * @return DOCUMENT ME!
707 public boolean getColourText()
718 public void setDisplayText(boolean state)
726 * @return DOCUMENT ME!
728 public boolean getDisplayText()
739 public void setDisplayBoxes(boolean state)
741 displayBoxes = state;
747 * @return DOCUMENT ME!
749 public boolean getDisplayBoxes()
757 * @return DOCUMENT ME!
759 public int getWidth()
761 // MC This needs to get reset when characters are inserted and deleted
762 if (sequences.size() > 0)
764 width = ((SequenceI) sequences.elementAt(0)).getLength();
767 for (int i = 1; i < sequences.size(); i++)
769 SequenceI seq = (SequenceI) sequences.elementAt(i);
771 if (seq.getLength() > width)
773 width = seq.getLength();
786 public void setOutlineColour(Color c)
794 * @return DOCUMENT ME!
796 public Color getOutlineColour()
798 return outlineColour;
803 * returns the sequences in the group ordered by the ordering given by al.
804 * this used to return an array with null entries regardless, new behaviour is
805 * below. TODO: verify that this does not affect use in applet or application
809 * @return SequenceI[] intersection of sequences in group with al, ordered by
810 * al, or null if group does not intersect with al
812 public SequenceI[] getSequencesInOrder(AlignmentI al)
814 return getSequencesInOrder(al, true);
818 * return an array representing the intersection of the group with al,
819 * optionally returning an array the size of al.getHeight() where nulls mark
820 * the non-intersected sequences
824 * @return null or array
826 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
828 int sSize = sequences.size();
829 int alHeight = al.getHeight();
831 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
834 for (int i = 0; i < alHeight && index < sSize; i++)
836 if (sequences.contains(al.getSequenceAt(i)))
838 seqs[(trim) ? index : i] = al.getSequenceAt(i);
850 if (index < seqs.length)
852 SequenceI[] dummy = seqs;
853 seqs = new SequenceI[index];
856 seqs[index] = dummy[index];
864 * @return the idColour
866 public Color getIdColour()
873 * the idColour to set
875 public void setIdColour(Color idColour)
877 this.idColour = idColour;
881 * @return the representative sequence for this group
883 public SequenceI getSeqrep()
889 * set the representative sequence for this group. Note - this affects the
890 * interpretation of the Hidereps attribute.
893 * the seqrep to set (null means no sequence representative)
895 public void setSeqrep(SequenceI seqrep)
897 this.seqrep = seqrep;
902 * @return true if group has a sequence representative
904 public boolean hasSeqrep()
906 return seqrep != null;
910 * visibility of rows or represented rows covered by group
912 private boolean hidereps = false;
915 * set visibility of sequences covered by (if no sequence representative is
916 * defined) or represented by this group.
920 public void setHidereps(boolean visibility)
922 hidereps = visibility;
927 * @return true if sequences represented (or covered) by this group should be
930 public boolean isHidereps()
936 * visibility of columns intersecting this group
938 private boolean hidecols = false;
941 * set intended visibility of columns covered by this group
945 public void setHideCols(boolean visibility)
947 hidecols = visibility;
952 * @return true if columns covered by group should be hidden
954 public boolean isHideCols()
960 * create a new sequence group from the intersection of this group with an
961 * alignment Hashtable of hidden representatives
967 * @return new group containing sequences common to this group and alignment
969 public SequenceGroup intersect(AlignmentI alignment,
970 Map<SequenceI, SequenceCollectionI> map)
972 SequenceGroup sgroup = new SequenceGroup(this);
973 SequenceI[] insect = getSequencesInOrder(alignment);
974 sgroup.sequences = new Vector();
975 for (int s = 0; insect != null && s < insect.length; s++)
977 if (map == null || map.containsKey(insect[s]))
979 sgroup.sequences.addElement(insect[s]);
982 // Enumeration en =getSequences(hashtable).elements();
983 // while (en.hasMoreElements())
985 // SequenceI elem = (SequenceI) en.nextElement();
986 // if (alignment.getSequences().contains(elem))
988 // sgroup.addSequence(elem, false);
995 * @return the showUnconserved
997 public boolean getShowNonconserved()
999 return showNonconserved;
1003 * @param showNonconserved
1004 * the showUnconserved to set
1006 public void setShowNonconserved(boolean displayNonconserved)
1008 this.showNonconserved = displayNonconserved;
1011 AlignmentAnnotation consensus = null, conservation = null;
1014 * flag indicating if consensus histogram should be rendered
1016 private boolean showConsensusHistogram;
1019 * set this alignmentAnnotation object as the one used to render consensus
1024 public void setConsensus(AlignmentAnnotation aan)
1026 if (consensus == null)
1034 * @return automatically calculated consensus row
1036 public AlignmentAnnotation getConsensus()
1038 // TODO get or calculate and get consensus annotation row for this group
1039 int aWidth = this.getWidth();
1047 if (consensus == null)
1049 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1050 100f, AlignmentAnnotation.BAR_GRAPH);
1051 consensus.hasText = true;
1052 consensus.autoCalculated = true;
1053 consensus.groupRef = this;
1054 consensus.label = "Consensus for " + getName();
1055 consensus.description = "Percent Identity";
1061 * set this alignmentAnnotation object as the one used to render consensus
1066 public void setConservationRow(AlignmentAnnotation aan)
1068 if (conservation == null)
1075 * get the conservation annotation row for this group
1077 * @return autoCalculated annotation row
1079 public AlignmentAnnotation getConservationRow()
1081 if (conservation == null)
1083 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1084 11f, AlignmentAnnotation.BAR_GRAPH);
1087 conservation.hasText = true;
1088 conservation.autoCalculated = true;
1089 conservation.groupRef = this;
1090 conservation.label = "Conservation for " + getName();
1091 conservation.description = "Conservation for group " + getName()
1092 + " less than " + consPercGaps + "% gaps";
1093 return conservation;
1098 * @return true if annotation rows have been instantiated for this group
1100 public boolean hasAnnotationRows()
1102 return consensus != null || conservation != null;
1105 public SequenceI getConsensusSeq()
1108 StringBuffer seqs = new StringBuffer();
1109 for (int i = 0; i < consensus.annotations.length; i++)
1111 if (consensus.annotations[i] != null)
1113 if (consensus.annotations[i].description.charAt(0) == '[')
1115 seqs.append(consensus.annotations[i].description.charAt(1));
1119 seqs.append(consensus.annotations[i].displayCharacter);
1124 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1126 sq.setDescription("Percentage Identity Consensus "
1127 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1131 public void setIgnoreGapsConsensus(boolean state)
1133 if (this.ignoreGapsInConsensus != state && consensus != null)
1135 ignoreGapsInConsensus = state;
1136 recalcConservation();
1138 ignoreGapsInConsensus = state;
1141 public boolean getIgnoreGapsConsensus()
1143 return ignoreGapsInConsensus;
1147 * @param showSequenceLogo
1148 * indicates if a sequence logo is shown for consensus annotation
1150 public void setshowSequenceLogo(boolean showSequenceLogo)
1152 // TODO: decouple calculation from settings update
1153 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1155 this.showSequenceLogo = showSequenceLogo;
1156 recalcConservation();
1158 this.showSequenceLogo = showSequenceLogo;
1163 * @param showConsHist
1164 * flag indicating if the consensus histogram for this group should
1167 public void setShowConsensusHistogram(boolean showConsHist)
1170 if (showConsensusHistogram != showConsHist && consensus != null)
1172 this.showConsensusHistogram = showConsHist;
1173 recalcConservation();
1175 this.showConsensusHistogram = showConsHist;
1179 * @return the showConsensusHistogram
1181 public boolean isShowConsensusHistogram()
1183 return showConsensusHistogram;
1187 * set flag indicating if logo should be normalised when rendered
1191 public void setNormaliseSequenceLogo(boolean norm)
1193 normaliseSequenceLogo = norm;
1196 public boolean isNormaliseSequenceLogo()
1198 return normaliseSequenceLogo;
1203 * returns a new array with all annotation involving this group
1205 public AlignmentAnnotation[] getAlignmentAnnotation()
1207 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1209 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1210 for (SequenceI seq : (Vector<SequenceI>) sequences)
1212 AlignmentAnnotation[] aa = seq.getAnnotation();
1215 for (AlignmentAnnotation al : aa)
1217 if (al.groupRef == this)
1224 if (consensus != null)
1226 annot.add(consensus);
1228 if (conservation != null)
1230 annot.add(conservation);
1232 return annot.toArray(new AlignmentAnnotation[0]);
1236 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1238 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1239 for (AlignmentAnnotation a : getAlignmentAnnotation())
1241 if (a.getCalcId() == calcId)
1253 private AnnotatedCollectionI context;
1255 * set the alignment or group context for this group
1258 public void setContext(AnnotatedCollectionI context)
1260 this.context = context;
1263 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1266 public AnnotatedCollectionI getContext()