2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
18 package jalview.datamodel;
21 import java.util.List;
25 import jalview.analysis.*;
26 import jalview.schemes.*;
29 * Collects a set contiguous ranges on a set of sequences
34 public class SequenceGroup implements AnnotatedCollectionI
40 Conservation conserve;
44 boolean displayBoxes = true;
46 boolean displayText = true;
48 boolean colourText = false;
51 * after Olivier's non-conserved only character display
53 boolean showNonconserved = false;
58 private Vector<SequenceI> sequences = new Vector<SequenceI>();
61 * representative sequence for this group (if any)
63 private SequenceI seqrep = null;
68 * Colourscheme applied to group if any
70 public ColourSchemeI cs;
76 public Color outlineColour = Color.black;
78 public Color idColour = null;
80 public int thresholdTextColour = 0;
82 public Color textColour = Color.black;
84 public Color textColour2 = Color.white;
87 * consensus calculation property
89 private boolean ignoreGapsInConsensus = true;
92 * consensus calculation property
94 private boolean showSequenceLogo = false;
97 * flag indicating if logo should be rendered normalised
99 private boolean normaliseSequenceLogo;
102 * @return the includeAllConsSymbols
104 public boolean isShowSequenceLogo()
106 return showSequenceLogo;
110 * Creates a new SequenceGroup object.
112 public SequenceGroup()
114 groupName = "JGroup:" + this.hashCode();
118 * Creates a new SequenceGroup object.
123 * @param displayBoxes
127 * first column of group
129 * last column of group
131 public SequenceGroup(Vector sequences, String groupName,
132 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
133 boolean colourText, int start, int end)
135 this.sequences = sequences;
136 this.groupName = groupName;
137 this.displayBoxes = displayBoxes;
138 this.displayText = displayText;
139 this.colourText = colourText;
143 recalcConservation();
151 public SequenceGroup(SequenceGroup seqsel)
155 sequences = new Vector();
156 Enumeration<SequenceI> sq = seqsel.sequences.elements();
157 while (sq.hasMoreElements())
159 sequences.addElement(sq.nextElement());
162 if (seqsel.groupName != null)
164 groupName = new String(seqsel.groupName);
166 displayBoxes = seqsel.displayBoxes;
167 displayText = seqsel.displayText;
168 colourText = seqsel.colourText;
169 startRes = seqsel.startRes;
170 endRes = seqsel.endRes;
172 if (seqsel.description != null)
173 description = new String(seqsel.description);
174 hidecols = seqsel.hidecols;
175 hidereps = seqsel.hidereps;
176 idColour = seqsel.idColour;
177 outlineColour = seqsel.outlineColour;
178 seqrep = seqsel.seqrep;
179 textColour = seqsel.textColour;
180 textColour2 = seqsel.textColour2;
181 thresholdTextColour = seqsel.thresholdTextColour;
182 width = seqsel.width;
183 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
184 if (seqsel.conserve != null)
186 recalcConservation(); // safer than
187 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
192 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
194 int iSize = sequences.size();
195 SequenceI[] seqs = new SequenceI[iSize];
196 SequenceI[] inorder = getSequencesInOrder(align);
198 for (int i = 0, ipos = 0; i < inorder.length; i++)
200 SequenceI seq = inorder[i];
202 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
203 if (seqs[ipos] != null)
205 seqs[ipos].setDescription(seq.getDescription());
206 seqs[ipos].setDBRef(seq.getDBRef());
207 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
208 if (seq.getDatasetSequence() != null)
210 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
213 if (seq.getAnnotation() != null)
215 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
216 // Only copy annotation that is either a score or referenced by the
217 // alignment's annotation vector
218 for (int a = 0; a < seq.getAnnotation().length; a++)
220 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
223 boolean found = false;
224 for (int pos = 0; pos < alann.length; pos++)
226 if (alann[pos] == tocopy)
235 AlignmentAnnotation newannot = new AlignmentAnnotation(
236 seq.getAnnotation()[a]);
237 newannot.restrict(startRes, endRes);
238 newannot.setSequenceRef(seqs[ipos]);
239 newannot.adjustForAlignment();
240 seqs[ipos].addAlignmentAnnotation(newannot);
250 if (iSize != inorder.length)
252 SequenceI[] nseqs = new SequenceI[iSize];
253 System.arraycopy(seqs, 0, nseqs, 0, iSize);
261 * If sequence ends in gaps, the end residue can be correctly calculated here
267 public int findEndRes(SequenceI seq)
272 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
274 ch = seq.getCharAt(j);
275 if (!jalview.util.Comparison.isGap((ch)))
283 eres += seq.getStart() - 1;
289 public List<SequenceI> getSequences()
294 public List<SequenceI> getSequences(
295 Map<SequenceI, SequenceCollectionI> hiddenReps)
297 if (hiddenReps == null)
303 Vector allSequences = new Vector();
305 for (int i = 0; i < sequences.size(); i++)
307 seq = (SequenceI) sequences.elementAt(i);
308 allSequences.addElement(seq);
309 if (hiddenReps.containsKey(seq))
311 SequenceCollectionI hsg = hiddenReps.get(seq);
312 for (SequenceI seq2 : hsg.getSequences())
314 if (seq2 != seq && !allSequences.contains(seq2))
316 allSequences.addElement(seq2);
326 public SequenceI[] getSequencesAsArray(
327 Map<SequenceI, SequenceCollectionI> map)
329 List<SequenceI> tmp = getSequences(map);
334 return tmp.toArray(new SequenceI[tmp.size()]);
343 * @return DOCUMENT ME!
345 public boolean adjustForRemoveLeft(int col)
347 // return value is true if the group still exists
350 startRes = startRes - col;
355 endRes = endRes - col;
357 if (startRes > endRes)
364 // must delete this group!!
377 * @return DOCUMENT ME!
379 public boolean adjustForRemoveRight(int col)
398 * @return DOCUMENT ME!
400 public String getName()
405 public String getDescription()
416 public void setName(String name)
419 // TODO: URGENT: update dependent objects (annotation row)
422 public void setDescription(String desc)
430 * @return DOCUMENT ME!
432 public Conservation getConservation()
443 public void setConservation(Conservation c)
449 * Add s to this sequence group. If aligment sequence is already contained in
450 * group, it will not be added again, but recalculation may happen if the flag
454 * alignment sequence to be added
456 * true means Group's conservation should be recalculated
458 public void addSequence(SequenceI s, boolean recalc)
460 if (s != null && !sequences.contains(s))
462 sequences.addElement(s);
467 recalcConservation();
472 * Max Gaps Threshold for performing a conservation calculation TODO: make
473 * this a configurable property - or global to an alignment view
475 private int consPercGaps = 25;
478 * calculate residue conservation for group - but only if necessary.
480 public void recalcConservation()
482 if (cs == null && consensus == null && conservation == null)
488 cs.alignmentChanged(this, null);
492 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
493 endRes + 1, showSequenceLogo);
494 if (consensus != null)
496 _updateConsensusRow(cnsns);
500 cs.setConsensus(cnsns);
501 cs.alignmentChanged(this, null);
504 if ((conservation != null)
505 || (cs != null && cs.conservationApplied()))
507 Conservation c = new Conservation(groupName,
508 ResidueProperties.propHash, 3, sequences, startRes,
511 c.verdict(false, consPercGaps);
512 if (conservation != null)
514 _updateConservationRow(c);
518 if (cs.conservationApplied())
520 cs.setConservation(c);
521 cs.alignmentChanged(this, null);
525 } catch (java.lang.OutOfMemoryError err)
528 System.out.println("Out of memory loading groups: " + err);
533 private void _updateConservationRow(Conservation c)
535 if (conservation == null)
540 conservation.label = "Conservation for " + getName();
541 conservation.description = "Conservation for group " + getName()
542 + " less than " + consPercGaps + "% gaps";
543 // preserve width if already set
544 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
547 conservation.annotations = null;
548 conservation.annotations = new Annotation[aWidth]; // should be alignment
550 c.completeAnnotations(conservation, null, startRes, endRes + 1);
553 public Hashtable[] consensusData = null;
555 private void _updateConsensusRow(Hashtable[] cnsns)
557 if (consensus == null)
561 consensus.label = "Consensus for " + getName();
562 consensus.description = "Percent Identity";
563 consensusData = cnsns;
564 // preserve width if already set
565 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
568 consensus.annotations = null;
569 consensus.annotations = new Annotation[aWidth]; // should be alignment width
571 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
572 ignoreGapsInConsensus, showSequenceLogo); // TODO: setting container
574 // ignoreGapsInConsensusCalculation);
579 * sequence to either add or remove from group
581 * flag passed to delete/addSequence to indicate if group properties
582 * should be recalculated
584 public void addOrRemove(SequenceI s, boolean recalc)
586 if (sequences.contains(s))
588 deleteSequence(s, recalc);
592 addSequence(s, recalc);
604 public void deleteSequence(SequenceI s, boolean recalc)
606 sequences.removeElement(s);
610 recalcConservation();
617 * @return DOCUMENT ME!
619 public int getStartRes()
627 * @return DOCUMENT ME!
629 public int getEndRes()
635 * Set the first column selected by this group. Runs from 0<=i<N_cols
639 public void setStartRes(int i)
645 * Set the groups last selected column. Runs from 0<=i<N_cols
649 public void setEndRes(int i)
657 * @return DOCUMENT ME!
661 return sequences.size();
670 * @return DOCUMENT ME!
672 public SequenceI getSequenceAt(int i)
674 return (SequenceI) sequences.elementAt(i);
683 public void setColourText(boolean state)
691 * @return DOCUMENT ME!
693 public boolean getColourText()
704 public void setDisplayText(boolean state)
712 * @return DOCUMENT ME!
714 public boolean getDisplayText()
725 public void setDisplayBoxes(boolean state)
727 displayBoxes = state;
733 * @return DOCUMENT ME!
735 public boolean getDisplayBoxes()
743 * @return DOCUMENT ME!
745 public int getWidth()
747 // MC This needs to get reset when characters are inserted and deleted
748 if (sequences.size() > 0)
750 width = ((SequenceI) sequences.elementAt(0)).getLength();
753 for (int i = 1; i < sequences.size(); i++)
755 SequenceI seq = (SequenceI) sequences.elementAt(i);
757 if (seq.getLength() > width)
759 width = seq.getLength();
772 public void setOutlineColour(Color c)
780 * @return DOCUMENT ME!
782 public Color getOutlineColour()
784 return outlineColour;
789 * returns the sequences in the group ordered by the ordering given by al.
790 * this used to return an array with null entries regardless, new behaviour is
791 * below. TODO: verify that this does not affect use in applet or application
795 * @return SequenceI[] intersection of sequences in group with al, ordered by
796 * al, or null if group does not intersect with al
798 public SequenceI[] getSequencesInOrder(AlignmentI al)
800 return getSequencesInOrder(al, true);
804 * return an array representing the intersection of the group with al,
805 * optionally returning an array the size of al.getHeight() where nulls mark
806 * the non-intersected sequences
810 * @return null or array
812 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
814 int sSize = sequences.size();
815 int alHeight = al.getHeight();
817 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
820 for (int i = 0; i < alHeight && index < sSize; i++)
822 if (sequences.contains(al.getSequenceAt(i)))
824 seqs[(trim) ? index : i] = al.getSequenceAt(i);
836 if (index < seqs.length)
838 SequenceI[] dummy = seqs;
839 seqs = new SequenceI[index];
842 seqs[index] = dummy[index];
850 * @return the idColour
852 public Color getIdColour()
859 * the idColour to set
861 public void setIdColour(Color idColour)
863 this.idColour = idColour;
867 * @return the representative sequence for this group
869 public SequenceI getSeqrep()
875 * set the representative sequence for this group. Note - this affects the
876 * interpretation of the Hidereps attribute.
879 * the seqrep to set (null means no sequence representative)
881 public void setSeqrep(SequenceI seqrep)
883 this.seqrep = seqrep;
888 * @return true if group has a sequence representative
890 public boolean hasSeqrep()
892 return seqrep != null;
896 * visibility of rows or represented rows covered by group
898 private boolean hidereps = false;
901 * set visibility of sequences covered by (if no sequence representative is
902 * defined) or represented by this group.
906 public void setHidereps(boolean visibility)
908 hidereps = visibility;
913 * @return true if sequences represented (or covered) by this group should be
916 public boolean isHidereps()
922 * visibility of columns intersecting this group
924 private boolean hidecols = false;
927 * set intended visibility of columns covered by this group
931 public void setHideCols(boolean visibility)
933 hidecols = visibility;
938 * @return true if columns covered by group should be hidden
940 public boolean isHideCols()
946 * create a new sequence group from the intersection of this group with an
947 * alignment Hashtable of hidden representatives
953 * @return new group containing sequences common to this group and alignment
955 public SequenceGroup intersect(AlignmentI alignment,
956 Map<SequenceI, SequenceCollectionI> map)
958 SequenceGroup sgroup = new SequenceGroup(this);
959 SequenceI[] insect = getSequencesInOrder(alignment);
960 sgroup.sequences = new Vector();
961 for (int s = 0; insect != null && s < insect.length; s++)
963 if (map == null || map.containsKey(insect[s]))
965 sgroup.sequences.addElement(insect[s]);
968 // Enumeration en =getSequences(hashtable).elements();
969 // while (en.hasMoreElements())
971 // SequenceI elem = (SequenceI) en.nextElement();
972 // if (alignment.getSequences().contains(elem))
974 // sgroup.addSequence(elem, false);
981 * @return the showUnconserved
983 public boolean getShowNonconserved()
985 return showNonconserved;
989 * @param showNonconserved
990 * the showUnconserved to set
992 public void setShowNonconserved(boolean displayNonconserved)
994 this.showNonconserved = displayNonconserved;
997 AlignmentAnnotation consensus = null, conservation = null;
1000 * flag indicating if consensus histogram should be rendered
1002 private boolean showConsensusHistogram;
1005 * set this alignmentAnnotation object as the one used to render consensus
1010 public void setConsensus(AlignmentAnnotation aan)
1012 if (consensus == null)
1020 * @return automatically calculated consensus row
1022 public AlignmentAnnotation getConsensus()
1024 // TODO get or calculate and get consensus annotation row for this group
1025 int aWidth = this.getWidth();
1033 if (consensus == null)
1035 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1036 100f, AlignmentAnnotation.BAR_GRAPH);
1038 consensus.hasText = true;
1039 consensus.autoCalculated = true;
1040 consensus.groupRef = this;
1041 consensus.label = "Consensus for " + getName();
1042 consensus.description = "Percent Identity";
1047 * set this alignmentAnnotation object as the one used to render consensus
1052 public void setConservationRow(AlignmentAnnotation aan)
1054 if (conservation == null)
1061 * get the conservation annotation row for this group
1063 * @return autoCalculated annotation row
1065 public AlignmentAnnotation getConservationRow()
1067 if (conservation == null)
1069 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1070 11f, AlignmentAnnotation.BAR_GRAPH);
1073 conservation.hasText = true;
1074 conservation.autoCalculated = true;
1075 conservation.groupRef = this;
1076 conservation.label = "Conservation for " + getName();
1077 conservation.description = "Conservation for group " + getName()
1078 + " less than " + consPercGaps + "% gaps";
1079 return conservation;
1084 * @return true if annotation rows have been instantiated for this group
1086 public boolean hasAnnotationRows()
1088 return consensus != null || conservation != null;
1091 public SequenceI getConsensusSeq()
1094 StringBuffer seqs = new StringBuffer();
1095 for (int i = 0; i < consensus.annotations.length; i++)
1097 if (consensus.annotations[i] != null)
1099 if (consensus.annotations[i].description.charAt(0) == '[')
1101 seqs.append(consensus.annotations[i].description.charAt(1));
1105 seqs.append(consensus.annotations[i].displayCharacter);
1110 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1112 sq.setDescription("Percentage Identity Consensus "
1113 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1117 public void setIgnoreGapsConsensus(boolean state)
1119 if (this.ignoreGapsInConsensus != state && consensus != null)
1121 ignoreGapsInConsensus = state;
1122 recalcConservation();
1124 ignoreGapsInConsensus = state;
1127 public boolean getIgnoreGapsConsensus()
1129 return ignoreGapsInConsensus;
1133 * @param showSequenceLogo
1134 * indicates if a sequence logo is shown for consensus annotation
1136 public void setshowSequenceLogo(boolean showSequenceLogo)
1138 // TODO: decouple calculation from settings update
1139 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1141 this.showSequenceLogo = showSequenceLogo;
1142 recalcConservation();
1144 this.showSequenceLogo = showSequenceLogo;
1149 * @param showConsHist
1150 * flag indicating if the consensus histogram for this group should
1153 public void setShowConsensusHistogram(boolean showConsHist)
1156 if (showConsensusHistogram != showConsHist && consensus != null)
1158 this.showConsensusHistogram = showConsHist;
1159 recalcConservation();
1161 this.showConsensusHistogram = showConsHist;
1165 * @return the showConsensusHistogram
1167 public boolean isShowConsensusHistogram()
1169 return showConsensusHistogram;
1173 * set flag indicating if logo should be normalised when rendered
1177 public void setNormaliseSequenceLogo(boolean norm)
1179 normaliseSequenceLogo = norm;
1182 public boolean isNormaliseSequenceLogo()
1184 return normaliseSequenceLogo;
1189 * returns a new array with all annotation involving this group
1191 public AlignmentAnnotation[] getAlignmentAnnotation()
1193 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1195 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1196 for (SequenceI seq : (Vector<SequenceI>) sequences)
1198 for (AlignmentAnnotation al : seq.getAnnotation())
1200 if (al.groupRef == this)
1206 if (consensus != null)
1208 annot.add(consensus);
1210 if (conservation != null)
1212 annot.add(conservation);
1214 return annot.toArray(new AlignmentAnnotation[0]);
1218 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1220 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1221 for (AlignmentAnnotation a : getAlignmentAnnotation())
1223 if (a.getCalcId() == calcId)