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.api.ColorI;
26 import jalview.schemes.Colour;
27 import jalview.schemes.ColourSchemeI;
28 import jalview.schemes.ResidueProperties;
30 import java.awt.Color;
31 import java.util.ArrayList;
32 import java.util.Hashtable;
33 import java.util.List;
35 import java.util.Vector;
38 * Collects a set contiguous ranges on a set of sequences
43 public class SequenceGroup implements AnnotatedCollectionI
49 Conservation conserve;
53 boolean displayBoxes = true;
55 boolean displayText = true;
57 boolean colourText = false;
60 * after Olivier's non-conserved only character display
62 boolean showNonconserved = false;
67 private List<SequenceI> sequences = new ArrayList<SequenceI>();
70 * representative sequence for this group (if any)
72 private SequenceI seqrep = null;
77 * Colourscheme applied to group if any
79 public ColourSchemeI cs;
81 // start column (base 0)
84 // end column (base 0)
87 public ColorI outlineColour = Colour.black;
89 public ColorI idColour = null;
91 public int thresholdTextColour = 0;
93 public ColorI textColour = Colour.black;
95 public ColorI textColour2 = Colour.white;
98 * consensus calculation property
100 private boolean ignoreGapsInConsensus = true;
103 * consensus calculation property
105 private boolean showSequenceLogo = false;
108 * flag indicating if logo should be rendered normalised
110 private boolean normaliseSequenceLogo;
113 * @return the includeAllConsSymbols
115 public boolean isShowSequenceLogo()
117 return showSequenceLogo;
121 * Creates a new SequenceGroup object.
123 public SequenceGroup()
125 groupName = "JGroup:" + this.hashCode();
129 * Creates a new SequenceGroup object.
134 * @param displayBoxes
138 * first column of group
140 * last column of group
142 public SequenceGroup(List<SequenceI> sequences, String groupName,
143 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
144 boolean colourText, int start, int end)
146 this.sequences = sequences;
147 this.groupName = groupName;
148 this.displayBoxes = displayBoxes;
149 this.displayText = displayText;
150 this.colourText = colourText;
154 recalcConservation();
162 public SequenceGroup(SequenceGroup seqsel)
166 sequences = new ArrayList<SequenceI>();
167 sequences.addAll(seqsel.sequences);
168 if (seqsel.groupName != null)
170 groupName = new String(seqsel.groupName);
172 displayBoxes = seqsel.displayBoxes;
173 displayText = seqsel.displayText;
174 colourText = seqsel.colourText;
175 startRes = seqsel.startRes;
176 endRes = seqsel.endRes;
178 if (seqsel.description != null)
180 description = new String(seqsel.description);
182 hidecols = seqsel.hidecols;
183 hidereps = seqsel.hidereps;
184 idColour = seqsel.idColour;
185 outlineColour = seqsel.outlineColour;
186 seqrep = seqsel.seqrep;
187 textColour = seqsel.textColour;
188 textColour2 = seqsel.textColour2;
189 thresholdTextColour = seqsel.thresholdTextColour;
190 width = seqsel.width;
191 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
192 if (seqsel.conserve != null)
194 recalcConservation(); // safer than
195 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
200 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
202 int iSize = sequences.size();
203 SequenceI[] seqs = new SequenceI[iSize];
204 SequenceI[] inorder = getSequencesInOrder(align);
206 for (int i = 0, ipos = 0; i < inorder.length; i++)
208 SequenceI seq = inorder[i];
210 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
211 if (seqs[ipos] != null)
213 seqs[ipos].setDescription(seq.getDescription());
214 seqs[ipos].setDBRefs(seq.getDBRefs());
215 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
216 if (seq.getDatasetSequence() != null)
218 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
221 if (seq.getAnnotation() != null)
223 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
224 // Only copy annotation that is either a score or referenced by the
225 // alignment's annotation vector
226 for (int a = 0; a < seq.getAnnotation().length; a++)
228 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
231 boolean found = false;
232 for (int pos = 0; pos < alann.length; pos++)
234 if (alann[pos] == tocopy)
245 AlignmentAnnotation newannot = new AlignmentAnnotation(
246 seq.getAnnotation()[a]);
247 newannot.restrict(startRes, endRes);
248 newannot.setSequenceRef(seqs[ipos]);
249 newannot.adjustForAlignment();
250 seqs[ipos].addAlignmentAnnotation(newannot);
260 if (iSize != inorder.length)
262 SequenceI[] nseqs = new SequenceI[iSize];
263 System.arraycopy(seqs, 0, nseqs, 0, iSize);
271 * If sequence ends in gaps, the end residue can be correctly calculated here
277 public int findEndRes(SequenceI seq)
282 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
284 ch = seq.getCharAt(j);
285 if (!jalview.util.Comparison.isGap((ch)))
293 eres += seq.getStart() - 1;
300 public List<SequenceI> getSequences()
306 public List<SequenceI> getSequences(
307 Map<SequenceI, SequenceCollectionI> hiddenReps)
309 if (hiddenReps == null)
311 // TODO: need a synchronizedCollection here ?
316 List<SequenceI> allSequences = new ArrayList<SequenceI>();
317 for (SequenceI seq : sequences)
319 allSequences.add(seq);
320 if (hiddenReps.containsKey(seq))
322 SequenceCollectionI hsg = hiddenReps.get(seq);
323 for (SequenceI seq2 : hsg.getSequences())
325 if (seq2 != seq && !allSequences.contains(seq2))
327 allSequences.add(seq2);
337 public SequenceI[] getSequencesAsArray(
338 Map<SequenceI, SequenceCollectionI> map)
340 List<SequenceI> tmp = getSequences(map);
345 return tmp.toArray(new SequenceI[tmp.size()]);
354 * @return DOCUMENT ME!
356 public boolean adjustForRemoveLeft(int col)
358 // return value is true if the group still exists
361 startRes = startRes - col;
366 endRes = endRes - col;
368 if (startRes > endRes)
375 // must delete this group!!
388 * @return DOCUMENT ME!
390 public boolean adjustForRemoveRight(int col)
409 * @return DOCUMENT ME!
411 public String getName()
416 public String getDescription()
427 public void setName(String name)
430 // TODO: URGENT: update dependent objects (annotation row)
433 public void setDescription(String desc)
441 * @return DOCUMENT ME!
443 public Conservation getConservation()
454 public void setConservation(Conservation c)
460 * Add s to this sequence group. If aligment sequence is already contained in
461 * group, it will not be added again, but recalculation may happen if the flag
465 * alignment sequence to be added
467 * true means Group's conservation should be recalculated
469 public void addSequence(SequenceI s, boolean recalc)
471 synchronized (sequences)
473 if (s != null && !sequences.contains(s))
480 recalcConservation();
486 * Max Gaps Threshold (percent) for performing a conservation calculation
488 private int consPercGaps = 25;
491 * @return Max Gaps Threshold for performing a conservation calculation
493 public int getConsPercGaps()
499 * set Max Gaps Threshold (percent) for performing a conservation calculation
501 * @param consPercGaps
503 public void setConsPercGaps(int consPercGaps)
505 this.consPercGaps = consPercGaps;
509 * calculate residue conservation for group - but only if necessary.
511 public void recalcConservation()
513 if (cs == null && consensus == null && conservation == null)
519 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
520 endRes + 1, showSequenceLogo);
521 if (consensus != null)
523 _updateConsensusRow(cnsns, sequences.size());
527 cs.setConsensus(cnsns);
530 if ((conservation != null)
531 || (cs != null && cs.conservationApplied()))
533 Conservation c = new Conservation(groupName,
534 ResidueProperties.propHash, 3, sequences, startRes,
537 c.verdict(false, consPercGaps);
538 if (conservation != null)
540 _updateConservationRow(c);
544 if (cs.conservationApplied())
546 cs.setConservation(c);
552 cs.alignmentChanged(context != null ? context : this, null);
554 } catch (java.lang.OutOfMemoryError err)
557 System.out.println("Out of memory loading groups: " + err);
562 private void _updateConservationRow(Conservation c)
564 if (conservation == null)
569 conservation.label = "Conservation for " + getName();
570 conservation.description = "Conservation for group " + getName()
571 + " less than " + consPercGaps + "% gaps";
572 // preserve width if already set
573 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
576 conservation.annotations = null;
577 conservation.annotations = new Annotation[aWidth]; // should be alignment
579 c.completeAnnotations(conservation, null, startRes, endRes + 1);
582 public Hashtable[] consensusData = null;
584 private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
586 if (consensus == null)
590 consensus.label = "Consensus for " + getName();
591 consensus.description = "Percent Identity";
592 consensusData = cnsns;
593 // preserve width if already set
594 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
597 consensus.annotations = null;
598 consensus.annotations = new Annotation[aWidth]; // should be alignment width
600 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
601 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
604 // ignoreGapsInConsensusCalculation);
609 * sequence to either add or remove from group
611 * flag passed to delete/addSequence to indicate if group properties
612 * should be recalculated
614 public void addOrRemove(SequenceI s, boolean recalc)
616 synchronized (sequences)
618 if (sequences.contains(s))
620 deleteSequence(s, recalc);
624 addSequence(s, recalc);
635 * true means recalculate conservation
637 public void deleteSequence(SequenceI s, boolean recalc)
639 synchronized (sequences)
645 recalcConservation();
653 * @return the first column selected by this group. Runs from 0<=i<N_cols
656 public int getStartRes()
663 * @return the groups last selected column. Runs from 0<=i<N_cols
666 public int getEndRes()
672 * Set the first column selected by this group. Runs from 0<=i<N_cols
676 public void setStartRes(int i)
682 * Set the groups last selected column. Runs from 0<=i<N_cols
686 public void setEndRes(int i)
692 * @return number of sequences in group
696 return sequences.size();
701 * @return the ith sequence
703 public SequenceI getSequenceAt(int i)
705 return sequences.get(i);
712 public void setColourText(boolean state)
720 * @return DOCUMENT ME!
722 public boolean getColourText()
733 public void setDisplayText(boolean state)
741 * @return DOCUMENT ME!
743 public boolean getDisplayText()
754 public void setDisplayBoxes(boolean state)
756 displayBoxes = state;
762 * @return DOCUMENT ME!
764 public boolean getDisplayBoxes()
770 * computes the width of current set of sequences and returns it
772 * @return DOCUMENT ME!
775 public int getWidth()
777 synchronized (sequences)
779 // MC This needs to get reset when characters are inserted and deleted
780 boolean first = true;
781 for (SequenceI seq : sequences)
783 if (first || seq.getLength() > width)
785 width = seq.getLength();
799 public void setOutlineColour(Color c)
801 outlineColour = new Colour(c);
807 * @return DOCUMENT ME!
809 public ColorI getOutlineColour()
811 return outlineColour;
816 * returns the sequences in the group ordered by the ordering given by al.
817 * this used to return an array with null entries regardless, new behaviour is
818 * below. TODO: verify that this does not affect use in applet or application
822 * @return SequenceI[] intersection of sequences in group with al, ordered by
823 * al, or null if group does not intersect with al
825 public SequenceI[] getSequencesInOrder(AlignmentI al)
827 return getSequencesInOrder(al, true);
831 * return an array representing the intersection of the group with al,
832 * optionally returning an array the size of al.getHeight() where nulls mark
833 * the non-intersected sequences
837 * @return null or array
839 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
841 synchronized (sequences)
843 int sSize = sequences.size();
844 int alHeight = al.getHeight();
846 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
849 for (int i = 0; i < alHeight && index < sSize; i++)
851 if (sequences.contains(al.getSequenceAt(i)))
853 seqs[(trim) ? index : i] = al.getSequenceAt(i);
865 if (index < seqs.length)
867 SequenceI[] dummy = seqs;
868 seqs = new SequenceI[index];
871 seqs[index] = dummy[index];
880 * @return the idColour
882 public ColorI getIdColour()
889 * the idColour to set
891 public void setIdColour(Color idColour)
893 this.idColour = new Colour(idColour);
897 * @return the representative sequence for this group
900 public SequenceI getSeqrep()
906 * set the representative sequence for this group. Note - this affects the
907 * interpretation of the Hidereps attribute.
910 * the seqrep to set (null means no sequence representative)
913 public void setSeqrep(SequenceI seqrep)
915 this.seqrep = seqrep;
920 * @return true if group has a sequence representative
923 public boolean hasSeqrep()
925 return seqrep != null;
929 * visibility of rows or represented rows covered by group
931 private boolean hidereps = false;
934 * set visibility of sequences covered by (if no sequence representative is
935 * defined) or represented by this group.
939 public void setHidereps(boolean visibility)
941 hidereps = visibility;
946 * @return true if sequences represented (or covered) by this group should be
949 public boolean isHidereps()
955 * visibility of columns intersecting this group
957 private boolean hidecols = false;
960 * set intended visibility of columns covered by this group
964 public void setHideCols(boolean visibility)
966 hidecols = visibility;
971 * @return true if columns covered by group should be hidden
973 public boolean isHideCols()
979 * create a new sequence group from the intersection of this group with an
980 * alignment Hashtable of hidden representatives
986 * @return new group containing sequences common to this group and alignment
988 public SequenceGroup intersect(AlignmentI alignment,
989 Map<SequenceI, SequenceCollectionI> map)
991 SequenceGroup sgroup = new SequenceGroup(this);
992 SequenceI[] insect = getSequencesInOrder(alignment);
993 sgroup.sequences = new ArrayList<SequenceI>();
994 for (int s = 0; insect != null && s < insect.length; s++)
996 if (map == null || map.containsKey(insect[s]))
998 sgroup.sequences.add(insect[s]);
1005 * @return the showUnconserved
1007 public boolean getShowNonconserved()
1009 return showNonconserved;
1013 * @param showNonconserved
1014 * the showUnconserved to set
1016 public void setShowNonconserved(boolean displayNonconserved)
1018 this.showNonconserved = displayNonconserved;
1021 AlignmentAnnotation consensus = null, conservation = null;
1024 * flag indicating if consensus histogram should be rendered
1026 private boolean showConsensusHistogram;
1029 * set this alignmentAnnotation object as the one used to render consensus
1034 public void setConsensus(AlignmentAnnotation aan)
1036 if (consensus == null)
1044 * @return automatically calculated consensus row
1046 public AlignmentAnnotation getConsensus()
1048 // TODO get or calculate and get consensus annotation row for this group
1049 int aWidth = this.getWidth();
1057 if (consensus == null)
1059 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1060 100f, AlignmentAnnotation.BAR_GRAPH);
1061 consensus.hasText = true;
1062 consensus.autoCalculated = true;
1063 consensus.groupRef = this;
1064 consensus.label = "Consensus for " + getName();
1065 consensus.description = "Percent Identity";
1071 * set this alignmentAnnotation object as the one used to render consensus
1076 public void setConservationRow(AlignmentAnnotation aan)
1078 if (conservation == null)
1085 * get the conservation annotation row for this group
1087 * @return autoCalculated annotation row
1089 public AlignmentAnnotation getConservationRow()
1091 if (conservation == null)
1093 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1094 11f, AlignmentAnnotation.BAR_GRAPH);
1097 conservation.hasText = true;
1098 conservation.autoCalculated = true;
1099 conservation.groupRef = this;
1100 conservation.label = "Conservation for " + getName();
1101 conservation.description = "Conservation for group " + getName()
1102 + " less than " + consPercGaps + "% gaps";
1103 return conservation;
1108 * @return true if annotation rows have been instantiated for this group
1110 public boolean hasAnnotationRows()
1112 return consensus != null || conservation != null;
1115 public SequenceI getConsensusSeq()
1118 StringBuffer seqs = new StringBuffer();
1119 for (int i = 0; i < consensus.annotations.length; i++)
1121 if (consensus.annotations[i] != null)
1123 if (consensus.annotations[i].description.charAt(0) == '[')
1125 seqs.append(consensus.annotations[i].description.charAt(1));
1129 seqs.append(consensus.annotations[i].displayCharacter);
1134 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1136 sq.setDescription("Percentage Identity Consensus "
1137 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1141 public void setIgnoreGapsConsensus(boolean state)
1143 if (this.ignoreGapsInConsensus != state && consensus != null)
1145 ignoreGapsInConsensus = state;
1146 recalcConservation();
1148 ignoreGapsInConsensus = state;
1151 public boolean getIgnoreGapsConsensus()
1153 return ignoreGapsInConsensus;
1157 * @param showSequenceLogo
1158 * indicates if a sequence logo is shown for consensus annotation
1160 public void setshowSequenceLogo(boolean showSequenceLogo)
1162 // TODO: decouple calculation from settings update
1163 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1165 this.showSequenceLogo = showSequenceLogo;
1166 recalcConservation();
1168 this.showSequenceLogo = showSequenceLogo;
1173 * @param showConsHist
1174 * flag indicating if the consensus histogram for this group should
1177 public void setShowConsensusHistogram(boolean showConsHist)
1180 if (showConsensusHistogram != showConsHist && consensus != null)
1182 this.showConsensusHistogram = showConsHist;
1183 recalcConservation();
1185 this.showConsensusHistogram = showConsHist;
1189 * @return the showConsensusHistogram
1191 public boolean isShowConsensusHistogram()
1193 return showConsensusHistogram;
1197 * set flag indicating if logo should be normalised when rendered
1201 public void setNormaliseSequenceLogo(boolean norm)
1203 normaliseSequenceLogo = norm;
1206 public boolean isNormaliseSequenceLogo()
1208 return normaliseSequenceLogo;
1213 * returns a new array with all annotation involving this group
1215 public AlignmentAnnotation[] getAlignmentAnnotation()
1217 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1219 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1220 synchronized (sequences)
1222 for (SequenceI seq : sequences)
1224 AlignmentAnnotation[] aa = seq.getAnnotation();
1227 for (AlignmentAnnotation al : aa)
1229 if (al.groupRef == this)
1236 if (consensus != null)
1238 annot.add(consensus);
1240 if (conservation != null)
1242 annot.add(conservation);
1245 return annot.toArray(new AlignmentAnnotation[0]);
1249 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1251 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1252 for (AlignmentAnnotation a : getAlignmentAnnotation())
1254 if (a.getCalcId() == calcId)
1263 * Returns a list of annotations that match the specified sequenceRef, calcId
1264 * and label, ignoring null values.
1266 * @return list of AlignmentAnnotation objects
1269 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1270 String calcId, String label)
1272 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1273 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1275 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1276 && ann.sequenceRef != null && ann.sequenceRef == seq
1277 && ann.label != null && ann.label.equals(label))
1286 * Answer true if any annotation matches the calcId passed in (if not null).
1291 public boolean hasAnnotation(String calcId)
1293 if (calcId != null && !"".equals(calcId))
1295 for (AlignmentAnnotation a : getAlignmentAnnotation())
1297 if (a.getCalcId() == calcId)
1307 * Remove all sequences from the group (leaving other properties unchanged).
1311 synchronized (sequences)
1317 private AnnotatedCollectionI context;
1320 * set the alignment or group context for this group
1324 public void setContext(AnnotatedCollectionI context)
1326 this.context = context;
1332 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1335 public AnnotatedCollectionI getContext()